xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision a8e884fc4e2fae0c79454ee865c9b9e0534f6763)
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"
21*a8e884fcSEd Tanous #include "human_sort.hpp"
22e284a7c1SJames Feist #include "openbmc_dbus_rest.hpp"
233ccb3adbSEd Tanous #include "query.hpp"
24*a8e884fcSEd 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;
105a85afbe1SWilly Tu             driveJson["@odata.id"] =
106be13ceceSJames Feist                 "/redfish/v1/Systems/system/Storage/1/Drives/" +
107a85afbe1SWilly 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"] =
1610fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
162e284a7c1SJames Feist                 std::to_string(index);
163e284a7c1SJames Feist             storageController["Name"] = id;
164e284a7c1SJames Feist             storageController["MemberId"] = id;
165e284a7c1SJames Feist             storageController["Status"]["State"] = "Enabled";
166e284a7c1SJames Feist 
1671e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
1681e1e598dSJonathan Doman                 *crow::connections::systemBus, connectionName, path,
1691e1e598dSJonathan Doman                 "xyz.openbmc_project.Inventory.Item", "Present",
170002d39b4SEd Tanous                 [asyncResp, index](const boost::system::error_code ec2,
171cef57e85SWilly Tu                                    bool isPresent) {
1727e860f15SJohn Edward Broadbent                 // this interface isn't necessary, only check it
1737e860f15SJohn Edward Broadbent                 // if we get a good return
17423a21a1cSEd Tanous                 if (ec2)
175e284a7c1SJames Feist                 {
176e284a7c1SJames Feist                     return;
177e284a7c1SJames Feist                 }
178cef57e85SWilly Tu                 if (!isPresent)
179e284a7c1SJames Feist                 {
180002d39b4SEd Tanous                     asyncResp->res.jsonValue["StorageControllers"][index]
181cef57e85SWilly Tu                                             ["Status"]["State"] = "Absent";
182e284a7c1SJames Feist                 }
1831e1e598dSJonathan Doman                 });
184e284a7c1SJames Feist 
185d1bde9e5SKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
186d1bde9e5SKrzysztof Grobelny                 *crow::connections::systemBus, connectionName, path,
187d1bde9e5SKrzysztof Grobelny                 "xyz.openbmc_project.Inventory.Decorator.Asset",
188a85afbe1SWilly Tu                 [asyncResp, index](
189a85afbe1SWilly Tu                     const boost::system::error_code ec2,
190a85afbe1SWilly Tu                     const std::vector<
191a85afbe1SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
1927e860f15SJohn Edward Broadbent                         propertiesList) {
1937e860f15SJohn Edward Broadbent                 if (ec2)
1947e860f15SJohn Edward Broadbent                 {
1957e860f15SJohn Edward Broadbent                     // this interface isn't necessary
1967e860f15SJohn Edward Broadbent                     return;
1977e860f15SJohn Edward Broadbent                 }
198d1bde9e5SKrzysztof Grobelny 
199d1bde9e5SKrzysztof Grobelny                 const std::string* partNumber = nullptr;
200d1bde9e5SKrzysztof Grobelny                 const std::string* serialNumber = nullptr;
201d1bde9e5SKrzysztof Grobelny                 const std::string* manufacturer = nullptr;
202d1bde9e5SKrzysztof Grobelny                 const std::string* model = nullptr;
203d1bde9e5SKrzysztof Grobelny 
204d1bde9e5SKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
205d1bde9e5SKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), propertiesList,
206d1bde9e5SKrzysztof Grobelny                     "PartNumber", partNumber, "SerialNumber", serialNumber,
207d1bde9e5SKrzysztof Grobelny                     "Manufacturer", manufacturer, "Model", model);
208d1bde9e5SKrzysztof Grobelny 
209d1bde9e5SKrzysztof Grobelny                 if (!success)
2107e860f15SJohn Edward Broadbent                 {
211002d39b4SEd Tanous                     messages::internalError(asyncResp->res);
2127e860f15SJohn Edward Broadbent                     return;
2137e860f15SJohn Edward Broadbent                 }
214d1bde9e5SKrzysztof Grobelny 
215d1bde9e5SKrzysztof Grobelny                 nlohmann::json& controller =
216d1bde9e5SKrzysztof Grobelny                     asyncResp->res.jsonValue["StorageControllers"][index];
217d1bde9e5SKrzysztof Grobelny 
218d1bde9e5SKrzysztof Grobelny                 if (partNumber != nullptr)
219d1bde9e5SKrzysztof Grobelny                 {
220d1bde9e5SKrzysztof Grobelny                     controller["PartNumber"] = *partNumber;
2217e860f15SJohn Edward Broadbent                 }
222d1bde9e5SKrzysztof Grobelny 
223d1bde9e5SKrzysztof Grobelny                 if (serialNumber != nullptr)
224d1bde9e5SKrzysztof Grobelny                 {
225d1bde9e5SKrzysztof Grobelny                     controller["SerialNumber"] = *serialNumber;
2267e860f15SJohn Edward Broadbent                 }
227d1bde9e5SKrzysztof Grobelny 
228d1bde9e5SKrzysztof Grobelny                 if (manufacturer != nullptr)
229d1bde9e5SKrzysztof Grobelny                 {
230d1bde9e5SKrzysztof Grobelny                     controller["Manufacturer"] = *manufacturer;
231d1bde9e5SKrzysztof Grobelny                 }
232d1bde9e5SKrzysztof Grobelny 
233d1bde9e5SKrzysztof Grobelny                 if (model != nullptr)
234d1bde9e5SKrzysztof Grobelny                 {
235d1bde9e5SKrzysztof Grobelny                     controller["Model"] = *model;
236d1bde9e5SKrzysztof Grobelny                 }
237d1bde9e5SKrzysztof Grobelny                 });
2387e860f15SJohn Edward Broadbent         }
2397e860f15SJohn Edward Broadbent 
2407e860f15SJohn Edward Broadbent         // this is done after we know the json array will no longer
2417e860f15SJohn Edward Broadbent         // be resized, as json::array uses vector underneath and we
2427e860f15SJohn Edward Broadbent         // need references to its members that won't change
2437e860f15SJohn Edward Broadbent         size_t count = 0;
244dfababfcSNan Zhou         // Pointer based on |asyncResp->res.jsonValue|
245dfababfcSNan Zhou         nlohmann::json::json_pointer rootPtr =
246dfababfcSNan Zhou             "/StorageControllers"_json_pointer;
2477e860f15SJohn Edward Broadbent         for (const auto& [path, interfaceDict] : subtree)
2487e860f15SJohn Edward Broadbent         {
2497e860f15SJohn Edward Broadbent             auto subHealth = std::make_shared<HealthPopulate>(
250dfababfcSNan Zhou                 asyncResp, rootPtr / count / "Status");
2517e860f15SJohn Edward Broadbent             subHealth->inventory.emplace_back(path);
2527e860f15SJohn Edward Broadbent             health->inventory.emplace_back(path);
2537e860f15SJohn Edward Broadbent             health->children.emplace_back(subHealth);
2547e860f15SJohn Edward Broadbent             count++;
2557e860f15SJohn Edward Broadbent         }
256e99073f5SGeorge Liu         });
257a85afbe1SWilly Tu }
258a85afbe1SWilly Tu 
259a85afbe1SWilly Tu inline void requestRoutesStorage(App& app)
260a85afbe1SWilly Tu {
261a85afbe1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
262a85afbe1SWilly Tu         .privileges(redfish::privileges::getStorage)
263a85afbe1SWilly Tu         .methods(boost::beast::http::verb::get)(
264a85afbe1SWilly Tu             [&app](const crow::Request& req,
265a85afbe1SWilly Tu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2663ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
267a85afbe1SWilly Tu         {
268a85afbe1SWilly Tu             return;
269a85afbe1SWilly Tu         }
270a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
271a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
272a85afbe1SWilly Tu             "/redfish/v1/Systems/system/Storage/1";
273a85afbe1SWilly Tu         asyncResp->res.jsonValue["Name"] = "Storage";
274a85afbe1SWilly Tu         asyncResp->res.jsonValue["Id"] = "1";
275a85afbe1SWilly Tu         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
276a85afbe1SWilly Tu 
277a85afbe1SWilly Tu         auto health = std::make_shared<HealthPopulate>(asyncResp);
278a85afbe1SWilly Tu         health->populate();
279a85afbe1SWilly Tu 
280a85afbe1SWilly Tu         getDrives(asyncResp, health);
281a85afbe1SWilly Tu         getStorageControllers(asyncResp, health);
2827e860f15SJohn Edward Broadbent         });
2837e860f15SJohn Edward Broadbent }
2847e860f15SJohn Edward Broadbent 
28503913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28603913171SWilly Tu                           const std::string& connectionName,
28703913171SWilly Tu                           const std::string& path)
28803913171SWilly Tu {
289d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
290d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, path,
291d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Asset",
292168e20c1SEd Tanous         [asyncResp](const boost::system::error_code ec,
293168e20c1SEd Tanous                     const std::vector<
294168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
29503913171SWilly Tu                         propertiesList) {
29603913171SWilly Tu         if (ec)
29703913171SWilly Tu         {
29803913171SWilly Tu             // this interface isn't necessary
29903913171SWilly Tu             return;
30003913171SWilly Tu         }
301d1bde9e5SKrzysztof Grobelny 
302d1bde9e5SKrzysztof Grobelny         const std::string* partNumber = nullptr;
303d1bde9e5SKrzysztof Grobelny         const std::string* serialNumber = nullptr;
304d1bde9e5SKrzysztof Grobelny         const std::string* manufacturer = nullptr;
305d1bde9e5SKrzysztof Grobelny         const std::string* model = nullptr;
306d1bde9e5SKrzysztof Grobelny 
307d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
308d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
309d1bde9e5SKrzysztof Grobelny             partNumber, "SerialNumber", serialNumber, "Manufacturer",
310d1bde9e5SKrzysztof Grobelny             manufacturer, "Model", model);
311d1bde9e5SKrzysztof Grobelny 
312d1bde9e5SKrzysztof Grobelny         if (!success)
31303913171SWilly Tu         {
31403913171SWilly Tu             messages::internalError(asyncResp->res);
31503913171SWilly Tu             return;
31603913171SWilly Tu         }
317d1bde9e5SKrzysztof Grobelny 
318d1bde9e5SKrzysztof Grobelny         if (partNumber != nullptr)
319d1bde9e5SKrzysztof Grobelny         {
320d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["PartNumber"] = *partNumber;
32103913171SWilly Tu         }
322d1bde9e5SKrzysztof Grobelny 
323d1bde9e5SKrzysztof Grobelny         if (serialNumber != nullptr)
324d1bde9e5SKrzysztof Grobelny         {
325d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
32603913171SWilly Tu         }
327d1bde9e5SKrzysztof Grobelny 
328d1bde9e5SKrzysztof Grobelny         if (manufacturer != nullptr)
329d1bde9e5SKrzysztof Grobelny         {
330d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
331d1bde9e5SKrzysztof Grobelny         }
332d1bde9e5SKrzysztof Grobelny 
333d1bde9e5SKrzysztof Grobelny         if (model != nullptr)
334d1bde9e5SKrzysztof Grobelny         {
335d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Model"] = *model;
336d1bde9e5SKrzysztof Grobelny         }
337d1bde9e5SKrzysztof Grobelny         });
33803913171SWilly Tu }
33903913171SWilly Tu 
34003913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34103913171SWilly Tu                             const std::string& connectionName,
34203913171SWilly Tu                             const std::string& path)
34303913171SWilly Tu {
3441e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3451e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3461e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
34703913171SWilly Tu         [asyncResp, path](const boost::system::error_code ec,
348cef57e85SWilly Tu                           const bool isPresent) {
34903913171SWilly Tu         // this interface isn't necessary, only check it if
35003913171SWilly Tu         // we get a good return
35103913171SWilly Tu         if (ec)
35203913171SWilly Tu         {
35303913171SWilly Tu             return;
35403913171SWilly Tu         }
35503913171SWilly Tu 
356cef57e85SWilly Tu         if (!isPresent)
35703913171SWilly Tu         {
358cef57e85SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Absent";
35903913171SWilly Tu         }
3601e1e598dSJonathan Doman         });
36103913171SWilly Tu }
36203913171SWilly Tu 
36303913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36403913171SWilly Tu                           const std::string& connectionName,
36503913171SWilly Tu                           const std::string& path)
36603913171SWilly Tu {
3671e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3681e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3691e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3701e1e598dSJonathan Doman         [asyncResp](const boost::system::error_code ec, const bool updating) {
37103913171SWilly Tu         // this interface isn't necessary, only check it
37203913171SWilly Tu         // if we get a good return
37303913171SWilly Tu         if (ec)
37403913171SWilly Tu         {
37503913171SWilly Tu             return;
37603913171SWilly Tu         }
37703913171SWilly Tu 
37803913171SWilly Tu         // updating and disabled in the backend shouldn't be
37903913171SWilly Tu         // able to be set at the same time, so we don't need
38003913171SWilly Tu         // to check for the race condition of these two
38103913171SWilly Tu         // calls
3821e1e598dSJonathan Doman         if (updating)
38303913171SWilly Tu         {
38403913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Updating";
38503913171SWilly Tu         }
3861e1e598dSJonathan Doman         });
38703913171SWilly Tu }
38803913171SWilly Tu 
38919b8e9a0SWilly Tu inline std::optional<std::string> convertDriveType(const std::string& type)
39019b8e9a0SWilly Tu {
39119b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
39219b8e9a0SWilly Tu     {
39319b8e9a0SWilly Tu         return "HDD";
39419b8e9a0SWilly Tu     }
39519b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
39619b8e9a0SWilly Tu     {
39719b8e9a0SWilly Tu         return "SSD";
39819b8e9a0SWilly Tu     }
39919b8e9a0SWilly Tu 
40019b8e9a0SWilly Tu     return std::nullopt;
40119b8e9a0SWilly Tu }
40219b8e9a0SWilly Tu 
40319b8e9a0SWilly Tu inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
40419b8e9a0SWilly Tu {
40519b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
40619b8e9a0SWilly Tu     {
40719b8e9a0SWilly Tu         return "SAS";
40819b8e9a0SWilly Tu     }
40919b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
41019b8e9a0SWilly Tu     {
41119b8e9a0SWilly Tu         return "SATA";
41219b8e9a0SWilly Tu     }
41319b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
41419b8e9a0SWilly Tu     {
41519b8e9a0SWilly Tu         return "NVMe";
41619b8e9a0SWilly Tu     }
41719b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
41819b8e9a0SWilly Tu     {
41919b8e9a0SWilly Tu         return "FC";
42019b8e9a0SWilly Tu     }
42119b8e9a0SWilly Tu 
42219b8e9a0SWilly Tu     return std::nullopt;
42319b8e9a0SWilly Tu }
42419b8e9a0SWilly Tu 
42519b8e9a0SWilly Tu inline void
42619b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
42719b8e9a0SWilly Tu                            const std::string& connectionName,
42819b8e9a0SWilly Tu                            const std::string& path)
42919b8e9a0SWilly Tu {
43019b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
43119b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
43219b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
43319b8e9a0SWilly Tu         [asyncResp](const boost::system::error_code ec,
43419b8e9a0SWilly Tu                     const std::vector<
43519b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
43619b8e9a0SWilly Tu                         propertiesList) {
43719b8e9a0SWilly Tu         if (ec)
43819b8e9a0SWilly Tu         {
43919b8e9a0SWilly Tu             // this interface isn't required
44019b8e9a0SWilly Tu             return;
44119b8e9a0SWilly Tu         }
44219b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
44319b8e9a0SWilly Tu                  property : propertiesList)
44419b8e9a0SWilly Tu         {
44519b8e9a0SWilly Tu             const std::string& propertyName = property.first;
44619b8e9a0SWilly Tu             if (propertyName == "Type")
44719b8e9a0SWilly Tu             {
44819b8e9a0SWilly Tu                 const std::string* value =
44919b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
45019b8e9a0SWilly Tu                 if (value == nullptr)
45119b8e9a0SWilly Tu                 {
45219b8e9a0SWilly Tu                     // illegal property
45319b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Type";
45419b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
45519b8e9a0SWilly Tu                     return;
45619b8e9a0SWilly Tu                 }
45719b8e9a0SWilly Tu 
458002d39b4SEd Tanous                 std::optional<std::string> mediaType = convertDriveType(*value);
45919b8e9a0SWilly Tu                 if (!mediaType)
46019b8e9a0SWilly Tu                 {
46119b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
46219b8e9a0SWilly Tu                                      << *value;
46319b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
46419b8e9a0SWilly Tu                     return;
46519b8e9a0SWilly Tu                 }
46619b8e9a0SWilly Tu 
46719b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
46819b8e9a0SWilly Tu             }
46919b8e9a0SWilly Tu             else if (propertyName == "Capacity")
47019b8e9a0SWilly Tu             {
47119b8e9a0SWilly Tu                 const uint64_t* capacity =
47219b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
47319b8e9a0SWilly Tu                 if (capacity == nullptr)
47419b8e9a0SWilly Tu                 {
47519b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Capacity";
47619b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
47719b8e9a0SWilly Tu                     return;
47819b8e9a0SWilly Tu                 }
47919b8e9a0SWilly Tu                 if (*capacity == 0)
48019b8e9a0SWilly Tu                 {
48119b8e9a0SWilly Tu                     // drive capacity not known
48219b8e9a0SWilly Tu                     continue;
48319b8e9a0SWilly Tu                 }
48419b8e9a0SWilly Tu 
48519b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
48619b8e9a0SWilly Tu             }
48719b8e9a0SWilly Tu             else if (propertyName == "Protocol")
48819b8e9a0SWilly Tu             {
48919b8e9a0SWilly Tu                 const std::string* value =
49019b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
49119b8e9a0SWilly Tu                 if (value == nullptr)
49219b8e9a0SWilly Tu                 {
49319b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Protocol";
49419b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
49519b8e9a0SWilly Tu                     return;
49619b8e9a0SWilly Tu                 }
49719b8e9a0SWilly Tu 
498002d39b4SEd Tanous                 std::optional<std::string> proto = convertDriveProtocol(*value);
49919b8e9a0SWilly Tu                 if (!proto)
50019b8e9a0SWilly Tu                 {
501002d39b4SEd Tanous                     BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: "
50219b8e9a0SWilly Tu                                      << *value;
50319b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
50419b8e9a0SWilly Tu                     return;
50519b8e9a0SWilly Tu                 }
50619b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
50719b8e9a0SWilly Tu             }
5083fe4d5ccSJohn Edward Broadbent             else if (propertyName == "PredictedMediaLifeLeftPercent")
5093fe4d5ccSJohn Edward Broadbent             {
5103fe4d5ccSJohn Edward Broadbent                 const uint8_t* lifeLeft =
5113fe4d5ccSJohn Edward Broadbent                     std::get_if<uint8_t>(&property.second);
5123fe4d5ccSJohn Edward Broadbent                 if (lifeLeft == nullptr)
5133fe4d5ccSJohn Edward Broadbent                 {
5143fe4d5ccSJohn Edward Broadbent                     BMCWEB_LOG_ERROR
5153fe4d5ccSJohn Edward Broadbent                         << "Illegal property: PredictedMediaLifeLeftPercent";
5163fe4d5ccSJohn Edward Broadbent                     messages::internalError(asyncResp->res);
5173fe4d5ccSJohn Edward Broadbent                     return;
5183fe4d5ccSJohn Edward Broadbent                 }
5193fe4d5ccSJohn Edward Broadbent                 // 255 means reading the value is not supported
5203fe4d5ccSJohn Edward Broadbent                 if (*lifeLeft != 255)
5213fe4d5ccSJohn Edward Broadbent                 {
5223fe4d5ccSJohn Edward Broadbent                     asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
5233fe4d5ccSJohn Edward Broadbent                         *lifeLeft;
5243fe4d5ccSJohn Edward Broadbent                 }
5253fe4d5ccSJohn Edward Broadbent             }
52619b8e9a0SWilly Tu         }
52719b8e9a0SWilly Tu         });
52819b8e9a0SWilly Tu }
52919b8e9a0SWilly Tu 
530b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
531b53dcd9dSNan Zhou                             const std::string& connectionName,
532b53dcd9dSNan Zhou                             const std::string& path,
533e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
534e56ed6b9SJohn Edward Broadbent {
535e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
536e56ed6b9SJohn Edward Broadbent     {
537e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
538e56ed6b9SJohn Edward Broadbent         {
539e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
540e56ed6b9SJohn Edward Broadbent         }
541e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
542e56ed6b9SJohn Edward Broadbent         {
543e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
544e56ed6b9SJohn Edward Broadbent         }
545e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
546e56ed6b9SJohn Edward Broadbent         {
547e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
548e56ed6b9SJohn Edward Broadbent         }
549e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
550e56ed6b9SJohn Edward Broadbent         {
551e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
552e56ed6b9SJohn Edward Broadbent         }
553e56ed6b9SJohn Edward Broadbent     }
554e56ed6b9SJohn Edward Broadbent }
555e56ed6b9SJohn Edward Broadbent 
5567e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
5577e860f15SJohn Edward Broadbent {
55822d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
559ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
560002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
561002d39b4SEd Tanous             [&app](const crow::Request& req,
56245ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
56322d268cbSEd Tanous                    const std::string& systemName, const std::string& driveId) {
5643ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
56545ca1b86SEd Tanous         {
56645ca1b86SEd Tanous             return;
56745ca1b86SEd Tanous         }
56822d268cbSEd Tanous         if (systemName != "system")
56922d268cbSEd Tanous         {
57022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
57122d268cbSEd Tanous                                        systemName);
57222d268cbSEd Tanous             return;
57322d268cbSEd Tanous         }
57422d268cbSEd Tanous 
575e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
576e99073f5SGeorge Liu             "xyz.openbmc_project.Inventory.Item.Drive"};
577e99073f5SGeorge Liu         dbus::utility::getSubTree(
578e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, interfaces,
579002d39b4SEd Tanous             [asyncResp,
580e99073f5SGeorge Liu              driveId](const boost::system::error_code& ec,
581b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
5827e860f15SJohn Edward Broadbent             if (ec)
5837e860f15SJohn Edward Broadbent             {
5847e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Drive mapper call error";
5857e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
5867e860f15SJohn Edward Broadbent                 return;
5877e860f15SJohn Edward Broadbent             }
5887e860f15SJohn Edward Broadbent 
58903913171SWilly Tu             auto drive = std::find_if(
5907e860f15SJohn Edward Broadbent                 subtree.begin(), subtree.end(),
591002d39b4SEd Tanous                 [&driveId](
5928cb65f8aSNan Zhou                     const std::pair<std::string,
5938cb65f8aSNan Zhou                                     dbus::utility::MapperServiceMap>& object) {
59403913171SWilly Tu                 return sdbusplus::message::object_path(object.first)
59503913171SWilly Tu                            .filename() == driveId;
5967e860f15SJohn Edward Broadbent                 });
5977e860f15SJohn Edward Broadbent 
59803913171SWilly Tu             if (drive == subtree.end())
5997e860f15SJohn Edward Broadbent             {
600002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
6017e860f15SJohn Edward Broadbent                 return;
6027e860f15SJohn Edward Broadbent             }
6037e860f15SJohn Edward Broadbent 
60403913171SWilly Tu             const std::string& path = drive->first;
6058cb65f8aSNan Zhou             const dbus::utility::MapperServiceMap& connectionNames =
6068cb65f8aSNan Zhou                 drive->second;
6077e860f15SJohn Edward Broadbent 
608002d39b4SEd Tanous             asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
6097e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
610002d39b4SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId;
6117e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = driveId;
6127e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = driveId;
6137e860f15SJohn Edward Broadbent 
6147e860f15SJohn Edward Broadbent             if (connectionNames.size() != 1)
6157e860f15SJohn Edward Broadbent             {
616002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
61703913171SWilly Tu                                  << ", not equal to 1";
6187e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
6197e860f15SJohn Edward Broadbent                 return;
6207e860f15SJohn Edward Broadbent             }
6217e860f15SJohn Edward Broadbent 
6227e860f15SJohn Edward Broadbent             getMainChassisId(
623002d39b4SEd Tanous                 asyncResp, [](const std::string& chassisId,
6247e860f15SJohn Edward Broadbent                               const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
625002d39b4SEd Tanous                     aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
6261476687dSEd Tanous                         "/redfish/v1/Chassis/" + chassisId;
6277e860f15SJohn Edward Broadbent                 });
6287e860f15SJohn Edward Broadbent 
629a25aeccfSNikhil Potade             // default it to Enabled
630a25aeccfSNikhil Potade             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
631a25aeccfSNikhil Potade 
6322ad9c2f6SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
633e284a7c1SJames Feist             health->inventory.emplace_back(path);
6342ad9c2f6SJames Feist             health->populate();
6352ad9c2f6SJames Feist 
636e56ed6b9SJohn Edward Broadbent             addAllDriveInfo(asyncResp, connectionNames[0].first, path,
637e56ed6b9SJohn Edward Broadbent                             connectionNames[0].second);
638e99073f5SGeorge Liu             });
6397e860f15SJohn Edward Broadbent         });
640a25aeccfSNikhil Potade }
64192903bd4SJohn Edward Broadbent 
64292903bd4SJohn Edward Broadbent /**
64392903bd4SJohn Edward Broadbent  * Chassis drives, this URL will show all the DriveCollection
64492903bd4SJohn Edward Broadbent  * information
64592903bd4SJohn Edward Broadbent  */
646b53dcd9dSNan Zhou inline void chassisDriveCollectionGet(
64792903bd4SJohn Edward Broadbent     crow::App& app, const crow::Request& req,
64892903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
64992903bd4SJohn Edward Broadbent     const std::string& chassisId)
65092903bd4SJohn Edward Broadbent {
6513ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
65292903bd4SJohn Edward Broadbent     {
65392903bd4SJohn Edward Broadbent         return;
65492903bd4SJohn Edward Broadbent     }
65592903bd4SJohn Edward Broadbent 
65692903bd4SJohn Edward Broadbent     // mapper call lambda
657e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
658e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Board",
659e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Chassis"};
660e99073f5SGeorge Liu     dbus::utility::getSubTree(
661e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
66292903bd4SJohn Edward Broadbent         [asyncResp,
663e99073f5SGeorge Liu          chassisId](const boost::system::error_code& ec,
66492903bd4SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
66592903bd4SJohn Edward Broadbent         if (ec)
66692903bd4SJohn Edward Broadbent         {
66792903bd4SJohn Edward Broadbent             if (ec == boost::system::errc::host_unreachable)
66892903bd4SJohn Edward Broadbent             {
66992903bd4SJohn Edward Broadbent                 messages::resourceNotFound(asyncResp->res, "Chassis",
67092903bd4SJohn Edward Broadbent                                            chassisId);
67192903bd4SJohn Edward Broadbent                 return;
67292903bd4SJohn Edward Broadbent             }
67392903bd4SJohn Edward Broadbent             messages::internalError(asyncResp->res);
67492903bd4SJohn Edward Broadbent             return;
67592903bd4SJohn Edward Broadbent         }
67692903bd4SJohn Edward Broadbent 
67792903bd4SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
6788cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
67992903bd4SJohn Edward Broadbent         {
68092903bd4SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
68192903bd4SJohn Edward Broadbent             if (objPath.filename() != chassisId)
68292903bd4SJohn Edward Broadbent             {
68392903bd4SJohn Edward Broadbent                 continue;
68492903bd4SJohn Edward Broadbent             }
68592903bd4SJohn Edward Broadbent 
68692903bd4SJohn Edward Broadbent             if (connectionNames.empty())
68792903bd4SJohn Edward Broadbent             {
68892903bd4SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
68992903bd4SJohn Edward Broadbent                 continue;
69092903bd4SJohn Edward Broadbent             }
69192903bd4SJohn Edward Broadbent 
69292903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.type"] =
69392903bd4SJohn Edward Broadbent                 "#DriveCollection.DriveCollection";
69492903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
69514bd7d9aSJohn Edward Broadbent                 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
69614bd7d9aSJohn Edward Broadbent                                              chassisId, "Drives");
69792903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "Drive Collection";
69892903bd4SJohn Edward Broadbent 
69992903bd4SJohn Edward Broadbent             // Association lambda
70092903bd4SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
70192903bd4SJohn Edward Broadbent                 *crow::connections::systemBus,
70292903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
70392903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
70492903bd4SJohn Edward Broadbent                 [asyncResp, chassisId](const boost::system::error_code ec3,
70592903bd4SJohn Edward Broadbent                                        const std::vector<std::string>& 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;
72792903bd4SJohn Edward Broadbent                     member["@odata.id"] = crow::utility::urlFromPieces(
72892903bd4SJohn Edward Broadbent                         "redfish", "v1", "Chassis", chassisId, "Drives",
72992903bd4SJohn Edward Broadbent                         leafName);
73092903bd4SJohn Edward Broadbent                     members.push_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,
751e56ed6b9SJohn Edward Broadbent                        const boost::system::error_code ec,
752e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
753e56ed6b9SJohn Edward Broadbent {
754e56ed6b9SJohn Edward Broadbent 
755e56ed6b9SJohn Edward Broadbent     if (ec)
756e56ed6b9SJohn Edward Broadbent     {
757e56ed6b9SJohn Edward Broadbent         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
758e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
759e56ed6b9SJohn Edward Broadbent         return;
760e56ed6b9SJohn Edward Broadbent     }
761e56ed6b9SJohn Edward Broadbent 
762e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
7638cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
764e56ed6b9SJohn Edward Broadbent     {
765e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
766e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
767e56ed6b9SJohn Edward Broadbent         {
768e56ed6b9SJohn Edward Broadbent             continue;
769e56ed6b9SJohn Edward Broadbent         }
770e56ed6b9SJohn Edward Broadbent 
771e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
772e56ed6b9SJohn Edward Broadbent         {
773e56ed6b9SJohn Edward Broadbent             BMCWEB_LOG_ERROR << "Got 0 Connection names";
774e56ed6b9SJohn Edward Broadbent             continue;
775e56ed6b9SJohn Edward Broadbent         }
776e56ed6b9SJohn Edward Broadbent 
777e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
778a0cb40cbSJohn Edward Broadbent             "redfish", "v1", "Chassis", chassisId, "Drives", driveName);
779e56ed6b9SJohn Edward Broadbent 
780e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
781a0cb40cbSJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = driveName;
782e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
783e56ed6b9SJohn Edward Broadbent         // default it to Enabled
784e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
785e56ed6b9SJohn Edward Broadbent 
786e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
787e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
788e56ed6b9SJohn Edward Broadbent             crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId);
789e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
790e56ed6b9SJohn Edward Broadbent 
791e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
792e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
793e56ed6b9SJohn Edward Broadbent     }
794e56ed6b9SJohn Edward Broadbent }
795e56ed6b9SJohn Edward Broadbent 
796b53dcd9dSNan Zhou inline void
797b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
798e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
799e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
800e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
801e56ed6b9SJohn Edward Broadbent {
802e56ed6b9SJohn Edward Broadbent 
803e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
804e56ed6b9SJohn Edward Broadbent     {
805e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
806e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
807e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
808e56ed6b9SJohn Edward Broadbent         {
809e56ed6b9SJohn Edward Broadbent             continue;
810e56ed6b9SJohn Edward Broadbent         }
811e56ed6b9SJohn Edward Broadbent         //  mapper call drive
812e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> driveInterface = {
813e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
814e99073f5SGeorge Liu         dbus::utility::getSubTree(
815e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, driveInterface,
816e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
817e99073f5SGeorge Liu                 const boost::system::error_code& ec,
818e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
819e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
820e99073f5SGeorge Liu             });
821e56ed6b9SJohn Edward Broadbent     }
822e56ed6b9SJohn Edward Broadbent }
823e56ed6b9SJohn Edward Broadbent 
824b53dcd9dSNan Zhou inline void
825b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
826e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
827e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
828e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
829e56ed6b9SJohn Edward Broadbent {
83003810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
831e56ed6b9SJohn Edward Broadbent     {
832e56ed6b9SJohn Edward Broadbent         return;
833e56ed6b9SJohn Edward Broadbent     }
834e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
835e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
836e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
837e56ed6b9SJohn Edward Broadbent 
838e56ed6b9SJohn Edward Broadbent     // mapper call chassis
839e99073f5SGeorge Liu     dbus::utility::getSubTree(
840e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
841e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
842e99073f5SGeorge Liu          driveName](const boost::system::error_code& ec,
843e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
844e56ed6b9SJohn Edward Broadbent         if (ec)
845e56ed6b9SJohn Edward Broadbent         {
846e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
847e56ed6b9SJohn Edward Broadbent             return;
848e56ed6b9SJohn Edward Broadbent         }
849e56ed6b9SJohn Edward Broadbent 
850e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
8518cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
852e56ed6b9SJohn Edward Broadbent         {
853e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
854e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
855e56ed6b9SJohn Edward Broadbent             {
856e56ed6b9SJohn Edward Broadbent                 continue;
857e56ed6b9SJohn Edward Broadbent             }
858e56ed6b9SJohn Edward Broadbent 
859e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
860e56ed6b9SJohn Edward Broadbent             {
861e56ed6b9SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
862e56ed6b9SJohn Edward Broadbent                 continue;
863e56ed6b9SJohn Edward Broadbent             }
864e56ed6b9SJohn Edward Broadbent 
865e56ed6b9SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
866e56ed6b9SJohn Edward Broadbent                 *crow::connections::systemBus,
867e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
868e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
869e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
870e56ed6b9SJohn Edward Broadbent                  driveName](const boost::system::error_code ec3,
871e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& resp) {
872e56ed6b9SJohn Edward Broadbent                 if (ec3)
873e56ed6b9SJohn Edward Broadbent                 {
874e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
875e56ed6b9SJohn Edward Broadbent                 }
876e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
877e56ed6b9SJohn Edward Broadbent                 });
878e56ed6b9SJohn Edward Broadbent             break;
879e56ed6b9SJohn Edward Broadbent         }
880e99073f5SGeorge Liu         });
881e56ed6b9SJohn Edward Broadbent }
882e56ed6b9SJohn Edward Broadbent 
883e56ed6b9SJohn Edward Broadbent /**
884e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
885e56ed6b9SJohn Edward Broadbent  */
886e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
887e56ed6b9SJohn Edward Broadbent {
888e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
889e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
890e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
891e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
892e56ed6b9SJohn Edward Broadbent }
893e56ed6b9SJohn Edward Broadbent 
894a25aeccfSNikhil Potade } // namespace redfish
895