blob: a6095f08be704ac8e0ecfb83202afaace2e3f597 [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;
Christopher Meiscf6a75b2025-06-03 07:53:50 +020067};
68
James Feist1a996582019-05-14 15:10:06 -070069inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070070{
James Feist1ffa4a42020-04-22 18:27:17 -070071 if (!deviceHasLogging(record))
72 {
73 return;
74 }
James Feist1a996582019-05-14 15:10:06 -070075 auto findType = record.find("Type");
76 auto findAsset =
77 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
78
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030079 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070080 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030081 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -050082 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070083
84 if (findType != record.end())
85 {
86 type = findType->get<std::string>();
87 }
88 if (findAsset != record.end())
89 {
90 auto findModel = findAsset->find("Model");
91 auto findSn = findAsset->find("SerialNumber");
92 if (findModel != findAsset->end())
93 {
94 model = findModel->get<std::string>();
95 }
96 if (findSn != findAsset->end())
97 {
James Feistf5125b02019-06-06 11:27:43 -070098 const std::string* getSn = findSn->get_ptr<const std::string*>();
99 if (getSn != nullptr)
100 {
101 sn = *getSn;
102 }
103 else
104 {
105 sn = findSn->dump();
106 }
James Feist1a996582019-05-14 15:10:06 -0700107 }
108 }
109
Matt Spinler200bf402022-08-04 09:14:18 -0500110 auto findName = record.find("Name");
111 if (findName != record.end())
112 {
113 name = findName->get<std::string>();
114 }
115
116 sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i",
117 LOG_INFO, "REDFISH_MESSAGE_ID=%s",
118 "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700119 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500120 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700121}
122
James Feist1a996582019-05-14 15:10:06 -0700123inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700124{
James Feist1ffa4a42020-04-22 18:27:17 -0700125 if (!deviceHasLogging(record))
126 {
127 return;
128 }
James Feist1a996582019-05-14 15:10:06 -0700129 auto findType = record.find("Type");
130 auto findAsset =
131 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
132
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300133 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700134 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300135 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -0500136 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700137
138 if (findType != record.end())
139 {
140 type = findType->get<std::string>();
141 }
142 if (findAsset != record.end())
143 {
144 auto findModel = findAsset->find("Model");
145 auto findSn = findAsset->find("SerialNumber");
146 if (findModel != findAsset->end())
147 {
148 model = findModel->get<std::string>();
149 }
150 if (findSn != findAsset->end())
151 {
James Feistf5125b02019-06-06 11:27:43 -0700152 const std::string* getSn = findSn->get_ptr<const std::string*>();
153 if (getSn != nullptr)
154 {
155 sn = *getSn;
156 }
157 else
158 {
159 sn = findSn->dump();
160 }
James Feist1a996582019-05-14 15:10:06 -0700161 }
162 }
James Feist1df06a42019-04-11 14:23:04 -0700163
Matt Spinler200bf402022-08-04 09:14:18 -0500164 auto findName = record.find("Name");
165 if (findName != record.end())
166 {
167 name = findName->get<std::string>();
168 }
169
170 sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(),
171 "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s",
172 "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700173 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500174 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700175}