blob: c244ad341ee07f74b914d780ffcfaebe03e78e41 [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"
Christopher Meisf7252572025-06-11 13:22:05 +020021#include "configuration.hpp"
Alexander Hansen57604ed2025-06-27 13:22:28 +020022#include "dbus_interface.hpp"
Christopher Meiscf6a75b2025-06-03 07:53:50 +020023#include "topology.hpp"
James Feist733f7652019-11-13 14:32:29 -080024
James Feist1df06a42019-04-11 14:23:04 -070025#include <systemd/sd-journal.h>
26
James Feist733f7652019-11-13 14:32:29 -080027#include <boost/container/flat_map.hpp>
James Feist1a996582019-05-14 15:10:06 -070028#include <nlohmann/json.hpp>
Christopher Meiscf6a75b2025-06-03 07:53:50 +020029#include <sdbusplus/asio/connection.hpp>
James Feist4dc617b2020-05-01 09:54:47 -070030#include <sdbusplus/asio/object_server.hpp>
James Feist8c505da2020-05-28 10:06:33 -070031
James Feist1df06a42019-04-11 14:23:04 -070032#include <string>
33
Christopher Meiscf6a75b2025-06-03 07:53:50 +020034class EntityManager
35{
36 public:
37 explicit EntityManager(
Alexander Hansena555acf2025-06-27 11:59:10 +020038 std::shared_ptr<sdbusplus::asio::connection>& systemBus,
39 boost::asio::io_context& io);
Christopher Meiscf6a75b2025-06-03 07:53:50 +020040
Alexander Hansen3b7678c2025-07-22 18:33:23 +020041 // disable copy
42 EntityManager(const EntityManager&) = delete;
43 EntityManager& operator=(const EntityManager&) = delete;
44
45 // disable move
46 EntityManager(EntityManager&&) = delete;
47 EntityManager& operator=(EntityManager&&) = delete;
48
49 ~EntityManager() = default;
50
Christopher Meiscf6a75b2025-06-03 07:53:50 +020051 std::shared_ptr<sdbusplus::asio::connection> systemBus;
52 sdbusplus::asio::object_server objServer;
53 std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface;
Christopher Meisf7252572025-06-11 13:22:05 +020054 Configuration configuration;
Christopher Meiscf6a75b2025-06-03 07:53:50 +020055 nlohmann::json lastJson;
56 nlohmann::json systemConfiguration;
57 Topology topology;
Alexander Hansena555acf2025-06-27 11:59:10 +020058 boost::asio::io_context& io;
Christopher Meiscf6a75b2025-06-03 07:53:50 +020059
Alexander Hansen57604ed2025-06-27 13:22:28 +020060 dbus_interface::EMDBusInterface dbus_interface;
61
Christopher Meiscf6a75b2025-06-03 07:53:50 +020062 void propertiesChangedCallback();
63 void registerCallback(const std::string& path);
64 void publishNewConfiguration(const size_t& instance, size_t count,
65 boost::asio::steady_timer& timer,
66 nlohmann::json newConfiguration);
67 void postToDbus(const nlohmann::json& newConfiguration);
68 void pruneConfiguration(bool powerOff, const std::string& name,
69 const nlohmann::json& device);
70
Alexander Hansenad28e402025-06-25 12:38:20 +020071 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 Meisf7252572025-06-11 13:22:05 +020090 void initFilters(const std::unordered_set<std::string>& probeInterfaces);
Christopher Meiscf6a75b2025-06-03 07:53:50 +020091};
92
James Feist1a996582019-05-14 15:10:06 -070093inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070094{
James Feist1ffa4a42020-04-22 18:27:17 -070095 if (!deviceHasLogging(record))
96 {
97 return;
98 }
James Feist1a996582019-05-14 15:10:06 -070099 auto findType = record.find("Type");
100 auto findAsset =
101 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
102
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300103 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700104 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300105 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -0500106 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700107
108 if (findType != record.end())
109 {
110 type = findType->get<std::string>();
111 }
112 if (findAsset != record.end())
113 {
114 auto findModel = findAsset->find("Model");
115 auto findSn = findAsset->find("SerialNumber");
116 if (findModel != findAsset->end())
117 {
118 model = findModel->get<std::string>();
119 }
120 if (findSn != findAsset->end())
121 {
James Feistf5125b02019-06-06 11:27:43 -0700122 const std::string* getSn = findSn->get_ptr<const std::string*>();
123 if (getSn != nullptr)
124 {
125 sn = *getSn;
126 }
127 else
128 {
129 sn = findSn->dump();
130 }
James Feist1a996582019-05-14 15:10:06 -0700131 }
132 }
133
Matt Spinler200bf402022-08-04 09:14:18 -0500134 auto findName = record.find("Name");
135 if (findName != record.end())
136 {
137 name = findName->get<std::string>();
138 }
139
140 sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i",
141 LOG_INFO, "REDFISH_MESSAGE_ID=%s",
142 "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700143 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500144 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700145}
146
James Feist1a996582019-05-14 15:10:06 -0700147inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700148{
James Feist1ffa4a42020-04-22 18:27:17 -0700149 if (!deviceHasLogging(record))
150 {
151 return;
152 }
James Feist1a996582019-05-14 15:10:06 -0700153 auto findType = record.find("Type");
154 auto findAsset =
155 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
156
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300157 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700158 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300159 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -0500160 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700161
162 if (findType != record.end())
163 {
164 type = findType->get<std::string>();
165 }
166 if (findAsset != record.end())
167 {
168 auto findModel = findAsset->find("Model");
169 auto findSn = findAsset->find("SerialNumber");
170 if (findModel != findAsset->end())
171 {
172 model = findModel->get<std::string>();
173 }
174 if (findSn != findAsset->end())
175 {
James Feistf5125b02019-06-06 11:27:43 -0700176 const std::string* getSn = findSn->get_ptr<const std::string*>();
177 if (getSn != nullptr)
178 {
179 sn = *getSn;
180 }
181 else
182 {
183 sn = findSn->dump();
184 }
James Feist1a996582019-05-14 15:10:06 -0700185 }
186 }
James Feist1df06a42019-04-11 14:23:04 -0700187
Matt Spinler200bf402022-08-04 09:14:18 -0500188 auto findName = record.find("Name");
189 if (findName != record.end())
190 {
191 name = findName->get<std::string>();
192 }
193
194 sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(),
195 "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s",
196 "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700197 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500198 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700199}