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 | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame^] | 27 | #include <sdbusplus/asio/object_server.hpp> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 28 | #include <string> |
| 29 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 30 | using DBusProbeObjectT = boost::container::flat_map< |
| 31 | std::string, |
| 32 | std::vector<boost::container::flat_map<std::string, BasicVariantType>>>; |
| 33 | |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 34 | using FoundDeviceT = |
| 35 | std::vector<boost::container::flat_map<std::string, BasicVariantType>>; |
| 36 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 37 | struct PerformScan : std::enable_shared_from_this<PerformScan> |
| 38 | { |
| 39 | |
| 40 | PerformScan(nlohmann::json& systemConfiguration, |
| 41 | nlohmann::json& missingConfigurations, |
| 42 | std::list<nlohmann::json>& configurations, |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame^] | 43 | sdbusplus::asio::object_server& objServer, |
| 44 | std::function<void()>&& callback); |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 45 | void run(void); |
| 46 | virtual ~PerformScan(); |
| 47 | nlohmann::json& _systemConfiguration; |
| 48 | nlohmann::json& _missingConfigurations; |
| 49 | std::list<nlohmann::json> _configurations; |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame^] | 50 | sdbusplus::asio::object_server& objServer; |
| 51 | std::function<void()> _callback; |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 52 | bool _passed = false; |
| 53 | bool powerWasOn = isPowerOn(); |
| 54 | DBusProbeObjectT dbusProbeObjects; |
| 55 | std::vector<std::string> passedProbes; |
| 56 | }; |
| 57 | |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 58 | // this class finds the needed dbus fields and on destruction runs the probe |
| 59 | struct PerformProbe : std::enable_shared_from_this<PerformProbe> |
| 60 | { |
| 61 | PerformProbe(const std::vector<std::string>& probeCommand, |
| 62 | std::shared_ptr<PerformScan>& scanPtr, |
| 63 | std::function<void(FoundDeviceT&)>&& callback); |
| 64 | virtual ~PerformProbe(); |
| 65 | |
| 66 | std::vector<std::string> _probeCommand; |
| 67 | std::shared_ptr<PerformScan> scan; |
| 68 | std::function<void(FoundDeviceT&)> _callback; |
| 69 | }; |
| 70 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 71 | inline void logDeviceAdded(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 72 | { |
| 73 | |
James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 74 | if (!deviceHasLogging(record)) |
| 75 | { |
| 76 | return; |
| 77 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 78 | auto findType = record.find("Type"); |
| 79 | auto findAsset = |
| 80 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 81 | |
| 82 | std::string model = "Unkown"; |
| 83 | std::string type = "Unknown"; |
| 84 | std::string sn = "Unkown"; |
| 85 | |
| 86 | if (findType != record.end()) |
| 87 | { |
| 88 | type = findType->get<std::string>(); |
| 89 | } |
| 90 | if (findAsset != record.end()) |
| 91 | { |
| 92 | auto findModel = findAsset->find("Model"); |
| 93 | auto findSn = findAsset->find("SerialNumber"); |
| 94 | if (findModel != findAsset->end()) |
| 95 | { |
| 96 | model = findModel->get<std::string>(); |
| 97 | } |
| 98 | if (findSn != findAsset->end()) |
| 99 | { |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 100 | const std::string* getSn = findSn->get_ptr<const std::string*>(); |
| 101 | if (getSn != nullptr) |
| 102 | { |
| 103 | sn = *getSn; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | sn = findSn->dump(); |
| 108 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 112 | sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR, |
| 113 | "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 114 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
| 115 | type.c_str(), sn.c_str(), NULL); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 116 | } |
| 117 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 118 | inline void logDeviceRemoved(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 119 | { |
James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 120 | if (!deviceHasLogging(record)) |
| 121 | { |
| 122 | return; |
| 123 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 124 | auto findType = record.find("Type"); |
| 125 | auto findAsset = |
| 126 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 127 | |
| 128 | std::string model = "Unkown"; |
| 129 | std::string type = "Unknown"; |
| 130 | std::string sn = "Unkown"; |
| 131 | |
| 132 | if (findType != record.end()) |
| 133 | { |
| 134 | type = findType->get<std::string>(); |
| 135 | } |
| 136 | if (findAsset != record.end()) |
| 137 | { |
| 138 | auto findModel = findAsset->find("Model"); |
| 139 | auto findSn = findAsset->find("SerialNumber"); |
| 140 | if (findModel != findAsset->end()) |
| 141 | { |
| 142 | model = findModel->get<std::string>(); |
| 143 | } |
| 144 | if (findSn != findAsset->end()) |
| 145 | { |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 146 | const std::string* getSn = findSn->get_ptr<const std::string*>(); |
| 147 | if (getSn != nullptr) |
| 148 | { |
| 149 | sn = *getSn; |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | sn = findSn->dump(); |
| 154 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 157 | |
| 158 | sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR, |
| 159 | "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 160 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
| 161 | type.c_str(), sn.c_str(), NULL); |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame^] | 162 | } |