blob: 548cc5f65d322b8f852cced1127ed33b8efd7718 [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*/
16
17#pragma once
18
19#include <systemd/sd-journal.h>
20
21#include <iostream>
James Feist1a996582019-05-14 15:10:06 -070022#include <nlohmann/json.hpp>
James Feist1df06a42019-04-11 14:23:04 -070023#include <string>
24
James Feist1a996582019-05-14 15:10:06 -070025inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070026{
27
James Feist1a996582019-05-14 15:10:06 -070028 auto findType = record.find("Type");
29 auto findAsset =
30 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
31
32 std::string model = "Unkown";
33 std::string type = "Unknown";
34 std::string sn = "Unkown";
35
36 if (findType != record.end())
37 {
38 type = findType->get<std::string>();
39 }
40 if (findAsset != record.end())
41 {
42 auto findModel = findAsset->find("Model");
43 auto findSn = findAsset->find("SerialNumber");
44 if (findModel != findAsset->end())
45 {
46 model = findModel->get<std::string>();
47 }
48 if (findSn != findAsset->end())
49 {
James Feistf5125b02019-06-06 11:27:43 -070050 const std::string* getSn = findSn->get_ptr<const std::string*>();
51 if (getSn != nullptr)
52 {
53 sn = *getSn;
54 }
55 else
56 {
57 sn = findSn->dump();
58 }
James Feist1a996582019-05-14 15:10:06 -070059 }
60 }
61
James Feist1df06a42019-04-11 14:23:04 -070062 sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR,
63 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -070064 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
65 type.c_str(), sn.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -070066}
67
James Feist1a996582019-05-14 15:10:06 -070068inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070069{
James Feist1a996582019-05-14 15:10:06 -070070 auto findType = record.find("Type");
71 auto findAsset =
72 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
73
74 std::string model = "Unkown";
75 std::string type = "Unknown";
76 std::string sn = "Unkown";
77
78 if (findType != record.end())
79 {
80 type = findType->get<std::string>();
81 }
82 if (findAsset != record.end())
83 {
84 auto findModel = findAsset->find("Model");
85 auto findSn = findAsset->find("SerialNumber");
86 if (findModel != findAsset->end())
87 {
88 model = findModel->get<std::string>();
89 }
90 if (findSn != findAsset->end())
91 {
James Feistf5125b02019-06-06 11:27:43 -070092 const std::string* getSn = findSn->get_ptr<const std::string*>();
93 if (getSn != nullptr)
94 {
95 sn = *getSn;
96 }
97 else
98 {
99 sn = findSn->dump();
100 }
James Feist1a996582019-05-14 15:10:06 -0700101 }
102 }
James Feist1df06a42019-04-11 14:23:04 -0700103
104 sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR,
105 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700106 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
107 type.c_str(), sn.c_str(), NULL);
James Feistf5125b02019-06-06 11:27:43 -0700108}
109
110enum class TemplateOperation
111{
112 addition,
113 division,
114 multiplication,
115 subtraction,
116 modulo,
117};