xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision eddfc437edfc871b469199552bdeb0d65ee2dcf3)
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>
291e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
30d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
31a25aeccfSNikhil Potade 
327a1dbc48SGeorge Liu #include <array>
337a1dbc48SGeorge Liu #include <string_view>
347a1dbc48SGeorge Liu 
35a25aeccfSNikhil Potade namespace redfish
36a25aeccfSNikhil Potade {
377e860f15SJohn Edward Broadbent inline void requestRoutesStorageCollection(App& app)
38a25aeccfSNikhil Potade {
3922d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
40ed398213SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
417e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
4245ca1b86SEd Tanous             [&app](const crow::Request& req,
4322d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4422d268cbSEd Tanous                    const std::string& systemName) {
453ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
4645ca1b86SEd Tanous         {
4745ca1b86SEd Tanous             return;
4845ca1b86SEd Tanous         }
4922d268cbSEd Tanous         if (systemName != "system")
5022d268cbSEd Tanous         {
5122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
5222d268cbSEd Tanous                                        systemName);
5322d268cbSEd Tanous             return;
5422d268cbSEd Tanous         }
5522d268cbSEd Tanous 
568d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
578d1b46d7Szhanghch05             "#StorageCollection.StorageCollection";
588d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
598d1b46d7Szhanghch05             "/redfish/v1/Systems/system/Storage";
608d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Storage Collection";
611476687dSEd Tanous         nlohmann::json::array_t members;
621476687dSEd Tanous         nlohmann::json::object_t member;
631476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
641476687dSEd Tanous         members.emplace_back(member);
651476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
668d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
677e860f15SJohn Edward Broadbent         });
68a25aeccfSNikhil Potade }
69a25aeccfSNikhil Potade 
70a85afbe1SWilly Tu inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
71a85afbe1SWilly Tu                       const std::shared_ptr<HealthPopulate>& health)
72a25aeccfSNikhil Potade {
737a1dbc48SGeorge Liu     const std::array<std::string_view, 1> interfaces = {
747a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Drive"};
757a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
767a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
77a85afbe1SWilly Tu         [asyncResp, health](
787a1dbc48SGeorge Liu             const boost::system::error_code& ec,
79a85afbe1SWilly Tu             const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
80a25aeccfSNikhil Potade         if (ec)
81a25aeccfSNikhil Potade         {
82a25aeccfSNikhil Potade             BMCWEB_LOG_ERROR << "Drive mapper call error";
83a25aeccfSNikhil Potade             messages::internalError(asyncResp->res);
84a25aeccfSNikhil Potade             return;
85a25aeccfSNikhil Potade         }
862ad9c2f6SJames Feist 
87a85afbe1SWilly Tu         nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
88a85afbe1SWilly Tu         driveArray = nlohmann::json::array();
89a85afbe1SWilly Tu         auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
90a85afbe1SWilly Tu         count = 0;
912ad9c2f6SJames Feist 
92a85afbe1SWilly Tu         health->inventory.insert(health->inventory.end(), driveList.begin(),
93a85afbe1SWilly Tu                                  driveList.end());
94a85afbe1SWilly Tu 
95a85afbe1SWilly Tu         for (const std::string& drive : driveList)
96a25aeccfSNikhil Potade         {
97a85afbe1SWilly Tu             sdbusplus::message::object_path object(drive);
98a85afbe1SWilly Tu             if (object.filename().empty())
99a25aeccfSNikhil Potade             {
100a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
101a85afbe1SWilly Tu                 return;
102a25aeccfSNikhil Potade             }
103a85afbe1SWilly Tu 
104a85afbe1SWilly Tu             nlohmann::json::object_t driveJson;
105*eddfc437SWilly Tu             driveJson["@odata.id"] = crow::utility::urlFromPieces(
106*eddfc437SWilly Tu                 "redfish", "v1", "Systems", "system", "Storage", "1", "Drives",
107*eddfc437SWilly Tu                 object.filename());
108a85afbe1SWilly Tu             driveArray.push_back(std::move(driveJson));
109a25aeccfSNikhil Potade         }
110a25aeccfSNikhil Potade 
111a85afbe1SWilly Tu         count = driveArray.size();
1127a1dbc48SGeorge Liu         });
113a85afbe1SWilly Tu }
114e284a7c1SJames Feist 
115a85afbe1SWilly Tu inline void
116a85afbe1SWilly Tu     getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
117a85afbe1SWilly Tu                           const std::shared_ptr<HealthPopulate>& health)
118a85afbe1SWilly Tu {
119e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
120e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.StorageController"};
121e99073f5SGeorge Liu     dbus::utility::getSubTree(
122e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
123002d39b4SEd Tanous         [asyncResp,
124e99073f5SGeorge Liu          health](const boost::system::error_code& ec,
125b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreeResponse& subtree) {
12626f6976fSEd Tanous         if (ec || subtree.empty())
127e284a7c1SJames Feist         {
128d819a420SJames Feist             // doesn't have to be there
129e284a7c1SJames Feist             return;
130e284a7c1SJames Feist         }
131e284a7c1SJames Feist 
132a85afbe1SWilly Tu         nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
133e284a7c1SJames Feist         root = nlohmann::json::array();
134e284a7c1SJames Feist         for (const auto& [path, interfaceDict] : subtree)
135e284a7c1SJames Feist         {
136a85afbe1SWilly Tu             sdbusplus::message::object_path object(path);
137a85afbe1SWilly Tu             std::string id = object.filename();
138a85afbe1SWilly Tu             if (id.empty())
139e284a7c1SJames Feist             {
140a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
141e284a7c1SJames Feist                 return;
142e284a7c1SJames Feist             }
143e284a7c1SJames Feist 
144e284a7c1SJames Feist             if (interfaceDict.size() != 1)
145e284a7c1SJames Feist             {
146a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
147e284a7c1SJames Feist                                  << ", greater than 1";
148e284a7c1SJames Feist                 messages::internalError(asyncResp->res);
149e284a7c1SJames Feist                 return;
150e284a7c1SJames Feist             }
151e284a7c1SJames Feist 
152002d39b4SEd Tanous             const std::string& connectionName = interfaceDict.front().first;
153e284a7c1SJames Feist 
154e284a7c1SJames Feist             size_t index = root.size();
155e284a7c1SJames Feist             nlohmann::json& storageController =
156e284a7c1SJames Feist                 root.emplace_back(nlohmann::json::object());
157e284a7c1SJames Feist 
158e284a7c1SJames Feist             storageController["@odata.type"] =
159e284a7c1SJames Feist                 "#Storage.v1_7_0.StorageController";
160e284a7c1SJames Feist             storageController["@odata.id"] =
161*eddfc437SWilly Tu                 crow::utility::urlFromPieces("redfish", "v1", "Systems",
162*eddfc437SWilly Tu                                              "system", "Storage", "1")
163*eddfc437SWilly Tu                     .set_fragment(("/StorageControllers"_json_pointer / index)
164*eddfc437SWilly Tu                                       .to_string());
165e284a7c1SJames Feist             storageController["Name"] = id;
166e284a7c1SJames Feist             storageController["MemberId"] = id;
167e284a7c1SJames Feist             storageController["Status"]["State"] = "Enabled";
168e284a7c1SJames Feist 
1691e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
1701e1e598dSJonathan Doman                 *crow::connections::systemBus, connectionName, path,
1711e1e598dSJonathan Doman                 "xyz.openbmc_project.Inventory.Item", "Present",
172002d39b4SEd Tanous                 [asyncResp, index](const boost::system::error_code ec2,
173cef57e85SWilly Tu                                    bool isPresent) {
1747e860f15SJohn Edward Broadbent                 // this interface isn't necessary, only check it
1757e860f15SJohn Edward Broadbent                 // if we get a good return
17623a21a1cSEd Tanous                 if (ec2)
177e284a7c1SJames Feist                 {
178e284a7c1SJames Feist                     return;
179e284a7c1SJames Feist                 }
180cef57e85SWilly Tu                 if (!isPresent)
181e284a7c1SJames Feist                 {
182002d39b4SEd Tanous                     asyncResp->res.jsonValue["StorageControllers"][index]
183cef57e85SWilly Tu                                             ["Status"]["State"] = "Absent";
184e284a7c1SJames Feist                 }
1851e1e598dSJonathan Doman                 });
186e284a7c1SJames Feist 
187d1bde9e5SKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
188d1bde9e5SKrzysztof Grobelny                 *crow::connections::systemBus, connectionName, path,
189d1bde9e5SKrzysztof Grobelny                 "xyz.openbmc_project.Inventory.Decorator.Asset",
190a85afbe1SWilly Tu                 [asyncResp, index](
191a85afbe1SWilly Tu                     const boost::system::error_code ec2,
192a85afbe1SWilly Tu                     const std::vector<
193a85afbe1SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
1947e860f15SJohn Edward Broadbent                         propertiesList) {
1957e860f15SJohn Edward Broadbent                 if (ec2)
1967e860f15SJohn Edward Broadbent                 {
1977e860f15SJohn Edward Broadbent                     // this interface isn't necessary
1987e860f15SJohn Edward Broadbent                     return;
1997e860f15SJohn Edward Broadbent                 }
200d1bde9e5SKrzysztof Grobelny 
201d1bde9e5SKrzysztof Grobelny                 const std::string* partNumber = nullptr;
202d1bde9e5SKrzysztof Grobelny                 const std::string* serialNumber = nullptr;
203d1bde9e5SKrzysztof Grobelny                 const std::string* manufacturer = nullptr;
204d1bde9e5SKrzysztof Grobelny                 const std::string* model = nullptr;
205d1bde9e5SKrzysztof Grobelny 
206d1bde9e5SKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
207d1bde9e5SKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), propertiesList,
208d1bde9e5SKrzysztof Grobelny                     "PartNumber", partNumber, "SerialNumber", serialNumber,
209d1bde9e5SKrzysztof Grobelny                     "Manufacturer", manufacturer, "Model", model);
210d1bde9e5SKrzysztof Grobelny 
211d1bde9e5SKrzysztof Grobelny                 if (!success)
2127e860f15SJohn Edward Broadbent                 {
213002d39b4SEd Tanous                     messages::internalError(asyncResp->res);
2147e860f15SJohn Edward Broadbent                     return;
2157e860f15SJohn Edward Broadbent                 }
216d1bde9e5SKrzysztof Grobelny 
217d1bde9e5SKrzysztof Grobelny                 nlohmann::json& controller =
218d1bde9e5SKrzysztof Grobelny                     asyncResp->res.jsonValue["StorageControllers"][index];
219d1bde9e5SKrzysztof Grobelny 
220d1bde9e5SKrzysztof Grobelny                 if (partNumber != nullptr)
221d1bde9e5SKrzysztof Grobelny                 {
222d1bde9e5SKrzysztof Grobelny                     controller["PartNumber"] = *partNumber;
2237e860f15SJohn Edward Broadbent                 }
224d1bde9e5SKrzysztof Grobelny 
225d1bde9e5SKrzysztof Grobelny                 if (serialNumber != nullptr)
226d1bde9e5SKrzysztof Grobelny                 {
227d1bde9e5SKrzysztof Grobelny                     controller["SerialNumber"] = *serialNumber;
2287e860f15SJohn Edward Broadbent                 }
229d1bde9e5SKrzysztof Grobelny 
230d1bde9e5SKrzysztof Grobelny                 if (manufacturer != nullptr)
231d1bde9e5SKrzysztof Grobelny                 {
232d1bde9e5SKrzysztof Grobelny                     controller["Manufacturer"] = *manufacturer;
233d1bde9e5SKrzysztof Grobelny                 }
234d1bde9e5SKrzysztof Grobelny 
235d1bde9e5SKrzysztof Grobelny                 if (model != nullptr)
236d1bde9e5SKrzysztof Grobelny                 {
237d1bde9e5SKrzysztof Grobelny                     controller["Model"] = *model;
238d1bde9e5SKrzysztof Grobelny                 }
239d1bde9e5SKrzysztof Grobelny                 });
2407e860f15SJohn Edward Broadbent         }
2417e860f15SJohn Edward Broadbent 
2427e860f15SJohn Edward Broadbent         // this is done after we know the json array will no longer
2437e860f15SJohn Edward Broadbent         // be resized, as json::array uses vector underneath and we
2447e860f15SJohn Edward Broadbent         // need references to its members that won't change
2457e860f15SJohn Edward Broadbent         size_t count = 0;
246dfababfcSNan Zhou         // Pointer based on |asyncResp->res.jsonValue|
247dfababfcSNan Zhou         nlohmann::json::json_pointer rootPtr =
248dfababfcSNan Zhou             "/StorageControllers"_json_pointer;
2497e860f15SJohn Edward Broadbent         for (const auto& [path, interfaceDict] : subtree)
2507e860f15SJohn Edward Broadbent         {
2517e860f15SJohn Edward Broadbent             auto subHealth = std::make_shared<HealthPopulate>(
252dfababfcSNan Zhou                 asyncResp, rootPtr / count / "Status");
2537e860f15SJohn Edward Broadbent             subHealth->inventory.emplace_back(path);
2547e860f15SJohn Edward Broadbent             health->inventory.emplace_back(path);
2557e860f15SJohn Edward Broadbent             health->children.emplace_back(subHealth);
2567e860f15SJohn Edward Broadbent             count++;
2577e860f15SJohn Edward Broadbent         }
258e99073f5SGeorge Liu         });
259a85afbe1SWilly Tu }
260a85afbe1SWilly Tu 
261a85afbe1SWilly Tu inline void requestRoutesStorage(App& app)
262a85afbe1SWilly Tu {
263a85afbe1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
264a85afbe1SWilly Tu         .privileges(redfish::privileges::getStorage)
265a85afbe1SWilly Tu         .methods(boost::beast::http::verb::get)(
266a85afbe1SWilly Tu             [&app](const crow::Request& req,
267a85afbe1SWilly Tu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2683ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
269a85afbe1SWilly Tu         {
270a85afbe1SWilly Tu             return;
271a85afbe1SWilly Tu         }
272a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
273a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
274a85afbe1SWilly Tu             "/redfish/v1/Systems/system/Storage/1";
275a85afbe1SWilly Tu         asyncResp->res.jsonValue["Name"] = "Storage";
276a85afbe1SWilly Tu         asyncResp->res.jsonValue["Id"] = "1";
277a85afbe1SWilly Tu         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
278a85afbe1SWilly Tu 
279a85afbe1SWilly Tu         auto health = std::make_shared<HealthPopulate>(asyncResp);
280a85afbe1SWilly Tu         health->populate();
281a85afbe1SWilly Tu 
282a85afbe1SWilly Tu         getDrives(asyncResp, health);
283a85afbe1SWilly Tu         getStorageControllers(asyncResp, health);
2847e860f15SJohn Edward Broadbent         });
2857e860f15SJohn Edward Broadbent }
2867e860f15SJohn Edward Broadbent 
28703913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28803913171SWilly Tu                           const std::string& connectionName,
28903913171SWilly Tu                           const std::string& path)
29003913171SWilly Tu {
291d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
292d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, path,
293d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Asset",
294168e20c1SEd Tanous         [asyncResp](const boost::system::error_code ec,
295168e20c1SEd Tanous                     const std::vector<
296168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
29703913171SWilly Tu                         propertiesList) {
29803913171SWilly Tu         if (ec)
29903913171SWilly Tu         {
30003913171SWilly Tu             // this interface isn't necessary
30103913171SWilly Tu             return;
30203913171SWilly Tu         }
303d1bde9e5SKrzysztof Grobelny 
304d1bde9e5SKrzysztof Grobelny         const std::string* partNumber = nullptr;
305d1bde9e5SKrzysztof Grobelny         const std::string* serialNumber = nullptr;
306d1bde9e5SKrzysztof Grobelny         const std::string* manufacturer = nullptr;
307d1bde9e5SKrzysztof Grobelny         const std::string* model = nullptr;
308d1bde9e5SKrzysztof Grobelny 
309d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
310d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
311d1bde9e5SKrzysztof Grobelny             partNumber, "SerialNumber", serialNumber, "Manufacturer",
312d1bde9e5SKrzysztof Grobelny             manufacturer, "Model", model);
313d1bde9e5SKrzysztof Grobelny 
314d1bde9e5SKrzysztof Grobelny         if (!success)
31503913171SWilly Tu         {
31603913171SWilly Tu             messages::internalError(asyncResp->res);
31703913171SWilly Tu             return;
31803913171SWilly Tu         }
319d1bde9e5SKrzysztof Grobelny 
320d1bde9e5SKrzysztof Grobelny         if (partNumber != nullptr)
321d1bde9e5SKrzysztof Grobelny         {
322d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["PartNumber"] = *partNumber;
32303913171SWilly Tu         }
324d1bde9e5SKrzysztof Grobelny 
325d1bde9e5SKrzysztof Grobelny         if (serialNumber != nullptr)
326d1bde9e5SKrzysztof Grobelny         {
327d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
32803913171SWilly Tu         }
329d1bde9e5SKrzysztof Grobelny 
330d1bde9e5SKrzysztof Grobelny         if (manufacturer != nullptr)
331d1bde9e5SKrzysztof Grobelny         {
332d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
333d1bde9e5SKrzysztof Grobelny         }
334d1bde9e5SKrzysztof Grobelny 
335d1bde9e5SKrzysztof Grobelny         if (model != nullptr)
336d1bde9e5SKrzysztof Grobelny         {
337d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Model"] = *model;
338d1bde9e5SKrzysztof Grobelny         }
339d1bde9e5SKrzysztof Grobelny         });
34003913171SWilly Tu }
34103913171SWilly Tu 
34203913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34303913171SWilly Tu                             const std::string& connectionName,
34403913171SWilly Tu                             const std::string& path)
34503913171SWilly Tu {
3461e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3471e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3481e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
34903913171SWilly Tu         [asyncResp, path](const boost::system::error_code ec,
350cef57e85SWilly Tu                           const bool isPresent) {
35103913171SWilly Tu         // this interface isn't necessary, only check it if
35203913171SWilly Tu         // we get a good return
35303913171SWilly Tu         if (ec)
35403913171SWilly Tu         {
35503913171SWilly Tu             return;
35603913171SWilly Tu         }
35703913171SWilly Tu 
358cef57e85SWilly Tu         if (!isPresent)
35903913171SWilly Tu         {
360cef57e85SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Absent";
36103913171SWilly Tu         }
3621e1e598dSJonathan Doman         });
36303913171SWilly Tu }
36403913171SWilly Tu 
36503913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36603913171SWilly Tu                           const std::string& connectionName,
36703913171SWilly Tu                           const std::string& path)
36803913171SWilly Tu {
3691e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3701e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3711e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3721e1e598dSJonathan Doman         [asyncResp](const boost::system::error_code ec, const bool updating) {
37303913171SWilly Tu         // this interface isn't necessary, only check it
37403913171SWilly Tu         // if we get a good return
37503913171SWilly Tu         if (ec)
37603913171SWilly Tu         {
37703913171SWilly Tu             return;
37803913171SWilly Tu         }
37903913171SWilly Tu 
38003913171SWilly Tu         // updating and disabled in the backend shouldn't be
38103913171SWilly Tu         // able to be set at the same time, so we don't need
38203913171SWilly Tu         // to check for the race condition of these two
38303913171SWilly Tu         // calls
3841e1e598dSJonathan Doman         if (updating)
38503913171SWilly Tu         {
38603913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Updating";
38703913171SWilly Tu         }
3881e1e598dSJonathan Doman         });
38903913171SWilly Tu }
39003913171SWilly Tu 
39119b8e9a0SWilly Tu inline std::optional<std::string> convertDriveType(const std::string& type)
39219b8e9a0SWilly Tu {
39319b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
39419b8e9a0SWilly Tu     {
39519b8e9a0SWilly Tu         return "HDD";
39619b8e9a0SWilly Tu     }
39719b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
39819b8e9a0SWilly Tu     {
39919b8e9a0SWilly Tu         return "SSD";
40019b8e9a0SWilly Tu     }
40119b8e9a0SWilly Tu 
40219b8e9a0SWilly Tu     return std::nullopt;
40319b8e9a0SWilly Tu }
40419b8e9a0SWilly Tu 
40519b8e9a0SWilly Tu inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
40619b8e9a0SWilly Tu {
40719b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
40819b8e9a0SWilly Tu     {
40919b8e9a0SWilly Tu         return "SAS";
41019b8e9a0SWilly Tu     }
41119b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
41219b8e9a0SWilly Tu     {
41319b8e9a0SWilly Tu         return "SATA";
41419b8e9a0SWilly Tu     }
41519b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
41619b8e9a0SWilly Tu     {
41719b8e9a0SWilly Tu         return "NVMe";
41819b8e9a0SWilly Tu     }
41919b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
42019b8e9a0SWilly Tu     {
42119b8e9a0SWilly Tu         return "FC";
42219b8e9a0SWilly Tu     }
42319b8e9a0SWilly Tu 
42419b8e9a0SWilly Tu     return std::nullopt;
42519b8e9a0SWilly Tu }
42619b8e9a0SWilly Tu 
42719b8e9a0SWilly Tu inline void
42819b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
42919b8e9a0SWilly Tu                            const std::string& connectionName,
43019b8e9a0SWilly Tu                            const std::string& path)
43119b8e9a0SWilly Tu {
43219b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
43319b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
43419b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
43519b8e9a0SWilly Tu         [asyncResp](const boost::system::error_code ec,
43619b8e9a0SWilly Tu                     const std::vector<
43719b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
43819b8e9a0SWilly Tu                         propertiesList) {
43919b8e9a0SWilly Tu         if (ec)
44019b8e9a0SWilly Tu         {
44119b8e9a0SWilly Tu             // this interface isn't required
44219b8e9a0SWilly Tu             return;
44319b8e9a0SWilly Tu         }
44419b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
44519b8e9a0SWilly Tu                  property : propertiesList)
44619b8e9a0SWilly Tu         {
44719b8e9a0SWilly Tu             const std::string& propertyName = property.first;
44819b8e9a0SWilly Tu             if (propertyName == "Type")
44919b8e9a0SWilly Tu             {
45019b8e9a0SWilly Tu                 const std::string* value =
45119b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
45219b8e9a0SWilly Tu                 if (value == nullptr)
45319b8e9a0SWilly Tu                 {
45419b8e9a0SWilly Tu                     // illegal property
45519b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Type";
45619b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
45719b8e9a0SWilly Tu                     return;
45819b8e9a0SWilly Tu                 }
45919b8e9a0SWilly Tu 
460002d39b4SEd Tanous                 std::optional<std::string> mediaType = convertDriveType(*value);
46119b8e9a0SWilly Tu                 if (!mediaType)
46219b8e9a0SWilly Tu                 {
46319b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
46419b8e9a0SWilly Tu                                      << *value;
46519b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
46619b8e9a0SWilly Tu                     return;
46719b8e9a0SWilly Tu                 }
46819b8e9a0SWilly Tu 
46919b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
47019b8e9a0SWilly Tu             }
47119b8e9a0SWilly Tu             else if (propertyName == "Capacity")
47219b8e9a0SWilly Tu             {
47319b8e9a0SWilly Tu                 const uint64_t* capacity =
47419b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
47519b8e9a0SWilly Tu                 if (capacity == nullptr)
47619b8e9a0SWilly Tu                 {
47719b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Capacity";
47819b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
47919b8e9a0SWilly Tu                     return;
48019b8e9a0SWilly Tu                 }
48119b8e9a0SWilly Tu                 if (*capacity == 0)
48219b8e9a0SWilly Tu                 {
48319b8e9a0SWilly Tu                     // drive capacity not known
48419b8e9a0SWilly Tu                     continue;
48519b8e9a0SWilly Tu                 }
48619b8e9a0SWilly Tu 
48719b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
48819b8e9a0SWilly Tu             }
48919b8e9a0SWilly Tu             else if (propertyName == "Protocol")
49019b8e9a0SWilly Tu             {
49119b8e9a0SWilly Tu                 const std::string* value =
49219b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
49319b8e9a0SWilly Tu                 if (value == nullptr)
49419b8e9a0SWilly Tu                 {
49519b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Protocol";
49619b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
49719b8e9a0SWilly Tu                     return;
49819b8e9a0SWilly Tu                 }
49919b8e9a0SWilly Tu 
500002d39b4SEd Tanous                 std::optional<std::string> proto = convertDriveProtocol(*value);
50119b8e9a0SWilly Tu                 if (!proto)
50219b8e9a0SWilly Tu                 {
503002d39b4SEd Tanous                     BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: "
50419b8e9a0SWilly Tu                                      << *value;
50519b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
50619b8e9a0SWilly Tu                     return;
50719b8e9a0SWilly Tu                 }
50819b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
50919b8e9a0SWilly Tu             }
5103fe4d5ccSJohn Edward Broadbent             else if (propertyName == "PredictedMediaLifeLeftPercent")
5113fe4d5ccSJohn Edward Broadbent             {
5123fe4d5ccSJohn Edward Broadbent                 const uint8_t* lifeLeft =
5133fe4d5ccSJohn Edward Broadbent                     std::get_if<uint8_t>(&property.second);
5143fe4d5ccSJohn Edward Broadbent                 if (lifeLeft == nullptr)
5153fe4d5ccSJohn Edward Broadbent                 {
5163fe4d5ccSJohn Edward Broadbent                     BMCWEB_LOG_ERROR
5173fe4d5ccSJohn Edward Broadbent                         << "Illegal property: PredictedMediaLifeLeftPercent";
5183fe4d5ccSJohn Edward Broadbent                     messages::internalError(asyncResp->res);
5193fe4d5ccSJohn Edward Broadbent                     return;
5203fe4d5ccSJohn Edward Broadbent                 }
5213fe4d5ccSJohn Edward Broadbent                 // 255 means reading the value is not supported
5223fe4d5ccSJohn Edward Broadbent                 if (*lifeLeft != 255)
5233fe4d5ccSJohn Edward Broadbent                 {
5243fe4d5ccSJohn Edward Broadbent                     asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
5253fe4d5ccSJohn Edward Broadbent                         *lifeLeft;
5263fe4d5ccSJohn Edward Broadbent                 }
5273fe4d5ccSJohn Edward Broadbent             }
52819b8e9a0SWilly Tu         }
52919b8e9a0SWilly Tu         });
53019b8e9a0SWilly Tu }
53119b8e9a0SWilly Tu 
532b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
533b53dcd9dSNan Zhou                             const std::string& connectionName,
534b53dcd9dSNan Zhou                             const std::string& path,
535e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
536e56ed6b9SJohn Edward Broadbent {
537e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
538e56ed6b9SJohn Edward Broadbent     {
539e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
540e56ed6b9SJohn Edward Broadbent         {
541e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
542e56ed6b9SJohn Edward Broadbent         }
543e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
544e56ed6b9SJohn Edward Broadbent         {
545e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
546e56ed6b9SJohn Edward Broadbent         }
547e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
548e56ed6b9SJohn Edward Broadbent         {
549e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
550e56ed6b9SJohn Edward Broadbent         }
551e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
552e56ed6b9SJohn Edward Broadbent         {
553e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
554e56ed6b9SJohn Edward Broadbent         }
555e56ed6b9SJohn Edward Broadbent     }
556e56ed6b9SJohn Edward Broadbent }
557e56ed6b9SJohn Edward Broadbent 
5587e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
5597e860f15SJohn Edward Broadbent {
56022d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
561ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
562002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
563002d39b4SEd Tanous             [&app](const crow::Request& req,
56445ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
56522d268cbSEd Tanous                    const std::string& systemName, const std::string& driveId) {
5663ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
56745ca1b86SEd Tanous         {
56845ca1b86SEd Tanous             return;
56945ca1b86SEd Tanous         }
57022d268cbSEd Tanous         if (systemName != "system")
57122d268cbSEd Tanous         {
57222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
57322d268cbSEd Tanous                                        systemName);
57422d268cbSEd Tanous             return;
57522d268cbSEd Tanous         }
57622d268cbSEd Tanous 
577e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
578e99073f5SGeorge Liu             "xyz.openbmc_project.Inventory.Item.Drive"};
579e99073f5SGeorge Liu         dbus::utility::getSubTree(
580e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, interfaces,
581002d39b4SEd Tanous             [asyncResp,
582e99073f5SGeorge Liu              driveId](const boost::system::error_code& ec,
583b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
5847e860f15SJohn Edward Broadbent             if (ec)
5857e860f15SJohn Edward Broadbent             {
5867e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Drive mapper call error";
5877e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
5887e860f15SJohn Edward Broadbent                 return;
5897e860f15SJohn Edward Broadbent             }
5907e860f15SJohn Edward Broadbent 
59103913171SWilly Tu             auto drive = std::find_if(
5927e860f15SJohn Edward Broadbent                 subtree.begin(), subtree.end(),
593002d39b4SEd Tanous                 [&driveId](
5948cb65f8aSNan Zhou                     const std::pair<std::string,
5958cb65f8aSNan Zhou                                     dbus::utility::MapperServiceMap>& object) {
59603913171SWilly Tu                 return sdbusplus::message::object_path(object.first)
59703913171SWilly Tu                            .filename() == driveId;
5987e860f15SJohn Edward Broadbent                 });
5997e860f15SJohn Edward Broadbent 
60003913171SWilly Tu             if (drive == subtree.end())
6017e860f15SJohn Edward Broadbent             {
602002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
6037e860f15SJohn Edward Broadbent                 return;
6047e860f15SJohn Edward Broadbent             }
6057e860f15SJohn Edward Broadbent 
60603913171SWilly Tu             const std::string& path = drive->first;
6078cb65f8aSNan Zhou             const dbus::utility::MapperServiceMap& connectionNames =
6088cb65f8aSNan Zhou                 drive->second;
6097e860f15SJohn Edward Broadbent 
610002d39b4SEd Tanous             asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
6117e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
612*eddfc437SWilly Tu                 crow::utility::urlFromPieces("redfish", "v1", "Systems",
613*eddfc437SWilly Tu                                              "system", "Storage", "1", "Drives",
614*eddfc437SWilly Tu                                              driveId);
6157e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = driveId;
6167e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = driveId;
6177e860f15SJohn Edward Broadbent 
6187e860f15SJohn Edward Broadbent             if (connectionNames.size() != 1)
6197e860f15SJohn Edward Broadbent             {
620002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
62103913171SWilly Tu                                  << ", not equal to 1";
6227e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
6237e860f15SJohn Edward Broadbent                 return;
6247e860f15SJohn Edward Broadbent             }
6257e860f15SJohn Edward Broadbent 
6267e860f15SJohn Edward Broadbent             getMainChassisId(
627002d39b4SEd Tanous                 asyncResp, [](const std::string& chassisId,
6287e860f15SJohn Edward Broadbent                               const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
629002d39b4SEd Tanous                     aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
630*eddfc437SWilly Tu                         crow::utility::urlFromPieces("redfish", "v1", "Chassis",
631*eddfc437SWilly Tu                                                      chassisId);
6327e860f15SJohn Edward Broadbent                 });
6337e860f15SJohn Edward Broadbent 
634a25aeccfSNikhil Potade             // default it to Enabled
635a25aeccfSNikhil Potade             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
636a25aeccfSNikhil Potade 
6372ad9c2f6SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
638e284a7c1SJames Feist             health->inventory.emplace_back(path);
6392ad9c2f6SJames Feist             health->populate();
6402ad9c2f6SJames Feist 
641e56ed6b9SJohn Edward Broadbent             addAllDriveInfo(asyncResp, connectionNames[0].first, path,
642e56ed6b9SJohn Edward Broadbent                             connectionNames[0].second);
643e99073f5SGeorge Liu             });
6447e860f15SJohn Edward Broadbent         });
645a25aeccfSNikhil Potade }
64692903bd4SJohn Edward Broadbent 
64792903bd4SJohn Edward Broadbent /**
64892903bd4SJohn Edward Broadbent  * Chassis drives, this URL will show all the DriveCollection
64992903bd4SJohn Edward Broadbent  * information
65092903bd4SJohn Edward Broadbent  */
651b53dcd9dSNan Zhou inline void chassisDriveCollectionGet(
65292903bd4SJohn Edward Broadbent     crow::App& app, const crow::Request& req,
65392903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
65492903bd4SJohn Edward Broadbent     const std::string& chassisId)
65592903bd4SJohn Edward Broadbent {
6563ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
65792903bd4SJohn Edward Broadbent     {
65892903bd4SJohn Edward Broadbent         return;
65992903bd4SJohn Edward Broadbent     }
66092903bd4SJohn Edward Broadbent 
66192903bd4SJohn Edward Broadbent     // mapper call lambda
662e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
663e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Board",
664e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Chassis"};
665e99073f5SGeorge Liu     dbus::utility::getSubTree(
666e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
66792903bd4SJohn Edward Broadbent         [asyncResp,
668e99073f5SGeorge Liu          chassisId](const boost::system::error_code& ec,
66992903bd4SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
67092903bd4SJohn Edward Broadbent         if (ec)
67192903bd4SJohn Edward Broadbent         {
67292903bd4SJohn Edward Broadbent             if (ec == boost::system::errc::host_unreachable)
67392903bd4SJohn Edward Broadbent             {
67492903bd4SJohn Edward Broadbent                 messages::resourceNotFound(asyncResp->res, "Chassis",
67592903bd4SJohn Edward Broadbent                                            chassisId);
67692903bd4SJohn Edward Broadbent                 return;
67792903bd4SJohn Edward Broadbent             }
67892903bd4SJohn Edward Broadbent             messages::internalError(asyncResp->res);
67992903bd4SJohn Edward Broadbent             return;
68092903bd4SJohn Edward Broadbent         }
68192903bd4SJohn Edward Broadbent 
68292903bd4SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
6838cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
68492903bd4SJohn Edward Broadbent         {
68592903bd4SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
68692903bd4SJohn Edward Broadbent             if (objPath.filename() != chassisId)
68792903bd4SJohn Edward Broadbent             {
68892903bd4SJohn Edward Broadbent                 continue;
68992903bd4SJohn Edward Broadbent             }
69092903bd4SJohn Edward Broadbent 
69192903bd4SJohn Edward Broadbent             if (connectionNames.empty())
69292903bd4SJohn Edward Broadbent             {
69392903bd4SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
69492903bd4SJohn Edward Broadbent                 continue;
69592903bd4SJohn Edward Broadbent             }
69692903bd4SJohn Edward Broadbent 
69792903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.type"] =
69892903bd4SJohn Edward Broadbent                 "#DriveCollection.DriveCollection";
69992903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
70014bd7d9aSJohn Edward Broadbent                 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
70114bd7d9aSJohn Edward Broadbent                                              chassisId, "Drives");
70292903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "Drive Collection";
70392903bd4SJohn Edward Broadbent 
70492903bd4SJohn Edward Broadbent             // Association lambda
70592903bd4SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
70692903bd4SJohn Edward Broadbent                 *crow::connections::systemBus,
70792903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
70892903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
70992903bd4SJohn Edward Broadbent                 [asyncResp, chassisId](const boost::system::error_code ec3,
71092903bd4SJohn Edward Broadbent                                        const std::vector<std::string>& resp) {
71192903bd4SJohn Edward Broadbent                 if (ec3)
71292903bd4SJohn Edward Broadbent                 {
71392903bd4SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
71492903bd4SJohn Edward Broadbent                 }
71592903bd4SJohn Edward Broadbent                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
71692903bd4SJohn Edward Broadbent                 // important if array is empty
71792903bd4SJohn Edward Broadbent                 members = nlohmann::json::array();
71892903bd4SJohn Edward Broadbent 
71992903bd4SJohn Edward Broadbent                 std::vector<std::string> leafNames;
72092903bd4SJohn Edward Broadbent                 for (const auto& drive : resp)
72192903bd4SJohn Edward Broadbent                 {
7228a592810SEd Tanous                     sdbusplus::message::object_path drivePath(drive);
7238a592810SEd Tanous                     leafNames.push_back(drivePath.filename());
72492903bd4SJohn Edward Broadbent                 }
72592903bd4SJohn Edward Broadbent 
72692903bd4SJohn Edward Broadbent                 std::sort(leafNames.begin(), leafNames.end(),
72792903bd4SJohn Edward Broadbent                           AlphanumLess<std::string>());
72892903bd4SJohn Edward Broadbent 
72992903bd4SJohn Edward Broadbent                 for (const auto& leafName : leafNames)
73092903bd4SJohn Edward Broadbent                 {
73192903bd4SJohn Edward Broadbent                     nlohmann::json::object_t member;
73292903bd4SJohn Edward Broadbent                     member["@odata.id"] = crow::utility::urlFromPieces(
73392903bd4SJohn Edward Broadbent                         "redfish", "v1", "Chassis", chassisId, "Drives",
73492903bd4SJohn Edward Broadbent                         leafName);
73592903bd4SJohn Edward Broadbent                     members.push_back(std::move(member));
73692903bd4SJohn Edward Broadbent                     // navigation links will be registered in next patch set
73792903bd4SJohn Edward Broadbent                 }
73892903bd4SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
73992903bd4SJohn Edward Broadbent                 }); // end association lambda
74092903bd4SJohn Edward Broadbent 
74192903bd4SJohn Edward Broadbent         } // end Iterate over all retrieved ObjectPaths
742e99073f5SGeorge Liu         });
74392903bd4SJohn Edward Broadbent }
74492903bd4SJohn Edward Broadbent 
74592903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
74692903bd4SJohn Edward Broadbent {
74792903bd4SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
74892903bd4SJohn Edward Broadbent         .privileges(redfish::privileges::getDriveCollection)
74992903bd4SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
75092903bd4SJohn Edward Broadbent             std::bind_front(chassisDriveCollectionGet, std::ref(app)));
75192903bd4SJohn Edward Broadbent }
75292903bd4SJohn Edward Broadbent 
753b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
754b53dcd9dSNan Zhou                        const std::string& chassisId,
755b53dcd9dSNan Zhou                        const std::string& driveName,
756e56ed6b9SJohn Edward Broadbent                        const boost::system::error_code ec,
757e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
758e56ed6b9SJohn Edward Broadbent {
759e56ed6b9SJohn Edward Broadbent 
760e56ed6b9SJohn Edward Broadbent     if (ec)
761e56ed6b9SJohn Edward Broadbent     {
762e56ed6b9SJohn Edward Broadbent         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
763e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
764e56ed6b9SJohn Edward Broadbent         return;
765e56ed6b9SJohn Edward Broadbent     }
766e56ed6b9SJohn Edward Broadbent 
767e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
7688cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
769e56ed6b9SJohn Edward Broadbent     {
770e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
771e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
772e56ed6b9SJohn Edward Broadbent         {
773e56ed6b9SJohn Edward Broadbent             continue;
774e56ed6b9SJohn Edward Broadbent         }
775e56ed6b9SJohn Edward Broadbent 
776e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
777e56ed6b9SJohn Edward Broadbent         {
778e56ed6b9SJohn Edward Broadbent             BMCWEB_LOG_ERROR << "Got 0 Connection names";
779e56ed6b9SJohn Edward Broadbent             continue;
780e56ed6b9SJohn Edward Broadbent         }
781e56ed6b9SJohn Edward Broadbent 
782e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
783a0cb40cbSJohn Edward Broadbent             "redfish", "v1", "Chassis", chassisId, "Drives", driveName);
784e56ed6b9SJohn Edward Broadbent 
785e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
786a0cb40cbSJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = driveName;
787e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
788e56ed6b9SJohn Edward Broadbent         // default it to Enabled
789e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
790e56ed6b9SJohn Edward Broadbent 
791e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
792e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
793e56ed6b9SJohn Edward Broadbent             crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId);
794e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
795e56ed6b9SJohn Edward Broadbent 
796e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
797e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
798e56ed6b9SJohn Edward Broadbent     }
799e56ed6b9SJohn Edward Broadbent }
800e56ed6b9SJohn Edward Broadbent 
801b53dcd9dSNan Zhou inline void
802b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
803e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
804e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
805e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
806e56ed6b9SJohn Edward Broadbent {
807e56ed6b9SJohn Edward Broadbent 
808e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
809e56ed6b9SJohn Edward Broadbent     {
810e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
811e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
812e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
813e56ed6b9SJohn Edward Broadbent         {
814e56ed6b9SJohn Edward Broadbent             continue;
815e56ed6b9SJohn Edward Broadbent         }
816e56ed6b9SJohn Edward Broadbent         //  mapper call drive
817e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> driveInterface = {
818e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
819e99073f5SGeorge Liu         dbus::utility::getSubTree(
820e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, driveInterface,
821e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
822e99073f5SGeorge Liu                 const boost::system::error_code& ec,
823e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
824e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
825e99073f5SGeorge Liu             });
826e56ed6b9SJohn Edward Broadbent     }
827e56ed6b9SJohn Edward Broadbent }
828e56ed6b9SJohn Edward Broadbent 
829b53dcd9dSNan Zhou inline void
830b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
831e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
832e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
833e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
834e56ed6b9SJohn Edward Broadbent {
83503810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
836e56ed6b9SJohn Edward Broadbent     {
837e56ed6b9SJohn Edward Broadbent         return;
838e56ed6b9SJohn Edward Broadbent     }
839e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
840e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
841e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
842e56ed6b9SJohn Edward Broadbent 
843e56ed6b9SJohn Edward Broadbent     // mapper call chassis
844e99073f5SGeorge Liu     dbus::utility::getSubTree(
845e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
846e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
847e99073f5SGeorge Liu          driveName](const boost::system::error_code& ec,
848e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
849e56ed6b9SJohn Edward Broadbent         if (ec)
850e56ed6b9SJohn Edward Broadbent         {
851e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
852e56ed6b9SJohn Edward Broadbent             return;
853e56ed6b9SJohn Edward Broadbent         }
854e56ed6b9SJohn Edward Broadbent 
855e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
8568cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
857e56ed6b9SJohn Edward Broadbent         {
858e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
859e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
860e56ed6b9SJohn Edward Broadbent             {
861e56ed6b9SJohn Edward Broadbent                 continue;
862e56ed6b9SJohn Edward Broadbent             }
863e56ed6b9SJohn Edward Broadbent 
864e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
865e56ed6b9SJohn Edward Broadbent             {
866e56ed6b9SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
867e56ed6b9SJohn Edward Broadbent                 continue;
868e56ed6b9SJohn Edward Broadbent             }
869e56ed6b9SJohn Edward Broadbent 
870e56ed6b9SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
871e56ed6b9SJohn Edward Broadbent                 *crow::connections::systemBus,
872e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
873e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
874e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
875e56ed6b9SJohn Edward Broadbent                  driveName](const boost::system::error_code ec3,
876e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& resp) {
877e56ed6b9SJohn Edward Broadbent                 if (ec3)
878e56ed6b9SJohn Edward Broadbent                 {
879e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
880e56ed6b9SJohn Edward Broadbent                 }
881e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
882e56ed6b9SJohn Edward Broadbent                 });
883e56ed6b9SJohn Edward Broadbent             break;
884e56ed6b9SJohn Edward Broadbent         }
885e99073f5SGeorge Liu         });
886e56ed6b9SJohn Edward Broadbent }
887e56ed6b9SJohn Edward Broadbent 
888e56ed6b9SJohn Edward Broadbent /**
889e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
890e56ed6b9SJohn Edward Broadbent  */
891e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
892e56ed6b9SJohn Edward Broadbent {
893e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
894e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
895e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
896e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
897e56ed6b9SJohn Edward Broadbent }
898e56ed6b9SJohn Edward Broadbent 
899a25aeccfSNikhil Potade } // namespace redfish
900