blob: 524fa5d59c24d7b83ab5a03c378d003b6aafcd26 [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 Feist1a996582019-05-14 15:10:06 -070071 auto findType = record.find("Type");
72 auto findAsset =
73 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
74
75 std::string model = "Unkown";
76 std::string type = "Unknown";
77 std::string sn = "Unkown";
78
79 if (findType != record.end())
80 {
81 type = findType->get<std::string>();
82 }
83 if (findAsset != record.end())
84 {
85 auto findModel = findAsset->find("Model");
86 auto findSn = findAsset->find("SerialNumber");
87 if (findModel != findAsset->end())
88 {
89 model = findModel->get<std::string>();
90 }
91 if (findSn != findAsset->end())
92 {
James Feistf5125b02019-06-06 11:27:43 -070093 const std::string* getSn = findSn->get_ptr<const std::string*>();
94 if (getSn != nullptr)
95 {
96 sn = *getSn;
97 }
98 else
99 {
100 sn = findSn->dump();
101 }
James Feist1a996582019-05-14 15:10:06 -0700102 }
103 }
104
James Feist1df06a42019-04-11 14:23:04 -0700105 sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR,
106 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700107 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
108 type.c_str(), sn.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700109}
110
James Feist1a996582019-05-14 15:10:06 -0700111inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700112{
James Feist1a996582019-05-14 15:10:06 -0700113 auto findType = record.find("Type");
114 auto findAsset =
115 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
116
117 std::string model = "Unkown";
118 std::string type = "Unknown";
119 std::string sn = "Unkown";
120
121 if (findType != record.end())
122 {
123 type = findType->get<std::string>();
124 }
125 if (findAsset != record.end())
126 {
127 auto findModel = findAsset->find("Model");
128 auto findSn = findAsset->find("SerialNumber");
129 if (findModel != findAsset->end())
130 {
131 model = findModel->get<std::string>();
132 }
133 if (findSn != findAsset->end())
134 {
James Feistf5125b02019-06-06 11:27:43 -0700135 const std::string* getSn = findSn->get_ptr<const std::string*>();
136 if (getSn != nullptr)
137 {
138 sn = *getSn;
139 }
140 else
141 {
142 sn = findSn->dump();
143 }
James Feist1a996582019-05-14 15:10:06 -0700144 }
145 }
James Feist1df06a42019-04-11 14:23:04 -0700146
147 sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR,
148 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700149 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
150 type.c_str(), sn.c_str(), NULL);
James Feist481c5d52019-08-13 14:40:40 -0700151}