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