blob: 81c2136c9a0da14854f8430373276b9b3e22e234 [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
71 void startRemovedTimer(boost::asio::steady_timer& timer,
72 nlohmann::json& systemConfiguration);
Christopher Meiscf6a75b2025-06-03 07:53:50 +020073};
74
James Feist1a996582019-05-14 15:10:06 -070075inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070076{
James Feist1ffa4a42020-04-22 18:27:17 -070077 if (!deviceHasLogging(record))
78 {
79 return;
80 }
James Feist1a996582019-05-14 15:10:06 -070081 auto findType = record.find("Type");
82 auto findAsset =
83 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
84
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030085 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070086 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030087 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -050088 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070089
90 if (findType != record.end())
91 {
92 type = findType->get<std::string>();
93 }
94 if (findAsset != record.end())
95 {
96 auto findModel = findAsset->find("Model");
97 auto findSn = findAsset->find("SerialNumber");
98 if (findModel != findAsset->end())
99 {
100 model = findModel->get<std::string>();
101 }
102 if (findSn != findAsset->end())
103 {
James Feistf5125b02019-06-06 11:27:43 -0700104 const std::string* getSn = findSn->get_ptr<const std::string*>();
105 if (getSn != nullptr)
106 {
107 sn = *getSn;
108 }
109 else
110 {
111 sn = findSn->dump();
112 }
James Feist1a996582019-05-14 15:10:06 -0700113 }
114 }
115
Matt Spinler200bf402022-08-04 09:14:18 -0500116 auto findName = record.find("Name");
117 if (findName != record.end())
118 {
119 name = findName->get<std::string>();
120 }
121
122 sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i",
123 LOG_INFO, "REDFISH_MESSAGE_ID=%s",
124 "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700125 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500126 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700127}
128
James Feist1a996582019-05-14 15:10:06 -0700129inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700130{
James Feist1ffa4a42020-04-22 18:27:17 -0700131 if (!deviceHasLogging(record))
132 {
133 return;
134 }
James Feist1a996582019-05-14 15:10:06 -0700135 auto findType = record.find("Type");
136 auto findAsset =
137 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
138
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300139 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700140 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300141 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -0500142 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700143
144 if (findType != record.end())
145 {
146 type = findType->get<std::string>();
147 }
148 if (findAsset != record.end())
149 {
150 auto findModel = findAsset->find("Model");
151 auto findSn = findAsset->find("SerialNumber");
152 if (findModel != findAsset->end())
153 {
154 model = findModel->get<std::string>();
155 }
156 if (findSn != findAsset->end())
157 {
James Feistf5125b02019-06-06 11:27:43 -0700158 const std::string* getSn = findSn->get_ptr<const std::string*>();
159 if (getSn != nullptr)
160 {
161 sn = *getSn;
162 }
163 else
164 {
165 sn = findSn->dump();
166 }
James Feist1a996582019-05-14 15:10:06 -0700167 }
168 }
James Feist1df06a42019-04-11 14:23:04 -0700169
Matt Spinler200bf402022-08-04 09:14:18 -0500170 auto findName = record.find("Name");
171 if (findName != record.end())
172 {
173 name = findName->get<std::string>();
174 }
175
176 sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(),
177 "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s",
178 "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700179 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500180 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700181}