xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision 36b5f1ed97fe5806c53e0eb5b2f9a07e9a8acd5f)
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"
242ad9c2f6SJames Feist #include "health.hpp"
25a8e884fcSEd Tanous #include "human_sort.hpp"
26e284a7c1SJames Feist #include "openbmc_dbus_rest.hpp"
273ccb3adbSEd Tanous #include "query.hpp"
28a8e884fcSEd Tanous #include "redfish_util.hpp"
293ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
305e577bc1SWilly Tu #include "utils/collection.hpp"
313ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
322ad9c2f6SJames Feist 
33e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
34ef4c65b7SEd Tanous #include <boost/url/format.hpp>
351e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
36d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
37a25aeccfSNikhil Potade 
387a1dbc48SGeorge Liu #include <array>
393544d2a7SEd Tanous #include <ranges>
407a1dbc48SGeorge Liu #include <string_view>
417a1dbc48SGeorge Liu 
42a25aeccfSNikhil Potade namespace redfish
43a25aeccfSNikhil Potade {
4436d52334SEd Tanous 
4536d52334SEd Tanous inline void handleSystemsStorageCollectionGet(
4636d52334SEd Tanous     App& app, const crow::Request& req,
4722d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4836d52334SEd Tanous     const std::string& systemName)
4936d52334SEd Tanous {
503ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
5145ca1b86SEd Tanous     {
5245ca1b86SEd Tanous         return;
5345ca1b86SEd Tanous     }
5422d268cbSEd Tanous     if (systemName != "system")
5522d268cbSEd Tanous     {
5622d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
5722d268cbSEd Tanous                                    systemName);
5822d268cbSEd Tanous         return;
5922d268cbSEd Tanous     }
6022d268cbSEd Tanous 
618d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
628d1b46d7Szhanghch05         "#StorageCollection.StorageCollection";
638d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.id"] =
648d1b46d7Szhanghch05         "/redfish/v1/Systems/system/Storage";
658d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Storage Collection";
665e577bc1SWilly Tu 
675e577bc1SWilly Tu     constexpr std::array<std::string_view, 1> interface {
685e577bc1SWilly Tu         "xyz.openbmc_project.Inventory.Item.Storage"
695e577bc1SWilly Tu     };
705e577bc1SWilly Tu     collection_util::getCollectionMembers(
715e577bc1SWilly Tu         asyncResp, boost::urls::format("/redfish/v1/Systems/system/Storage"),
72*36b5f1edSLakshmi 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 {
885e577bc1SWilly Tu         "xyz.openbmc_project.Inventory.Item.Storage"
895e577bc1SWilly Tu     };
905e577bc1SWilly Tu     collection_util::getCollectionMembers(
91*36b5f1edSLakshmi Yadlapati         asyncResp, boost::urls::format("/redfish/v1/Storage"), interface,
92*36b5f1edSLakshmi Yadlapati         "/xyz/openbmc_project/inventory");
93a25aeccfSNikhil Potade }
94a25aeccfSNikhil Potade 
9536d52334SEd Tanous inline void requestRoutesStorageCollection(App& app)
96a25aeccfSNikhil Potade {
9736d52334SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
9836d52334SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
9936d52334SEd Tanous         .methods(boost::beast::http::verb::get)(
10036d52334SEd Tanous             std::bind_front(handleSystemsStorageCollectionGet, std::ref(app)));
1015e577bc1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Storage/")
1025e577bc1SWilly Tu         .privileges(redfish::privileges::getStorageCollection)
1035e577bc1SWilly Tu         .methods(boost::beast::http::verb::get)(
1045e577bc1SWilly Tu             std::bind_front(handleStorageCollectionGet, std::ref(app)));
10536d52334SEd Tanous }
10636d52334SEd Tanous 
10736d52334SEd Tanous inline void afterChassisDriveCollectionSubtree(
10836d52334SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10936d52334SEd Tanous     const std::shared_ptr<HealthPopulate>& health,
1107a1dbc48SGeorge Liu     const boost::system::error_code& ec,
11136d52334SEd Tanous     const dbus::utility::MapperGetSubTreePathsResponse& driveList)
11236d52334SEd Tanous {
113a25aeccfSNikhil Potade     if (ec)
114a25aeccfSNikhil Potade     {
11562598e31SEd Tanous         BMCWEB_LOG_ERROR("Drive mapper call error");
116a25aeccfSNikhil Potade         messages::internalError(asyncResp->res);
117a25aeccfSNikhil Potade         return;
118a25aeccfSNikhil Potade     }
1192ad9c2f6SJames Feist 
120a85afbe1SWilly Tu     nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
121a85afbe1SWilly Tu     driveArray = nlohmann::json::array();
122a85afbe1SWilly Tu     auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
123a85afbe1SWilly Tu     count = 0;
1242ad9c2f6SJames Feist 
12513451e39SWilly Tu     if constexpr (bmcwebEnableHealthPopulate)
12613451e39SWilly Tu     {
127a85afbe1SWilly Tu         health->inventory.insert(health->inventory.end(), driveList.begin(),
128a85afbe1SWilly Tu                                  driveList.end());
12913451e39SWilly Tu     }
130a85afbe1SWilly Tu 
131a85afbe1SWilly Tu     for (const std::string& drive : driveList)
132a25aeccfSNikhil Potade     {
133a85afbe1SWilly Tu         sdbusplus::message::object_path object(drive);
134a85afbe1SWilly Tu         if (object.filename().empty())
135a25aeccfSNikhil Potade         {
13662598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to find filename in {}", drive);
137a85afbe1SWilly Tu             return;
138a25aeccfSNikhil Potade         }
139a85afbe1SWilly Tu 
140a85afbe1SWilly Tu         nlohmann::json::object_t driveJson;
141ef4c65b7SEd Tanous         driveJson["@odata.id"] = boost::urls::format(
142ef4c65b7SEd Tanous             "/redfish/v1/Systems/system/Storage/1/Drives/{}",
143eddfc437SWilly Tu             object.filename());
144b2ba3072SPatrick Williams         driveArray.emplace_back(std::move(driveJson));
145a25aeccfSNikhil Potade     }
146a25aeccfSNikhil Potade 
147a85afbe1SWilly Tu     count = driveArray.size();
14836d52334SEd Tanous }
14936d52334SEd Tanous inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15036d52334SEd Tanous                       const std::shared_ptr<HealthPopulate>& health)
15136d52334SEd Tanous {
15236d52334SEd Tanous     const std::array<std::string_view, 1> interfaces = {
15336d52334SEd Tanous         "xyz.openbmc_project.Inventory.Item.Drive"};
15436d52334SEd Tanous     dbus::utility::getSubTreePaths(
15536d52334SEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces,
15636d52334SEd Tanous         std::bind_front(afterChassisDriveCollectionSubtree, asyncResp, health));
157a85afbe1SWilly Tu }
158e284a7c1SJames Feist 
1595e577bc1SWilly Tu inline void afterSystemsStorageGetSubtree(
1605e577bc1SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1615e577bc1SWilly Tu     const std::string& storageId, const boost::system::error_code& ec,
1625e577bc1SWilly Tu     const dbus::utility::MapperGetSubTreeResponse& subtree)
163a85afbe1SWilly Tu {
1645e577bc1SWilly Tu     if (ec)
165a85afbe1SWilly Tu     {
16662598e31SEd Tanous         BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
1675e577bc1SWilly Tu         messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
1685e577bc1SWilly Tu                                    storageId);
169a85afbe1SWilly Tu         return;
170a85afbe1SWilly Tu     }
1713544d2a7SEd Tanous     auto storage = std::ranges::find_if(
1723544d2a7SEd Tanous         subtree,
1735e577bc1SWilly Tu         [&storageId](const std::pair<std::string,
1745e577bc1SWilly Tu                                      dbus::utility::MapperServiceMap>& object) {
1755e577bc1SWilly Tu         return sdbusplus::message::object_path(object.first).filename() ==
1765e577bc1SWilly Tu                storageId;
1775e577bc1SWilly Tu         });
1785e577bc1SWilly Tu     if (storage == subtree.end())
1795e577bc1SWilly Tu     {
1805e577bc1SWilly Tu         messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
1815e577bc1SWilly Tu                                    storageId);
1825e577bc1SWilly Tu         return;
1835e577bc1SWilly Tu     }
1845e577bc1SWilly Tu 
18561b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
186a85afbe1SWilly Tu     asyncResp->res.jsonValue["@odata.id"] =
1875e577bc1SWilly Tu         boost::urls::format("/redfish/v1/Systems/system/Storage/{}", storageId);
188a85afbe1SWilly Tu     asyncResp->res.jsonValue["Name"] = "Storage";
1895e577bc1SWilly Tu     asyncResp->res.jsonValue["Id"] = storageId;
190a85afbe1SWilly Tu     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
191a85afbe1SWilly Tu 
192a85afbe1SWilly Tu     auto health = std::make_shared<HealthPopulate>(asyncResp);
19313451e39SWilly Tu     if constexpr (bmcwebEnableHealthPopulate)
19413451e39SWilly Tu     {
195a85afbe1SWilly Tu         health->populate();
19613451e39SWilly Tu     }
197a85afbe1SWilly Tu 
198a85afbe1SWilly Tu     getDrives(asyncResp, health);
1995e577bc1SWilly Tu     asyncResp->res.jsonValue["Controllers"]["@odata.id"] = boost::urls::format(
2005e577bc1SWilly Tu         "/redfish/v1/Systems/system/Storage/{}/Controllers", storageId);
2015e577bc1SWilly Tu }
2025e577bc1SWilly Tu 
2035e577bc1SWilly Tu inline void
2045e577bc1SWilly Tu     handleSystemsStorageGet(App& app, const crow::Request& req,
2055e577bc1SWilly Tu                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2067f3e84a1SEd Tanous                             const std::string& systemName,
2075e577bc1SWilly Tu                             const std::string& storageId)
2085e577bc1SWilly Tu {
2095e577bc1SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2105e577bc1SWilly Tu     {
2115e577bc1SWilly Tu         return;
2125e577bc1SWilly Tu     }
2137f3e84a1SEd Tanous     if constexpr (bmcwebEnableMultiHost)
2147f3e84a1SEd Tanous     {
2157f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
2167f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2177f3e84a1SEd Tanous                                    systemName);
2187f3e84a1SEd Tanous         return;
2197f3e84a1SEd Tanous     }
2205e577bc1SWilly Tu 
2215e577bc1SWilly Tu     constexpr std::array<std::string_view, 1> interfaces = {
2225e577bc1SWilly Tu         "xyz.openbmc_project.Inventory.Item.Storage"};
2235e577bc1SWilly Tu     dbus::utility::getSubTree(
2245e577bc1SWilly Tu         "/xyz/openbmc_project/inventory", 0, interfaces,
2255e577bc1SWilly Tu         std::bind_front(afterSystemsStorageGetSubtree, asyncResp, storageId));
2265e577bc1SWilly Tu }
2275e577bc1SWilly Tu 
2285e577bc1SWilly Tu inline void afterSubtree(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2295e577bc1SWilly Tu                          const std::string& storageId,
2305e577bc1SWilly Tu                          const boost::system::error_code& ec,
2315e577bc1SWilly Tu                          const dbus::utility::MapperGetSubTreeResponse& subtree)
2325e577bc1SWilly Tu {
2335e577bc1SWilly Tu     if (ec)
2345e577bc1SWilly Tu     {
23562598e31SEd Tanous         BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
2365e577bc1SWilly Tu         messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
2375e577bc1SWilly Tu                                    storageId);
2385e577bc1SWilly Tu         return;
2395e577bc1SWilly Tu     }
2403544d2a7SEd Tanous     auto storage = std::ranges::find_if(
2413544d2a7SEd Tanous         subtree,
2425e577bc1SWilly Tu         [&storageId](const std::pair<std::string,
2435e577bc1SWilly Tu                                      dbus::utility::MapperServiceMap>& object) {
2445e577bc1SWilly Tu         return sdbusplus::message::object_path(object.first).filename() ==
2455e577bc1SWilly Tu                storageId;
2465e577bc1SWilly Tu         });
2475e577bc1SWilly Tu     if (storage == subtree.end())
2485e577bc1SWilly Tu     {
2495e577bc1SWilly Tu         messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
2505e577bc1SWilly Tu                                    storageId);
2515e577bc1SWilly Tu         return;
2525e577bc1SWilly Tu     }
2535e577bc1SWilly Tu 
2545e577bc1SWilly Tu     asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
2555e577bc1SWilly Tu     asyncResp->res.jsonValue["@odata.id"] =
2565e577bc1SWilly Tu         boost::urls::format("/redfish/v1/Storage/{}", storageId);
2575e577bc1SWilly Tu     asyncResp->res.jsonValue["Name"] = "Storage";
2585e577bc1SWilly Tu     asyncResp->res.jsonValue["Id"] = storageId;
2595e577bc1SWilly Tu     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
2605e577bc1SWilly Tu 
2615e577bc1SWilly Tu     // Storage subsystem to Storage link.
2625e577bc1SWilly Tu     nlohmann::json::array_t storageServices;
2635e577bc1SWilly Tu     nlohmann::json::object_t storageService;
2645e577bc1SWilly Tu     storageService["@odata.id"] =
2655e577bc1SWilly Tu         boost::urls::format("/redfish/v1/Systems/system/Storage/{}", storageId);
2665e577bc1SWilly Tu     storageServices.emplace_back(storageService);
2675e577bc1SWilly Tu     asyncResp->res.jsonValue["Links"]["StorageServices"] =
2685e577bc1SWilly Tu         std::move(storageServices);
2695e577bc1SWilly Tu     asyncResp->res.jsonValue["Links"]["StorageServices@odata.count"] = 1;
2705e577bc1SWilly Tu }
2715e577bc1SWilly Tu 
2725e577bc1SWilly Tu inline void
2735e577bc1SWilly Tu     handleStorageGet(App& app, const crow::Request& req,
2745e577bc1SWilly Tu                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2755e577bc1SWilly Tu                      const std::string& storageId)
2765e577bc1SWilly Tu {
2775e577bc1SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2785e577bc1SWilly Tu     {
27962598e31SEd Tanous         BMCWEB_LOG_DEBUG("requestRoutesStorage setUpRedfishRoute failed");
2805e577bc1SWilly Tu         return;
2815e577bc1SWilly Tu     }
2825e577bc1SWilly Tu 
2835e577bc1SWilly Tu     constexpr std::array<std::string_view, 1> interfaces = {
2845e577bc1SWilly Tu         "xyz.openbmc_project.Inventory.Item.Storage"};
2855e577bc1SWilly Tu     dbus::utility::getSubTree(
2865e577bc1SWilly Tu         "/xyz/openbmc_project/inventory", 0, interfaces,
2875e577bc1SWilly Tu         std::bind_front(afterSubtree, asyncResp, storageId));
28836d52334SEd Tanous }
28936d52334SEd Tanous 
29036d52334SEd Tanous inline void requestRoutesStorage(App& app)
29136d52334SEd Tanous {
2927f3e84a1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/<str>/")
29336d52334SEd Tanous         .privileges(redfish::privileges::getStorage)
29436d52334SEd Tanous         .methods(boost::beast::http::verb::get)(
29536d52334SEd Tanous             std::bind_front(handleSystemsStorageGet, std::ref(app)));
2965e577bc1SWilly Tu 
2975e577bc1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Storage/<str>/")
2985e577bc1SWilly Tu         .privileges(redfish::privileges::getStorage)
2995e577bc1SWilly Tu         .methods(boost::beast::http::verb::get)(
3005e577bc1SWilly Tu             std::bind_front(handleStorageGet, std::ref(app)));
3017e860f15SJohn Edward Broadbent }
3027e860f15SJohn Edward Broadbent 
30303913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30403913171SWilly Tu                           const std::string& connectionName,
30503913171SWilly Tu                           const std::string& path)
30603913171SWilly Tu {
307d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
308d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, path,
309d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Asset",
3105e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
311168e20c1SEd Tanous                     const std::vector<
312168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
31303913171SWilly Tu                         propertiesList) {
31403913171SWilly Tu         if (ec)
31503913171SWilly Tu         {
31603913171SWilly Tu             // this interface isn't necessary
31703913171SWilly Tu             return;
31803913171SWilly Tu         }
319d1bde9e5SKrzysztof Grobelny 
320d1bde9e5SKrzysztof Grobelny         const std::string* partNumber = nullptr;
321d1bde9e5SKrzysztof Grobelny         const std::string* serialNumber = nullptr;
322d1bde9e5SKrzysztof Grobelny         const std::string* manufacturer = nullptr;
323d1bde9e5SKrzysztof Grobelny         const std::string* model = nullptr;
324d1bde9e5SKrzysztof Grobelny 
325d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
326d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
327d1bde9e5SKrzysztof Grobelny             partNumber, "SerialNumber", serialNumber, "Manufacturer",
328d1bde9e5SKrzysztof Grobelny             manufacturer, "Model", model);
329d1bde9e5SKrzysztof Grobelny 
330d1bde9e5SKrzysztof Grobelny         if (!success)
33103913171SWilly Tu         {
33203913171SWilly Tu             messages::internalError(asyncResp->res);
33303913171SWilly Tu             return;
33403913171SWilly Tu         }
335d1bde9e5SKrzysztof Grobelny 
336d1bde9e5SKrzysztof Grobelny         if (partNumber != nullptr)
337d1bde9e5SKrzysztof Grobelny         {
338d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["PartNumber"] = *partNumber;
33903913171SWilly Tu         }
340d1bde9e5SKrzysztof Grobelny 
341d1bde9e5SKrzysztof Grobelny         if (serialNumber != nullptr)
342d1bde9e5SKrzysztof Grobelny         {
343d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
34403913171SWilly Tu         }
345d1bde9e5SKrzysztof Grobelny 
346d1bde9e5SKrzysztof Grobelny         if (manufacturer != nullptr)
347d1bde9e5SKrzysztof Grobelny         {
348d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
349d1bde9e5SKrzysztof Grobelny         }
350d1bde9e5SKrzysztof Grobelny 
351d1bde9e5SKrzysztof Grobelny         if (model != nullptr)
352d1bde9e5SKrzysztof Grobelny         {
353d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Model"] = *model;
354d1bde9e5SKrzysztof Grobelny         }
355d1bde9e5SKrzysztof Grobelny         });
35603913171SWilly Tu }
35703913171SWilly Tu 
35803913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
35903913171SWilly Tu                             const std::string& connectionName,
36003913171SWilly Tu                             const std::string& path)
36103913171SWilly Tu {
3621e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3631e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3641e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
3655e7e2dc5SEd Tanous         [asyncResp, path](const boost::system::error_code& ec,
366cef57e85SWilly Tu                           const bool isPresent) {
36703913171SWilly Tu         // this interface isn't necessary, only check it if
36803913171SWilly Tu         // we get a good return
36903913171SWilly Tu         if (ec)
37003913171SWilly Tu         {
37103913171SWilly Tu             return;
37203913171SWilly Tu         }
37303913171SWilly Tu 
374cef57e85SWilly Tu         if (!isPresent)
37503913171SWilly Tu         {
376cef57e85SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Absent";
37703913171SWilly Tu         }
3781e1e598dSJonathan Doman         });
37903913171SWilly Tu }
38003913171SWilly Tu 
38103913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
38203913171SWilly Tu                           const std::string& connectionName,
38303913171SWilly Tu                           const std::string& path)
38403913171SWilly Tu {
3851e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3861e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3871e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3885e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec, const bool updating) {
38903913171SWilly Tu         // this interface isn't necessary, only check it
39003913171SWilly Tu         // if we get a good return
39103913171SWilly Tu         if (ec)
39203913171SWilly Tu         {
39303913171SWilly Tu             return;
39403913171SWilly Tu         }
39503913171SWilly Tu 
39603913171SWilly Tu         // updating and disabled in the backend shouldn't be
39703913171SWilly Tu         // able to be set at the same time, so we don't need
39803913171SWilly Tu         // to check for the race condition of these two
39903913171SWilly Tu         // calls
4001e1e598dSJonathan Doman         if (updating)
40103913171SWilly Tu         {
40203913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Updating";
40303913171SWilly Tu         }
4041e1e598dSJonathan Doman         });
40503913171SWilly Tu }
40603913171SWilly Tu 
407dde9bc12SGeorge Liu inline std::optional<drive::MediaType> convertDriveType(std::string_view type)
40819b8e9a0SWilly Tu {
40919b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
41019b8e9a0SWilly Tu     {
411dde9bc12SGeorge Liu         return drive::MediaType::HDD;
41219b8e9a0SWilly Tu     }
41319b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
41419b8e9a0SWilly Tu     {
415dde9bc12SGeorge Liu         return drive::MediaType::SSD;
41619b8e9a0SWilly Tu     }
417dde9bc12SGeorge Liu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.Unknown")
418dde9bc12SGeorge Liu     {
41919b8e9a0SWilly Tu         return std::nullopt;
42019b8e9a0SWilly Tu     }
42119b8e9a0SWilly Tu 
422dde9bc12SGeorge Liu     return drive::MediaType::Invalid;
423dde9bc12SGeorge Liu }
424dde9bc12SGeorge Liu 
425dde9bc12SGeorge Liu inline std::optional<protocol::Protocol>
426dde9bc12SGeorge Liu     convertDriveProtocol(std::string_view proto)
42719b8e9a0SWilly Tu {
42819b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
42919b8e9a0SWilly Tu     {
430dde9bc12SGeorge Liu         return protocol::Protocol::SAS;
43119b8e9a0SWilly Tu     }
43219b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
43319b8e9a0SWilly Tu     {
434dde9bc12SGeorge Liu         return protocol::Protocol::SATA;
43519b8e9a0SWilly Tu     }
43619b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
43719b8e9a0SWilly Tu     {
438dde9bc12SGeorge Liu         return protocol::Protocol::NVMe;
43919b8e9a0SWilly Tu     }
44019b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
44119b8e9a0SWilly Tu     {
442dde9bc12SGeorge Liu         return protocol::Protocol::FC;
443dde9bc12SGeorge Liu     }
444dde9bc12SGeorge Liu     if (proto ==
445dde9bc12SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.Unknown")
446dde9bc12SGeorge Liu     {
447dde9bc12SGeorge Liu         return std::nullopt;
44819b8e9a0SWilly Tu     }
44919b8e9a0SWilly Tu 
450dde9bc12SGeorge Liu     return protocol::Protocol::Invalid;
45119b8e9a0SWilly Tu }
45219b8e9a0SWilly Tu 
45319b8e9a0SWilly Tu inline void
45419b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
45519b8e9a0SWilly Tu                            const std::string& connectionName,
45619b8e9a0SWilly Tu                            const std::string& path)
45719b8e9a0SWilly Tu {
45819b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
45919b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
46019b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
4615e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
46219b8e9a0SWilly Tu                     const std::vector<
46319b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
46419b8e9a0SWilly Tu                         propertiesList) {
46519b8e9a0SWilly Tu         if (ec)
46619b8e9a0SWilly Tu         {
46719b8e9a0SWilly Tu             // this interface isn't required
46819b8e9a0SWilly Tu             return;
46919b8e9a0SWilly Tu         }
470e5029d88SJohn Edward Broadbent         const std::string* encryptionStatus = nullptr;
471e5029d88SJohn Edward Broadbent         const bool* isLocked = nullptr;
47219b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
47319b8e9a0SWilly Tu                  property : propertiesList)
47419b8e9a0SWilly Tu         {
47519b8e9a0SWilly Tu             const std::string& propertyName = property.first;
47619b8e9a0SWilly Tu             if (propertyName == "Type")
47719b8e9a0SWilly Tu             {
47819b8e9a0SWilly Tu                 const std::string* value =
47919b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
48019b8e9a0SWilly Tu                 if (value == nullptr)
48119b8e9a0SWilly Tu                 {
48219b8e9a0SWilly Tu                     // illegal property
48362598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: Type");
48419b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
48519b8e9a0SWilly Tu                     return;
48619b8e9a0SWilly Tu                 }
48719b8e9a0SWilly Tu 
488dde9bc12SGeorge Liu                 std::optional<drive::MediaType> mediaType =
489dde9bc12SGeorge Liu                     convertDriveType(*value);
49019b8e9a0SWilly Tu                 if (!mediaType)
49119b8e9a0SWilly Tu                 {
49262598e31SEd Tanous                     BMCWEB_LOG_WARNING("UnknownDriveType Interface: {}",
49362598e31SEd Tanous                                        *value);
494dde9bc12SGeorge Liu                     continue;
495dde9bc12SGeorge Liu                 }
496dde9bc12SGeorge Liu                 if (*mediaType == drive::MediaType::Invalid)
497dde9bc12SGeorge Liu                 {
49819b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
49919b8e9a0SWilly Tu                     return;
50019b8e9a0SWilly Tu                 }
50119b8e9a0SWilly Tu 
50219b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
50319b8e9a0SWilly Tu             }
50419b8e9a0SWilly Tu             else if (propertyName == "Capacity")
50519b8e9a0SWilly Tu             {
50619b8e9a0SWilly Tu                 const uint64_t* capacity =
50719b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
50819b8e9a0SWilly Tu                 if (capacity == nullptr)
50919b8e9a0SWilly Tu                 {
51062598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: Capacity");
51119b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
51219b8e9a0SWilly Tu                     return;
51319b8e9a0SWilly Tu                 }
51419b8e9a0SWilly Tu                 if (*capacity == 0)
51519b8e9a0SWilly Tu                 {
51619b8e9a0SWilly Tu                     // drive capacity not known
51719b8e9a0SWilly Tu                     continue;
51819b8e9a0SWilly Tu                 }
51919b8e9a0SWilly Tu 
52019b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
52119b8e9a0SWilly Tu             }
52219b8e9a0SWilly Tu             else if (propertyName == "Protocol")
52319b8e9a0SWilly Tu             {
52419b8e9a0SWilly Tu                 const std::string* value =
52519b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
52619b8e9a0SWilly Tu                 if (value == nullptr)
52719b8e9a0SWilly Tu                 {
52862598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: Protocol");
52919b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
53019b8e9a0SWilly Tu                     return;
53119b8e9a0SWilly Tu                 }
53219b8e9a0SWilly Tu 
533dde9bc12SGeorge Liu                 std::optional<protocol::Protocol> proto =
534dde9bc12SGeorge Liu                     convertDriveProtocol(*value);
53519b8e9a0SWilly Tu                 if (!proto)
53619b8e9a0SWilly Tu                 {
53762598e31SEd Tanous                     BMCWEB_LOG_WARNING("Unknown DrivePrototype Interface: {}",
53862598e31SEd Tanous                                        *value);
539dde9bc12SGeorge Liu                     continue;
540dde9bc12SGeorge Liu                 }
541dde9bc12SGeorge Liu                 if (*proto == protocol::Protocol::Invalid)
542dde9bc12SGeorge Liu                 {
54319b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
54419b8e9a0SWilly Tu                     return;
54519b8e9a0SWilly Tu                 }
54619b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
54719b8e9a0SWilly Tu             }
5483fe4d5ccSJohn Edward Broadbent             else if (propertyName == "PredictedMediaLifeLeftPercent")
5493fe4d5ccSJohn Edward Broadbent             {
5503fe4d5ccSJohn Edward Broadbent                 const uint8_t* lifeLeft =
5513fe4d5ccSJohn Edward Broadbent                     std::get_if<uint8_t>(&property.second);
5523fe4d5ccSJohn Edward Broadbent                 if (lifeLeft == nullptr)
5533fe4d5ccSJohn Edward Broadbent                 {
55462598e31SEd Tanous                     BMCWEB_LOG_ERROR(
55562598e31SEd Tanous                         "Illegal property: PredictedMediaLifeLeftPercent");
5563fe4d5ccSJohn Edward Broadbent                     messages::internalError(asyncResp->res);
5573fe4d5ccSJohn Edward Broadbent                     return;
5583fe4d5ccSJohn Edward Broadbent                 }
5593fe4d5ccSJohn Edward Broadbent                 // 255 means reading the value is not supported
5603fe4d5ccSJohn Edward Broadbent                 if (*lifeLeft != 255)
5613fe4d5ccSJohn Edward Broadbent                 {
5623fe4d5ccSJohn Edward Broadbent                     asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
5633fe4d5ccSJohn Edward Broadbent                         *lifeLeft;
5643fe4d5ccSJohn Edward Broadbent                 }
5653fe4d5ccSJohn Edward Broadbent             }
566e5029d88SJohn Edward Broadbent             else if (propertyName == "EncryptionStatus")
567e5029d88SJohn Edward Broadbent             {
568e5029d88SJohn Edward Broadbent                 encryptionStatus = std::get_if<std::string>(&property.second);
569e5029d88SJohn Edward Broadbent                 if (encryptionStatus == nullptr)
570e5029d88SJohn Edward Broadbent                 {
57162598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: EncryptionStatus");
572e5029d88SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
573e5029d88SJohn Edward Broadbent                     return;
57419b8e9a0SWilly Tu                 }
575e5029d88SJohn Edward Broadbent             }
576e5029d88SJohn Edward Broadbent             else if (propertyName == "Locked")
577e5029d88SJohn Edward Broadbent             {
578e5029d88SJohn Edward Broadbent                 isLocked = std::get_if<bool>(&property.second);
579e5029d88SJohn Edward Broadbent                 if (isLocked == nullptr)
580e5029d88SJohn Edward Broadbent                 {
58162598e31SEd Tanous                     BMCWEB_LOG_ERROR("Illegal property: Locked");
582e5029d88SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
583e5029d88SJohn Edward Broadbent                     return;
584e5029d88SJohn Edward Broadbent                 }
585e5029d88SJohn Edward Broadbent             }
586e5029d88SJohn Edward Broadbent         }
587e5029d88SJohn Edward Broadbent 
588e5029d88SJohn Edward Broadbent         if (encryptionStatus == nullptr || isLocked == nullptr ||
589e5029d88SJohn Edward Broadbent             *encryptionStatus ==
590e5029d88SJohn Edward Broadbent                 "xyz.openbmc_project.Drive.DriveEncryptionState.Unknown")
591e5029d88SJohn Edward Broadbent         {
592e5029d88SJohn Edward Broadbent             return;
593e5029d88SJohn Edward Broadbent         }
594e5029d88SJohn Edward Broadbent         if (*encryptionStatus !=
595e5029d88SJohn Edward Broadbent             "xyz.openbmc_project.Drive.DriveEncryptionState.Encrypted")
596e5029d88SJohn Edward Broadbent         {
597e5029d88SJohn Edward Broadbent             //"The drive is not currently encrypted."
598e5029d88SJohn Edward Broadbent             asyncResp->res.jsonValue["EncryptionStatus"] =
599e5029d88SJohn Edward Broadbent                 drive::EncryptionStatus::Unencrypted;
600e5029d88SJohn Edward Broadbent             return;
601e5029d88SJohn Edward Broadbent         }
602e5029d88SJohn Edward Broadbent         if (*isLocked)
603e5029d88SJohn Edward Broadbent         {
604e5029d88SJohn Edward Broadbent             //"The drive is currently encrypted and the data is not
605e5029d88SJohn Edward Broadbent             // accessible to the user."
606e5029d88SJohn Edward Broadbent             asyncResp->res.jsonValue["EncryptionStatus"] =
607e5029d88SJohn Edward Broadbent                 drive::EncryptionStatus::Locked;
608e5029d88SJohn Edward Broadbent             return;
609e5029d88SJohn Edward Broadbent         }
610e5029d88SJohn Edward Broadbent         // if not locked
611e5029d88SJohn Edward Broadbent         // "The drive is currently encrypted but the data is accessible
612e5029d88SJohn Edward Broadbent         // to the user in unencrypted form."
613e5029d88SJohn Edward Broadbent         asyncResp->res.jsonValue["EncryptionStatus"] =
614e5029d88SJohn Edward Broadbent             drive::EncryptionStatus::Unlocked;
61519b8e9a0SWilly Tu         });
61619b8e9a0SWilly Tu }
61719b8e9a0SWilly Tu 
618b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
619b53dcd9dSNan Zhou                             const std::string& connectionName,
620b53dcd9dSNan Zhou                             const std::string& path,
621e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
622e56ed6b9SJohn Edward Broadbent {
623e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
624e56ed6b9SJohn Edward Broadbent     {
625e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
626e56ed6b9SJohn Edward Broadbent         {
627e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
628e56ed6b9SJohn Edward Broadbent         }
629e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
630e56ed6b9SJohn Edward Broadbent         {
631e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
632e56ed6b9SJohn Edward Broadbent         }
633e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
634e56ed6b9SJohn Edward Broadbent         {
635e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
636e56ed6b9SJohn Edward Broadbent         }
637e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
638e56ed6b9SJohn Edward Broadbent         {
639e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
640e56ed6b9SJohn Edward Broadbent         }
641e56ed6b9SJohn Edward Broadbent     }
642e56ed6b9SJohn Edward Broadbent }
643e56ed6b9SJohn Edward Broadbent 
64436d52334SEd Tanous inline void afterGetSubtreeSystemsStorageDrive(
64545ca1b86SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
64636d52334SEd Tanous     const std::string& driveId, const boost::system::error_code& ec,
64736d52334SEd Tanous     const dbus::utility::MapperGetSubTreeResponse& subtree)
64845ca1b86SEd Tanous {
6497e860f15SJohn Edward Broadbent     if (ec)
6507e860f15SJohn Edward Broadbent     {
65162598e31SEd Tanous         BMCWEB_LOG_ERROR("Drive mapper call error");
6527e860f15SJohn Edward Broadbent         messages::internalError(asyncResp->res);
6537e860f15SJohn Edward Broadbent         return;
6547e860f15SJohn Edward Broadbent     }
6557e860f15SJohn Edward Broadbent 
6563544d2a7SEd Tanous     auto drive = std::ranges::find_if(
6573544d2a7SEd Tanous         subtree,
65836d52334SEd Tanous         [&driveId](const std::pair<std::string,
6598cb65f8aSNan Zhou                                    dbus::utility::MapperServiceMap>& object) {
66036d52334SEd Tanous         return sdbusplus::message::object_path(object.first).filename() ==
66136d52334SEd Tanous                driveId;
6627e860f15SJohn Edward Broadbent         });
6637e860f15SJohn Edward Broadbent 
66403913171SWilly Tu     if (drive == subtree.end())
6657e860f15SJohn Edward Broadbent     {
666002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "Drive", driveId);
6677e860f15SJohn Edward Broadbent         return;
6687e860f15SJohn Edward Broadbent     }
6697e860f15SJohn Edward Broadbent 
67003913171SWilly Tu     const std::string& path = drive->first;
67136d52334SEd Tanous     const dbus::utility::MapperServiceMap& connectionNames = drive->second;
6727e860f15SJohn Edward Broadbent 
673002d39b4SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
674ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
675ef4c65b7SEd Tanous         "/redfish/v1/Systems/system/Storage/1/Drives/{}", driveId);
6767e860f15SJohn Edward Broadbent     asyncResp->res.jsonValue["Name"] = driveId;
6777e860f15SJohn Edward Broadbent     asyncResp->res.jsonValue["Id"] = driveId;
6787e860f15SJohn Edward Broadbent 
6797e860f15SJohn Edward Broadbent     if (connectionNames.size() != 1)
6807e860f15SJohn Edward Broadbent     {
68162598e31SEd Tanous         BMCWEB_LOG_ERROR("Connection size {}, not equal to 1",
68262598e31SEd Tanous                          connectionNames.size());
6837e860f15SJohn Edward Broadbent         messages::internalError(asyncResp->res);
6847e860f15SJohn Edward Broadbent         return;
6857e860f15SJohn Edward Broadbent     }
6867e860f15SJohn Edward Broadbent 
68736d52334SEd Tanous     getMainChassisId(asyncResp,
688ef4c65b7SEd Tanous                      [](const std::string& chassisId,
6897e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
690002d39b4SEd Tanous         aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
691ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
6927e860f15SJohn Edward Broadbent     });
6937e860f15SJohn Edward Broadbent 
694a25aeccfSNikhil Potade     // default it to Enabled
695a25aeccfSNikhil Potade     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
696a25aeccfSNikhil Potade 
69713451e39SWilly Tu     if constexpr (bmcwebEnableHealthPopulate)
69813451e39SWilly Tu     {
6992ad9c2f6SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
700e284a7c1SJames Feist         health->inventory.emplace_back(path);
7012ad9c2f6SJames Feist         health->populate();
70213451e39SWilly Tu     }
7032ad9c2f6SJames Feist 
704e56ed6b9SJohn Edward Broadbent     addAllDriveInfo(asyncResp, connectionNames[0].first, path,
705e56ed6b9SJohn Edward Broadbent                     connectionNames[0].second);
706a25aeccfSNikhil Potade }
70792903bd4SJohn Edward Broadbent 
70836d52334SEd Tanous inline void handleSystemsStorageDriveGet(
70936d52334SEd Tanous     App& app, const crow::Request& req,
71092903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
71136d52334SEd Tanous     const std::string& systemName, const std::string& driveId)
71292903bd4SJohn Edward Broadbent {
7133ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
71492903bd4SJohn Edward Broadbent     {
71592903bd4SJohn Edward Broadbent         return;
71692903bd4SJohn Edward Broadbent     }
7177f3e84a1SEd Tanous     if constexpr (bmcwebEnableMultiHost)
7187f3e84a1SEd Tanous     {
7197f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
7207f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
7217f3e84a1SEd Tanous                                    systemName);
7227f3e84a1SEd Tanous         return;
7237f3e84a1SEd Tanous     }
7247f3e84a1SEd Tanous 
72536d52334SEd Tanous     if (systemName != "system")
72636d52334SEd Tanous     {
72736d52334SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
72836d52334SEd Tanous                                    systemName);
72936d52334SEd Tanous         return;
73036d52334SEd Tanous     }
73192903bd4SJohn Edward Broadbent 
73236d52334SEd Tanous     constexpr std::array<std::string_view, 1> interfaces = {
73336d52334SEd Tanous         "xyz.openbmc_project.Inventory.Item.Drive"};
734e99073f5SGeorge Liu     dbus::utility::getSubTree(
735e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
73636d52334SEd Tanous         std::bind_front(afterGetSubtreeSystemsStorageDrive, asyncResp,
73736d52334SEd Tanous                         driveId));
73836d52334SEd Tanous }
73936d52334SEd Tanous 
74036d52334SEd Tanous inline void requestRoutesDrive(App& app)
74136d52334SEd Tanous {
74236d52334SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
74336d52334SEd Tanous         .privileges(redfish::privileges::getDrive)
74436d52334SEd Tanous         .methods(boost::beast::http::verb::get)(
74536d52334SEd Tanous             std::bind_front(handleSystemsStorageDriveGet, std::ref(app)));
74636d52334SEd Tanous }
74736d52334SEd Tanous 
74836d52334SEd Tanous inline void afterChassisDriveCollectionSubtreeGet(
74936d52334SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
75036d52334SEd Tanous     const std::string& chassisId, const boost::system::error_code& ec,
75136d52334SEd Tanous     const dbus::utility::MapperGetSubTreeResponse& subtree)
75236d52334SEd Tanous {
75392903bd4SJohn Edward Broadbent     if (ec)
75492903bd4SJohn Edward Broadbent     {
75592903bd4SJohn Edward Broadbent         if (ec == boost::system::errc::host_unreachable)
75692903bd4SJohn Edward Broadbent         {
75736d52334SEd Tanous             messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
75892903bd4SJohn Edward Broadbent             return;
75992903bd4SJohn Edward Broadbent         }
76092903bd4SJohn Edward Broadbent         messages::internalError(asyncResp->res);
76192903bd4SJohn Edward Broadbent         return;
76292903bd4SJohn Edward Broadbent     }
76392903bd4SJohn Edward Broadbent 
76492903bd4SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
7658cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
76692903bd4SJohn Edward Broadbent     {
76792903bd4SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
76892903bd4SJohn Edward Broadbent         if (objPath.filename() != chassisId)
76992903bd4SJohn Edward Broadbent         {
77092903bd4SJohn Edward Broadbent             continue;
77192903bd4SJohn Edward Broadbent         }
77292903bd4SJohn Edward Broadbent 
77392903bd4SJohn Edward Broadbent         if (connectionNames.empty())
77492903bd4SJohn Edward Broadbent         {
77562598e31SEd Tanous             BMCWEB_LOG_ERROR("Got 0 Connection names");
77692903bd4SJohn Edward Broadbent             continue;
77792903bd4SJohn Edward Broadbent         }
77892903bd4SJohn Edward Broadbent 
77992903bd4SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] =
78092903bd4SJohn Edward Broadbent             "#DriveCollection.DriveCollection";
78192903bd4SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] =
782ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/Chassis/{}/Drives", chassisId);
78392903bd4SJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = "Drive Collection";
78492903bd4SJohn Edward Broadbent 
78592903bd4SJohn Edward Broadbent         // Association lambda
7866c3e9451SGeorge Liu         dbus::utility::getAssociationEndPoints(
7876c3e9451SGeorge Liu             path + "/drive",
78836d52334SEd Tanous             [asyncResp, chassisId](const boost::system::error_code& ec3,
7896c3e9451SGeorge Liu                                    const dbus::utility::MapperEndPoints& resp) {
79092903bd4SJohn Edward Broadbent             if (ec3)
79192903bd4SJohn Edward Broadbent             {
79262598e31SEd Tanous                 BMCWEB_LOG_ERROR("Error in chassis Drive association ");
79392903bd4SJohn Edward Broadbent             }
79492903bd4SJohn Edward Broadbent             nlohmann::json& members = asyncResp->res.jsonValue["Members"];
79592903bd4SJohn Edward Broadbent             // important if array is empty
79692903bd4SJohn Edward Broadbent             members = nlohmann::json::array();
79792903bd4SJohn Edward Broadbent 
79892903bd4SJohn Edward Broadbent             std::vector<std::string> leafNames;
79992903bd4SJohn Edward Broadbent             for (const auto& drive : resp)
80092903bd4SJohn Edward Broadbent             {
8018a592810SEd Tanous                 sdbusplus::message::object_path drivePath(drive);
8028a592810SEd Tanous                 leafNames.push_back(drivePath.filename());
80392903bd4SJohn Edward Broadbent             }
80492903bd4SJohn Edward Broadbent 
8053544d2a7SEd Tanous             std::ranges::sort(leafNames, AlphanumLess<std::string>());
80692903bd4SJohn Edward Broadbent 
80792903bd4SJohn Edward Broadbent             for (const auto& leafName : leafNames)
80892903bd4SJohn Edward Broadbent             {
80992903bd4SJohn Edward Broadbent                 nlohmann::json::object_t member;
81036d52334SEd Tanous                 member["@odata.id"] = boost::urls::format(
81136d52334SEd Tanous                     "/redfish/v1/Chassis/{}/Drives/{}", chassisId, leafName);
812b2ba3072SPatrick Williams                 members.emplace_back(std::move(member));
81392903bd4SJohn Edward Broadbent                 // navigation links will be registered in next patch set
81492903bd4SJohn Edward Broadbent             }
81592903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
81692903bd4SJohn Edward Broadbent             }); // end association lambda
81792903bd4SJohn Edward Broadbent 
81892903bd4SJohn Edward Broadbent     }           // end Iterate over all retrieved ObjectPaths
81936d52334SEd Tanous }
82036d52334SEd Tanous /**
82136d52334SEd Tanous  * Chassis drives, this URL will show all the DriveCollection
82236d52334SEd Tanous  * information
82336d52334SEd Tanous  */
82436d52334SEd Tanous inline void chassisDriveCollectionGet(
82536d52334SEd Tanous     crow::App& app, const crow::Request& req,
82636d52334SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
82736d52334SEd Tanous     const std::string& chassisId)
82836d52334SEd Tanous {
82936d52334SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
83036d52334SEd Tanous     {
83136d52334SEd Tanous         return;
83236d52334SEd Tanous     }
83336d52334SEd Tanous 
83436d52334SEd Tanous     // mapper call lambda
83536d52334SEd Tanous     constexpr std::array<std::string_view, 2> interfaces = {
83636d52334SEd Tanous         "xyz.openbmc_project.Inventory.Item.Board",
83736d52334SEd Tanous         "xyz.openbmc_project.Inventory.Item.Chassis"};
83836d52334SEd Tanous     dbus::utility::getSubTree(
83936d52334SEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces,
84036d52334SEd Tanous         std::bind_front(afterChassisDriveCollectionSubtreeGet, asyncResp,
84136d52334SEd Tanous                         chassisId));
84292903bd4SJohn Edward Broadbent }
84392903bd4SJohn Edward Broadbent 
84492903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
84592903bd4SJohn Edward Broadbent {
84692903bd4SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
84792903bd4SJohn Edward Broadbent         .privileges(redfish::privileges::getDriveCollection)
84892903bd4SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
84992903bd4SJohn Edward Broadbent             std::bind_front(chassisDriveCollectionGet, std::ref(app)));
85092903bd4SJohn Edward Broadbent }
85192903bd4SJohn Edward Broadbent 
852b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
853b53dcd9dSNan Zhou                        const std::string& chassisId,
854b53dcd9dSNan Zhou                        const std::string& driveName,
8555e7e2dc5SEd Tanous                        const boost::system::error_code& ec,
856e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
857e56ed6b9SJohn Edward Broadbent {
858e56ed6b9SJohn Edward Broadbent     if (ec)
859e56ed6b9SJohn Edward Broadbent     {
86062598e31SEd Tanous         BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
861e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
862e56ed6b9SJohn Edward Broadbent         return;
863e56ed6b9SJohn Edward Broadbent     }
864e56ed6b9SJohn Edward Broadbent 
865e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
8668cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
867e56ed6b9SJohn Edward Broadbent     {
868e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
869e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
870e56ed6b9SJohn Edward Broadbent         {
871e56ed6b9SJohn Edward Broadbent             continue;
872e56ed6b9SJohn Edward Broadbent         }
873e56ed6b9SJohn Edward Broadbent 
874e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
875e56ed6b9SJohn Edward Broadbent         {
87662598e31SEd Tanous             BMCWEB_LOG_ERROR("Got 0 Connection names");
877e56ed6b9SJohn Edward Broadbent             continue;
878e56ed6b9SJohn Edward Broadbent         }
879e56ed6b9SJohn Edward Broadbent 
880ef4c65b7SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
881ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/Drives/{}", chassisId, driveName);
882e56ed6b9SJohn Edward Broadbent 
883e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
884a0cb40cbSJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = driveName;
885e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
886e56ed6b9SJohn Edward Broadbent         // default it to Enabled
887e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
888e56ed6b9SJohn Edward Broadbent 
889e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
890e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
891ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
892e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
893e56ed6b9SJohn Edward Broadbent 
894e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
895e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
896e56ed6b9SJohn Edward Broadbent     }
897e56ed6b9SJohn Edward Broadbent }
898e56ed6b9SJohn Edward Broadbent 
899b53dcd9dSNan Zhou inline void
900b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
901e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
902e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
903e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
904e56ed6b9SJohn Edward Broadbent {
905e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
906e56ed6b9SJohn Edward Broadbent     {
907e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
908e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
909e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
910e56ed6b9SJohn Edward Broadbent         {
911e56ed6b9SJohn Edward Broadbent             continue;
912e56ed6b9SJohn Edward Broadbent         }
913e56ed6b9SJohn Edward Broadbent         //  mapper call drive
914e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> driveInterface = {
915e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
916e99073f5SGeorge Liu         dbus::utility::getSubTree(
917e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, driveInterface,
918e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
919e99073f5SGeorge Liu                 const boost::system::error_code& ec,
920e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
921e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
922e99073f5SGeorge Liu             });
923e56ed6b9SJohn Edward Broadbent     }
924e56ed6b9SJohn Edward Broadbent }
925e56ed6b9SJohn Edward Broadbent 
926b53dcd9dSNan Zhou inline void
927b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
928e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
929e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
930e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
931e56ed6b9SJohn Edward Broadbent {
93203810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
933e56ed6b9SJohn Edward Broadbent     {
934e56ed6b9SJohn Edward Broadbent         return;
935e56ed6b9SJohn Edward Broadbent     }
936e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
937e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
938e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
939e56ed6b9SJohn Edward Broadbent 
940e56ed6b9SJohn Edward Broadbent     // mapper call chassis
941e99073f5SGeorge Liu     dbus::utility::getSubTree(
942e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
943e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
944e99073f5SGeorge Liu          driveName](const boost::system::error_code& ec,
945e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
946e56ed6b9SJohn Edward Broadbent         if (ec)
947e56ed6b9SJohn Edward Broadbent         {
948e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
949e56ed6b9SJohn Edward Broadbent             return;
950e56ed6b9SJohn Edward Broadbent         }
951e56ed6b9SJohn Edward Broadbent 
952e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
9538cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
954e56ed6b9SJohn Edward Broadbent         {
955e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
956e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
957e56ed6b9SJohn Edward Broadbent             {
958e56ed6b9SJohn Edward Broadbent                 continue;
959e56ed6b9SJohn Edward Broadbent             }
960e56ed6b9SJohn Edward Broadbent 
961e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
962e56ed6b9SJohn Edward Broadbent             {
96362598e31SEd Tanous                 BMCWEB_LOG_ERROR("Got 0 Connection names");
964e56ed6b9SJohn Edward Broadbent                 continue;
965e56ed6b9SJohn Edward Broadbent             }
966e56ed6b9SJohn Edward Broadbent 
9676c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
9686c3e9451SGeorge Liu                 path + "/drive",
969e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
9705e7e2dc5SEd Tanous                  driveName](const boost::system::error_code& ec3,
9716c3e9451SGeorge Liu                             const dbus::utility::MapperEndPoints& resp) {
972e56ed6b9SJohn Edward Broadbent                 if (ec3)
973e56ed6b9SJohn Edward Broadbent                 {
974e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
975e56ed6b9SJohn Edward Broadbent                 }
976e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
977e56ed6b9SJohn Edward Broadbent                 });
978e56ed6b9SJohn Edward Broadbent             break;
979e56ed6b9SJohn Edward Broadbent         }
980e99073f5SGeorge Liu         });
981e56ed6b9SJohn Edward Broadbent }
982e56ed6b9SJohn Edward Broadbent 
983e56ed6b9SJohn Edward Broadbent /**
984e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
985e56ed6b9SJohn Edward Broadbent  */
986e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
987e56ed6b9SJohn Edward Broadbent {
988e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
989e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
990e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
991e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
992e56ed6b9SJohn Edward Broadbent }
993e56ed6b9SJohn Edward Broadbent 
99461b1eb21SWilly Tu inline void getStorageControllerAsset(
99561b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
99661b1eb21SWilly Tu     const boost::system::error_code& ec,
99761b1eb21SWilly Tu     const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
99861b1eb21SWilly Tu         propertiesList)
99961b1eb21SWilly Tu {
100061b1eb21SWilly Tu     if (ec)
100161b1eb21SWilly Tu     {
100261b1eb21SWilly Tu         // this interface isn't necessary
100362598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to get StorageControllerAsset");
100461b1eb21SWilly Tu         return;
100561b1eb21SWilly Tu     }
100661b1eb21SWilly Tu 
100761b1eb21SWilly Tu     const std::string* partNumber = nullptr;
100861b1eb21SWilly Tu     const std::string* serialNumber = nullptr;
100961b1eb21SWilly Tu     const std::string* manufacturer = nullptr;
101061b1eb21SWilly Tu     const std::string* model = nullptr;
101161b1eb21SWilly Tu     if (!sdbusplus::unpackPropertiesNoThrow(
101261b1eb21SWilly Tu             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
101361b1eb21SWilly Tu             partNumber, "SerialNumber", serialNumber, "Manufacturer",
101461b1eb21SWilly Tu             manufacturer, "Model", model))
101561b1eb21SWilly Tu     {
101661b1eb21SWilly Tu         messages::internalError(asyncResp->res);
101761b1eb21SWilly Tu         return;
101861b1eb21SWilly Tu     }
101961b1eb21SWilly Tu 
102061b1eb21SWilly Tu     if (partNumber != nullptr)
102161b1eb21SWilly Tu     {
102261b1eb21SWilly Tu         asyncResp->res.jsonValue["PartNumber"] = *partNumber;
102361b1eb21SWilly Tu     }
102461b1eb21SWilly Tu 
102561b1eb21SWilly Tu     if (serialNumber != nullptr)
102661b1eb21SWilly Tu     {
102761b1eb21SWilly Tu         asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
102861b1eb21SWilly Tu     }
102961b1eb21SWilly Tu 
103061b1eb21SWilly Tu     if (manufacturer != nullptr)
103161b1eb21SWilly Tu     {
103261b1eb21SWilly Tu         asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
103361b1eb21SWilly Tu     }
103461b1eb21SWilly Tu 
103561b1eb21SWilly Tu     if (model != nullptr)
103661b1eb21SWilly Tu     {
103761b1eb21SWilly Tu         asyncResp->res.jsonValue["Model"] = *model;
103861b1eb21SWilly Tu     }
103961b1eb21SWilly Tu }
104061b1eb21SWilly Tu 
104161b1eb21SWilly Tu inline void populateStorageController(
104261b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
104361b1eb21SWilly Tu     const std::string& controllerId, const std::string& connectionName,
104461b1eb21SWilly Tu     const std::string& path)
104561b1eb21SWilly Tu {
104661b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.type"] =
104761b1eb21SWilly Tu         "#StorageController.v1_6_0.StorageController";
104861b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
104961b1eb21SWilly Tu         "/redfish/v1/Systems/system/Storage/1/Controllers/{}", controllerId);
105061b1eb21SWilly Tu     asyncResp->res.jsonValue["Name"] = controllerId;
105161b1eb21SWilly Tu     asyncResp->res.jsonValue["Id"] = controllerId;
105261b1eb21SWilly Tu     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
105361b1eb21SWilly Tu 
105461b1eb21SWilly Tu     sdbusplus::asio::getProperty<bool>(
105561b1eb21SWilly Tu         *crow::connections::systemBus, connectionName, path,
105661b1eb21SWilly Tu         "xyz.openbmc_project.Inventory.Item", "Present",
105761b1eb21SWilly Tu         [asyncResp](const boost::system::error_code& ec, bool isPresent) {
105861b1eb21SWilly Tu         // this interface isn't necessary, only check it
105961b1eb21SWilly Tu         // if we get a good return
106061b1eb21SWilly Tu         if (ec)
106161b1eb21SWilly Tu         {
106262598e31SEd Tanous             BMCWEB_LOG_DEBUG("Failed to get Present property");
106361b1eb21SWilly Tu             return;
106461b1eb21SWilly Tu         }
106561b1eb21SWilly Tu         if (!isPresent)
106661b1eb21SWilly Tu         {
106761b1eb21SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Absent";
106861b1eb21SWilly Tu         }
106961b1eb21SWilly Tu         });
107061b1eb21SWilly Tu 
107161b1eb21SWilly Tu     sdbusplus::asio::getAllProperties(
107261b1eb21SWilly Tu         *crow::connections::systemBus, connectionName, path,
107361b1eb21SWilly Tu         "xyz.openbmc_project.Inventory.Decorator.Asset",
107461b1eb21SWilly Tu         [asyncResp](const boost::system::error_code& ec,
107561b1eb21SWilly Tu                     const std::vector<
107661b1eb21SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
107761b1eb21SWilly Tu                         propertiesList) {
107861b1eb21SWilly Tu         getStorageControllerAsset(asyncResp, ec, propertiesList);
107961b1eb21SWilly Tu         });
108061b1eb21SWilly Tu }
108161b1eb21SWilly Tu 
108261b1eb21SWilly Tu inline void getStorageControllerHandler(
108361b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
108461b1eb21SWilly Tu     const std::string& controllerId, const boost::system::error_code& ec,
108561b1eb21SWilly Tu     const dbus::utility::MapperGetSubTreeResponse& subtree)
108661b1eb21SWilly Tu {
108761b1eb21SWilly Tu     if (ec || subtree.empty())
108861b1eb21SWilly Tu     {
108961b1eb21SWilly Tu         // doesn't have to be there
109062598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to handle StorageController");
109161b1eb21SWilly Tu         return;
109261b1eb21SWilly Tu     }
109361b1eb21SWilly Tu 
109461b1eb21SWilly Tu     for (const auto& [path, interfaceDict] : subtree)
109561b1eb21SWilly Tu     {
109661b1eb21SWilly Tu         sdbusplus::message::object_path object(path);
109761b1eb21SWilly Tu         std::string id = object.filename();
109861b1eb21SWilly Tu         if (id.empty())
109961b1eb21SWilly Tu         {
110062598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
110161b1eb21SWilly Tu             return;
110261b1eb21SWilly Tu         }
110361b1eb21SWilly Tu         if (id != controllerId)
110461b1eb21SWilly Tu         {
110561b1eb21SWilly Tu             continue;
110661b1eb21SWilly Tu         }
110761b1eb21SWilly Tu 
110861b1eb21SWilly Tu         if (interfaceDict.size() != 1)
110961b1eb21SWilly Tu         {
111062598e31SEd Tanous             BMCWEB_LOG_ERROR("Connection size {}, greater than 1",
111162598e31SEd Tanous                              interfaceDict.size());
111261b1eb21SWilly Tu             messages::internalError(asyncResp->res);
111361b1eb21SWilly Tu             return;
111461b1eb21SWilly Tu         }
111561b1eb21SWilly Tu 
111661b1eb21SWilly Tu         const std::string& connectionName = interfaceDict.front().first;
111761b1eb21SWilly Tu         populateStorageController(asyncResp, controllerId, connectionName,
111861b1eb21SWilly Tu                                   path);
111961b1eb21SWilly Tu     }
112061b1eb21SWilly Tu }
112161b1eb21SWilly Tu 
112261b1eb21SWilly Tu inline void populateStorageControllerCollection(
112361b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
112461b1eb21SWilly Tu     const boost::system::error_code& ec,
112561b1eb21SWilly Tu     const dbus::utility::MapperGetSubTreePathsResponse& controllerList)
112661b1eb21SWilly Tu {
112761b1eb21SWilly Tu     nlohmann::json::array_t members;
112861b1eb21SWilly Tu     if (ec || controllerList.empty())
112961b1eb21SWilly Tu     {
113061b1eb21SWilly Tu         asyncResp->res.jsonValue["Members"] = std::move(members);
113161b1eb21SWilly Tu         asyncResp->res.jsonValue["Members@odata.count"] = 0;
113262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to find any StorageController");
113361b1eb21SWilly Tu         return;
113461b1eb21SWilly Tu     }
113561b1eb21SWilly Tu 
113661b1eb21SWilly Tu     for (const std::string& path : controllerList)
113761b1eb21SWilly Tu     {
113861b1eb21SWilly Tu         std::string id = sdbusplus::message::object_path(path).filename();
113961b1eb21SWilly Tu         if (id.empty())
114061b1eb21SWilly Tu         {
114162598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
114261b1eb21SWilly Tu             return;
114361b1eb21SWilly Tu         }
114461b1eb21SWilly Tu         nlohmann::json::object_t member;
114561b1eb21SWilly Tu         member["@odata.id"] = boost::urls::format(
114661b1eb21SWilly Tu             "/redfish/v1/Systems/system/Storage/1/Controllers/{}", id);
114761b1eb21SWilly Tu         members.emplace_back(member);
114861b1eb21SWilly Tu     }
114961b1eb21SWilly Tu     asyncResp->res.jsonValue["Members@odata.count"] = members.size();
115061b1eb21SWilly Tu     asyncResp->res.jsonValue["Members"] = std::move(members);
115161b1eb21SWilly Tu }
115261b1eb21SWilly Tu 
115336d52334SEd Tanous inline void handleSystemsStorageControllerCollectionGet(
115461b1eb21SWilly Tu     App& app, const crow::Request& req,
115561b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
115661b1eb21SWilly Tu     const std::string& systemName)
115761b1eb21SWilly Tu {
115861b1eb21SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
115961b1eb21SWilly Tu     {
116062598e31SEd Tanous         BMCWEB_LOG_DEBUG(
116162598e31SEd Tanous             "Failed to setup Redfish Route for StorageController Collection");
116261b1eb21SWilly Tu         return;
116361b1eb21SWilly Tu     }
116461b1eb21SWilly Tu     if (systemName != "system")
116561b1eb21SWilly Tu     {
116661b1eb21SWilly Tu         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
116761b1eb21SWilly Tu                                    systemName);
116862598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
116961b1eb21SWilly Tu         return;
117061b1eb21SWilly Tu     }
117161b1eb21SWilly Tu 
117261b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.type"] =
117361b1eb21SWilly Tu         "#StorageControllerCollection.StorageControllerCollection";
117461b1eb21SWilly Tu     asyncResp->res.jsonValue["@odata.id"] =
117561b1eb21SWilly Tu         "/redfish/v1/Systems/system/Storage/1/Controllers";
117661b1eb21SWilly Tu     asyncResp->res.jsonValue["Name"] = "Storage Controller Collection";
117761b1eb21SWilly Tu 
117861b1eb21SWilly Tu     constexpr std::array<std::string_view, 1> interfaces = {
117961b1eb21SWilly Tu         "xyz.openbmc_project.Inventory.Item.StorageController"};
118061b1eb21SWilly Tu     dbus::utility::getSubTreePaths(
118161b1eb21SWilly Tu         "/xyz/openbmc_project/inventory", 0, interfaces,
118261b1eb21SWilly Tu         [asyncResp](const boost::system::error_code& ec,
118361b1eb21SWilly Tu                     const dbus::utility::MapperGetSubTreePathsResponse&
118461b1eb21SWilly Tu                         controllerList) {
118561b1eb21SWilly Tu         populateStorageControllerCollection(asyncResp, ec, controllerList);
118661b1eb21SWilly Tu         });
118761b1eb21SWilly Tu }
118861b1eb21SWilly Tu 
118936d52334SEd Tanous inline void handleSystemsStorageControllerGet(
119061b1eb21SWilly Tu     App& app, const crow::Request& req,
119161b1eb21SWilly Tu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
119261b1eb21SWilly Tu     const std::string& systemName, const std::string& controllerId)
119361b1eb21SWilly Tu {
119461b1eb21SWilly Tu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
119561b1eb21SWilly Tu     {
119662598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to setup Redfish Route for StorageController");
119761b1eb21SWilly Tu         return;
119861b1eb21SWilly Tu     }
119961b1eb21SWilly Tu     if (systemName != "system")
120061b1eb21SWilly Tu     {
120161b1eb21SWilly Tu         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
120261b1eb21SWilly Tu                                    systemName);
120362598e31SEd Tanous         BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
120461b1eb21SWilly Tu         return;
120561b1eb21SWilly Tu     }
120661b1eb21SWilly Tu     constexpr std::array<std::string_view, 1> interfaces = {
120761b1eb21SWilly Tu         "xyz.openbmc_project.Inventory.Item.StorageController"};
120861b1eb21SWilly Tu     dbus::utility::getSubTree(
120961b1eb21SWilly Tu         "/xyz/openbmc_project/inventory", 0, interfaces,
121061b1eb21SWilly Tu         [asyncResp,
121161b1eb21SWilly Tu          controllerId](const boost::system::error_code& ec,
121261b1eb21SWilly Tu                        const dbus::utility::MapperGetSubTreeResponse& subtree) {
121361b1eb21SWilly Tu         getStorageControllerHandler(asyncResp, controllerId, ec, subtree);
121461b1eb21SWilly Tu         });
121561b1eb21SWilly Tu }
121661b1eb21SWilly Tu 
121761b1eb21SWilly Tu inline void requestRoutesStorageControllerCollection(App& app)
121861b1eb21SWilly Tu {
121961b1eb21SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/")
122061b1eb21SWilly Tu         .privileges(redfish::privileges::getStorageControllerCollection)
122136d52334SEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
122236d52334SEd Tanous             handleSystemsStorageControllerCollectionGet, std::ref(app)));
122361b1eb21SWilly Tu }
122461b1eb21SWilly Tu 
122561b1eb21SWilly Tu inline void requestRoutesStorageController(App& app)
122661b1eb21SWilly Tu {
122761b1eb21SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/<str>")
122861b1eb21SWilly Tu         .privileges(redfish::privileges::getStorageController)
122961b1eb21SWilly Tu         .methods(boost::beast::http::verb::get)(
123036d52334SEd Tanous             std::bind_front(handleSystemsStorageControllerGet, std::ref(app)));
123161b1eb21SWilly Tu }
123261b1eb21SWilly Tu 
1233a25aeccfSNikhil Potade } // namespace redfish
1234