James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame^] | 19 | #include "Utils.hpp" |
| 20 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 21 | #include <systemd/sd-journal.h> |
| 22 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame^] | 23 | #include <boost/container/flat_map.hpp> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 24 | #include <iostream> |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame^] | 25 | #include <list> |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 26 | #include <nlohmann/json.hpp> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 27 | #include <string> |
| 28 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame^] | 29 | using DBusProbeObjectT = boost::container::flat_map< |
| 30 | std::string, |
| 31 | std::vector<boost::container::flat_map<std::string, BasicVariantType>>>; |
| 32 | |
| 33 | struct 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 Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 52 | inline void logDeviceAdded(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 53 | { |
| 54 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 55 | 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 Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 77 | 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 Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 89 | sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR, |
| 90 | "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 91 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
| 92 | type.c_str(), sn.c_str(), NULL); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 93 | } |
| 94 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 95 | inline void logDeviceRemoved(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 96 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 97 | 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 Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 119 | 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 Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 130 | |
| 131 | sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR, |
| 132 | "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 133 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
| 134 | type.c_str(), sn.c_str(), NULL); |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 135 | } |