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