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 | |
Christopher Meis | fc9e7fd | 2025-04-03 13:13:35 +0200 | [diff] [blame] | 20 | #include "../utils.hpp" |
Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 21 | #include "configuration.hpp" |
Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 22 | #include "dbus_interface.hpp" |
Alexander Hansen | 0ab32b3 | 2025-06-27 14:50:33 +0200 | [diff] [blame^] | 23 | #include "power_status_monitor.hpp" |
Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 24 | #include "topology.hpp" |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 25 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 26 | #include <systemd/sd-journal.h> |
| 27 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 28 | #include <boost/container/flat_map.hpp> |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 29 | #include <nlohmann/json.hpp> |
Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 30 | #include <sdbusplus/asio/connection.hpp> |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 31 | #include <sdbusplus/asio/object_server.hpp> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 32 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 33 | #include <string> |
| 34 | |
Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 35 | class EntityManager |
| 36 | { |
| 37 | public: |
| 38 | explicit EntityManager( |
Alexander Hansen | a555acf | 2025-06-27 11:59:10 +0200 | [diff] [blame] | 39 | std::shared_ptr<sdbusplus::asio::connection>& systemBus, |
| 40 | boost::asio::io_context& io); |
Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 41 | |
Alexander Hansen | 3b7678c | 2025-07-22 18:33:23 +0200 | [diff] [blame] | 42 | // disable copy |
| 43 | EntityManager(const EntityManager&) = delete; |
| 44 | EntityManager& operator=(const EntityManager&) = delete; |
| 45 | |
| 46 | // disable move |
| 47 | EntityManager(EntityManager&&) = delete; |
| 48 | EntityManager& operator=(EntityManager&&) = delete; |
| 49 | |
| 50 | ~EntityManager() = default; |
| 51 | |
Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 52 | std::shared_ptr<sdbusplus::asio::connection> systemBus; |
| 53 | sdbusplus::asio::object_server objServer; |
| 54 | std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface; |
Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 55 | Configuration configuration; |
Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 56 | nlohmann::json lastJson; |
| 57 | nlohmann::json systemConfiguration; |
| 58 | Topology topology; |
Alexander Hansen | a555acf | 2025-06-27 11:59:10 +0200 | [diff] [blame] | 59 | boost::asio::io_context& io; |
Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 60 | |
Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 61 | dbus_interface::EMDBusInterface dbus_interface; |
| 62 | |
Alexander Hansen | 0ab32b3 | 2025-06-27 14:50:33 +0200 | [diff] [blame^] | 63 | power::PowerStatusMonitor powerStatus; |
| 64 | |
Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 65 | void propertiesChangedCallback(); |
| 66 | void registerCallback(const std::string& path); |
| 67 | void publishNewConfiguration(const size_t& instance, size_t count, |
| 68 | boost::asio::steady_timer& timer, |
| 69 | nlohmann::json newConfiguration); |
| 70 | void postToDbus(const nlohmann::json& newConfiguration); |
| 71 | void pruneConfiguration(bool powerOff, const std::string& name, |
| 72 | const nlohmann::json& device); |
| 73 | |
Alexander Hansen | ad28e40 | 2025-06-25 12:38:20 +0200 | [diff] [blame] | 74 | void handleCurrentConfigurationJson(); |
Alexander Hansen | 0f7bd78 | 2025-06-27 13:39:53 +0200 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | std::unique_ptr<sdbusplus::bus::match_t> nameOwnerChangedMatch = nullptr; |
| 78 | std::unique_ptr<sdbusplus::bus::match_t> interfacesAddedMatch = nullptr; |
| 79 | std::unique_ptr<sdbusplus::bus::match_t> interfacesRemovedMatch = nullptr; |
Alexander Hansen | 95ab18f | 2025-06-27 13:58:10 +0200 | [diff] [blame] | 80 | |
| 81 | bool scannedPowerOff = false; |
| 82 | bool scannedPowerOn = false; |
| 83 | |
Alexander Hansen | 16d4002 | 2025-06-27 14:22:56 +0200 | [diff] [blame] | 84 | bool propertiesChangedInProgress = false; |
Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 85 | boost::asio::steady_timer propertiesChangedTimer; |
Alexander Hansen | fc1b1e2 | 2025-06-27 14:34:11 +0200 | [diff] [blame] | 86 | size_t propertiesChangedInstance = 0; |
Alexander Hansen | 16d4002 | 2025-06-27 14:22:56 +0200 | [diff] [blame] | 87 | |
Alexander Hansen | 6080318 | 2025-06-27 14:41:28 +0200 | [diff] [blame] | 88 | boost::container::flat_map<std::string, sdbusplus::bus::match_t> |
| 89 | dbusMatches; |
| 90 | |
Alexander Hansen | 95ab18f | 2025-06-27 13:58:10 +0200 | [diff] [blame] | 91 | void startRemovedTimer(boost::asio::steady_timer& timer, |
| 92 | nlohmann::json& systemConfiguration); |
Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 93 | void initFilters(const std::unordered_set<std::string>& probeInterfaces); |
Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [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 | { |
James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 98 | if (!deviceHasLogging(record)) |
| 99 | { |
| 100 | return; |
| 101 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 102 | auto findType = record.find("Type"); |
| 103 | auto findAsset = |
| 104 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 105 | |
Konstantin Aladyshev | e7ac9c9 | 2021-09-10 09:20:17 +0300 | [diff] [blame] | 106 | std::string model = "Unknown"; |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 107 | std::string type = "Unknown"; |
Konstantin Aladyshev | e7ac9c9 | 2021-09-10 09:20:17 +0300 | [diff] [blame] | 108 | std::string sn = "Unknown"; |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 109 | std::string name = "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 | |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 137 | auto findName = record.find("Name"); |
| 138 | if (findName != record.end()) |
| 139 | { |
| 140 | name = findName->get<std::string>(); |
| 141 | } |
| 142 | |
| 143 | sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i", |
| 144 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 145 | "OpenBMC.0.1.InventoryAdded", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 146 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 147 | type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 148 | } |
| 149 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 150 | inline void logDeviceRemoved(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 151 | { |
James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 152 | if (!deviceHasLogging(record)) |
| 153 | { |
| 154 | return; |
| 155 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 156 | auto findType = record.find("Type"); |
| 157 | auto findAsset = |
| 158 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 159 | |
Konstantin Aladyshev | e7ac9c9 | 2021-09-10 09:20:17 +0300 | [diff] [blame] | 160 | std::string model = "Unknown"; |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 161 | std::string type = "Unknown"; |
Konstantin Aladyshev | e7ac9c9 | 2021-09-10 09:20:17 +0300 | [diff] [blame] | 162 | std::string sn = "Unknown"; |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 163 | std::string name = "Unknown"; |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 164 | |
| 165 | if (findType != record.end()) |
| 166 | { |
| 167 | type = findType->get<std::string>(); |
| 168 | } |
| 169 | if (findAsset != record.end()) |
| 170 | { |
| 171 | auto findModel = findAsset->find("Model"); |
| 172 | auto findSn = findAsset->find("SerialNumber"); |
| 173 | if (findModel != findAsset->end()) |
| 174 | { |
| 175 | model = findModel->get<std::string>(); |
| 176 | } |
| 177 | if (findSn != findAsset->end()) |
| 178 | { |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 179 | const std::string* getSn = findSn->get_ptr<const std::string*>(); |
| 180 | if (getSn != nullptr) |
| 181 | { |
| 182 | sn = *getSn; |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | sn = findSn->dump(); |
| 187 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 190 | |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 191 | auto findName = record.find("Name"); |
| 192 | if (findName != record.end()) |
| 193 | { |
| 194 | name = findName->get<std::string>(); |
| 195 | } |
| 196 | |
| 197 | sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(), |
| 198 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 199 | "OpenBMC.0.1.InventoryRemoved", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 200 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 201 | type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL); |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 202 | } |