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