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 | |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame^] | 33 | using FoundDeviceT = |
| 34 | std::vector<boost::container::flat_map<std::string, BasicVariantType>>; |
| 35 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 36 | struct 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 Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame^] | 55 | // this class finds the needed dbus fields and on destruction runs the probe |
| 56 | struct 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 Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 68 | inline void logDeviceAdded(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 69 | { |
| 70 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 71 | 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 Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 93 | 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 Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 105 | sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR, |
| 106 | "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 107 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
| 108 | type.c_str(), sn.c_str(), NULL); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 109 | } |
| 110 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 111 | inline void logDeviceRemoved(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 112 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 113 | 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 Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 135 | 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 Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 146 | |
| 147 | sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR, |
| 148 | "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 149 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
| 150 | type.c_str(), sn.c_str(), NULL); |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 151 | } |