| 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 | */ | 
| Brad Bishop | 1fb9f3f | 2020-08-28 08:15:13 -0400 | [diff] [blame^] | 16 | /// \file EntityManager.hpp | 
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 17 |  | 
 | 18 | #pragma once | 
 | 19 |  | 
| James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 20 | #include "Utils.hpp" | 
 | 21 |  | 
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 22 | #include <systemd/sd-journal.h> | 
 | 23 |  | 
| James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 24 | #include <boost/container/flat_map.hpp> | 
| James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 25 | #include <nlohmann/json.hpp> | 
| James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 26 | #include <sdbusplus/asio/object_server.hpp> | 
| James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 27 |  | 
 | 28 | #include <iostream> | 
 | 29 | #include <list> | 
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 30 | #include <string> | 
 | 31 |  | 
| James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 32 | using DBusProbeObjectT = boost::container::flat_map< | 
 | 33 |     std::string, | 
 | 34 |     std::vector<boost::container::flat_map<std::string, BasicVariantType>>>; | 
 | 35 |  | 
| James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 36 | using FoundDeviceT = | 
 | 37 |     std::vector<boost::container::flat_map<std::string, BasicVariantType>>; | 
 | 38 |  | 
| James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 39 | struct PerformScan : std::enable_shared_from_this<PerformScan> | 
 | 40 | { | 
 | 41 |  | 
 | 42 |     PerformScan(nlohmann::json& systemConfiguration, | 
 | 43 |                 nlohmann::json& missingConfigurations, | 
 | 44 |                 std::list<nlohmann::json>& configurations, | 
| James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 45 |                 sdbusplus::asio::object_server& objServer, | 
 | 46 |                 std::function<void()>&& callback); | 
| James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 47 |     void run(void); | 
 | 48 |     virtual ~PerformScan(); | 
 | 49 |     nlohmann::json& _systemConfiguration; | 
 | 50 |     nlohmann::json& _missingConfigurations; | 
 | 51 |     std::list<nlohmann::json> _configurations; | 
| James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 52 |     sdbusplus::asio::object_server& objServer; | 
 | 53 |     std::function<void()> _callback; | 
| James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 54 |     bool _passed = false; | 
 | 55 |     bool powerWasOn = isPowerOn(); | 
 | 56 |     DBusProbeObjectT dbusProbeObjects; | 
 | 57 |     std::vector<std::string> passedProbes; | 
 | 58 | }; | 
 | 59 |  | 
| James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 60 | // this class finds the needed dbus fields and on destruction runs the probe | 
 | 61 | struct PerformProbe : std::enable_shared_from_this<PerformProbe> | 
 | 62 | { | 
 | 63 |     PerformProbe(const std::vector<std::string>& probeCommand, | 
 | 64 |                  std::shared_ptr<PerformScan>& scanPtr, | 
 | 65 |                  std::function<void(FoundDeviceT&)>&& callback); | 
 | 66 |     virtual ~PerformProbe(); | 
 | 67 |  | 
 | 68 |     std::vector<std::string> _probeCommand; | 
 | 69 |     std::shared_ptr<PerformScan> scan; | 
 | 70 |     std::function<void(FoundDeviceT&)> _callback; | 
 | 71 | }; | 
 | 72 |  | 
| James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 73 | inline void logDeviceAdded(const nlohmann::json& record) | 
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 74 | { | 
 | 75 |  | 
| James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 76 |     if (!deviceHasLogging(record)) | 
 | 77 |     { | 
 | 78 |         return; | 
 | 79 |     } | 
| James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 80 |     auto findType = record.find("Type"); | 
 | 81 |     auto findAsset = | 
 | 82 |         record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); | 
 | 83 |  | 
 | 84 |     std::string model = "Unkown"; | 
 | 85 |     std::string type = "Unknown"; | 
 | 86 |     std::string sn = "Unkown"; | 
 | 87 |  | 
 | 88 |     if (findType != record.end()) | 
 | 89 |     { | 
 | 90 |         type = findType->get<std::string>(); | 
 | 91 |     } | 
 | 92 |     if (findAsset != record.end()) | 
 | 93 |     { | 
 | 94 |         auto findModel = findAsset->find("Model"); | 
 | 95 |         auto findSn = findAsset->find("SerialNumber"); | 
 | 96 |         if (findModel != findAsset->end()) | 
 | 97 |         { | 
 | 98 |             model = findModel->get<std::string>(); | 
 | 99 |         } | 
 | 100 |         if (findSn != findAsset->end()) | 
 | 101 |         { | 
| James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 102 |             const std::string* getSn = findSn->get_ptr<const std::string*>(); | 
 | 103 |             if (getSn != nullptr) | 
 | 104 |             { | 
 | 105 |                 sn = *getSn; | 
 | 106 |             } | 
 | 107 |             else | 
 | 108 |             { | 
 | 109 |                 sn = findSn->dump(); | 
 | 110 |             } | 
| James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 111 |         } | 
 | 112 |     } | 
 | 113 |  | 
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 114 |     sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR, | 
 | 115 |                     "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded", | 
| James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 116 |                     "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), | 
 | 117 |                     type.c_str(), sn.c_str(), NULL); | 
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 118 | } | 
 | 119 |  | 
| James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 120 | inline void logDeviceRemoved(const nlohmann::json& record) | 
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 121 | { | 
| James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 122 |     if (!deviceHasLogging(record)) | 
 | 123 |     { | 
 | 124 |         return; | 
 | 125 |     } | 
| James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 126 |     auto findType = record.find("Type"); | 
 | 127 |     auto findAsset = | 
 | 128 |         record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); | 
 | 129 |  | 
 | 130 |     std::string model = "Unkown"; | 
 | 131 |     std::string type = "Unknown"; | 
 | 132 |     std::string sn = "Unkown"; | 
 | 133 |  | 
 | 134 |     if (findType != record.end()) | 
 | 135 |     { | 
 | 136 |         type = findType->get<std::string>(); | 
 | 137 |     } | 
 | 138 |     if (findAsset != record.end()) | 
 | 139 |     { | 
 | 140 |         auto findModel = findAsset->find("Model"); | 
 | 141 |         auto findSn = findAsset->find("SerialNumber"); | 
 | 142 |         if (findModel != findAsset->end()) | 
 | 143 |         { | 
 | 144 |             model = findModel->get<std::string>(); | 
 | 145 |         } | 
 | 146 |         if (findSn != findAsset->end()) | 
 | 147 |         { | 
| James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 148 |             const std::string* getSn = findSn->get_ptr<const std::string*>(); | 
 | 149 |             if (getSn != nullptr) | 
 | 150 |             { | 
 | 151 |                 sn = *getSn; | 
 | 152 |             } | 
 | 153 |             else | 
 | 154 |             { | 
 | 155 |                 sn = findSn->dump(); | 
 | 156 |             } | 
| James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 157 |         } | 
 | 158 |     } | 
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 159 |  | 
 | 160 |     sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR, | 
 | 161 |                     "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved", | 
| James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 162 |                     "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), | 
 | 163 |                     type.c_str(), sn.c_str(), NULL); | 
| James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 164 | } |