blob: 4d8d69dcd9b07eeb270a82b48442d33e19487254 [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
Brad Bishope45d8c72022-05-25 15:12:53 -040020#include "utils.hpp"
James Feist733f7652019-11-13 14:32:29 -080021
James Feist1df06a42019-04-11 14:23:04 -070022#include <systemd/sd-journal.h>
23
James Feist733f7652019-11-13 14:32:29 -080024#include <boost/container/flat_map.hpp>
James Feist1a996582019-05-14 15:10:06 -070025#include <nlohmann/json.hpp>
James Feist4dc617b2020-05-01 09:54:47 -070026#include <sdbusplus/asio/object_server.hpp>
James Feist8c505da2020-05-28 10:06:33 -070027
James Feist1df06a42019-04-11 14:23:04 -070028#include <string>
29
James Feist1a996582019-05-14 15:10:06 -070030inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070031{
James Feist1ffa4a42020-04-22 18:27:17 -070032 if (!deviceHasLogging(record))
33 {
34 return;
35 }
James Feist1a996582019-05-14 15:10:06 -070036 auto findType = record.find("Type");
37 auto findAsset =
38 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
39
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030040 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070041 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030042 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -050043 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070044
45 if (findType != record.end())
46 {
47 type = findType->get<std::string>();
48 }
49 if (findAsset != record.end())
50 {
51 auto findModel = findAsset->find("Model");
52 auto findSn = findAsset->find("SerialNumber");
53 if (findModel != findAsset->end())
54 {
55 model = findModel->get<std::string>();
56 }
57 if (findSn != findAsset->end())
58 {
James Feistf5125b02019-06-06 11:27:43 -070059 const std::string* getSn = findSn->get_ptr<const std::string*>();
60 if (getSn != nullptr)
61 {
62 sn = *getSn;
63 }
64 else
65 {
66 sn = findSn->dump();
67 }
James Feist1a996582019-05-14 15:10:06 -070068 }
69 }
70
Matt Spinler200bf402022-08-04 09:14:18 -050071 auto findName = record.find("Name");
72 if (findName != record.end())
73 {
74 name = findName->get<std::string>();
75 }
76
77 sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i",
78 LOG_INFO, "REDFISH_MESSAGE_ID=%s",
79 "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -070080 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -050081 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -070082}
83
James Feist1a996582019-05-14 15:10:06 -070084inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070085{
James Feist1ffa4a42020-04-22 18:27:17 -070086 if (!deviceHasLogging(record))
87 {
88 return;
89 }
James Feist1a996582019-05-14 15:10:06 -070090 auto findType = record.find("Type");
91 auto findAsset =
92 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
93
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030094 std::string model = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070095 std::string type = "Unknown";
Konstantin Aladysheve7ac9c92021-09-10 09:20:17 +030096 std::string sn = "Unknown";
Matt Spinler200bf402022-08-04 09:14:18 -050097 std::string name = "Unknown";
James Feist1a996582019-05-14 15:10:06 -070098
99 if (findType != record.end())
100 {
101 type = findType->get<std::string>();
102 }
103 if (findAsset != record.end())
104 {
105 auto findModel = findAsset->find("Model");
106 auto findSn = findAsset->find("SerialNumber");
107 if (findModel != findAsset->end())
108 {
109 model = findModel->get<std::string>();
110 }
111 if (findSn != findAsset->end())
112 {
James Feistf5125b02019-06-06 11:27:43 -0700113 const std::string* getSn = findSn->get_ptr<const std::string*>();
114 if (getSn != nullptr)
115 {
116 sn = *getSn;
117 }
118 else
119 {
120 sn = findSn->dump();
121 }
James Feist1a996582019-05-14 15:10:06 -0700122 }
123 }
James Feist1df06a42019-04-11 14:23:04 -0700124
Matt Spinler200bf402022-08-04 09:14:18 -0500125 auto findName = record.find("Name");
126 if (findName != record.end())
127 {
128 name = findName->get<std::string>();
129 }
130
131 sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(),
132 "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s",
133 "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700134 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
Matt Spinler200bf402022-08-04 09:14:18 -0500135 type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700136}