blob: cbb5f0ac1179fc98235b241ce2df2f306391bc76 [file] [log] [blame]
James Feist1df06a42019-04-11 14:23:04 -07001/*
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 Bishope45d8c72022-05-25 15:12:53 -040016/// \file entity_manager.hpp
James Feist1df06a42019-04-11 14:23:04 -070017
18#pragma once
19
Christopher Meisfc9e7fd2025-04-03 13:13:35 +020020#include "../utils.hpp"
Alexander Hansen57604ed2025-06-27 13:22:28 +020021#include "dbus_interface.hpp"
Christopher Meiscf6a75b2025-06-03 07:53:50 +020022#include "topology.hpp"
James Feist733f7652019-11-13 14:32:29 -080023
James Feist1df06a42019-04-11 14:23:04 -070024#include <systemd/sd-journal.h>
25
James Feist733f7652019-11-13 14:32:29 -080026#include <boost/container/flat_map.hpp>
James Feist1a996582019-05-14 15:10:06 -070027#include <nlohmann/json.hpp>
Christopher Meiscf6a75b2025-06-03 07:53:50 +020028#include <sdbusplus/asio/connection.hpp>
James Feist4dc617b2020-05-01 09:54:47 -070029#include <sdbusplus/asio/object_server.hpp>
James Feist8c505da2020-05-28 10:06:33 -070030
James Feist1df06a42019-04-11 14:23:04 -070031#include <string>
32
Christopher Meiscf6a75b2025-06-03 07:53:50 +020033class EntityManager
34{
35 public:
36 explicit EntityManager(
Alexander Hansena555acf2025-06-27 11:59:10 +020037 std::shared_ptr<sdbusplus::asio::connection>& systemBus,
38 boost::asio::io_context& io);
Christopher Meiscf6a75b2025-06-03 07:53:50 +020039
Alexander Hansen3b7678c2025-07-22 18:33:23 +020040 // 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 Meiscf6a75b2025-06-03 07:53:50 +020050 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 Hansena555acf2025-06-27 11:59:10 +020056 boost::asio::io_context& io;
Christopher Meiscf6a75b2025-06-03 07:53:50 +020057
Alexander Hansen57604ed2025-06-27 13:22:28 +020058 dbus_interface::EMDBusInterface dbus_interface;
59
Christopher Meiscf6a75b2025-06-03 07:53:50 +020060 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 Hansenad28e402025-06-25 12:38:20 +020070
71 void handleCurrentConfigurationJson();
Alexander Hansen0f7bd782025-06-27 13:39:53 +020072
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 Hansen95ab18f2025-06-27 13:58:10 +020077
78 bool scannedPowerOff = false;
79 bool scannedPowerOn = false;
80
Alexander Hansen16d40022025-06-27 14:22:56 +020081 bool propertiesChangedInProgress = false;
Alexander Hansenb1340da2025-06-27 14:29:13 +020082 boost::asio::steady_timer propertiesChangedTimer;
Alexander Hansenfc1b1e22025-06-27 14:34:11 +020083 size_t propertiesChangedInstance = 0;
Alexander Hansen16d40022025-06-27 14:22:56 +020084
Alexander Hansen60803182025-06-27 14:41:28 +020085 boost::container::flat_map<std::string, sdbusplus::bus::match_t>
86 dbusMatches;
87
Alexander Hansen95ab18f2025-06-27 13:58:10 +020088 void startRemovedTimer(boost::asio::steady_timer& timer,
89 nlohmann::json& systemConfiguration);
Christopher Meiscf6a75b2025-06-03 07:53:50 +020090};
91
James Feist1a996582019-05-14 15:10:06 -070092inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070093{
James Feist1ffa4a42020-04-22 18:27:17 -070094 if (!deviceHasLogging(record))
95 {
96 return;
97 }
James Feist1a996582019-05-14 15:10:06 -070098 auto findType = record.find("Type");
99 auto findAsset =
100 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
101
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300102 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700103 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300104 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -0500105 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700106
107 if (findType != record.end())
108 {
109 type = findType->get<std::string>();
110 }
111 if (findAsset != record.end())
112 {
113 auto findModel = findAsset->find("Model");
114 auto findSn = findAsset->find("SerialNumber");
115 if (findModel != findAsset->end())
116 {
117 model = findModel->get<std::string>();
118 }
119 if (findSn != findAsset->end())
120 {
James Feistf5125b02019-06-06 11:27:43 -0700121 const std::string* getSn = findSn->get_ptr<const std::string*>();
122 if (getSn != nullptr)
123 {
124 sn = *getSn;
125 }
126 else
127 {
128 sn = findSn->dump();
129 }
James Feist1a996582019-05-14 15:10:06 -0700130 }
131 }
132
Matt Spinler200bf402022-08-04 09:14:18 -0500133 auto findName = record.find("Name");
134 if (findName != record.end())
135 {
136 name = findName->get<std::string>();
137 }
138
139 sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i",
140 LOG_INFO, "REDFISH_MESSAGE_ID=%s",
141 "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700142 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500143 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700144}
145
James Feist1a996582019-05-14 15:10:06 -0700146inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700147{
James Feist1ffa4a42020-04-22 18:27:17 -0700148 if (!deviceHasLogging(record))
149 {
150 return;
151 }
James Feist1a996582019-05-14 15:10:06 -0700152 auto findType = record.find("Type");
153 auto findAsset =
154 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
155
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300156 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700157 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300158 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -0500159 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700160
161 if (findType != record.end())
162 {
163 type = findType->get<std::string>();
164 }
165 if (findAsset != record.end())
166 {
167 auto findModel = findAsset->find("Model");
168 auto findSn = findAsset->find("SerialNumber");
169 if (findModel != findAsset->end())
170 {
171 model = findModel->get<std::string>();
172 }
173 if (findSn != findAsset->end())
174 {
James Feistf5125b02019-06-06 11:27:43 -0700175 const std::string* getSn = findSn->get_ptr<const std::string*>();
176 if (getSn != nullptr)
177 {
178 sn = *getSn;
179 }
180 else
181 {
182 sn = findSn->dump();
183 }
James Feist1a996582019-05-14 15:10:06 -0700184 }
185 }
James Feist1df06a42019-04-11 14:23:04 -0700186
Matt Spinler200bf402022-08-04 09:14:18 -0500187 auto findName = record.find("Name");
188 if (findName != record.end())
189 {
190 name = findName->get<std::string>();
191 }
192
193 sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(),
194 "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s",
195 "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700196 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500197 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700198}