blob: ba3914f13dcb462c959be9f3f3082a62645ef1eb [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(
Alexander Hansena555acf2025-06-27 11:59:10 +020036 std::shared_ptr<sdbusplus::asio::connection>& systemBus,
37 boost::asio::io_context& io);
Christopher Meiscf6a75b2025-06-03 07:53:50 +020038
39 std::shared_ptr<sdbusplus::asio::connection> systemBus;
40 sdbusplus::asio::object_server objServer;
41 std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface;
42 nlohmann::json lastJson;
43 nlohmann::json systemConfiguration;
44 Topology topology;
Alexander Hansena555acf2025-06-27 11:59:10 +020045 boost::asio::io_context& io;
Christopher Meiscf6a75b2025-06-03 07:53:50 +020046
47 void propertiesChangedCallback();
48 void registerCallback(const std::string& path);
49 void publishNewConfiguration(const size_t& instance, size_t count,
50 boost::asio::steady_timer& timer,
51 nlohmann::json newConfiguration);
52 void postToDbus(const nlohmann::json& newConfiguration);
53 void pruneConfiguration(bool powerOff, const std::string& name,
54 const nlohmann::json& device);
55
56 void initFilters(const std::set<std::string>& probeInterfaces);
Alexander Hansenad28e402025-06-25 12:38:20 +020057
58 void handleCurrentConfigurationJson();
Christopher Meiscf6a75b2025-06-03 07:53:50 +020059};
60
James Feist1a996582019-05-14 15:10:06 -070061inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070062{
James Feist1ffa4a42020-04-22 18:27:17 -070063 if (!deviceHasLogging(record))
64 {
65 return;
66 }
James Feist1a996582019-05-14 15:10:06 -070067 auto findType = record.find("Type");
68 auto findAsset =
69 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
70
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030071 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070072 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030073 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -050074 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070075
76 if (findType != record.end())
77 {
78 type = findType->get<std::string>();
79 }
80 if (findAsset != record.end())
81 {
82 auto findModel = findAsset->find("Model");
83 auto findSn = findAsset->find("SerialNumber");
84 if (findModel != findAsset->end())
85 {
86 model = findModel->get<std::string>();
87 }
88 if (findSn != findAsset->end())
89 {
James Feistf5125b02019-06-06 11:27:43 -070090 const std::string* getSn = findSn->get_ptr<const std::string*>();
91 if (getSn != nullptr)
92 {
93 sn = *getSn;
94 }
95 else
96 {
97 sn = findSn->dump();
98 }
James Feist1a996582019-05-14 15:10:06 -070099 }
100 }
101
Matt Spinler200bf402022-08-04 09:14:18 -0500102 auto findName = record.find("Name");
103 if (findName != record.end())
104 {
105 name = findName->get<std::string>();
106 }
107
108 sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i",
109 LOG_INFO, "REDFISH_MESSAGE_ID=%s",
110 "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700111 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500112 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700113}
114
James Feist1a996582019-05-14 15:10:06 -0700115inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700116{
James Feist1ffa4a42020-04-22 18:27:17 -0700117 if (!deviceHasLogging(record))
118 {
119 return;
120 }
James Feist1a996582019-05-14 15:10:06 -0700121 auto findType = record.find("Type");
122 auto findAsset =
123 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
124
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300125 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700126 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300127 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -0500128 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700129
130 if (findType != record.end())
131 {
132 type = findType->get<std::string>();
133 }
134 if (findAsset != record.end())
135 {
136 auto findModel = findAsset->find("Model");
137 auto findSn = findAsset->find("SerialNumber");
138 if (findModel != findAsset->end())
139 {
140 model = findModel->get<std::string>();
141 }
142 if (findSn != findAsset->end())
143 {
James Feistf5125b02019-06-06 11:27:43 -0700144 const std::string* getSn = findSn->get_ptr<const std::string*>();
145 if (getSn != nullptr)
146 {
147 sn = *getSn;
148 }
149 else
150 {
151 sn = findSn->dump();
152 }
James Feist1a996582019-05-14 15:10:06 -0700153 }
154 }
James Feist1df06a42019-04-11 14:23:04 -0700155
Matt Spinler200bf402022-08-04 09:14:18 -0500156 auto findName = record.find("Name");
157 if (findName != record.end())
158 {
159 name = findName->get<std::string>();
160 }
161
162 sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(),
163 "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s",
164 "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700165 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500166 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700167}