xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision 25b54dba775b31021a3a4677eb79e9771bcb97f7)
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"
24a8e884fcSEd Tanous #include "human_sort.hpp"
25e284a7c1SJames Feist #include "openbmc_dbus_rest.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     }
5322d268cbSEd Tanous     if (systemName != "system")
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";
628d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.id"] =
638d1b46d7Szhanghch05         "/redfish/v1/Systems/system/Storage";
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(
695e577bc1SWilly Tu         asyncResp, boost::urls::format("/redfish/v1/Systems/system/Storage"),
7036b5f1edSLakshmi Yadlapati         interface, "/xyz/openbmc_project/inventory");
715e577bc1SWilly Tu }
725e577bc1SWilly Tu 
735e577bc1SWilly Tu inline void handleStorageCollectionGet(
745e577bc1SWilly Tu     App& app, const crow::Request& req,
755e577bc1SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
765e577bc1SWilly Tu {
775e577bc1SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
785e577bc1SWilly Tu     {
795e577bc1SWilly Tu         return;
805e577bc1SWilly Tu     }
815e577bc1SWilly Tu     asyncResp->res.jsonValue["@odata.type"] =
825e577bc1SWilly Tu         "#StorageCollection.StorageCollection";
835e577bc1SWilly Tu     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Storage";
845e577bc1SWilly Tu     asyncResp->res.jsonValue["Name"] = "Storage Collection";
855e577bc1SWilly Tu     constexpr std::array<std::string_view, 1> interface{
865a39f77aSPatrick Williams         "xyz.openbmc_project.Inventory.Item.Storage"};
875e577bc1SWilly Tu     collection_util::getCollectionMembers(
8836b5f1edSLakshmi Yadlapati         asyncResp, boost::urls::format("/redfish/v1/Storage"), interface,
8936b5f1edSLakshmi Yadlapati         "/xyz/openbmc_project/inventory");
90a25aeccfSNikhil Potade }
91a25aeccfSNikhil Potade 
9236d52334SEd Tanous inline void requestRoutesStorageCollection(App& app)
93a25aeccfSNikhil Potade {
9436d52334SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
9536d52334SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
9636d52334SEd Tanous         .methods(boost::beast::http::verb::get)(
9736d52334SEd Tanous             std::bind_front(handleSystemsStorageCollectionGet, std::ref(app)));
985e577bc1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Storage/")
995e577bc1SWilly Tu         .privileges(redfish::privileges::getStorageCollection)
1005e577bc1SWilly Tu         .methods(boost::beast::http::verb::get)(
1015e577bc1SWilly Tu             std::bind_front(handleStorageCollectionGet, std::ref(app)));
10236d52334SEd Tanous }
10336d52334SEd Tanous 
10436d52334SEd Tanous inline void afterChassisDriveCollectionSubtree(
10536d52334SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1067a1dbc48SGeorge Liu     const boost::system::error_code& ec,
10736d52334SEd Tanous     const dbus::utility::MapperGetSubTreePathsResponse& driveList)
10836d52334SEd Tanous {
109a25aeccfSNikhil Potade     if (ec)
110a25aeccfSNikhil Potade     {
11162598e31SEd Tanous         BMCWEB_LOG_ERROR("Drive mapper call error");
112a25aeccfSNikhil Potade         messages::internalError(asyncResp->res);
113a25aeccfSNikhil Potade         return;
114a25aeccfSNikhil Potade     }
1152ad9c2f6SJames Feist 
116a85afbe1SWilly Tu     nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
117a85afbe1SWilly Tu     driveArray = nlohmann::json::array();
118a85afbe1SWilly Tu     auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
119a85afbe1SWilly Tu     count = 0;
1202ad9c2f6SJames Feist 
121a85afbe1SWilly Tu     for (const std::string& drive : driveList)
122a25aeccfSNikhil Potade     {
123a85afbe1SWilly Tu         sdbusplus::message::object_path object(drive);
124a85afbe1SWilly Tu         if (object.filename().empty())
125a25aeccfSNikhil Potade         {
12662598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to find filename in {}", drive);
127a85afbe1SWilly Tu             return;
128a25aeccfSNikhil Potade         }
129a85afbe1SWilly Tu 
130a85afbe1SWilly Tu         nlohmann::json::object_t driveJson;
131ef4c65b7SEd Tanous         driveJson["@odata.id"] = boost::urls::format(
132ef4c65b7SEd Tanous             "/redfish/v1/Systems/system/Storage/1/Drives/{}",
133eddfc437SWilly Tu             object.filename());
134b2ba3072SPatrick Williams         driveArray.emplace_back(std::move(driveJson));
135a25aeccfSNikhil Potade     }
136a25aeccfSNikhil Potade 
137a85afbe1SWilly Tu     count = driveArray.size();
13836d52334SEd Tanous }
1397ac13cc9SGunnar Mills inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14036d52334SEd Tanous {
14136d52334SEd Tanous     const std::array<std::string_view, 1> interfaces = {
14236d52334SEd Tanous         "xyz.openbmc_project.Inventory.Item.Drive"};
14336d52334SEd Tanous     dbus::utility::getSubTreePaths(
14436d52334SEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces,
1457ac13cc9SGunnar Mills         std::bind_front(afterChassisDriveCollectionSubtree, asyncResp));
146a85afbe1SWilly Tu }
147e284a7c1SJames Feist 
1485e577bc1SWilly Tu inline void afterSystemsStorageGetSubtree(
1495e577bc1SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1505e577bc1SWilly Tu     const std::string& storageId, const boost::system::error_code& ec,
1515e577bc1SWilly Tu     const dbus::utility::MapperGetSubTreeResponse& subtree)
152a85afbe1SWilly Tu {
1535e577bc1SWilly Tu     if (ec)
154a85afbe1SWilly Tu     {
15562598e31SEd Tanous         BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
1565e577bc1SWilly Tu         messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
1575e577bc1SWilly Tu                                    storageId);
158a85afbe1SWilly Tu         return;
159a85afbe1SWilly Tu     }
1603544d2a7SEd Tanous     auto storage = std::ranges::find_if(
1613544d2a7SEd Tanous         subtree,
1625e577bc1SWilly Tu         [&storageId](const std::pair<std::string,
1635e577bc1SWilly Tu                                      dbus::utility::MapperServiceMap>& object) {
1645e577bc1SWilly Tu         return sdbusplus::message::object_path(object.first).filename() ==
1655e577bc1SWilly Tu                storageId;
1665e577bc1SWilly Tu     });
1675e577bc1SWilly Tu     if (storage == subtree.end())
1685e577bc1SWilly Tu     {
1695e577bc1SWilly Tu         messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
1705e577bc1SWilly Tu                                    storageId);
1715e577bc1SWilly Tu         return;
1725e577bc1SWilly Tu     }
1735e577bc1SWilly Tu 
17461b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
175a85afbe1SWilly Tu     asyncResp->res.jsonValue["@odata.id"] =
1765e577bc1SWilly Tu         boost::urls::format("/redfish/v1/Systems/system/Storage/{}", storageId);
177a85afbe1SWilly Tu     asyncResp->res.jsonValue["Name"] = "Storage";
1785e577bc1SWilly Tu     asyncResp->res.jsonValue["Id"] = storageId;
179a85afbe1SWilly Tu     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
180a85afbe1SWilly Tu 
1817ac13cc9SGunnar Mills     getDrives(asyncResp);
1825e577bc1SWilly Tu     asyncResp->res.jsonValue["Controllers"]["@odata.id"] = boost::urls::format(
1835e577bc1SWilly Tu         "/redfish/v1/Systems/system/Storage/{}/Controllers", storageId);
1845e577bc1SWilly Tu }
1855e577bc1SWilly Tu 
1865e577bc1SWilly Tu inline void
1875e577bc1SWilly Tu     handleSystemsStorageGet(App& app, const crow::Request& req,
1885e577bc1SWilly Tu                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1897f3e84a1SEd Tanous                             const std::string& systemName,
1905e577bc1SWilly Tu                             const std::string& storageId)
1915e577bc1SWilly Tu {
1925e577bc1SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1935e577bc1SWilly Tu     {
1945e577bc1SWilly Tu         return;
1955e577bc1SWilly Tu     }
196*25b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
1977f3e84a1SEd Tanous     {
1987f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
1997f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2007f3e84a1SEd Tanous                                    systemName);
2017f3e84a1SEd Tanous         return;
2027f3e84a1SEd Tanous     }
2035e577bc1SWilly Tu 
2045e577bc1SWilly Tu     constexpr std::array<std::string_view, 1> interfaces = {
2055e577bc1SWilly Tu         "xyz.openbmc_project.Inventory.Item.Storage"};
2065e577bc1SWilly Tu     dbus::utility::getSubTree(
2075e577bc1SWilly Tu         "/xyz/openbmc_project/inventory", 0, interfaces,
2085e577bc1SWilly Tu         std::bind_front(afterSystemsStorageGetSubtree, asyncResp, storageId));
2095e577bc1SWilly Tu }
2105e577bc1SWilly Tu 
2115e577bc1SWilly Tu inline void afterSubtree(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2125e577bc1SWilly Tu                          const std::string& storageId,
2135e577bc1SWilly Tu                          const boost::system::error_code& ec,
2145e577bc1SWilly Tu                          const dbus::utility::MapperGetSubTreeResponse& subtree)
2155e577bc1SWilly Tu {
2165e577bc1SWilly Tu     if (ec)
2175e577bc1SWilly Tu     {
21862598e31SEd Tanous         BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
2195e577bc1SWilly Tu         messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
2205e577bc1SWilly Tu                                    storageId);
2215e577bc1SWilly Tu         return;
2225e577bc1SWilly Tu     }
2233544d2a7SEd Tanous     auto storage = std::ranges::find_if(
2243544d2a7SEd Tanous         subtree,
2255e577bc1SWilly Tu         [&storageId](const std::pair<std::string,
2265e577bc1SWilly Tu                                      dbus::utility::MapperServiceMap>& object) {
2275e577bc1SWilly Tu         return sdbusplus::message::object_path(object.first).filename() ==
2285e577bc1SWilly Tu                storageId;
2295e577bc1SWilly Tu     });
2305e577bc1SWilly Tu     if (storage == subtree.end())
2315e577bc1SWilly Tu     {
2325e577bc1SWilly Tu         messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
2335e577bc1SWilly Tu                                    storageId);
2345e577bc1SWilly Tu         return;
2355e577bc1SWilly Tu     }
2365e577bc1SWilly Tu 
2375e577bc1SWilly Tu     asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
2385e577bc1SWilly Tu     asyncResp->res.jsonValue["@odata.id"] =
2395e577bc1SWilly Tu         boost::urls::format("/redfish/v1/Storage/{}", storageId);
2405e577bc1SWilly Tu     asyncResp->res.jsonValue["Name"] = "Storage";
2415e577bc1SWilly Tu     asyncResp->res.jsonValue["Id"] = storageId;
2425e577bc1SWilly Tu     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
2435e577bc1SWilly Tu 
2445e577bc1SWilly Tu     // Storage subsystem to Storage link.
2455e577bc1SWilly Tu     nlohmann::json::array_t storageServices;
2465e577bc1SWilly Tu     nlohmann::json::object_t storageService;
2475e577bc1SWilly Tu     storageService["@odata.id"] =
2485e577bc1SWilly Tu         boost::urls::format("/redfish/v1/Systems/system/Storage/{}", storageId);
2495e577bc1SWilly Tu     storageServices.emplace_back(storageService);
2505e577bc1SWilly Tu     asyncResp->res.jsonValue["Links"]["StorageServices"] =
2515e577bc1SWilly Tu         std::move(storageServices);
2525e577bc1SWilly Tu     asyncResp->res.jsonValue["Links"]["StorageServices@odata.count"] = 1;
2535e577bc1SWilly Tu }
2545e577bc1SWilly Tu 
2555e577bc1SWilly Tu inline void
2565e577bc1SWilly Tu     handleStorageGet(App& app, const crow::Request& req,
2575e577bc1SWilly Tu                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2585e577bc1SWilly Tu                      const std::string& storageId)
2595e577bc1SWilly Tu {
2605e577bc1SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2615e577bc1SWilly Tu     {
26262598e31SEd Tanous         BMCWEB_LOG_DEBUG("requestRoutesStorage setUpRedfishRoute failed");
2635e577bc1SWilly Tu         return;
2645e577bc1SWilly Tu     }
2655e577bc1SWilly Tu 
2665e577bc1SWilly Tu     constexpr std::array<std::string_view, 1> interfaces = {
2675e577bc1SWilly Tu         "xyz.openbmc_project.Inventory.Item.Storage"};
2685e577bc1SWilly Tu     dbus::utility::getSubTree(
2695e577bc1SWilly Tu         "/xyz/openbmc_project/inventory", 0, interfaces,
2705e577bc1SWilly Tu         std::bind_front(afterSubtree, asyncResp, storageId));
27136d52334SEd Tanous }
27236d52334SEd Tanous 
27336d52334SEd Tanous inline void requestRoutesStorage(App& app)
27436d52334SEd Tanous {
2757f3e84a1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/<str>/")
27636d52334SEd Tanous         .privileges(redfish::privileges::getStorage)
27736d52334SEd Tanous         .methods(boost::beast::http::verb::get)(
27836d52334SEd Tanous             std::bind_front(handleSystemsStorageGet, std::ref(app)));
2795e577bc1SWilly Tu 
2805e577bc1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Storage/<str>/")
2815e577bc1SWilly Tu         .privileges(redfish::privileges::getStorage)
2825e577bc1SWilly Tu         .methods(boost::beast::http::verb::get)(
2835e577bc1SWilly Tu             std::bind_front(handleStorageGet, std::ref(app)));
2847e860f15SJohn Edward Broadbent }
2857e860f15SJohn Edward Broadbent 
28603913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28703913171SWilly Tu                           const std::string& connectionName,
28803913171SWilly Tu                           const std::string& path)
28903913171SWilly Tu {
290d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
291d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, path,
292d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Asset",
2935e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
294168e20c1SEd Tanous                     const std::vector<
295168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
29603913171SWilly Tu                         propertiesList) {
29703913171SWilly Tu         if (ec)
29803913171SWilly Tu         {
29903913171SWilly Tu             // this interface isn't necessary
30003913171SWilly Tu             return;
30103913171SWilly Tu         }
302d1bde9e5SKrzysztof Grobelny 
303d1bde9e5SKrzysztof Grobelny         const std::string* partNumber = nullptr;
304d1bde9e5SKrzysztof Grobelny         const std::string* serialNumber = nullptr;
305d1bde9e5SKrzysztof Grobelny         const std::string* manufacturer = nullptr;
306d1bde9e5SKrzysztof Grobelny         const std::string* model = nullptr;
307d1bde9e5SKrzysztof Grobelny 
308d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
309d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
310d1bde9e5SKrzysztof Grobelny             partNumber, "SerialNumber", serialNumber, "Manufacturer",
311d1bde9e5SKrzysztof Grobelny             manufacturer, "Model", model);
312d1bde9e5SKrzysztof Grobelny 
313d1bde9e5SKrzysztof Grobelny         if (!success)
31403913171SWilly Tu         {
31503913171SWilly Tu             messages::internalError(asyncResp->res);
31603913171SWilly Tu             return;
31703913171SWilly Tu         }
318d1bde9e5SKrzysztof Grobelny 
319d1bde9e5SKrzysztof Grobelny         if (partNumber != nullptr)
320d1bde9e5SKrzysztof Grobelny         {
321d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["PartNumber"] = *partNumber;
32203913171SWilly Tu         }
323d1bde9e5SKrzysztof Grobelny 
324d1bde9e5SKrzysztof Grobelny         if (serialNumber != nullptr)
325d1bde9e5SKrzysztof Grobelny         {
326d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
32703913171SWilly Tu         }
328d1bde9e5SKrzysztof Grobelny 
329d1bde9e5SKrzysztof Grobelny         if (manufacturer != nullptr)
330d1bde9e5SKrzysztof Grobelny         {
331d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
332d1bde9e5SKrzysztof Grobelny         }
333d1bde9e5SKrzysztof Grobelny 
334d1bde9e5SKrzysztof Grobelny         if (model != nullptr)
335d1bde9e5SKrzysztof Grobelny         {
336d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Model"] = *model;
337d1bde9e5SKrzysztof Grobelny         }
338d1bde9e5SKrzysztof Grobelny     });
33903913171SWilly Tu }
34003913171SWilly Tu 
34103913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34203913171SWilly Tu                             const std::string& connectionName,
34303913171SWilly Tu                             const std::string& path)
34403913171SWilly Tu {
3451e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3461e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3471e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
3485e7e2dc5SEd Tanous         [asyncResp, path](const boost::system::error_code& ec,
349cef57e85SWilly Tu                           const bool isPresent) {
35003913171SWilly Tu         // this interface isn't necessary, only check it if
35103913171SWilly Tu         // we get a good return
35203913171SWilly Tu         if (ec)
35303913171SWilly Tu         {
35403913171SWilly Tu             return;
35503913171SWilly Tu         }
35603913171SWilly Tu 
357cef57e85SWilly Tu         if (!isPresent)
35803913171SWilly Tu         {
359cef57e85SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Absent";
36003913171SWilly Tu         }
3611e1e598dSJonathan Doman     });
36203913171SWilly Tu }
36303913171SWilly Tu 
36403913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36503913171SWilly Tu                           const std::string& connectionName,
36603913171SWilly Tu                           const std::string& path)
36703913171SWilly Tu {
3681e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3691e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3701e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3715e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec, const bool updating) {
37203913171SWilly Tu         // this interface isn't necessary, only check it
37303913171SWilly Tu         // if we get a good return
37403913171SWilly Tu         if (ec)
37503913171SWilly Tu         {
37603913171SWilly Tu             return;
37703913171SWilly Tu         }
37803913171SWilly Tu 
37903913171SWilly Tu         // updating and disabled in the backend shouldn't be
38003913171SWilly Tu         // able to be set at the same time, so we don't need
38103913171SWilly Tu         // to check for the race condition of these two
38203913171SWilly Tu         // calls
3831e1e598dSJonathan Doman         if (updating)
38403913171SWilly Tu         {
38503913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Updating";
38603913171SWilly Tu         }
3871e1e598dSJonathan Doman     });
38803913171SWilly Tu }
38903913171SWilly Tu 
390dde9bc12SGeorge Liu inline std::optional<drive::MediaType> convertDriveType(std::string_view type)
39119b8e9a0SWilly Tu {
39219b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
39319b8e9a0SWilly Tu     {
394dde9bc12SGeorge Liu         return drive::MediaType::HDD;
39519b8e9a0SWilly Tu     }
39619b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
39719b8e9a0SWilly Tu     {
398dde9bc12SGeorge Liu         return drive::MediaType::SSD;
39919b8e9a0SWilly Tu     }
400dde9bc12SGeorge Liu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.Unknown")
401dde9bc12SGeorge Liu     {
40219b8e9a0SWilly Tu         return std::nullopt;
40319b8e9a0SWilly Tu     }
40419b8e9a0SWilly Tu 
405dde9bc12SGeorge Liu     return drive::MediaType::Invalid;
406dde9bc12SGeorge Liu }
407dde9bc12SGeorge Liu 
408dde9bc12SGeorge Liu inline std::optional<protocol::Protocol>
409dde9bc12SGeorge Liu     convertDriveProtocol(std::string_view proto)
41019b8e9a0SWilly Tu {
41119b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
41219b8e9a0SWilly Tu     {
413dde9bc12SGeorge Liu         return protocol::Protocol::SAS;
41419b8e9a0SWilly Tu     }
41519b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
41619b8e9a0SWilly Tu     {
417dde9bc12SGeorge Liu         return protocol::Protocol::SATA;
41819b8e9a0SWilly Tu     }
41919b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
42019b8e9a0SWilly Tu     {
421dde9bc12SGeorge Liu         return protocol::Protocol::NVMe;
42219b8e9a0SWilly Tu     }
42319b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
42419b8e9a0SWilly Tu     {
425dde9bc12SGeorge Liu         return protocol::Protocol::FC;
426dde9bc12SGeorge Liu     }
427dde9bc12SGeorge Liu     if (proto ==
428dde9bc12SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.Unknown")
429dde9bc12SGeorge Liu     {
430dde9bc12SGeorge Liu         return std::nullopt;
43119b8e9a0SWilly Tu     }
43219b8e9a0SWilly Tu 
433dde9bc12SGeorge Liu     return protocol::Protocol::Invalid;
43419b8e9a0SWilly Tu }
43519b8e9a0SWilly Tu 
43619b8e9a0SWilly Tu inline void
43719b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
43819b8e9a0SWilly Tu                            const std::string& connectionName,
43919b8e9a0SWilly Tu                            const std::string& path)
44019b8e9a0SWilly Tu {
44119b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
44219b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
44319b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
4445e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
44519b8e9a0SWilly Tu                     const std::vector<
44619b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
44719b8e9a0SWilly Tu                         propertiesList) {
44819b8e9a0SWilly Tu         if (ec)
44919b8e9a0SWilly Tu         {
45019b8e9a0SWilly Tu             // this interface isn't required
45119b8e9a0SWilly Tu             return;
45219b8e9a0SWilly Tu         }
453e5029d88SJohn Edward Broadbent         const std::string* encryptionStatus = nullptr;
454e5029d88SJohn Edward Broadbent         const bool* isLocked = nullptr;
45519b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
45619b8e9a0SWilly Tu                  property : propertiesList)
45719b8e9a0SWilly Tu         {
45819b8e9a0SWilly Tu             const std::string& propertyName = property.first;
45919b8e9a0SWilly Tu             if (propertyName == "Type")
46019b8e9a0SWilly Tu             {
46119b8e9a0SWilly Tu                 const std::string* value =
46219b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
46319b8e9a0SWilly Tu                 if (value == nullptr)
46419b8e9a0SWilly Tu                 {
46519b8e9a0SWilly Tu                     // illegal property
46662598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: Type");
46719b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
46819b8e9a0SWilly Tu                     return;
46919b8e9a0SWilly Tu                 }
47019b8e9a0SWilly Tu 
471dde9bc12SGeorge Liu                 std::optional<drive::MediaType> mediaType =
472dde9bc12SGeorge Liu                     convertDriveType(*value);
47319b8e9a0SWilly Tu                 if (!mediaType)
47419b8e9a0SWilly Tu                 {
47562598e31SEd Tanous                     BMCWEB_LOG_WARNING("UnknownDriveType Interface: {}",
47662598e31SEd Tanous                                        *value);
477dde9bc12SGeorge Liu                     continue;
478dde9bc12SGeorge Liu                 }
479dde9bc12SGeorge Liu                 if (*mediaType == drive::MediaType::Invalid)
480dde9bc12SGeorge Liu                 {
48119b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
48219b8e9a0SWilly Tu                     return;
48319b8e9a0SWilly Tu                 }
48419b8e9a0SWilly Tu 
48519b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
48619b8e9a0SWilly Tu             }
48719b8e9a0SWilly Tu             else if (propertyName == "Capacity")
48819b8e9a0SWilly Tu             {
48919b8e9a0SWilly Tu                 const uint64_t* capacity =
49019b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
49119b8e9a0SWilly Tu                 if (capacity == nullptr)
49219b8e9a0SWilly Tu                 {
49362598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: Capacity");
49419b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
49519b8e9a0SWilly Tu                     return;
49619b8e9a0SWilly Tu                 }
49719b8e9a0SWilly Tu                 if (*capacity == 0)
49819b8e9a0SWilly Tu                 {
49919b8e9a0SWilly Tu                     // drive capacity not known
50019b8e9a0SWilly Tu                     continue;
50119b8e9a0SWilly Tu                 }
50219b8e9a0SWilly Tu 
50319b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
50419b8e9a0SWilly Tu             }
50519b8e9a0SWilly Tu             else if (propertyName == "Protocol")
50619b8e9a0SWilly Tu             {
50719b8e9a0SWilly Tu                 const std::string* value =
50819b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
50919b8e9a0SWilly Tu                 if (value == nullptr)
51019b8e9a0SWilly Tu                 {
51162598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: Protocol");
51219b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
51319b8e9a0SWilly Tu                     return;
51419b8e9a0SWilly Tu                 }
51519b8e9a0SWilly Tu 
516dde9bc12SGeorge Liu                 std::optional<protocol::Protocol> proto =
517dde9bc12SGeorge Liu                     convertDriveProtocol(*value);
51819b8e9a0SWilly Tu                 if (!proto)
51919b8e9a0SWilly Tu                 {
52062598e31SEd Tanous                     BMCWEB_LOG_WARNING("Unknown DrivePrototype Interface: {}",
52162598e31SEd Tanous                                        *value);
522dde9bc12SGeorge Liu                     continue;
523dde9bc12SGeorge Liu                 }
524dde9bc12SGeorge Liu                 if (*proto == protocol::Protocol::Invalid)
525dde9bc12SGeorge Liu                 {
52619b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
52719b8e9a0SWilly Tu                     return;
52819b8e9a0SWilly Tu                 }
52919b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
53019b8e9a0SWilly Tu             }
5313fe4d5ccSJohn Edward Broadbent             else if (propertyName == "PredictedMediaLifeLeftPercent")
5323fe4d5ccSJohn Edward Broadbent             {
5333fe4d5ccSJohn Edward Broadbent                 const uint8_t* lifeLeft =
5343fe4d5ccSJohn Edward Broadbent                     std::get_if<uint8_t>(&property.second);
5353fe4d5ccSJohn Edward Broadbent                 if (lifeLeft == nullptr)
5363fe4d5ccSJohn Edward Broadbent                 {
53762598e31SEd Tanous                     BMCWEB_LOG_ERROR(
53862598e31SEd Tanous                         "Illegal property: PredictedMediaLifeLeftPercent");
5393fe4d5ccSJohn Edward Broadbent                     messages::internalError(asyncResp->res);
5403fe4d5ccSJohn Edward Broadbent                     return;
5413fe4d5ccSJohn Edward Broadbent                 }
5423fe4d5ccSJohn Edward Broadbent                 // 255 means reading the value is not supported
5433fe4d5ccSJohn Edward Broadbent                 if (*lifeLeft != 255)
5443fe4d5ccSJohn Edward Broadbent                 {
5453fe4d5ccSJohn Edward Broadbent                     asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
5463fe4d5ccSJohn Edward Broadbent                         *lifeLeft;
5473fe4d5ccSJohn Edward Broadbent                 }
5483fe4d5ccSJohn Edward Broadbent             }
549e5029d88SJohn Edward Broadbent             else if (propertyName == "EncryptionStatus")
550e5029d88SJohn Edward Broadbent             {
551e5029d88SJohn Edward Broadbent                 encryptionStatus = std::get_if<std::string>(&property.second);
552e5029d88SJohn Edward Broadbent                 if (encryptionStatus == nullptr)
553e5029d88SJohn Edward Broadbent                 {
55462598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: EncryptionStatus");
555e5029d88SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
556e5029d88SJohn Edward Broadbent                     return;
55719b8e9a0SWilly Tu                 }
558e5029d88SJohn Edward Broadbent             }
559e5029d88SJohn Edward Broadbent             else if (propertyName == "Locked")
560e5029d88SJohn Edward Broadbent             {
561e5029d88SJohn Edward Broadbent                 isLocked = std::get_if<bool>(&property.second);
562e5029d88SJohn Edward Broadbent                 if (isLocked == nullptr)
563e5029d88SJohn Edward Broadbent                 {
56462598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: Locked");
565e5029d88SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
566e5029d88SJohn Edward Broadbent                     return;
567e5029d88SJohn Edward Broadbent                 }
568e5029d88SJohn Edward Broadbent             }
569e5029d88SJohn Edward Broadbent         }
570e5029d88SJohn Edward Broadbent 
571e5029d88SJohn Edward Broadbent         if (encryptionStatus == nullptr || isLocked == nullptr ||
572e5029d88SJohn Edward Broadbent             *encryptionStatus ==
573a684c226SKonda Reddy Kachana                 "xyz.openbmc_project.Inventory.Item.Drive.DriveEncryptionState.Unknown")
574e5029d88SJohn Edward Broadbent         {
575e5029d88SJohn Edward Broadbent             return;
576e5029d88SJohn Edward Broadbent         }
577e5029d88SJohn Edward Broadbent         if (*encryptionStatus !=
578a684c226SKonda Reddy Kachana             "xyz.openbmc_project.Inventory.Item.Drive.DriveEncryptionState.Encrypted")
579e5029d88SJohn Edward Broadbent         {
580e5029d88SJohn Edward Broadbent             //"The drive is not currently encrypted."
581e5029d88SJohn Edward Broadbent             asyncResp->res.jsonValue["EncryptionStatus"] =
582e5029d88SJohn Edward Broadbent                 drive::EncryptionStatus::Unencrypted;
583e5029d88SJohn Edward Broadbent             return;
584e5029d88SJohn Edward Broadbent         }
585e5029d88SJohn Edward Broadbent         if (*isLocked)
586e5029d88SJohn Edward Broadbent         {
587e5029d88SJohn Edward Broadbent             //"The drive is currently encrypted and the data is not
588e5029d88SJohn Edward Broadbent             // accessible to the user."
589e5029d88SJohn Edward Broadbent             asyncResp->res.jsonValue["EncryptionStatus"] =
590e5029d88SJohn Edward Broadbent                 drive::EncryptionStatus::Locked;
591e5029d88SJohn Edward Broadbent             return;
592e5029d88SJohn Edward Broadbent         }
593e5029d88SJohn Edward Broadbent         // if not locked
594e5029d88SJohn Edward Broadbent         // "The drive is currently encrypted but the data is accessible
595e5029d88SJohn Edward Broadbent         // to the user in unencrypted form."
596e5029d88SJohn Edward Broadbent         asyncResp->res.jsonValue["EncryptionStatus"] =
597e5029d88SJohn Edward Broadbent             drive::EncryptionStatus::Unlocked;
59819b8e9a0SWilly Tu     });
59919b8e9a0SWilly Tu }
60019b8e9a0SWilly Tu 
601b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
602b53dcd9dSNan Zhou                             const std::string& connectionName,
603b53dcd9dSNan Zhou                             const std::string& path,
604e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
605e56ed6b9SJohn Edward Broadbent {
606e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
607e56ed6b9SJohn Edward Broadbent     {
608e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
609e56ed6b9SJohn Edward Broadbent         {
610e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
611e56ed6b9SJohn Edward Broadbent         }
612e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
613e56ed6b9SJohn Edward Broadbent         {
614e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
615e56ed6b9SJohn Edward Broadbent         }
616e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
617e56ed6b9SJohn Edward Broadbent         {
618e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
619e56ed6b9SJohn Edward Broadbent         }
620e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
621e56ed6b9SJohn Edward Broadbent         {
622e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
623e56ed6b9SJohn Edward Broadbent         }
624e56ed6b9SJohn Edward Broadbent     }
625e56ed6b9SJohn Edward Broadbent }
626e56ed6b9SJohn Edward Broadbent 
62736d52334SEd Tanous inline void afterGetSubtreeSystemsStorageDrive(
62845ca1b86SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
62936d52334SEd Tanous     const std::string& driveId, const boost::system::error_code& ec,
63036d52334SEd Tanous     const dbus::utility::MapperGetSubTreeResponse& subtree)
63145ca1b86SEd Tanous {
6327e860f15SJohn Edward Broadbent     if (ec)
6337e860f15SJohn Edward Broadbent     {
63462598e31SEd Tanous         BMCWEB_LOG_ERROR("Drive mapper call error");
6357e860f15SJohn Edward Broadbent         messages::internalError(asyncResp->res);
6367e860f15SJohn Edward Broadbent         return;
6377e860f15SJohn Edward Broadbent     }
6387e860f15SJohn Edward Broadbent 
6393544d2a7SEd Tanous     auto drive = std::ranges::find_if(
6403544d2a7SEd Tanous         subtree,
64136d52334SEd Tanous         [&driveId](const std::pair<std::string,
6428cb65f8aSNan Zhou                                    dbus::utility::MapperServiceMap>& object) {
64336d52334SEd Tanous         return sdbusplus::message::object_path(object.first).filename() ==
64436d52334SEd Tanous                driveId;
6457e860f15SJohn Edward Broadbent     });
6467e860f15SJohn Edward Broadbent 
64703913171SWilly Tu     if (drive == subtree.end())
6487e860f15SJohn Edward Broadbent     {
649002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "Drive", driveId);
6507e860f15SJohn Edward Broadbent         return;
6517e860f15SJohn Edward Broadbent     }
6527e860f15SJohn Edward Broadbent 
65303913171SWilly Tu     const std::string& path = drive->first;
65436d52334SEd Tanous     const dbus::utility::MapperServiceMap& connectionNames = drive->second;
6557e860f15SJohn Edward Broadbent 
656002d39b4SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
657ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
658ef4c65b7SEd Tanous         "/redfish/v1/Systems/system/Storage/1/Drives/{}", driveId);
6597e860f15SJohn Edward Broadbent     asyncResp->res.jsonValue["Name"] = driveId;
6607e860f15SJohn Edward Broadbent     asyncResp->res.jsonValue["Id"] = driveId;
6617e860f15SJohn Edward Broadbent 
6627e860f15SJohn Edward Broadbent     if (connectionNames.size() != 1)
6637e860f15SJohn Edward Broadbent     {
66462598e31SEd Tanous         BMCWEB_LOG_ERROR("Connection size {}, not equal to 1",
66562598e31SEd Tanous                          connectionNames.size());
6667e860f15SJohn Edward Broadbent         messages::internalError(asyncResp->res);
6677e860f15SJohn Edward Broadbent         return;
6687e860f15SJohn Edward Broadbent     }
6697e860f15SJohn Edward Broadbent 
67036d52334SEd Tanous     getMainChassisId(asyncResp,
671ef4c65b7SEd Tanous                      [](const std::string& chassisId,
6727e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
673002d39b4SEd Tanous         aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
674ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
6757e860f15SJohn Edward Broadbent     });
6767e860f15SJohn Edward Broadbent 
677a25aeccfSNikhil Potade     // default it to Enabled
678a25aeccfSNikhil Potade     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
679a25aeccfSNikhil Potade 
680e56ed6b9SJohn Edward Broadbent     addAllDriveInfo(asyncResp, connectionNames[0].first, path,
681e56ed6b9SJohn Edward Broadbent                     connectionNames[0].second);
682a25aeccfSNikhil Potade }
68392903bd4SJohn Edward Broadbent 
68436d52334SEd Tanous inline void handleSystemsStorageDriveGet(
68536d52334SEd Tanous     App& app, const crow::Request& req,
68692903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
68736d52334SEd Tanous     const std::string& systemName, const std::string& driveId)
68892903bd4SJohn Edward Broadbent {
6893ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
69092903bd4SJohn Edward Broadbent     {
69192903bd4SJohn Edward Broadbent         return;
69292903bd4SJohn Edward Broadbent     }
693*25b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
6947f3e84a1SEd Tanous     {
6957f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
6967f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
6977f3e84a1SEd Tanous                                    systemName);
6987f3e84a1SEd Tanous         return;
6997f3e84a1SEd Tanous     }
7007f3e84a1SEd Tanous 
70136d52334SEd Tanous     if (systemName != "system")
70236d52334SEd Tanous     {
70336d52334SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
70436d52334SEd Tanous                                    systemName);
70536d52334SEd Tanous         return;
70636d52334SEd Tanous     }
70792903bd4SJohn Edward Broadbent 
70836d52334SEd Tanous     constexpr std::array<std::string_view, 1> interfaces = {
70936d52334SEd Tanous         "xyz.openbmc_project.Inventory.Item.Drive"};
710e99073f5SGeorge Liu     dbus::utility::getSubTree(
711e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
71236d52334SEd Tanous         std::bind_front(afterGetSubtreeSystemsStorageDrive, asyncResp,
71336d52334SEd Tanous                         driveId));
71436d52334SEd Tanous }
71536d52334SEd Tanous 
71636d52334SEd Tanous inline void requestRoutesDrive(App& app)
71736d52334SEd Tanous {
71836d52334SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
71936d52334SEd Tanous         .privileges(redfish::privileges::getDrive)
72036d52334SEd Tanous         .methods(boost::beast::http::verb::get)(
72136d52334SEd Tanous             std::bind_front(handleSystemsStorageDriveGet, std::ref(app)));
72236d52334SEd Tanous }
72336d52334SEd Tanous 
72436d52334SEd Tanous inline void afterChassisDriveCollectionSubtreeGet(
72536d52334SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
72636d52334SEd Tanous     const std::string& chassisId, const boost::system::error_code& ec,
72736d52334SEd Tanous     const dbus::utility::MapperGetSubTreeResponse& subtree)
72836d52334SEd Tanous {
72992903bd4SJohn Edward Broadbent     if (ec)
73092903bd4SJohn Edward Broadbent     {
73192903bd4SJohn Edward Broadbent         if (ec == boost::system::errc::host_unreachable)
73292903bd4SJohn Edward Broadbent         {
73336d52334SEd Tanous             messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
73492903bd4SJohn Edward Broadbent             return;
73592903bd4SJohn Edward Broadbent         }
73692903bd4SJohn Edward Broadbent         messages::internalError(asyncResp->res);
73792903bd4SJohn Edward Broadbent         return;
73892903bd4SJohn Edward Broadbent     }
73992903bd4SJohn Edward Broadbent 
74092903bd4SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
7418cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
74292903bd4SJohn Edward Broadbent     {
74392903bd4SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
74492903bd4SJohn Edward Broadbent         if (objPath.filename() != chassisId)
74592903bd4SJohn Edward Broadbent         {
74692903bd4SJohn Edward Broadbent             continue;
74792903bd4SJohn Edward Broadbent         }
74892903bd4SJohn Edward Broadbent 
74992903bd4SJohn Edward Broadbent         if (connectionNames.empty())
75092903bd4SJohn Edward Broadbent         {
75162598e31SEd Tanous             BMCWEB_LOG_ERROR("Got 0 Connection names");
75292903bd4SJohn Edward Broadbent             continue;
75392903bd4SJohn Edward Broadbent         }
75492903bd4SJohn Edward Broadbent 
75592903bd4SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] =
75692903bd4SJohn Edward Broadbent             "#DriveCollection.DriveCollection";
75792903bd4SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] =
758ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/Chassis/{}/Drives", chassisId);
75992903bd4SJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = "Drive Collection";
76092903bd4SJohn Edward Broadbent 
76192903bd4SJohn Edward Broadbent         // Association lambda
7626c3e9451SGeorge Liu         dbus::utility::getAssociationEndPoints(
7636c3e9451SGeorge Liu             path + "/drive",
76436d52334SEd Tanous             [asyncResp, chassisId](const boost::system::error_code& ec3,
7656c3e9451SGeorge Liu                                    const dbus::utility::MapperEndPoints& resp) {
76692903bd4SJohn Edward Broadbent             if (ec3)
76792903bd4SJohn Edward Broadbent             {
76862598e31SEd Tanous                 BMCWEB_LOG_ERROR("Error in chassis Drive association ");
76992903bd4SJohn Edward Broadbent             }
77092903bd4SJohn Edward Broadbent             nlohmann::json& members = asyncResp->res.jsonValue["Members"];
77192903bd4SJohn Edward Broadbent             // important if array is empty
77292903bd4SJohn Edward Broadbent             members = nlohmann::json::array();
77392903bd4SJohn Edward Broadbent 
77492903bd4SJohn Edward Broadbent             std::vector<std::string> leafNames;
77592903bd4SJohn Edward Broadbent             for (const auto& drive : resp)
77692903bd4SJohn Edward Broadbent             {
7778a592810SEd Tanous                 sdbusplus::message::object_path drivePath(drive);
7788a592810SEd Tanous                 leafNames.push_back(drivePath.filename());
77992903bd4SJohn Edward Broadbent             }
78092903bd4SJohn Edward Broadbent 
7813544d2a7SEd Tanous             std::ranges::sort(leafNames, AlphanumLess<std::string>());
78292903bd4SJohn Edward Broadbent 
78392903bd4SJohn Edward Broadbent             for (const auto& leafName : leafNames)
78492903bd4SJohn Edward Broadbent             {
78592903bd4SJohn Edward Broadbent                 nlohmann::json::object_t member;
78636d52334SEd Tanous                 member["@odata.id"] = boost::urls::format(
78736d52334SEd Tanous                     "/redfish/v1/Chassis/{}/Drives/{}", chassisId, leafName);
788b2ba3072SPatrick Williams                 members.emplace_back(std::move(member));
78992903bd4SJohn Edward Broadbent                 // navigation links will be registered in next patch set
79092903bd4SJohn Edward Broadbent             }
79192903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
79292903bd4SJohn Edward Broadbent         }); // end association lambda
79392903bd4SJohn Edward Broadbent 
79492903bd4SJohn Edward Broadbent     }       // end Iterate over all retrieved ObjectPaths
79536d52334SEd Tanous }
79636d52334SEd Tanous /**
79736d52334SEd Tanous  * Chassis drives, this URL will show all the DriveCollection
79836d52334SEd Tanous  * information
79936d52334SEd Tanous  */
80036d52334SEd Tanous inline void chassisDriveCollectionGet(
80136d52334SEd Tanous     crow::App& app, const crow::Request& req,
80236d52334SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
80336d52334SEd Tanous     const std::string& chassisId)
80436d52334SEd Tanous {
80536d52334SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
80636d52334SEd Tanous     {
80736d52334SEd Tanous         return;
80836d52334SEd Tanous     }
80936d52334SEd Tanous 
81036d52334SEd Tanous     // mapper call lambda
81136d52334SEd Tanous     constexpr std::array<std::string_view, 2> interfaces = {
81236d52334SEd Tanous         "xyz.openbmc_project.Inventory.Item.Board",
81336d52334SEd Tanous         "xyz.openbmc_project.Inventory.Item.Chassis"};
81436d52334SEd Tanous     dbus::utility::getSubTree(
81536d52334SEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces,
81636d52334SEd Tanous         std::bind_front(afterChassisDriveCollectionSubtreeGet, asyncResp,
81736d52334SEd Tanous                         chassisId));
81892903bd4SJohn Edward Broadbent }
81992903bd4SJohn Edward Broadbent 
82092903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
82192903bd4SJohn Edward Broadbent {
82292903bd4SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
82392903bd4SJohn Edward Broadbent         .privileges(redfish::privileges::getDriveCollection)
82492903bd4SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
82592903bd4SJohn Edward Broadbent             std::bind_front(chassisDriveCollectionGet, std::ref(app)));
82692903bd4SJohn Edward Broadbent }
82792903bd4SJohn Edward Broadbent 
828b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
829b53dcd9dSNan Zhou                        const std::string& chassisId,
830b53dcd9dSNan Zhou                        const std::string& driveName,
8315e7e2dc5SEd Tanous                        const boost::system::error_code& ec,
832e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
833e56ed6b9SJohn Edward Broadbent {
834e56ed6b9SJohn Edward Broadbent     if (ec)
835e56ed6b9SJohn Edward Broadbent     {
83662598e31SEd Tanous         BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
837e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
838e56ed6b9SJohn Edward Broadbent         return;
839e56ed6b9SJohn Edward Broadbent     }
840e56ed6b9SJohn Edward Broadbent 
841e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
8428cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
843e56ed6b9SJohn Edward Broadbent     {
844e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
845e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
846e56ed6b9SJohn Edward Broadbent         {
847e56ed6b9SJohn Edward Broadbent             continue;
848e56ed6b9SJohn Edward Broadbent         }
849e56ed6b9SJohn Edward Broadbent 
850e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
851e56ed6b9SJohn Edward Broadbent         {
85262598e31SEd Tanous             BMCWEB_LOG_ERROR("Got 0 Connection names");
853e56ed6b9SJohn Edward Broadbent             continue;
854e56ed6b9SJohn Edward Broadbent         }
855e56ed6b9SJohn Edward Broadbent 
856ef4c65b7SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
857ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/Drives/{}", chassisId, driveName);
858e56ed6b9SJohn Edward Broadbent 
859e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
860a0cb40cbSJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = driveName;
861e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
862e56ed6b9SJohn Edward Broadbent         // default it to Enabled
863e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
864e56ed6b9SJohn Edward Broadbent 
865e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
866e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
867ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
868e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
869e56ed6b9SJohn Edward Broadbent 
870e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
871e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
872e56ed6b9SJohn Edward Broadbent     }
873e56ed6b9SJohn Edward Broadbent }
874e56ed6b9SJohn Edward Broadbent 
875b53dcd9dSNan Zhou inline void
876b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
877e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
878e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
879e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
880e56ed6b9SJohn Edward Broadbent {
881e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
882e56ed6b9SJohn Edward Broadbent     {
883e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
884e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
885e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
886e56ed6b9SJohn Edward Broadbent         {
887e56ed6b9SJohn Edward Broadbent             continue;
888e56ed6b9SJohn Edward Broadbent         }
889e56ed6b9SJohn Edward Broadbent         //  mapper call drive
890e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> driveInterface = {
891e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
892e99073f5SGeorge Liu         dbus::utility::getSubTree(
893e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, driveInterface,
894e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
895e99073f5SGeorge Liu                 const boost::system::error_code& ec,
896e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
897e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
898e99073f5SGeorge Liu         });
899e56ed6b9SJohn Edward Broadbent     }
900e56ed6b9SJohn Edward Broadbent }
901e56ed6b9SJohn Edward Broadbent 
902b53dcd9dSNan Zhou inline void
903b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
904e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
905e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
906e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
907e56ed6b9SJohn Edward Broadbent {
90803810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
909e56ed6b9SJohn Edward Broadbent     {
910e56ed6b9SJohn Edward Broadbent         return;
911e56ed6b9SJohn Edward Broadbent     }
912e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
913e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
914e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
915e56ed6b9SJohn Edward Broadbent 
916e56ed6b9SJohn Edward Broadbent     // mapper call chassis
917e99073f5SGeorge Liu     dbus::utility::getSubTree(
918e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
919e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
920e99073f5SGeorge Liu          driveName](const boost::system::error_code& ec,
921e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
922e56ed6b9SJohn Edward Broadbent         if (ec)
923e56ed6b9SJohn Edward Broadbent         {
924e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
925e56ed6b9SJohn Edward Broadbent             return;
926e56ed6b9SJohn Edward Broadbent         }
927e56ed6b9SJohn Edward Broadbent 
928e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
9298cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
930e56ed6b9SJohn Edward Broadbent         {
931e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
932e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
933e56ed6b9SJohn Edward Broadbent             {
934e56ed6b9SJohn Edward Broadbent                 continue;
935e56ed6b9SJohn Edward Broadbent             }
936e56ed6b9SJohn Edward Broadbent 
937e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
938e56ed6b9SJohn Edward Broadbent             {
93962598e31SEd Tanous                 BMCWEB_LOG_ERROR("Got 0 Connection names");
940e56ed6b9SJohn Edward Broadbent                 continue;
941e56ed6b9SJohn Edward Broadbent             }
942e56ed6b9SJohn Edward Broadbent 
9436c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
9446c3e9451SGeorge Liu                 path + "/drive",
945e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
9465e7e2dc5SEd Tanous                  driveName](const boost::system::error_code& ec3,
9476c3e9451SGeorge Liu                             const dbus::utility::MapperEndPoints& resp) {
948e56ed6b9SJohn Edward Broadbent                 if (ec3)
949e56ed6b9SJohn Edward Broadbent                 {
950e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
951e56ed6b9SJohn Edward Broadbent                 }
952e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
953e56ed6b9SJohn Edward Broadbent             });
954e56ed6b9SJohn Edward Broadbent             break;
955e56ed6b9SJohn Edward Broadbent         }
956e99073f5SGeorge Liu     });
957e56ed6b9SJohn Edward Broadbent }
958e56ed6b9SJohn Edward Broadbent 
959e56ed6b9SJohn Edward Broadbent /**
960e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
961e56ed6b9SJohn Edward Broadbent  */
962e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
963e56ed6b9SJohn Edward Broadbent {
964e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
965e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
966e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
967e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
968e56ed6b9SJohn Edward Broadbent }
969e56ed6b9SJohn Edward Broadbent 
97061b1eb21SWilly Tu inline void getStorageControllerAsset(
97161b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
97261b1eb21SWilly Tu     const boost::system::error_code& ec,
97361b1eb21SWilly Tu     const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
97461b1eb21SWilly Tu         propertiesList)
97561b1eb21SWilly Tu {
97661b1eb21SWilly Tu     if (ec)
97761b1eb21SWilly Tu     {
97861b1eb21SWilly Tu         // this interface isn't necessary
97962598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to get StorageControllerAsset");
98061b1eb21SWilly Tu         return;
98161b1eb21SWilly Tu     }
98261b1eb21SWilly Tu 
98361b1eb21SWilly Tu     const std::string* partNumber = nullptr;
98461b1eb21SWilly Tu     const std::string* serialNumber = nullptr;
98561b1eb21SWilly Tu     const std::string* manufacturer = nullptr;
98661b1eb21SWilly Tu     const std::string* model = nullptr;
98761b1eb21SWilly Tu     if (!sdbusplus::unpackPropertiesNoThrow(
98861b1eb21SWilly Tu             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
98961b1eb21SWilly Tu             partNumber, "SerialNumber", serialNumber, "Manufacturer",
99061b1eb21SWilly Tu             manufacturer, "Model", model))
99161b1eb21SWilly Tu     {
99261b1eb21SWilly Tu         messages::internalError(asyncResp->res);
99361b1eb21SWilly Tu         return;
99461b1eb21SWilly Tu     }
99561b1eb21SWilly Tu 
99661b1eb21SWilly Tu     if (partNumber != nullptr)
99761b1eb21SWilly Tu     {
99861b1eb21SWilly Tu         asyncResp->res.jsonValue["PartNumber"] = *partNumber;
99961b1eb21SWilly Tu     }
100061b1eb21SWilly Tu 
100161b1eb21SWilly Tu     if (serialNumber != nullptr)
100261b1eb21SWilly Tu     {
100361b1eb21SWilly Tu         asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
100461b1eb21SWilly Tu     }
100561b1eb21SWilly Tu 
100661b1eb21SWilly Tu     if (manufacturer != nullptr)
100761b1eb21SWilly Tu     {
100861b1eb21SWilly Tu         asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
100961b1eb21SWilly Tu     }
101061b1eb21SWilly Tu 
101161b1eb21SWilly Tu     if (model != nullptr)
101261b1eb21SWilly Tu     {
101361b1eb21SWilly Tu         asyncResp->res.jsonValue["Model"] = *model;
101461b1eb21SWilly Tu     }
101561b1eb21SWilly Tu }
101661b1eb21SWilly Tu 
101761b1eb21SWilly Tu inline void populateStorageController(
101861b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
101961b1eb21SWilly Tu     const std::string& controllerId, const std::string& connectionName,
102061b1eb21SWilly Tu     const std::string& path)
102161b1eb21SWilly Tu {
102261b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.type"] =
102361b1eb21SWilly Tu         "#StorageController.v1_6_0.StorageController";
102461b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
102561b1eb21SWilly Tu         "/redfish/v1/Systems/system/Storage/1/Controllers/{}", controllerId);
102661b1eb21SWilly Tu     asyncResp->res.jsonValue["Name"] = controllerId;
102761b1eb21SWilly Tu     asyncResp->res.jsonValue["Id"] = controllerId;
102861b1eb21SWilly Tu     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
102961b1eb21SWilly Tu 
103061b1eb21SWilly Tu     sdbusplus::asio::getProperty<bool>(
103161b1eb21SWilly Tu         *crow::connections::systemBus, connectionName, path,
103261b1eb21SWilly Tu         "xyz.openbmc_project.Inventory.Item", "Present",
103361b1eb21SWilly Tu         [asyncResp](const boost::system::error_code& ec, bool isPresent) {
103461b1eb21SWilly Tu         // this interface isn't necessary, only check it
103561b1eb21SWilly Tu         // if we get a good return
103661b1eb21SWilly Tu         if (ec)
103761b1eb21SWilly Tu         {
103862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Failed to get Present property");
103961b1eb21SWilly Tu             return;
104061b1eb21SWilly Tu         }
104161b1eb21SWilly Tu         if (!isPresent)
104261b1eb21SWilly Tu         {
104361b1eb21SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Absent";
104461b1eb21SWilly Tu         }
104561b1eb21SWilly Tu     });
104661b1eb21SWilly Tu 
104761b1eb21SWilly Tu     sdbusplus::asio::getAllProperties(
104861b1eb21SWilly Tu         *crow::connections::systemBus, connectionName, path,
104961b1eb21SWilly Tu         "xyz.openbmc_project.Inventory.Decorator.Asset",
105061b1eb21SWilly Tu         [asyncResp](const boost::system::error_code& ec,
105161b1eb21SWilly Tu                     const std::vector<
105261b1eb21SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
105361b1eb21SWilly Tu                         propertiesList) {
105461b1eb21SWilly Tu         getStorageControllerAsset(asyncResp, ec, propertiesList);
105561b1eb21SWilly Tu     });
105661b1eb21SWilly Tu }
105761b1eb21SWilly Tu 
105861b1eb21SWilly Tu inline void getStorageControllerHandler(
105961b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
106061b1eb21SWilly Tu     const std::string& controllerId, const boost::system::error_code& ec,
106161b1eb21SWilly Tu     const dbus::utility::MapperGetSubTreeResponse& subtree)
106261b1eb21SWilly Tu {
106361b1eb21SWilly Tu     if (ec || subtree.empty())
106461b1eb21SWilly Tu     {
106561b1eb21SWilly Tu         // doesn't have to be there
106662598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to handle StorageController");
106761b1eb21SWilly Tu         return;
106861b1eb21SWilly Tu     }
106961b1eb21SWilly Tu 
107061b1eb21SWilly Tu     for (const auto& [path, interfaceDict] : subtree)
107161b1eb21SWilly Tu     {
107261b1eb21SWilly Tu         sdbusplus::message::object_path object(path);
107361b1eb21SWilly Tu         std::string id = object.filename();
107461b1eb21SWilly Tu         if (id.empty())
107561b1eb21SWilly Tu         {
107662598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
107761b1eb21SWilly Tu             return;
107861b1eb21SWilly Tu         }
107961b1eb21SWilly Tu         if (id != controllerId)
108061b1eb21SWilly Tu         {
108161b1eb21SWilly Tu             continue;
108261b1eb21SWilly Tu         }
108361b1eb21SWilly Tu 
108461b1eb21SWilly Tu         if (interfaceDict.size() != 1)
108561b1eb21SWilly Tu         {
108662598e31SEd Tanous             BMCWEB_LOG_ERROR("Connection size {}, greater than 1",
108762598e31SEd Tanous                              interfaceDict.size());
108861b1eb21SWilly Tu             messages::internalError(asyncResp->res);
108961b1eb21SWilly Tu             return;
109061b1eb21SWilly Tu         }
109161b1eb21SWilly Tu 
109261b1eb21SWilly Tu         const std::string& connectionName = interfaceDict.front().first;
109361b1eb21SWilly Tu         populateStorageController(asyncResp, controllerId, connectionName,
109461b1eb21SWilly Tu                                   path);
109561b1eb21SWilly Tu     }
109661b1eb21SWilly Tu }
109761b1eb21SWilly Tu 
109861b1eb21SWilly Tu inline void populateStorageControllerCollection(
109961b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
110061b1eb21SWilly Tu     const boost::system::error_code& ec,
110161b1eb21SWilly Tu     const dbus::utility::MapperGetSubTreePathsResponse& controllerList)
110261b1eb21SWilly Tu {
110361b1eb21SWilly Tu     nlohmann::json::array_t members;
110461b1eb21SWilly Tu     if (ec || controllerList.empty())
110561b1eb21SWilly Tu     {
110661b1eb21SWilly Tu         asyncResp->res.jsonValue["Members"] = std::move(members);
110761b1eb21SWilly Tu         asyncResp->res.jsonValue["Members@odata.count"] = 0;
110862598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to find any StorageController");
110961b1eb21SWilly Tu         return;
111061b1eb21SWilly Tu     }
111161b1eb21SWilly Tu 
111261b1eb21SWilly Tu     for (const std::string& path : controllerList)
111361b1eb21SWilly Tu     {
111461b1eb21SWilly Tu         std::string id = sdbusplus::message::object_path(path).filename();
111561b1eb21SWilly Tu         if (id.empty())
111661b1eb21SWilly Tu         {
111762598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
111861b1eb21SWilly Tu             return;
111961b1eb21SWilly Tu         }
112061b1eb21SWilly Tu         nlohmann::json::object_t member;
112161b1eb21SWilly Tu         member["@odata.id"] = boost::urls::format(
112261b1eb21SWilly Tu             "/redfish/v1/Systems/system/Storage/1/Controllers/{}", id);
112361b1eb21SWilly Tu         members.emplace_back(member);
112461b1eb21SWilly Tu     }
112561b1eb21SWilly Tu     asyncResp->res.jsonValue["Members@odata.count"] = members.size();
112661b1eb21SWilly Tu     asyncResp->res.jsonValue["Members"] = std::move(members);
112761b1eb21SWilly Tu }
112861b1eb21SWilly Tu 
112936d52334SEd Tanous inline void handleSystemsStorageControllerCollectionGet(
113061b1eb21SWilly Tu     App& app, const crow::Request& req,
113161b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
113261b1eb21SWilly Tu     const std::string& systemName)
113361b1eb21SWilly Tu {
113461b1eb21SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
113561b1eb21SWilly Tu     {
113662598e31SEd Tanous         BMCWEB_LOG_DEBUG(
113762598e31SEd Tanous             "Failed to setup Redfish Route for StorageController Collection");
113861b1eb21SWilly Tu         return;
113961b1eb21SWilly Tu     }
114061b1eb21SWilly Tu     if (systemName != "system")
114161b1eb21SWilly Tu     {
114261b1eb21SWilly Tu         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
114361b1eb21SWilly Tu                                    systemName);
114462598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
114561b1eb21SWilly Tu         return;
114661b1eb21SWilly Tu     }
114761b1eb21SWilly Tu 
114861b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.type"] =
114961b1eb21SWilly Tu         "#StorageControllerCollection.StorageControllerCollection";
115061b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.id"] =
115161b1eb21SWilly Tu         "/redfish/v1/Systems/system/Storage/1/Controllers";
115261b1eb21SWilly Tu     asyncResp->res.jsonValue["Name"] = "Storage Controller Collection";
115361b1eb21SWilly Tu 
115461b1eb21SWilly Tu     constexpr std::array<std::string_view, 1> interfaces = {
115561b1eb21SWilly Tu         "xyz.openbmc_project.Inventory.Item.StorageController"};
115661b1eb21SWilly Tu     dbus::utility::getSubTreePaths(
115761b1eb21SWilly Tu         "/xyz/openbmc_project/inventory", 0, interfaces,
115861b1eb21SWilly Tu         [asyncResp](const boost::system::error_code& ec,
115961b1eb21SWilly Tu                     const dbus::utility::MapperGetSubTreePathsResponse&
116061b1eb21SWilly Tu                         controllerList) {
116161b1eb21SWilly Tu         populateStorageControllerCollection(asyncResp, ec, controllerList);
116261b1eb21SWilly Tu     });
116361b1eb21SWilly Tu }
116461b1eb21SWilly Tu 
116536d52334SEd Tanous inline void handleSystemsStorageControllerGet(
116661b1eb21SWilly Tu     App& app, const crow::Request& req,
116761b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
116861b1eb21SWilly Tu     const std::string& systemName, const std::string& controllerId)
116961b1eb21SWilly Tu {
117061b1eb21SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
117161b1eb21SWilly Tu     {
117262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to setup Redfish Route for StorageController");
117361b1eb21SWilly Tu         return;
117461b1eb21SWilly Tu     }
117561b1eb21SWilly Tu     if (systemName != "system")
117661b1eb21SWilly Tu     {
117761b1eb21SWilly Tu         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
117861b1eb21SWilly Tu                                    systemName);
117962598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
118061b1eb21SWilly Tu         return;
118161b1eb21SWilly Tu     }
118261b1eb21SWilly Tu     constexpr std::array<std::string_view, 1> interfaces = {
118361b1eb21SWilly Tu         "xyz.openbmc_project.Inventory.Item.StorageController"};
118461b1eb21SWilly Tu     dbus::utility::getSubTree(
118561b1eb21SWilly Tu         "/xyz/openbmc_project/inventory", 0, interfaces,
118661b1eb21SWilly Tu         [asyncResp,
118761b1eb21SWilly Tu          controllerId](const boost::system::error_code& ec,
118861b1eb21SWilly Tu                        const dbus::utility::MapperGetSubTreeResponse& subtree) {
118961b1eb21SWilly Tu         getStorageControllerHandler(asyncResp, controllerId, ec, subtree);
119061b1eb21SWilly Tu     });
119161b1eb21SWilly Tu }
119261b1eb21SWilly Tu 
119361b1eb21SWilly Tu inline void requestRoutesStorageControllerCollection(App& app)
119461b1eb21SWilly Tu {
119561b1eb21SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/")
119661b1eb21SWilly Tu         .privileges(redfish::privileges::getStorageControllerCollection)
119736d52334SEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
119836d52334SEd Tanous             handleSystemsStorageControllerCollectionGet, std::ref(app)));
119961b1eb21SWilly Tu }
120061b1eb21SWilly Tu 
120161b1eb21SWilly Tu inline void requestRoutesStorageController(App& app)
120261b1eb21SWilly Tu {
120361b1eb21SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/<str>")
120461b1eb21SWilly Tu         .privileges(redfish::privileges::getStorageController)
120561b1eb21SWilly Tu         .methods(boost::beast::http::verb::get)(
120636d52334SEd Tanous             std::bind_front(handleSystemsStorageControllerGet, std::ref(app)));
120761b1eb21SWilly Tu }
120861b1eb21SWilly Tu 
1209a25aeccfSNikhil Potade } // namespace redfish
1210