140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
340e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2019 Intel Corporation
4a25aeccfSNikhil Potade #pragma once
5a25aeccfSNikhil Potade
613451e39SWilly Tu #include "bmcweb_config.h"
713451e39SWilly Tu
83ccb3adbSEd Tanous #include "app.hpp"
9d7857201SEd Tanous #include "async_resp.hpp"
107a1dbc48SGeorge Liu #include "dbus_utility.hpp"
11d7857201SEd Tanous #include "error_messages.hpp"
12e5029d88SJohn Edward Broadbent #include "generated/enums/drive.hpp"
13dde9bc12SGeorge Liu #include "generated/enums/protocol.hpp"
14539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
15d7857201SEd Tanous #include "http_request.hpp"
16a8e884fcSEd Tanous #include "human_sort.hpp"
17d7857201SEd Tanous #include "logging.hpp"
183ccb3adbSEd Tanous #include "query.hpp"
19a8e884fcSEd Tanous #include "redfish_util.hpp"
203ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
215e577bc1SWilly Tu #include "utils/collection.hpp"
223ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
232ad9c2f6SJames Feist
24d7857201SEd Tanous #include <boost/beast/http/verb.hpp>
25e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
26ef4c65b7SEd Tanous #include <boost/url/format.hpp>
27d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
28d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
29a25aeccfSNikhil Potade
30d7857201SEd Tanous #include <algorithm>
317a1dbc48SGeorge Liu #include <array>
32d7857201SEd Tanous #include <cstdint>
33d7857201SEd Tanous #include <format>
34d7857201SEd Tanous #include <functional>
35d7857201SEd Tanous #include <memory>
36d7857201SEd Tanous #include <optional>
373544d2a7SEd Tanous #include <ranges>
38d7857201SEd Tanous #include <string>
397a1dbc48SGeorge Liu #include <string_view>
40d7857201SEd Tanous #include <utility>
41d7857201SEd Tanous #include <variant>
42d7857201SEd Tanous #include <vector>
437a1dbc48SGeorge Liu
44a25aeccfSNikhil Potade namespace redfish
45a25aeccfSNikhil Potade {
4636d52334SEd Tanous
handleSystemsStorageCollectionGet(App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & systemName)4736d52334SEd Tanous inline void handleSystemsStorageCollectionGet(
4836d52334SEd Tanous App& app, const crow::Request& req,
4922d268cbSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5036d52334SEd Tanous const std::string& systemName)
5136d52334SEd Tanous {
523ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp))
5345ca1b86SEd Tanous {
5445ca1b86SEd Tanous return;
5545ca1b86SEd Tanous }
56253f11b8SEd Tanous if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
5722d268cbSEd Tanous {
5822d268cbSEd Tanous messages::resourceNotFound(asyncResp->res, "ComputerSystem",
5922d268cbSEd Tanous systemName);
6022d268cbSEd Tanous return;
6122d268cbSEd Tanous }
6222d268cbSEd Tanous
638d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] =
648d1b46d7Szhanghch05 "#StorageCollection.StorageCollection";
65253f11b8SEd Tanous asyncResp->res.jsonValue["@odata.id"] = std::format(
66253f11b8SEd Tanous "/redfish/v1/Systems/{}/Storage", BMCWEB_REDFISH_SYSTEM_URI_NAME);
678d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Storage Collection";
685e577bc1SWilly Tu
695e577bc1SWilly Tu constexpr std::array<std::string_view, 1> interface{
705a39f77aSPatrick Williams "xyz.openbmc_project.Inventory.Item.Storage"};
715e577bc1SWilly Tu collection_util::getCollectionMembers(
72253f11b8SEd Tanous asyncResp,
73253f11b8SEd Tanous boost::urls::format("/redfish/v1/Systems/{}/Storage",
74253f11b8SEd Tanous BMCWEB_REDFISH_SYSTEM_URI_NAME),
7536b5f1edSLakshmi Yadlapati interface, "/xyz/openbmc_project/inventory");
765e577bc1SWilly Tu }
775e577bc1SWilly Tu
handleStorageCollectionGet(App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp)785e577bc1SWilly Tu inline void handleStorageCollectionGet(
795e577bc1SWilly Tu App& app, const crow::Request& req,
805e577bc1SWilly Tu const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
815e577bc1SWilly Tu {
825e577bc1SWilly Tu if (!redfish::setUpRedfishRoute(app, req, asyncResp))
835e577bc1SWilly Tu {
845e577bc1SWilly Tu return;
855e577bc1SWilly Tu }
865e577bc1SWilly Tu asyncResp->res.jsonValue["@odata.type"] =
875e577bc1SWilly Tu "#StorageCollection.StorageCollection";
885e577bc1SWilly Tu asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Storage";
895e577bc1SWilly Tu asyncResp->res.jsonValue["Name"] = "Storage Collection";
905e577bc1SWilly Tu constexpr std::array<std::string_view, 1> interface{
915a39f77aSPatrick Williams "xyz.openbmc_project.Inventory.Item.Storage"};
925e577bc1SWilly Tu collection_util::getCollectionMembers(
9336b5f1edSLakshmi Yadlapati asyncResp, boost::urls::format("/redfish/v1/Storage"), interface,
9436b5f1edSLakshmi Yadlapati "/xyz/openbmc_project/inventory");
95a25aeccfSNikhil Potade }
96a25aeccfSNikhil Potade
requestRoutesStorageCollection(App & app)9736d52334SEd Tanous inline void requestRoutesStorageCollection(App& app)
98a25aeccfSNikhil Potade {
9936d52334SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
10036d52334SEd Tanous .privileges(redfish::privileges::getStorageCollection)
10136d52334SEd Tanous .methods(boost::beast::http::verb::get)(
10236d52334SEd Tanous std::bind_front(handleSystemsStorageCollectionGet, std::ref(app)));
1035e577bc1SWilly Tu BMCWEB_ROUTE(app, "/redfish/v1/Storage/")
1045e577bc1SWilly Tu .privileges(redfish::privileges::getStorageCollection)
1055e577bc1SWilly Tu .methods(boost::beast::http::verb::get)(
1065e577bc1SWilly Tu std::bind_front(handleStorageCollectionGet, std::ref(app)));
10736d52334SEd Tanous }
10836d52334SEd Tanous
afterChassisDriveCollectionSubtree(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const boost::system::error_code & ec,const dbus::utility::MapperGetSubTreePathsResponse & driveList)10936d52334SEd Tanous inline void afterChassisDriveCollectionSubtree(
11036d52334SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1117a1dbc48SGeorge Liu const boost::system::error_code& ec,
11236d52334SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& driveList)
11336d52334SEd Tanous {
114a25aeccfSNikhil Potade if (ec)
115a25aeccfSNikhil Potade {
11662598e31SEd Tanous BMCWEB_LOG_ERROR("Drive mapper call error");
117a25aeccfSNikhil Potade messages::internalError(asyncResp->res);
118a25aeccfSNikhil Potade return;
119a25aeccfSNikhil Potade }
1202ad9c2f6SJames Feist
121a85afbe1SWilly Tu nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
122a85afbe1SWilly Tu driveArray = nlohmann::json::array();
123a85afbe1SWilly Tu auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
124a85afbe1SWilly Tu count = 0;
1252ad9c2f6SJames Feist
126a85afbe1SWilly Tu for (const std::string& drive : driveList)
127a25aeccfSNikhil Potade {
128a85afbe1SWilly Tu sdbusplus::message::object_path object(drive);
129a85afbe1SWilly Tu if (object.filename().empty())
130a25aeccfSNikhil Potade {
13162598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to find filename in {}", drive);
132a85afbe1SWilly Tu return;
133a25aeccfSNikhil Potade }
134a85afbe1SWilly Tu
135a85afbe1SWilly Tu nlohmann::json::object_t driveJson;
136ef4c65b7SEd Tanous driveJson["@odata.id"] = boost::urls::format(
137253f11b8SEd Tanous "/redfish/v1/Systems/{}/Storage/1/Drives/{}",
138253f11b8SEd Tanous BMCWEB_REDFISH_SYSTEM_URI_NAME, object.filename());
139b2ba3072SPatrick Williams driveArray.emplace_back(std::move(driveJson));
140a25aeccfSNikhil Potade }
141a25aeccfSNikhil Potade
142a85afbe1SWilly Tu count = driveArray.size();
14336d52334SEd Tanous }
getDrives(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp)1447ac13cc9SGunnar Mills inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14536d52334SEd Tanous {
14636d52334SEd Tanous const std::array<std::string_view, 1> interfaces = {
14736d52334SEd Tanous "xyz.openbmc_project.Inventory.Item.Drive"};
14836d52334SEd Tanous dbus::utility::getSubTreePaths(
14936d52334SEd Tanous "/xyz/openbmc_project/inventory", 0, interfaces,
1507ac13cc9SGunnar Mills std::bind_front(afterChassisDriveCollectionSubtree, asyncResp));
151a85afbe1SWilly Tu }
152e284a7c1SJames Feist
afterSystemsStorageGetSubtree(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & storageId,const boost::system::error_code & ec,const dbus::utility::MapperGetSubTreeResponse & subtree)1535e577bc1SWilly Tu inline void afterSystemsStorageGetSubtree(
1545e577bc1SWilly Tu const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1555e577bc1SWilly Tu const std::string& storageId, const boost::system::error_code& ec,
1565e577bc1SWilly Tu const dbus::utility::MapperGetSubTreeResponse& subtree)
157a85afbe1SWilly Tu {
1585e577bc1SWilly Tu if (ec)
159a85afbe1SWilly Tu {
16062598e31SEd Tanous BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
1615e577bc1SWilly Tu messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
1625e577bc1SWilly Tu storageId);
163a85afbe1SWilly Tu return;
164a85afbe1SWilly Tu }
1653544d2a7SEd Tanous auto storage = std::ranges::find_if(
1663544d2a7SEd Tanous subtree,
1675e577bc1SWilly Tu [&storageId](const std::pair<std::string,
1685e577bc1SWilly Tu dbus::utility::MapperServiceMap>& object) {
1695e577bc1SWilly Tu return sdbusplus::message::object_path(object.first).filename() ==
1705e577bc1SWilly Tu storageId;
1715e577bc1SWilly Tu });
1725e577bc1SWilly Tu if (storage == subtree.end())
1735e577bc1SWilly Tu {
1745e577bc1SWilly Tu messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
1755e577bc1SWilly Tu storageId);
1765e577bc1SWilly Tu return;
1775e577bc1SWilly Tu }
1785e577bc1SWilly Tu
17961b1eb21SWilly Tu asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
180a85afbe1SWilly Tu asyncResp->res.jsonValue["@odata.id"] =
181253f11b8SEd Tanous boost::urls::format("/redfish/v1/Systems/{}/Storage/{}",
182253f11b8SEd Tanous BMCWEB_REDFISH_SYSTEM_URI_NAME, storageId);
183a85afbe1SWilly Tu asyncResp->res.jsonValue["Name"] = "Storage";
1845e577bc1SWilly Tu asyncResp->res.jsonValue["Id"] = storageId;
185539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
186a85afbe1SWilly Tu
1877ac13cc9SGunnar Mills getDrives(asyncResp);
188253f11b8SEd Tanous asyncResp->res.jsonValue["Controllers"]["@odata.id"] =
189253f11b8SEd Tanous boost::urls::format("/redfish/v1/Systems/{}/Storage/{}/Controllers",
190253f11b8SEd Tanous BMCWEB_REDFISH_SYSTEM_URI_NAME, storageId);
1915e577bc1SWilly Tu }
1925e577bc1SWilly Tu
handleSystemsStorageGet(App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & systemName,const std::string & storageId)193bd79bce8SPatrick Williams inline void handleSystemsStorageGet(
194bd79bce8SPatrick Williams App& app, const crow::Request& req,
1955e577bc1SWilly Tu const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
196bd79bce8SPatrick Williams const std::string& systemName, const std::string& storageId)
1975e577bc1SWilly Tu {
1985e577bc1SWilly Tu if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1995e577bc1SWilly Tu {
2005e577bc1SWilly Tu return;
2015e577bc1SWilly Tu }
20225b54dbaSEd Tanous if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
2037f3e84a1SEd Tanous {
2047f3e84a1SEd Tanous // Option currently returns no systems. TBD
2057f3e84a1SEd Tanous messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2067f3e84a1SEd Tanous systemName);
2077f3e84a1SEd Tanous return;
2087f3e84a1SEd Tanous }
2095e577bc1SWilly Tu
2105e577bc1SWilly Tu constexpr std::array<std::string_view, 1> interfaces = {
2115e577bc1SWilly Tu "xyz.openbmc_project.Inventory.Item.Storage"};
2125e577bc1SWilly Tu dbus::utility::getSubTree(
2135e577bc1SWilly Tu "/xyz/openbmc_project/inventory", 0, interfaces,
2145e577bc1SWilly Tu std::bind_front(afterSystemsStorageGetSubtree, asyncResp, storageId));
2155e577bc1SWilly Tu }
2165e577bc1SWilly Tu
afterSubtree(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & storageId,const boost::system::error_code & ec,const dbus::utility::MapperGetSubTreeResponse & subtree)2175e577bc1SWilly Tu inline void afterSubtree(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2185e577bc1SWilly Tu const std::string& storageId,
2195e577bc1SWilly Tu const boost::system::error_code& ec,
2205e577bc1SWilly Tu const dbus::utility::MapperGetSubTreeResponse& subtree)
2215e577bc1SWilly Tu {
2225e577bc1SWilly Tu if (ec)
2235e577bc1SWilly Tu {
22462598e31SEd Tanous BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
2255e577bc1SWilly Tu messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
2265e577bc1SWilly Tu storageId);
2275e577bc1SWilly Tu return;
2285e577bc1SWilly Tu }
2293544d2a7SEd Tanous auto storage = std::ranges::find_if(
2303544d2a7SEd Tanous subtree,
2315e577bc1SWilly Tu [&storageId](const std::pair<std::string,
2325e577bc1SWilly Tu dbus::utility::MapperServiceMap>& object) {
2335e577bc1SWilly Tu return sdbusplus::message::object_path(object.first).filename() ==
2345e577bc1SWilly Tu storageId;
2355e577bc1SWilly Tu });
2365e577bc1SWilly Tu if (storage == subtree.end())
2375e577bc1SWilly Tu {
2385e577bc1SWilly Tu messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
2395e577bc1SWilly Tu storageId);
2405e577bc1SWilly Tu return;
2415e577bc1SWilly Tu }
2425e577bc1SWilly Tu
2435e577bc1SWilly Tu asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
2445e577bc1SWilly Tu asyncResp->res.jsonValue["@odata.id"] =
2455e577bc1SWilly Tu boost::urls::format("/redfish/v1/Storage/{}", storageId);
2465e577bc1SWilly Tu asyncResp->res.jsonValue["Name"] = "Storage";
2475e577bc1SWilly Tu asyncResp->res.jsonValue["Id"] = storageId;
248539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
2495e577bc1SWilly Tu
2505e577bc1SWilly Tu // Storage subsystem to Storage link.
2515e577bc1SWilly Tu nlohmann::json::array_t storageServices;
2525e577bc1SWilly Tu nlohmann::json::object_t storageService;
2535e577bc1SWilly Tu storageService["@odata.id"] =
254253f11b8SEd Tanous boost::urls::format("/redfish/v1/Systems/{}/Storage/{}",
255253f11b8SEd Tanous BMCWEB_REDFISH_SYSTEM_URI_NAME, storageId);
2565e577bc1SWilly Tu storageServices.emplace_back(storageService);
2575e577bc1SWilly Tu asyncResp->res.jsonValue["Links"]["StorageServices"] =
2585e577bc1SWilly Tu std::move(storageServices);
2595e577bc1SWilly Tu asyncResp->res.jsonValue["Links"]["StorageServices@odata.count"] = 1;
2605e577bc1SWilly Tu }
2615e577bc1SWilly Tu
handleStorageGet(App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & storageId)262*504af5a0SPatrick Williams inline void handleStorageGet(
263*504af5a0SPatrick Williams App& app, const crow::Request& req,
2645e577bc1SWilly Tu const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2655e577bc1SWilly Tu const std::string& storageId)
2665e577bc1SWilly Tu {
2675e577bc1SWilly Tu if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2685e577bc1SWilly Tu {
26962598e31SEd Tanous BMCWEB_LOG_DEBUG("requestRoutesStorage setUpRedfishRoute failed");
2705e577bc1SWilly Tu return;
2715e577bc1SWilly Tu }
2725e577bc1SWilly Tu
2735e577bc1SWilly Tu constexpr std::array<std::string_view, 1> interfaces = {
2745e577bc1SWilly Tu "xyz.openbmc_project.Inventory.Item.Storage"};
2755e577bc1SWilly Tu dbus::utility::getSubTree(
2765e577bc1SWilly Tu "/xyz/openbmc_project/inventory", 0, interfaces,
2775e577bc1SWilly Tu std::bind_front(afterSubtree, asyncResp, storageId));
27836d52334SEd Tanous }
27936d52334SEd Tanous
requestRoutesStorage(App & app)28036d52334SEd Tanous inline void requestRoutesStorage(App& app)
28136d52334SEd Tanous {
2827f3e84a1SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/<str>/")
28336d52334SEd Tanous .privileges(redfish::privileges::getStorage)
28436d52334SEd Tanous .methods(boost::beast::http::verb::get)(
28536d52334SEd Tanous std::bind_front(handleSystemsStorageGet, std::ref(app)));
2865e577bc1SWilly Tu
2875e577bc1SWilly Tu BMCWEB_ROUTE(app, "/redfish/v1/Storage/<str>/")
2885e577bc1SWilly Tu .privileges(redfish::privileges::getStorage)
2895e577bc1SWilly Tu .methods(boost::beast::http::verb::get)(
2905e577bc1SWilly Tu std::bind_front(handleStorageGet, std::ref(app)));
2917e860f15SJohn Edward Broadbent }
2927e860f15SJohn Edward Broadbent
getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & connectionName,const std::string & path)29303913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29403913171SWilly Tu const std::string& connectionName,
29503913171SWilly Tu const std::string& path)
29603913171SWilly Tu {
297deae6a78SEd Tanous dbus::utility::getAllProperties(
298deae6a78SEd Tanous connectionName, path, "xyz.openbmc_project.Inventory.Decorator.Asset",
2995e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec,
300168e20c1SEd Tanous const std::vector<
301168e20c1SEd Tanous std::pair<std::string, dbus::utility::DbusVariantType>>&
30203913171SWilly Tu propertiesList) {
30303913171SWilly Tu if (ec)
30403913171SWilly Tu {
30503913171SWilly Tu // this interface isn't necessary
30603913171SWilly Tu return;
30703913171SWilly Tu }
308d1bde9e5SKrzysztof Grobelny
309d1bde9e5SKrzysztof Grobelny const std::string* partNumber = nullptr;
310d1bde9e5SKrzysztof Grobelny const std::string* serialNumber = nullptr;
311d1bde9e5SKrzysztof Grobelny const std::string* manufacturer = nullptr;
312d1bde9e5SKrzysztof Grobelny const std::string* model = nullptr;
313d1bde9e5SKrzysztof Grobelny
314d1bde9e5SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow(
315d1bde9e5SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
316d1bde9e5SKrzysztof Grobelny partNumber, "SerialNumber", serialNumber, "Manufacturer",
317d1bde9e5SKrzysztof Grobelny manufacturer, "Model", model);
318d1bde9e5SKrzysztof Grobelny
319d1bde9e5SKrzysztof Grobelny if (!success)
32003913171SWilly Tu {
32103913171SWilly Tu messages::internalError(asyncResp->res);
32203913171SWilly Tu return;
32303913171SWilly Tu }
324d1bde9e5SKrzysztof Grobelny
325d1bde9e5SKrzysztof Grobelny if (partNumber != nullptr)
326d1bde9e5SKrzysztof Grobelny {
327d1bde9e5SKrzysztof Grobelny asyncResp->res.jsonValue["PartNumber"] = *partNumber;
32803913171SWilly Tu }
329d1bde9e5SKrzysztof Grobelny
330d1bde9e5SKrzysztof Grobelny if (serialNumber != nullptr)
331d1bde9e5SKrzysztof Grobelny {
332d1bde9e5SKrzysztof Grobelny asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
33303913171SWilly Tu }
334d1bde9e5SKrzysztof Grobelny
335d1bde9e5SKrzysztof Grobelny if (manufacturer != nullptr)
336d1bde9e5SKrzysztof Grobelny {
337d1bde9e5SKrzysztof Grobelny asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
338d1bde9e5SKrzysztof Grobelny }
339d1bde9e5SKrzysztof Grobelny
340d1bde9e5SKrzysztof Grobelny if (model != nullptr)
341d1bde9e5SKrzysztof Grobelny {
342d1bde9e5SKrzysztof Grobelny asyncResp->res.jsonValue["Model"] = *model;
343d1bde9e5SKrzysztof Grobelny }
344d1bde9e5SKrzysztof Grobelny });
34503913171SWilly Tu }
34603913171SWilly Tu
getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & connectionName,const std::string & path)34703913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34803913171SWilly Tu const std::string& connectionName,
34903913171SWilly Tu const std::string& path)
35003913171SWilly Tu {
351deae6a78SEd Tanous dbus::utility::getProperty<bool>(
352deae6a78SEd Tanous connectionName, path, "xyz.openbmc_project.Inventory.Item", "Present",
353bd79bce8SPatrick Williams [asyncResp,
354bd79bce8SPatrick Williams path](const boost::system::error_code& ec, const bool isPresent) {
35503913171SWilly Tu // this interface isn't necessary, only check it if
35603913171SWilly Tu // we get a good return
35703913171SWilly Tu if (ec)
35803913171SWilly Tu {
35903913171SWilly Tu return;
36003913171SWilly Tu }
36103913171SWilly Tu
362cef57e85SWilly Tu if (!isPresent)
36303913171SWilly Tu {
364539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] =
365539d8c6bSEd Tanous resource::State::Absent;
36603913171SWilly Tu }
3671e1e598dSJonathan Doman });
36803913171SWilly Tu }
36903913171SWilly Tu
getDriveState(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & connectionName,const std::string & path)37003913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
37103913171SWilly Tu const std::string& connectionName,
37203913171SWilly Tu const std::string& path)
37303913171SWilly Tu {
374deae6a78SEd Tanous dbus::utility::getProperty<bool>(
375deae6a78SEd Tanous connectionName, path, "xyz.openbmc_project.State.Drive", "Rebuilding",
3765e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec, const bool updating) {
37703913171SWilly Tu // this interface isn't necessary, only check it
37803913171SWilly Tu // if we get a good return
37903913171SWilly Tu if (ec)
38003913171SWilly Tu {
38103913171SWilly Tu return;
38203913171SWilly Tu }
38303913171SWilly Tu
38403913171SWilly Tu // updating and disabled in the backend shouldn't be
38503913171SWilly Tu // able to be set at the same time, so we don't need
38603913171SWilly Tu // to check for the race condition of these two
38703913171SWilly Tu // calls
3881e1e598dSJonathan Doman if (updating)
38903913171SWilly Tu {
390539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] =
391539d8c6bSEd Tanous resource::State::Updating;
39203913171SWilly Tu }
3931e1e598dSJonathan Doman });
39403913171SWilly Tu }
39503913171SWilly Tu
convertDriveType(std::string_view type)396dde9bc12SGeorge Liu inline std::optional<drive::MediaType> convertDriveType(std::string_view type)
39719b8e9a0SWilly Tu {
39819b8e9a0SWilly Tu if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
39919b8e9a0SWilly Tu {
400dde9bc12SGeorge Liu return drive::MediaType::HDD;
40119b8e9a0SWilly Tu }
40219b8e9a0SWilly Tu if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
40319b8e9a0SWilly Tu {
404dde9bc12SGeorge Liu return drive::MediaType::SSD;
40519b8e9a0SWilly Tu }
406dde9bc12SGeorge Liu if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.Unknown")
407dde9bc12SGeorge Liu {
40819b8e9a0SWilly Tu return std::nullopt;
40919b8e9a0SWilly Tu }
41019b8e9a0SWilly Tu
411dde9bc12SGeorge Liu return drive::MediaType::Invalid;
412dde9bc12SGeorge Liu }
413dde9bc12SGeorge Liu
convertDriveProtocol(std::string_view proto)414*504af5a0SPatrick Williams inline std::optional<protocol::Protocol> convertDriveProtocol(
415*504af5a0SPatrick Williams std::string_view proto)
41619b8e9a0SWilly Tu {
41719b8e9a0SWilly Tu if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
41819b8e9a0SWilly Tu {
419dde9bc12SGeorge Liu return protocol::Protocol::SAS;
42019b8e9a0SWilly Tu }
42119b8e9a0SWilly Tu if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
42219b8e9a0SWilly Tu {
423dde9bc12SGeorge Liu return protocol::Protocol::SATA;
42419b8e9a0SWilly Tu }
42519b8e9a0SWilly Tu if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
42619b8e9a0SWilly Tu {
427dde9bc12SGeorge Liu return protocol::Protocol::NVMe;
42819b8e9a0SWilly Tu }
42919b8e9a0SWilly Tu if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
43019b8e9a0SWilly Tu {
431dde9bc12SGeorge Liu return protocol::Protocol::FC;
432dde9bc12SGeorge Liu }
433dde9bc12SGeorge Liu if (proto ==
434dde9bc12SGeorge Liu "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.Unknown")
435dde9bc12SGeorge Liu {
436dde9bc12SGeorge Liu return std::nullopt;
43719b8e9a0SWilly Tu }
43819b8e9a0SWilly Tu
439dde9bc12SGeorge Liu return protocol::Protocol::Invalid;
44019b8e9a0SWilly Tu }
44119b8e9a0SWilly Tu
getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & connectionName,const std::string & path)442bd79bce8SPatrick Williams inline void getDriveItemProperties(
443bd79bce8SPatrick Williams const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
444bd79bce8SPatrick Williams const std::string& connectionName, const std::string& path)
44519b8e9a0SWilly Tu {
446deae6a78SEd Tanous dbus::utility::getAllProperties(
447deae6a78SEd Tanous connectionName, path, "xyz.openbmc_project.Inventory.Item.Drive",
4485e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec,
44919b8e9a0SWilly Tu const std::vector<
45019b8e9a0SWilly Tu std::pair<std::string, dbus::utility::DbusVariantType>>&
45119b8e9a0SWilly Tu propertiesList) {
45219b8e9a0SWilly Tu if (ec)
45319b8e9a0SWilly Tu {
45419b8e9a0SWilly Tu // this interface isn't required
45519b8e9a0SWilly Tu return;
45619b8e9a0SWilly Tu }
457e5029d88SJohn Edward Broadbent const std::string* encryptionStatus = nullptr;
458e5029d88SJohn Edward Broadbent const bool* isLocked = nullptr;
45919b8e9a0SWilly Tu for (const std::pair<std::string, dbus::utility::DbusVariantType>&
46019b8e9a0SWilly Tu property : propertiesList)
46119b8e9a0SWilly Tu {
46219b8e9a0SWilly Tu const std::string& propertyName = property.first;
46319b8e9a0SWilly Tu if (propertyName == "Type")
46419b8e9a0SWilly Tu {
46519b8e9a0SWilly Tu const std::string* value =
46619b8e9a0SWilly Tu std::get_if<std::string>(&property.second);
46719b8e9a0SWilly Tu if (value == nullptr)
46819b8e9a0SWilly Tu {
46919b8e9a0SWilly Tu // illegal property
47062598e31SEd Tanous BMCWEB_LOG_ERROR("Illegal property: Type");
47119b8e9a0SWilly Tu messages::internalError(asyncResp->res);
47219b8e9a0SWilly Tu return;
47319b8e9a0SWilly Tu }
47419b8e9a0SWilly Tu
475dde9bc12SGeorge Liu std::optional<drive::MediaType> mediaType =
476dde9bc12SGeorge Liu convertDriveType(*value);
47719b8e9a0SWilly Tu if (!mediaType)
47819b8e9a0SWilly Tu {
47962598e31SEd Tanous BMCWEB_LOG_WARNING("UnknownDriveType Interface: {}",
48062598e31SEd Tanous *value);
481dde9bc12SGeorge Liu continue;
482dde9bc12SGeorge Liu }
483dde9bc12SGeorge Liu if (*mediaType == drive::MediaType::Invalid)
484dde9bc12SGeorge Liu {
48519b8e9a0SWilly Tu messages::internalError(asyncResp->res);
48619b8e9a0SWilly Tu return;
48719b8e9a0SWilly Tu }
48819b8e9a0SWilly Tu
48919b8e9a0SWilly Tu asyncResp->res.jsonValue["MediaType"] = *mediaType;
49019b8e9a0SWilly Tu }
49119b8e9a0SWilly Tu else if (propertyName == "Capacity")
49219b8e9a0SWilly Tu {
49319b8e9a0SWilly Tu const uint64_t* capacity =
49419b8e9a0SWilly Tu std::get_if<uint64_t>(&property.second);
49519b8e9a0SWilly Tu if (capacity == nullptr)
49619b8e9a0SWilly Tu {
49762598e31SEd Tanous BMCWEB_LOG_ERROR("Illegal property: Capacity");
49819b8e9a0SWilly Tu messages::internalError(asyncResp->res);
49919b8e9a0SWilly Tu return;
50019b8e9a0SWilly Tu }
50119b8e9a0SWilly Tu if (*capacity == 0)
50219b8e9a0SWilly Tu {
50319b8e9a0SWilly Tu // drive capacity not known
50419b8e9a0SWilly Tu continue;
50519b8e9a0SWilly Tu }
50619b8e9a0SWilly Tu
50719b8e9a0SWilly Tu asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
50819b8e9a0SWilly Tu }
50919b8e9a0SWilly Tu else if (propertyName == "Protocol")
51019b8e9a0SWilly Tu {
51119b8e9a0SWilly Tu const std::string* value =
51219b8e9a0SWilly Tu std::get_if<std::string>(&property.second);
51319b8e9a0SWilly Tu if (value == nullptr)
51419b8e9a0SWilly Tu {
51562598e31SEd Tanous BMCWEB_LOG_ERROR("Illegal property: Protocol");
51619b8e9a0SWilly Tu messages::internalError(asyncResp->res);
51719b8e9a0SWilly Tu return;
51819b8e9a0SWilly Tu }
51919b8e9a0SWilly Tu
520dde9bc12SGeorge Liu std::optional<protocol::Protocol> proto =
521dde9bc12SGeorge Liu convertDriveProtocol(*value);
52219b8e9a0SWilly Tu if (!proto)
52319b8e9a0SWilly Tu {
524bd79bce8SPatrick Williams BMCWEB_LOG_WARNING(
525bd79bce8SPatrick Williams "Unknown DrivePrototype Interface: {}", *value);
526dde9bc12SGeorge Liu continue;
527dde9bc12SGeorge Liu }
528dde9bc12SGeorge Liu if (*proto == protocol::Protocol::Invalid)
529dde9bc12SGeorge Liu {
53019b8e9a0SWilly Tu messages::internalError(asyncResp->res);
53119b8e9a0SWilly Tu return;
53219b8e9a0SWilly Tu }
53319b8e9a0SWilly Tu asyncResp->res.jsonValue["Protocol"] = *proto;
53419b8e9a0SWilly Tu }
5353fe4d5ccSJohn Edward Broadbent else if (propertyName == "PredictedMediaLifeLeftPercent")
5363fe4d5ccSJohn Edward Broadbent {
5373fe4d5ccSJohn Edward Broadbent const uint8_t* lifeLeft =
5383fe4d5ccSJohn Edward Broadbent std::get_if<uint8_t>(&property.second);
5393fe4d5ccSJohn Edward Broadbent if (lifeLeft == nullptr)
5403fe4d5ccSJohn Edward Broadbent {
54162598e31SEd Tanous BMCWEB_LOG_ERROR(
54262598e31SEd Tanous "Illegal property: PredictedMediaLifeLeftPercent");
5433fe4d5ccSJohn Edward Broadbent messages::internalError(asyncResp->res);
5443fe4d5ccSJohn Edward Broadbent return;
5453fe4d5ccSJohn Edward Broadbent }
5463fe4d5ccSJohn Edward Broadbent // 255 means reading the value is not supported
5473fe4d5ccSJohn Edward Broadbent if (*lifeLeft != 255)
5483fe4d5ccSJohn Edward Broadbent {
549bd79bce8SPatrick Williams asyncResp->res
550bd79bce8SPatrick Williams .jsonValue["PredictedMediaLifeLeftPercent"] =
5513fe4d5ccSJohn Edward Broadbent *lifeLeft;
5523fe4d5ccSJohn Edward Broadbent }
5533fe4d5ccSJohn Edward Broadbent }
554e5029d88SJohn Edward Broadbent else if (propertyName == "EncryptionStatus")
555e5029d88SJohn Edward Broadbent {
556bd79bce8SPatrick Williams encryptionStatus =
557bd79bce8SPatrick Williams std::get_if<std::string>(&property.second);
558e5029d88SJohn Edward Broadbent if (encryptionStatus == nullptr)
559e5029d88SJohn Edward Broadbent {
56062598e31SEd Tanous BMCWEB_LOG_ERROR("Illegal property: EncryptionStatus");
561e5029d88SJohn Edward Broadbent messages::internalError(asyncResp->res);
562e5029d88SJohn Edward Broadbent return;
56319b8e9a0SWilly Tu }
564e5029d88SJohn Edward Broadbent }
565e5029d88SJohn Edward Broadbent else if (propertyName == "Locked")
566e5029d88SJohn Edward Broadbent {
567e5029d88SJohn Edward Broadbent isLocked = std::get_if<bool>(&property.second);
568e5029d88SJohn Edward Broadbent if (isLocked == nullptr)
569e5029d88SJohn Edward Broadbent {
57062598e31SEd Tanous BMCWEB_LOG_ERROR("Illegal property: Locked");
571e5029d88SJohn Edward Broadbent messages::internalError(asyncResp->res);
572e5029d88SJohn Edward Broadbent return;
573e5029d88SJohn Edward Broadbent }
574e5029d88SJohn Edward Broadbent }
575e5029d88SJohn Edward Broadbent }
576e5029d88SJohn Edward Broadbent
577e5029d88SJohn Edward Broadbent if (encryptionStatus == nullptr || isLocked == nullptr ||
578e5029d88SJohn Edward Broadbent *encryptionStatus ==
579a684c226SKonda Reddy Kachana "xyz.openbmc_project.Inventory.Item.Drive.DriveEncryptionState.Unknown")
580e5029d88SJohn Edward Broadbent {
581e5029d88SJohn Edward Broadbent return;
582e5029d88SJohn Edward Broadbent }
583e5029d88SJohn Edward Broadbent if (*encryptionStatus !=
584a684c226SKonda Reddy Kachana "xyz.openbmc_project.Inventory.Item.Drive.DriveEncryptionState.Encrypted")
585e5029d88SJohn Edward Broadbent {
586e5029d88SJohn Edward Broadbent //"The drive is not currently encrypted."
587e5029d88SJohn Edward Broadbent asyncResp->res.jsonValue["EncryptionStatus"] =
588e5029d88SJohn Edward Broadbent drive::EncryptionStatus::Unencrypted;
589e5029d88SJohn Edward Broadbent return;
590e5029d88SJohn Edward Broadbent }
591e5029d88SJohn Edward Broadbent if (*isLocked)
592e5029d88SJohn Edward Broadbent {
593e5029d88SJohn Edward Broadbent //"The drive is currently encrypted and the data is not
594e5029d88SJohn Edward Broadbent // accessible to the user."
595e5029d88SJohn Edward Broadbent asyncResp->res.jsonValue["EncryptionStatus"] =
596e5029d88SJohn Edward Broadbent drive::EncryptionStatus::Locked;
597e5029d88SJohn Edward Broadbent return;
598e5029d88SJohn Edward Broadbent }
599e5029d88SJohn Edward Broadbent // if not locked
600e5029d88SJohn Edward Broadbent // "The drive is currently encrypted but the data is accessible
601e5029d88SJohn Edward Broadbent // to the user in unencrypted form."
602e5029d88SJohn Edward Broadbent asyncResp->res.jsonValue["EncryptionStatus"] =
603e5029d88SJohn Edward Broadbent drive::EncryptionStatus::Unlocked;
60419b8e9a0SWilly Tu });
60519b8e9a0SWilly Tu }
60619b8e9a0SWilly Tu
addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & connectionName,const std::string & path,const std::vector<std::string> & interfaces)6074ff0f1f4SEd Tanous inline void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
608b53dcd9dSNan Zhou const std::string& connectionName,
609b53dcd9dSNan Zhou const std::string& path,
610e56ed6b9SJohn Edward Broadbent const std::vector<std::string>& interfaces)
611e56ed6b9SJohn Edward Broadbent {
612e56ed6b9SJohn Edward Broadbent for (const std::string& interface : interfaces)
613e56ed6b9SJohn Edward Broadbent {
614e56ed6b9SJohn Edward Broadbent if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
615e56ed6b9SJohn Edward Broadbent {
616e56ed6b9SJohn Edward Broadbent getDriveAsset(asyncResp, connectionName, path);
617e56ed6b9SJohn Edward Broadbent }
618e56ed6b9SJohn Edward Broadbent else if (interface == "xyz.openbmc_project.Inventory.Item")
619e56ed6b9SJohn Edward Broadbent {
620e56ed6b9SJohn Edward Broadbent getDrivePresent(asyncResp, connectionName, path);
621e56ed6b9SJohn Edward Broadbent }
622e56ed6b9SJohn Edward Broadbent else if (interface == "xyz.openbmc_project.State.Drive")
623e56ed6b9SJohn Edward Broadbent {
624e56ed6b9SJohn Edward Broadbent getDriveState(asyncResp, connectionName, path);
625e56ed6b9SJohn Edward Broadbent }
626e56ed6b9SJohn Edward Broadbent else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
627e56ed6b9SJohn Edward Broadbent {
628e56ed6b9SJohn Edward Broadbent getDriveItemProperties(asyncResp, connectionName, path);
629e56ed6b9SJohn Edward Broadbent }
630e56ed6b9SJohn Edward Broadbent }
631e56ed6b9SJohn Edward Broadbent }
632e56ed6b9SJohn Edward Broadbent
afterGetSubtreeSystemsStorageDrive(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & driveId,const boost::system::error_code & ec,const dbus::utility::MapperGetSubTreeResponse & subtree)63336d52334SEd Tanous inline void afterGetSubtreeSystemsStorageDrive(
63445ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
63536d52334SEd Tanous const std::string& driveId, const boost::system::error_code& ec,
63636d52334SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree)
63745ca1b86SEd Tanous {
6387e860f15SJohn Edward Broadbent if (ec)
6397e860f15SJohn Edward Broadbent {
64062598e31SEd Tanous BMCWEB_LOG_ERROR("Drive mapper call error");
6417e860f15SJohn Edward Broadbent messages::internalError(asyncResp->res);
6427e860f15SJohn Edward Broadbent return;
6437e860f15SJohn Edward Broadbent }
6447e860f15SJohn Edward Broadbent
6453544d2a7SEd Tanous auto drive = std::ranges::find_if(
6463544d2a7SEd Tanous subtree,
64736d52334SEd Tanous [&driveId](const std::pair<std::string,
6488cb65f8aSNan Zhou dbus::utility::MapperServiceMap>& object) {
64936d52334SEd Tanous return sdbusplus::message::object_path(object.first).filename() ==
65036d52334SEd Tanous driveId;
6517e860f15SJohn Edward Broadbent });
6527e860f15SJohn Edward Broadbent
65303913171SWilly Tu if (drive == subtree.end())
6547e860f15SJohn Edward Broadbent {
655002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "Drive", driveId);
6567e860f15SJohn Edward Broadbent return;
6577e860f15SJohn Edward Broadbent }
6587e860f15SJohn Edward Broadbent
65903913171SWilly Tu const std::string& path = drive->first;
66036d52334SEd Tanous const dbus::utility::MapperServiceMap& connectionNames = drive->second;
6617e860f15SJohn Edward Broadbent
662002d39b4SEd Tanous asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
663253f11b8SEd Tanous asyncResp->res.jsonValue["@odata.id"] =
664253f11b8SEd Tanous boost::urls::format("/redfish/v1/Systems/{}/Storage/1/Drives/{}",
665253f11b8SEd Tanous BMCWEB_REDFISH_SYSTEM_URI_NAME, driveId);
6667e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Name"] = driveId;
6677e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Id"] = driveId;
6687e860f15SJohn Edward Broadbent
6697e860f15SJohn Edward Broadbent if (connectionNames.size() != 1)
6707e860f15SJohn Edward Broadbent {
67162598e31SEd Tanous BMCWEB_LOG_ERROR("Connection size {}, not equal to 1",
67262598e31SEd Tanous connectionNames.size());
6737e860f15SJohn Edward Broadbent messages::internalError(asyncResp->res);
6747e860f15SJohn Edward Broadbent return;
6757e860f15SJohn Edward Broadbent }
6767e860f15SJohn Edward Broadbent
677bd79bce8SPatrick Williams getMainChassisId(
678bd79bce8SPatrick Williams asyncResp, [](const std::string& chassisId,
6797e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
680002d39b4SEd Tanous aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
681ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
6827e860f15SJohn Edward Broadbent });
6837e860f15SJohn Edward Broadbent
684a25aeccfSNikhil Potade // default it to Enabled
685539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
686a25aeccfSNikhil Potade
687e56ed6b9SJohn Edward Broadbent addAllDriveInfo(asyncResp, connectionNames[0].first, path,
688e56ed6b9SJohn Edward Broadbent connectionNames[0].second);
689a25aeccfSNikhil Potade }
69092903bd4SJohn Edward Broadbent
handleSystemsStorageDriveGet(App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & systemName,const std::string & driveId)69136d52334SEd Tanous inline void handleSystemsStorageDriveGet(
69236d52334SEd Tanous App& app, const crow::Request& req,
69392903bd4SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
69436d52334SEd Tanous const std::string& systemName, const std::string& driveId)
69592903bd4SJohn Edward Broadbent {
6963ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp))
69792903bd4SJohn Edward Broadbent {
69892903bd4SJohn Edward Broadbent return;
69992903bd4SJohn Edward Broadbent }
70025b54dbaSEd Tanous if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
7017f3e84a1SEd Tanous {
7027f3e84a1SEd Tanous // Option currently returns no systems. TBD
7037f3e84a1SEd Tanous messages::resourceNotFound(asyncResp->res, "ComputerSystem",
7047f3e84a1SEd Tanous systemName);
7057f3e84a1SEd Tanous return;
7067f3e84a1SEd Tanous }
7077f3e84a1SEd Tanous
708253f11b8SEd Tanous if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
70936d52334SEd Tanous {
71036d52334SEd Tanous messages::resourceNotFound(asyncResp->res, "ComputerSystem",
71136d52334SEd Tanous systemName);
71236d52334SEd Tanous return;
71336d52334SEd Tanous }
71492903bd4SJohn Edward Broadbent
71536d52334SEd Tanous constexpr std::array<std::string_view, 1> interfaces = {
71636d52334SEd Tanous "xyz.openbmc_project.Inventory.Item.Drive"};
717e99073f5SGeorge Liu dbus::utility::getSubTree(
718e99073f5SGeorge Liu "/xyz/openbmc_project/inventory", 0, interfaces,
71936d52334SEd Tanous std::bind_front(afterGetSubtreeSystemsStorageDrive, asyncResp,
72036d52334SEd Tanous driveId));
72136d52334SEd Tanous }
72236d52334SEd Tanous
requestRoutesDrive(App & app)72336d52334SEd Tanous inline void requestRoutesDrive(App& app)
72436d52334SEd Tanous {
72536d52334SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
72636d52334SEd Tanous .privileges(redfish::privileges::getDrive)
72736d52334SEd Tanous .methods(boost::beast::http::verb::get)(
72836d52334SEd Tanous std::bind_front(handleSystemsStorageDriveGet, std::ref(app)));
72936d52334SEd Tanous }
73036d52334SEd Tanous
afterChassisDriveCollectionSubtreeGet(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisId,const boost::system::error_code & ec,const dbus::utility::MapperGetSubTreeResponse & subtree)73136d52334SEd Tanous inline void afterChassisDriveCollectionSubtreeGet(
73236d52334SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
73336d52334SEd Tanous const std::string& chassisId, const boost::system::error_code& ec,
73436d52334SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree)
73536d52334SEd Tanous {
73692903bd4SJohn Edward Broadbent if (ec)
73792903bd4SJohn Edward Broadbent {
73892903bd4SJohn Edward Broadbent if (ec == boost::system::errc::host_unreachable)
73992903bd4SJohn Edward Broadbent {
74036d52334SEd Tanous messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
74192903bd4SJohn Edward Broadbent return;
74292903bd4SJohn Edward Broadbent }
74392903bd4SJohn Edward Broadbent messages::internalError(asyncResp->res);
74492903bd4SJohn Edward Broadbent return;
74592903bd4SJohn Edward Broadbent }
74692903bd4SJohn Edward Broadbent
74792903bd4SJohn Edward Broadbent // Iterate over all retrieved ObjectPaths.
7488cb65f8aSNan Zhou for (const auto& [path, connectionNames] : subtree)
74992903bd4SJohn Edward Broadbent {
75092903bd4SJohn Edward Broadbent sdbusplus::message::object_path objPath(path);
75192903bd4SJohn Edward Broadbent if (objPath.filename() != chassisId)
75292903bd4SJohn Edward Broadbent {
75392903bd4SJohn Edward Broadbent continue;
75492903bd4SJohn Edward Broadbent }
75592903bd4SJohn Edward Broadbent
75692903bd4SJohn Edward Broadbent if (connectionNames.empty())
75792903bd4SJohn Edward Broadbent {
75862598e31SEd Tanous BMCWEB_LOG_ERROR("Got 0 Connection names");
75992903bd4SJohn Edward Broadbent continue;
76092903bd4SJohn Edward Broadbent }
76192903bd4SJohn Edward Broadbent
76292903bd4SJohn Edward Broadbent asyncResp->res.jsonValue["@odata.type"] =
76392903bd4SJohn Edward Broadbent "#DriveCollection.DriveCollection";
76492903bd4SJohn Edward Broadbent asyncResp->res.jsonValue["@odata.id"] =
765ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}/Drives", chassisId);
76692903bd4SJohn Edward Broadbent asyncResp->res.jsonValue["Name"] = "Drive Collection";
76792903bd4SJohn Edward Broadbent
76892903bd4SJohn Edward Broadbent // Association lambda
7696c3e9451SGeorge Liu dbus::utility::getAssociationEndPoints(
7706c3e9451SGeorge Liu path + "/drive",
77136d52334SEd Tanous [asyncResp, chassisId](const boost::system::error_code& ec3,
7726c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& resp) {
77392903bd4SJohn Edward Broadbent if (ec3)
77492903bd4SJohn Edward Broadbent {
77562598e31SEd Tanous BMCWEB_LOG_ERROR("Error in chassis Drive association ");
77692903bd4SJohn Edward Broadbent }
77792903bd4SJohn Edward Broadbent nlohmann::json& members = asyncResp->res.jsonValue["Members"];
77892903bd4SJohn Edward Broadbent // important if array is empty
77992903bd4SJohn Edward Broadbent members = nlohmann::json::array();
78092903bd4SJohn Edward Broadbent
78192903bd4SJohn Edward Broadbent std::vector<std::string> leafNames;
78292903bd4SJohn Edward Broadbent for (const auto& drive : resp)
78392903bd4SJohn Edward Broadbent {
7848a592810SEd Tanous sdbusplus::message::object_path drivePath(drive);
7858a592810SEd Tanous leafNames.push_back(drivePath.filename());
78692903bd4SJohn Edward Broadbent }
78792903bd4SJohn Edward Broadbent
7883544d2a7SEd Tanous std::ranges::sort(leafNames, AlphanumLess<std::string>());
78992903bd4SJohn Edward Broadbent
79092903bd4SJohn Edward Broadbent for (const auto& leafName : leafNames)
79192903bd4SJohn Edward Broadbent {
79292903bd4SJohn Edward Broadbent nlohmann::json::object_t member;
793bd79bce8SPatrick Williams member["@odata.id"] =
794bd79bce8SPatrick Williams boost::urls::format("/redfish/v1/Chassis/{}/Drives/{}",
795bd79bce8SPatrick Williams chassisId, leafName);
796b2ba3072SPatrick Williams members.emplace_back(std::move(member));
79792903bd4SJohn Edward Broadbent // navigation links will be registered in next patch set
79892903bd4SJohn Edward Broadbent }
79992903bd4SJohn Edward Broadbent asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
80092903bd4SJohn Edward Broadbent }); // end association lambda
80192903bd4SJohn Edward Broadbent
80292903bd4SJohn Edward Broadbent } // end Iterate over all retrieved ObjectPaths
80336d52334SEd Tanous }
80436d52334SEd Tanous /**
80536d52334SEd Tanous * Chassis drives, this URL will show all the DriveCollection
80636d52334SEd Tanous * information
80736d52334SEd Tanous */
chassisDriveCollectionGet(crow::App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisId)80836d52334SEd Tanous inline void chassisDriveCollectionGet(
80936d52334SEd Tanous crow::App& app, const crow::Request& req,
81036d52334SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
81136d52334SEd Tanous const std::string& chassisId)
81236d52334SEd Tanous {
81336d52334SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp))
81436d52334SEd Tanous {
81536d52334SEd Tanous return;
81636d52334SEd Tanous }
81736d52334SEd Tanous
81836d52334SEd Tanous // mapper call lambda
81936d52334SEd Tanous constexpr std::array<std::string_view, 2> interfaces = {
82036d52334SEd Tanous "xyz.openbmc_project.Inventory.Item.Board",
82136d52334SEd Tanous "xyz.openbmc_project.Inventory.Item.Chassis"};
82236d52334SEd Tanous dbus::utility::getSubTree(
82336d52334SEd Tanous "/xyz/openbmc_project/inventory", 0, interfaces,
82436d52334SEd Tanous std::bind_front(afterChassisDriveCollectionSubtreeGet, asyncResp,
82536d52334SEd Tanous chassisId));
82692903bd4SJohn Edward Broadbent }
82792903bd4SJohn Edward Broadbent
requestRoutesChassisDrive(App & app)82892903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
82992903bd4SJohn Edward Broadbent {
83092903bd4SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
83192903bd4SJohn Edward Broadbent .privileges(redfish::privileges::getDriveCollection)
83292903bd4SJohn Edward Broadbent .methods(boost::beast::http::verb::get)(
83392903bd4SJohn Edward Broadbent std::bind_front(chassisDriveCollectionGet, std::ref(app)));
83492903bd4SJohn Edward Broadbent }
83592903bd4SJohn Edward Broadbent
buildDrive(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisId,const std::string & driveName,const boost::system::error_code & ec,const dbus::utility::MapperGetSubTreeResponse & subtree)836b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
837b53dcd9dSNan Zhou const std::string& chassisId,
838b53dcd9dSNan Zhou const std::string& driveName,
8395e7e2dc5SEd Tanous const boost::system::error_code& ec,
840e56ed6b9SJohn Edward Broadbent const dbus::utility::MapperGetSubTreeResponse& subtree)
841e56ed6b9SJohn Edward Broadbent {
842e56ed6b9SJohn Edward Broadbent if (ec)
843e56ed6b9SJohn Edward Broadbent {
84462598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
845e56ed6b9SJohn Edward Broadbent messages::internalError(asyncResp->res);
846e56ed6b9SJohn Edward Broadbent return;
847e56ed6b9SJohn Edward Broadbent }
848e56ed6b9SJohn Edward Broadbent
849e56ed6b9SJohn Edward Broadbent // Iterate over all retrieved ObjectPaths.
8508cb65f8aSNan Zhou for (const auto& [path, connectionNames] : subtree)
851e56ed6b9SJohn Edward Broadbent {
852e56ed6b9SJohn Edward Broadbent sdbusplus::message::object_path objPath(path);
853e56ed6b9SJohn Edward Broadbent if (objPath.filename() != driveName)
854e56ed6b9SJohn Edward Broadbent {
855e56ed6b9SJohn Edward Broadbent continue;
856e56ed6b9SJohn Edward Broadbent }
857e56ed6b9SJohn Edward Broadbent
858e56ed6b9SJohn Edward Broadbent if (connectionNames.empty())
859e56ed6b9SJohn Edward Broadbent {
86062598e31SEd Tanous BMCWEB_LOG_ERROR("Got 0 Connection names");
861e56ed6b9SJohn Edward Broadbent continue;
862e56ed6b9SJohn Edward Broadbent }
863e56ed6b9SJohn Edward Broadbent
864ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
865ef4c65b7SEd Tanous "/redfish/v1/Chassis/{}/Drives/{}", chassisId, driveName);
866e56ed6b9SJohn Edward Broadbent
867e56ed6b9SJohn Edward Broadbent asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
868a0cb40cbSJohn Edward Broadbent asyncResp->res.jsonValue["Name"] = driveName;
869e56ed6b9SJohn Edward Broadbent asyncResp->res.jsonValue["Id"] = driveName;
870e56ed6b9SJohn Edward Broadbent // default it to Enabled
871539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
872e56ed6b9SJohn Edward Broadbent
873e56ed6b9SJohn Edward Broadbent nlohmann::json::object_t linkChassisNav;
874e56ed6b9SJohn Edward Broadbent linkChassisNav["@odata.id"] =
875ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
876e56ed6b9SJohn Edward Broadbent asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
877e56ed6b9SJohn Edward Broadbent
878e56ed6b9SJohn Edward Broadbent addAllDriveInfo(asyncResp, connectionNames[0].first, path,
879e56ed6b9SJohn Edward Broadbent connectionNames[0].second);
880e56ed6b9SJohn Edward Broadbent }
881e56ed6b9SJohn Edward Broadbent }
882e56ed6b9SJohn Edward Broadbent
matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisId,const std::string & driveName,const std::vector<std::string> & resp)883bd79bce8SPatrick Williams inline void matchAndFillDrive(
884bd79bce8SPatrick Williams const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
885bd79bce8SPatrick Williams const std::string& chassisId, const std::string& driveName,
886e56ed6b9SJohn Edward Broadbent const std::vector<std::string>& resp)
887e56ed6b9SJohn Edward Broadbent {
888e56ed6b9SJohn Edward Broadbent for (const std::string& drivePath : resp)
889e56ed6b9SJohn Edward Broadbent {
890e56ed6b9SJohn Edward Broadbent sdbusplus::message::object_path path(drivePath);
891e56ed6b9SJohn Edward Broadbent std::string leaf = path.filename();
892e56ed6b9SJohn Edward Broadbent if (leaf != driveName)
893e56ed6b9SJohn Edward Broadbent {
894e56ed6b9SJohn Edward Broadbent continue;
895e56ed6b9SJohn Edward Broadbent }
896e56ed6b9SJohn Edward Broadbent // mapper call drive
897e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> driveInterface = {
898e56ed6b9SJohn Edward Broadbent "xyz.openbmc_project.Inventory.Item.Drive"};
899e99073f5SGeorge Liu dbus::utility::getSubTree(
900e99073f5SGeorge Liu "/xyz/openbmc_project/inventory", 0, driveInterface,
901e56ed6b9SJohn Edward Broadbent [asyncResp, chassisId, driveName](
902e99073f5SGeorge Liu const boost::system::error_code& ec,
903e56ed6b9SJohn Edward Broadbent const dbus::utility::MapperGetSubTreeResponse& subtree) {
904e56ed6b9SJohn Edward Broadbent buildDrive(asyncResp, chassisId, driveName, ec, subtree);
905e99073f5SGeorge Liu });
906e56ed6b9SJohn Edward Broadbent }
907e56ed6b9SJohn Edward Broadbent }
908e56ed6b9SJohn Edward Broadbent
handleChassisDriveGet(crow::App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisId,const std::string & driveName)909bd79bce8SPatrick Williams inline void handleChassisDriveGet(
910bd79bce8SPatrick Williams crow::App& app, const crow::Request& req,
911e56ed6b9SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
912bd79bce8SPatrick Williams const std::string& chassisId, const std::string& driveName)
913e56ed6b9SJohn Edward Broadbent {
91403810a11SMichal Orzel if (!redfish::setUpRedfishRoute(app, req, asyncResp))
915e56ed6b9SJohn Edward Broadbent {
916e56ed6b9SJohn Edward Broadbent return;
917e56ed6b9SJohn Edward Broadbent }
918e99073f5SGeorge Liu constexpr std::array<std::string_view, 2> interfaces = {
919e56ed6b9SJohn Edward Broadbent "xyz.openbmc_project.Inventory.Item.Board",
920e56ed6b9SJohn Edward Broadbent "xyz.openbmc_project.Inventory.Item.Chassis"};
921e56ed6b9SJohn Edward Broadbent
922e56ed6b9SJohn Edward Broadbent // mapper call chassis
923e99073f5SGeorge Liu dbus::utility::getSubTree(
924e99073f5SGeorge Liu "/xyz/openbmc_project/inventory", 0, interfaces,
925e56ed6b9SJohn Edward Broadbent [asyncResp, chassisId,
926e99073f5SGeorge Liu driveName](const boost::system::error_code& ec,
927e56ed6b9SJohn Edward Broadbent const dbus::utility::MapperGetSubTreeResponse& subtree) {
928e56ed6b9SJohn Edward Broadbent if (ec)
929e56ed6b9SJohn Edward Broadbent {
930e56ed6b9SJohn Edward Broadbent messages::internalError(asyncResp->res);
931e56ed6b9SJohn Edward Broadbent return;
932e56ed6b9SJohn Edward Broadbent }
933e56ed6b9SJohn Edward Broadbent
934e56ed6b9SJohn Edward Broadbent // Iterate over all retrieved ObjectPaths.
9358cb65f8aSNan Zhou for (const auto& [path, connectionNames] : subtree)
936e56ed6b9SJohn Edward Broadbent {
937e56ed6b9SJohn Edward Broadbent sdbusplus::message::object_path objPath(path);
938e56ed6b9SJohn Edward Broadbent if (objPath.filename() != chassisId)
939e56ed6b9SJohn Edward Broadbent {
940e56ed6b9SJohn Edward Broadbent continue;
941e56ed6b9SJohn Edward Broadbent }
942e56ed6b9SJohn Edward Broadbent
943e56ed6b9SJohn Edward Broadbent if (connectionNames.empty())
944e56ed6b9SJohn Edward Broadbent {
94562598e31SEd Tanous BMCWEB_LOG_ERROR("Got 0 Connection names");
946e56ed6b9SJohn Edward Broadbent continue;
947e56ed6b9SJohn Edward Broadbent }
948e56ed6b9SJohn Edward Broadbent
9496c3e9451SGeorge Liu dbus::utility::getAssociationEndPoints(
9506c3e9451SGeorge Liu path + "/drive",
951e56ed6b9SJohn Edward Broadbent [asyncResp, chassisId,
9525e7e2dc5SEd Tanous driveName](const boost::system::error_code& ec3,
9536c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& resp) {
954e56ed6b9SJohn Edward Broadbent if (ec3)
955e56ed6b9SJohn Edward Broadbent {
956e56ed6b9SJohn Edward Broadbent return; // no drives = no failures
957e56ed6b9SJohn Edward Broadbent }
958bd79bce8SPatrick Williams matchAndFillDrive(asyncResp, chassisId, driveName,
959bd79bce8SPatrick Williams resp);
960e56ed6b9SJohn Edward Broadbent });
961e56ed6b9SJohn Edward Broadbent break;
962e56ed6b9SJohn Edward Broadbent }
963e99073f5SGeorge Liu });
964e56ed6b9SJohn Edward Broadbent }
965e56ed6b9SJohn Edward Broadbent
966e56ed6b9SJohn Edward Broadbent /**
967e56ed6b9SJohn Edward Broadbent * This URL will show the drive interface for the specific drive in the chassis
968e56ed6b9SJohn Edward Broadbent */
requestRoutesChassisDriveName(App & app)969e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
970e56ed6b9SJohn Edward Broadbent {
971e56ed6b9SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
972e56ed6b9SJohn Edward Broadbent .privileges(redfish::privileges::getChassis)
973e56ed6b9SJohn Edward Broadbent .methods(boost::beast::http::verb::get)(
974e56ed6b9SJohn Edward Broadbent std::bind_front(handleChassisDriveGet, std::ref(app)));
975e56ed6b9SJohn Edward Broadbent }
976e56ed6b9SJohn Edward Broadbent
getStorageControllerAsset(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const boost::system::error_code & ec,const std::vector<std::pair<std::string,dbus::utility::DbusVariantType>> & propertiesList)97761b1eb21SWilly Tu inline void getStorageControllerAsset(
97861b1eb21SWilly Tu const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
97961b1eb21SWilly Tu const boost::system::error_code& ec,
98061b1eb21SWilly Tu const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
98161b1eb21SWilly Tu propertiesList)
98261b1eb21SWilly Tu {
98361b1eb21SWilly Tu if (ec)
98461b1eb21SWilly Tu {
98561b1eb21SWilly Tu // this interface isn't necessary
98662598e31SEd Tanous BMCWEB_LOG_DEBUG("Failed to get StorageControllerAsset");
98761b1eb21SWilly Tu return;
98861b1eb21SWilly Tu }
98961b1eb21SWilly Tu
99061b1eb21SWilly Tu const std::string* partNumber = nullptr;
99161b1eb21SWilly Tu const std::string* serialNumber = nullptr;
99261b1eb21SWilly Tu const std::string* manufacturer = nullptr;
99361b1eb21SWilly Tu const std::string* model = nullptr;
99461b1eb21SWilly Tu if (!sdbusplus::unpackPropertiesNoThrow(
99561b1eb21SWilly Tu dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
99661b1eb21SWilly Tu partNumber, "SerialNumber", serialNumber, "Manufacturer",
99761b1eb21SWilly Tu manufacturer, "Model", model))
99861b1eb21SWilly Tu {
99961b1eb21SWilly Tu messages::internalError(asyncResp->res);
100061b1eb21SWilly Tu return;
100161b1eb21SWilly Tu }
100261b1eb21SWilly Tu
100361b1eb21SWilly Tu if (partNumber != nullptr)
100461b1eb21SWilly Tu {
100561b1eb21SWilly Tu asyncResp->res.jsonValue["PartNumber"] = *partNumber;
100661b1eb21SWilly Tu }
100761b1eb21SWilly Tu
100861b1eb21SWilly Tu if (serialNumber != nullptr)
100961b1eb21SWilly Tu {
101061b1eb21SWilly Tu asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
101161b1eb21SWilly Tu }
101261b1eb21SWilly Tu
101361b1eb21SWilly Tu if (manufacturer != nullptr)
101461b1eb21SWilly Tu {
101561b1eb21SWilly Tu asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
101661b1eb21SWilly Tu }
101761b1eb21SWilly Tu
101861b1eb21SWilly Tu if (model != nullptr)
101961b1eb21SWilly Tu {
102061b1eb21SWilly Tu asyncResp->res.jsonValue["Model"] = *model;
102161b1eb21SWilly Tu }
102261b1eb21SWilly Tu }
102361b1eb21SWilly Tu
populateStorageController(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & controllerId,const std::string & connectionName,const std::string & path)102461b1eb21SWilly Tu inline void populateStorageController(
102561b1eb21SWilly Tu const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
102661b1eb21SWilly Tu const std::string& controllerId, const std::string& connectionName,
102761b1eb21SWilly Tu const std::string& path)
102861b1eb21SWilly Tu {
102961b1eb21SWilly Tu asyncResp->res.jsonValue["@odata.type"] =
103061b1eb21SWilly Tu "#StorageController.v1_6_0.StorageController";
1031253f11b8SEd Tanous asyncResp->res.jsonValue["@odata.id"] =
1032253f11b8SEd Tanous boost::urls::format("/redfish/v1/Systems/{}/Storage/1/Controllers/{}",
1033253f11b8SEd Tanous BMCWEB_REDFISH_SYSTEM_URI_NAME, controllerId);
103461b1eb21SWilly Tu asyncResp->res.jsonValue["Name"] = controllerId;
103561b1eb21SWilly Tu asyncResp->res.jsonValue["Id"] = controllerId;
1036539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
103761b1eb21SWilly Tu
1038deae6a78SEd Tanous dbus::utility::getProperty<bool>(
1039deae6a78SEd Tanous connectionName, path, "xyz.openbmc_project.Inventory.Item", "Present",
104061b1eb21SWilly Tu [asyncResp](const boost::system::error_code& ec, bool isPresent) {
104161b1eb21SWilly Tu // this interface isn't necessary, only check it
104261b1eb21SWilly Tu // if we get a good return
104361b1eb21SWilly Tu if (ec)
104461b1eb21SWilly Tu {
104562598e31SEd Tanous BMCWEB_LOG_DEBUG("Failed to get Present property");
104661b1eb21SWilly Tu return;
104761b1eb21SWilly Tu }
104861b1eb21SWilly Tu if (!isPresent)
104961b1eb21SWilly Tu {
1050539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] =
1051539d8c6bSEd Tanous resource::State::Absent;
105261b1eb21SWilly Tu }
105361b1eb21SWilly Tu });
105461b1eb21SWilly Tu
1055deae6a78SEd Tanous dbus::utility::getAllProperties(
1056deae6a78SEd Tanous connectionName, path, "xyz.openbmc_project.Inventory.Decorator.Asset",
105761b1eb21SWilly Tu [asyncResp](const boost::system::error_code& ec,
105861b1eb21SWilly Tu const std::vector<
105961b1eb21SWilly Tu std::pair<std::string, dbus::utility::DbusVariantType>>&
106061b1eb21SWilly Tu propertiesList) {
106161b1eb21SWilly Tu getStorageControllerAsset(asyncResp, ec, propertiesList);
106261b1eb21SWilly Tu });
106361b1eb21SWilly Tu }
106461b1eb21SWilly Tu
getStorageControllerHandler(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & controllerId,const boost::system::error_code & ec,const dbus::utility::MapperGetSubTreeResponse & subtree)106561b1eb21SWilly Tu inline void getStorageControllerHandler(
106661b1eb21SWilly Tu const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
106761b1eb21SWilly Tu const std::string& controllerId, const boost::system::error_code& ec,
106861b1eb21SWilly Tu const dbus::utility::MapperGetSubTreeResponse& subtree)
106961b1eb21SWilly Tu {
107061b1eb21SWilly Tu if (ec || subtree.empty())
107161b1eb21SWilly Tu {
107261b1eb21SWilly Tu // doesn't have to be there
107362598e31SEd Tanous BMCWEB_LOG_DEBUG("Failed to handle StorageController");
107461b1eb21SWilly Tu return;
107561b1eb21SWilly Tu }
107661b1eb21SWilly Tu
107761b1eb21SWilly Tu for (const auto& [path, interfaceDict] : subtree)
107861b1eb21SWilly Tu {
107961b1eb21SWilly Tu sdbusplus::message::object_path object(path);
108061b1eb21SWilly Tu std::string id = object.filename();
108161b1eb21SWilly Tu if (id.empty())
108261b1eb21SWilly Tu {
108362598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
108461b1eb21SWilly Tu return;
108561b1eb21SWilly Tu }
108661b1eb21SWilly Tu if (id != controllerId)
108761b1eb21SWilly Tu {
108861b1eb21SWilly Tu continue;
108961b1eb21SWilly Tu }
109061b1eb21SWilly Tu
109161b1eb21SWilly Tu if (interfaceDict.size() != 1)
109261b1eb21SWilly Tu {
109362598e31SEd Tanous BMCWEB_LOG_ERROR("Connection size {}, greater than 1",
109462598e31SEd Tanous interfaceDict.size());
109561b1eb21SWilly Tu messages::internalError(asyncResp->res);
109661b1eb21SWilly Tu return;
109761b1eb21SWilly Tu }
109861b1eb21SWilly Tu
109961b1eb21SWilly Tu const std::string& connectionName = interfaceDict.front().first;
110061b1eb21SWilly Tu populateStorageController(asyncResp, controllerId, connectionName,
110161b1eb21SWilly Tu path);
110261b1eb21SWilly Tu }
110361b1eb21SWilly Tu }
110461b1eb21SWilly Tu
populateStorageControllerCollection(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const boost::system::error_code & ec,const dbus::utility::MapperGetSubTreePathsResponse & controllerList)110561b1eb21SWilly Tu inline void populateStorageControllerCollection(
110661b1eb21SWilly Tu const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
110761b1eb21SWilly Tu const boost::system::error_code& ec,
110861b1eb21SWilly Tu const dbus::utility::MapperGetSubTreePathsResponse& controllerList)
110961b1eb21SWilly Tu {
111061b1eb21SWilly Tu nlohmann::json::array_t members;
111161b1eb21SWilly Tu if (ec || controllerList.empty())
111261b1eb21SWilly Tu {
111361b1eb21SWilly Tu asyncResp->res.jsonValue["Members"] = std::move(members);
111461b1eb21SWilly Tu asyncResp->res.jsonValue["Members@odata.count"] = 0;
111562598e31SEd Tanous BMCWEB_LOG_DEBUG("Failed to find any StorageController");
111661b1eb21SWilly Tu return;
111761b1eb21SWilly Tu }
111861b1eb21SWilly Tu
111961b1eb21SWilly Tu for (const std::string& path : controllerList)
112061b1eb21SWilly Tu {
112161b1eb21SWilly Tu std::string id = sdbusplus::message::object_path(path).filename();
112261b1eb21SWilly Tu if (id.empty())
112361b1eb21SWilly Tu {
112462598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
112561b1eb21SWilly Tu return;
112661b1eb21SWilly Tu }
112761b1eb21SWilly Tu nlohmann::json::object_t member;
112861b1eb21SWilly Tu member["@odata.id"] = boost::urls::format(
1129253f11b8SEd Tanous "/redfish/v1/Systems/{}/Storage/1/Controllers/{}",
1130253f11b8SEd Tanous BMCWEB_REDFISH_SYSTEM_URI_NAME, id);
113161b1eb21SWilly Tu members.emplace_back(member);
113261b1eb21SWilly Tu }
113361b1eb21SWilly Tu asyncResp->res.jsonValue["Members@odata.count"] = members.size();
113461b1eb21SWilly Tu asyncResp->res.jsonValue["Members"] = std::move(members);
113561b1eb21SWilly Tu }
113661b1eb21SWilly Tu
handleSystemsStorageControllerCollectionGet(App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & systemName)113736d52334SEd Tanous inline void handleSystemsStorageControllerCollectionGet(
113861b1eb21SWilly Tu App& app, const crow::Request& req,
113961b1eb21SWilly Tu const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
114061b1eb21SWilly Tu const std::string& systemName)
114161b1eb21SWilly Tu {
114261b1eb21SWilly Tu if (!redfish::setUpRedfishRoute(app, req, asyncResp))
114361b1eb21SWilly Tu {
114462598e31SEd Tanous BMCWEB_LOG_DEBUG(
114562598e31SEd Tanous "Failed to setup Redfish Route for StorageController Collection");
114661b1eb21SWilly Tu return;
114761b1eb21SWilly Tu }
1148253f11b8SEd Tanous if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
114961b1eb21SWilly Tu {
115061b1eb21SWilly Tu messages::resourceNotFound(asyncResp->res, "ComputerSystem",
115161b1eb21SWilly Tu systemName);
115262598e31SEd Tanous BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
115361b1eb21SWilly Tu return;
115461b1eb21SWilly Tu }
115561b1eb21SWilly Tu
115661b1eb21SWilly Tu asyncResp->res.jsonValue["@odata.type"] =
115761b1eb21SWilly Tu "#StorageControllerCollection.StorageControllerCollection";
115861b1eb21SWilly Tu asyncResp->res.jsonValue["@odata.id"] =
1159253f11b8SEd Tanous std::format("/redfish/v1/Systems/{}/Storage/1/Controllers",
1160253f11b8SEd Tanous BMCWEB_REDFISH_SYSTEM_URI_NAME);
116161b1eb21SWilly Tu asyncResp->res.jsonValue["Name"] = "Storage Controller Collection";
116261b1eb21SWilly Tu
116361b1eb21SWilly Tu constexpr std::array<std::string_view, 1> interfaces = {
116461b1eb21SWilly Tu "xyz.openbmc_project.Inventory.Item.StorageController"};
116561b1eb21SWilly Tu dbus::utility::getSubTreePaths(
116661b1eb21SWilly Tu "/xyz/openbmc_project/inventory", 0, interfaces,
116761b1eb21SWilly Tu [asyncResp](const boost::system::error_code& ec,
116861b1eb21SWilly Tu const dbus::utility::MapperGetSubTreePathsResponse&
116961b1eb21SWilly Tu controllerList) {
117061b1eb21SWilly Tu populateStorageControllerCollection(asyncResp, ec, controllerList);
117161b1eb21SWilly Tu });
117261b1eb21SWilly Tu }
117361b1eb21SWilly Tu
handleSystemsStorageControllerGet(App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & systemName,const std::string & controllerId)117436d52334SEd Tanous inline void handleSystemsStorageControllerGet(
117561b1eb21SWilly Tu App& app, const crow::Request& req,
117661b1eb21SWilly Tu const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
117761b1eb21SWilly Tu const std::string& systemName, const std::string& controllerId)
117861b1eb21SWilly Tu {
117961b1eb21SWilly Tu if (!redfish::setUpRedfishRoute(app, req, asyncResp))
118061b1eb21SWilly Tu {
118162598e31SEd Tanous BMCWEB_LOG_DEBUG("Failed to setup Redfish Route for StorageController");
118261b1eb21SWilly Tu return;
118361b1eb21SWilly Tu }
1184253f11b8SEd Tanous if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
118561b1eb21SWilly Tu {
118661b1eb21SWilly Tu messages::resourceNotFound(asyncResp->res, "ComputerSystem",
118761b1eb21SWilly Tu systemName);
118862598e31SEd Tanous BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
118961b1eb21SWilly Tu return;
119061b1eb21SWilly Tu }
119161b1eb21SWilly Tu constexpr std::array<std::string_view, 1> interfaces = {
119261b1eb21SWilly Tu "xyz.openbmc_project.Inventory.Item.StorageController"};
119361b1eb21SWilly Tu dbus::utility::getSubTree(
119461b1eb21SWilly Tu "/xyz/openbmc_project/inventory", 0, interfaces,
119561b1eb21SWilly Tu [asyncResp,
119661b1eb21SWilly Tu controllerId](const boost::system::error_code& ec,
119761b1eb21SWilly Tu const dbus::utility::MapperGetSubTreeResponse& subtree) {
119861b1eb21SWilly Tu getStorageControllerHandler(asyncResp, controllerId, ec, subtree);
119961b1eb21SWilly Tu });
120061b1eb21SWilly Tu }
120161b1eb21SWilly Tu
requestRoutesStorageControllerCollection(App & app)120261b1eb21SWilly Tu inline void requestRoutesStorageControllerCollection(App& app)
120361b1eb21SWilly Tu {
120461b1eb21SWilly Tu BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/")
120561b1eb21SWilly Tu .privileges(redfish::privileges::getStorageControllerCollection)
120636d52334SEd Tanous .methods(boost::beast::http::verb::get)(std::bind_front(
120736d52334SEd Tanous handleSystemsStorageControllerCollectionGet, std::ref(app)));
120861b1eb21SWilly Tu }
120961b1eb21SWilly Tu
requestRoutesStorageController(App & app)121061b1eb21SWilly Tu inline void requestRoutesStorageController(App& app)
121161b1eb21SWilly Tu {
121261b1eb21SWilly Tu BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/<str>")
121361b1eb21SWilly Tu .privileges(redfish::privileges::getStorageController)
121461b1eb21SWilly Tu .methods(boost::beast::http::verb::get)(
121536d52334SEd Tanous std::bind_front(handleSystemsStorageControllerGet, std::ref(app)));
121661b1eb21SWilly Tu }
121761b1eb21SWilly Tu
1218a25aeccfSNikhil Potade } // namespace redfish
1219