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 | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 21 | #include "topology.hpp" |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 22 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 23 | #include <systemd/sd-journal.h> |
| 24 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 25 | #include <boost/container/flat_map.hpp> |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 26 | #include <nlohmann/json.hpp> |
Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 27 | #include <sdbusplus/asio/connection.hpp> |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 28 | #include <sdbusplus/asio/object_server.hpp> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 29 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 30 | #include <string> |
| 31 | |
Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 32 | class EntityManager |
| 33 | { |
| 34 | public: |
| 35 | explicit EntityManager( |
| 36 | std::shared_ptr<sdbusplus::asio::connection>& systemBus); |
| 37 | |
| 38 | std::shared_ptr<sdbusplus::asio::connection> systemBus; |
| 39 | sdbusplus::asio::object_server objServer; |
| 40 | std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface; |
| 41 | nlohmann::json lastJson; |
| 42 | nlohmann::json systemConfiguration; |
| 43 | Topology topology; |
| 44 | |
| 45 | void propertiesChangedCallback(); |
| 46 | void registerCallback(const std::string& path); |
| 47 | void publishNewConfiguration(const size_t& instance, size_t count, |
| 48 | boost::asio::steady_timer& timer, |
| 49 | nlohmann::json newConfiguration); |
| 50 | void postToDbus(const nlohmann::json& newConfiguration); |
| 51 | void pruneConfiguration(bool powerOff, const std::string& name, |
| 52 | const nlohmann::json& device); |
| 53 | |
| 54 | void initFilters(const std::set<std::string>& probeInterfaces); |
Alexander Hansen | ad28e40 | 2025-06-25 12:38:20 +0200 | [diff] [blame^] | 55 | |
| 56 | void handleCurrentConfigurationJson(); |
Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 57 | }; |
| 58 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 59 | inline void logDeviceAdded(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 60 | { |
James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 61 | if (!deviceHasLogging(record)) |
| 62 | { |
| 63 | return; |
| 64 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 65 | auto findType = record.find("Type"); |
| 66 | auto findAsset = |
| 67 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 68 | |
Konstantin Aladyshev | e7ac9c9 | 2021-09-10 09:20:17 +0300 | [diff] [blame] | 69 | std::string model = "Unknown"; |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 70 | std::string type = "Unknown"; |
Konstantin Aladyshev | e7ac9c9 | 2021-09-10 09:20:17 +0300 | [diff] [blame] | 71 | std::string sn = "Unknown"; |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 72 | std::string name = "Unknown"; |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 73 | |
| 74 | if (findType != record.end()) |
| 75 | { |
| 76 | type = findType->get<std::string>(); |
| 77 | } |
| 78 | if (findAsset != record.end()) |
| 79 | { |
| 80 | auto findModel = findAsset->find("Model"); |
| 81 | auto findSn = findAsset->find("SerialNumber"); |
| 82 | if (findModel != findAsset->end()) |
| 83 | { |
| 84 | model = findModel->get<std::string>(); |
| 85 | } |
| 86 | if (findSn != findAsset->end()) |
| 87 | { |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 88 | const std::string* getSn = findSn->get_ptr<const std::string*>(); |
| 89 | if (getSn != nullptr) |
| 90 | { |
| 91 | sn = *getSn; |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | sn = findSn->dump(); |
| 96 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 100 | auto findName = record.find("Name"); |
| 101 | if (findName != record.end()) |
| 102 | { |
| 103 | name = findName->get<std::string>(); |
| 104 | } |
| 105 | |
| 106 | sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i", |
| 107 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 108 | "OpenBMC.0.1.InventoryAdded", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 109 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 110 | type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 111 | } |
| 112 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 113 | inline void logDeviceRemoved(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 114 | { |
James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 115 | if (!deviceHasLogging(record)) |
| 116 | { |
| 117 | return; |
| 118 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 119 | auto findType = record.find("Type"); |
| 120 | auto findAsset = |
| 121 | record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 122 | |
Konstantin Aladyshev | e7ac9c9 | 2021-09-10 09:20:17 +0300 | [diff] [blame] | 123 | std::string model = "Unknown"; |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 124 | std::string type = "Unknown"; |
Konstantin Aladyshev | e7ac9c9 | 2021-09-10 09:20:17 +0300 | [diff] [blame] | 125 | std::string sn = "Unknown"; |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 126 | std::string name = "Unknown"; |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 127 | |
| 128 | if (findType != record.end()) |
| 129 | { |
| 130 | type = findType->get<std::string>(); |
| 131 | } |
| 132 | if (findAsset != record.end()) |
| 133 | { |
| 134 | auto findModel = findAsset->find("Model"); |
| 135 | auto findSn = findAsset->find("SerialNumber"); |
| 136 | if (findModel != findAsset->end()) |
| 137 | { |
| 138 | model = findModel->get<std::string>(); |
| 139 | } |
| 140 | if (findSn != findAsset->end()) |
| 141 | { |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 142 | const std::string* getSn = findSn->get_ptr<const std::string*>(); |
| 143 | if (getSn != nullptr) |
| 144 | { |
| 145 | sn = *getSn; |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | sn = findSn->dump(); |
| 150 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 153 | |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 154 | auto findName = record.find("Name"); |
| 155 | if (findName != record.end()) |
| 156 | { |
| 157 | name = findName->get<std::string>(); |
| 158 | } |
| 159 | |
| 160 | sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(), |
| 161 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 162 | "OpenBMC.0.1.InventoryRemoved", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 163 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
Matt Spinler | 200bf40 | 2022-08-04 09:14:18 -0500 | [diff] [blame] | 164 | type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL); |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 165 | } |