xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision 22d268cb2c0bc00676d08c79f6ab8958bee74a25)
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 
182ad9c2f6SJames Feist #include "health.hpp"
19e284a7c1SJames Feist #include "openbmc_dbus_rest.hpp"
202ad9c2f6SJames Feist 
217e860f15SJohn Edward Broadbent #include <app.hpp>
22168e20c1SEd Tanous #include <dbus_utility.hpp>
2345ca1b86SEd Tanous #include <query.hpp>
24ed398213SEd Tanous #include <registries/privilege_registry.hpp>
251e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
26d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
27d1bde9e5SKrzysztof Grobelny #include <utils/dbus_utils.hpp>
28a25aeccfSNikhil Potade 
29a25aeccfSNikhil Potade namespace redfish
30a25aeccfSNikhil Potade {
317e860f15SJohn Edward Broadbent inline void requestRoutesStorageCollection(App& app)
32a25aeccfSNikhil Potade {
33*22d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
34ed398213SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
357e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3645ca1b86SEd Tanous             [&app](const crow::Request& req,
37*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
38*22d268cbSEd Tanous                    const std::string& systemName) {
393ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
4045ca1b86SEd Tanous         {
4145ca1b86SEd Tanous             return;
4245ca1b86SEd Tanous         }
43*22d268cbSEd Tanous         if (systemName != "system")
44*22d268cbSEd Tanous         {
45*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
46*22d268cbSEd Tanous                                        systemName);
47*22d268cbSEd Tanous             return;
48*22d268cbSEd Tanous         }
49*22d268cbSEd Tanous 
508d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
518d1b46d7Szhanghch05             "#StorageCollection.StorageCollection";
528d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
538d1b46d7Szhanghch05             "/redfish/v1/Systems/system/Storage";
548d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Storage Collection";
551476687dSEd Tanous         nlohmann::json::array_t members;
561476687dSEd Tanous         nlohmann::json::object_t member;
571476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
581476687dSEd Tanous         members.emplace_back(member);
591476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
608d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
617e860f15SJohn Edward Broadbent         });
62a25aeccfSNikhil Potade }
63a25aeccfSNikhil Potade 
64a85afbe1SWilly Tu inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
65a85afbe1SWilly Tu                       const std::shared_ptr<HealthPopulate>& health)
66a25aeccfSNikhil Potade {
67a25aeccfSNikhil Potade     crow::connections::systemBus->async_method_call(
68a85afbe1SWilly Tu         [asyncResp, health](
69a85afbe1SWilly Tu             const boost::system::error_code ec,
70a85afbe1SWilly Tu             const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
71a25aeccfSNikhil Potade         if (ec)
72a25aeccfSNikhil Potade         {
73a25aeccfSNikhil Potade             BMCWEB_LOG_ERROR << "Drive mapper call error";
74a25aeccfSNikhil Potade             messages::internalError(asyncResp->res);
75a25aeccfSNikhil Potade             return;
76a25aeccfSNikhil Potade         }
772ad9c2f6SJames Feist 
78a85afbe1SWilly Tu         nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
79a85afbe1SWilly Tu         driveArray = nlohmann::json::array();
80a85afbe1SWilly Tu         auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
81a85afbe1SWilly Tu         count = 0;
822ad9c2f6SJames Feist 
83a85afbe1SWilly Tu         health->inventory.insert(health->inventory.end(), driveList.begin(),
84a85afbe1SWilly Tu                                  driveList.end());
85a85afbe1SWilly Tu 
86a85afbe1SWilly Tu         for (const std::string& drive : driveList)
87a25aeccfSNikhil Potade         {
88a85afbe1SWilly Tu             sdbusplus::message::object_path object(drive);
89a85afbe1SWilly Tu             if (object.filename().empty())
90a25aeccfSNikhil Potade             {
91a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
92a85afbe1SWilly Tu                 return;
93a25aeccfSNikhil Potade             }
94a85afbe1SWilly Tu 
95a85afbe1SWilly Tu             nlohmann::json::object_t driveJson;
96a85afbe1SWilly Tu             driveJson["@odata.id"] =
97be13ceceSJames Feist                 "/redfish/v1/Systems/system/Storage/1/Drives/" +
98a85afbe1SWilly Tu                 object.filename();
99a85afbe1SWilly Tu             driveArray.push_back(std::move(driveJson));
100a25aeccfSNikhil Potade         }
101a25aeccfSNikhil Potade 
102a85afbe1SWilly Tu         count = driveArray.size();
103a25aeccfSNikhil Potade         },
104a25aeccfSNikhil Potade         "xyz.openbmc_project.ObjectMapper",
105a25aeccfSNikhil Potade         "/xyz/openbmc_project/object_mapper",
106a25aeccfSNikhil Potade         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
107a25aeccfSNikhil Potade         "/xyz/openbmc_project/inventory", int32_t(0),
108a85afbe1SWilly Tu         std::array<const char*, 1>{"xyz.openbmc_project.Inventory.Item.Drive"});
109a85afbe1SWilly Tu }
110e284a7c1SJames Feist 
111a85afbe1SWilly Tu inline void
112a85afbe1SWilly Tu     getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
113a85afbe1SWilly Tu                           const std::shared_ptr<HealthPopulate>& health)
114a85afbe1SWilly Tu {
115e284a7c1SJames Feist     crow::connections::systemBus->async_method_call(
116002d39b4SEd Tanous         [asyncResp,
117002d39b4SEd Tanous          health](const boost::system::error_code ec,
118b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreeResponse& subtree) {
11926f6976fSEd Tanous         if (ec || subtree.empty())
120e284a7c1SJames Feist         {
121d819a420SJames Feist             // doesn't have to be there
122e284a7c1SJames Feist             return;
123e284a7c1SJames Feist         }
124e284a7c1SJames Feist 
125a85afbe1SWilly Tu         nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
126e284a7c1SJames Feist         root = nlohmann::json::array();
127e284a7c1SJames Feist         for (const auto& [path, interfaceDict] : subtree)
128e284a7c1SJames Feist         {
129a85afbe1SWilly Tu             sdbusplus::message::object_path object(path);
130a85afbe1SWilly Tu             std::string id = object.filename();
131a85afbe1SWilly Tu             if (id.empty())
132e284a7c1SJames Feist             {
133a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
134e284a7c1SJames Feist                 return;
135e284a7c1SJames Feist             }
136e284a7c1SJames Feist 
137e284a7c1SJames Feist             if (interfaceDict.size() != 1)
138e284a7c1SJames Feist             {
139a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
140e284a7c1SJames Feist                                  << ", greater than 1";
141e284a7c1SJames Feist                 messages::internalError(asyncResp->res);
142e284a7c1SJames Feist                 return;
143e284a7c1SJames Feist             }
144e284a7c1SJames Feist 
145002d39b4SEd Tanous             const std::string& connectionName = interfaceDict.front().first;
146e284a7c1SJames Feist 
147e284a7c1SJames Feist             size_t index = root.size();
148e284a7c1SJames Feist             nlohmann::json& storageController =
149e284a7c1SJames Feist                 root.emplace_back(nlohmann::json::object());
150e284a7c1SJames Feist 
151e284a7c1SJames Feist             storageController["@odata.type"] =
152e284a7c1SJames Feist                 "#Storage.v1_7_0.StorageController";
153e284a7c1SJames Feist             storageController["@odata.id"] =
1540fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
155e284a7c1SJames Feist                 std::to_string(index);
156e284a7c1SJames Feist             storageController["Name"] = id;
157e284a7c1SJames Feist             storageController["MemberId"] = id;
158e284a7c1SJames Feist             storageController["Status"]["State"] = "Enabled";
159e284a7c1SJames Feist 
1601e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
1611e1e598dSJonathan Doman                 *crow::connections::systemBus, connectionName, path,
1621e1e598dSJonathan Doman                 "xyz.openbmc_project.Inventory.Item", "Present",
163002d39b4SEd Tanous                 [asyncResp, index](const boost::system::error_code ec2,
1641e1e598dSJonathan Doman                                    bool enabled) {
1657e860f15SJohn Edward Broadbent                 // this interface isn't necessary, only check it
1667e860f15SJohn Edward Broadbent                 // if we get a good return
16723a21a1cSEd Tanous                 if (ec2)
168e284a7c1SJames Feist                 {
169e284a7c1SJames Feist                     return;
170e284a7c1SJames Feist                 }
1711e1e598dSJonathan Doman                 if (!enabled)
172e284a7c1SJames Feist                 {
173002d39b4SEd Tanous                     asyncResp->res.jsonValue["StorageControllers"][index]
174a85afbe1SWilly Tu                                             ["Status"]["State"] = "Disabled";
175e284a7c1SJames Feist                 }
1761e1e598dSJonathan Doman                 });
177e284a7c1SJames Feist 
178d1bde9e5SKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
179d1bde9e5SKrzysztof Grobelny                 *crow::connections::systemBus, connectionName, path,
180d1bde9e5SKrzysztof Grobelny                 "xyz.openbmc_project.Inventory.Decorator.Asset",
181a85afbe1SWilly Tu                 [asyncResp, index](
182a85afbe1SWilly Tu                     const boost::system::error_code ec2,
183a85afbe1SWilly Tu                     const std::vector<
184a85afbe1SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
1857e860f15SJohn Edward Broadbent                         propertiesList) {
1867e860f15SJohn Edward Broadbent                 if (ec2)
1877e860f15SJohn Edward Broadbent                 {
1887e860f15SJohn Edward Broadbent                     // this interface isn't necessary
1897e860f15SJohn Edward Broadbent                     return;
1907e860f15SJohn Edward Broadbent                 }
191d1bde9e5SKrzysztof Grobelny 
192d1bde9e5SKrzysztof Grobelny                 const std::string* partNumber = nullptr;
193d1bde9e5SKrzysztof Grobelny                 const std::string* serialNumber = nullptr;
194d1bde9e5SKrzysztof Grobelny                 const std::string* manufacturer = nullptr;
195d1bde9e5SKrzysztof Grobelny                 const std::string* model = nullptr;
196d1bde9e5SKrzysztof Grobelny 
197d1bde9e5SKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
198d1bde9e5SKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), propertiesList,
199d1bde9e5SKrzysztof Grobelny                     "PartNumber", partNumber, "SerialNumber", serialNumber,
200d1bde9e5SKrzysztof Grobelny                     "Manufacturer", manufacturer, "Model", model);
201d1bde9e5SKrzysztof Grobelny 
202d1bde9e5SKrzysztof Grobelny                 if (!success)
2037e860f15SJohn Edward Broadbent                 {
204002d39b4SEd Tanous                     messages::internalError(asyncResp->res);
2057e860f15SJohn Edward Broadbent                     return;
2067e860f15SJohn Edward Broadbent                 }
207d1bde9e5SKrzysztof Grobelny 
208d1bde9e5SKrzysztof Grobelny                 nlohmann::json& controller =
209d1bde9e5SKrzysztof Grobelny                     asyncResp->res.jsonValue["StorageControllers"][index];
210d1bde9e5SKrzysztof Grobelny 
211d1bde9e5SKrzysztof Grobelny                 if (partNumber != nullptr)
212d1bde9e5SKrzysztof Grobelny                 {
213d1bde9e5SKrzysztof Grobelny                     controller["PartNumber"] = *partNumber;
2147e860f15SJohn Edward Broadbent                 }
215d1bde9e5SKrzysztof Grobelny 
216d1bde9e5SKrzysztof Grobelny                 if (serialNumber != nullptr)
217d1bde9e5SKrzysztof Grobelny                 {
218d1bde9e5SKrzysztof Grobelny                     controller["SerialNumber"] = *serialNumber;
2197e860f15SJohn Edward Broadbent                 }
220d1bde9e5SKrzysztof Grobelny 
221d1bde9e5SKrzysztof Grobelny                 if (manufacturer != nullptr)
222d1bde9e5SKrzysztof Grobelny                 {
223d1bde9e5SKrzysztof Grobelny                     controller["Manufacturer"] = *manufacturer;
224d1bde9e5SKrzysztof Grobelny                 }
225d1bde9e5SKrzysztof Grobelny 
226d1bde9e5SKrzysztof Grobelny                 if (model != nullptr)
227d1bde9e5SKrzysztof Grobelny                 {
228d1bde9e5SKrzysztof Grobelny                     controller["Model"] = *model;
229d1bde9e5SKrzysztof Grobelny                 }
230d1bde9e5SKrzysztof Grobelny                 });
2317e860f15SJohn Edward Broadbent         }
2327e860f15SJohn Edward Broadbent 
2337e860f15SJohn Edward Broadbent         // this is done after we know the json array will no longer
2347e860f15SJohn Edward Broadbent         // be resized, as json::array uses vector underneath and we
2357e860f15SJohn Edward Broadbent         // need references to its members that won't change
2367e860f15SJohn Edward Broadbent         size_t count = 0;
237dfababfcSNan Zhou         // Pointer based on |asyncResp->res.jsonValue|
238dfababfcSNan Zhou         nlohmann::json::json_pointer rootPtr =
239dfababfcSNan Zhou             "/StorageControllers"_json_pointer;
2407e860f15SJohn Edward Broadbent         for (const auto& [path, interfaceDict] : subtree)
2417e860f15SJohn Edward Broadbent         {
2427e860f15SJohn Edward Broadbent             auto subHealth = std::make_shared<HealthPopulate>(
243dfababfcSNan Zhou                 asyncResp, rootPtr / count / "Status");
2447e860f15SJohn Edward Broadbent             subHealth->inventory.emplace_back(path);
2457e860f15SJohn Edward Broadbent             health->inventory.emplace_back(path);
2467e860f15SJohn Edward Broadbent             health->children.emplace_back(subHealth);
2477e860f15SJohn Edward Broadbent             count++;
2487e860f15SJohn Edward Broadbent         }
2497e860f15SJohn Edward Broadbent         },
2507e860f15SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
2517e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
2527e860f15SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2537e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", int32_t(0),
2547e860f15SJohn Edward Broadbent         std::array<const char*, 1>{
2557e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.StorageController"});
256a85afbe1SWilly Tu }
257a85afbe1SWilly Tu 
258a85afbe1SWilly Tu inline void requestRoutesStorage(App& app)
259a85afbe1SWilly Tu {
260a85afbe1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
261a85afbe1SWilly Tu         .privileges(redfish::privileges::getStorage)
262a85afbe1SWilly Tu         .methods(boost::beast::http::verb::get)(
263a85afbe1SWilly Tu             [&app](const crow::Request& req,
264a85afbe1SWilly Tu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2653ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
266a85afbe1SWilly Tu         {
267a85afbe1SWilly Tu             return;
268a85afbe1SWilly Tu         }
269a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
270a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
271a85afbe1SWilly Tu             "/redfish/v1/Systems/system/Storage/1";
272a85afbe1SWilly Tu         asyncResp->res.jsonValue["Name"] = "Storage";
273a85afbe1SWilly Tu         asyncResp->res.jsonValue["Id"] = "1";
274a85afbe1SWilly Tu         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
275a85afbe1SWilly Tu 
276a85afbe1SWilly Tu         auto health = std::make_shared<HealthPopulate>(asyncResp);
277a85afbe1SWilly Tu         health->populate();
278a85afbe1SWilly Tu 
279a85afbe1SWilly Tu         getDrives(asyncResp, health);
280a85afbe1SWilly Tu         getStorageControllers(asyncResp, health);
2817e860f15SJohn Edward Broadbent         });
2827e860f15SJohn Edward Broadbent }
2837e860f15SJohn Edward Broadbent 
28403913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28503913171SWilly Tu                           const std::string& connectionName,
28603913171SWilly Tu                           const std::string& path)
28703913171SWilly Tu {
288d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
289d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, path,
290d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Asset",
291168e20c1SEd Tanous         [asyncResp](const boost::system::error_code ec,
292168e20c1SEd Tanous                     const std::vector<
293168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
29403913171SWilly Tu                         propertiesList) {
29503913171SWilly Tu         if (ec)
29603913171SWilly Tu         {
29703913171SWilly Tu             // this interface isn't necessary
29803913171SWilly Tu             return;
29903913171SWilly Tu         }
300d1bde9e5SKrzysztof Grobelny 
301d1bde9e5SKrzysztof Grobelny         const std::string* partNumber = nullptr;
302d1bde9e5SKrzysztof Grobelny         const std::string* serialNumber = nullptr;
303d1bde9e5SKrzysztof Grobelny         const std::string* manufacturer = nullptr;
304d1bde9e5SKrzysztof Grobelny         const std::string* model = nullptr;
305d1bde9e5SKrzysztof Grobelny 
306d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
307d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
308d1bde9e5SKrzysztof Grobelny             partNumber, "SerialNumber", serialNumber, "Manufacturer",
309d1bde9e5SKrzysztof Grobelny             manufacturer, "Model", model);
310d1bde9e5SKrzysztof Grobelny 
311d1bde9e5SKrzysztof Grobelny         if (!success)
31203913171SWilly Tu         {
31303913171SWilly Tu             messages::internalError(asyncResp->res);
31403913171SWilly Tu             return;
31503913171SWilly Tu         }
316d1bde9e5SKrzysztof Grobelny 
317d1bde9e5SKrzysztof Grobelny         if (partNumber != nullptr)
318d1bde9e5SKrzysztof Grobelny         {
319d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["PartNumber"] = *partNumber;
32003913171SWilly Tu         }
321d1bde9e5SKrzysztof Grobelny 
322d1bde9e5SKrzysztof Grobelny         if (serialNumber != nullptr)
323d1bde9e5SKrzysztof Grobelny         {
324d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
32503913171SWilly Tu         }
326d1bde9e5SKrzysztof Grobelny 
327d1bde9e5SKrzysztof Grobelny         if (manufacturer != nullptr)
328d1bde9e5SKrzysztof Grobelny         {
329d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
330d1bde9e5SKrzysztof Grobelny         }
331d1bde9e5SKrzysztof Grobelny 
332d1bde9e5SKrzysztof Grobelny         if (model != nullptr)
333d1bde9e5SKrzysztof Grobelny         {
334d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Model"] = *model;
335d1bde9e5SKrzysztof Grobelny         }
336d1bde9e5SKrzysztof Grobelny         });
33703913171SWilly Tu }
33803913171SWilly Tu 
33903913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34003913171SWilly Tu                             const std::string& connectionName,
34103913171SWilly Tu                             const std::string& path)
34203913171SWilly Tu {
3431e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3441e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3451e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
34603913171SWilly Tu         [asyncResp, path](const boost::system::error_code ec,
3471e1e598dSJonathan Doman                           const bool enabled) {
34803913171SWilly Tu         // this interface isn't necessary, only check it if
34903913171SWilly Tu         // we get a good return
35003913171SWilly Tu         if (ec)
35103913171SWilly Tu         {
35203913171SWilly Tu             return;
35303913171SWilly Tu         }
35403913171SWilly Tu 
3551e1e598dSJonathan Doman         if (!enabled)
35603913171SWilly Tu         {
35703913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
35803913171SWilly Tu         }
3591e1e598dSJonathan Doman         });
36003913171SWilly Tu }
36103913171SWilly Tu 
36203913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36303913171SWilly Tu                           const std::string& connectionName,
36403913171SWilly Tu                           const std::string& path)
36503913171SWilly Tu {
3661e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3671e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3681e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3691e1e598dSJonathan Doman         [asyncResp](const boost::system::error_code ec, const bool updating) {
37003913171SWilly Tu         // this interface isn't necessary, only check it
37103913171SWilly Tu         // if we get a good return
37203913171SWilly Tu         if (ec)
37303913171SWilly Tu         {
37403913171SWilly Tu             return;
37503913171SWilly Tu         }
37603913171SWilly Tu 
37703913171SWilly Tu         // updating and disabled in the backend shouldn't be
37803913171SWilly Tu         // able to be set at the same time, so we don't need
37903913171SWilly Tu         // to check for the race condition of these two
38003913171SWilly Tu         // calls
3811e1e598dSJonathan Doman         if (updating)
38203913171SWilly Tu         {
38303913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Updating";
38403913171SWilly Tu         }
3851e1e598dSJonathan Doman         });
38603913171SWilly Tu }
38703913171SWilly Tu 
38819b8e9a0SWilly Tu inline std::optional<std::string> convertDriveType(const std::string& type)
38919b8e9a0SWilly Tu {
39019b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
39119b8e9a0SWilly Tu     {
39219b8e9a0SWilly Tu         return "HDD";
39319b8e9a0SWilly Tu     }
39419b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
39519b8e9a0SWilly Tu     {
39619b8e9a0SWilly Tu         return "SSD";
39719b8e9a0SWilly Tu     }
39819b8e9a0SWilly Tu 
39919b8e9a0SWilly Tu     return std::nullopt;
40019b8e9a0SWilly Tu }
40119b8e9a0SWilly Tu 
40219b8e9a0SWilly Tu inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
40319b8e9a0SWilly Tu {
40419b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
40519b8e9a0SWilly Tu     {
40619b8e9a0SWilly Tu         return "SAS";
40719b8e9a0SWilly Tu     }
40819b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
40919b8e9a0SWilly Tu     {
41019b8e9a0SWilly Tu         return "SATA";
41119b8e9a0SWilly Tu     }
41219b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
41319b8e9a0SWilly Tu     {
41419b8e9a0SWilly Tu         return "NVMe";
41519b8e9a0SWilly Tu     }
41619b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
41719b8e9a0SWilly Tu     {
41819b8e9a0SWilly Tu         return "FC";
41919b8e9a0SWilly Tu     }
42019b8e9a0SWilly Tu 
42119b8e9a0SWilly Tu     return std::nullopt;
42219b8e9a0SWilly Tu }
42319b8e9a0SWilly Tu 
42419b8e9a0SWilly Tu inline void
42519b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
42619b8e9a0SWilly Tu                            const std::string& connectionName,
42719b8e9a0SWilly Tu                            const std::string& path)
42819b8e9a0SWilly Tu {
42919b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
43019b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
43119b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
43219b8e9a0SWilly Tu         [asyncResp](const boost::system::error_code ec,
43319b8e9a0SWilly Tu                     const std::vector<
43419b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
43519b8e9a0SWilly Tu                         propertiesList) {
43619b8e9a0SWilly Tu         if (ec)
43719b8e9a0SWilly Tu         {
43819b8e9a0SWilly Tu             // this interface isn't required
43919b8e9a0SWilly Tu             return;
44019b8e9a0SWilly Tu         }
44119b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
44219b8e9a0SWilly Tu                  property : propertiesList)
44319b8e9a0SWilly Tu         {
44419b8e9a0SWilly Tu             const std::string& propertyName = property.first;
44519b8e9a0SWilly Tu             if (propertyName == "Type")
44619b8e9a0SWilly Tu             {
44719b8e9a0SWilly Tu                 const std::string* value =
44819b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
44919b8e9a0SWilly Tu                 if (value == nullptr)
45019b8e9a0SWilly Tu                 {
45119b8e9a0SWilly Tu                     // illegal property
45219b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Type";
45319b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
45419b8e9a0SWilly Tu                     return;
45519b8e9a0SWilly Tu                 }
45619b8e9a0SWilly Tu 
457002d39b4SEd Tanous                 std::optional<std::string> mediaType = convertDriveType(*value);
45819b8e9a0SWilly Tu                 if (!mediaType)
45919b8e9a0SWilly Tu                 {
46019b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
46119b8e9a0SWilly Tu                                      << *value;
46219b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
46319b8e9a0SWilly Tu                     return;
46419b8e9a0SWilly Tu                 }
46519b8e9a0SWilly Tu 
46619b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
46719b8e9a0SWilly Tu             }
46819b8e9a0SWilly Tu             else if (propertyName == "Capacity")
46919b8e9a0SWilly Tu             {
47019b8e9a0SWilly Tu                 const uint64_t* capacity =
47119b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
47219b8e9a0SWilly Tu                 if (capacity == nullptr)
47319b8e9a0SWilly Tu                 {
47419b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Capacity";
47519b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
47619b8e9a0SWilly Tu                     return;
47719b8e9a0SWilly Tu                 }
47819b8e9a0SWilly Tu                 if (*capacity == 0)
47919b8e9a0SWilly Tu                 {
48019b8e9a0SWilly Tu                     // drive capacity not known
48119b8e9a0SWilly Tu                     continue;
48219b8e9a0SWilly Tu                 }
48319b8e9a0SWilly Tu 
48419b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
48519b8e9a0SWilly Tu             }
48619b8e9a0SWilly Tu             else if (propertyName == "Protocol")
48719b8e9a0SWilly Tu             {
48819b8e9a0SWilly Tu                 const std::string* value =
48919b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
49019b8e9a0SWilly Tu                 if (value == nullptr)
49119b8e9a0SWilly Tu                 {
49219b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Protocol";
49319b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
49419b8e9a0SWilly Tu                     return;
49519b8e9a0SWilly Tu                 }
49619b8e9a0SWilly Tu 
497002d39b4SEd Tanous                 std::optional<std::string> proto = convertDriveProtocol(*value);
49819b8e9a0SWilly Tu                 if (!proto)
49919b8e9a0SWilly Tu                 {
500002d39b4SEd Tanous                     BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: "
50119b8e9a0SWilly Tu                                      << *value;
50219b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
50319b8e9a0SWilly Tu                     return;
50419b8e9a0SWilly Tu                 }
50519b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
50619b8e9a0SWilly Tu             }
5073fe4d5ccSJohn Edward Broadbent             else if (propertyName == "PredictedMediaLifeLeftPercent")
5083fe4d5ccSJohn Edward Broadbent             {
5093fe4d5ccSJohn Edward Broadbent                 const uint8_t* lifeLeft =
5103fe4d5ccSJohn Edward Broadbent                     std::get_if<uint8_t>(&property.second);
5113fe4d5ccSJohn Edward Broadbent                 if (lifeLeft == nullptr)
5123fe4d5ccSJohn Edward Broadbent                 {
5133fe4d5ccSJohn Edward Broadbent                     BMCWEB_LOG_ERROR
5143fe4d5ccSJohn Edward Broadbent                         << "Illegal property: PredictedMediaLifeLeftPercent";
5153fe4d5ccSJohn Edward Broadbent                     messages::internalError(asyncResp->res);
5163fe4d5ccSJohn Edward Broadbent                     return;
5173fe4d5ccSJohn Edward Broadbent                 }
5183fe4d5ccSJohn Edward Broadbent                 // 255 means reading the value is not supported
5193fe4d5ccSJohn Edward Broadbent                 if (*lifeLeft != 255)
5203fe4d5ccSJohn Edward Broadbent                 {
5213fe4d5ccSJohn Edward Broadbent                     asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
5223fe4d5ccSJohn Edward Broadbent                         *lifeLeft;
5233fe4d5ccSJohn Edward Broadbent                 }
5243fe4d5ccSJohn Edward Broadbent             }
52519b8e9a0SWilly Tu         }
52619b8e9a0SWilly Tu         });
52719b8e9a0SWilly Tu }
52819b8e9a0SWilly Tu 
529b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
530b53dcd9dSNan Zhou                             const std::string& connectionName,
531b53dcd9dSNan Zhou                             const std::string& path,
532e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
533e56ed6b9SJohn Edward Broadbent {
534e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
535e56ed6b9SJohn Edward Broadbent     {
536e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
537e56ed6b9SJohn Edward Broadbent         {
538e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
539e56ed6b9SJohn Edward Broadbent         }
540e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
541e56ed6b9SJohn Edward Broadbent         {
542e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
543e56ed6b9SJohn Edward Broadbent         }
544e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
545e56ed6b9SJohn Edward Broadbent         {
546e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
547e56ed6b9SJohn Edward Broadbent         }
548e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
549e56ed6b9SJohn Edward Broadbent         {
550e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
551e56ed6b9SJohn Edward Broadbent         }
552e56ed6b9SJohn Edward Broadbent     }
553e56ed6b9SJohn Edward Broadbent }
554e56ed6b9SJohn Edward Broadbent 
5557e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
5567e860f15SJohn Edward Broadbent {
557*22d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
558ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
559002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
560002d39b4SEd Tanous             [&app](const crow::Request& req,
56145ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
562*22d268cbSEd Tanous                    const std::string& systemName, const std::string& driveId) {
5633ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
56445ca1b86SEd Tanous         {
56545ca1b86SEd Tanous             return;
56645ca1b86SEd Tanous         }
567*22d268cbSEd Tanous         if (systemName != "system")
568*22d268cbSEd Tanous         {
569*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
570*22d268cbSEd Tanous                                        systemName);
571*22d268cbSEd Tanous             return;
572*22d268cbSEd Tanous         }
573*22d268cbSEd Tanous 
5747e860f15SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
575002d39b4SEd Tanous             [asyncResp,
576002d39b4SEd Tanous              driveId](const boost::system::error_code ec,
577b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
5787e860f15SJohn Edward Broadbent             if (ec)
5797e860f15SJohn Edward Broadbent             {
5807e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Drive mapper call error";
5817e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
5827e860f15SJohn Edward Broadbent                 return;
5837e860f15SJohn Edward Broadbent             }
5847e860f15SJohn Edward Broadbent 
58503913171SWilly Tu             auto drive = std::find_if(
5867e860f15SJohn Edward Broadbent                 subtree.begin(), subtree.end(),
587002d39b4SEd Tanous                 [&driveId](
5888cb65f8aSNan Zhou                     const std::pair<std::string,
5898cb65f8aSNan Zhou                                     dbus::utility::MapperServiceMap>& object) {
59003913171SWilly Tu                 return sdbusplus::message::object_path(object.first)
59103913171SWilly Tu                            .filename() == driveId;
5927e860f15SJohn Edward Broadbent                 });
5937e860f15SJohn Edward Broadbent 
59403913171SWilly Tu             if (drive == subtree.end())
5957e860f15SJohn Edward Broadbent             {
596002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
5977e860f15SJohn Edward Broadbent                 return;
5987e860f15SJohn Edward Broadbent             }
5997e860f15SJohn Edward Broadbent 
60003913171SWilly Tu             const std::string& path = drive->first;
6018cb65f8aSNan Zhou             const dbus::utility::MapperServiceMap& connectionNames =
6028cb65f8aSNan Zhou                 drive->second;
6037e860f15SJohn Edward Broadbent 
604002d39b4SEd Tanous             asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
6057e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
606002d39b4SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId;
6077e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = driveId;
6087e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = driveId;
6097e860f15SJohn Edward Broadbent 
6107e860f15SJohn Edward Broadbent             if (connectionNames.size() != 1)
6117e860f15SJohn Edward Broadbent             {
612002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
61303913171SWilly Tu                                  << ", not equal to 1";
6147e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
6157e860f15SJohn Edward Broadbent                 return;
6167e860f15SJohn Edward Broadbent             }
6177e860f15SJohn Edward Broadbent 
6187e860f15SJohn Edward Broadbent             getMainChassisId(
619002d39b4SEd Tanous                 asyncResp, [](const std::string& chassisId,
6207e860f15SJohn Edward Broadbent                               const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
621002d39b4SEd Tanous                     aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
6221476687dSEd Tanous                         "/redfish/v1/Chassis/" + chassisId;
6237e860f15SJohn Edward Broadbent                 });
6247e860f15SJohn Edward Broadbent 
625a25aeccfSNikhil Potade             // default it to Enabled
626a25aeccfSNikhil Potade             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
627a25aeccfSNikhil Potade 
6282ad9c2f6SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
629e284a7c1SJames Feist             health->inventory.emplace_back(path);
6302ad9c2f6SJames Feist             health->populate();
6312ad9c2f6SJames Feist 
632e56ed6b9SJohn Edward Broadbent             addAllDriveInfo(asyncResp, connectionNames[0].first, path,
633e56ed6b9SJohn Edward Broadbent                             connectionNames[0].second);
634a25aeccfSNikhil Potade             },
635a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper",
636a25aeccfSNikhil Potade             "/xyz/openbmc_project/object_mapper",
637a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
638a25aeccfSNikhil Potade             "/xyz/openbmc_project/inventory", int32_t(0),
639a25aeccfSNikhil Potade             std::array<const char*, 1>{
640a25aeccfSNikhil Potade                 "xyz.openbmc_project.Inventory.Item.Drive"});
6417e860f15SJohn Edward Broadbent         });
642a25aeccfSNikhil Potade }
64392903bd4SJohn Edward Broadbent 
64492903bd4SJohn Edward Broadbent /**
64592903bd4SJohn Edward Broadbent  * Chassis drives, this URL will show all the DriveCollection
64692903bd4SJohn Edward Broadbent  * information
64792903bd4SJohn Edward Broadbent  */
648b53dcd9dSNan Zhou inline void chassisDriveCollectionGet(
64992903bd4SJohn Edward Broadbent     crow::App& app, const crow::Request& req,
65092903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
65192903bd4SJohn Edward Broadbent     const std::string& chassisId)
65292903bd4SJohn Edward Broadbent {
6533ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
65492903bd4SJohn Edward Broadbent     {
65592903bd4SJohn Edward Broadbent         return;
65692903bd4SJohn Edward Broadbent     }
65792903bd4SJohn Edward Broadbent 
65892903bd4SJohn Edward Broadbent     // mapper call lambda
65992903bd4SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
66092903bd4SJohn Edward Broadbent         [asyncResp,
66192903bd4SJohn Edward Broadbent          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
73592903bd4SJohn Edward Broadbent         },
73692903bd4SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
73792903bd4SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
73892903bd4SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
73992903bd4SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", 0,
74092903bd4SJohn Edward Broadbent         std::array<const char*, 2>{
74192903bd4SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Board",
74292903bd4SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Chassis"});
74392903bd4SJohn Edward Broadbent }
74492903bd4SJohn Edward Broadbent 
74592903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
74692903bd4SJohn Edward Broadbent {
74792903bd4SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
74892903bd4SJohn Edward Broadbent         .privileges(redfish::privileges::getDriveCollection)
74992903bd4SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
75092903bd4SJohn Edward Broadbent             std::bind_front(chassisDriveCollectionGet, std::ref(app)));
75192903bd4SJohn Edward Broadbent }
75292903bd4SJohn Edward Broadbent 
753b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
754b53dcd9dSNan Zhou                        const std::string& chassisId,
755b53dcd9dSNan Zhou                        const std::string& driveName,
756e56ed6b9SJohn Edward Broadbent                        const boost::system::error_code ec,
757e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
758e56ed6b9SJohn Edward Broadbent {
759e56ed6b9SJohn Edward Broadbent 
760e56ed6b9SJohn Edward Broadbent     if (ec)
761e56ed6b9SJohn Edward Broadbent     {
762e56ed6b9SJohn Edward Broadbent         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
763e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
764e56ed6b9SJohn Edward Broadbent         return;
765e56ed6b9SJohn Edward Broadbent     }
766e56ed6b9SJohn Edward Broadbent 
767e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
7688cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
769e56ed6b9SJohn Edward Broadbent     {
770e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
771e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
772e56ed6b9SJohn Edward Broadbent         {
773e56ed6b9SJohn Edward Broadbent             continue;
774e56ed6b9SJohn Edward Broadbent         }
775e56ed6b9SJohn Edward Broadbent 
776e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
777e56ed6b9SJohn Edward Broadbent         {
778e56ed6b9SJohn Edward Broadbent             BMCWEB_LOG_ERROR << "Got 0 Connection names";
779e56ed6b9SJohn Edward Broadbent             continue;
780e56ed6b9SJohn Edward Broadbent         }
781e56ed6b9SJohn Edward Broadbent 
782e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
783a0cb40cbSJohn Edward Broadbent             "redfish", "v1", "Chassis", chassisId, "Drives", driveName);
784e56ed6b9SJohn Edward Broadbent 
785e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
786a0cb40cbSJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = driveName;
787e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
788e56ed6b9SJohn Edward Broadbent         // default it to Enabled
789e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
790e56ed6b9SJohn Edward Broadbent 
791e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
792e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
793e56ed6b9SJohn Edward Broadbent             crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId);
794e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
795e56ed6b9SJohn Edward Broadbent 
796e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
797e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
798e56ed6b9SJohn Edward Broadbent     }
799e56ed6b9SJohn Edward Broadbent }
800e56ed6b9SJohn Edward Broadbent 
801b53dcd9dSNan Zhou inline void
802b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
803e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
804e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
805e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
806e56ed6b9SJohn Edward Broadbent {
807e56ed6b9SJohn Edward Broadbent 
808e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
809e56ed6b9SJohn Edward Broadbent     {
810e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
811e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
812e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
813e56ed6b9SJohn Edward Broadbent         {
814e56ed6b9SJohn Edward Broadbent             continue;
815e56ed6b9SJohn Edward Broadbent         }
816e56ed6b9SJohn Edward Broadbent         //  mapper call drive
817e56ed6b9SJohn Edward Broadbent         const std::array<const char*, 1> driveInterface = {
818e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
819e56ed6b9SJohn Edward Broadbent 
820e56ed6b9SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
821e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
822e56ed6b9SJohn Edward Broadbent                 const boost::system::error_code ec,
823e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
824e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
825e56ed6b9SJohn Edward Broadbent             },
826e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper",
827e56ed6b9SJohn Edward Broadbent             "/xyz/openbmc_project/object_mapper",
828e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
829e56ed6b9SJohn Edward Broadbent             "/xyz/openbmc_project/inventory", 0, driveInterface);
830e56ed6b9SJohn Edward Broadbent     }
831e56ed6b9SJohn Edward Broadbent }
832e56ed6b9SJohn Edward Broadbent 
833b53dcd9dSNan Zhou inline void
834b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
835e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
836e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
837e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
838e56ed6b9SJohn Edward Broadbent {
83903810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
840e56ed6b9SJohn Edward Broadbent     {
841e56ed6b9SJohn Edward Broadbent         return;
842e56ed6b9SJohn Edward Broadbent     }
843e56ed6b9SJohn Edward Broadbent     const std::array<const char*, 2> interfaces = {
844e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
845e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
846e56ed6b9SJohn Edward Broadbent 
847e56ed6b9SJohn Edward Broadbent     // mapper call chassis
848e56ed6b9SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
849e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
850e56ed6b9SJohn Edward Broadbent          driveName](const boost::system::error_code ec,
851e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
852e56ed6b9SJohn Edward Broadbent         if (ec)
853e56ed6b9SJohn Edward Broadbent         {
854e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
855e56ed6b9SJohn Edward Broadbent             return;
856e56ed6b9SJohn Edward Broadbent         }
857e56ed6b9SJohn Edward Broadbent 
858e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
8598cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
860e56ed6b9SJohn Edward Broadbent         {
861e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
862e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
863e56ed6b9SJohn Edward Broadbent             {
864e56ed6b9SJohn Edward Broadbent                 continue;
865e56ed6b9SJohn Edward Broadbent             }
866e56ed6b9SJohn Edward Broadbent 
867e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
868e56ed6b9SJohn Edward Broadbent             {
869e56ed6b9SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
870e56ed6b9SJohn Edward Broadbent                 continue;
871e56ed6b9SJohn Edward Broadbent             }
872e56ed6b9SJohn Edward Broadbent 
873e56ed6b9SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
874e56ed6b9SJohn Edward Broadbent                 *crow::connections::systemBus,
875e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
876e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
877e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
878e56ed6b9SJohn Edward Broadbent                  driveName](const boost::system::error_code ec3,
879e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& resp) {
880e56ed6b9SJohn Edward Broadbent                 if (ec3)
881e56ed6b9SJohn Edward Broadbent                 {
882e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
883e56ed6b9SJohn Edward Broadbent                 }
884e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
885e56ed6b9SJohn Edward Broadbent                 });
886e56ed6b9SJohn Edward Broadbent             break;
887e56ed6b9SJohn Edward Broadbent         }
888e56ed6b9SJohn Edward Broadbent         },
889e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
890e56ed6b9SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
891e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
892e56ed6b9SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", 0, interfaces);
893e56ed6b9SJohn Edward Broadbent }
894e56ed6b9SJohn Edward Broadbent 
895e56ed6b9SJohn Edward Broadbent /**
896e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
897e56ed6b9SJohn Edward Broadbent  */
898e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
899e56ed6b9SJohn Edward Broadbent {
900e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
901e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
902e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
903e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
904e56ed6b9SJohn Edward Broadbent }
905e56ed6b9SJohn Edward Broadbent 
906a25aeccfSNikhil Potade } // namespace redfish
907