blob: 97ef441f50e7c57537b0ac35318aa094f4e13c18 [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);
55};
56
James Feist1a996582019-05-14 15:10:06 -070057inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070058{
James Feist1ffa4a42020-04-22 18:27:17 -070059 if (!deviceHasLogging(record))
60 {
61 return;
62 }
James Feist1a996582019-05-14 15:10:06 -070063 auto findType = record.find("Type");
64 auto findAsset =
65 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
66
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030067 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070068 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030069 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -050070 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070071
72 if (findType != record.end())
73 {
74 type = findType->get<std::string>();
75 }
76 if (findAsset != record.end())
77 {
78 auto findModel = findAsset->find("Model");
79 auto findSn = findAsset->find("SerialNumber");
80 if (findModel != findAsset->end())
81 {
82 model = findModel->get<std::string>();
83 }
84 if (findSn != findAsset->end())
85 {
James Feistf5125b02019-06-06 11:27:43 -070086 const std::string* getSn = findSn->get_ptr<const std::string*>();
87 if (getSn != nullptr)
88 {
89 sn = *getSn;
90 }
91 else
92 {
93 sn = findSn->dump();
94 }
James Feist1a996582019-05-14 15:10:06 -070095 }
96 }
97
Matt Spinler200bf402022-08-04 09:14:18 -050098 auto findName = record.find("Name");
99 if (findName != record.end())
100 {
101 name = findName->get<std::string>();
102 }
103
104 sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i",
105 LOG_INFO, "REDFISH_MESSAGE_ID=%s",
106 "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700107 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500108 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700109}
110
James Feist1a996582019-05-14 15:10:06 -0700111inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700112{
James Feist1ffa4a42020-04-22 18:27:17 -0700113 if (!deviceHasLogging(record))
114 {
115 return;
116 }
James Feist1a996582019-05-14 15:10:06 -0700117 auto findType = record.find("Type");
118 auto findAsset =
119 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
120
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300121 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700122 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +0300123 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -0500124 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -0700125
126 if (findType != record.end())
127 {
128 type = findType->get<std::string>();
129 }
130 if (findAsset != record.end())
131 {
132 auto findModel = findAsset->find("Model");
133 auto findSn = findAsset->find("SerialNumber");
134 if (findModel != findAsset->end())
135 {
136 model = findModel->get<std::string>();
137 }
138 if (findSn != findAsset->end())
139 {
James Feistf5125b02019-06-06 11:27:43 -0700140 const std::string* getSn = findSn->get_ptr<const std::string*>();
141 if (getSn != nullptr)
142 {
143 sn = *getSn;
144 }
145 else
146 {
147 sn = findSn->dump();
148 }
James Feist1a996582019-05-14 15:10:06 -0700149 }
150 }
James Feist1df06a42019-04-11 14:23:04 -0700151
Matt Spinler200bf402022-08-04 09:14:18 -0500152 auto findName = record.find("Name");
153 if (findName != record.end())
154 {
155 name = findName->get<std::string>();
156 }
157
158 sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(),
159 "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s",
160 "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700161 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500162 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700163}