blob: ae120140a16dba764ad855781cec2f3d37e48c4c [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
33struct PerformScan : std::enable_shared_from_this<PerformScan>
34{
35
36 PerformScan(nlohmann::json& systemConfiguration,
37 nlohmann::json& missingConfigurations,
38 std::list<nlohmann::json>& configurations,
39 std::function<void(const DBusProbeObjectT&)>&& callback);
40 void run(void);
41 virtual ~PerformScan();
42 nlohmann::json& _systemConfiguration;
43 nlohmann::json& _missingConfigurations;
44 std::list<nlohmann::json> _configurations;
45 std::function<void(const DBusProbeObjectT&)> _callback;
46 bool _passed = false;
47 bool powerWasOn = isPowerOn();
48 DBusProbeObjectT dbusProbeObjects;
49 std::vector<std::string> passedProbes;
50};
51
James Feist1a996582019-05-14 15:10:06 -070052inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070053{
54
James Feist1a996582019-05-14 15:10:06 -070055 auto findType = record.find("Type");
56 auto findAsset =
57 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
58
59 std::string model = "Unkown";
60 std::string type = "Unknown";
61 std::string sn = "Unkown";
62
63 if (findType != record.end())
64 {
65 type = findType->get<std::string>();
66 }
67 if (findAsset != record.end())
68 {
69 auto findModel = findAsset->find("Model");
70 auto findSn = findAsset->find("SerialNumber");
71 if (findModel != findAsset->end())
72 {
73 model = findModel->get<std::string>();
74 }
75 if (findSn != findAsset->end())
76 {
James Feistf5125b02019-06-06 11:27:43 -070077 const std::string* getSn = findSn->get_ptr<const std::string*>();
78 if (getSn != nullptr)
79 {
80 sn = *getSn;
81 }
82 else
83 {
84 sn = findSn->dump();
85 }
James Feist1a996582019-05-14 15:10:06 -070086 }
87 }
88
James Feist1df06a42019-04-11 14:23:04 -070089 sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR,
90 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -070091 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
92 type.c_str(), sn.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -070093}
94
James Feist1a996582019-05-14 15:10:06 -070095inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070096{
James Feist1a996582019-05-14 15:10:06 -070097 auto findType = record.find("Type");
98 auto findAsset =
99 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
100
101 std::string model = "Unkown";
102 std::string type = "Unknown";
103 std::string sn = "Unkown";
104
105 if (findType != record.end())
106 {
107 type = findType->get<std::string>();
108 }
109 if (findAsset != record.end())
110 {
111 auto findModel = findAsset->find("Model");
112 auto findSn = findAsset->find("SerialNumber");
113 if (findModel != findAsset->end())
114 {
115 model = findModel->get<std::string>();
116 }
117 if (findSn != findAsset->end())
118 {
James Feistf5125b02019-06-06 11:27:43 -0700119 const std::string* getSn = findSn->get_ptr<const std::string*>();
120 if (getSn != nullptr)
121 {
122 sn = *getSn;
123 }
124 else
125 {
126 sn = findSn->dump();
127 }
James Feist1a996582019-05-14 15:10:06 -0700128 }
129 }
James Feist1df06a42019-04-11 14:23:04 -0700130
131 sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR,
132 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700133 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
134 type.c_str(), sn.c_str(), NULL);
James Feist481c5d52019-08-13 14:40:40 -0700135}