blob: 2b4565bd9afe94b47105528e3cbba8e295dde85d [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 Jeffery1983d2f2022-04-05 14:55:13 +093033// The string is the path to the object hosting the interface
34using FoundDeviceT = std::vector<std::tuple<DBusInterface, std::string>>;
James Feist7d807752019-11-13 14:47:57 -080035
Andrew Jeffery47af65a2021-12-01 14:16:31 +103036struct CmpStr
37{
38 bool operator()(const char* a, const char* b) const
39 {
40 return std::strcmp(a, b) < 0;
41 }
42};
43
44// underscore T for collison with dbus c api
45enum class probe_type_codes
46{
47 FALSE_T,
48 TRUE_T,
49 AND,
50 OR,
51 FOUND,
52 MATCH_ONE
53};
54
Andrew Jeffery666583b2021-12-01 15:50:12 +103055using FoundProbeTypeT =
56 std::optional<boost::container::flat_map<const char*, probe_type_codes,
57 CmpStr>::const_iterator>;
58FoundProbeTypeT findProbeType(const std::string& probe);
59
James Feist733f7652019-11-13 14:32:29 -080060struct PerformScan : std::enable_shared_from_this<PerformScan>
61{
62
63 PerformScan(nlohmann::json& systemConfiguration,
64 nlohmann::json& missingConfigurations,
65 std::list<nlohmann::json>& configurations,
James Feist4dc617b2020-05-01 09:54:47 -070066 sdbusplus::asio::object_server& objServer,
67 std::function<void()>&& callback);
James Feist733f7652019-11-13 14:32:29 -080068 void run(void);
69 virtual ~PerformScan();
70 nlohmann::json& _systemConfiguration;
71 nlohmann::json& _missingConfigurations;
72 std::list<nlohmann::json> _configurations;
James Feist4dc617b2020-05-01 09:54:47 -070073 sdbusplus::asio::object_server& objServer;
74 std::function<void()> _callback;
James Feist733f7652019-11-13 14:32:29 -080075 bool _passed = false;
Andrew Jefferyac20bd92022-04-05 11:11:40 +093076 MapperGetSubTreeResponse dbusProbeObjects;
James Feist733f7652019-11-13 14:32:29 -080077 std::vector<std::string> passedProbes;
78};
79
James Feist7d807752019-11-13 14:47:57 -080080// this class finds the needed dbus fields and on destruction runs the probe
81struct PerformProbe : std::enable_shared_from_this<PerformProbe>
82{
Matt Spinlere789bf12021-02-24 12:33:01 -060083 PerformProbe(
84 const std::vector<std::string>& probeCommand,
85 std::shared_ptr<PerformScan>& scanPtr,
Andrew Jefferyac20bd92022-04-05 11:11:40 +093086 std::function<void(FoundDeviceT&, const MapperGetSubTreeResponse&)>&&
87 callback);
James Feist7d807752019-11-13 14:47:57 -080088 virtual ~PerformProbe();
89
90 std::vector<std::string> _probeCommand;
91 std::shared_ptr<PerformScan> scan;
Andrew Jefferyac20bd92022-04-05 11:11:40 +093092 std::function<void(FoundDeviceT&, const MapperGetSubTreeResponse&)>
93 _callback;
James Feist7d807752019-11-13 14:47:57 -080094};
95
James Feist1a996582019-05-14 15:10:06 -070096inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070097{
98
James Feist1ffa4a42020-04-22 18:27:17 -070099 if (!deviceHasLogging(record))
100 {
101 return;
102 }
James Feist1a996582019-05-14 15:10:06 -0700103 auto findType = record.find("Type");
104 auto findAsset =
105 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
106
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300107 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700108 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300109 std::string sn = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700110
111 if (findType != record.end())
112 {
113 type = findType->get<std::string>();
114 }
115 if (findAsset != record.end())
116 {
117 auto findModel = findAsset->find("Model");
118 auto findSn = findAsset->find("SerialNumber");
119 if (findModel != findAsset->end())
120 {
121 model = findModel->get<std::string>();
122 }
123 if (findSn != findAsset->end())
124 {
James Feistf5125b02019-06-06 11:27:43 -0700125 const std::string* getSn = findSn->get_ptr<const std::string*>();
126 if (getSn != nullptr)
127 {
128 sn = *getSn;
129 }
130 else
131 {
132 sn = findSn->dump();
133 }
James Feist1a996582019-05-14 15:10:06 -0700134 }
135 }
136
Konstantin Aladysheve115c782022-01-11 18:56:31 +0300137 sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_INFO,
James Feist1df06a42019-04-11 14:23:04 -0700138 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700139 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
140 type.c_str(), sn.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700141}
142
James Feist1a996582019-05-14 15:10:06 -0700143inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700144{
James Feist1ffa4a42020-04-22 18:27:17 -0700145 if (!deviceHasLogging(record))
146 {
147 return;
148 }
James Feist1a996582019-05-14 15:10:06 -0700149 auto findType = record.find("Type");
150 auto findAsset =
151 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
152
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300153 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700154 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300155 std::string sn = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700156
157 if (findType != record.end())
158 {
159 type = findType->get<std::string>();
160 }
161 if (findAsset != record.end())
162 {
163 auto findModel = findAsset->find("Model");
164 auto findSn = findAsset->find("SerialNumber");
165 if (findModel != findAsset->end())
166 {
167 model = findModel->get<std::string>();
168 }
169 if (findSn != findAsset->end())
170 {
James Feistf5125b02019-06-06 11:27:43 -0700171 const std::string* getSn = findSn->get_ptr<const std::string*>();
172 if (getSn != nullptr)
173 {
174 sn = *getSn;
175 }
176 else
177 {
178 sn = findSn->dump();
179 }
James Feist1a996582019-05-14 15:10:06 -0700180 }
181 }
James Feist1df06a42019-04-11 14:23:04 -0700182
Konstantin Aladysheve115c782022-01-11 18:56:31 +0300183 sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_INFO,
James Feist1df06a42019-04-11 14:23:04 -0700184 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700185 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
186 type.c_str(), sn.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700187}