xref: /openbmc/bmcweb/features/redfish/include/utils/asset_utils.hpp (revision 83237dd618c38a54aaa91b9fe4968eeb454a5dfa)
11ac7f6cfSMyung Bae // SPDX-License-Identifier: Apache-2.0
21ac7f6cfSMyung Bae // SPDX-FileCopyrightText: Copyright OpenBMC Authors
31ac7f6cfSMyung Bae #pragma once
41ac7f6cfSMyung Bae 
51ac7f6cfSMyung Bae #include "async_resp.hpp"
61ac7f6cfSMyung Bae #include "dbus_utility.hpp"
71ac7f6cfSMyung Bae #include "error_messages.hpp"
81ac7f6cfSMyung Bae #include "logging.hpp"
91ac7f6cfSMyung Bae #include "utils/dbus_utils.hpp"
101ac7f6cfSMyung Bae 
111ac7f6cfSMyung Bae #include <asm-generic/errno.h>
121ac7f6cfSMyung Bae 
131ac7f6cfSMyung Bae #include <boost/system/error_code.hpp>
141ac7f6cfSMyung Bae #include <nlohmann/json.hpp>
151ac7f6cfSMyung Bae #include <sdbusplus/unpack_properties.hpp>
161ac7f6cfSMyung Bae 
171ac7f6cfSMyung Bae #include <functional>
181ac7f6cfSMyung Bae #include <memory>
191ac7f6cfSMyung Bae #include <ranges>
201ac7f6cfSMyung Bae #include <string>
211ac7f6cfSMyung Bae 
221ac7f6cfSMyung Bae namespace redfish
231ac7f6cfSMyung Bae {
241ac7f6cfSMyung Bae namespace asset_utils
251ac7f6cfSMyung Bae {
261ac7f6cfSMyung Bae 
271ac7f6cfSMyung Bae inline void extractAssetInfo(
281ac7f6cfSMyung Bae     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
291ac7f6cfSMyung Bae     const nlohmann::json::json_pointer& jsonKeyName,
301ac7f6cfSMyung Bae     const dbus::utility::DBusPropertiesMap& assetList,
31*83237dd6SSunnySrivastava1984     bool includeSparePartNumber = false, bool includeManufacturer = true)
321ac7f6cfSMyung Bae {
331ac7f6cfSMyung Bae     const std::string* manufacturer = nullptr;
341ac7f6cfSMyung Bae     const std::string* model = nullptr;
351ac7f6cfSMyung Bae     const std::string* partNumber = nullptr;
361ac7f6cfSMyung Bae     const std::string* serialNumber = nullptr;
371ac7f6cfSMyung Bae     const std::string* sparePartNumber = nullptr;
381ac7f6cfSMyung Bae 
391ac7f6cfSMyung Bae     const bool success = sdbusplus::unpackPropertiesNoThrow(
401ac7f6cfSMyung Bae         dbus_utils::UnpackErrorPrinter(), assetList, "Manufacturer",
411ac7f6cfSMyung Bae         manufacturer, "Model", model, "PartNumber", partNumber, "SerialNumber",
421ac7f6cfSMyung Bae         serialNumber, "SparePartNumber", sparePartNumber);
431ac7f6cfSMyung Bae     if (!success)
441ac7f6cfSMyung Bae     {
451ac7f6cfSMyung Bae         messages::internalError(asyncResp->res);
461ac7f6cfSMyung Bae         return;
471ac7f6cfSMyung Bae     }
481ac7f6cfSMyung Bae 
491ac7f6cfSMyung Bae     nlohmann::json& assetData = asyncResp->res.jsonValue[jsonKeyName];
501ac7f6cfSMyung Bae 
51*83237dd6SSunnySrivastava1984     if (includeManufacturer && manufacturer != nullptr)
521ac7f6cfSMyung Bae     {
531ac7f6cfSMyung Bae         assetData["Manufacturer"] = *manufacturer;
541ac7f6cfSMyung Bae     }
551ac7f6cfSMyung Bae     if (model != nullptr)
561ac7f6cfSMyung Bae     {
571ac7f6cfSMyung Bae         assetData["Model"] = *model;
581ac7f6cfSMyung Bae     }
591ac7f6cfSMyung Bae     if (partNumber != nullptr)
601ac7f6cfSMyung Bae     {
611ac7f6cfSMyung Bae         assetData["PartNumber"] = *partNumber;
621ac7f6cfSMyung Bae     }
631ac7f6cfSMyung Bae     if (serialNumber != nullptr)
641ac7f6cfSMyung Bae     {
651ac7f6cfSMyung Bae         assetData["SerialNumber"] = *serialNumber;
661ac7f6cfSMyung Bae     }
671ac7f6cfSMyung Bae     // SparePartNumber is optional on D-Bus so skip if it is empty
681ac7f6cfSMyung Bae     if (includeSparePartNumber && sparePartNumber != nullptr &&
691ac7f6cfSMyung Bae         !sparePartNumber->empty())
701ac7f6cfSMyung Bae     {
711ac7f6cfSMyung Bae         assetData["SparePartNumber"] = *sparePartNumber;
721ac7f6cfSMyung Bae     }
731ac7f6cfSMyung Bae }
741ac7f6cfSMyung Bae 
75*83237dd6SSunnySrivastava1984 inline void getAssetInfo(
76*83237dd6SSunnySrivastava1984     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
77*83237dd6SSunnySrivastava1984     const std::string& serviceName, const std::string& dbusPath,
781ac7f6cfSMyung Bae     const nlohmann::json::json_pointer& jsonKeyName,
79*83237dd6SSunnySrivastava1984     bool includeSparePartNumber = false, bool includeManufacturer = true)
801ac7f6cfSMyung Bae {
811ac7f6cfSMyung Bae     dbus::utility::getAllProperties(
821ac7f6cfSMyung Bae         serviceName, dbusPath, "xyz.openbmc_project.Inventory.Decorator.Asset",
83*83237dd6SSunnySrivastava1984         [asyncResp, jsonKeyName, includeSparePartNumber, includeManufacturer](
841ac7f6cfSMyung Bae             const boost::system::error_code& ec,
851ac7f6cfSMyung Bae             const dbus::utility::DBusPropertiesMap& assetList) {
861ac7f6cfSMyung Bae             if (ec)
871ac7f6cfSMyung Bae             {
881ac7f6cfSMyung Bae                 if (ec.value() != EBADR)
891ac7f6cfSMyung Bae                 {
901ac7f6cfSMyung Bae                     BMCWEB_LOG_ERROR("DBUS response error for Properties {}",
911ac7f6cfSMyung Bae                                      ec.value());
921ac7f6cfSMyung Bae                     messages::internalError(asyncResp->res);
931ac7f6cfSMyung Bae                 }
941ac7f6cfSMyung Bae                 return;
951ac7f6cfSMyung Bae             }
961ac7f6cfSMyung Bae             extractAssetInfo(asyncResp, jsonKeyName, assetList,
97*83237dd6SSunnySrivastava1984                              includeSparePartNumber, includeManufacturer);
981ac7f6cfSMyung Bae         });
991ac7f6cfSMyung Bae }
1001ac7f6cfSMyung Bae 
1011ac7f6cfSMyung Bae } // namespace asset_utils
1021ac7f6cfSMyung Bae } // namespace redfish
103