xref: /openbmc/entity-manager/src/entity_manager/entity_manager.hpp (revision cfc7f4f423c8163c394fbd777afcaf10a835206f)
1 /*
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 */
16 /// \file entity_manager.hpp
17 
18 #pragma once
19 
20 #include "../utils.hpp"
21 
22 #include <systemd/sd-journal.h>
23 
24 #include <boost/container/flat_map.hpp>
25 #include <nlohmann/json.hpp>
26 #include <sdbusplus/asio/object_server.hpp>
27 
28 #include <string>
29 
30 inline void logDeviceAdded(const nlohmann::json& record)
31 {
32     if (!deviceHasLogging(record))
33     {
34         return;
35     }
36     auto findType = record.find("Type");
37     auto findAsset =
38         record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
39 
40     std::string model = "Unknown";
41     std::string type = "Unknown";
42     std::string sn = "Unknown";
43     std::string name = "Unknown";
44 
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         {
59             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             }
68         }
69     }
70 
71     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",
80                     "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
81                     type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
82 }
83 
84 inline void logDeviceRemoved(const nlohmann::json& record)
85 {
86     if (!deviceHasLogging(record))
87     {
88         return;
89     }
90     auto findType = record.find("Type");
91     auto findAsset =
92         record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
93 
94     std::string model = "Unknown";
95     std::string type = "Unknown";
96     std::string sn = "Unknown";
97     std::string name = "Unknown";
98 
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         {
113             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             }
122         }
123     }
124 
125     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",
134                     "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
135                     type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL);
136 }
137