blob: b9c1b476e5ba6f00cae6b399a8f8b677ca9a9298 [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*/
16
17#pragma once
18
James Feist733f7652019-11-13 14:32:29 -080019#include "Utils.hpp"
20
James Feist1df06a42019-04-11 14:23:04 -070021#include <systemd/sd-journal.h>
22
James Feist733f7652019-11-13 14:32:29 -080023#include <boost/container/flat_map.hpp>
James Feist1df06a42019-04-11 14:23:04 -070024#include <iostream>
James Feist733f7652019-11-13 14:32:29 -080025#include <list>
James Feist1a996582019-05-14 15:10:06 -070026#include <nlohmann/json.hpp>
James Feist4dc617b2020-05-01 09:54:47 -070027#include <sdbusplus/asio/object_server.hpp>
James Feist1df06a42019-04-11 14:23:04 -070028#include <string>
29
James Feist733f7652019-11-13 14:32:29 -080030using DBusProbeObjectT = boost::container::flat_map<
31 std::string,
32 std::vector<boost::container::flat_map<std::string, BasicVariantType>>>;
33
James Feist7d807752019-11-13 14:47:57 -080034using FoundDeviceT =
35 std::vector<boost::container::flat_map<std::string, BasicVariantType>>;
36
James Feist733f7652019-11-13 14:32:29 -080037struct PerformScan : std::enable_shared_from_this<PerformScan>
38{
39
40 PerformScan(nlohmann::json& systemConfiguration,
41 nlohmann::json& missingConfigurations,
42 std::list<nlohmann::json>& configurations,
James Feist4dc617b2020-05-01 09:54:47 -070043 sdbusplus::asio::object_server& objServer,
44 std::function<void()>&& callback);
James Feist733f7652019-11-13 14:32:29 -080045 void run(void);
46 virtual ~PerformScan();
47 nlohmann::json& _systemConfiguration;
48 nlohmann::json& _missingConfigurations;
49 std::list<nlohmann::json> _configurations;
James Feist4dc617b2020-05-01 09:54:47 -070050 sdbusplus::asio::object_server& objServer;
51 std::function<void()> _callback;
James Feist733f7652019-11-13 14:32:29 -080052 bool _passed = false;
53 bool powerWasOn = isPowerOn();
54 DBusProbeObjectT dbusProbeObjects;
55 std::vector<std::string> passedProbes;
56};
57
James Feist7d807752019-11-13 14:47:57 -080058// this class finds the needed dbus fields and on destruction runs the probe
59struct PerformProbe : std::enable_shared_from_this<PerformProbe>
60{
61 PerformProbe(const std::vector<std::string>& probeCommand,
62 std::shared_ptr<PerformScan>& scanPtr,
63 std::function<void(FoundDeviceT&)>&& callback);
64 virtual ~PerformProbe();
65
66 std::vector<std::string> _probeCommand;
67 std::shared_ptr<PerformScan> scan;
68 std::function<void(FoundDeviceT&)> _callback;
69};
70
James Feist1a996582019-05-14 15:10:06 -070071inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070072{
73
James Feist1ffa4a42020-04-22 18:27:17 -070074 if (!deviceHasLogging(record))
75 {
76 return;
77 }
James Feist1a996582019-05-14 15:10:06 -070078 auto findType = record.find("Type");
79 auto findAsset =
80 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
81
82 std::string model = "Unkown";
83 std::string type = "Unknown";
84 std::string sn = "Unkown";
85
86 if (findType != record.end())
87 {
88 type = findType->get<std::string>();
89 }
90 if (findAsset != record.end())
91 {
92 auto findModel = findAsset->find("Model");
93 auto findSn = findAsset->find("SerialNumber");
94 if (findModel != findAsset->end())
95 {
96 model = findModel->get<std::string>();
97 }
98 if (findSn != findAsset->end())
99 {
James Feistf5125b02019-06-06 11:27:43 -0700100 const std::string* getSn = findSn->get_ptr<const std::string*>();
101 if (getSn != nullptr)
102 {
103 sn = *getSn;
104 }
105 else
106 {
107 sn = findSn->dump();
108 }
James Feist1a996582019-05-14 15:10:06 -0700109 }
110 }
111
James Feist1df06a42019-04-11 14:23:04 -0700112 sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR,
113 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700114 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
115 type.c_str(), sn.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700116}
117
James Feist1a996582019-05-14 15:10:06 -0700118inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700119{
James Feist1ffa4a42020-04-22 18:27:17 -0700120 if (!deviceHasLogging(record))
121 {
122 return;
123 }
James Feist1a996582019-05-14 15:10:06 -0700124 auto findType = record.find("Type");
125 auto findAsset =
126 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
127
128 std::string model = "Unkown";
129 std::string type = "Unknown";
130 std::string sn = "Unkown";
131
132 if (findType != record.end())
133 {
134 type = findType->get<std::string>();
135 }
136 if (findAsset != record.end())
137 {
138 auto findModel = findAsset->find("Model");
139 auto findSn = findAsset->find("SerialNumber");
140 if (findModel != findAsset->end())
141 {
142 model = findModel->get<std::string>();
143 }
144 if (findSn != findAsset->end())
145 {
James Feistf5125b02019-06-06 11:27:43 -0700146 const std::string* getSn = findSn->get_ptr<const std::string*>();
147 if (getSn != nullptr)
148 {
149 sn = *getSn;
150 }
151 else
152 {
153 sn = findSn->dump();
154 }
James Feist1a996582019-05-14 15:10:06 -0700155 }
156 }
James Feist1df06a42019-04-11 14:23:04 -0700157
158 sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR,
159 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700160 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
161 type.c_str(), sn.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700162}