xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision d1bde9e590f165f28d948fda93e48d51b30bb463)
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>
26*d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
27*d1bde9e5SKrzysztof Grobelny #include <utils/dbus_utils.hpp>
28a25aeccfSNikhil Potade 
29a25aeccfSNikhil Potade namespace redfish
30a25aeccfSNikhil Potade {
317e860f15SJohn Edward Broadbent inline void requestRoutesStorageCollection(App& app)
32a25aeccfSNikhil Potade {
337e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/")
34ed398213SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
357e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3645ca1b86SEd Tanous             [&app](const crow::Request& req,
377e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
383ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3945ca1b86SEd Tanous         {
4045ca1b86SEd Tanous             return;
4145ca1b86SEd Tanous         }
428d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
438d1b46d7Szhanghch05             "#StorageCollection.StorageCollection";
448d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
458d1b46d7Szhanghch05             "/redfish/v1/Systems/system/Storage";
468d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Storage Collection";
471476687dSEd Tanous         nlohmann::json::array_t members;
481476687dSEd Tanous         nlohmann::json::object_t member;
491476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
501476687dSEd Tanous         members.emplace_back(member);
511476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
528d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
537e860f15SJohn Edward Broadbent         });
54a25aeccfSNikhil Potade }
55a25aeccfSNikhil Potade 
56a85afbe1SWilly Tu inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
57a85afbe1SWilly Tu                       const std::shared_ptr<HealthPopulate>& health)
58a25aeccfSNikhil Potade {
59a25aeccfSNikhil Potade     crow::connections::systemBus->async_method_call(
60a85afbe1SWilly Tu         [asyncResp, health](
61a85afbe1SWilly Tu             const boost::system::error_code ec,
62a85afbe1SWilly Tu             const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
63a25aeccfSNikhil Potade         if (ec)
64a25aeccfSNikhil Potade         {
65a25aeccfSNikhil Potade             BMCWEB_LOG_ERROR << "Drive mapper call error";
66a25aeccfSNikhil Potade             messages::internalError(asyncResp->res);
67a25aeccfSNikhil Potade             return;
68a25aeccfSNikhil Potade         }
692ad9c2f6SJames Feist 
70a85afbe1SWilly Tu         nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
71a85afbe1SWilly Tu         driveArray = nlohmann::json::array();
72a85afbe1SWilly Tu         auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
73a85afbe1SWilly Tu         count = 0;
742ad9c2f6SJames Feist 
75a85afbe1SWilly Tu         health->inventory.insert(health->inventory.end(), driveList.begin(),
76a85afbe1SWilly Tu                                  driveList.end());
77a85afbe1SWilly Tu 
78a85afbe1SWilly Tu         for (const std::string& drive : driveList)
79a25aeccfSNikhil Potade         {
80a85afbe1SWilly Tu             sdbusplus::message::object_path object(drive);
81a85afbe1SWilly Tu             if (object.filename().empty())
82a25aeccfSNikhil Potade             {
83a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
84a85afbe1SWilly Tu                 return;
85a25aeccfSNikhil Potade             }
86a85afbe1SWilly Tu 
87a85afbe1SWilly Tu             nlohmann::json::object_t driveJson;
88a85afbe1SWilly Tu             driveJson["@odata.id"] =
89be13ceceSJames Feist                 "/redfish/v1/Systems/system/Storage/1/Drives/" +
90a85afbe1SWilly Tu                 object.filename();
91a85afbe1SWilly Tu             driveArray.push_back(std::move(driveJson));
92a25aeccfSNikhil Potade         }
93a25aeccfSNikhil Potade 
94a85afbe1SWilly Tu         count = driveArray.size();
95a25aeccfSNikhil Potade         },
96a25aeccfSNikhil Potade         "xyz.openbmc_project.ObjectMapper",
97a25aeccfSNikhil Potade         "/xyz/openbmc_project/object_mapper",
98a25aeccfSNikhil Potade         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
99a25aeccfSNikhil Potade         "/xyz/openbmc_project/inventory", int32_t(0),
100a85afbe1SWilly Tu         std::array<const char*, 1>{"xyz.openbmc_project.Inventory.Item.Drive"});
101a85afbe1SWilly Tu }
102e284a7c1SJames Feist 
103a85afbe1SWilly Tu inline void
104a85afbe1SWilly Tu     getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
105a85afbe1SWilly Tu                           const std::shared_ptr<HealthPopulate>& health)
106a85afbe1SWilly Tu {
107e284a7c1SJames Feist     crow::connections::systemBus->async_method_call(
108002d39b4SEd Tanous         [asyncResp,
109002d39b4SEd Tanous          health](const boost::system::error_code ec,
110b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreeResponse& subtree) {
11126f6976fSEd Tanous         if (ec || subtree.empty())
112e284a7c1SJames Feist         {
113d819a420SJames Feist             // doesn't have to be there
114e284a7c1SJames Feist             return;
115e284a7c1SJames Feist         }
116e284a7c1SJames Feist 
117a85afbe1SWilly Tu         nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
118e284a7c1SJames Feist         root = nlohmann::json::array();
119e284a7c1SJames Feist         for (const auto& [path, interfaceDict] : subtree)
120e284a7c1SJames Feist         {
121a85afbe1SWilly Tu             sdbusplus::message::object_path object(path);
122a85afbe1SWilly Tu             std::string id = object.filename();
123a85afbe1SWilly Tu             if (id.empty())
124e284a7c1SJames Feist             {
125a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
126e284a7c1SJames Feist                 return;
127e284a7c1SJames Feist             }
128e284a7c1SJames Feist 
129e284a7c1SJames Feist             if (interfaceDict.size() != 1)
130e284a7c1SJames Feist             {
131a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
132e284a7c1SJames Feist                                  << ", greater than 1";
133e284a7c1SJames Feist                 messages::internalError(asyncResp->res);
134e284a7c1SJames Feist                 return;
135e284a7c1SJames Feist             }
136e284a7c1SJames Feist 
137002d39b4SEd Tanous             const std::string& connectionName = interfaceDict.front().first;
138e284a7c1SJames Feist 
139e284a7c1SJames Feist             size_t index = root.size();
140e284a7c1SJames Feist             nlohmann::json& storageController =
141e284a7c1SJames Feist                 root.emplace_back(nlohmann::json::object());
142e284a7c1SJames Feist 
143e284a7c1SJames Feist             storageController["@odata.type"] =
144e284a7c1SJames Feist                 "#Storage.v1_7_0.StorageController";
145e284a7c1SJames Feist             storageController["@odata.id"] =
1460fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
147e284a7c1SJames Feist                 std::to_string(index);
148e284a7c1SJames Feist             storageController["Name"] = id;
149e284a7c1SJames Feist             storageController["MemberId"] = id;
150e284a7c1SJames Feist             storageController["Status"]["State"] = "Enabled";
151e284a7c1SJames Feist 
1521e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
1531e1e598dSJonathan Doman                 *crow::connections::systemBus, connectionName, path,
1541e1e598dSJonathan Doman                 "xyz.openbmc_project.Inventory.Item", "Present",
155002d39b4SEd Tanous                 [asyncResp, index](const boost::system::error_code ec2,
1561e1e598dSJonathan Doman                                    bool enabled) {
1577e860f15SJohn Edward Broadbent                 // this interface isn't necessary, only check it
1587e860f15SJohn Edward Broadbent                 // if we get a good return
15923a21a1cSEd Tanous                 if (ec2)
160e284a7c1SJames Feist                 {
161e284a7c1SJames Feist                     return;
162e284a7c1SJames Feist                 }
1631e1e598dSJonathan Doman                 if (!enabled)
164e284a7c1SJames Feist                 {
165002d39b4SEd Tanous                     asyncResp->res.jsonValue["StorageControllers"][index]
166a85afbe1SWilly Tu                                             ["Status"]["State"] = "Disabled";
167e284a7c1SJames Feist                 }
1681e1e598dSJonathan Doman                 });
169e284a7c1SJames Feist 
170*d1bde9e5SKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
171*d1bde9e5SKrzysztof Grobelny                 *crow::connections::systemBus, connectionName, path,
172*d1bde9e5SKrzysztof Grobelny                 "xyz.openbmc_project.Inventory.Decorator.Asset",
173a85afbe1SWilly Tu                 [asyncResp, index](
174a85afbe1SWilly Tu                     const boost::system::error_code ec2,
175a85afbe1SWilly Tu                     const std::vector<
176a85afbe1SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
1777e860f15SJohn Edward Broadbent                         propertiesList) {
1787e860f15SJohn Edward Broadbent                 if (ec2)
1797e860f15SJohn Edward Broadbent                 {
1807e860f15SJohn Edward Broadbent                     // this interface isn't necessary
1817e860f15SJohn Edward Broadbent                     return;
1827e860f15SJohn Edward Broadbent                 }
183*d1bde9e5SKrzysztof Grobelny 
184*d1bde9e5SKrzysztof Grobelny                 const std::string* partNumber = nullptr;
185*d1bde9e5SKrzysztof Grobelny                 const std::string* serialNumber = nullptr;
186*d1bde9e5SKrzysztof Grobelny                 const std::string* manufacturer = nullptr;
187*d1bde9e5SKrzysztof Grobelny                 const std::string* model = nullptr;
188*d1bde9e5SKrzysztof Grobelny 
189*d1bde9e5SKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
190*d1bde9e5SKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), propertiesList,
191*d1bde9e5SKrzysztof Grobelny                     "PartNumber", partNumber, "SerialNumber", serialNumber,
192*d1bde9e5SKrzysztof Grobelny                     "Manufacturer", manufacturer, "Model", model);
193*d1bde9e5SKrzysztof Grobelny 
194*d1bde9e5SKrzysztof Grobelny                 if (!success)
1957e860f15SJohn Edward Broadbent                 {
196002d39b4SEd Tanous                     messages::internalError(asyncResp->res);
1977e860f15SJohn Edward Broadbent                     return;
1987e860f15SJohn Edward Broadbent                 }
199*d1bde9e5SKrzysztof Grobelny 
200*d1bde9e5SKrzysztof Grobelny                 nlohmann::json& controller =
201*d1bde9e5SKrzysztof Grobelny                     asyncResp->res.jsonValue["StorageControllers"][index];
202*d1bde9e5SKrzysztof Grobelny 
203*d1bde9e5SKrzysztof Grobelny                 if (partNumber != nullptr)
204*d1bde9e5SKrzysztof Grobelny                 {
205*d1bde9e5SKrzysztof Grobelny                     controller["PartNumber"] = *partNumber;
2067e860f15SJohn Edward Broadbent                 }
207*d1bde9e5SKrzysztof Grobelny 
208*d1bde9e5SKrzysztof Grobelny                 if (serialNumber != nullptr)
209*d1bde9e5SKrzysztof Grobelny                 {
210*d1bde9e5SKrzysztof Grobelny                     controller["SerialNumber"] = *serialNumber;
2117e860f15SJohn Edward Broadbent                 }
212*d1bde9e5SKrzysztof Grobelny 
213*d1bde9e5SKrzysztof Grobelny                 if (manufacturer != nullptr)
214*d1bde9e5SKrzysztof Grobelny                 {
215*d1bde9e5SKrzysztof Grobelny                     controller["Manufacturer"] = *manufacturer;
216*d1bde9e5SKrzysztof Grobelny                 }
217*d1bde9e5SKrzysztof Grobelny 
218*d1bde9e5SKrzysztof Grobelny                 if (model != nullptr)
219*d1bde9e5SKrzysztof Grobelny                 {
220*d1bde9e5SKrzysztof Grobelny                     controller["Model"] = *model;
221*d1bde9e5SKrzysztof Grobelny                 }
222*d1bde9e5SKrzysztof Grobelny                 });
2237e860f15SJohn Edward Broadbent         }
2247e860f15SJohn Edward Broadbent 
2257e860f15SJohn Edward Broadbent         // this is done after we know the json array will no longer
2267e860f15SJohn Edward Broadbent         // be resized, as json::array uses vector underneath and we
2277e860f15SJohn Edward Broadbent         // need references to its members that won't change
2287e860f15SJohn Edward Broadbent         size_t count = 0;
229dfababfcSNan Zhou         // Pointer based on |asyncResp->res.jsonValue|
230dfababfcSNan Zhou         nlohmann::json::json_pointer rootPtr =
231dfababfcSNan Zhou             "/StorageControllers"_json_pointer;
2327e860f15SJohn Edward Broadbent         for (const auto& [path, interfaceDict] : subtree)
2337e860f15SJohn Edward Broadbent         {
2347e860f15SJohn Edward Broadbent             auto subHealth = std::make_shared<HealthPopulate>(
235dfababfcSNan Zhou                 asyncResp, rootPtr / count / "Status");
2367e860f15SJohn Edward Broadbent             subHealth->inventory.emplace_back(path);
2377e860f15SJohn Edward Broadbent             health->inventory.emplace_back(path);
2387e860f15SJohn Edward Broadbent             health->children.emplace_back(subHealth);
2397e860f15SJohn Edward Broadbent             count++;
2407e860f15SJohn Edward Broadbent         }
2417e860f15SJohn Edward Broadbent         },
2427e860f15SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
2437e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
2447e860f15SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2457e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", int32_t(0),
2467e860f15SJohn Edward Broadbent         std::array<const char*, 1>{
2477e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.StorageController"});
248a85afbe1SWilly Tu }
249a85afbe1SWilly Tu 
250a85afbe1SWilly Tu inline void requestRoutesStorage(App& app)
251a85afbe1SWilly Tu {
252a85afbe1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
253a85afbe1SWilly Tu         .privileges(redfish::privileges::getStorage)
254a85afbe1SWilly Tu         .methods(boost::beast::http::verb::get)(
255a85afbe1SWilly Tu             [&app](const crow::Request& req,
256a85afbe1SWilly Tu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2573ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
258a85afbe1SWilly Tu         {
259a85afbe1SWilly Tu             return;
260a85afbe1SWilly Tu         }
261a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
262a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
263a85afbe1SWilly Tu             "/redfish/v1/Systems/system/Storage/1";
264a85afbe1SWilly Tu         asyncResp->res.jsonValue["Name"] = "Storage";
265a85afbe1SWilly Tu         asyncResp->res.jsonValue["Id"] = "1";
266a85afbe1SWilly Tu         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
267a85afbe1SWilly Tu 
268a85afbe1SWilly Tu         auto health = std::make_shared<HealthPopulate>(asyncResp);
269a85afbe1SWilly Tu         health->populate();
270a85afbe1SWilly Tu 
271a85afbe1SWilly Tu         getDrives(asyncResp, health);
272a85afbe1SWilly Tu         getStorageControllers(asyncResp, health);
2737e860f15SJohn Edward Broadbent         });
2747e860f15SJohn Edward Broadbent }
2757e860f15SJohn Edward Broadbent 
27603913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27703913171SWilly Tu                           const std::string& connectionName,
27803913171SWilly Tu                           const std::string& path)
27903913171SWilly Tu {
280*d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
281*d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, path,
282*d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Asset",
283168e20c1SEd Tanous         [asyncResp](const boost::system::error_code ec,
284168e20c1SEd Tanous                     const std::vector<
285168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
28603913171SWilly Tu                         propertiesList) {
28703913171SWilly Tu         if (ec)
28803913171SWilly Tu         {
28903913171SWilly Tu             // this interface isn't necessary
29003913171SWilly Tu             return;
29103913171SWilly Tu         }
292*d1bde9e5SKrzysztof Grobelny 
293*d1bde9e5SKrzysztof Grobelny         const std::string* partNumber = nullptr;
294*d1bde9e5SKrzysztof Grobelny         const std::string* serialNumber = nullptr;
295*d1bde9e5SKrzysztof Grobelny         const std::string* manufacturer = nullptr;
296*d1bde9e5SKrzysztof Grobelny         const std::string* model = nullptr;
297*d1bde9e5SKrzysztof Grobelny 
298*d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
299*d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
300*d1bde9e5SKrzysztof Grobelny             partNumber, "SerialNumber", serialNumber, "Manufacturer",
301*d1bde9e5SKrzysztof Grobelny             manufacturer, "Model", model);
302*d1bde9e5SKrzysztof Grobelny 
303*d1bde9e5SKrzysztof Grobelny         if (!success)
30403913171SWilly Tu         {
30503913171SWilly Tu             messages::internalError(asyncResp->res);
30603913171SWilly Tu             return;
30703913171SWilly Tu         }
308*d1bde9e5SKrzysztof Grobelny 
309*d1bde9e5SKrzysztof Grobelny         if (partNumber != nullptr)
310*d1bde9e5SKrzysztof Grobelny         {
311*d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["PartNumber"] = *partNumber;
31203913171SWilly Tu         }
313*d1bde9e5SKrzysztof Grobelny 
314*d1bde9e5SKrzysztof Grobelny         if (serialNumber != nullptr)
315*d1bde9e5SKrzysztof Grobelny         {
316*d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
31703913171SWilly Tu         }
318*d1bde9e5SKrzysztof Grobelny 
319*d1bde9e5SKrzysztof Grobelny         if (manufacturer != nullptr)
320*d1bde9e5SKrzysztof Grobelny         {
321*d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
322*d1bde9e5SKrzysztof Grobelny         }
323*d1bde9e5SKrzysztof Grobelny 
324*d1bde9e5SKrzysztof Grobelny         if (model != nullptr)
325*d1bde9e5SKrzysztof Grobelny         {
326*d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Model"] = *model;
327*d1bde9e5SKrzysztof Grobelny         }
328*d1bde9e5SKrzysztof Grobelny         });
32903913171SWilly Tu }
33003913171SWilly Tu 
33103913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
33203913171SWilly Tu                             const std::string& connectionName,
33303913171SWilly Tu                             const std::string& path)
33403913171SWilly Tu {
3351e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3361e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3371e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
33803913171SWilly Tu         [asyncResp, path](const boost::system::error_code ec,
3391e1e598dSJonathan Doman                           const bool enabled) {
34003913171SWilly Tu         // this interface isn't necessary, only check it if
34103913171SWilly Tu         // we get a good return
34203913171SWilly Tu         if (ec)
34303913171SWilly Tu         {
34403913171SWilly Tu             return;
34503913171SWilly Tu         }
34603913171SWilly Tu 
3471e1e598dSJonathan Doman         if (!enabled)
34803913171SWilly Tu         {
34903913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
35003913171SWilly Tu         }
3511e1e598dSJonathan Doman         });
35203913171SWilly Tu }
35303913171SWilly Tu 
35403913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
35503913171SWilly Tu                           const std::string& connectionName,
35603913171SWilly Tu                           const std::string& path)
35703913171SWilly Tu {
3581e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3591e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3601e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3611e1e598dSJonathan Doman         [asyncResp](const boost::system::error_code ec, const bool updating) {
36203913171SWilly Tu         // this interface isn't necessary, only check it
36303913171SWilly Tu         // if we get a good return
36403913171SWilly Tu         if (ec)
36503913171SWilly Tu         {
36603913171SWilly Tu             return;
36703913171SWilly Tu         }
36803913171SWilly Tu 
36903913171SWilly Tu         // updating and disabled in the backend shouldn't be
37003913171SWilly Tu         // able to be set at the same time, so we don't need
37103913171SWilly Tu         // to check for the race condition of these two
37203913171SWilly Tu         // calls
3731e1e598dSJonathan Doman         if (updating)
37403913171SWilly Tu         {
37503913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Updating";
37603913171SWilly Tu         }
3771e1e598dSJonathan Doman         });
37803913171SWilly Tu }
37903913171SWilly Tu 
38019b8e9a0SWilly Tu inline std::optional<std::string> convertDriveType(const std::string& type)
38119b8e9a0SWilly Tu {
38219b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
38319b8e9a0SWilly Tu     {
38419b8e9a0SWilly Tu         return "HDD";
38519b8e9a0SWilly Tu     }
38619b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
38719b8e9a0SWilly Tu     {
38819b8e9a0SWilly Tu         return "SSD";
38919b8e9a0SWilly Tu     }
39019b8e9a0SWilly Tu 
39119b8e9a0SWilly Tu     return std::nullopt;
39219b8e9a0SWilly Tu }
39319b8e9a0SWilly Tu 
39419b8e9a0SWilly Tu inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
39519b8e9a0SWilly Tu {
39619b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
39719b8e9a0SWilly Tu     {
39819b8e9a0SWilly Tu         return "SAS";
39919b8e9a0SWilly Tu     }
40019b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
40119b8e9a0SWilly Tu     {
40219b8e9a0SWilly Tu         return "SATA";
40319b8e9a0SWilly Tu     }
40419b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
40519b8e9a0SWilly Tu     {
40619b8e9a0SWilly Tu         return "NVMe";
40719b8e9a0SWilly Tu     }
40819b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
40919b8e9a0SWilly Tu     {
41019b8e9a0SWilly Tu         return "FC";
41119b8e9a0SWilly Tu     }
41219b8e9a0SWilly Tu 
41319b8e9a0SWilly Tu     return std::nullopt;
41419b8e9a0SWilly Tu }
41519b8e9a0SWilly Tu 
41619b8e9a0SWilly Tu inline void
41719b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
41819b8e9a0SWilly Tu                            const std::string& connectionName,
41919b8e9a0SWilly Tu                            const std::string& path)
42019b8e9a0SWilly Tu {
42119b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
42219b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
42319b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
42419b8e9a0SWilly Tu         [asyncResp](const boost::system::error_code ec,
42519b8e9a0SWilly Tu                     const std::vector<
42619b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
42719b8e9a0SWilly Tu                         propertiesList) {
42819b8e9a0SWilly Tu         if (ec)
42919b8e9a0SWilly Tu         {
43019b8e9a0SWilly Tu             // this interface isn't required
43119b8e9a0SWilly Tu             return;
43219b8e9a0SWilly Tu         }
43319b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
43419b8e9a0SWilly Tu                  property : propertiesList)
43519b8e9a0SWilly Tu         {
43619b8e9a0SWilly Tu             const std::string& propertyName = property.first;
43719b8e9a0SWilly Tu             if (propertyName == "Type")
43819b8e9a0SWilly Tu             {
43919b8e9a0SWilly Tu                 const std::string* value =
44019b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
44119b8e9a0SWilly Tu                 if (value == nullptr)
44219b8e9a0SWilly Tu                 {
44319b8e9a0SWilly Tu                     // illegal property
44419b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Type";
44519b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
44619b8e9a0SWilly Tu                     return;
44719b8e9a0SWilly Tu                 }
44819b8e9a0SWilly Tu 
449002d39b4SEd Tanous                 std::optional<std::string> mediaType = convertDriveType(*value);
45019b8e9a0SWilly Tu                 if (!mediaType)
45119b8e9a0SWilly Tu                 {
45219b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
45319b8e9a0SWilly Tu                                      << *value;
45419b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
45519b8e9a0SWilly Tu                     return;
45619b8e9a0SWilly Tu                 }
45719b8e9a0SWilly Tu 
45819b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
45919b8e9a0SWilly Tu             }
46019b8e9a0SWilly Tu             else if (propertyName == "Capacity")
46119b8e9a0SWilly Tu             {
46219b8e9a0SWilly Tu                 const uint64_t* capacity =
46319b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
46419b8e9a0SWilly Tu                 if (capacity == nullptr)
46519b8e9a0SWilly Tu                 {
46619b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Capacity";
46719b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
46819b8e9a0SWilly Tu                     return;
46919b8e9a0SWilly Tu                 }
47019b8e9a0SWilly Tu                 if (*capacity == 0)
47119b8e9a0SWilly Tu                 {
47219b8e9a0SWilly Tu                     // drive capacity not known
47319b8e9a0SWilly Tu                     continue;
47419b8e9a0SWilly Tu                 }
47519b8e9a0SWilly Tu 
47619b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
47719b8e9a0SWilly Tu             }
47819b8e9a0SWilly Tu             else if (propertyName == "Protocol")
47919b8e9a0SWilly Tu             {
48019b8e9a0SWilly Tu                 const std::string* value =
48119b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
48219b8e9a0SWilly Tu                 if (value == nullptr)
48319b8e9a0SWilly Tu                 {
48419b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Protocol";
48519b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
48619b8e9a0SWilly Tu                     return;
48719b8e9a0SWilly Tu                 }
48819b8e9a0SWilly Tu 
489002d39b4SEd Tanous                 std::optional<std::string> proto = convertDriveProtocol(*value);
49019b8e9a0SWilly Tu                 if (!proto)
49119b8e9a0SWilly Tu                 {
492002d39b4SEd Tanous                     BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: "
49319b8e9a0SWilly Tu                                      << *value;
49419b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
49519b8e9a0SWilly Tu                     return;
49619b8e9a0SWilly Tu                 }
49719b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
49819b8e9a0SWilly Tu             }
4993fe4d5ccSJohn Edward Broadbent             else if (propertyName == "PredictedMediaLifeLeftPercent")
5003fe4d5ccSJohn Edward Broadbent             {
5013fe4d5ccSJohn Edward Broadbent                 const uint8_t* lifeLeft =
5023fe4d5ccSJohn Edward Broadbent                     std::get_if<uint8_t>(&property.second);
5033fe4d5ccSJohn Edward Broadbent                 if (lifeLeft == nullptr)
5043fe4d5ccSJohn Edward Broadbent                 {
5053fe4d5ccSJohn Edward Broadbent                     BMCWEB_LOG_ERROR
5063fe4d5ccSJohn Edward Broadbent                         << "Illegal property: PredictedMediaLifeLeftPercent";
5073fe4d5ccSJohn Edward Broadbent                     messages::internalError(asyncResp->res);
5083fe4d5ccSJohn Edward Broadbent                     return;
5093fe4d5ccSJohn Edward Broadbent                 }
5103fe4d5ccSJohn Edward Broadbent                 // 255 means reading the value is not supported
5113fe4d5ccSJohn Edward Broadbent                 if (*lifeLeft != 255)
5123fe4d5ccSJohn Edward Broadbent                 {
5133fe4d5ccSJohn Edward Broadbent                     asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
5143fe4d5ccSJohn Edward Broadbent                         *lifeLeft;
5153fe4d5ccSJohn Edward Broadbent                 }
5163fe4d5ccSJohn Edward Broadbent             }
51719b8e9a0SWilly Tu         }
51819b8e9a0SWilly Tu         });
51919b8e9a0SWilly Tu }
52019b8e9a0SWilly Tu 
521b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
522b53dcd9dSNan Zhou                             const std::string& connectionName,
523b53dcd9dSNan Zhou                             const std::string& path,
524e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
525e56ed6b9SJohn Edward Broadbent {
526e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
527e56ed6b9SJohn Edward Broadbent     {
528e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
529e56ed6b9SJohn Edward Broadbent         {
530e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
531e56ed6b9SJohn Edward Broadbent         }
532e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
533e56ed6b9SJohn Edward Broadbent         {
534e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
535e56ed6b9SJohn Edward Broadbent         }
536e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
537e56ed6b9SJohn Edward Broadbent         {
538e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
539e56ed6b9SJohn Edward Broadbent         }
540e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
541e56ed6b9SJohn Edward Broadbent         {
542e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
543e56ed6b9SJohn Edward Broadbent         }
544e56ed6b9SJohn Edward Broadbent     }
545e56ed6b9SJohn Edward Broadbent }
546e56ed6b9SJohn Edward Broadbent 
5477e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
5487e860f15SJohn Edward Broadbent {
5497e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/Drives/<str>/")
550ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
551002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
552002d39b4SEd Tanous             [&app](const crow::Request& req,
55345ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5547e860f15SJohn Edward Broadbent                    const std::string& driveId) {
5553ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
55645ca1b86SEd Tanous         {
55745ca1b86SEd Tanous             return;
55845ca1b86SEd Tanous         }
5597e860f15SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
560002d39b4SEd Tanous             [asyncResp,
561002d39b4SEd Tanous              driveId](const boost::system::error_code ec,
562b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
5637e860f15SJohn Edward Broadbent             if (ec)
5647e860f15SJohn Edward Broadbent             {
5657e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Drive mapper call error";
5667e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
5677e860f15SJohn Edward Broadbent                 return;
5687e860f15SJohn Edward Broadbent             }
5697e860f15SJohn Edward Broadbent 
57003913171SWilly Tu             auto drive = std::find_if(
5717e860f15SJohn Edward Broadbent                 subtree.begin(), subtree.end(),
572002d39b4SEd Tanous                 [&driveId](
5738cb65f8aSNan Zhou                     const std::pair<std::string,
5748cb65f8aSNan Zhou                                     dbus::utility::MapperServiceMap>& object) {
57503913171SWilly Tu                 return sdbusplus::message::object_path(object.first)
57603913171SWilly Tu                            .filename() == driveId;
5777e860f15SJohn Edward Broadbent                 });
5787e860f15SJohn Edward Broadbent 
57903913171SWilly Tu             if (drive == subtree.end())
5807e860f15SJohn Edward Broadbent             {
581002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
5827e860f15SJohn Edward Broadbent                 return;
5837e860f15SJohn Edward Broadbent             }
5847e860f15SJohn Edward Broadbent 
58503913171SWilly Tu             const std::string& path = drive->first;
5868cb65f8aSNan Zhou             const dbus::utility::MapperServiceMap& connectionNames =
5878cb65f8aSNan Zhou                 drive->second;
5887e860f15SJohn Edward Broadbent 
589002d39b4SEd Tanous             asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
5907e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
591002d39b4SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId;
5927e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = driveId;
5937e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = driveId;
5947e860f15SJohn Edward Broadbent 
5957e860f15SJohn Edward Broadbent             if (connectionNames.size() != 1)
5967e860f15SJohn Edward Broadbent             {
597002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
59803913171SWilly Tu                                  << ", not equal to 1";
5997e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
6007e860f15SJohn Edward Broadbent                 return;
6017e860f15SJohn Edward Broadbent             }
6027e860f15SJohn Edward Broadbent 
6037e860f15SJohn Edward Broadbent             getMainChassisId(
604002d39b4SEd Tanous                 asyncResp, [](const std::string& chassisId,
6057e860f15SJohn Edward Broadbent                               const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
606002d39b4SEd Tanous                     aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
6071476687dSEd Tanous                         "/redfish/v1/Chassis/" + chassisId;
6087e860f15SJohn Edward Broadbent                 });
6097e860f15SJohn Edward Broadbent 
610a25aeccfSNikhil Potade             // default it to Enabled
611a25aeccfSNikhil Potade             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
612a25aeccfSNikhil Potade 
6132ad9c2f6SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
614e284a7c1SJames Feist             health->inventory.emplace_back(path);
6152ad9c2f6SJames Feist             health->populate();
6162ad9c2f6SJames Feist 
617e56ed6b9SJohn Edward Broadbent             addAllDriveInfo(asyncResp, connectionNames[0].first, path,
618e56ed6b9SJohn Edward Broadbent                             connectionNames[0].second);
619a25aeccfSNikhil Potade             },
620a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper",
621a25aeccfSNikhil Potade             "/xyz/openbmc_project/object_mapper",
622a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
623a25aeccfSNikhil Potade             "/xyz/openbmc_project/inventory", int32_t(0),
624a25aeccfSNikhil Potade             std::array<const char*, 1>{
625a25aeccfSNikhil Potade                 "xyz.openbmc_project.Inventory.Item.Drive"});
6267e860f15SJohn Edward Broadbent         });
627a25aeccfSNikhil Potade }
62892903bd4SJohn Edward Broadbent 
62992903bd4SJohn Edward Broadbent /**
63092903bd4SJohn Edward Broadbent  * Chassis drives, this URL will show all the DriveCollection
63192903bd4SJohn Edward Broadbent  * information
63292903bd4SJohn Edward Broadbent  */
633b53dcd9dSNan Zhou inline void chassisDriveCollectionGet(
63492903bd4SJohn Edward Broadbent     crow::App& app, const crow::Request& req,
63592903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
63692903bd4SJohn Edward Broadbent     const std::string& chassisId)
63792903bd4SJohn Edward Broadbent {
6383ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
63992903bd4SJohn Edward Broadbent     {
64092903bd4SJohn Edward Broadbent         return;
64192903bd4SJohn Edward Broadbent     }
64292903bd4SJohn Edward Broadbent 
64392903bd4SJohn Edward Broadbent     // mapper call lambda
64492903bd4SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
64592903bd4SJohn Edward Broadbent         [asyncResp,
64692903bd4SJohn Edward Broadbent          chassisId](const boost::system::error_code ec,
64792903bd4SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
64892903bd4SJohn Edward Broadbent         if (ec)
64992903bd4SJohn Edward Broadbent         {
65092903bd4SJohn Edward Broadbent             if (ec == boost::system::errc::host_unreachable)
65192903bd4SJohn Edward Broadbent             {
65292903bd4SJohn Edward Broadbent                 messages::resourceNotFound(asyncResp->res, "Chassis",
65392903bd4SJohn Edward Broadbent                                            chassisId);
65492903bd4SJohn Edward Broadbent                 return;
65592903bd4SJohn Edward Broadbent             }
65692903bd4SJohn Edward Broadbent             messages::internalError(asyncResp->res);
65792903bd4SJohn Edward Broadbent             return;
65892903bd4SJohn Edward Broadbent         }
65992903bd4SJohn Edward Broadbent 
66092903bd4SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
6618cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
66292903bd4SJohn Edward Broadbent         {
66392903bd4SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
66492903bd4SJohn Edward Broadbent             if (objPath.filename() != chassisId)
66592903bd4SJohn Edward Broadbent             {
66692903bd4SJohn Edward Broadbent                 continue;
66792903bd4SJohn Edward Broadbent             }
66892903bd4SJohn Edward Broadbent 
66992903bd4SJohn Edward Broadbent             if (connectionNames.empty())
67092903bd4SJohn Edward Broadbent             {
67192903bd4SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
67292903bd4SJohn Edward Broadbent                 continue;
67392903bd4SJohn Edward Broadbent             }
67492903bd4SJohn Edward Broadbent 
67592903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.type"] =
67692903bd4SJohn Edward Broadbent                 "#DriveCollection.DriveCollection";
67792903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
67814bd7d9aSJohn Edward Broadbent                 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
67914bd7d9aSJohn Edward Broadbent                                              chassisId, "Drives");
68092903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "Drive Collection";
68192903bd4SJohn Edward Broadbent 
68292903bd4SJohn Edward Broadbent             // Association lambda
68392903bd4SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
68492903bd4SJohn Edward Broadbent                 *crow::connections::systemBus,
68592903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
68692903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
68792903bd4SJohn Edward Broadbent                 [asyncResp, chassisId](const boost::system::error_code ec3,
68892903bd4SJohn Edward Broadbent                                        const std::vector<std::string>& resp) {
68992903bd4SJohn Edward Broadbent                 if (ec3)
69092903bd4SJohn Edward Broadbent                 {
69192903bd4SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
69292903bd4SJohn Edward Broadbent                 }
69392903bd4SJohn Edward Broadbent                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
69492903bd4SJohn Edward Broadbent                 // important if array is empty
69592903bd4SJohn Edward Broadbent                 members = nlohmann::json::array();
69692903bd4SJohn Edward Broadbent 
69792903bd4SJohn Edward Broadbent                 std::vector<std::string> leafNames;
69892903bd4SJohn Edward Broadbent                 for (const auto& drive : resp)
69992903bd4SJohn Edward Broadbent                 {
7008a592810SEd Tanous                     sdbusplus::message::object_path drivePath(drive);
7018a592810SEd Tanous                     leafNames.push_back(drivePath.filename());
70292903bd4SJohn Edward Broadbent                 }
70392903bd4SJohn Edward Broadbent 
70492903bd4SJohn Edward Broadbent                 std::sort(leafNames.begin(), leafNames.end(),
70592903bd4SJohn Edward Broadbent                           AlphanumLess<std::string>());
70692903bd4SJohn Edward Broadbent 
70792903bd4SJohn Edward Broadbent                 for (const auto& leafName : leafNames)
70892903bd4SJohn Edward Broadbent                 {
70992903bd4SJohn Edward Broadbent                     nlohmann::json::object_t member;
71092903bd4SJohn Edward Broadbent                     member["@odata.id"] = crow::utility::urlFromPieces(
71192903bd4SJohn Edward Broadbent                         "redfish", "v1", "Chassis", chassisId, "Drives",
71292903bd4SJohn Edward Broadbent                         leafName);
71392903bd4SJohn Edward Broadbent                     members.push_back(std::move(member));
71492903bd4SJohn Edward Broadbent                     // navigation links will be registered in next patch set
71592903bd4SJohn Edward Broadbent                 }
71692903bd4SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
71792903bd4SJohn Edward Broadbent                 }); // end association lambda
71892903bd4SJohn Edward Broadbent 
71992903bd4SJohn Edward Broadbent         } // end Iterate over all retrieved ObjectPaths
72092903bd4SJohn Edward Broadbent         },
72192903bd4SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
72292903bd4SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
72392903bd4SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
72492903bd4SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", 0,
72592903bd4SJohn Edward Broadbent         std::array<const char*, 2>{
72692903bd4SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Board",
72792903bd4SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Chassis"});
72892903bd4SJohn Edward Broadbent }
72992903bd4SJohn Edward Broadbent 
73092903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
73192903bd4SJohn Edward Broadbent {
73292903bd4SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
73392903bd4SJohn Edward Broadbent         .privileges(redfish::privileges::getDriveCollection)
73492903bd4SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
73592903bd4SJohn Edward Broadbent             std::bind_front(chassisDriveCollectionGet, std::ref(app)));
73692903bd4SJohn Edward Broadbent }
73792903bd4SJohn Edward Broadbent 
738b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
739b53dcd9dSNan Zhou                        const std::string& chassisId,
740b53dcd9dSNan Zhou                        const std::string& driveName,
741e56ed6b9SJohn Edward Broadbent                        const boost::system::error_code ec,
742e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
743e56ed6b9SJohn Edward Broadbent {
744e56ed6b9SJohn Edward Broadbent 
745e56ed6b9SJohn Edward Broadbent     if (ec)
746e56ed6b9SJohn Edward Broadbent     {
747e56ed6b9SJohn Edward Broadbent         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
748e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
749e56ed6b9SJohn Edward Broadbent         return;
750e56ed6b9SJohn Edward Broadbent     }
751e56ed6b9SJohn Edward Broadbent 
752e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
7538cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
754e56ed6b9SJohn Edward Broadbent     {
755e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
756e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
757e56ed6b9SJohn Edward Broadbent         {
758e56ed6b9SJohn Edward Broadbent             continue;
759e56ed6b9SJohn Edward Broadbent         }
760e56ed6b9SJohn Edward Broadbent 
761e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
762e56ed6b9SJohn Edward Broadbent         {
763e56ed6b9SJohn Edward Broadbent             BMCWEB_LOG_ERROR << "Got 0 Connection names";
764e56ed6b9SJohn Edward Broadbent             continue;
765e56ed6b9SJohn Edward Broadbent         }
766e56ed6b9SJohn Edward Broadbent 
767e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
768a0cb40cbSJohn Edward Broadbent             "redfish", "v1", "Chassis", chassisId, "Drives", driveName);
769e56ed6b9SJohn Edward Broadbent 
770e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
771a0cb40cbSJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = driveName;
772e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
773e56ed6b9SJohn Edward Broadbent         // default it to Enabled
774e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
775e56ed6b9SJohn Edward Broadbent 
776e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
777e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
778e56ed6b9SJohn Edward Broadbent             crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId);
779e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
780e56ed6b9SJohn Edward Broadbent 
781e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
782e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
783e56ed6b9SJohn Edward Broadbent     }
784e56ed6b9SJohn Edward Broadbent }
785e56ed6b9SJohn Edward Broadbent 
786b53dcd9dSNan Zhou inline void
787b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
788e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
789e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
790e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
791e56ed6b9SJohn Edward Broadbent {
792e56ed6b9SJohn Edward Broadbent 
793e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
794e56ed6b9SJohn Edward Broadbent     {
795e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
796e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
797e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
798e56ed6b9SJohn Edward Broadbent         {
799e56ed6b9SJohn Edward Broadbent             continue;
800e56ed6b9SJohn Edward Broadbent         }
801e56ed6b9SJohn Edward Broadbent         //  mapper call drive
802e56ed6b9SJohn Edward Broadbent         const std::array<const char*, 1> driveInterface = {
803e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
804e56ed6b9SJohn Edward Broadbent 
805e56ed6b9SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
806e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
807e56ed6b9SJohn Edward Broadbent                 const boost::system::error_code ec,
808e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
809e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
810e56ed6b9SJohn Edward Broadbent             },
811e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper",
812e56ed6b9SJohn Edward Broadbent             "/xyz/openbmc_project/object_mapper",
813e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
814e56ed6b9SJohn Edward Broadbent             "/xyz/openbmc_project/inventory", 0, driveInterface);
815e56ed6b9SJohn Edward Broadbent     }
816e56ed6b9SJohn Edward Broadbent }
817e56ed6b9SJohn Edward Broadbent 
818b53dcd9dSNan Zhou inline void
819b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
820e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
821e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
822e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
823e56ed6b9SJohn Edward Broadbent {
82403810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
825e56ed6b9SJohn Edward Broadbent     {
826e56ed6b9SJohn Edward Broadbent         return;
827e56ed6b9SJohn Edward Broadbent     }
828e56ed6b9SJohn Edward Broadbent     const std::array<const char*, 2> interfaces = {
829e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
830e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
831e56ed6b9SJohn Edward Broadbent 
832e56ed6b9SJohn Edward Broadbent     // mapper call chassis
833e56ed6b9SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
834e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
835e56ed6b9SJohn Edward Broadbent          driveName](const boost::system::error_code ec,
836e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
837e56ed6b9SJohn Edward Broadbent         if (ec)
838e56ed6b9SJohn Edward Broadbent         {
839e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
840e56ed6b9SJohn Edward Broadbent             return;
841e56ed6b9SJohn Edward Broadbent         }
842e56ed6b9SJohn Edward Broadbent 
843e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
8448cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
845e56ed6b9SJohn Edward Broadbent         {
846e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
847e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
848e56ed6b9SJohn Edward Broadbent             {
849e56ed6b9SJohn Edward Broadbent                 continue;
850e56ed6b9SJohn Edward Broadbent             }
851e56ed6b9SJohn Edward Broadbent 
852e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
853e56ed6b9SJohn Edward Broadbent             {
854e56ed6b9SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
855e56ed6b9SJohn Edward Broadbent                 continue;
856e56ed6b9SJohn Edward Broadbent             }
857e56ed6b9SJohn Edward Broadbent 
858e56ed6b9SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
859e56ed6b9SJohn Edward Broadbent                 *crow::connections::systemBus,
860e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
861e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
862e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
863e56ed6b9SJohn Edward Broadbent                  driveName](const boost::system::error_code ec3,
864e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& resp) {
865e56ed6b9SJohn Edward Broadbent                 if (ec3)
866e56ed6b9SJohn Edward Broadbent                 {
867e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
868e56ed6b9SJohn Edward Broadbent                 }
869e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
870e56ed6b9SJohn Edward Broadbent                 });
871e56ed6b9SJohn Edward Broadbent             break;
872e56ed6b9SJohn Edward Broadbent         }
873e56ed6b9SJohn Edward Broadbent         },
874e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
875e56ed6b9SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
876e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
877e56ed6b9SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", 0, interfaces);
878e56ed6b9SJohn Edward Broadbent }
879e56ed6b9SJohn Edward Broadbent 
880e56ed6b9SJohn Edward Broadbent /**
881e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
882e56ed6b9SJohn Edward Broadbent  */
883e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
884e56ed6b9SJohn Edward Broadbent {
885e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
886e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
887e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
888e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
889e56ed6b9SJohn Edward Broadbent }
890e56ed6b9SJohn Edward Broadbent 
891a25aeccfSNikhil Potade } // namespace redfish
892