blob: eb8a86df68ceb21aba1e0749f6ec8a8b0b1c8e36 [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
Matt Spinlere789bf12021-02-24 12:33:01 -060033// paths - > interfaces -> properties
Andrew Jefferyac20bd92022-04-05 11:11:40 +093034using MapperGetSubTreeResponse = boost::container::flat_map<
James Feist733f7652019-11-13 14:32:29 -080035 std::string,
Matt Spinlere789bf12021-02-24 12:33:01 -060036 boost::container::flat_map<
37 std::string,
Andrew Jefferyeab49292022-04-05 14:42:20 +093038 boost::container::flat_map<std::string, DBusValueVariant>>>;
James Feist733f7652019-11-13 14:32:29 -080039
Matt Spinlere789bf12021-02-24 12:33:01 -060040// vector of tuple<map<propertyName, variant>, D-Bus path>>
41using FoundDeviceT = std::vector<std::tuple<
Andrew Jefferyeab49292022-04-05 14:42:20 +093042 boost::container::flat_map<std::string, DBusValueVariant>, std::string>>;
James Feist7d807752019-11-13 14:47:57 -080043
Andrew Jeffery47af65a2021-12-01 14:16:31 +103044struct CmpStr
45{
46 bool operator()(const char* a, const char* b) const
47 {
48 return std::strcmp(a, b) < 0;
49 }
50};
51
52// underscore T for collison with dbus c api
53enum class probe_type_codes
54{
55 FALSE_T,
56 TRUE_T,
57 AND,
58 OR,
59 FOUND,
60 MATCH_ONE
61};
62
Andrew Jeffery666583b2021-12-01 15:50:12 +103063using FoundProbeTypeT =
64 std::optional<boost::container::flat_map<const char*, probe_type_codes,
65 CmpStr>::const_iterator>;
66FoundProbeTypeT findProbeType(const std::string& probe);
67
James Feist733f7652019-11-13 14:32:29 -080068struct PerformScan : std::enable_shared_from_this<PerformScan>
69{
70
71 PerformScan(nlohmann::json& systemConfiguration,
72 nlohmann::json& missingConfigurations,
73 std::list<nlohmann::json>& configurations,
James Feist4dc617b2020-05-01 09:54:47 -070074 sdbusplus::asio::object_server& objServer,
75 std::function<void()>&& callback);
James Feist733f7652019-11-13 14:32:29 -080076 void run(void);
77 virtual ~PerformScan();
78 nlohmann::json& _systemConfiguration;
79 nlohmann::json& _missingConfigurations;
80 std::list<nlohmann::json> _configurations;
James Feist4dc617b2020-05-01 09:54:47 -070081 sdbusplus::asio::object_server& objServer;
82 std::function<void()> _callback;
James Feist733f7652019-11-13 14:32:29 -080083 bool _passed = false;
Andrew Jefferyac20bd92022-04-05 11:11:40 +093084 MapperGetSubTreeResponse dbusProbeObjects;
James Feist733f7652019-11-13 14:32:29 -080085 std::vector<std::string> passedProbes;
86};
87
James Feist7d807752019-11-13 14:47:57 -080088// this class finds the needed dbus fields and on destruction runs the probe
89struct PerformProbe : std::enable_shared_from_this<PerformProbe>
90{
Matt Spinlere789bf12021-02-24 12:33:01 -060091 PerformProbe(
92 const std::vector<std::string>& probeCommand,
93 std::shared_ptr<PerformScan>& scanPtr,
Andrew Jefferyac20bd92022-04-05 11:11:40 +093094 std::function<void(FoundDeviceT&, const MapperGetSubTreeResponse&)>&&
95 callback);
James Feist7d807752019-11-13 14:47:57 -080096 virtual ~PerformProbe();
97
98 std::vector<std::string> _probeCommand;
99 std::shared_ptr<PerformScan> scan;
Andrew Jefferyac20bd92022-04-05 11:11:40 +0930100 std::function<void(FoundDeviceT&, const MapperGetSubTreeResponse&)>
101 _callback;
James Feist7d807752019-11-13 14:47:57 -0800102};
103
James Feist1a996582019-05-14 15:10:06 -0700104inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700105{
106
James Feist1ffa4a42020-04-22 18:27:17 -0700107 if (!deviceHasLogging(record))
108 {
109 return;
110 }
James Feist1a996582019-05-14 15:10:06 -0700111 auto findType = record.find("Type");
112 auto findAsset =
113 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
114
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300115 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700116 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300117 std::string sn = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700118
119 if (findType != record.end())
120 {
121 type = findType->get<std::string>();
122 }
123 if (findAsset != record.end())
124 {
125 auto findModel = findAsset->find("Model");
126 auto findSn = findAsset->find("SerialNumber");
127 if (findModel != findAsset->end())
128 {
129 model = findModel->get<std::string>();
130 }
131 if (findSn != findAsset->end())
132 {
James Feistf5125b02019-06-06 11:27:43 -0700133 const std::string* getSn = findSn->get_ptr<const std::string*>();
134 if (getSn != nullptr)
135 {
136 sn = *getSn;
137 }
138 else
139 {
140 sn = findSn->dump();
141 }
James Feist1a996582019-05-14 15:10:06 -0700142 }
143 }
144
Konstantin Aladysheve115c782022-01-11 18:56:31 +0300145 sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_INFO,
James Feist1df06a42019-04-11 14:23:04 -0700146 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700147 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
148 type.c_str(), sn.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700149}
150
James Feist1a996582019-05-14 15:10:06 -0700151inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700152{
James Feist1ffa4a42020-04-22 18:27:17 -0700153 if (!deviceHasLogging(record))
154 {
155 return;
156 }
James Feist1a996582019-05-14 15:10:06 -0700157 auto findType = record.find("Type");
158 auto findAsset =
159 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
160
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300161 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700162 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300163 std::string sn = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700164
165 if (findType != record.end())
166 {
167 type = findType->get<std::string>();
168 }
169 if (findAsset != record.end())
170 {
171 auto findModel = findAsset->find("Model");
172 auto findSn = findAsset->find("SerialNumber");
173 if (findModel != findAsset->end())
174 {
175 model = findModel->get<std::string>();
176 }
177 if (findSn != findAsset->end())
178 {
James Feistf5125b02019-06-06 11:27:43 -0700179 const std::string* getSn = findSn->get_ptr<const std::string*>();
180 if (getSn != nullptr)
181 {
182 sn = *getSn;
183 }
184 else
185 {
186 sn = findSn->dump();
187 }
James Feist1a996582019-05-14 15:10:06 -0700188 }
189 }
James Feist1df06a42019-04-11 14:23:04 -0700190
Konstantin Aladysheve115c782022-01-11 18:56:31 +0300191 sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_INFO,
James Feist1df06a42019-04-11 14:23:04 -0700192 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700193 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
194 type.c_str(), sn.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700195}