blob: f730ddee0ca37ff49e9b37105ac40c58829159e5 [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
40 std::shared_ptr<sdbusplus::asio::connection> systemBus;
41 sdbusplus::asio::object_server objServer;
42 std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface;
43 nlohmann::json lastJson;
44 nlohmann::json systemConfiguration;
45 Topology topology;
Alexander Hansena555acf2025-06-27 11:59:10 +020046 boost::asio::io_context& io;
Christopher Meiscf6a75b2025-06-03 07:53:50 +020047
Alexander Hansen57604ed2025-06-27 13:22:28 +020048 dbus_interface::EMDBusInterface dbus_interface;
49
Christopher Meiscf6a75b2025-06-03 07:53:50 +020050 void propertiesChangedCallback();
51 void registerCallback(const std::string& path);
52 void publishNewConfiguration(const size_t& instance, size_t count,
53 boost::asio::steady_timer& timer,
54 nlohmann::json newConfiguration);
55 void postToDbus(const nlohmann::json& newConfiguration);
56 void pruneConfiguration(bool powerOff, const std::string& name,
57 const nlohmann::json& device);
58
59 void initFilters(const std::set<std::string>& probeInterfaces);
Alexander Hansenad28e402025-06-25 12:38:20 +020060
61 void handleCurrentConfigurationJson();
Alexander Hansen0f7bd782025-06-27 13:39:53 +020062
63 private:
64 std::unique_ptr<sdbusplus::bus::match_t> nameOwnerChangedMatch = nullptr;
65 std::unique_ptr<sdbusplus::bus::match_t> interfacesAddedMatch = nullptr;
66 std::unique_ptr<sdbusplus::bus::match_t> interfacesRemovedMatch = nullptr;
Alexander Hansen95ab18f2025-06-27 13:58:10 +020067
68 bool scannedPowerOff = false;
69 bool scannedPowerOn = false;
70
Alexander Hansen16d40022025-06-27 14:22:56 +020071 bool propertiesChangedInProgress = false;
72
Alexander Hansen95ab18f2025-06-27 13:58:10 +020073 void startRemovedTimer(boost::asio::steady_timer& timer,
74 nlohmann::json& systemConfiguration);
Christopher Meiscf6a75b2025-06-03 07:53:50 +020075};
76
James Feist1a996582019-05-14 15:10:06 -070077inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070078{
James Feist1ffa4a42020-04-22 18:27:17 -070079 if (!deviceHasLogging(record))
80 {
81 return;
82 }
James Feist1a996582019-05-14 15:10:06 -070083 auto findType = record.find("Type");
84 auto findAsset =
85 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
86
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030087 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070088 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030089 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -050090 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070091
92 if (findType != record.end())
93 {
94 type = findType->get<std::string>();
95 }
96 if (findAsset != record.end())
97 {
98 auto findModel = findAsset->find("Model");
99 auto findSn = findAsset->find("SerialNumber");
100 if (findModel != findAsset->end())
101 {
102 model = findModel->get<std::string>();
103 }
104 if (findSn != findAsset->end())
105 {
James Feistf5125b02019-06-06 11:27:43 -0700106 const std::string* getSn = findSn->get_ptr<const std::string*>();
107 if (getSn != nullptr)
108 {
109 sn = *getSn;
110 }
111 else
112 {
113 sn = findSn->dump();
114 }
James Feist1a996582019-05-14 15:10:06 -0700115 }
116 }
117
Matt Spinler200bf402022-08-04 09:14:18 -0500118 auto findName = record.find("Name");
119 if (findName != record.end())
120 {
121 name = findName->get<std::string>();
122 }
123
124 sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i",
125 LOG_INFO, "REDFISH_MESSAGE_ID=%s",
126 "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700127 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500128 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700129}
130
James Feist1a996582019-05-14 15:10:06 -0700131inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700132{
James Feist1ffa4a42020-04-22 18:27:17 -0700133 if (!deviceHasLogging(record))
134 {
135 return;
136 }
James Feist1a996582019-05-14 15:10:06 -0700137 auto findType = record.find("Type");
138 auto findAsset =
139 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
140
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300141 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700142 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300143 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -0500144 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700145
146 if (findType != record.end())
147 {
148 type = findType->get<std::string>();
149 }
150 if (findAsset != record.end())
151 {
152 auto findModel = findAsset->find("Model");
153 auto findSn = findAsset->find("SerialNumber");
154 if (findModel != findAsset->end())
155 {
156 model = findModel->get<std::string>();
157 }
158 if (findSn != findAsset->end())
159 {
James Feistf5125b02019-06-06 11:27:43 -0700160 const std::string* getSn = findSn->get_ptr<const std::string*>();
161 if (getSn != nullptr)
162 {
163 sn = *getSn;
164 }
165 else
166 {
167 sn = findSn->dump();
168 }
James Feist1a996582019-05-14 15:10:06 -0700169 }
170 }
James Feist1df06a42019-04-11 14:23:04 -0700171
Matt Spinler200bf402022-08-04 09:14:18 -0500172 auto findName = record.find("Name");
173 if (findName != record.end())
174 {
175 name = findName->get<std::string>();
176 }
177
178 sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(),
179 "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s",
180 "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700181 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500182 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700183}