/* // Copyright (c) 2018 Intel Corporation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. */ /// \file entity_manager.hpp #pragma once #include "utils.hpp" #include #include #include #include #include #include #include #include struct DBusDeviceDescriptor { DBusInterface interface; std::string path; }; using FoundDevices = std::vector; struct CmpStr { bool operator()(const char* a, const char* b) const { return std::strcmp(a, b) < 0; } }; // underscore T for collison with dbus c api enum class probe_type_codes { FALSE_T, TRUE_T, AND, OR, FOUND, MATCH_ONE }; using FoundProbeTypeT = std::optional::const_iterator>; FoundProbeTypeT findProbeType(const std::string& probe); struct PerformScan : std::enable_shared_from_this { PerformScan(nlohmann::json& systemConfiguration, nlohmann::json& missingConfigurations, std::list& configurations, sdbusplus::asio::object_server& objServer, std::function&& callback); void updateSystemConfiguration(const nlohmann::json& recordRef, const std::string& probeName, FoundDevices& foundDevices); void run(); virtual ~PerformScan(); nlohmann::json& _systemConfiguration; nlohmann::json& _missingConfigurations; std::list _configurations; sdbusplus::asio::object_server& objServer; std::function _callback; bool _passed = false; MapperGetSubTreeResponse dbusProbeObjects; std::vector passedProbes; }; // this class finds the needed dbus fields and on destruction runs the probe struct PerformProbe : std::enable_shared_from_this { PerformProbe(nlohmann::json& recordRef, const std::vector& probeCommand, std::string probeName, std::shared_ptr& scanPtr); virtual ~PerformProbe(); nlohmann::json& recordRef; std::vector _probeCommand; std::string probeName; std::shared_ptr scan; }; inline void logDeviceAdded(const nlohmann::json& record) { if (!deviceHasLogging(record)) { return; } auto findType = record.find("Type"); auto findAsset = record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); std::string model = "Unknown"; std::string type = "Unknown"; std::string sn = "Unknown"; std::string name = "Unknown"; if (findType != record.end()) { type = findType->get(); } if (findAsset != record.end()) { auto findModel = findAsset->find("Model"); auto findSn = findAsset->find("SerialNumber"); if (findModel != findAsset->end()) { model = findModel->get(); } if (findSn != findAsset->end()) { const std::string* getSn = findSn->get_ptr(); if (getSn != nullptr) { sn = *getSn; } else { sn = findSn->dump(); } } } auto findName = record.find("Name"); if (findName != record.end()) { name = findName->get(); } sd_journal_send("MESSAGE=Inventory Added: %s", name.c_str(), "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded", "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL); } inline void logDeviceRemoved(const nlohmann::json& record) { if (!deviceHasLogging(record)) { return; } auto findType = record.find("Type"); auto findAsset = record.find("xyz.openbmc_project.Inventory.Decorator.Asset"); std::string model = "Unknown"; std::string type = "Unknown"; std::string sn = "Unknown"; std::string name = "Unknown"; if (findType != record.end()) { type = findType->get(); } if (findAsset != record.end()) { auto findModel = findAsset->find("Model"); auto findSn = findAsset->find("SerialNumber"); if (findModel != findAsset->end()) { model = findModel->get(); } if (findSn != findAsset->end()) { const std::string* getSn = findSn->get_ptr(); if (getSn != nullptr) { sn = *getSn; } else { sn = findSn->dump(); } } } auto findName = record.find("Name"); if (findName != record.end()) { name = findName->get(); } sd_journal_send("MESSAGE=Inventory Removed: %s", name.c_str(), "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved", "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(), type.c_str(), sn.c_str(), "NAME=%s", name.c_str(), NULL); }