1fc9e7fdaSChristopher Meis /*
2fc9e7fdaSChristopher Meis // Copyright (c) 2018 Intel Corporation
3fc9e7fdaSChristopher Meis //
4fc9e7fdaSChristopher Meis // Licensed under the Apache License, Version 2.0 (the "License");
5fc9e7fdaSChristopher Meis // you may not use this file except in compliance with the License.
6fc9e7fdaSChristopher Meis // You may obtain a copy of the License at
7fc9e7fdaSChristopher Meis //
8fc9e7fdaSChristopher Meis // http://www.apache.org/licenses/LICENSE-2.0
9fc9e7fdaSChristopher Meis //
10fc9e7fdaSChristopher Meis // Unless required by applicable law or agreed to in writing, software
11fc9e7fdaSChristopher Meis // distributed under the License is distributed on an "AS IS" BASIS,
12fc9e7fdaSChristopher Meis // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fc9e7fdaSChristopher Meis // See the License for the specific language governing permissions and
14fc9e7fdaSChristopher Meis // limitations under the License.
15fc9e7fdaSChristopher Meis */
16fc9e7fdaSChristopher Meis /// \file entity_manager.hpp
17fc9e7fdaSChristopher Meis
18fc9e7fdaSChristopher Meis #pragma once
19fc9e7fdaSChristopher Meis
20fc9e7fdaSChristopher Meis #include "../utils.hpp"
21*cf6a75bdSChristopher Meis #include "topology.hpp"
22fc9e7fdaSChristopher Meis
23fc9e7fdaSChristopher Meis #include <systemd/sd-journal.h>
24fc9e7fdaSChristopher Meis
25fc9e7fdaSChristopher Meis #include <boost/container/flat_map.hpp>
26fc9e7fdaSChristopher Meis #include <nlohmann/json.hpp>
27*cf6a75bdSChristopher Meis #include <sdbusplus/asio/connection.hpp>
28fc9e7fdaSChristopher Meis #include <sdbusplus/asio/object_server.hpp>
29fc9e7fdaSChristopher Meis
logDeviceAdded(const nlohmann::json & record)30fc9e7fdaSChristopher Meis #include <string>
31fc9e7fdaSChristopher Meis
32*cf6a75bdSChristopher Meis class EntityManager
33*cf6a75bdSChristopher Meis {
34*cf6a75bdSChristopher Meis public:
35*cf6a75bdSChristopher Meis explicit EntityManager(
36*cf6a75bdSChristopher Meis std::shared_ptr<sdbusplus::asio::connection>& systemBus);
37*cf6a75bdSChristopher Meis
38*cf6a75bdSChristopher Meis std::shared_ptr<sdbusplus::asio::connection> systemBus;
39*cf6a75bdSChristopher Meis sdbusplus::asio::object_server objServer;
40*cf6a75bdSChristopher Meis std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface;
41*cf6a75bdSChristopher Meis nlohmann::json lastJson;
42*cf6a75bdSChristopher Meis nlohmann::json systemConfiguration;
43*cf6a75bdSChristopher Meis Topology topology;
44*cf6a75bdSChristopher Meis
45*cf6a75bdSChristopher Meis void propertiesChangedCallback();
46*cf6a75bdSChristopher Meis void registerCallback(const std::string& path);
47*cf6a75bdSChristopher Meis void publishNewConfiguration(const size_t& instance, size_t count,
48*cf6a75bdSChristopher Meis boost::asio::steady_timer& timer,
49*cf6a75bdSChristopher Meis nlohmann::json newConfiguration);
50*cf6a75bdSChristopher Meis void postToDbus(const nlohmann::json& newConfiguration);
51*cf6a75bdSChristopher Meis void pruneConfiguration(bool powerOff, const std::string& name,
52*cf6a75bdSChristopher Meis const nlohmann::json& device);
53*cf6a75bdSChristopher Meis
54*cf6a75bdSChristopher Meis void initFilters(const std::set<std::string>& probeInterfaces);
55*cf6a75bdSChristopher Meis };
56*cf6a75bdSChristopher Meis
57fc9e7fdaSChristopher Meis inline void logDeviceAdded(const nlohmann::json& record)
58fc9e7fdaSChristopher Meis {
59fc9e7fdaSChristopher Meis if (!deviceHasLogging(record))
60fc9e7fdaSChristopher Meis {
61fc9e7fdaSChristopher Meis return;
62fc9e7fdaSChristopher Meis }
63fc9e7fdaSChristopher Meis auto findType = record.find("Type");
64fc9e7fdaSChristopher Meis auto findAsset =
65fc9e7fdaSChristopher Meis record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
66fc9e7fdaSChristopher Meis
67fc9e7fdaSChristopher Meis std::string model = "Unknown";
68fc9e7fdaSChristopher Meis std::string type = "Unknown";
69fc9e7fdaSChristopher Meis std::string sn = "Unknown";
70fc9e7fdaSChristopher Meis std::string name = "Unknown";
71fc9e7fdaSChristopher Meis
72fc9e7fdaSChristopher Meis if (findType != record.end())
73fc9e7fdaSChristopher Meis {
74fc9e7fdaSChristopher Meis type = findType->get<std::string>();
75fc9e7fdaSChristopher Meis }
76fc9e7fdaSChristopher Meis if (findAsset != record.end())
77fc9e7fdaSChristopher Meis {
78fc9e7fdaSChristopher Meis auto findModel = findAsset->find("Model");
79fc9e7fdaSChristopher Meis auto findSn = findAsset->find("SerialNumber");
80fc9e7fdaSChristopher Meis if (findModel != findAsset->end())
81fc9e7fdaSChristopher Meis {
82fc9e7fdaSChristopher Meis model = findModel->get<std::string>();
83fc9e7fdaSChristopher Meis }
logDeviceRemoved(const nlohmann::json & record)84fc9e7fdaSChristopher Meis if (findSn != findAsset->end())
85fc9e7fdaSChristopher Meis {
86fc9e7fdaSChristopher Meis const std::string* getSn = findSn->get_ptr<const std::string*>();
87fc9e7fdaSChristopher Meis if (getSn != nullptr)
88fc9e7fdaSChristopher Meis {
89fc9e7fdaSChristopher Meis sn = *getSn;
90fc9e7fdaSChristopher Meis }
91fc9e7fdaSChristopher Meis else
92fc9e7fdaSChristopher Meis {
93fc9e7fdaSChristopher Meis sn = findSn->dump();
94fc9e7fdaSChristopher Meis }
95fc9e7fdaSChristopher Meis }
96fc9e7fdaSChristopher Meis }
97fc9e7fdaSChristopher Meis
98fc9e7fdaSChristopher Meis auto findName = record.find("Name");
99fc9e7fdaSChristopher Meis if (findName != record.end())
100fc9e7fdaSChristopher Meis {
101fc9e7fdaSChristopher Meis name = findName->get<std::string>();
102fc9e7fdaSChristopher Meis }
103fc9e7fdaSChristopher Meis
104fc9e7fdaSChristopher Meis sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i",
105fc9e7fdaSChristopher Meis LOG_INFO, "REDFISH_MESSAGE_ID=%s",
106fc9e7fdaSChristopher Meis "OpenBMC.0.1.InventoryAdded",
107fc9e7fdaSChristopher Meis "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
108fc9e7fdaSChristopher Meis type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
109fc9e7fdaSChristopher Meis }
110fc9e7fdaSChristopher Meis
111fc9e7fdaSChristopher Meis inline void logDeviceRemoved(const nlohmann::json& record)
112fc9e7fdaSChristopher Meis {
113fc9e7fdaSChristopher Meis if (!deviceHasLogging(record))
114fc9e7fdaSChristopher Meis {
115fc9e7fdaSChristopher Meis return;
116fc9e7fdaSChristopher Meis }
117fc9e7fdaSChristopher Meis auto findType = record.find("Type");
118fc9e7fdaSChristopher Meis auto findAsset =
119fc9e7fdaSChristopher Meis record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
120fc9e7fdaSChristopher Meis
121fc9e7fdaSChristopher Meis std::string model = "Unknown";
122fc9e7fdaSChristopher Meis std::string type = "Unknown";
123fc9e7fdaSChristopher Meis std::string sn = "Unknown";
124fc9e7fdaSChristopher Meis std::string name = "Unknown";
125fc9e7fdaSChristopher Meis
126fc9e7fdaSChristopher Meis if (findType != record.end())
127fc9e7fdaSChristopher Meis {
128fc9e7fdaSChristopher Meis type = findType->get<std::string>();
129fc9e7fdaSChristopher Meis }
130fc9e7fdaSChristopher Meis if (findAsset != record.end())
131fc9e7fdaSChristopher Meis {
132fc9e7fdaSChristopher Meis auto findModel = findAsset->find("Model");
133fc9e7fdaSChristopher Meis auto findSn = findAsset->find("SerialNumber");
134fc9e7fdaSChristopher Meis if (findModel != findAsset->end())
135fc9e7fdaSChristopher Meis {
136fc9e7fdaSChristopher Meis model = findModel->get<std::string>();
137fc9e7fdaSChristopher Meis }
138fc9e7fdaSChristopher Meis if (findSn != findAsset->end())
139fc9e7fdaSChristopher Meis {
140fc9e7fdaSChristopher Meis const std::string* getSn = findSn->get_ptr<const std::string*>();
141fc9e7fdaSChristopher Meis if (getSn != nullptr)
142fc9e7fdaSChristopher Meis {
143fc9e7fdaSChristopher Meis sn = *getSn;
144fc9e7fdaSChristopher Meis }
145fc9e7fdaSChristopher Meis else
146fc9e7fdaSChristopher Meis {
147fc9e7fdaSChristopher Meis sn = findSn->dump();
148fc9e7fdaSChristopher Meis }
149fc9e7fdaSChristopher Meis }
150fc9e7fdaSChristopher Meis }
151fc9e7fdaSChristopher Meis
152fc9e7fdaSChristopher Meis auto findName = record.find("Name");
153fc9e7fdaSChristopher Meis if (findName != record.end())
154fc9e7fdaSChristopher Meis {
155fc9e7fdaSChristopher Meis name = findName->get<std::string>();
156fc9e7fdaSChristopher Meis }
157fc9e7fdaSChristopher Meis
158fc9e7fdaSChristopher Meis sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(),
159fc9e7fdaSChristopher Meis "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s",
160fc9e7fdaSChristopher Meis "OpenBMC.0.1.InventoryRemoved",
161fc9e7fdaSChristopher Meis "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
162fc9e7fdaSChristopher Meis type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
163fc9e7fdaSChristopher Meis }
164