blob: 1b247d58c1db6142576164cda75a10a9d285e548 [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 Feist1df06a42019-04-11 14:23:04 -070027#include <string>
28
James Feist733f7652019-11-13 14:32:29 -080029using DBusProbeObjectT = boost::container::flat_map<
30 std::string,
31 std::vector<boost::container::flat_map<std::string, BasicVariantType>>>;
32
James Feist7d807752019-11-13 14:47:57 -080033using FoundDeviceT =
34 std::vector<boost::container::flat_map<std::string, BasicVariantType>>;
35
James Feist733f7652019-11-13 14:32:29 -080036struct PerformScan : std::enable_shared_from_this<PerformScan>
37{
38
39 PerformScan(nlohmann::json& systemConfiguration,
40 nlohmann::json& missingConfigurations,
41 std::list<nlohmann::json>& configurations,
42 std::function<void(const DBusProbeObjectT&)>&& callback);
43 void run(void);
44 virtual ~PerformScan();
45 nlohmann::json& _systemConfiguration;
46 nlohmann::json& _missingConfigurations;
47 std::list<nlohmann::json> _configurations;
48 std::function<void(const DBusProbeObjectT&)> _callback;
49 bool _passed = false;
50 bool powerWasOn = isPowerOn();
51 DBusProbeObjectT dbusProbeObjects;
52 std::vector<std::string> passedProbes;
53};
54
James Feist7d807752019-11-13 14:47:57 -080055// this class finds the needed dbus fields and on destruction runs the probe
56struct PerformProbe : std::enable_shared_from_this<PerformProbe>
57{
58 PerformProbe(const std::vector<std::string>& probeCommand,
59 std::shared_ptr<PerformScan>& scanPtr,
60 std::function<void(FoundDeviceT&)>&& callback);
61 virtual ~PerformProbe();
62
63 std::vector<std::string> _probeCommand;
64 std::shared_ptr<PerformScan> scan;
65 std::function<void(FoundDeviceT&)> _callback;
66};
67
James Feist1a996582019-05-14 15:10:06 -070068inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070069{
70
James Feist1ffa4a42020-04-22 18:27:17 -070071 if (!deviceHasLogging(record))
72 {
73 return;
74 }
James Feist1a996582019-05-14 15:10:06 -070075 auto findType = record.find("Type");
76 auto findAsset =
77 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
78
79 std::string model = "Unkown";
80 std::string type = "Unknown";
81 std::string sn = "Unkown";
82
83 if (findType != record.end())
84 {
85 type = findType->get<std::string>();
86 }
87 if (findAsset != record.end())
88 {
89 auto findModel = findAsset->find("Model");
90 auto findSn = findAsset->find("SerialNumber");
91 if (findModel != findAsset->end())
92 {
93 model = findModel->get<std::string>();
94 }
95 if (findSn != findAsset->end())
96 {
James Feistf5125b02019-06-06 11:27:43 -070097 const std::string* getSn = findSn->get_ptr<const std::string*>();
98 if (getSn != nullptr)
99 {
100 sn = *getSn;
101 }
102 else
103 {
104 sn = findSn->dump();
105 }
James Feist1a996582019-05-14 15:10:06 -0700106 }
107 }
108
James Feist1df06a42019-04-11 14:23:04 -0700109 sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR,
110 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700111 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
112 type.c_str(), sn.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700113}
114
James Feist1a996582019-05-14 15:10:06 -0700115inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700116{
James Feist1ffa4a42020-04-22 18:27:17 -0700117 if (!deviceHasLogging(record))
118 {
119 return;
120 }
James Feist1a996582019-05-14 15:10:06 -0700121 auto findType = record.find("Type");
122 auto findAsset =
123 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
124
125 std::string model = "Unkown";
126 std::string type = "Unknown";
127 std::string sn = "Unkown";
128
129 if (findType != record.end())
130 {
131 type = findType->get<std::string>();
132 }
133 if (findAsset != record.end())
134 {
135 auto findModel = findAsset->find("Model");
136 auto findSn = findAsset->find("SerialNumber");
137 if (findModel != findAsset->end())
138 {
139 model = findModel->get<std::string>();
140 }
141 if (findSn != findAsset->end())
142 {
James Feistf5125b02019-06-06 11:27:43 -0700143 const std::string* getSn = findSn->get_ptr<const std::string*>();
144 if (getSn != nullptr)
145 {
146 sn = *getSn;
147 }
148 else
149 {
150 sn = findSn->dump();
151 }
James Feist1a996582019-05-14 15:10:06 -0700152 }
153 }
James Feist1df06a42019-04-11 14:23:04 -0700154
155 sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR,
156 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700157 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
158 type.c_str(), sn.c_str(), NULL);
James Feist481c5d52019-08-13 14:40:40 -0700159}