blob: 748c49baaabc4133c843141c78a64850b2e3485d [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>
James Feist1df06a42019-04-11 14:23:04 -070030#include <string>
31
Matt Spinlere789bf12021-02-24 12:33:01 -060032// paths - > interfaces -> properties
James Feist733f7652019-11-13 14:32:29 -080033using DBusProbeObjectT = boost::container::flat_map<
34 std::string,
Matt Spinlere789bf12021-02-24 12:33:01 -060035 boost::container::flat_map<
36 std::string,
37 boost::container::flat_map<std::string, BasicVariantType>>>;
James Feist733f7652019-11-13 14:32:29 -080038
Matt Spinlere789bf12021-02-24 12:33:01 -060039// vector of tuple<map<propertyName, variant>, D-Bus path>>
40using FoundDeviceT = std::vector<std::tuple<
41 boost::container::flat_map<std::string, BasicVariantType>, std::string>>;
James Feist7d807752019-11-13 14:47:57 -080042
James Feist733f7652019-11-13 14:32:29 -080043struct PerformScan : std::enable_shared_from_this<PerformScan>
44{
45
46 PerformScan(nlohmann::json& systemConfiguration,
47 nlohmann::json& missingConfigurations,
48 std::list<nlohmann::json>& configurations,
James Feist4dc617b2020-05-01 09:54:47 -070049 sdbusplus::asio::object_server& objServer,
50 std::function<void()>&& callback);
James Feist733f7652019-11-13 14:32:29 -080051 void run(void);
52 virtual ~PerformScan();
53 nlohmann::json& _systemConfiguration;
54 nlohmann::json& _missingConfigurations;
55 std::list<nlohmann::json> _configurations;
James Feist4dc617b2020-05-01 09:54:47 -070056 sdbusplus::asio::object_server& objServer;
57 std::function<void()> _callback;
James Feist733f7652019-11-13 14:32:29 -080058 bool _passed = false;
59 bool powerWasOn = isPowerOn();
60 DBusProbeObjectT dbusProbeObjects;
61 std::vector<std::string> passedProbes;
62};
63
James Feist7d807752019-11-13 14:47:57 -080064// this class finds the needed dbus fields and on destruction runs the probe
65struct PerformProbe : std::enable_shared_from_this<PerformProbe>
66{
Matt Spinlere789bf12021-02-24 12:33:01 -060067 PerformProbe(
68 const std::vector<std::string>& probeCommand,
69 std::shared_ptr<PerformScan>& scanPtr,
70 std::function<void(FoundDeviceT&, const DBusProbeObjectT&)>&& callback);
James Feist7d807752019-11-13 14:47:57 -080071 virtual ~PerformProbe();
72
73 std::vector<std::string> _probeCommand;
74 std::shared_ptr<PerformScan> scan;
Matt Spinlere789bf12021-02-24 12:33:01 -060075 std::function<void(FoundDeviceT&, const DBusProbeObjectT&)> _callback;
James Feist7d807752019-11-13 14:47:57 -080076};
77
James Feist1a996582019-05-14 15:10:06 -070078inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070079{
80
James Feist1ffa4a42020-04-22 18:27:17 -070081 if (!deviceHasLogging(record))
82 {
83 return;
84 }
James Feist1a996582019-05-14 15:10:06 -070085 auto findType = record.find("Type");
86 auto findAsset =
87 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
88
89 std::string model = "Unkown";
90 std::string type = "Unknown";
91 std::string sn = "Unkown";
92
93 if (findType != record.end())
94 {
95 type = findType->get<std::string>();
96 }
97 if (findAsset != record.end())
98 {
99 auto findModel = findAsset->find("Model");
100 auto findSn = findAsset->find("SerialNumber");
101 if (findModel != findAsset->end())
102 {
103 model = findModel->get<std::string>();
104 }
105 if (findSn != findAsset->end())
106 {
James Feistf5125b02019-06-06 11:27:43 -0700107 const std::string* getSn = findSn->get_ptr<const std::string*>();
108 if (getSn != nullptr)
109 {
110 sn = *getSn;
111 }
112 else
113 {
114 sn = findSn->dump();
115 }
James Feist1a996582019-05-14 15:10:06 -0700116 }
117 }
118
James Feist1df06a42019-04-11 14:23:04 -0700119 sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR,
120 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700121 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
122 type.c_str(), sn.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700123}
124
James Feist1a996582019-05-14 15:10:06 -0700125inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700126{
James Feist1ffa4a42020-04-22 18:27:17 -0700127 if (!deviceHasLogging(record))
128 {
129 return;
130 }
James Feist1a996582019-05-14 15:10:06 -0700131 auto findType = record.find("Type");
132 auto findAsset =
133 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
134
135 std::string model = "Unkown";
136 std::string type = "Unknown";
137 std::string sn = "Unkown";
138
139 if (findType != record.end())
140 {
141 type = findType->get<std::string>();
142 }
143 if (findAsset != record.end())
144 {
145 auto findModel = findAsset->find("Model");
146 auto findSn = findAsset->find("SerialNumber");
147 if (findModel != findAsset->end())
148 {
149 model = findModel->get<std::string>();
150 }
151 if (findSn != findAsset->end())
152 {
James Feistf5125b02019-06-06 11:27:43 -0700153 const std::string* getSn = findSn->get_ptr<const std::string*>();
154 if (getSn != nullptr)
155 {
156 sn = *getSn;
157 }
158 else
159 {
160 sn = findSn->dump();
161 }
James Feist1a996582019-05-14 15:10:06 -0700162 }
163 }
James Feist1df06a42019-04-11 14:23:04 -0700164
165 sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR,
166 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700167 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
168 type.c_str(), sn.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700169}