xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision 539d8c6bc399e516d39bcaa18262cf206f4c1035)
1a25aeccfSNikhil Potade /*
2a25aeccfSNikhil Potade // Copyright (c) 2019 Intel Corporation
3a25aeccfSNikhil Potade //
4a25aeccfSNikhil Potade // Licensed under the Apache License, Version 2.0 (the "License");
5a25aeccfSNikhil Potade // you may not use this file except in compliance with the License.
6a25aeccfSNikhil Potade // You may obtain a copy of the License at
7a25aeccfSNikhil Potade //
8a25aeccfSNikhil Potade //      http://www.apache.org/licenses/LICENSE-2.0
9a25aeccfSNikhil Potade //
10a25aeccfSNikhil Potade // Unless required by applicable law or agreed to in writing, software
11a25aeccfSNikhil Potade // distributed under the License is distributed on an "AS IS" BASIS,
12a25aeccfSNikhil Potade // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a25aeccfSNikhil Potade // See the License for the specific language governing permissions and
14a25aeccfSNikhil Potade // limitations under the License.
15a25aeccfSNikhil Potade */
16a25aeccfSNikhil Potade #pragma once
17a25aeccfSNikhil Potade 
1813451e39SWilly Tu #include "bmcweb_config.h"
1913451e39SWilly Tu 
203ccb3adbSEd Tanous #include "app.hpp"
217a1dbc48SGeorge Liu #include "dbus_utility.hpp"
22e5029d88SJohn Edward Broadbent #include "generated/enums/drive.hpp"
23dde9bc12SGeorge Liu #include "generated/enums/protocol.hpp"
24*539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
25a8e884fcSEd Tanous #include "human_sort.hpp"
263ccb3adbSEd Tanous #include "query.hpp"
27a8e884fcSEd Tanous #include "redfish_util.hpp"
283ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
295e577bc1SWilly Tu #include "utils/collection.hpp"
303ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
312ad9c2f6SJames Feist 
32e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
33ef4c65b7SEd Tanous #include <boost/url/format.hpp>
341e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
35d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
36a25aeccfSNikhil Potade 
377a1dbc48SGeorge Liu #include <array>
383544d2a7SEd Tanous #include <ranges>
397a1dbc48SGeorge Liu #include <string_view>
407a1dbc48SGeorge Liu 
41a25aeccfSNikhil Potade namespace redfish
42a25aeccfSNikhil Potade {
4336d52334SEd Tanous 
4436d52334SEd Tanous inline void handleSystemsStorageCollectionGet(
4536d52334SEd Tanous     App& app, const crow::Request& req,
4622d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4736d52334SEd Tanous     const std::string& systemName)
4836d52334SEd Tanous {
493ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
5045ca1b86SEd Tanous     {
5145ca1b86SEd Tanous         return;
5245ca1b86SEd Tanous     }
53253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
5422d268cbSEd Tanous     {
5522d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
5622d268cbSEd Tanous                                    systemName);
5722d268cbSEd Tanous         return;
5822d268cbSEd Tanous     }
5922d268cbSEd Tanous 
608d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
618d1b46d7Szhanghch05         "#StorageCollection.StorageCollection";
62253f11b8SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = std::format(
63253f11b8SEd Tanous         "/redfish/v1/Systems/{}/Storage", BMCWEB_REDFISH_SYSTEM_URI_NAME);
648d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Storage Collection";
655e577bc1SWilly Tu 
665e577bc1SWilly Tu     constexpr std::array<std::string_view, 1> interface{
675a39f77aSPatrick Williams         "xyz.openbmc_project.Inventory.Item.Storage"};
685e577bc1SWilly Tu     collection_util::getCollectionMembers(
69253f11b8SEd Tanous         asyncResp,
70253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Systems/{}/Storage",
71253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME),
7236b5f1edSLakshmi Yadlapati         interface, "/xyz/openbmc_project/inventory");
735e577bc1SWilly Tu }
745e577bc1SWilly Tu 
755e577bc1SWilly Tu inline void handleStorageCollectionGet(
765e577bc1SWilly Tu     App& app, const crow::Request& req,
775e577bc1SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
785e577bc1SWilly Tu {
795e577bc1SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
805e577bc1SWilly Tu     {
815e577bc1SWilly Tu         return;
825e577bc1SWilly Tu     }
835e577bc1SWilly Tu     asyncResp->res.jsonValue["@odata.type"] =
845e577bc1SWilly Tu         "#StorageCollection.StorageCollection";
855e577bc1SWilly Tu     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Storage";
865e577bc1SWilly Tu     asyncResp->res.jsonValue["Name"] = "Storage Collection";
875e577bc1SWilly Tu     constexpr std::array<std::string_view, 1> interface{
885a39f77aSPatrick Williams         "xyz.openbmc_project.Inventory.Item.Storage"};
895e577bc1SWilly Tu     collection_util::getCollectionMembers(
9036b5f1edSLakshmi Yadlapati         asyncResp, boost::urls::format("/redfish/v1/Storage"), interface,
9136b5f1edSLakshmi Yadlapati         "/xyz/openbmc_project/inventory");
92a25aeccfSNikhil Potade }
93a25aeccfSNikhil Potade 
9436d52334SEd Tanous inline void requestRoutesStorageCollection(App& app)
95a25aeccfSNikhil Potade {
9636d52334SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
9736d52334SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
9836d52334SEd Tanous         .methods(boost::beast::http::verb::get)(
9936d52334SEd Tanous             std::bind_front(handleSystemsStorageCollectionGet, std::ref(app)));
1005e577bc1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Storage/")
1015e577bc1SWilly Tu         .privileges(redfish::privileges::getStorageCollection)
1025e577bc1SWilly Tu         .methods(boost::beast::http::verb::get)(
1035e577bc1SWilly Tu             std::bind_front(handleStorageCollectionGet, std::ref(app)));
10436d52334SEd Tanous }
10536d52334SEd Tanous 
10636d52334SEd Tanous inline void afterChassisDriveCollectionSubtree(
10736d52334SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1087a1dbc48SGeorge Liu     const boost::system::error_code& ec,
10936d52334SEd Tanous     const dbus::utility::MapperGetSubTreePathsResponse& driveList)
11036d52334SEd Tanous {
111a25aeccfSNikhil Potade     if (ec)
112a25aeccfSNikhil Potade     {
11362598e31SEd Tanous         BMCWEB_LOG_ERROR("Drive mapper call error");
114a25aeccfSNikhil Potade         messages::internalError(asyncResp->res);
115a25aeccfSNikhil Potade         return;
116a25aeccfSNikhil Potade     }
1172ad9c2f6SJames Feist 
118a85afbe1SWilly Tu     nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
119a85afbe1SWilly Tu     driveArray = nlohmann::json::array();
120a85afbe1SWilly Tu     auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
121a85afbe1SWilly Tu     count = 0;
1222ad9c2f6SJames Feist 
123a85afbe1SWilly Tu     for (const std::string& drive : driveList)
124a25aeccfSNikhil Potade     {
125a85afbe1SWilly Tu         sdbusplus::message::object_path object(drive);
126a85afbe1SWilly Tu         if (object.filename().empty())
127a25aeccfSNikhil Potade         {
12862598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to find filename in {}", drive);
129a85afbe1SWilly Tu             return;
130a25aeccfSNikhil Potade         }
131a85afbe1SWilly Tu 
132a85afbe1SWilly Tu         nlohmann::json::object_t driveJson;
133ef4c65b7SEd Tanous         driveJson["@odata.id"] = boost::urls::format(
134253f11b8SEd Tanous             "/redfish/v1/Systems/{}/Storage/1/Drives/{}",
135253f11b8SEd Tanous             BMCWEB_REDFISH_SYSTEM_URI_NAME, object.filename());
136b2ba3072SPatrick Williams         driveArray.emplace_back(std::move(driveJson));
137a25aeccfSNikhil Potade     }
138a25aeccfSNikhil Potade 
139a85afbe1SWilly Tu     count = driveArray.size();
14036d52334SEd Tanous }
1417ac13cc9SGunnar Mills inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14236d52334SEd Tanous {
14336d52334SEd Tanous     const std::array<std::string_view, 1> interfaces = {
14436d52334SEd Tanous         "xyz.openbmc_project.Inventory.Item.Drive"};
14536d52334SEd Tanous     dbus::utility::getSubTreePaths(
14636d52334SEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces,
1477ac13cc9SGunnar Mills         std::bind_front(afterChassisDriveCollectionSubtree, asyncResp));
148a85afbe1SWilly Tu }
149e284a7c1SJames Feist 
1505e577bc1SWilly Tu inline void afterSystemsStorageGetSubtree(
1515e577bc1SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1525e577bc1SWilly Tu     const std::string& storageId, const boost::system::error_code& ec,
1535e577bc1SWilly Tu     const dbus::utility::MapperGetSubTreeResponse& subtree)
154a85afbe1SWilly Tu {
1555e577bc1SWilly Tu     if (ec)
156a85afbe1SWilly Tu     {
15762598e31SEd Tanous         BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
1585e577bc1SWilly Tu         messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
1595e577bc1SWilly Tu                                    storageId);
160a85afbe1SWilly Tu         return;
161a85afbe1SWilly Tu     }
1623544d2a7SEd Tanous     auto storage = std::ranges::find_if(
1633544d2a7SEd Tanous         subtree,
1645e577bc1SWilly Tu         [&storageId](const std::pair<std::string,
1655e577bc1SWilly Tu                                      dbus::utility::MapperServiceMap>& object) {
1665e577bc1SWilly Tu         return sdbusplus::message::object_path(object.first).filename() ==
1675e577bc1SWilly Tu                storageId;
1685e577bc1SWilly Tu     });
1695e577bc1SWilly Tu     if (storage == subtree.end())
1705e577bc1SWilly Tu     {
1715e577bc1SWilly Tu         messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
1725e577bc1SWilly Tu                                    storageId);
1735e577bc1SWilly Tu         return;
1745e577bc1SWilly Tu     }
1755e577bc1SWilly Tu 
17661b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
177a85afbe1SWilly Tu     asyncResp->res.jsonValue["@odata.id"] =
178253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Systems/{}/Storage/{}",
179253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME, storageId);
180a85afbe1SWilly Tu     asyncResp->res.jsonValue["Name"] = "Storage";
1815e577bc1SWilly Tu     asyncResp->res.jsonValue["Id"] = storageId;
182*539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
183a85afbe1SWilly Tu 
1847ac13cc9SGunnar Mills     getDrives(asyncResp);
185253f11b8SEd Tanous     asyncResp->res.jsonValue["Controllers"]["@odata.id"] =
186253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Systems/{}/Storage/{}/Controllers",
187253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME, storageId);
1885e577bc1SWilly Tu }
1895e577bc1SWilly Tu 
1905e577bc1SWilly Tu inline void
1915e577bc1SWilly Tu     handleSystemsStorageGet(App& app, const crow::Request& req,
1925e577bc1SWilly Tu                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1937f3e84a1SEd Tanous                             const std::string& systemName,
1945e577bc1SWilly Tu                             const std::string& storageId)
1955e577bc1SWilly Tu {
1965e577bc1SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1975e577bc1SWilly Tu     {
1985e577bc1SWilly Tu         return;
1995e577bc1SWilly Tu     }
20025b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
2017f3e84a1SEd Tanous     {
2027f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
2037f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2047f3e84a1SEd Tanous                                    systemName);
2057f3e84a1SEd Tanous         return;
2067f3e84a1SEd Tanous     }
2075e577bc1SWilly Tu 
2085e577bc1SWilly Tu     constexpr std::array<std::string_view, 1> interfaces = {
2095e577bc1SWilly Tu         "xyz.openbmc_project.Inventory.Item.Storage"};
2105e577bc1SWilly Tu     dbus::utility::getSubTree(
2115e577bc1SWilly Tu         "/xyz/openbmc_project/inventory", 0, interfaces,
2125e577bc1SWilly Tu         std::bind_front(afterSystemsStorageGetSubtree, asyncResp, storageId));
2135e577bc1SWilly Tu }
2145e577bc1SWilly Tu 
2155e577bc1SWilly Tu inline void afterSubtree(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2165e577bc1SWilly Tu                          const std::string& storageId,
2175e577bc1SWilly Tu                          const boost::system::error_code& ec,
2185e577bc1SWilly Tu                          const dbus::utility::MapperGetSubTreeResponse& subtree)
2195e577bc1SWilly Tu {
2205e577bc1SWilly Tu     if (ec)
2215e577bc1SWilly Tu     {
22262598e31SEd Tanous         BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
2235e577bc1SWilly Tu         messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
2245e577bc1SWilly Tu                                    storageId);
2255e577bc1SWilly Tu         return;
2265e577bc1SWilly Tu     }
2273544d2a7SEd Tanous     auto storage = std::ranges::find_if(
2283544d2a7SEd Tanous         subtree,
2295e577bc1SWilly Tu         [&storageId](const std::pair<std::string,
2305e577bc1SWilly Tu                                      dbus::utility::MapperServiceMap>& object) {
2315e577bc1SWilly Tu         return sdbusplus::message::object_path(object.first).filename() ==
2325e577bc1SWilly Tu                storageId;
2335e577bc1SWilly Tu     });
2345e577bc1SWilly Tu     if (storage == subtree.end())
2355e577bc1SWilly Tu     {
2365e577bc1SWilly Tu         messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
2375e577bc1SWilly Tu                                    storageId);
2385e577bc1SWilly Tu         return;
2395e577bc1SWilly Tu     }
2405e577bc1SWilly Tu 
2415e577bc1SWilly Tu     asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
2425e577bc1SWilly Tu     asyncResp->res.jsonValue["@odata.id"] =
2435e577bc1SWilly Tu         boost::urls::format("/redfish/v1/Storage/{}", storageId);
2445e577bc1SWilly Tu     asyncResp->res.jsonValue["Name"] = "Storage";
2455e577bc1SWilly Tu     asyncResp->res.jsonValue["Id"] = storageId;
246*539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
2475e577bc1SWilly Tu 
2485e577bc1SWilly Tu     // Storage subsystem to Storage link.
2495e577bc1SWilly Tu     nlohmann::json::array_t storageServices;
2505e577bc1SWilly Tu     nlohmann::json::object_t storageService;
2515e577bc1SWilly Tu     storageService["@odata.id"] =
252253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Systems/{}/Storage/{}",
253253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME, storageId);
2545e577bc1SWilly Tu     storageServices.emplace_back(storageService);
2555e577bc1SWilly Tu     asyncResp->res.jsonValue["Links"]["StorageServices"] =
2565e577bc1SWilly Tu         std::move(storageServices);
2575e577bc1SWilly Tu     asyncResp->res.jsonValue["Links"]["StorageServices@odata.count"] = 1;
2585e577bc1SWilly Tu }
2595e577bc1SWilly Tu 
2605e577bc1SWilly Tu inline void
2615e577bc1SWilly Tu     handleStorageGet(App& app, const crow::Request& req,
2625e577bc1SWilly Tu                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2635e577bc1SWilly Tu                      const std::string& storageId)
2645e577bc1SWilly Tu {
2655e577bc1SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2665e577bc1SWilly Tu     {
26762598e31SEd Tanous         BMCWEB_LOG_DEBUG("requestRoutesStorage setUpRedfishRoute failed");
2685e577bc1SWilly Tu         return;
2695e577bc1SWilly Tu     }
2705e577bc1SWilly Tu 
2715e577bc1SWilly Tu     constexpr std::array<std::string_view, 1> interfaces = {
2725e577bc1SWilly Tu         "xyz.openbmc_project.Inventory.Item.Storage"};
2735e577bc1SWilly Tu     dbus::utility::getSubTree(
2745e577bc1SWilly Tu         "/xyz/openbmc_project/inventory", 0, interfaces,
2755e577bc1SWilly Tu         std::bind_front(afterSubtree, asyncResp, storageId));
27636d52334SEd Tanous }
27736d52334SEd Tanous 
27836d52334SEd Tanous inline void requestRoutesStorage(App& app)
27936d52334SEd Tanous {
2807f3e84a1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/<str>/")
28136d52334SEd Tanous         .privileges(redfish::privileges::getStorage)
28236d52334SEd Tanous         .methods(boost::beast::http::verb::get)(
28336d52334SEd Tanous             std::bind_front(handleSystemsStorageGet, std::ref(app)));
2845e577bc1SWilly Tu 
2855e577bc1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Storage/<str>/")
2865e577bc1SWilly Tu         .privileges(redfish::privileges::getStorage)
2875e577bc1SWilly Tu         .methods(boost::beast::http::verb::get)(
2885e577bc1SWilly Tu             std::bind_front(handleStorageGet, std::ref(app)));
2897e860f15SJohn Edward Broadbent }
2907e860f15SJohn Edward Broadbent 
29103913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29203913171SWilly Tu                           const std::string& connectionName,
29303913171SWilly Tu                           const std::string& path)
29403913171SWilly Tu {
295d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
296d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, path,
297d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Asset",
2985e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
299168e20c1SEd Tanous                     const std::vector<
300168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
30103913171SWilly Tu                         propertiesList) {
30203913171SWilly Tu         if (ec)
30303913171SWilly Tu         {
30403913171SWilly Tu             // this interface isn't necessary
30503913171SWilly Tu             return;
30603913171SWilly Tu         }
307d1bde9e5SKrzysztof Grobelny 
308d1bde9e5SKrzysztof Grobelny         const std::string* partNumber = nullptr;
309d1bde9e5SKrzysztof Grobelny         const std::string* serialNumber = nullptr;
310d1bde9e5SKrzysztof Grobelny         const std::string* manufacturer = nullptr;
311d1bde9e5SKrzysztof Grobelny         const std::string* model = nullptr;
312d1bde9e5SKrzysztof Grobelny 
313d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
314d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
315d1bde9e5SKrzysztof Grobelny             partNumber, "SerialNumber", serialNumber, "Manufacturer",
316d1bde9e5SKrzysztof Grobelny             manufacturer, "Model", model);
317d1bde9e5SKrzysztof Grobelny 
318d1bde9e5SKrzysztof Grobelny         if (!success)
31903913171SWilly Tu         {
32003913171SWilly Tu             messages::internalError(asyncResp->res);
32103913171SWilly Tu             return;
32203913171SWilly Tu         }
323d1bde9e5SKrzysztof Grobelny 
324d1bde9e5SKrzysztof Grobelny         if (partNumber != nullptr)
325d1bde9e5SKrzysztof Grobelny         {
326d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["PartNumber"] = *partNumber;
32703913171SWilly Tu         }
328d1bde9e5SKrzysztof Grobelny 
329d1bde9e5SKrzysztof Grobelny         if (serialNumber != nullptr)
330d1bde9e5SKrzysztof Grobelny         {
331d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
33203913171SWilly Tu         }
333d1bde9e5SKrzysztof Grobelny 
334d1bde9e5SKrzysztof Grobelny         if (manufacturer != nullptr)
335d1bde9e5SKrzysztof Grobelny         {
336d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
337d1bde9e5SKrzysztof Grobelny         }
338d1bde9e5SKrzysztof Grobelny 
339d1bde9e5SKrzysztof Grobelny         if (model != nullptr)
340d1bde9e5SKrzysztof Grobelny         {
341d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Model"] = *model;
342d1bde9e5SKrzysztof Grobelny         }
343d1bde9e5SKrzysztof Grobelny     });
34403913171SWilly Tu }
34503913171SWilly Tu 
34603913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34703913171SWilly Tu                             const std::string& connectionName,
34803913171SWilly Tu                             const std::string& path)
34903913171SWilly Tu {
3501e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3511e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3521e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
3535e7e2dc5SEd Tanous         [asyncResp, path](const boost::system::error_code& ec,
354cef57e85SWilly Tu                           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         {
364*539d8c6bSEd Tanous             asyncResp->res.jsonValue["Status"]["State"] =
365*539d8c6bSEd Tanous                 resource::State::Absent;
36603913171SWilly Tu         }
3671e1e598dSJonathan Doman     });
36803913171SWilly Tu }
36903913171SWilly Tu 
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 {
3741e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3751e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3761e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3775e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec, const bool updating) {
37803913171SWilly Tu         // this interface isn't necessary, only check it
37903913171SWilly Tu         // if we get a good return
38003913171SWilly Tu         if (ec)
38103913171SWilly Tu         {
38203913171SWilly Tu             return;
38303913171SWilly Tu         }
38403913171SWilly Tu 
38503913171SWilly Tu         // updating and disabled in the backend shouldn't be
38603913171SWilly Tu         // able to be set at the same time, so we don't need
38703913171SWilly Tu         // to check for the race condition of these two
38803913171SWilly Tu         // calls
3891e1e598dSJonathan Doman         if (updating)
39003913171SWilly Tu         {
391*539d8c6bSEd Tanous             asyncResp->res.jsonValue["Status"]["State"] =
392*539d8c6bSEd Tanous                 resource::State::Updating;
39303913171SWilly Tu         }
3941e1e598dSJonathan Doman     });
39503913171SWilly Tu }
39603913171SWilly Tu 
397dde9bc12SGeorge Liu inline std::optional<drive::MediaType> convertDriveType(std::string_view type)
39819b8e9a0SWilly Tu {
39919b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
40019b8e9a0SWilly Tu     {
401dde9bc12SGeorge Liu         return drive::MediaType::HDD;
40219b8e9a0SWilly Tu     }
40319b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
40419b8e9a0SWilly Tu     {
405dde9bc12SGeorge Liu         return drive::MediaType::SSD;
40619b8e9a0SWilly Tu     }
407dde9bc12SGeorge Liu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.Unknown")
408dde9bc12SGeorge Liu     {
40919b8e9a0SWilly Tu         return std::nullopt;
41019b8e9a0SWilly Tu     }
41119b8e9a0SWilly Tu 
412dde9bc12SGeorge Liu     return drive::MediaType::Invalid;
413dde9bc12SGeorge Liu }
414dde9bc12SGeorge Liu 
415dde9bc12SGeorge Liu inline std::optional<protocol::Protocol>
416dde9bc12SGeorge Liu     convertDriveProtocol(std::string_view proto)
41719b8e9a0SWilly Tu {
41819b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
41919b8e9a0SWilly Tu     {
420dde9bc12SGeorge Liu         return protocol::Protocol::SAS;
42119b8e9a0SWilly Tu     }
42219b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
42319b8e9a0SWilly Tu     {
424dde9bc12SGeorge Liu         return protocol::Protocol::SATA;
42519b8e9a0SWilly Tu     }
42619b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
42719b8e9a0SWilly Tu     {
428dde9bc12SGeorge Liu         return protocol::Protocol::NVMe;
42919b8e9a0SWilly Tu     }
43019b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
43119b8e9a0SWilly Tu     {
432dde9bc12SGeorge Liu         return protocol::Protocol::FC;
433dde9bc12SGeorge Liu     }
434dde9bc12SGeorge Liu     if (proto ==
435dde9bc12SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.Unknown")
436dde9bc12SGeorge Liu     {
437dde9bc12SGeorge Liu         return std::nullopt;
43819b8e9a0SWilly Tu     }
43919b8e9a0SWilly Tu 
440dde9bc12SGeorge Liu     return protocol::Protocol::Invalid;
44119b8e9a0SWilly Tu }
44219b8e9a0SWilly Tu 
44319b8e9a0SWilly Tu inline void
44419b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
44519b8e9a0SWilly Tu                            const std::string& connectionName,
44619b8e9a0SWilly Tu                            const std::string& path)
44719b8e9a0SWilly Tu {
44819b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
44919b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
45019b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
4515e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
45219b8e9a0SWilly Tu                     const std::vector<
45319b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
45419b8e9a0SWilly Tu                         propertiesList) {
45519b8e9a0SWilly Tu         if (ec)
45619b8e9a0SWilly Tu         {
45719b8e9a0SWilly Tu             // this interface isn't required
45819b8e9a0SWilly Tu             return;
45919b8e9a0SWilly Tu         }
460e5029d88SJohn Edward Broadbent         const std::string* encryptionStatus = nullptr;
461e5029d88SJohn Edward Broadbent         const bool* isLocked = nullptr;
46219b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
46319b8e9a0SWilly Tu                  property : propertiesList)
46419b8e9a0SWilly Tu         {
46519b8e9a0SWilly Tu             const std::string& propertyName = property.first;
46619b8e9a0SWilly Tu             if (propertyName == "Type")
46719b8e9a0SWilly Tu             {
46819b8e9a0SWilly Tu                 const std::string* value =
46919b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
47019b8e9a0SWilly Tu                 if (value == nullptr)
47119b8e9a0SWilly Tu                 {
47219b8e9a0SWilly Tu                     // illegal property
47362598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: Type");
47419b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
47519b8e9a0SWilly Tu                     return;
47619b8e9a0SWilly Tu                 }
47719b8e9a0SWilly Tu 
478dde9bc12SGeorge Liu                 std::optional<drive::MediaType> mediaType =
479dde9bc12SGeorge Liu                     convertDriveType(*value);
48019b8e9a0SWilly Tu                 if (!mediaType)
48119b8e9a0SWilly Tu                 {
48262598e31SEd Tanous                     BMCWEB_LOG_WARNING("UnknownDriveType Interface: {}",
48362598e31SEd Tanous                                        *value);
484dde9bc12SGeorge Liu                     continue;
485dde9bc12SGeorge Liu                 }
486dde9bc12SGeorge Liu                 if (*mediaType == drive::MediaType::Invalid)
487dde9bc12SGeorge Liu                 {
48819b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
48919b8e9a0SWilly Tu                     return;
49019b8e9a0SWilly Tu                 }
49119b8e9a0SWilly Tu 
49219b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
49319b8e9a0SWilly Tu             }
49419b8e9a0SWilly Tu             else if (propertyName == "Capacity")
49519b8e9a0SWilly Tu             {
49619b8e9a0SWilly Tu                 const uint64_t* capacity =
49719b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
49819b8e9a0SWilly Tu                 if (capacity == nullptr)
49919b8e9a0SWilly Tu                 {
50062598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: Capacity");
50119b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
50219b8e9a0SWilly Tu                     return;
50319b8e9a0SWilly Tu                 }
50419b8e9a0SWilly Tu                 if (*capacity == 0)
50519b8e9a0SWilly Tu                 {
50619b8e9a0SWilly Tu                     // drive capacity not known
50719b8e9a0SWilly Tu                     continue;
50819b8e9a0SWilly Tu                 }
50919b8e9a0SWilly Tu 
51019b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
51119b8e9a0SWilly Tu             }
51219b8e9a0SWilly Tu             else if (propertyName == "Protocol")
51319b8e9a0SWilly Tu             {
51419b8e9a0SWilly Tu                 const std::string* value =
51519b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
51619b8e9a0SWilly Tu                 if (value == nullptr)
51719b8e9a0SWilly Tu                 {
51862598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: Protocol");
51919b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
52019b8e9a0SWilly Tu                     return;
52119b8e9a0SWilly Tu                 }
52219b8e9a0SWilly Tu 
523dde9bc12SGeorge Liu                 std::optional<protocol::Protocol> proto =
524dde9bc12SGeorge Liu                     convertDriveProtocol(*value);
52519b8e9a0SWilly Tu                 if (!proto)
52619b8e9a0SWilly Tu                 {
52762598e31SEd Tanous                     BMCWEB_LOG_WARNING("Unknown DrivePrototype Interface: {}",
52862598e31SEd Tanous                                        *value);
529dde9bc12SGeorge Liu                     continue;
530dde9bc12SGeorge Liu                 }
531dde9bc12SGeorge Liu                 if (*proto == protocol::Protocol::Invalid)
532dde9bc12SGeorge Liu                 {
53319b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
53419b8e9a0SWilly Tu                     return;
53519b8e9a0SWilly Tu                 }
53619b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
53719b8e9a0SWilly Tu             }
5383fe4d5ccSJohn Edward Broadbent             else if (propertyName == "PredictedMediaLifeLeftPercent")
5393fe4d5ccSJohn Edward Broadbent             {
5403fe4d5ccSJohn Edward Broadbent                 const uint8_t* lifeLeft =
5413fe4d5ccSJohn Edward Broadbent                     std::get_if<uint8_t>(&property.second);
5423fe4d5ccSJohn Edward Broadbent                 if (lifeLeft == nullptr)
5433fe4d5ccSJohn Edward Broadbent                 {
54462598e31SEd Tanous                     BMCWEB_LOG_ERROR(
54562598e31SEd Tanous                         "Illegal property: PredictedMediaLifeLeftPercent");
5463fe4d5ccSJohn Edward Broadbent                     messages::internalError(asyncResp->res);
5473fe4d5ccSJohn Edward Broadbent                     return;
5483fe4d5ccSJohn Edward Broadbent                 }
5493fe4d5ccSJohn Edward Broadbent                 // 255 means reading the value is not supported
5503fe4d5ccSJohn Edward Broadbent                 if (*lifeLeft != 255)
5513fe4d5ccSJohn Edward Broadbent                 {
5523fe4d5ccSJohn Edward Broadbent                     asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
5533fe4d5ccSJohn Edward Broadbent                         *lifeLeft;
5543fe4d5ccSJohn Edward Broadbent                 }
5553fe4d5ccSJohn Edward Broadbent             }
556e5029d88SJohn Edward Broadbent             else if (propertyName == "EncryptionStatus")
557e5029d88SJohn Edward Broadbent             {
558e5029d88SJohn Edward Broadbent                 encryptionStatus = std::get_if<std::string>(&property.second);
559e5029d88SJohn Edward Broadbent                 if (encryptionStatus == nullptr)
560e5029d88SJohn Edward Broadbent                 {
56162598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: EncryptionStatus");
562e5029d88SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
563e5029d88SJohn Edward Broadbent                     return;
56419b8e9a0SWilly Tu                 }
565e5029d88SJohn Edward Broadbent             }
566e5029d88SJohn Edward Broadbent             else if (propertyName == "Locked")
567e5029d88SJohn Edward Broadbent             {
568e5029d88SJohn Edward Broadbent                 isLocked = std::get_if<bool>(&property.second);
569e5029d88SJohn Edward Broadbent                 if (isLocked == nullptr)
570e5029d88SJohn Edward Broadbent                 {
57162598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: Locked");
572e5029d88SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
573e5029d88SJohn Edward Broadbent                     return;
574e5029d88SJohn Edward Broadbent                 }
575e5029d88SJohn Edward Broadbent             }
576e5029d88SJohn Edward Broadbent         }
577e5029d88SJohn Edward Broadbent 
578e5029d88SJohn Edward Broadbent         if (encryptionStatus == nullptr || isLocked == nullptr ||
579e5029d88SJohn Edward Broadbent             *encryptionStatus ==
580a684c226SKonda Reddy Kachana                 "xyz.openbmc_project.Inventory.Item.Drive.DriveEncryptionState.Unknown")
581e5029d88SJohn Edward Broadbent         {
582e5029d88SJohn Edward Broadbent             return;
583e5029d88SJohn Edward Broadbent         }
584e5029d88SJohn Edward Broadbent         if (*encryptionStatus !=
585a684c226SKonda Reddy Kachana             "xyz.openbmc_project.Inventory.Item.Drive.DriveEncryptionState.Encrypted")
586e5029d88SJohn Edward Broadbent         {
587e5029d88SJohn Edward Broadbent             //"The drive is not currently encrypted."
588e5029d88SJohn Edward Broadbent             asyncResp->res.jsonValue["EncryptionStatus"] =
589e5029d88SJohn Edward Broadbent                 drive::EncryptionStatus::Unencrypted;
590e5029d88SJohn Edward Broadbent             return;
591e5029d88SJohn Edward Broadbent         }
592e5029d88SJohn Edward Broadbent         if (*isLocked)
593e5029d88SJohn Edward Broadbent         {
594e5029d88SJohn Edward Broadbent             //"The drive is currently encrypted and the data is not
595e5029d88SJohn Edward Broadbent             // accessible to the user."
596e5029d88SJohn Edward Broadbent             asyncResp->res.jsonValue["EncryptionStatus"] =
597e5029d88SJohn Edward Broadbent                 drive::EncryptionStatus::Locked;
598e5029d88SJohn Edward Broadbent             return;
599e5029d88SJohn Edward Broadbent         }
600e5029d88SJohn Edward Broadbent         // if not locked
601e5029d88SJohn Edward Broadbent         // "The drive is currently encrypted but the data is accessible
602e5029d88SJohn Edward Broadbent         // to the user in unencrypted form."
603e5029d88SJohn Edward Broadbent         asyncResp->res.jsonValue["EncryptionStatus"] =
604e5029d88SJohn Edward Broadbent             drive::EncryptionStatus::Unlocked;
60519b8e9a0SWilly Tu     });
60619b8e9a0SWilly Tu }
60719b8e9a0SWilly Tu 
608b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
609b53dcd9dSNan Zhou                             const std::string& connectionName,
610b53dcd9dSNan Zhou                             const std::string& path,
611e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
612e56ed6b9SJohn Edward Broadbent {
613e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
614e56ed6b9SJohn Edward Broadbent     {
615e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
616e56ed6b9SJohn Edward Broadbent         {
617e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
618e56ed6b9SJohn Edward Broadbent         }
619e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
620e56ed6b9SJohn Edward Broadbent         {
621e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
622e56ed6b9SJohn Edward Broadbent         }
623e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
624e56ed6b9SJohn Edward Broadbent         {
625e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
626e56ed6b9SJohn Edward Broadbent         }
627e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
628e56ed6b9SJohn Edward Broadbent         {
629e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
630e56ed6b9SJohn Edward Broadbent         }
631e56ed6b9SJohn Edward Broadbent     }
632e56ed6b9SJohn Edward Broadbent }
633e56ed6b9SJohn Edward Broadbent 
63436d52334SEd Tanous inline void afterGetSubtreeSystemsStorageDrive(
63545ca1b86SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
63636d52334SEd Tanous     const std::string& driveId, const boost::system::error_code& ec,
63736d52334SEd Tanous     const dbus::utility::MapperGetSubTreeResponse& subtree)
63845ca1b86SEd Tanous {
6397e860f15SJohn Edward Broadbent     if (ec)
6407e860f15SJohn Edward Broadbent     {
64162598e31SEd Tanous         BMCWEB_LOG_ERROR("Drive mapper call error");
6427e860f15SJohn Edward Broadbent         messages::internalError(asyncResp->res);
6437e860f15SJohn Edward Broadbent         return;
6447e860f15SJohn Edward Broadbent     }
6457e860f15SJohn Edward Broadbent 
6463544d2a7SEd Tanous     auto drive = std::ranges::find_if(
6473544d2a7SEd Tanous         subtree,
64836d52334SEd Tanous         [&driveId](const std::pair<std::string,
6498cb65f8aSNan Zhou                                    dbus::utility::MapperServiceMap>& object) {
65036d52334SEd Tanous         return sdbusplus::message::object_path(object.first).filename() ==
65136d52334SEd Tanous                driveId;
6527e860f15SJohn Edward Broadbent     });
6537e860f15SJohn Edward Broadbent 
65403913171SWilly Tu     if (drive == subtree.end())
6557e860f15SJohn Edward Broadbent     {
656002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "Drive", driveId);
6577e860f15SJohn Edward Broadbent         return;
6587e860f15SJohn Edward Broadbent     }
6597e860f15SJohn Edward Broadbent 
66003913171SWilly Tu     const std::string& path = drive->first;
66136d52334SEd Tanous     const dbus::utility::MapperServiceMap& connectionNames = drive->second;
6627e860f15SJohn Edward Broadbent 
663002d39b4SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
664253f11b8SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
665253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Systems/{}/Storage/1/Drives/{}",
666253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME, driveId);
6677e860f15SJohn Edward Broadbent     asyncResp->res.jsonValue["Name"] = driveId;
6687e860f15SJohn Edward Broadbent     asyncResp->res.jsonValue["Id"] = driveId;
6697e860f15SJohn Edward Broadbent 
6707e860f15SJohn Edward Broadbent     if (connectionNames.size() != 1)
6717e860f15SJohn Edward Broadbent     {
67262598e31SEd Tanous         BMCWEB_LOG_ERROR("Connection size {}, not equal to 1",
67362598e31SEd Tanous                          connectionNames.size());
6747e860f15SJohn Edward Broadbent         messages::internalError(asyncResp->res);
6757e860f15SJohn Edward Broadbent         return;
6767e860f15SJohn Edward Broadbent     }
6777e860f15SJohn Edward Broadbent 
67836d52334SEd Tanous     getMainChassisId(asyncResp,
679ef4c65b7SEd Tanous                      [](const std::string& chassisId,
6807e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
681002d39b4SEd Tanous         aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
682ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
6837e860f15SJohn Edward Broadbent     });
6847e860f15SJohn Edward Broadbent 
685a25aeccfSNikhil Potade     // default it to Enabled
686*539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
687a25aeccfSNikhil Potade 
688e56ed6b9SJohn Edward Broadbent     addAllDriveInfo(asyncResp, connectionNames[0].first, path,
689e56ed6b9SJohn Edward Broadbent                     connectionNames[0].second);
690a25aeccfSNikhil Potade }
69192903bd4SJohn Edward Broadbent 
69236d52334SEd Tanous inline void handleSystemsStorageDriveGet(
69336d52334SEd Tanous     App& app, const crow::Request& req,
69492903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
69536d52334SEd Tanous     const std::string& systemName, const std::string& driveId)
69692903bd4SJohn Edward Broadbent {
6973ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
69892903bd4SJohn Edward Broadbent     {
69992903bd4SJohn Edward Broadbent         return;
70092903bd4SJohn Edward Broadbent     }
70125b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
7027f3e84a1SEd Tanous     {
7037f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
7047f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
7057f3e84a1SEd Tanous                                    systemName);
7067f3e84a1SEd Tanous         return;
7077f3e84a1SEd Tanous     }
7087f3e84a1SEd Tanous 
709253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
71036d52334SEd Tanous     {
71136d52334SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
71236d52334SEd Tanous                                    systemName);
71336d52334SEd Tanous         return;
71436d52334SEd Tanous     }
71592903bd4SJohn Edward Broadbent 
71636d52334SEd Tanous     constexpr std::array<std::string_view, 1> interfaces = {
71736d52334SEd Tanous         "xyz.openbmc_project.Inventory.Item.Drive"};
718e99073f5SGeorge Liu     dbus::utility::getSubTree(
719e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
72036d52334SEd Tanous         std::bind_front(afterGetSubtreeSystemsStorageDrive, asyncResp,
72136d52334SEd Tanous                         driveId));
72236d52334SEd Tanous }
72336d52334SEd Tanous 
72436d52334SEd Tanous inline void requestRoutesDrive(App& app)
72536d52334SEd Tanous {
72636d52334SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
72736d52334SEd Tanous         .privileges(redfish::privileges::getDrive)
72836d52334SEd Tanous         .methods(boost::beast::http::verb::get)(
72936d52334SEd Tanous             std::bind_front(handleSystemsStorageDriveGet, std::ref(app)));
73036d52334SEd Tanous }
73136d52334SEd Tanous 
73236d52334SEd Tanous inline void afterChassisDriveCollectionSubtreeGet(
73336d52334SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
73436d52334SEd Tanous     const std::string& chassisId, const boost::system::error_code& ec,
73536d52334SEd Tanous     const dbus::utility::MapperGetSubTreeResponse& subtree)
73636d52334SEd Tanous {
73792903bd4SJohn Edward Broadbent     if (ec)
73892903bd4SJohn Edward Broadbent     {
73992903bd4SJohn Edward Broadbent         if (ec == boost::system::errc::host_unreachable)
74092903bd4SJohn Edward Broadbent         {
74136d52334SEd Tanous             messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
74292903bd4SJohn Edward Broadbent             return;
74392903bd4SJohn Edward Broadbent         }
74492903bd4SJohn Edward Broadbent         messages::internalError(asyncResp->res);
74592903bd4SJohn Edward Broadbent         return;
74692903bd4SJohn Edward Broadbent     }
74792903bd4SJohn Edward Broadbent 
74892903bd4SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
7498cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
75092903bd4SJohn Edward Broadbent     {
75192903bd4SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
75292903bd4SJohn Edward Broadbent         if (objPath.filename() != chassisId)
75392903bd4SJohn Edward Broadbent         {
75492903bd4SJohn Edward Broadbent             continue;
75592903bd4SJohn Edward Broadbent         }
75692903bd4SJohn Edward Broadbent 
75792903bd4SJohn Edward Broadbent         if (connectionNames.empty())
75892903bd4SJohn Edward Broadbent         {
75962598e31SEd Tanous             BMCWEB_LOG_ERROR("Got 0 Connection names");
76092903bd4SJohn Edward Broadbent             continue;
76192903bd4SJohn Edward Broadbent         }
76292903bd4SJohn Edward Broadbent 
76392903bd4SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] =
76492903bd4SJohn Edward Broadbent             "#DriveCollection.DriveCollection";
76592903bd4SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] =
766ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/Chassis/{}/Drives", chassisId);
76792903bd4SJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = "Drive Collection";
76892903bd4SJohn Edward Broadbent 
76992903bd4SJohn Edward Broadbent         // Association lambda
7706c3e9451SGeorge Liu         dbus::utility::getAssociationEndPoints(
7716c3e9451SGeorge Liu             path + "/drive",
77236d52334SEd Tanous             [asyncResp, chassisId](const boost::system::error_code& ec3,
7736c3e9451SGeorge Liu                                    const dbus::utility::MapperEndPoints& resp) {
77492903bd4SJohn Edward Broadbent             if (ec3)
77592903bd4SJohn Edward Broadbent             {
77662598e31SEd Tanous                 BMCWEB_LOG_ERROR("Error in chassis Drive association ");
77792903bd4SJohn Edward Broadbent             }
77892903bd4SJohn Edward Broadbent             nlohmann::json& members = asyncResp->res.jsonValue["Members"];
77992903bd4SJohn Edward Broadbent             // important if array is empty
78092903bd4SJohn Edward Broadbent             members = nlohmann::json::array();
78192903bd4SJohn Edward Broadbent 
78292903bd4SJohn Edward Broadbent             std::vector<std::string> leafNames;
78392903bd4SJohn Edward Broadbent             for (const auto& drive : resp)
78492903bd4SJohn Edward Broadbent             {
7858a592810SEd Tanous                 sdbusplus::message::object_path drivePath(drive);
7868a592810SEd Tanous                 leafNames.push_back(drivePath.filename());
78792903bd4SJohn Edward Broadbent             }
78892903bd4SJohn Edward Broadbent 
7893544d2a7SEd Tanous             std::ranges::sort(leafNames, AlphanumLess<std::string>());
79092903bd4SJohn Edward Broadbent 
79192903bd4SJohn Edward Broadbent             for (const auto& leafName : leafNames)
79292903bd4SJohn Edward Broadbent             {
79392903bd4SJohn Edward Broadbent                 nlohmann::json::object_t member;
79436d52334SEd Tanous                 member["@odata.id"] = boost::urls::format(
79536d52334SEd Tanous                     "/redfish/v1/Chassis/{}/Drives/{}", 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  */
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 
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 
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
871*539d8c6bSEd 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 
883b53dcd9dSNan Zhou inline void
884b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
885e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
886e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
887e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
888e56ed6b9SJohn Edward Broadbent {
889e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
890e56ed6b9SJohn Edward Broadbent     {
891e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
892e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
893e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
894e56ed6b9SJohn Edward Broadbent         {
895e56ed6b9SJohn Edward Broadbent             continue;
896e56ed6b9SJohn Edward Broadbent         }
897e56ed6b9SJohn Edward Broadbent         //  mapper call drive
898e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> driveInterface = {
899e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
900e99073f5SGeorge Liu         dbus::utility::getSubTree(
901e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, driveInterface,
902e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
903e99073f5SGeorge Liu                 const boost::system::error_code& ec,
904e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
905e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
906e99073f5SGeorge Liu         });
907e56ed6b9SJohn Edward Broadbent     }
908e56ed6b9SJohn Edward Broadbent }
909e56ed6b9SJohn Edward Broadbent 
910b53dcd9dSNan Zhou inline void
911b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
912e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
913e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
914e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
915e56ed6b9SJohn Edward Broadbent {
91603810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
917e56ed6b9SJohn Edward Broadbent     {
918e56ed6b9SJohn Edward Broadbent         return;
919e56ed6b9SJohn Edward Broadbent     }
920e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
921e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
922e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
923e56ed6b9SJohn Edward Broadbent 
924e56ed6b9SJohn Edward Broadbent     // mapper call chassis
925e99073f5SGeorge Liu     dbus::utility::getSubTree(
926e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
927e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
928e99073f5SGeorge Liu          driveName](const boost::system::error_code& ec,
929e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
930e56ed6b9SJohn Edward Broadbent         if (ec)
931e56ed6b9SJohn Edward Broadbent         {
932e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
933e56ed6b9SJohn Edward Broadbent             return;
934e56ed6b9SJohn Edward Broadbent         }
935e56ed6b9SJohn Edward Broadbent 
936e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
9378cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
938e56ed6b9SJohn Edward Broadbent         {
939e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
940e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
941e56ed6b9SJohn Edward Broadbent             {
942e56ed6b9SJohn Edward Broadbent                 continue;
943e56ed6b9SJohn Edward Broadbent             }
944e56ed6b9SJohn Edward Broadbent 
945e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
946e56ed6b9SJohn Edward Broadbent             {
94762598e31SEd Tanous                 BMCWEB_LOG_ERROR("Got 0 Connection names");
948e56ed6b9SJohn Edward Broadbent                 continue;
949e56ed6b9SJohn Edward Broadbent             }
950e56ed6b9SJohn Edward Broadbent 
9516c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
9526c3e9451SGeorge Liu                 path + "/drive",
953e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
9545e7e2dc5SEd Tanous                  driveName](const boost::system::error_code& ec3,
9556c3e9451SGeorge Liu                             const dbus::utility::MapperEndPoints& resp) {
956e56ed6b9SJohn Edward Broadbent                 if (ec3)
957e56ed6b9SJohn Edward Broadbent                 {
958e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
959e56ed6b9SJohn Edward Broadbent                 }
960e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
961e56ed6b9SJohn Edward Broadbent             });
962e56ed6b9SJohn Edward Broadbent             break;
963e56ed6b9SJohn Edward Broadbent         }
964e99073f5SGeorge Liu     });
965e56ed6b9SJohn Edward Broadbent }
966e56ed6b9SJohn Edward Broadbent 
967e56ed6b9SJohn Edward Broadbent /**
968e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
969e56ed6b9SJohn Edward Broadbent  */
970e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
971e56ed6b9SJohn Edward Broadbent {
972e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
973e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
974e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
975e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
976e56ed6b9SJohn Edward Broadbent }
977e56ed6b9SJohn Edward Broadbent 
97861b1eb21SWilly Tu inline void getStorageControllerAsset(
97961b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
98061b1eb21SWilly Tu     const boost::system::error_code& ec,
98161b1eb21SWilly Tu     const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
98261b1eb21SWilly Tu         propertiesList)
98361b1eb21SWilly Tu {
98461b1eb21SWilly Tu     if (ec)
98561b1eb21SWilly Tu     {
98661b1eb21SWilly Tu         // this interface isn't necessary
98762598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to get StorageControllerAsset");
98861b1eb21SWilly Tu         return;
98961b1eb21SWilly Tu     }
99061b1eb21SWilly Tu 
99161b1eb21SWilly Tu     const std::string* partNumber = nullptr;
99261b1eb21SWilly Tu     const std::string* serialNumber = nullptr;
99361b1eb21SWilly Tu     const std::string* manufacturer = nullptr;
99461b1eb21SWilly Tu     const std::string* model = nullptr;
99561b1eb21SWilly Tu     if (!sdbusplus::unpackPropertiesNoThrow(
99661b1eb21SWilly Tu             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
99761b1eb21SWilly Tu             partNumber, "SerialNumber", serialNumber, "Manufacturer",
99861b1eb21SWilly Tu             manufacturer, "Model", model))
99961b1eb21SWilly Tu     {
100061b1eb21SWilly Tu         messages::internalError(asyncResp->res);
100161b1eb21SWilly Tu         return;
100261b1eb21SWilly Tu     }
100361b1eb21SWilly Tu 
100461b1eb21SWilly Tu     if (partNumber != nullptr)
100561b1eb21SWilly Tu     {
100661b1eb21SWilly Tu         asyncResp->res.jsonValue["PartNumber"] = *partNumber;
100761b1eb21SWilly Tu     }
100861b1eb21SWilly Tu 
100961b1eb21SWilly Tu     if (serialNumber != nullptr)
101061b1eb21SWilly Tu     {
101161b1eb21SWilly Tu         asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
101261b1eb21SWilly Tu     }
101361b1eb21SWilly Tu 
101461b1eb21SWilly Tu     if (manufacturer != nullptr)
101561b1eb21SWilly Tu     {
101661b1eb21SWilly Tu         asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
101761b1eb21SWilly Tu     }
101861b1eb21SWilly Tu 
101961b1eb21SWilly Tu     if (model != nullptr)
102061b1eb21SWilly Tu     {
102161b1eb21SWilly Tu         asyncResp->res.jsonValue["Model"] = *model;
102261b1eb21SWilly Tu     }
102361b1eb21SWilly Tu }
102461b1eb21SWilly Tu 
102561b1eb21SWilly Tu inline void populateStorageController(
102661b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
102761b1eb21SWilly Tu     const std::string& controllerId, const std::string& connectionName,
102861b1eb21SWilly Tu     const std::string& path)
102961b1eb21SWilly Tu {
103061b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.type"] =
103161b1eb21SWilly Tu         "#StorageController.v1_6_0.StorageController";
1032253f11b8SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
1033253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Systems/{}/Storage/1/Controllers/{}",
1034253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME, controllerId);
103561b1eb21SWilly Tu     asyncResp->res.jsonValue["Name"] = controllerId;
103661b1eb21SWilly Tu     asyncResp->res.jsonValue["Id"] = controllerId;
1037*539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
103861b1eb21SWilly Tu 
103961b1eb21SWilly Tu     sdbusplus::asio::getProperty<bool>(
104061b1eb21SWilly Tu         *crow::connections::systemBus, connectionName, path,
104161b1eb21SWilly Tu         "xyz.openbmc_project.Inventory.Item", "Present",
104261b1eb21SWilly Tu         [asyncResp](const boost::system::error_code& ec, bool isPresent) {
104361b1eb21SWilly Tu         // this interface isn't necessary, only check it
104461b1eb21SWilly Tu         // if we get a good return
104561b1eb21SWilly Tu         if (ec)
104661b1eb21SWilly Tu         {
104762598e31SEd Tanous             BMCWEB_LOG_DEBUG("Failed to get Present property");
104861b1eb21SWilly Tu             return;
104961b1eb21SWilly Tu         }
105061b1eb21SWilly Tu         if (!isPresent)
105161b1eb21SWilly Tu         {
1052*539d8c6bSEd Tanous             asyncResp->res.jsonValue["Status"]["State"] =
1053*539d8c6bSEd Tanous                 resource::State::Absent;
105461b1eb21SWilly Tu         }
105561b1eb21SWilly Tu     });
105661b1eb21SWilly Tu 
105761b1eb21SWilly Tu     sdbusplus::asio::getAllProperties(
105861b1eb21SWilly Tu         *crow::connections::systemBus, connectionName, path,
105961b1eb21SWilly Tu         "xyz.openbmc_project.Inventory.Decorator.Asset",
106061b1eb21SWilly Tu         [asyncResp](const boost::system::error_code& ec,
106161b1eb21SWilly Tu                     const std::vector<
106261b1eb21SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
106361b1eb21SWilly Tu                         propertiesList) {
106461b1eb21SWilly Tu         getStorageControllerAsset(asyncResp, ec, propertiesList);
106561b1eb21SWilly Tu     });
106661b1eb21SWilly Tu }
106761b1eb21SWilly Tu 
106861b1eb21SWilly Tu inline void getStorageControllerHandler(
106961b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
107061b1eb21SWilly Tu     const std::string& controllerId, const boost::system::error_code& ec,
107161b1eb21SWilly Tu     const dbus::utility::MapperGetSubTreeResponse& subtree)
107261b1eb21SWilly Tu {
107361b1eb21SWilly Tu     if (ec || subtree.empty())
107461b1eb21SWilly Tu     {
107561b1eb21SWilly Tu         // doesn't have to be there
107662598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to handle StorageController");
107761b1eb21SWilly Tu         return;
107861b1eb21SWilly Tu     }
107961b1eb21SWilly Tu 
108061b1eb21SWilly Tu     for (const auto& [path, interfaceDict] : subtree)
108161b1eb21SWilly Tu     {
108261b1eb21SWilly Tu         sdbusplus::message::object_path object(path);
108361b1eb21SWilly Tu         std::string id = object.filename();
108461b1eb21SWilly Tu         if (id.empty())
108561b1eb21SWilly Tu         {
108662598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
108761b1eb21SWilly Tu             return;
108861b1eb21SWilly Tu         }
108961b1eb21SWilly Tu         if (id != controllerId)
109061b1eb21SWilly Tu         {
109161b1eb21SWilly Tu             continue;
109261b1eb21SWilly Tu         }
109361b1eb21SWilly Tu 
109461b1eb21SWilly Tu         if (interfaceDict.size() != 1)
109561b1eb21SWilly Tu         {
109662598e31SEd Tanous             BMCWEB_LOG_ERROR("Connection size {}, greater than 1",
109762598e31SEd Tanous                              interfaceDict.size());
109861b1eb21SWilly Tu             messages::internalError(asyncResp->res);
109961b1eb21SWilly Tu             return;
110061b1eb21SWilly Tu         }
110161b1eb21SWilly Tu 
110261b1eb21SWilly Tu         const std::string& connectionName = interfaceDict.front().first;
110361b1eb21SWilly Tu         populateStorageController(asyncResp, controllerId, connectionName,
110461b1eb21SWilly Tu                                   path);
110561b1eb21SWilly Tu     }
110661b1eb21SWilly Tu }
110761b1eb21SWilly Tu 
110861b1eb21SWilly Tu inline void populateStorageControllerCollection(
110961b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
111061b1eb21SWilly Tu     const boost::system::error_code& ec,
111161b1eb21SWilly Tu     const dbus::utility::MapperGetSubTreePathsResponse& controllerList)
111261b1eb21SWilly Tu {
111361b1eb21SWilly Tu     nlohmann::json::array_t members;
111461b1eb21SWilly Tu     if (ec || controllerList.empty())
111561b1eb21SWilly Tu     {
111661b1eb21SWilly Tu         asyncResp->res.jsonValue["Members"] = std::move(members);
111761b1eb21SWilly Tu         asyncResp->res.jsonValue["Members@odata.count"] = 0;
111862598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to find any StorageController");
111961b1eb21SWilly Tu         return;
112061b1eb21SWilly Tu     }
112161b1eb21SWilly Tu 
112261b1eb21SWilly Tu     for (const std::string& path : controllerList)
112361b1eb21SWilly Tu     {
112461b1eb21SWilly Tu         std::string id = sdbusplus::message::object_path(path).filename();
112561b1eb21SWilly Tu         if (id.empty())
112661b1eb21SWilly Tu         {
112762598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
112861b1eb21SWilly Tu             return;
112961b1eb21SWilly Tu         }
113061b1eb21SWilly Tu         nlohmann::json::object_t member;
113161b1eb21SWilly Tu         member["@odata.id"] = boost::urls::format(
1132253f11b8SEd Tanous             "/redfish/v1/Systems/{}/Storage/1/Controllers/{}",
1133253f11b8SEd Tanous             BMCWEB_REDFISH_SYSTEM_URI_NAME, id);
113461b1eb21SWilly Tu         members.emplace_back(member);
113561b1eb21SWilly Tu     }
113661b1eb21SWilly Tu     asyncResp->res.jsonValue["Members@odata.count"] = members.size();
113761b1eb21SWilly Tu     asyncResp->res.jsonValue["Members"] = std::move(members);
113861b1eb21SWilly Tu }
113961b1eb21SWilly Tu 
114036d52334SEd Tanous inline void handleSystemsStorageControllerCollectionGet(
114161b1eb21SWilly Tu     App& app, const crow::Request& req,
114261b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
114361b1eb21SWilly Tu     const std::string& systemName)
114461b1eb21SWilly Tu {
114561b1eb21SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
114661b1eb21SWilly Tu     {
114762598e31SEd Tanous         BMCWEB_LOG_DEBUG(
114862598e31SEd Tanous             "Failed to setup Redfish Route for StorageController Collection");
114961b1eb21SWilly Tu         return;
115061b1eb21SWilly Tu     }
1151253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
115261b1eb21SWilly Tu     {
115361b1eb21SWilly Tu         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
115461b1eb21SWilly Tu                                    systemName);
115562598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
115661b1eb21SWilly Tu         return;
115761b1eb21SWilly Tu     }
115861b1eb21SWilly Tu 
115961b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.type"] =
116061b1eb21SWilly Tu         "#StorageControllerCollection.StorageControllerCollection";
116161b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.id"] =
1162253f11b8SEd Tanous         std::format("/redfish/v1/Systems/{}/Storage/1/Controllers",
1163253f11b8SEd Tanous                     BMCWEB_REDFISH_SYSTEM_URI_NAME);
116461b1eb21SWilly Tu     asyncResp->res.jsonValue["Name"] = "Storage Controller Collection";
116561b1eb21SWilly Tu 
116661b1eb21SWilly Tu     constexpr std::array<std::string_view, 1> interfaces = {
116761b1eb21SWilly Tu         "xyz.openbmc_project.Inventory.Item.StorageController"};
116861b1eb21SWilly Tu     dbus::utility::getSubTreePaths(
116961b1eb21SWilly Tu         "/xyz/openbmc_project/inventory", 0, interfaces,
117061b1eb21SWilly Tu         [asyncResp](const boost::system::error_code& ec,
117161b1eb21SWilly Tu                     const dbus::utility::MapperGetSubTreePathsResponse&
117261b1eb21SWilly Tu                         controllerList) {
117361b1eb21SWilly Tu         populateStorageControllerCollection(asyncResp, ec, controllerList);
117461b1eb21SWilly Tu     });
117561b1eb21SWilly Tu }
117661b1eb21SWilly Tu 
117736d52334SEd Tanous inline void handleSystemsStorageControllerGet(
117861b1eb21SWilly Tu     App& app, const crow::Request& req,
117961b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
118061b1eb21SWilly Tu     const std::string& systemName, const std::string& controllerId)
118161b1eb21SWilly Tu {
118261b1eb21SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
118361b1eb21SWilly Tu     {
118462598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to setup Redfish Route for StorageController");
118561b1eb21SWilly Tu         return;
118661b1eb21SWilly Tu     }
1187253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
118861b1eb21SWilly Tu     {
118961b1eb21SWilly Tu         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
119061b1eb21SWilly Tu                                    systemName);
119162598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
119261b1eb21SWilly Tu         return;
119361b1eb21SWilly Tu     }
119461b1eb21SWilly Tu     constexpr std::array<std::string_view, 1> interfaces = {
119561b1eb21SWilly Tu         "xyz.openbmc_project.Inventory.Item.StorageController"};
119661b1eb21SWilly Tu     dbus::utility::getSubTree(
119761b1eb21SWilly Tu         "/xyz/openbmc_project/inventory", 0, interfaces,
119861b1eb21SWilly Tu         [asyncResp,
119961b1eb21SWilly Tu          controllerId](const boost::system::error_code& ec,
120061b1eb21SWilly Tu                        const dbus::utility::MapperGetSubTreeResponse& subtree) {
120161b1eb21SWilly Tu         getStorageControllerHandler(asyncResp, controllerId, ec, subtree);
120261b1eb21SWilly Tu     });
120361b1eb21SWilly Tu }
120461b1eb21SWilly Tu 
120561b1eb21SWilly Tu inline void requestRoutesStorageControllerCollection(App& app)
120661b1eb21SWilly Tu {
120761b1eb21SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/")
120861b1eb21SWilly Tu         .privileges(redfish::privileges::getStorageControllerCollection)
120936d52334SEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
121036d52334SEd Tanous             handleSystemsStorageControllerCollectionGet, std::ref(app)));
121161b1eb21SWilly Tu }
121261b1eb21SWilly Tu 
121361b1eb21SWilly Tu inline void requestRoutesStorageController(App& app)
121461b1eb21SWilly Tu {
121561b1eb21SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/<str>")
121661b1eb21SWilly Tu         .privileges(redfish::privileges::getStorageController)
121761b1eb21SWilly Tu         .methods(boost::beast::http::verb::get)(
121836d52334SEd Tanous             std::bind_front(handleSystemsStorageControllerGet, std::ref(app)));
121961b1eb21SWilly Tu }
122061b1eb21SWilly Tu 
1221a25aeccfSNikhil Potade } // namespace redfish
1222