James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 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 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <systemd/sd-journal.h> |
| 20 | |
| 21 | #include <iostream> |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 22 | #include <nlohmann/json.hpp> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 25 | inline void logDeviceAdded(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 26 | { |
| 27 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 28 | 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 Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame^] | 50 | 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 Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 62 | sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR, |
| 63 | "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 64 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
| 65 | type.c_str(), sn.c_str(), NULL); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 66 | } |
| 67 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 68 | inline void logDeviceRemoved(const nlohmann::json& record) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 69 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 70 | 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 Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame^] | 92 | 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 Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 103 | |
| 104 | sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR, |
| 105 | "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved", |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 106 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), |
| 107 | type.c_str(), sn.c_str(), NULL); |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame^] | 108 | } |
| 109 | |
| 110 | enum class TemplateOperation |
| 111 | { |
| 112 | addition, |
| 113 | division, |
| 114 | multiplication, |
| 115 | subtraction, |
| 116 | modulo, |
| 117 | }; |