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