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