blob: 8490d1c819d7deb411c79564187dfbdc71c02e5e [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 Meiscf6a75b2025-06-03 07:53:50 +020021#include "topology.hpp"
James Feist733f7652019-11-13 14:32:29 -080022
James Feist1df06a42019-04-11 14:23:04 -070023#include <systemd/sd-journal.h>
24
James Feist733f7652019-11-13 14:32:29 -080025#include <boost/container/flat_map.hpp>
James Feist1a996582019-05-14 15:10:06 -070026#include <nlohmann/json.hpp>
Christopher Meiscf6a75b2025-06-03 07:53:50 +020027#include <sdbusplus/asio/connection.hpp>
James Feist4dc617b2020-05-01 09:54:47 -070028#include <sdbusplus/asio/object_server.hpp>
James Feist8c505da2020-05-28 10:06:33 -070029
James Feist1df06a42019-04-11 14:23:04 -070030#include <string>
31
Christopher Meiscf6a75b2025-06-03 07:53:50 +020032class EntityManager
33{
34 public:
35 explicit EntityManager(
36 std::shared_ptr<sdbusplus::asio::connection>& systemBus);
37
38 std::shared_ptr<sdbusplus::asio::connection> systemBus;
39 sdbusplus::asio::object_server objServer;
40 std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface;
41 nlohmann::json lastJson;
42 nlohmann::json systemConfiguration;
43 Topology topology;
44
45 void propertiesChangedCallback();
46 void registerCallback(const std::string& path);
47 void publishNewConfiguration(const size_t& instance, size_t count,
48 boost::asio::steady_timer& timer,
49 nlohmann::json newConfiguration);
50 void postToDbus(const nlohmann::json& newConfiguration);
51 void pruneConfiguration(bool powerOff, const std::string& name,
52 const nlohmann::json& device);
53
54 void initFilters(const std::set<std::string>& probeInterfaces);
Alexander Hansenad28e402025-06-25 12:38:20 +020055
56 void handleCurrentConfigurationJson();
Christopher Meiscf6a75b2025-06-03 07:53:50 +020057};
58
James Feist1a996582019-05-14 15:10:06 -070059inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070060{
James Feist1ffa4a42020-04-22 18:27:17 -070061 if (!deviceHasLogging(record))
62 {
63 return;
64 }
James Feist1a996582019-05-14 15:10:06 -070065 auto findType = record.find("Type");
66 auto findAsset =
67 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
68
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030069 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070070 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030071 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -050072 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070073
74 if (findType != record.end())
75 {
76 type = findType->get<std::string>();
77 }
78 if (findAsset != record.end())
79 {
80 auto findModel = findAsset->find("Model");
81 auto findSn = findAsset->find("SerialNumber");
82 if (findModel != findAsset->end())
83 {
84 model = findModel->get<std::string>();
85 }
86 if (findSn != findAsset->end())
87 {
James Feistf5125b02019-06-06 11:27:43 -070088 const std::string* getSn = findSn->get_ptr<const std::string*>();
89 if (getSn != nullptr)
90 {
91 sn = *getSn;
92 }
93 else
94 {
95 sn = findSn->dump();
96 }
James Feist1a996582019-05-14 15:10:06 -070097 }
98 }
99
Matt Spinler200bf402022-08-04 09:14:18 -0500100 auto findName = record.find("Name");
101 if (findName != record.end())
102 {
103 name = findName->get<std::string>();
104 }
105
106 sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i",
107 LOG_INFO, "REDFISH_MESSAGE_ID=%s",
108 "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700109 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500110 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700111}
112
James Feist1a996582019-05-14 15:10:06 -0700113inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700114{
James Feist1ffa4a42020-04-22 18:27:17 -0700115 if (!deviceHasLogging(record))
116 {
117 return;
118 }
James Feist1a996582019-05-14 15:10:06 -0700119 auto findType = record.find("Type");
120 auto findAsset =
121 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
122
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300123 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700124 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300125 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -0500126 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700127
128 if (findType != record.end())
129 {
130 type = findType->get<std::string>();
131 }
132 if (findAsset != record.end())
133 {
134 auto findModel = findAsset->find("Model");
135 auto findSn = findAsset->find("SerialNumber");
136 if (findModel != findAsset->end())
137 {
138 model = findModel->get<std::string>();
139 }
140 if (findSn != findAsset->end())
141 {
James Feistf5125b02019-06-06 11:27:43 -0700142 const std::string* getSn = findSn->get_ptr<const std::string*>();
143 if (getSn != nullptr)
144 {
145 sn = *getSn;
146 }
147 else
148 {
149 sn = findSn->dump();
150 }
James Feist1a996582019-05-14 15:10:06 -0700151 }
152 }
James Feist1df06a42019-04-11 14:23:04 -0700153
Matt Spinler200bf402022-08-04 09:14:18 -0500154 auto findName = record.find("Name");
155 if (findName != record.end())
156 {
157 name = findName->get<std::string>();
158 }
159
160 sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(),
161 "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s",
162 "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700163 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500164 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700165}