xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision e99073f55f30e2af2c36d7859513ea9464fa647d)
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 
183ccb3adbSEd Tanous #include "app.hpp"
197a1dbc48SGeorge Liu #include "dbus_utility.hpp"
202ad9c2f6SJames Feist #include "health.hpp"
21e284a7c1SJames Feist #include "openbmc_dbus_rest.hpp"
223ccb3adbSEd Tanous #include "query.hpp"
233ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
243ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
252ad9c2f6SJames Feist 
26*e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
271e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
28d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
29a25aeccfSNikhil Potade 
307a1dbc48SGeorge Liu #include <array>
317a1dbc48SGeorge Liu #include <string_view>
327a1dbc48SGeorge Liu 
33a25aeccfSNikhil Potade namespace redfish
34a25aeccfSNikhil Potade {
357e860f15SJohn Edward Broadbent inline void requestRoutesStorageCollection(App& app)
36a25aeccfSNikhil Potade {
3722d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
38ed398213SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
397e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
4045ca1b86SEd Tanous             [&app](const crow::Request& req,
4122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4222d268cbSEd Tanous                    const std::string& systemName) {
433ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
4445ca1b86SEd Tanous         {
4545ca1b86SEd Tanous             return;
4645ca1b86SEd Tanous         }
4722d268cbSEd Tanous         if (systemName != "system")
4822d268cbSEd Tanous         {
4922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
5022d268cbSEd Tanous                                        systemName);
5122d268cbSEd Tanous             return;
5222d268cbSEd Tanous         }
5322d268cbSEd Tanous 
548d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
558d1b46d7Szhanghch05             "#StorageCollection.StorageCollection";
568d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
578d1b46d7Szhanghch05             "/redfish/v1/Systems/system/Storage";
588d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Storage Collection";
591476687dSEd Tanous         nlohmann::json::array_t members;
601476687dSEd Tanous         nlohmann::json::object_t member;
611476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
621476687dSEd Tanous         members.emplace_back(member);
631476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
648d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
657e860f15SJohn Edward Broadbent         });
66a25aeccfSNikhil Potade }
67a25aeccfSNikhil Potade 
68a85afbe1SWilly Tu inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
69a85afbe1SWilly Tu                       const std::shared_ptr<HealthPopulate>& health)
70a25aeccfSNikhil Potade {
717a1dbc48SGeorge Liu     const std::array<std::string_view, 1> interfaces = {
727a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Drive"};
737a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
747a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
75a85afbe1SWilly Tu         [asyncResp, health](
767a1dbc48SGeorge Liu             const boost::system::error_code& ec,
77a85afbe1SWilly Tu             const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
78a25aeccfSNikhil Potade         if (ec)
79a25aeccfSNikhil Potade         {
80a25aeccfSNikhil Potade             BMCWEB_LOG_ERROR << "Drive mapper call error";
81a25aeccfSNikhil Potade             messages::internalError(asyncResp->res);
82a25aeccfSNikhil Potade             return;
83a25aeccfSNikhil Potade         }
842ad9c2f6SJames Feist 
85a85afbe1SWilly Tu         nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
86a85afbe1SWilly Tu         driveArray = nlohmann::json::array();
87a85afbe1SWilly Tu         auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
88a85afbe1SWilly Tu         count = 0;
892ad9c2f6SJames Feist 
90a85afbe1SWilly Tu         health->inventory.insert(health->inventory.end(), driveList.begin(),
91a85afbe1SWilly Tu                                  driveList.end());
92a85afbe1SWilly Tu 
93a85afbe1SWilly Tu         for (const std::string& drive : driveList)
94a25aeccfSNikhil Potade         {
95a85afbe1SWilly Tu             sdbusplus::message::object_path object(drive);
96a85afbe1SWilly Tu             if (object.filename().empty())
97a25aeccfSNikhil Potade             {
98a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
99a85afbe1SWilly Tu                 return;
100a25aeccfSNikhil Potade             }
101a85afbe1SWilly Tu 
102a85afbe1SWilly Tu             nlohmann::json::object_t driveJson;
103a85afbe1SWilly Tu             driveJson["@odata.id"] =
104be13ceceSJames Feist                 "/redfish/v1/Systems/system/Storage/1/Drives/" +
105a85afbe1SWilly Tu                 object.filename();
106a85afbe1SWilly Tu             driveArray.push_back(std::move(driveJson));
107a25aeccfSNikhil Potade         }
108a25aeccfSNikhil Potade 
109a85afbe1SWilly Tu         count = driveArray.size();
1107a1dbc48SGeorge Liu         });
111a85afbe1SWilly Tu }
112e284a7c1SJames Feist 
113a85afbe1SWilly Tu inline void
114a85afbe1SWilly Tu     getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
115a85afbe1SWilly Tu                           const std::shared_ptr<HealthPopulate>& health)
116a85afbe1SWilly Tu {
117*e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
118*e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.StorageController"};
119*e99073f5SGeorge Liu     dbus::utility::getSubTree(
120*e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
121002d39b4SEd Tanous         [asyncResp,
122*e99073f5SGeorge Liu          health](const boost::system::error_code& ec,
123b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreeResponse& subtree) {
12426f6976fSEd Tanous         if (ec || subtree.empty())
125e284a7c1SJames Feist         {
126d819a420SJames Feist             // doesn't have to be there
127e284a7c1SJames Feist             return;
128e284a7c1SJames Feist         }
129e284a7c1SJames Feist 
130a85afbe1SWilly Tu         nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
131e284a7c1SJames Feist         root = nlohmann::json::array();
132e284a7c1SJames Feist         for (const auto& [path, interfaceDict] : subtree)
133e284a7c1SJames Feist         {
134a85afbe1SWilly Tu             sdbusplus::message::object_path object(path);
135a85afbe1SWilly Tu             std::string id = object.filename();
136a85afbe1SWilly Tu             if (id.empty())
137e284a7c1SJames Feist             {
138a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
139e284a7c1SJames Feist                 return;
140e284a7c1SJames Feist             }
141e284a7c1SJames Feist 
142e284a7c1SJames Feist             if (interfaceDict.size() != 1)
143e284a7c1SJames Feist             {
144a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
145e284a7c1SJames Feist                                  << ", greater than 1";
146e284a7c1SJames Feist                 messages::internalError(asyncResp->res);
147e284a7c1SJames Feist                 return;
148e284a7c1SJames Feist             }
149e284a7c1SJames Feist 
150002d39b4SEd Tanous             const std::string& connectionName = interfaceDict.front().first;
151e284a7c1SJames Feist 
152e284a7c1SJames Feist             size_t index = root.size();
153e284a7c1SJames Feist             nlohmann::json& storageController =
154e284a7c1SJames Feist                 root.emplace_back(nlohmann::json::object());
155e284a7c1SJames Feist 
156e284a7c1SJames Feist             storageController["@odata.type"] =
157e284a7c1SJames Feist                 "#Storage.v1_7_0.StorageController";
158e284a7c1SJames Feist             storageController["@odata.id"] =
1590fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
160e284a7c1SJames Feist                 std::to_string(index);
161e284a7c1SJames Feist             storageController["Name"] = id;
162e284a7c1SJames Feist             storageController["MemberId"] = id;
163e284a7c1SJames Feist             storageController["Status"]["State"] = "Enabled";
164e284a7c1SJames Feist 
1651e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
1661e1e598dSJonathan Doman                 *crow::connections::systemBus, connectionName, path,
1671e1e598dSJonathan Doman                 "xyz.openbmc_project.Inventory.Item", "Present",
168002d39b4SEd Tanous                 [asyncResp, index](const boost::system::error_code ec2,
1691e1e598dSJonathan Doman                                    bool enabled) {
1707e860f15SJohn Edward Broadbent                 // this interface isn't necessary, only check it
1717e860f15SJohn Edward Broadbent                 // if we get a good return
17223a21a1cSEd Tanous                 if (ec2)
173e284a7c1SJames Feist                 {
174e284a7c1SJames Feist                     return;
175e284a7c1SJames Feist                 }
1761e1e598dSJonathan Doman                 if (!enabled)
177e284a7c1SJames Feist                 {
178002d39b4SEd Tanous                     asyncResp->res.jsonValue["StorageControllers"][index]
179a85afbe1SWilly Tu                                             ["Status"]["State"] = "Disabled";
180e284a7c1SJames Feist                 }
1811e1e598dSJonathan Doman                 });
182e284a7c1SJames Feist 
183d1bde9e5SKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
184d1bde9e5SKrzysztof Grobelny                 *crow::connections::systemBus, connectionName, path,
185d1bde9e5SKrzysztof Grobelny                 "xyz.openbmc_project.Inventory.Decorator.Asset",
186a85afbe1SWilly Tu                 [asyncResp, index](
187a85afbe1SWilly Tu                     const boost::system::error_code ec2,
188a85afbe1SWilly Tu                     const std::vector<
189a85afbe1SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
1907e860f15SJohn Edward Broadbent                         propertiesList) {
1917e860f15SJohn Edward Broadbent                 if (ec2)
1927e860f15SJohn Edward Broadbent                 {
1937e860f15SJohn Edward Broadbent                     // this interface isn't necessary
1947e860f15SJohn Edward Broadbent                     return;
1957e860f15SJohn Edward Broadbent                 }
196d1bde9e5SKrzysztof Grobelny 
197d1bde9e5SKrzysztof Grobelny                 const std::string* partNumber = nullptr;
198d1bde9e5SKrzysztof Grobelny                 const std::string* serialNumber = nullptr;
199d1bde9e5SKrzysztof Grobelny                 const std::string* manufacturer = nullptr;
200d1bde9e5SKrzysztof Grobelny                 const std::string* model = nullptr;
201d1bde9e5SKrzysztof Grobelny 
202d1bde9e5SKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
203d1bde9e5SKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), propertiesList,
204d1bde9e5SKrzysztof Grobelny                     "PartNumber", partNumber, "SerialNumber", serialNumber,
205d1bde9e5SKrzysztof Grobelny                     "Manufacturer", manufacturer, "Model", model);
206d1bde9e5SKrzysztof Grobelny 
207d1bde9e5SKrzysztof Grobelny                 if (!success)
2087e860f15SJohn Edward Broadbent                 {
209002d39b4SEd Tanous                     messages::internalError(asyncResp->res);
2107e860f15SJohn Edward Broadbent                     return;
2117e860f15SJohn Edward Broadbent                 }
212d1bde9e5SKrzysztof Grobelny 
213d1bde9e5SKrzysztof Grobelny                 nlohmann::json& controller =
214d1bde9e5SKrzysztof Grobelny                     asyncResp->res.jsonValue["StorageControllers"][index];
215d1bde9e5SKrzysztof Grobelny 
216d1bde9e5SKrzysztof Grobelny                 if (partNumber != nullptr)
217d1bde9e5SKrzysztof Grobelny                 {
218d1bde9e5SKrzysztof Grobelny                     controller["PartNumber"] = *partNumber;
2197e860f15SJohn Edward Broadbent                 }
220d1bde9e5SKrzysztof Grobelny 
221d1bde9e5SKrzysztof Grobelny                 if (serialNumber != nullptr)
222d1bde9e5SKrzysztof Grobelny                 {
223d1bde9e5SKrzysztof Grobelny                     controller["SerialNumber"] = *serialNumber;
2247e860f15SJohn Edward Broadbent                 }
225d1bde9e5SKrzysztof Grobelny 
226d1bde9e5SKrzysztof Grobelny                 if (manufacturer != nullptr)
227d1bde9e5SKrzysztof Grobelny                 {
228d1bde9e5SKrzysztof Grobelny                     controller["Manufacturer"] = *manufacturer;
229d1bde9e5SKrzysztof Grobelny                 }
230d1bde9e5SKrzysztof Grobelny 
231d1bde9e5SKrzysztof Grobelny                 if (model != nullptr)
232d1bde9e5SKrzysztof Grobelny                 {
233d1bde9e5SKrzysztof Grobelny                     controller["Model"] = *model;
234d1bde9e5SKrzysztof Grobelny                 }
235d1bde9e5SKrzysztof Grobelny                 });
2367e860f15SJohn Edward Broadbent         }
2377e860f15SJohn Edward Broadbent 
2387e860f15SJohn Edward Broadbent         // this is done after we know the json array will no longer
2397e860f15SJohn Edward Broadbent         // be resized, as json::array uses vector underneath and we
2407e860f15SJohn Edward Broadbent         // need references to its members that won't change
2417e860f15SJohn Edward Broadbent         size_t count = 0;
242dfababfcSNan Zhou         // Pointer based on |asyncResp->res.jsonValue|
243dfababfcSNan Zhou         nlohmann::json::json_pointer rootPtr =
244dfababfcSNan Zhou             "/StorageControllers"_json_pointer;
2457e860f15SJohn Edward Broadbent         for (const auto& [path, interfaceDict] : subtree)
2467e860f15SJohn Edward Broadbent         {
2477e860f15SJohn Edward Broadbent             auto subHealth = std::make_shared<HealthPopulate>(
248dfababfcSNan Zhou                 asyncResp, rootPtr / count / "Status");
2497e860f15SJohn Edward Broadbent             subHealth->inventory.emplace_back(path);
2507e860f15SJohn Edward Broadbent             health->inventory.emplace_back(path);
2517e860f15SJohn Edward Broadbent             health->children.emplace_back(subHealth);
2527e860f15SJohn Edward Broadbent             count++;
2537e860f15SJohn Edward Broadbent         }
254*e99073f5SGeorge Liu         });
255a85afbe1SWilly Tu }
256a85afbe1SWilly Tu 
257a85afbe1SWilly Tu inline void requestRoutesStorage(App& app)
258a85afbe1SWilly Tu {
259a85afbe1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
260a85afbe1SWilly Tu         .privileges(redfish::privileges::getStorage)
261a85afbe1SWilly Tu         .methods(boost::beast::http::verb::get)(
262a85afbe1SWilly Tu             [&app](const crow::Request& req,
263a85afbe1SWilly Tu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2643ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
265a85afbe1SWilly Tu         {
266a85afbe1SWilly Tu             return;
267a85afbe1SWilly Tu         }
268a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
269a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
270a85afbe1SWilly Tu             "/redfish/v1/Systems/system/Storage/1";
271a85afbe1SWilly Tu         asyncResp->res.jsonValue["Name"] = "Storage";
272a85afbe1SWilly Tu         asyncResp->res.jsonValue["Id"] = "1";
273a85afbe1SWilly Tu         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
274a85afbe1SWilly Tu 
275a85afbe1SWilly Tu         auto health = std::make_shared<HealthPopulate>(asyncResp);
276a85afbe1SWilly Tu         health->populate();
277a85afbe1SWilly Tu 
278a85afbe1SWilly Tu         getDrives(asyncResp, health);
279a85afbe1SWilly Tu         getStorageControllers(asyncResp, health);
2807e860f15SJohn Edward Broadbent         });
2817e860f15SJohn Edward Broadbent }
2827e860f15SJohn Edward Broadbent 
28303913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28403913171SWilly Tu                           const std::string& connectionName,
28503913171SWilly Tu                           const std::string& path)
28603913171SWilly Tu {
287d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
288d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, path,
289d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Asset",
290168e20c1SEd Tanous         [asyncResp](const boost::system::error_code ec,
291168e20c1SEd Tanous                     const std::vector<
292168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
29303913171SWilly Tu                         propertiesList) {
29403913171SWilly Tu         if (ec)
29503913171SWilly Tu         {
29603913171SWilly Tu             // this interface isn't necessary
29703913171SWilly Tu             return;
29803913171SWilly Tu         }
299d1bde9e5SKrzysztof Grobelny 
300d1bde9e5SKrzysztof Grobelny         const std::string* partNumber = nullptr;
301d1bde9e5SKrzysztof Grobelny         const std::string* serialNumber = nullptr;
302d1bde9e5SKrzysztof Grobelny         const std::string* manufacturer = nullptr;
303d1bde9e5SKrzysztof Grobelny         const std::string* model = nullptr;
304d1bde9e5SKrzysztof Grobelny 
305d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
306d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
307d1bde9e5SKrzysztof Grobelny             partNumber, "SerialNumber", serialNumber, "Manufacturer",
308d1bde9e5SKrzysztof Grobelny             manufacturer, "Model", model);
309d1bde9e5SKrzysztof Grobelny 
310d1bde9e5SKrzysztof Grobelny         if (!success)
31103913171SWilly Tu         {
31203913171SWilly Tu             messages::internalError(asyncResp->res);
31303913171SWilly Tu             return;
31403913171SWilly Tu         }
315d1bde9e5SKrzysztof Grobelny 
316d1bde9e5SKrzysztof Grobelny         if (partNumber != nullptr)
317d1bde9e5SKrzysztof Grobelny         {
318d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["PartNumber"] = *partNumber;
31903913171SWilly Tu         }
320d1bde9e5SKrzysztof Grobelny 
321d1bde9e5SKrzysztof Grobelny         if (serialNumber != nullptr)
322d1bde9e5SKrzysztof Grobelny         {
323d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
32403913171SWilly Tu         }
325d1bde9e5SKrzysztof Grobelny 
326d1bde9e5SKrzysztof Grobelny         if (manufacturer != nullptr)
327d1bde9e5SKrzysztof Grobelny         {
328d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
329d1bde9e5SKrzysztof Grobelny         }
330d1bde9e5SKrzysztof Grobelny 
331d1bde9e5SKrzysztof Grobelny         if (model != nullptr)
332d1bde9e5SKrzysztof Grobelny         {
333d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Model"] = *model;
334d1bde9e5SKrzysztof Grobelny         }
335d1bde9e5SKrzysztof Grobelny         });
33603913171SWilly Tu }
33703913171SWilly Tu 
33803913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
33903913171SWilly Tu                             const std::string& connectionName,
34003913171SWilly Tu                             const std::string& path)
34103913171SWilly Tu {
3421e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3431e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3441e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
34503913171SWilly Tu         [asyncResp, path](const boost::system::error_code ec,
3461e1e598dSJonathan Doman                           const bool enabled) {
34703913171SWilly Tu         // this interface isn't necessary, only check it if
34803913171SWilly Tu         // we get a good return
34903913171SWilly Tu         if (ec)
35003913171SWilly Tu         {
35103913171SWilly Tu             return;
35203913171SWilly Tu         }
35303913171SWilly Tu 
3541e1e598dSJonathan Doman         if (!enabled)
35503913171SWilly Tu         {
35603913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
35703913171SWilly Tu         }
3581e1e598dSJonathan Doman         });
35903913171SWilly Tu }
36003913171SWilly Tu 
36103913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36203913171SWilly Tu                           const std::string& connectionName,
36303913171SWilly Tu                           const std::string& path)
36403913171SWilly Tu {
3651e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3661e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3671e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3681e1e598dSJonathan Doman         [asyncResp](const boost::system::error_code ec, const bool updating) {
36903913171SWilly Tu         // this interface isn't necessary, only check it
37003913171SWilly Tu         // if we get a good return
37103913171SWilly Tu         if (ec)
37203913171SWilly Tu         {
37303913171SWilly Tu             return;
37403913171SWilly Tu         }
37503913171SWilly Tu 
37603913171SWilly Tu         // updating and disabled in the backend shouldn't be
37703913171SWilly Tu         // able to be set at the same time, so we don't need
37803913171SWilly Tu         // to check for the race condition of these two
37903913171SWilly Tu         // calls
3801e1e598dSJonathan Doman         if (updating)
38103913171SWilly Tu         {
38203913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Updating";
38303913171SWilly Tu         }
3841e1e598dSJonathan Doman         });
38503913171SWilly Tu }
38603913171SWilly Tu 
38719b8e9a0SWilly Tu inline std::optional<std::string> convertDriveType(const std::string& type)
38819b8e9a0SWilly Tu {
38919b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
39019b8e9a0SWilly Tu     {
39119b8e9a0SWilly Tu         return "HDD";
39219b8e9a0SWilly Tu     }
39319b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
39419b8e9a0SWilly Tu     {
39519b8e9a0SWilly Tu         return "SSD";
39619b8e9a0SWilly Tu     }
39719b8e9a0SWilly Tu 
39819b8e9a0SWilly Tu     return std::nullopt;
39919b8e9a0SWilly Tu }
40019b8e9a0SWilly Tu 
40119b8e9a0SWilly Tu inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
40219b8e9a0SWilly Tu {
40319b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
40419b8e9a0SWilly Tu     {
40519b8e9a0SWilly Tu         return "SAS";
40619b8e9a0SWilly Tu     }
40719b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
40819b8e9a0SWilly Tu     {
40919b8e9a0SWilly Tu         return "SATA";
41019b8e9a0SWilly Tu     }
41119b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
41219b8e9a0SWilly Tu     {
41319b8e9a0SWilly Tu         return "NVMe";
41419b8e9a0SWilly Tu     }
41519b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
41619b8e9a0SWilly Tu     {
41719b8e9a0SWilly Tu         return "FC";
41819b8e9a0SWilly Tu     }
41919b8e9a0SWilly Tu 
42019b8e9a0SWilly Tu     return std::nullopt;
42119b8e9a0SWilly Tu }
42219b8e9a0SWilly Tu 
42319b8e9a0SWilly Tu inline void
42419b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
42519b8e9a0SWilly Tu                            const std::string& connectionName,
42619b8e9a0SWilly Tu                            const std::string& path)
42719b8e9a0SWilly Tu {
42819b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
42919b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
43019b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
43119b8e9a0SWilly Tu         [asyncResp](const boost::system::error_code ec,
43219b8e9a0SWilly Tu                     const std::vector<
43319b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
43419b8e9a0SWilly Tu                         propertiesList) {
43519b8e9a0SWilly Tu         if (ec)
43619b8e9a0SWilly Tu         {
43719b8e9a0SWilly Tu             // this interface isn't required
43819b8e9a0SWilly Tu             return;
43919b8e9a0SWilly Tu         }
44019b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
44119b8e9a0SWilly Tu                  property : propertiesList)
44219b8e9a0SWilly Tu         {
44319b8e9a0SWilly Tu             const std::string& propertyName = property.first;
44419b8e9a0SWilly Tu             if (propertyName == "Type")
44519b8e9a0SWilly Tu             {
44619b8e9a0SWilly Tu                 const std::string* value =
44719b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
44819b8e9a0SWilly Tu                 if (value == nullptr)
44919b8e9a0SWilly Tu                 {
45019b8e9a0SWilly Tu                     // illegal property
45119b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Type";
45219b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
45319b8e9a0SWilly Tu                     return;
45419b8e9a0SWilly Tu                 }
45519b8e9a0SWilly Tu 
456002d39b4SEd Tanous                 std::optional<std::string> mediaType = convertDriveType(*value);
45719b8e9a0SWilly Tu                 if (!mediaType)
45819b8e9a0SWilly Tu                 {
45919b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
46019b8e9a0SWilly Tu                                      << *value;
46119b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
46219b8e9a0SWilly Tu                     return;
46319b8e9a0SWilly Tu                 }
46419b8e9a0SWilly Tu 
46519b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
46619b8e9a0SWilly Tu             }
46719b8e9a0SWilly Tu             else if (propertyName == "Capacity")
46819b8e9a0SWilly Tu             {
46919b8e9a0SWilly Tu                 const uint64_t* capacity =
47019b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
47119b8e9a0SWilly Tu                 if (capacity == nullptr)
47219b8e9a0SWilly Tu                 {
47319b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Capacity";
47419b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
47519b8e9a0SWilly Tu                     return;
47619b8e9a0SWilly Tu                 }
47719b8e9a0SWilly Tu                 if (*capacity == 0)
47819b8e9a0SWilly Tu                 {
47919b8e9a0SWilly Tu                     // drive capacity not known
48019b8e9a0SWilly Tu                     continue;
48119b8e9a0SWilly Tu                 }
48219b8e9a0SWilly Tu 
48319b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
48419b8e9a0SWilly Tu             }
48519b8e9a0SWilly Tu             else if (propertyName == "Protocol")
48619b8e9a0SWilly Tu             {
48719b8e9a0SWilly Tu                 const std::string* value =
48819b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
48919b8e9a0SWilly Tu                 if (value == nullptr)
49019b8e9a0SWilly Tu                 {
49119b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Protocol";
49219b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
49319b8e9a0SWilly Tu                     return;
49419b8e9a0SWilly Tu                 }
49519b8e9a0SWilly Tu 
496002d39b4SEd Tanous                 std::optional<std::string> proto = convertDriveProtocol(*value);
49719b8e9a0SWilly Tu                 if (!proto)
49819b8e9a0SWilly Tu                 {
499002d39b4SEd Tanous                     BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: "
50019b8e9a0SWilly Tu                                      << *value;
50119b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
50219b8e9a0SWilly Tu                     return;
50319b8e9a0SWilly Tu                 }
50419b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
50519b8e9a0SWilly Tu             }
5063fe4d5ccSJohn Edward Broadbent             else if (propertyName == "PredictedMediaLifeLeftPercent")
5073fe4d5ccSJohn Edward Broadbent             {
5083fe4d5ccSJohn Edward Broadbent                 const uint8_t* lifeLeft =
5093fe4d5ccSJohn Edward Broadbent                     std::get_if<uint8_t>(&property.second);
5103fe4d5ccSJohn Edward Broadbent                 if (lifeLeft == nullptr)
5113fe4d5ccSJohn Edward Broadbent                 {
5123fe4d5ccSJohn Edward Broadbent                     BMCWEB_LOG_ERROR
5133fe4d5ccSJohn Edward Broadbent                         << "Illegal property: PredictedMediaLifeLeftPercent";
5143fe4d5ccSJohn Edward Broadbent                     messages::internalError(asyncResp->res);
5153fe4d5ccSJohn Edward Broadbent                     return;
5163fe4d5ccSJohn Edward Broadbent                 }
5173fe4d5ccSJohn Edward Broadbent                 // 255 means reading the value is not supported
5183fe4d5ccSJohn Edward Broadbent                 if (*lifeLeft != 255)
5193fe4d5ccSJohn Edward Broadbent                 {
5203fe4d5ccSJohn Edward Broadbent                     asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
5213fe4d5ccSJohn Edward Broadbent                         *lifeLeft;
5223fe4d5ccSJohn Edward Broadbent                 }
5233fe4d5ccSJohn Edward Broadbent             }
52419b8e9a0SWilly Tu         }
52519b8e9a0SWilly Tu         });
52619b8e9a0SWilly Tu }
52719b8e9a0SWilly Tu 
528b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
529b53dcd9dSNan Zhou                             const std::string& connectionName,
530b53dcd9dSNan Zhou                             const std::string& path,
531e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
532e56ed6b9SJohn Edward Broadbent {
533e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
534e56ed6b9SJohn Edward Broadbent     {
535e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
536e56ed6b9SJohn Edward Broadbent         {
537e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
538e56ed6b9SJohn Edward Broadbent         }
539e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
540e56ed6b9SJohn Edward Broadbent         {
541e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
542e56ed6b9SJohn Edward Broadbent         }
543e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
544e56ed6b9SJohn Edward Broadbent         {
545e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
546e56ed6b9SJohn Edward Broadbent         }
547e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
548e56ed6b9SJohn Edward Broadbent         {
549e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
550e56ed6b9SJohn Edward Broadbent         }
551e56ed6b9SJohn Edward Broadbent     }
552e56ed6b9SJohn Edward Broadbent }
553e56ed6b9SJohn Edward Broadbent 
5547e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
5557e860f15SJohn Edward Broadbent {
55622d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
557ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
558002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
559002d39b4SEd Tanous             [&app](const crow::Request& req,
56045ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
56122d268cbSEd Tanous                    const std::string& systemName, const std::string& driveId) {
5623ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
56345ca1b86SEd Tanous         {
56445ca1b86SEd Tanous             return;
56545ca1b86SEd Tanous         }
56622d268cbSEd Tanous         if (systemName != "system")
56722d268cbSEd Tanous         {
56822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
56922d268cbSEd Tanous                                        systemName);
57022d268cbSEd Tanous             return;
57122d268cbSEd Tanous         }
57222d268cbSEd Tanous 
573*e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
574*e99073f5SGeorge Liu             "xyz.openbmc_project.Inventory.Item.Drive"};
575*e99073f5SGeorge Liu         dbus::utility::getSubTree(
576*e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, interfaces,
577002d39b4SEd Tanous             [asyncResp,
578*e99073f5SGeorge Liu              driveId](const boost::system::error_code& ec,
579b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
5807e860f15SJohn Edward Broadbent             if (ec)
5817e860f15SJohn Edward Broadbent             {
5827e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Drive mapper call error";
5837e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
5847e860f15SJohn Edward Broadbent                 return;
5857e860f15SJohn Edward Broadbent             }
5867e860f15SJohn Edward Broadbent 
58703913171SWilly Tu             auto drive = std::find_if(
5887e860f15SJohn Edward Broadbent                 subtree.begin(), subtree.end(),
589002d39b4SEd Tanous                 [&driveId](
5908cb65f8aSNan Zhou                     const std::pair<std::string,
5918cb65f8aSNan Zhou                                     dbus::utility::MapperServiceMap>& object) {
59203913171SWilly Tu                 return sdbusplus::message::object_path(object.first)
59303913171SWilly Tu                            .filename() == driveId;
5947e860f15SJohn Edward Broadbent                 });
5957e860f15SJohn Edward Broadbent 
59603913171SWilly Tu             if (drive == subtree.end())
5977e860f15SJohn Edward Broadbent             {
598002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
5997e860f15SJohn Edward Broadbent                 return;
6007e860f15SJohn Edward Broadbent             }
6017e860f15SJohn Edward Broadbent 
60203913171SWilly Tu             const std::string& path = drive->first;
6038cb65f8aSNan Zhou             const dbus::utility::MapperServiceMap& connectionNames =
6048cb65f8aSNan Zhou                 drive->second;
6057e860f15SJohn Edward Broadbent 
606002d39b4SEd Tanous             asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
6077e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
608002d39b4SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId;
6097e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = driveId;
6107e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = driveId;
6117e860f15SJohn Edward Broadbent 
6127e860f15SJohn Edward Broadbent             if (connectionNames.size() != 1)
6137e860f15SJohn Edward Broadbent             {
614002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
61503913171SWilly Tu                                  << ", not equal to 1";
6167e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
6177e860f15SJohn Edward Broadbent                 return;
6187e860f15SJohn Edward Broadbent             }
6197e860f15SJohn Edward Broadbent 
6207e860f15SJohn Edward Broadbent             getMainChassisId(
621002d39b4SEd Tanous                 asyncResp, [](const std::string& chassisId,
6227e860f15SJohn Edward Broadbent                               const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
623002d39b4SEd Tanous                     aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
6241476687dSEd Tanous                         "/redfish/v1/Chassis/" + chassisId;
6257e860f15SJohn Edward Broadbent                 });
6267e860f15SJohn Edward Broadbent 
627a25aeccfSNikhil Potade             // default it to Enabled
628a25aeccfSNikhil Potade             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
629a25aeccfSNikhil Potade 
6302ad9c2f6SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
631e284a7c1SJames Feist             health->inventory.emplace_back(path);
6322ad9c2f6SJames Feist             health->populate();
6332ad9c2f6SJames Feist 
634e56ed6b9SJohn Edward Broadbent             addAllDriveInfo(asyncResp, connectionNames[0].first, path,
635e56ed6b9SJohn Edward Broadbent                             connectionNames[0].second);
636*e99073f5SGeorge Liu             });
6377e860f15SJohn Edward Broadbent         });
638a25aeccfSNikhil Potade }
63992903bd4SJohn Edward Broadbent 
64092903bd4SJohn Edward Broadbent /**
64192903bd4SJohn Edward Broadbent  * Chassis drives, this URL will show all the DriveCollection
64292903bd4SJohn Edward Broadbent  * information
64392903bd4SJohn Edward Broadbent  */
644b53dcd9dSNan Zhou inline void chassisDriveCollectionGet(
64592903bd4SJohn Edward Broadbent     crow::App& app, const crow::Request& req,
64692903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
64792903bd4SJohn Edward Broadbent     const std::string& chassisId)
64892903bd4SJohn Edward Broadbent {
6493ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
65092903bd4SJohn Edward Broadbent     {
65192903bd4SJohn Edward Broadbent         return;
65292903bd4SJohn Edward Broadbent     }
65392903bd4SJohn Edward Broadbent 
65492903bd4SJohn Edward Broadbent     // mapper call lambda
655*e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
656*e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Board",
657*e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Chassis"};
658*e99073f5SGeorge Liu     dbus::utility::getSubTree(
659*e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
66092903bd4SJohn Edward Broadbent         [asyncResp,
661*e99073f5SGeorge Liu          chassisId](const boost::system::error_code& ec,
66292903bd4SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
66392903bd4SJohn Edward Broadbent         if (ec)
66492903bd4SJohn Edward Broadbent         {
66592903bd4SJohn Edward Broadbent             if (ec == boost::system::errc::host_unreachable)
66692903bd4SJohn Edward Broadbent             {
66792903bd4SJohn Edward Broadbent                 messages::resourceNotFound(asyncResp->res, "Chassis",
66892903bd4SJohn Edward Broadbent                                            chassisId);
66992903bd4SJohn Edward Broadbent                 return;
67092903bd4SJohn Edward Broadbent             }
67192903bd4SJohn Edward Broadbent             messages::internalError(asyncResp->res);
67292903bd4SJohn Edward Broadbent             return;
67392903bd4SJohn Edward Broadbent         }
67492903bd4SJohn Edward Broadbent 
67592903bd4SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
6768cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
67792903bd4SJohn Edward Broadbent         {
67892903bd4SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
67992903bd4SJohn Edward Broadbent             if (objPath.filename() != chassisId)
68092903bd4SJohn Edward Broadbent             {
68192903bd4SJohn Edward Broadbent                 continue;
68292903bd4SJohn Edward Broadbent             }
68392903bd4SJohn Edward Broadbent 
68492903bd4SJohn Edward Broadbent             if (connectionNames.empty())
68592903bd4SJohn Edward Broadbent             {
68692903bd4SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
68792903bd4SJohn Edward Broadbent                 continue;
68892903bd4SJohn Edward Broadbent             }
68992903bd4SJohn Edward Broadbent 
69092903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.type"] =
69192903bd4SJohn Edward Broadbent                 "#DriveCollection.DriveCollection";
69292903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
69314bd7d9aSJohn Edward Broadbent                 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
69414bd7d9aSJohn Edward Broadbent                                              chassisId, "Drives");
69592903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "Drive Collection";
69692903bd4SJohn Edward Broadbent 
69792903bd4SJohn Edward Broadbent             // Association lambda
69892903bd4SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
69992903bd4SJohn Edward Broadbent                 *crow::connections::systemBus,
70092903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
70192903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
70292903bd4SJohn Edward Broadbent                 [asyncResp, chassisId](const boost::system::error_code ec3,
70392903bd4SJohn Edward Broadbent                                        const std::vector<std::string>& resp) {
70492903bd4SJohn Edward Broadbent                 if (ec3)
70592903bd4SJohn Edward Broadbent                 {
70692903bd4SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
70792903bd4SJohn Edward Broadbent                 }
70892903bd4SJohn Edward Broadbent                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
70992903bd4SJohn Edward Broadbent                 // important if array is empty
71092903bd4SJohn Edward Broadbent                 members = nlohmann::json::array();
71192903bd4SJohn Edward Broadbent 
71292903bd4SJohn Edward Broadbent                 std::vector<std::string> leafNames;
71392903bd4SJohn Edward Broadbent                 for (const auto& drive : resp)
71492903bd4SJohn Edward Broadbent                 {
7158a592810SEd Tanous                     sdbusplus::message::object_path drivePath(drive);
7168a592810SEd Tanous                     leafNames.push_back(drivePath.filename());
71792903bd4SJohn Edward Broadbent                 }
71892903bd4SJohn Edward Broadbent 
71992903bd4SJohn Edward Broadbent                 std::sort(leafNames.begin(), leafNames.end(),
72092903bd4SJohn Edward Broadbent                           AlphanumLess<std::string>());
72192903bd4SJohn Edward Broadbent 
72292903bd4SJohn Edward Broadbent                 for (const auto& leafName : leafNames)
72392903bd4SJohn Edward Broadbent                 {
72492903bd4SJohn Edward Broadbent                     nlohmann::json::object_t member;
72592903bd4SJohn Edward Broadbent                     member["@odata.id"] = crow::utility::urlFromPieces(
72692903bd4SJohn Edward Broadbent                         "redfish", "v1", "Chassis", chassisId, "Drives",
72792903bd4SJohn Edward Broadbent                         leafName);
72892903bd4SJohn Edward Broadbent                     members.push_back(std::move(member));
72992903bd4SJohn Edward Broadbent                     // navigation links will be registered in next patch set
73092903bd4SJohn Edward Broadbent                 }
73192903bd4SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
73292903bd4SJohn Edward Broadbent                 }); // end association lambda
73392903bd4SJohn Edward Broadbent 
73492903bd4SJohn Edward Broadbent         } // end Iterate over all retrieved ObjectPaths
735*e99073f5SGeorge Liu         });
73692903bd4SJohn Edward Broadbent }
73792903bd4SJohn Edward Broadbent 
73892903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
73992903bd4SJohn Edward Broadbent {
74092903bd4SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
74192903bd4SJohn Edward Broadbent         .privileges(redfish::privileges::getDriveCollection)
74292903bd4SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
74392903bd4SJohn Edward Broadbent             std::bind_front(chassisDriveCollectionGet, std::ref(app)));
74492903bd4SJohn Edward Broadbent }
74592903bd4SJohn Edward Broadbent 
746b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
747b53dcd9dSNan Zhou                        const std::string& chassisId,
748b53dcd9dSNan Zhou                        const std::string& driveName,
749e56ed6b9SJohn Edward Broadbent                        const boost::system::error_code ec,
750e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
751e56ed6b9SJohn Edward Broadbent {
752e56ed6b9SJohn Edward Broadbent 
753e56ed6b9SJohn Edward Broadbent     if (ec)
754e56ed6b9SJohn Edward Broadbent     {
755e56ed6b9SJohn Edward Broadbent         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
756e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
757e56ed6b9SJohn Edward Broadbent         return;
758e56ed6b9SJohn Edward Broadbent     }
759e56ed6b9SJohn Edward Broadbent 
760e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
7618cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
762e56ed6b9SJohn Edward Broadbent     {
763e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
764e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
765e56ed6b9SJohn Edward Broadbent         {
766e56ed6b9SJohn Edward Broadbent             continue;
767e56ed6b9SJohn Edward Broadbent         }
768e56ed6b9SJohn Edward Broadbent 
769e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
770e56ed6b9SJohn Edward Broadbent         {
771e56ed6b9SJohn Edward Broadbent             BMCWEB_LOG_ERROR << "Got 0 Connection names";
772e56ed6b9SJohn Edward Broadbent             continue;
773e56ed6b9SJohn Edward Broadbent         }
774e56ed6b9SJohn Edward Broadbent 
775e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
776a0cb40cbSJohn Edward Broadbent             "redfish", "v1", "Chassis", chassisId, "Drives", driveName);
777e56ed6b9SJohn Edward Broadbent 
778e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
779a0cb40cbSJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = driveName;
780e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
781e56ed6b9SJohn Edward Broadbent         // default it to Enabled
782e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
783e56ed6b9SJohn Edward Broadbent 
784e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
785e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
786e56ed6b9SJohn Edward Broadbent             crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId);
787e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
788e56ed6b9SJohn Edward Broadbent 
789e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
790e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
791e56ed6b9SJohn Edward Broadbent     }
792e56ed6b9SJohn Edward Broadbent }
793e56ed6b9SJohn Edward Broadbent 
794b53dcd9dSNan Zhou inline void
795b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
796e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
797e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
798e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
799e56ed6b9SJohn Edward Broadbent {
800e56ed6b9SJohn Edward Broadbent 
801e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
802e56ed6b9SJohn Edward Broadbent     {
803e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
804e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
805e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
806e56ed6b9SJohn Edward Broadbent         {
807e56ed6b9SJohn Edward Broadbent             continue;
808e56ed6b9SJohn Edward Broadbent         }
809e56ed6b9SJohn Edward Broadbent         //  mapper call drive
810*e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> driveInterface = {
811e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
812*e99073f5SGeorge Liu         dbus::utility::getSubTree(
813*e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, driveInterface,
814e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
815*e99073f5SGeorge Liu                 const boost::system::error_code& ec,
816e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
817e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
818*e99073f5SGeorge Liu             });
819e56ed6b9SJohn Edward Broadbent     }
820e56ed6b9SJohn Edward Broadbent }
821e56ed6b9SJohn Edward Broadbent 
822b53dcd9dSNan Zhou inline void
823b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
824e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
825e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
826e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
827e56ed6b9SJohn Edward Broadbent {
82803810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
829e56ed6b9SJohn Edward Broadbent     {
830e56ed6b9SJohn Edward Broadbent         return;
831e56ed6b9SJohn Edward Broadbent     }
832*e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
833e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
834e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
835e56ed6b9SJohn Edward Broadbent 
836e56ed6b9SJohn Edward Broadbent     // mapper call chassis
837*e99073f5SGeorge Liu     dbus::utility::getSubTree(
838*e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
839e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
840*e99073f5SGeorge Liu          driveName](const boost::system::error_code& ec,
841e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
842e56ed6b9SJohn Edward Broadbent         if (ec)
843e56ed6b9SJohn Edward Broadbent         {
844e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
845e56ed6b9SJohn Edward Broadbent             return;
846e56ed6b9SJohn Edward Broadbent         }
847e56ed6b9SJohn Edward Broadbent 
848e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
8498cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
850e56ed6b9SJohn Edward Broadbent         {
851e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
852e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
853e56ed6b9SJohn Edward Broadbent             {
854e56ed6b9SJohn Edward Broadbent                 continue;
855e56ed6b9SJohn Edward Broadbent             }
856e56ed6b9SJohn Edward Broadbent 
857e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
858e56ed6b9SJohn Edward Broadbent             {
859e56ed6b9SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
860e56ed6b9SJohn Edward Broadbent                 continue;
861e56ed6b9SJohn Edward Broadbent             }
862e56ed6b9SJohn Edward Broadbent 
863e56ed6b9SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
864e56ed6b9SJohn Edward Broadbent                 *crow::connections::systemBus,
865e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
866e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
867e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
868e56ed6b9SJohn Edward Broadbent                  driveName](const boost::system::error_code ec3,
869e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& resp) {
870e56ed6b9SJohn Edward Broadbent                 if (ec3)
871e56ed6b9SJohn Edward Broadbent                 {
872e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
873e56ed6b9SJohn Edward Broadbent                 }
874e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
875e56ed6b9SJohn Edward Broadbent                 });
876e56ed6b9SJohn Edward Broadbent             break;
877e56ed6b9SJohn Edward Broadbent         }
878*e99073f5SGeorge Liu         });
879e56ed6b9SJohn Edward Broadbent }
880e56ed6b9SJohn Edward Broadbent 
881e56ed6b9SJohn Edward Broadbent /**
882e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
883e56ed6b9SJohn Edward Broadbent  */
884e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
885e56ed6b9SJohn Edward Broadbent {
886e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
887e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
888e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
889e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
890e56ed6b9SJohn Edward Broadbent }
891e56ed6b9SJohn Edward Broadbent 
892a25aeccfSNikhil Potade } // namespace redfish
893