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 | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 16 | /// \file entity_manager.hpp |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 17 | |
| 18 | #pragma once |
| 19 | |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 20 | #include "utils.hpp" |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 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 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 60 | using FoundProbeTypeT = std::optional<boost::container::flat_map< |
| 61 | const char*, probe_type_codes, CmpStr>::const_iterator>; |
Andrew Jeffery | 666583b | 2021-12-01 15:50:12 +1030 | [diff] [blame] | 62 | FoundProbeTypeT findProbeType(const std::string& probe); |
| 63 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 64 | struct PerformScan : std::enable_shared_from_this<PerformScan> |
| 65 | { |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 66 | PerformScan(nlohmann::json& systemConfiguration, |
| 67 | nlohmann::json& missingConfigurations, |
| 68 | std::list<nlohmann::json>& configurations, |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 69 | sdbusplus::asio::object_server& objServer, |
| 70 | std::function<void()>&& callback); |
Andrew Jeffery | ae6c1a4 | 2022-04-19 15:44:42 +0930 | [diff] [blame] | 71 | void updateSystemConfiguration(const nlohmann::json& recordRef, |
| 72 | const std::string& probeName, |
Andrew Jeffery | af9b46b | 2022-04-21 12:34:43 +0930 | [diff] [blame] | 73 | FoundDevices& foundDevices); |
Delphine CC Chiu | a3ca14a | 2024-03-27 17:02:24 +0800 | [diff] [blame] | 74 | void run(); |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 75 | virtual ~PerformScan(); |
| 76 | nlohmann::json& _systemConfiguration; |
| 77 | nlohmann::json& _missingConfigurations; |
| 78 | std::list<nlohmann::json> _configurations; |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 79 | sdbusplus::asio::object_server& objServer; |
| 80 | std::function<void()> _callback; |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 81 | bool _passed = false; |
Andrew Jeffery | ac20bd9 | 2022-04-05 11:11:40 +0930 | [diff] [blame] | 82 | MapperGetSubTreeResponse dbusProbeObjects; |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 83 | std::vector<std::string> passedProbes; |
| 84 | }; |
| 85 | |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 86 | // this class finds the needed dbus fields and on destruction runs the probe |
| 87 | struct PerformProbe : std::enable_shared_from_this<PerformProbe> |
| 88 | { |
Andrew Jeffery | ea4ff02 | 2022-04-21 12:31:40 +0930 | [diff] [blame] | 89 | PerformProbe(nlohmann::json& recordRef, |
| 90 | const std::vector<std::string>& probeCommand, |
| 91 | std::string probeName, std::shared_ptr<PerformScan>& scanPtr); |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 92 | virtual ~PerformProbe(); |
| 93 | |
Andrew Jeffery | ea4ff02 | 2022-04-21 12:31:40 +0930 | [diff] [blame] | 94 | nlohmann::json& recordRef; |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 95 | std::vector<std::string> _probeCommand; |
Andrew Jeffery | ea4ff02 | 2022-04-21 12:31:40 +0930 | [diff] [blame] | 96 | std::string probeName; |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 97 | std::shared_ptr<PerformScan> scan; |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 100 | inline void logDeviceAdded(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 101 | { |
James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 102 | if (!deviceHasLogging(record)) |
| 103 | { |
| 104 | return; |
| 105 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 106 | auto findType = record.find("Type"); |
| 107 | auto findAsset = |
| 108 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 109 | |
Konstantin Aladyshev | e7ac9c9 | 2021-09-10 09:20:17 +0300 | [diff] [blame] | 110 | std::string model = "Unknown"; |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 111 | std::string type = "Unknown"; |
Konstantin Aladyshev | e7ac9c9 | 2021-09-10 09:20:17 +0300 | [diff] [blame] | 112 | std::string sn = "Unknown"; |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 113 | std::string name = "Unknown"; |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 114 | |
| 115 | if (findType != record.end()) |
| 116 | { |
| 117 | type = findType->get<std::string>(); |
| 118 | } |
| 119 | if (findAsset != record.end()) |
| 120 | { |
| 121 | auto findModel = findAsset->find("Model"); |
| 122 | auto findSn = findAsset->find("SerialNumber"); |
| 123 | if (findModel != findAsset->end()) |
| 124 | { |
| 125 | model = findModel->get<std::string>(); |
| 126 | } |
| 127 | if (findSn != findAsset->end()) |
| 128 | { |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 129 | const std::string* getSn = findSn->get_ptr<const std::string*>(); |
| 130 | if (getSn != nullptr) |
| 131 | { |
| 132 | sn = *getSn; |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | sn = findSn->dump(); |
| 137 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 141 | auto findName = record.find("Name"); |
| 142 | if (findName != record.end()) |
| 143 | { |
| 144 | name = findName->get<std::string>(); |
| 145 | } |
| 146 | |
| 147 | sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i", |
| 148 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 149 | "OpenBMC.0.1.InventoryAdded", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 150 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 151 | type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 152 | } |
| 153 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 154 | inline void logDeviceRemoved(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 155 | { |
James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 156 | if (!deviceHasLogging(record)) |
| 157 | { |
| 158 | return; |
| 159 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 160 | auto findType = record.find("Type"); |
| 161 | auto findAsset = |
| 162 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 163 | |
Konstantin Aladyshev | e7ac9c9 | 2021-09-10 09:20:17 +0300 | [diff] [blame] | 164 | std::string model = "Unknown"; |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 165 | std::string type = "Unknown"; |
Konstantin Aladyshev | e7ac9c9 | 2021-09-10 09:20:17 +0300 | [diff] [blame] | 166 | std::string sn = "Unknown"; |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 167 | std::string name = "Unknown"; |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 168 | |
| 169 | if (findType != record.end()) |
| 170 | { |
| 171 | type = findType->get<std::string>(); |
| 172 | } |
| 173 | if (findAsset != record.end()) |
| 174 | { |
| 175 | auto findModel = findAsset->find("Model"); |
| 176 | auto findSn = findAsset->find("SerialNumber"); |
| 177 | if (findModel != findAsset->end()) |
| 178 | { |
| 179 | model = findModel->get<std::string>(); |
| 180 | } |
| 181 | if (findSn != findAsset->end()) |
| 182 | { |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 183 | const std::string* getSn = findSn->get_ptr<const std::string*>(); |
| 184 | if (getSn != nullptr) |
| 185 | { |
| 186 | sn = *getSn; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | sn = findSn->dump(); |
| 191 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 194 | |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 195 | auto findName = record.find("Name"); |
| 196 | if (findName != record.end()) |
| 197 | { |
| 198 | name = findName->get<std::string>(); |
| 199 | } |
| 200 | |
| 201 | sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(), |
| 202 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 203 | "OpenBMC.0.1.InventoryRemoved", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 204 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 205 | type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL); |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 206 | } |