blob: 781d3a18c26000345449b2a3ce1d37770d02af54 [file] [log] [blame]
James Feist1df06a42019-04-11 14:23:04 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
Brad Bishop1fb9f3f2020-08-28 08:15:13 -040016/// \file EntityManager.hpp
James Feist1df06a42019-04-11 14:23:04 -070017
18#pragma once
19
James Feist733f7652019-11-13 14:32:29 -080020#include "Utils.hpp"
21
James Feist1df06a42019-04-11 14:23:04 -070022#include <systemd/sd-journal.h>
23
James Feist733f7652019-11-13 14:32:29 -080024#include <boost/container/flat_map.hpp>
James Feist1a996582019-05-14 15:10:06 -070025#include <nlohmann/json.hpp>
James Feist4dc617b2020-05-01 09:54:47 -070026#include <sdbusplus/asio/object_server.hpp>
James Feist8c505da2020-05-28 10:06:33 -070027
28#include <iostream>
29#include <list>
Andrew Jeffery666583b2021-12-01 15:50:12 +103030#include <optional>
James Feist1df06a42019-04-11 14:23:04 -070031#include <string>
32
Andrew Jefferyf5772d22022-04-12 22:05:30 +093033struct DBusDeviceDescriptor
34{
35 DBusInterface interface;
36 std::string path;
37};
38
39using FoundDevices = std::vector<DBusDeviceDescriptor>;
James Feist7d807752019-11-13 14:47:57 -080040
Andrew Jeffery47af65a2021-12-01 14:16:31 +103041struct CmpStr
42{
43 bool operator()(const char* a, const char* b) const
44 {
45 return std::strcmp(a, b) < 0;
46 }
47};
48
49// underscore T for collison with dbus c api
50enum class probe_type_codes
51{
52 FALSE_T,
53 TRUE_T,
54 AND,
55 OR,
56 FOUND,
57 MATCH_ONE
58};
59
Andrew Jeffery666583b2021-12-01 15:50:12 +103060using FoundProbeTypeT =
61 std::optional<boost::container::flat_map<const char*, probe_type_codes,
62 CmpStr>::const_iterator>;
63FoundProbeTypeT findProbeType(const std::string& probe);
64
James Feist733f7652019-11-13 14:32:29 -080065struct PerformScan : std::enable_shared_from_this<PerformScan>
66{
67
68 PerformScan(nlohmann::json& systemConfiguration,
69 nlohmann::json& missingConfigurations,
70 std::list<nlohmann::json>& configurations,
James Feist4dc617b2020-05-01 09:54:47 -070071 sdbusplus::asio::object_server& objServer,
72 std::function<void()>&& callback);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +093073 void updateSystemConfiguration(const nlohmann::json& recordRef,
74 const std::string& probeName,
75 FoundDevices& foundDevices,
76 const MapperGetSubTreeResponse& dbusSubtree);
James Feist733f7652019-11-13 14:32:29 -080077 void run(void);
78 virtual ~PerformScan();
79 nlohmann::json& _systemConfiguration;
80 nlohmann::json& _missingConfigurations;
81 std::list<nlohmann::json> _configurations;
James Feist4dc617b2020-05-01 09:54:47 -070082 sdbusplus::asio::object_server& objServer;
83 std::function<void()> _callback;
James Feist733f7652019-11-13 14:32:29 -080084 bool _passed = false;
Andrew Jefferyac20bd92022-04-05 11:11:40 +093085 MapperGetSubTreeResponse dbusProbeObjects;
James Feist733f7652019-11-13 14:32:29 -080086 std::vector<std::string> passedProbes;
87};
88
James Feist7d807752019-11-13 14:47:57 -080089// this class finds the needed dbus fields and on destruction runs the probe
90struct PerformProbe : std::enable_shared_from_this<PerformProbe>
91{
Matt Spinlere789bf12021-02-24 12:33:01 -060092 PerformProbe(
93 const std::vector<std::string>& probeCommand,
94 std::shared_ptr<PerformScan>& scanPtr,
Andrew Jefferyf5772d22022-04-12 22:05:30 +093095 std::function<void(FoundDevices&, const MapperGetSubTreeResponse&)>&&
Andrew Jefferyac20bd92022-04-05 11:11:40 +093096 callback);
James Feist7d807752019-11-13 14:47:57 -080097 virtual ~PerformProbe();
98
99 std::vector<std::string> _probeCommand;
100 std::shared_ptr<PerformScan> scan;
Andrew Jefferyf5772d22022-04-12 22:05:30 +0930101 std::function<void(FoundDevices&, const MapperGetSubTreeResponse&)>
Andrew Jefferyac20bd92022-04-05 11:11:40 +0930102 _callback;
James Feist7d807752019-11-13 14:47:57 -0800103};
104
James Feist1a996582019-05-14 15:10:06 -0700105inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700106{
107
James Feist1ffa4a42020-04-22 18:27:17 -0700108 if (!deviceHasLogging(record))
109 {
110 return;
111 }
James Feist1a996582019-05-14 15:10:06 -0700112 auto findType = record.find("Type");
113 auto findAsset =
114 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
115
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300116 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700117 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300118 std::string sn = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700119
120 if (findType != record.end())
121 {
122 type = findType->get<std::string>();
123 }
124 if (findAsset != record.end())
125 {
126 auto findModel = findAsset->find("Model");
127 auto findSn = findAsset->find("SerialNumber");
128 if (findModel != findAsset->end())
129 {
130 model = findModel->get<std::string>();
131 }
132 if (findSn != findAsset->end())
133 {
James Feistf5125b02019-06-06 11:27:43 -0700134 const std::string* getSn = findSn->get_ptr<const std::string*>();
135 if (getSn != nullptr)
136 {
137 sn = *getSn;
138 }
139 else
140 {
141 sn = findSn->dump();
142 }
James Feist1a996582019-05-14 15:10:06 -0700143 }
144 }
145
Konstantin Aladysheve115c782022-01-11 18:56:31 +0300146 sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_INFO,
James Feist1df06a42019-04-11 14:23:04 -0700147 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700148 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
149 type.c_str(), sn.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700150}
151
James Feist1a996582019-05-14 15:10:06 -0700152inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700153{
James Feist1ffa4a42020-04-22 18:27:17 -0700154 if (!deviceHasLogging(record))
155 {
156 return;
157 }
James Feist1a996582019-05-14 15:10:06 -0700158 auto findType = record.find("Type");
159 auto findAsset =
160 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
161
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300162 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700163 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300164 std::string sn = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700165
166 if (findType != record.end())
167 {
168 type = findType->get<std::string>();
169 }
170 if (findAsset != record.end())
171 {
172 auto findModel = findAsset->find("Model");
173 auto findSn = findAsset->find("SerialNumber");
174 if (findModel != findAsset->end())
175 {
176 model = findModel->get<std::string>();
177 }
178 if (findSn != findAsset->end())
179 {
James Feistf5125b02019-06-06 11:27:43 -0700180 const std::string* getSn = findSn->get_ptr<const std::string*>();
181 if (getSn != nullptr)
182 {
183 sn = *getSn;
184 }
185 else
186 {
187 sn = findSn->dump();
188 }
James Feist1a996582019-05-14 15:10:06 -0700189 }
190 }
James Feist1df06a42019-04-11 14:23:04 -0700191
Konstantin Aladysheve115c782022-01-11 18:56:31 +0300192 sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_INFO,
James Feist1df06a42019-04-11 14:23:04 -0700193 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700194 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
195 type.c_str(), sn.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700196}