xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision ef4c65b741724d724452a3a0efe8dff0d450514a)
1a25aeccfSNikhil Potade /*
2a25aeccfSNikhil Potade // Copyright (c) 2019 Intel Corporation
3a25aeccfSNikhil Potade //
4a25aeccfSNikhil Potade // Licensed under the Apache License, Version 2.0 (the "License");
5a25aeccfSNikhil Potade // you may not use this file except in compliance with the License.
6a25aeccfSNikhil Potade // You may obtain a copy of the License at
7a25aeccfSNikhil Potade //
8a25aeccfSNikhil Potade //      http://www.apache.org/licenses/LICENSE-2.0
9a25aeccfSNikhil Potade //
10a25aeccfSNikhil Potade // Unless required by applicable law or agreed to in writing, software
11a25aeccfSNikhil Potade // distributed under the License is distributed on an "AS IS" BASIS,
12a25aeccfSNikhil Potade // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a25aeccfSNikhil Potade // See the License for the specific language governing permissions and
14a25aeccfSNikhil Potade // limitations under the License.
15a25aeccfSNikhil Potade */
16a25aeccfSNikhil Potade #pragma once
17a25aeccfSNikhil Potade 
183ccb3adbSEd Tanous #include "app.hpp"
197a1dbc48SGeorge Liu #include "dbus_utility.hpp"
202ad9c2f6SJames Feist #include "health.hpp"
21a8e884fcSEd Tanous #include "human_sort.hpp"
22e284a7c1SJames Feist #include "openbmc_dbus_rest.hpp"
233ccb3adbSEd Tanous #include "query.hpp"
24a8e884fcSEd Tanous #include "redfish_util.hpp"
253ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
263ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
272ad9c2f6SJames Feist 
28e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
29*ef4c65b7SEd Tanous #include <boost/url/format.hpp>
301e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
31d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
32a25aeccfSNikhil Potade 
337a1dbc48SGeorge Liu #include <array>
347a1dbc48SGeorge Liu #include <string_view>
357a1dbc48SGeorge Liu 
36a25aeccfSNikhil Potade namespace redfish
37a25aeccfSNikhil Potade {
387e860f15SJohn Edward Broadbent inline void requestRoutesStorageCollection(App& app)
39a25aeccfSNikhil Potade {
4022d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
41ed398213SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
427e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
4345ca1b86SEd Tanous             [&app](const crow::Request& req,
4422d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4522d268cbSEd Tanous                    const std::string& systemName) {
463ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
4745ca1b86SEd Tanous         {
4845ca1b86SEd Tanous             return;
4945ca1b86SEd Tanous         }
5022d268cbSEd Tanous         if (systemName != "system")
5122d268cbSEd Tanous         {
5222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
5322d268cbSEd Tanous                                        systemName);
5422d268cbSEd Tanous             return;
5522d268cbSEd Tanous         }
5622d268cbSEd Tanous 
578d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
588d1b46d7Szhanghch05             "#StorageCollection.StorageCollection";
598d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
608d1b46d7Szhanghch05             "/redfish/v1/Systems/system/Storage";
618d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Storage Collection";
621476687dSEd Tanous         nlohmann::json::array_t members;
631476687dSEd Tanous         nlohmann::json::object_t member;
641476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
651476687dSEd Tanous         members.emplace_back(member);
661476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
678d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
687e860f15SJohn Edward Broadbent         });
69a25aeccfSNikhil Potade }
70a25aeccfSNikhil Potade 
71a85afbe1SWilly Tu inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
72a85afbe1SWilly Tu                       const std::shared_ptr<HealthPopulate>& health)
73a25aeccfSNikhil Potade {
747a1dbc48SGeorge Liu     const std::array<std::string_view, 1> interfaces = {
757a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Drive"};
767a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
777a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
78a85afbe1SWilly Tu         [asyncResp, health](
797a1dbc48SGeorge Liu             const boost::system::error_code& ec,
80a85afbe1SWilly Tu             const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
81a25aeccfSNikhil Potade         if (ec)
82a25aeccfSNikhil Potade         {
83a25aeccfSNikhil Potade             BMCWEB_LOG_ERROR << "Drive mapper call error";
84a25aeccfSNikhil Potade             messages::internalError(asyncResp->res);
85a25aeccfSNikhil Potade             return;
86a25aeccfSNikhil Potade         }
872ad9c2f6SJames Feist 
88a85afbe1SWilly Tu         nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
89a85afbe1SWilly Tu         driveArray = nlohmann::json::array();
90a85afbe1SWilly Tu         auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
91a85afbe1SWilly Tu         count = 0;
922ad9c2f6SJames Feist 
93a85afbe1SWilly Tu         health->inventory.insert(health->inventory.end(), driveList.begin(),
94a85afbe1SWilly Tu                                  driveList.end());
95a85afbe1SWilly Tu 
96a85afbe1SWilly Tu         for (const std::string& drive : driveList)
97a25aeccfSNikhil Potade         {
98a85afbe1SWilly Tu             sdbusplus::message::object_path object(drive);
99a85afbe1SWilly Tu             if (object.filename().empty())
100a25aeccfSNikhil Potade             {
101a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
102a85afbe1SWilly Tu                 return;
103a25aeccfSNikhil Potade             }
104a85afbe1SWilly Tu 
105a85afbe1SWilly Tu             nlohmann::json::object_t driveJson;
106*ef4c65b7SEd Tanous             driveJson["@odata.id"] = boost::urls::format(
107*ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/{}",
108eddfc437SWilly Tu                 object.filename());
109b2ba3072SPatrick Williams             driveArray.emplace_back(std::move(driveJson));
110a25aeccfSNikhil Potade         }
111a25aeccfSNikhil Potade 
112a85afbe1SWilly Tu         count = driveArray.size();
1137a1dbc48SGeorge Liu         });
114a85afbe1SWilly Tu }
115e284a7c1SJames Feist 
116a85afbe1SWilly Tu inline void
117a85afbe1SWilly Tu     getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
118a85afbe1SWilly Tu                           const std::shared_ptr<HealthPopulate>& health)
119a85afbe1SWilly Tu {
120e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
121e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.StorageController"};
122e99073f5SGeorge Liu     dbus::utility::getSubTree(
123e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
124002d39b4SEd Tanous         [asyncResp,
125e99073f5SGeorge Liu          health](const boost::system::error_code& ec,
126b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreeResponse& subtree) {
12726f6976fSEd Tanous         if (ec || subtree.empty())
128e284a7c1SJames Feist         {
129d819a420SJames Feist             // doesn't have to be there
130e284a7c1SJames Feist             return;
131e284a7c1SJames Feist         }
132e284a7c1SJames Feist 
133a85afbe1SWilly Tu         nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
134e284a7c1SJames Feist         root = nlohmann::json::array();
135e284a7c1SJames Feist         for (const auto& [path, interfaceDict] : subtree)
136e284a7c1SJames Feist         {
137a85afbe1SWilly Tu             sdbusplus::message::object_path object(path);
138a85afbe1SWilly Tu             std::string id = object.filename();
139a85afbe1SWilly Tu             if (id.empty())
140e284a7c1SJames Feist             {
141a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
142e284a7c1SJames Feist                 return;
143e284a7c1SJames Feist             }
144e284a7c1SJames Feist 
145e284a7c1SJames Feist             if (interfaceDict.size() != 1)
146e284a7c1SJames Feist             {
147a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
148e284a7c1SJames Feist                                  << ", greater than 1";
149e284a7c1SJames Feist                 messages::internalError(asyncResp->res);
150e284a7c1SJames Feist                 return;
151e284a7c1SJames Feist             }
152e284a7c1SJames Feist 
153002d39b4SEd Tanous             const std::string& connectionName = interfaceDict.front().first;
154e284a7c1SJames Feist 
155e284a7c1SJames Feist             size_t index = root.size();
156e284a7c1SJames Feist             nlohmann::json& storageController =
157e284a7c1SJames Feist                 root.emplace_back(nlohmann::json::object());
158e284a7c1SJames Feist 
159e284a7c1SJames Feist             storageController["@odata.type"] =
160e284a7c1SJames Feist                 "#Storage.v1_7_0.StorageController";
161*ef4c65b7SEd Tanous             storageController["@odata.id"] = boost::urls::format(
162*ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/Storage/1#{}",
163*ef4c65b7SEd Tanous                 ("/StorageControllers"_json_pointer / index).to_string());
164e284a7c1SJames Feist             storageController["Name"] = id;
165e284a7c1SJames Feist             storageController["MemberId"] = id;
166e284a7c1SJames Feist             storageController["Status"]["State"] = "Enabled";
167e284a7c1SJames Feist 
1681e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
1691e1e598dSJonathan Doman                 *crow::connections::systemBus, connectionName, path,
1701e1e598dSJonathan Doman                 "xyz.openbmc_project.Inventory.Item", "Present",
1715e7e2dc5SEd Tanous                 [asyncResp, index](const boost::system::error_code& ec2,
172cef57e85SWilly Tu                                    bool isPresent) {
1737e860f15SJohn Edward Broadbent                 // this interface isn't necessary, only check it
1747e860f15SJohn Edward Broadbent                 // if we get a good return
17523a21a1cSEd Tanous                 if (ec2)
176e284a7c1SJames Feist                 {
177e284a7c1SJames Feist                     return;
178e284a7c1SJames Feist                 }
179cef57e85SWilly Tu                 if (!isPresent)
180e284a7c1SJames Feist                 {
181002d39b4SEd Tanous                     asyncResp->res.jsonValue["StorageControllers"][index]
182cef57e85SWilly Tu                                             ["Status"]["State"] = "Absent";
183e284a7c1SJames Feist                 }
1841e1e598dSJonathan Doman                 });
185e284a7c1SJames Feist 
186d1bde9e5SKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
187d1bde9e5SKrzysztof Grobelny                 *crow::connections::systemBus, connectionName, path,
188d1bde9e5SKrzysztof Grobelny                 "xyz.openbmc_project.Inventory.Decorator.Asset",
189a85afbe1SWilly Tu                 [asyncResp, index](
1905e7e2dc5SEd Tanous                     const boost::system::error_code& ec2,
191a85afbe1SWilly Tu                     const std::vector<
192a85afbe1SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
1937e860f15SJohn Edward Broadbent                         propertiesList) {
1947e860f15SJohn Edward Broadbent                 if (ec2)
1957e860f15SJohn Edward Broadbent                 {
1967e860f15SJohn Edward Broadbent                     // this interface isn't necessary
1977e860f15SJohn Edward Broadbent                     return;
1987e860f15SJohn Edward Broadbent                 }
199d1bde9e5SKrzysztof Grobelny 
200d1bde9e5SKrzysztof Grobelny                 const std::string* partNumber = nullptr;
201d1bde9e5SKrzysztof Grobelny                 const std::string* serialNumber = nullptr;
202d1bde9e5SKrzysztof Grobelny                 const std::string* manufacturer = nullptr;
203d1bde9e5SKrzysztof Grobelny                 const std::string* model = nullptr;
204d1bde9e5SKrzysztof Grobelny 
205d1bde9e5SKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
206d1bde9e5SKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), propertiesList,
207d1bde9e5SKrzysztof Grobelny                     "PartNumber", partNumber, "SerialNumber", serialNumber,
208d1bde9e5SKrzysztof Grobelny                     "Manufacturer", manufacturer, "Model", model);
209d1bde9e5SKrzysztof Grobelny 
210d1bde9e5SKrzysztof Grobelny                 if (!success)
2117e860f15SJohn Edward Broadbent                 {
212002d39b4SEd Tanous                     messages::internalError(asyncResp->res);
2137e860f15SJohn Edward Broadbent                     return;
2147e860f15SJohn Edward Broadbent                 }
215d1bde9e5SKrzysztof Grobelny 
216d1bde9e5SKrzysztof Grobelny                 nlohmann::json& controller =
217d1bde9e5SKrzysztof Grobelny                     asyncResp->res.jsonValue["StorageControllers"][index];
218d1bde9e5SKrzysztof Grobelny 
219d1bde9e5SKrzysztof Grobelny                 if (partNumber != nullptr)
220d1bde9e5SKrzysztof Grobelny                 {
221d1bde9e5SKrzysztof Grobelny                     controller["PartNumber"] = *partNumber;
2227e860f15SJohn Edward Broadbent                 }
223d1bde9e5SKrzysztof Grobelny 
224d1bde9e5SKrzysztof Grobelny                 if (serialNumber != nullptr)
225d1bde9e5SKrzysztof Grobelny                 {
226d1bde9e5SKrzysztof Grobelny                     controller["SerialNumber"] = *serialNumber;
2277e860f15SJohn Edward Broadbent                 }
228d1bde9e5SKrzysztof Grobelny 
229d1bde9e5SKrzysztof Grobelny                 if (manufacturer != nullptr)
230d1bde9e5SKrzysztof Grobelny                 {
231d1bde9e5SKrzysztof Grobelny                     controller["Manufacturer"] = *manufacturer;
232d1bde9e5SKrzysztof Grobelny                 }
233d1bde9e5SKrzysztof Grobelny 
234d1bde9e5SKrzysztof Grobelny                 if (model != nullptr)
235d1bde9e5SKrzysztof Grobelny                 {
236d1bde9e5SKrzysztof Grobelny                     controller["Model"] = *model;
237d1bde9e5SKrzysztof Grobelny                 }
238d1bde9e5SKrzysztof Grobelny                 });
2397e860f15SJohn Edward Broadbent         }
2407e860f15SJohn Edward Broadbent 
2417e860f15SJohn Edward Broadbent         // this is done after we know the json array will no longer
2427e860f15SJohn Edward Broadbent         // be resized, as json::array uses vector underneath and we
2437e860f15SJohn Edward Broadbent         // need references to its members that won't change
2447e860f15SJohn Edward Broadbent         size_t count = 0;
245dfababfcSNan Zhou         // Pointer based on |asyncResp->res.jsonValue|
246dfababfcSNan Zhou         nlohmann::json::json_pointer rootPtr =
247dfababfcSNan Zhou             "/StorageControllers"_json_pointer;
2487e860f15SJohn Edward Broadbent         for (const auto& [path, interfaceDict] : subtree)
2497e860f15SJohn Edward Broadbent         {
2507e860f15SJohn Edward Broadbent             auto subHealth = std::make_shared<HealthPopulate>(
251dfababfcSNan Zhou                 asyncResp, rootPtr / count / "Status");
2527e860f15SJohn Edward Broadbent             subHealth->inventory.emplace_back(path);
2537e860f15SJohn Edward Broadbent             health->inventory.emplace_back(path);
2547e860f15SJohn Edward Broadbent             health->children.emplace_back(subHealth);
2557e860f15SJohn Edward Broadbent             count++;
2567e860f15SJohn Edward Broadbent         }
257e99073f5SGeorge Liu         });
258a85afbe1SWilly Tu }
259a85afbe1SWilly Tu 
260a85afbe1SWilly Tu inline void requestRoutesStorage(App& app)
261a85afbe1SWilly Tu {
262a85afbe1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
263a85afbe1SWilly Tu         .privileges(redfish::privileges::getStorage)
264a85afbe1SWilly Tu         .methods(boost::beast::http::verb::get)(
265a85afbe1SWilly Tu             [&app](const crow::Request& req,
266a85afbe1SWilly Tu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2673ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
268a85afbe1SWilly Tu         {
269a85afbe1SWilly Tu             return;
270a85afbe1SWilly Tu         }
271a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
272a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
273a85afbe1SWilly Tu             "/redfish/v1/Systems/system/Storage/1";
274a85afbe1SWilly Tu         asyncResp->res.jsonValue["Name"] = "Storage";
275a85afbe1SWilly Tu         asyncResp->res.jsonValue["Id"] = "1";
276a85afbe1SWilly Tu         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
277a85afbe1SWilly Tu 
278a85afbe1SWilly Tu         auto health = std::make_shared<HealthPopulate>(asyncResp);
279a85afbe1SWilly Tu         health->populate();
280a85afbe1SWilly Tu 
281a85afbe1SWilly Tu         getDrives(asyncResp, health);
282a85afbe1SWilly Tu         getStorageControllers(asyncResp, health);
2837e860f15SJohn Edward Broadbent         });
2847e860f15SJohn Edward Broadbent }
2857e860f15SJohn Edward Broadbent 
28603913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28703913171SWilly Tu                           const std::string& connectionName,
28803913171SWilly Tu                           const std::string& path)
28903913171SWilly Tu {
290d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
291d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, path,
292d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Asset",
2935e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
294168e20c1SEd Tanous                     const std::vector<
295168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
29603913171SWilly Tu                         propertiesList) {
29703913171SWilly Tu         if (ec)
29803913171SWilly Tu         {
29903913171SWilly Tu             // this interface isn't necessary
30003913171SWilly Tu             return;
30103913171SWilly Tu         }
302d1bde9e5SKrzysztof Grobelny 
303d1bde9e5SKrzysztof Grobelny         const std::string* partNumber = nullptr;
304d1bde9e5SKrzysztof Grobelny         const std::string* serialNumber = nullptr;
305d1bde9e5SKrzysztof Grobelny         const std::string* manufacturer = nullptr;
306d1bde9e5SKrzysztof Grobelny         const std::string* model = nullptr;
307d1bde9e5SKrzysztof Grobelny 
308d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
309d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
310d1bde9e5SKrzysztof Grobelny             partNumber, "SerialNumber", serialNumber, "Manufacturer",
311d1bde9e5SKrzysztof Grobelny             manufacturer, "Model", model);
312d1bde9e5SKrzysztof Grobelny 
313d1bde9e5SKrzysztof Grobelny         if (!success)
31403913171SWilly Tu         {
31503913171SWilly Tu             messages::internalError(asyncResp->res);
31603913171SWilly Tu             return;
31703913171SWilly Tu         }
318d1bde9e5SKrzysztof Grobelny 
319d1bde9e5SKrzysztof Grobelny         if (partNumber != nullptr)
320d1bde9e5SKrzysztof Grobelny         {
321d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["PartNumber"] = *partNumber;
32203913171SWilly Tu         }
323d1bde9e5SKrzysztof Grobelny 
324d1bde9e5SKrzysztof Grobelny         if (serialNumber != nullptr)
325d1bde9e5SKrzysztof Grobelny         {
326d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
32703913171SWilly Tu         }
328d1bde9e5SKrzysztof Grobelny 
329d1bde9e5SKrzysztof Grobelny         if (manufacturer != nullptr)
330d1bde9e5SKrzysztof Grobelny         {
331d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
332d1bde9e5SKrzysztof Grobelny         }
333d1bde9e5SKrzysztof Grobelny 
334d1bde9e5SKrzysztof Grobelny         if (model != nullptr)
335d1bde9e5SKrzysztof Grobelny         {
336d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Model"] = *model;
337d1bde9e5SKrzysztof Grobelny         }
338d1bde9e5SKrzysztof Grobelny         });
33903913171SWilly Tu }
34003913171SWilly Tu 
34103913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34203913171SWilly Tu                             const std::string& connectionName,
34303913171SWilly Tu                             const std::string& path)
34403913171SWilly Tu {
3451e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3461e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3471e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
3485e7e2dc5SEd Tanous         [asyncResp, path](const boost::system::error_code& ec,
349cef57e85SWilly Tu                           const bool isPresent) {
35003913171SWilly Tu         // this interface isn't necessary, only check it if
35103913171SWilly Tu         // we get a good return
35203913171SWilly Tu         if (ec)
35303913171SWilly Tu         {
35403913171SWilly Tu             return;
35503913171SWilly Tu         }
35603913171SWilly Tu 
357cef57e85SWilly Tu         if (!isPresent)
35803913171SWilly Tu         {
359cef57e85SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Absent";
36003913171SWilly Tu         }
3611e1e598dSJonathan Doman         });
36203913171SWilly Tu }
36303913171SWilly Tu 
36403913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36503913171SWilly Tu                           const std::string& connectionName,
36603913171SWilly Tu                           const std::string& path)
36703913171SWilly Tu {
3681e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3691e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3701e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3715e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec, const bool updating) {
37203913171SWilly Tu         // this interface isn't necessary, only check it
37303913171SWilly Tu         // if we get a good return
37403913171SWilly Tu         if (ec)
37503913171SWilly Tu         {
37603913171SWilly Tu             return;
37703913171SWilly Tu         }
37803913171SWilly Tu 
37903913171SWilly Tu         // updating and disabled in the backend shouldn't be
38003913171SWilly Tu         // able to be set at the same time, so we don't need
38103913171SWilly Tu         // to check for the race condition of these two
38203913171SWilly Tu         // calls
3831e1e598dSJonathan Doman         if (updating)
38403913171SWilly Tu         {
38503913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Updating";
38603913171SWilly Tu         }
3871e1e598dSJonathan Doman         });
38803913171SWilly Tu }
38903913171SWilly Tu 
39019b8e9a0SWilly Tu inline std::optional<std::string> convertDriveType(const std::string& type)
39119b8e9a0SWilly Tu {
39219b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
39319b8e9a0SWilly Tu     {
39419b8e9a0SWilly Tu         return "HDD";
39519b8e9a0SWilly Tu     }
39619b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
39719b8e9a0SWilly Tu     {
39819b8e9a0SWilly Tu         return "SSD";
39919b8e9a0SWilly Tu     }
40019b8e9a0SWilly Tu 
40119b8e9a0SWilly Tu     return std::nullopt;
40219b8e9a0SWilly Tu }
40319b8e9a0SWilly Tu 
40419b8e9a0SWilly Tu inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
40519b8e9a0SWilly Tu {
40619b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
40719b8e9a0SWilly Tu     {
40819b8e9a0SWilly Tu         return "SAS";
40919b8e9a0SWilly Tu     }
41019b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
41119b8e9a0SWilly Tu     {
41219b8e9a0SWilly Tu         return "SATA";
41319b8e9a0SWilly Tu     }
41419b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
41519b8e9a0SWilly Tu     {
41619b8e9a0SWilly Tu         return "NVMe";
41719b8e9a0SWilly Tu     }
41819b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
41919b8e9a0SWilly Tu     {
42019b8e9a0SWilly Tu         return "FC";
42119b8e9a0SWilly Tu     }
42219b8e9a0SWilly Tu 
42319b8e9a0SWilly Tu     return std::nullopt;
42419b8e9a0SWilly Tu }
42519b8e9a0SWilly Tu 
42619b8e9a0SWilly Tu inline void
42719b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
42819b8e9a0SWilly Tu                            const std::string& connectionName,
42919b8e9a0SWilly Tu                            const std::string& path)
43019b8e9a0SWilly Tu {
43119b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
43219b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
43319b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
4345e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
43519b8e9a0SWilly Tu                     const std::vector<
43619b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
43719b8e9a0SWilly Tu                         propertiesList) {
43819b8e9a0SWilly Tu         if (ec)
43919b8e9a0SWilly Tu         {
44019b8e9a0SWilly Tu             // this interface isn't required
44119b8e9a0SWilly Tu             return;
44219b8e9a0SWilly Tu         }
44319b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
44419b8e9a0SWilly Tu                  property : propertiesList)
44519b8e9a0SWilly Tu         {
44619b8e9a0SWilly Tu             const std::string& propertyName = property.first;
44719b8e9a0SWilly Tu             if (propertyName == "Type")
44819b8e9a0SWilly Tu             {
44919b8e9a0SWilly Tu                 const std::string* value =
45019b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
45119b8e9a0SWilly Tu                 if (value == nullptr)
45219b8e9a0SWilly Tu                 {
45319b8e9a0SWilly Tu                     // illegal property
45419b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Type";
45519b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
45619b8e9a0SWilly Tu                     return;
45719b8e9a0SWilly Tu                 }
45819b8e9a0SWilly Tu 
459002d39b4SEd Tanous                 std::optional<std::string> mediaType = convertDriveType(*value);
46019b8e9a0SWilly Tu                 if (!mediaType)
46119b8e9a0SWilly Tu                 {
46219b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
46319b8e9a0SWilly Tu                                      << *value;
46419b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
46519b8e9a0SWilly Tu                     return;
46619b8e9a0SWilly Tu                 }
46719b8e9a0SWilly Tu 
46819b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
46919b8e9a0SWilly Tu             }
47019b8e9a0SWilly Tu             else if (propertyName == "Capacity")
47119b8e9a0SWilly Tu             {
47219b8e9a0SWilly Tu                 const uint64_t* capacity =
47319b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
47419b8e9a0SWilly Tu                 if (capacity == nullptr)
47519b8e9a0SWilly Tu                 {
47619b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Capacity";
47719b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
47819b8e9a0SWilly Tu                     return;
47919b8e9a0SWilly Tu                 }
48019b8e9a0SWilly Tu                 if (*capacity == 0)
48119b8e9a0SWilly Tu                 {
48219b8e9a0SWilly Tu                     // drive capacity not known
48319b8e9a0SWilly Tu                     continue;
48419b8e9a0SWilly Tu                 }
48519b8e9a0SWilly Tu 
48619b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
48719b8e9a0SWilly Tu             }
48819b8e9a0SWilly Tu             else if (propertyName == "Protocol")
48919b8e9a0SWilly Tu             {
49019b8e9a0SWilly Tu                 const std::string* value =
49119b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
49219b8e9a0SWilly Tu                 if (value == nullptr)
49319b8e9a0SWilly Tu                 {
49419b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Protocol";
49519b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
49619b8e9a0SWilly Tu                     return;
49719b8e9a0SWilly Tu                 }
49819b8e9a0SWilly Tu 
499002d39b4SEd Tanous                 std::optional<std::string> proto = convertDriveProtocol(*value);
50019b8e9a0SWilly Tu                 if (!proto)
50119b8e9a0SWilly Tu                 {
502002d39b4SEd Tanous                     BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: "
50319b8e9a0SWilly Tu                                      << *value;
50419b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
50519b8e9a0SWilly Tu                     return;
50619b8e9a0SWilly Tu                 }
50719b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
50819b8e9a0SWilly Tu             }
5093fe4d5ccSJohn Edward Broadbent             else if (propertyName == "PredictedMediaLifeLeftPercent")
5103fe4d5ccSJohn Edward Broadbent             {
5113fe4d5ccSJohn Edward Broadbent                 const uint8_t* lifeLeft =
5123fe4d5ccSJohn Edward Broadbent                     std::get_if<uint8_t>(&property.second);
5133fe4d5ccSJohn Edward Broadbent                 if (lifeLeft == nullptr)
5143fe4d5ccSJohn Edward Broadbent                 {
5153fe4d5ccSJohn Edward Broadbent                     BMCWEB_LOG_ERROR
5163fe4d5ccSJohn Edward Broadbent                         << "Illegal property: PredictedMediaLifeLeftPercent";
5173fe4d5ccSJohn Edward Broadbent                     messages::internalError(asyncResp->res);
5183fe4d5ccSJohn Edward Broadbent                     return;
5193fe4d5ccSJohn Edward Broadbent                 }
5203fe4d5ccSJohn Edward Broadbent                 // 255 means reading the value is not supported
5213fe4d5ccSJohn Edward Broadbent                 if (*lifeLeft != 255)
5223fe4d5ccSJohn Edward Broadbent                 {
5233fe4d5ccSJohn Edward Broadbent                     asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
5243fe4d5ccSJohn Edward Broadbent                         *lifeLeft;
5253fe4d5ccSJohn Edward Broadbent                 }
5263fe4d5ccSJohn Edward Broadbent             }
52719b8e9a0SWilly Tu         }
52819b8e9a0SWilly Tu         });
52919b8e9a0SWilly Tu }
53019b8e9a0SWilly Tu 
531b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
532b53dcd9dSNan Zhou                             const std::string& connectionName,
533b53dcd9dSNan Zhou                             const std::string& path,
534e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
535e56ed6b9SJohn Edward Broadbent {
536e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
537e56ed6b9SJohn Edward Broadbent     {
538e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
539e56ed6b9SJohn Edward Broadbent         {
540e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
541e56ed6b9SJohn Edward Broadbent         }
542e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
543e56ed6b9SJohn Edward Broadbent         {
544e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
545e56ed6b9SJohn Edward Broadbent         }
546e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
547e56ed6b9SJohn Edward Broadbent         {
548e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
549e56ed6b9SJohn Edward Broadbent         }
550e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
551e56ed6b9SJohn Edward Broadbent         {
552e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
553e56ed6b9SJohn Edward Broadbent         }
554e56ed6b9SJohn Edward Broadbent     }
555e56ed6b9SJohn Edward Broadbent }
556e56ed6b9SJohn Edward Broadbent 
5577e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
5587e860f15SJohn Edward Broadbent {
55922d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
560ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
561002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
562002d39b4SEd Tanous             [&app](const crow::Request& req,
56345ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
56422d268cbSEd Tanous                    const std::string& systemName, const std::string& driveId) {
5653ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
56645ca1b86SEd Tanous         {
56745ca1b86SEd Tanous             return;
56845ca1b86SEd Tanous         }
56922d268cbSEd Tanous         if (systemName != "system")
57022d268cbSEd Tanous         {
57122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
57222d268cbSEd Tanous                                        systemName);
57322d268cbSEd Tanous             return;
57422d268cbSEd Tanous         }
57522d268cbSEd Tanous 
576e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
577e99073f5SGeorge Liu             "xyz.openbmc_project.Inventory.Item.Drive"};
578e99073f5SGeorge Liu         dbus::utility::getSubTree(
579e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, interfaces,
580002d39b4SEd Tanous             [asyncResp,
581e99073f5SGeorge Liu              driveId](const boost::system::error_code& ec,
582b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
5837e860f15SJohn Edward Broadbent             if (ec)
5847e860f15SJohn Edward Broadbent             {
5857e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Drive mapper call error";
5867e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
5877e860f15SJohn Edward Broadbent                 return;
5887e860f15SJohn Edward Broadbent             }
5897e860f15SJohn Edward Broadbent 
59003913171SWilly Tu             auto drive = std::find_if(
5917e860f15SJohn Edward Broadbent                 subtree.begin(), subtree.end(),
592002d39b4SEd Tanous                 [&driveId](
5938cb65f8aSNan Zhou                     const std::pair<std::string,
5948cb65f8aSNan Zhou                                     dbus::utility::MapperServiceMap>& object) {
59503913171SWilly Tu                 return sdbusplus::message::object_path(object.first)
59603913171SWilly Tu                            .filename() == driveId;
5977e860f15SJohn Edward Broadbent                 });
5987e860f15SJohn Edward Broadbent 
59903913171SWilly Tu             if (drive == subtree.end())
6007e860f15SJohn Edward Broadbent             {
601002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
6027e860f15SJohn Edward Broadbent                 return;
6037e860f15SJohn Edward Broadbent             }
6047e860f15SJohn Edward Broadbent 
60503913171SWilly Tu             const std::string& path = drive->first;
6068cb65f8aSNan Zhou             const dbus::utility::MapperServiceMap& connectionNames =
6078cb65f8aSNan Zhou                 drive->second;
6087e860f15SJohn Edward Broadbent 
609002d39b4SEd Tanous             asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
610*ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
611*ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/{}", driveId);
6127e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = driveId;
6137e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = driveId;
6147e860f15SJohn Edward Broadbent 
6157e860f15SJohn Edward Broadbent             if (connectionNames.size() != 1)
6167e860f15SJohn Edward Broadbent             {
617002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
61803913171SWilly Tu                                  << ", not equal to 1";
6197e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
6207e860f15SJohn Edward Broadbent                 return;
6217e860f15SJohn Edward Broadbent             }
6227e860f15SJohn Edward Broadbent 
6237e860f15SJohn Edward Broadbent             getMainChassisId(
624*ef4c65b7SEd Tanous                 asyncResp,
625*ef4c65b7SEd Tanous                 [](const std::string& chassisId,
6267e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
627002d39b4SEd Tanous                 aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
628*ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
6297e860f15SJohn Edward Broadbent                 });
6307e860f15SJohn Edward Broadbent 
631a25aeccfSNikhil Potade             // default it to Enabled
632a25aeccfSNikhil Potade             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
633a25aeccfSNikhil Potade 
6342ad9c2f6SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
635e284a7c1SJames Feist             health->inventory.emplace_back(path);
6362ad9c2f6SJames Feist             health->populate();
6372ad9c2f6SJames Feist 
638e56ed6b9SJohn Edward Broadbent             addAllDriveInfo(asyncResp, connectionNames[0].first, path,
639e56ed6b9SJohn Edward Broadbent                             connectionNames[0].second);
640e99073f5SGeorge Liu             });
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
659e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
660e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Board",
661e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Chassis"};
662e99073f5SGeorge Liu     dbus::utility::getSubTree(
663e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
66492903bd4SJohn Edward Broadbent         [asyncResp,
665e99073f5SGeorge Liu          chassisId](const boost::system::error_code& ec,
66692903bd4SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
66792903bd4SJohn Edward Broadbent         if (ec)
66892903bd4SJohn Edward Broadbent         {
66992903bd4SJohn Edward Broadbent             if (ec == boost::system::errc::host_unreachable)
67092903bd4SJohn Edward Broadbent             {
67192903bd4SJohn Edward Broadbent                 messages::resourceNotFound(asyncResp->res, "Chassis",
67292903bd4SJohn Edward Broadbent                                            chassisId);
67392903bd4SJohn Edward Broadbent                 return;
67492903bd4SJohn Edward Broadbent             }
67592903bd4SJohn Edward Broadbent             messages::internalError(asyncResp->res);
67692903bd4SJohn Edward Broadbent             return;
67792903bd4SJohn Edward Broadbent         }
67892903bd4SJohn Edward Broadbent 
67992903bd4SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
6808cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
68192903bd4SJohn Edward Broadbent         {
68292903bd4SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
68392903bd4SJohn Edward Broadbent             if (objPath.filename() != chassisId)
68492903bd4SJohn Edward Broadbent             {
68592903bd4SJohn Edward Broadbent                 continue;
68692903bd4SJohn Edward Broadbent             }
68792903bd4SJohn Edward Broadbent 
68892903bd4SJohn Edward Broadbent             if (connectionNames.empty())
68992903bd4SJohn Edward Broadbent             {
69092903bd4SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
69192903bd4SJohn Edward Broadbent                 continue;
69292903bd4SJohn Edward Broadbent             }
69392903bd4SJohn Edward Broadbent 
69492903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.type"] =
69592903bd4SJohn Edward Broadbent                 "#DriveCollection.DriveCollection";
69692903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
697*ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}/Drives", chassisId);
69892903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "Drive Collection";
69992903bd4SJohn Edward Broadbent 
70092903bd4SJohn Edward Broadbent             // Association lambda
7016c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
7026c3e9451SGeorge Liu                 path + "/drive",
7036c3e9451SGeorge Liu                 [asyncResp,
7046c3e9451SGeorge Liu                  chassisId](const boost::system::error_code& ec3,
7056c3e9451SGeorge Liu                             const dbus::utility::MapperEndPoints& resp) {
70692903bd4SJohn Edward Broadbent                 if (ec3)
70792903bd4SJohn Edward Broadbent                 {
70892903bd4SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
70992903bd4SJohn Edward Broadbent                 }
71092903bd4SJohn Edward Broadbent                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
71192903bd4SJohn Edward Broadbent                 // important if array is empty
71292903bd4SJohn Edward Broadbent                 members = nlohmann::json::array();
71392903bd4SJohn Edward Broadbent 
71492903bd4SJohn Edward Broadbent                 std::vector<std::string> leafNames;
71592903bd4SJohn Edward Broadbent                 for (const auto& drive : resp)
71692903bd4SJohn Edward Broadbent                 {
7178a592810SEd Tanous                     sdbusplus::message::object_path drivePath(drive);
7188a592810SEd Tanous                     leafNames.push_back(drivePath.filename());
71992903bd4SJohn Edward Broadbent                 }
72092903bd4SJohn Edward Broadbent 
72192903bd4SJohn Edward Broadbent                 std::sort(leafNames.begin(), leafNames.end(),
72292903bd4SJohn Edward Broadbent                           AlphanumLess<std::string>());
72392903bd4SJohn Edward Broadbent 
72492903bd4SJohn Edward Broadbent                 for (const auto& leafName : leafNames)
72592903bd4SJohn Edward Broadbent                 {
72692903bd4SJohn Edward Broadbent                     nlohmann::json::object_t member;
727*ef4c65b7SEd Tanous                     member["@odata.id"] =
728*ef4c65b7SEd Tanous                         boost::urls::format("/redfish/v1/Chassis/{}/Drives/{}",
729*ef4c65b7SEd Tanous                                             chassisId, leafName);
730b2ba3072SPatrick Williams                     members.emplace_back(std::move(member));
73192903bd4SJohn Edward Broadbent                     // navigation links will be registered in next patch set
73292903bd4SJohn Edward Broadbent                 }
73392903bd4SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
73492903bd4SJohn Edward Broadbent                 }); // end association lambda
73592903bd4SJohn Edward Broadbent 
73692903bd4SJohn Edward Broadbent         }           // end Iterate over all retrieved ObjectPaths
737e99073f5SGeorge Liu         });
73892903bd4SJohn Edward Broadbent }
73992903bd4SJohn Edward Broadbent 
74092903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
74192903bd4SJohn Edward Broadbent {
74292903bd4SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
74392903bd4SJohn Edward Broadbent         .privileges(redfish::privileges::getDriveCollection)
74492903bd4SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
74592903bd4SJohn Edward Broadbent             std::bind_front(chassisDriveCollectionGet, std::ref(app)));
74692903bd4SJohn Edward Broadbent }
74792903bd4SJohn Edward Broadbent 
748b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
749b53dcd9dSNan Zhou                        const std::string& chassisId,
750b53dcd9dSNan Zhou                        const std::string& driveName,
7515e7e2dc5SEd Tanous                        const boost::system::error_code& ec,
752e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
753e56ed6b9SJohn Edward Broadbent {
754e56ed6b9SJohn Edward Broadbent     if (ec)
755e56ed6b9SJohn Edward Broadbent     {
756e56ed6b9SJohn Edward Broadbent         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
757e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
758e56ed6b9SJohn Edward Broadbent         return;
759e56ed6b9SJohn Edward Broadbent     }
760e56ed6b9SJohn Edward Broadbent 
761e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
7628cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
763e56ed6b9SJohn Edward Broadbent     {
764e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
765e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
766e56ed6b9SJohn Edward Broadbent         {
767e56ed6b9SJohn Edward Broadbent             continue;
768e56ed6b9SJohn Edward Broadbent         }
769e56ed6b9SJohn Edward Broadbent 
770e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
771e56ed6b9SJohn Edward Broadbent         {
772e56ed6b9SJohn Edward Broadbent             BMCWEB_LOG_ERROR << "Got 0 Connection names";
773e56ed6b9SJohn Edward Broadbent             continue;
774e56ed6b9SJohn Edward Broadbent         }
775e56ed6b9SJohn Edward Broadbent 
776*ef4c65b7SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
777*ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/Drives/{}", chassisId, driveName);
778e56ed6b9SJohn Edward Broadbent 
779e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
780a0cb40cbSJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = driveName;
781e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
782e56ed6b9SJohn Edward Broadbent         // default it to Enabled
783e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
784e56ed6b9SJohn Edward Broadbent 
785e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
786e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
787*ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
788e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
789e56ed6b9SJohn Edward Broadbent 
790e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
791e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
792e56ed6b9SJohn Edward Broadbent     }
793e56ed6b9SJohn Edward Broadbent }
794e56ed6b9SJohn Edward Broadbent 
795b53dcd9dSNan Zhou inline void
796b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
797e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
798e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
799e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
800e56ed6b9SJohn Edward Broadbent {
801e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
802e56ed6b9SJohn Edward Broadbent     {
803e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
804e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
805e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
806e56ed6b9SJohn Edward Broadbent         {
807e56ed6b9SJohn Edward Broadbent             continue;
808e56ed6b9SJohn Edward Broadbent         }
809e56ed6b9SJohn Edward Broadbent         //  mapper call drive
810e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> driveInterface = {
811e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
812e99073f5SGeorge Liu         dbus::utility::getSubTree(
813e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, driveInterface,
814e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
815e99073f5SGeorge Liu                 const boost::system::error_code& ec,
816e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
817e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
818e99073f5SGeorge Liu             });
819e56ed6b9SJohn Edward Broadbent     }
820e56ed6b9SJohn Edward Broadbent }
821e56ed6b9SJohn Edward Broadbent 
822b53dcd9dSNan Zhou inline void
823b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
824e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
825e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
826e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
827e56ed6b9SJohn Edward Broadbent {
82803810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
829e56ed6b9SJohn Edward Broadbent     {
830e56ed6b9SJohn Edward Broadbent         return;
831e56ed6b9SJohn Edward Broadbent     }
832e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
833e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
834e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
835e56ed6b9SJohn Edward Broadbent 
836e56ed6b9SJohn Edward Broadbent     // mapper call chassis
837e99073f5SGeorge Liu     dbus::utility::getSubTree(
838e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
839e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
840e99073f5SGeorge Liu          driveName](const boost::system::error_code& ec,
841e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
842e56ed6b9SJohn Edward Broadbent         if (ec)
843e56ed6b9SJohn Edward Broadbent         {
844e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
845e56ed6b9SJohn Edward Broadbent             return;
846e56ed6b9SJohn Edward Broadbent         }
847e56ed6b9SJohn Edward Broadbent 
848e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
8498cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
850e56ed6b9SJohn Edward Broadbent         {
851e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
852e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
853e56ed6b9SJohn Edward Broadbent             {
854e56ed6b9SJohn Edward Broadbent                 continue;
855e56ed6b9SJohn Edward Broadbent             }
856e56ed6b9SJohn Edward Broadbent 
857e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
858e56ed6b9SJohn Edward Broadbent             {
859e56ed6b9SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
860e56ed6b9SJohn Edward Broadbent                 continue;
861e56ed6b9SJohn Edward Broadbent             }
862e56ed6b9SJohn Edward Broadbent 
8636c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
8646c3e9451SGeorge Liu                 path + "/drive",
865e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
8665e7e2dc5SEd Tanous                  driveName](const boost::system::error_code& ec3,
8676c3e9451SGeorge Liu                             const dbus::utility::MapperEndPoints& resp) {
868e56ed6b9SJohn Edward Broadbent                 if (ec3)
869e56ed6b9SJohn Edward Broadbent                 {
870e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
871e56ed6b9SJohn Edward Broadbent                 }
872e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
873e56ed6b9SJohn Edward Broadbent                 });
874e56ed6b9SJohn Edward Broadbent             break;
875e56ed6b9SJohn Edward Broadbent         }
876e99073f5SGeorge Liu         });
877e56ed6b9SJohn Edward Broadbent }
878e56ed6b9SJohn Edward Broadbent 
879e56ed6b9SJohn Edward Broadbent /**
880e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
881e56ed6b9SJohn Edward Broadbent  */
882e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
883e56ed6b9SJohn Edward Broadbent {
884e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
885e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
886e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
887e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
888e56ed6b9SJohn Edward Broadbent }
889e56ed6b9SJohn Edward Broadbent 
890a25aeccfSNikhil Potade } // namespace redfish
891