xref: /openbmc/entity-manager/src/entity_manager/log_device_inventory.cpp (revision 90e4f0583c56d3f9263ecc79cd7f3de05f051c32)
1 #include "log_device_inventory.hpp"
2 
3 #include "../utils.hpp"
4 
5 #include <systemd/sd-journal.h>
6 
7 #include <nlohmann/json.hpp>
8 #include <xyz/openbmc_project/Inventory/Decorator/Asset/common.hpp>
9 
10 #include <flat_map>
11 #include <string>
12 
setStringIfFound(std::string & value,const std::string & key,const nlohmann::json & record,bool dump=false)13 static void setStringIfFound(std::string& value, const std::string& key,
14                              const nlohmann::json& record, bool dump = false)
15 {
16     const nlohmann::json::const_iterator find = record.find(key);
17 
18     if (find == record.end())
19     {
20         return;
21     }
22 
23     const std::string* foundValue = find->get_ptr<const std::string*>();
24     if (foundValue != nullptr)
25     {
26         value = *foundValue;
27     }
28     else if (dump)
29     {
30         value = find->dump();
31     }
32 }
33 
queryInvInfo(const nlohmann::json & record)34 InvAddRemoveInfo queryInvInfo(const nlohmann::json& record)
35 {
36     InvAddRemoveInfo ret;
37 
38     setStringIfFound(ret.type, "Type", record);
39     setStringIfFound(ret.name, "Name", record);
40 
41     const nlohmann::json::const_iterator findAsset = record.find(
42         sdbusplus::common::xyz::openbmc_project::inventory::decorator::Asset::
43             interface);
44 
45     if (findAsset != record.end())
46     {
47         setStringIfFound(ret.model, "Model", *findAsset);
48         setStringIfFound(ret.sn, "SerialNumber", *findAsset, true);
49     }
50 
51     return ret;
52 }
53 
logDeviceAdded(const nlohmann::json & record)54 void logDeviceAdded(const nlohmann::json& record)
55 {
56     if (!EM_CACHE_CONFIGURATION)
57     {
58         return;
59     }
60     if (!deviceHasLogging(record))
61     {
62         return;
63     }
64 
65     const InvAddRemoveInfo info = queryInvInfo(record);
66 
67     sd_journal_send(
68         "MESSAGE=Inventory Added: %s", info.name.c_str(), "PRIORITY=%i",
69         LOG_INFO, "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded",
70         "REDFISH_MESSAGE_ARGS=%s,%s,%s", info.model.c_str(), info.type.c_str(),
71         info.sn.c_str(), "NAME=%s", info.name.c_str(), NULL);
72 }
73 
logDeviceRemoved(const nlohmann::json & record)74 void logDeviceRemoved(const nlohmann::json& record)
75 {
76     if (!deviceHasLogging(record))
77     {
78         return;
79     }
80 
81     const InvAddRemoveInfo info = queryInvInfo(record);
82 
83     sd_journal_send(
84         "MESSAGE=Inventory Removed: %s", info.name.c_str(), "PRIORITY=%i",
85         LOG_INFO, "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved",
86         "REDFISH_MESSAGE_ARGS=%s,%s,%s", info.model.c_str(), info.type.c_str(),
87         info.sn.c_str(), "NAME=%s", info.name.c_str(), NULL);
88 }
89