xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision 7a1dbc4803bf78bfc0c574e6676b3c5def4cdae3)
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 
18*7a1dbc48SGeorge Liu #include "dbus_utility.hpp"
192ad9c2f6SJames Feist #include "health.hpp"
20e284a7c1SJames Feist #include "openbmc_dbus_rest.hpp"
212ad9c2f6SJames Feist 
227e860f15SJohn Edward Broadbent #include <app.hpp>
2345ca1b86SEd Tanous #include <query.hpp>
24ed398213SEd Tanous #include <registries/privilege_registry.hpp>
251e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
26d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
27d1bde9e5SKrzysztof Grobelny #include <utils/dbus_utils.hpp>
28a25aeccfSNikhil Potade 
29*7a1dbc48SGeorge Liu #include <array>
30*7a1dbc48SGeorge Liu #include <string_view>
31*7a1dbc48SGeorge Liu 
32a25aeccfSNikhil Potade namespace redfish
33a25aeccfSNikhil Potade {
347e860f15SJohn Edward Broadbent inline void requestRoutesStorageCollection(App& app)
35a25aeccfSNikhil Potade {
3622d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
37ed398213SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
387e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3945ca1b86SEd Tanous             [&app](const crow::Request& req,
4022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4122d268cbSEd Tanous                    const std::string& systemName) {
423ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
4345ca1b86SEd Tanous         {
4445ca1b86SEd Tanous             return;
4545ca1b86SEd Tanous         }
4622d268cbSEd Tanous         if (systemName != "system")
4722d268cbSEd Tanous         {
4822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4922d268cbSEd Tanous                                        systemName);
5022d268cbSEd Tanous             return;
5122d268cbSEd Tanous         }
5222d268cbSEd Tanous 
538d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
548d1b46d7Szhanghch05             "#StorageCollection.StorageCollection";
558d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
568d1b46d7Szhanghch05             "/redfish/v1/Systems/system/Storage";
578d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Storage Collection";
581476687dSEd Tanous         nlohmann::json::array_t members;
591476687dSEd Tanous         nlohmann::json::object_t member;
601476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
611476687dSEd Tanous         members.emplace_back(member);
621476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
638d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
647e860f15SJohn Edward Broadbent         });
65a25aeccfSNikhil Potade }
66a25aeccfSNikhil Potade 
67a85afbe1SWilly Tu inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
68a85afbe1SWilly Tu                       const std::shared_ptr<HealthPopulate>& health)
69a25aeccfSNikhil Potade {
70*7a1dbc48SGeorge Liu     const std::array<std::string_view, 1> interfaces = {
71*7a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Drive"};
72*7a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
73*7a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
74a85afbe1SWilly Tu         [asyncResp, health](
75*7a1dbc48SGeorge Liu             const boost::system::error_code& ec,
76a85afbe1SWilly Tu             const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
77a25aeccfSNikhil Potade         if (ec)
78a25aeccfSNikhil Potade         {
79a25aeccfSNikhil Potade             BMCWEB_LOG_ERROR << "Drive mapper call error";
80a25aeccfSNikhil Potade             messages::internalError(asyncResp->res);
81a25aeccfSNikhil Potade             return;
82a25aeccfSNikhil Potade         }
832ad9c2f6SJames Feist 
84a85afbe1SWilly Tu         nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
85a85afbe1SWilly Tu         driveArray = nlohmann::json::array();
86a85afbe1SWilly Tu         auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
87a85afbe1SWilly Tu         count = 0;
882ad9c2f6SJames Feist 
89a85afbe1SWilly Tu         health->inventory.insert(health->inventory.end(), driveList.begin(),
90a85afbe1SWilly Tu                                  driveList.end());
91a85afbe1SWilly Tu 
92a85afbe1SWilly Tu         for (const std::string& drive : driveList)
93a25aeccfSNikhil Potade         {
94a85afbe1SWilly Tu             sdbusplus::message::object_path object(drive);
95a85afbe1SWilly Tu             if (object.filename().empty())
96a25aeccfSNikhil Potade             {
97a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
98a85afbe1SWilly Tu                 return;
99a25aeccfSNikhil Potade             }
100a85afbe1SWilly Tu 
101a85afbe1SWilly Tu             nlohmann::json::object_t driveJson;
102a85afbe1SWilly Tu             driveJson["@odata.id"] =
103be13ceceSJames Feist                 "/redfish/v1/Systems/system/Storage/1/Drives/" +
104a85afbe1SWilly Tu                 object.filename();
105a85afbe1SWilly Tu             driveArray.push_back(std::move(driveJson));
106a25aeccfSNikhil Potade         }
107a25aeccfSNikhil Potade 
108a85afbe1SWilly Tu         count = driveArray.size();
109*7a1dbc48SGeorge Liu         });
110a85afbe1SWilly Tu }
111e284a7c1SJames Feist 
112a85afbe1SWilly Tu inline void
113a85afbe1SWilly Tu     getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
114a85afbe1SWilly Tu                           const std::shared_ptr<HealthPopulate>& health)
115a85afbe1SWilly Tu {
116e284a7c1SJames Feist     crow::connections::systemBus->async_method_call(
117002d39b4SEd Tanous         [asyncResp,
118002d39b4SEd Tanous          health](const boost::system::error_code ec,
119b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreeResponse& subtree) {
12026f6976fSEd Tanous         if (ec || subtree.empty())
121e284a7c1SJames Feist         {
122d819a420SJames Feist             // doesn't have to be there
123e284a7c1SJames Feist             return;
124e284a7c1SJames Feist         }
125e284a7c1SJames Feist 
126a85afbe1SWilly Tu         nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
127e284a7c1SJames Feist         root = nlohmann::json::array();
128e284a7c1SJames Feist         for (const auto& [path, interfaceDict] : subtree)
129e284a7c1SJames Feist         {
130a85afbe1SWilly Tu             sdbusplus::message::object_path object(path);
131a85afbe1SWilly Tu             std::string id = object.filename();
132a85afbe1SWilly Tu             if (id.empty())
133e284a7c1SJames Feist             {
134a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
135e284a7c1SJames Feist                 return;
136e284a7c1SJames Feist             }
137e284a7c1SJames Feist 
138e284a7c1SJames Feist             if (interfaceDict.size() != 1)
139e284a7c1SJames Feist             {
140a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
141e284a7c1SJames Feist                                  << ", greater than 1";
142e284a7c1SJames Feist                 messages::internalError(asyncResp->res);
143e284a7c1SJames Feist                 return;
144e284a7c1SJames Feist             }
145e284a7c1SJames Feist 
146002d39b4SEd Tanous             const std::string& connectionName = interfaceDict.front().first;
147e284a7c1SJames Feist 
148e284a7c1SJames Feist             size_t index = root.size();
149e284a7c1SJames Feist             nlohmann::json& storageController =
150e284a7c1SJames Feist                 root.emplace_back(nlohmann::json::object());
151e284a7c1SJames Feist 
152e284a7c1SJames Feist             storageController["@odata.type"] =
153e284a7c1SJames Feist                 "#Storage.v1_7_0.StorageController";
154e284a7c1SJames Feist             storageController["@odata.id"] =
1550fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
156e284a7c1SJames Feist                 std::to_string(index);
157e284a7c1SJames Feist             storageController["Name"] = id;
158e284a7c1SJames Feist             storageController["MemberId"] = id;
159e284a7c1SJames Feist             storageController["Status"]["State"] = "Enabled";
160e284a7c1SJames Feist 
1611e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
1621e1e598dSJonathan Doman                 *crow::connections::systemBus, connectionName, path,
1631e1e598dSJonathan Doman                 "xyz.openbmc_project.Inventory.Item", "Present",
164002d39b4SEd Tanous                 [asyncResp, index](const boost::system::error_code ec2,
1651e1e598dSJonathan Doman                                    bool enabled) {
1667e860f15SJohn Edward Broadbent                 // this interface isn't necessary, only check it
1677e860f15SJohn Edward Broadbent                 // if we get a good return
16823a21a1cSEd Tanous                 if (ec2)
169e284a7c1SJames Feist                 {
170e284a7c1SJames Feist                     return;
171e284a7c1SJames Feist                 }
1721e1e598dSJonathan Doman                 if (!enabled)
173e284a7c1SJames Feist                 {
174002d39b4SEd Tanous                     asyncResp->res.jsonValue["StorageControllers"][index]
175a85afbe1SWilly Tu                                             ["Status"]["State"] = "Disabled";
176e284a7c1SJames Feist                 }
1771e1e598dSJonathan Doman                 });
178e284a7c1SJames Feist 
179d1bde9e5SKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
180d1bde9e5SKrzysztof Grobelny                 *crow::connections::systemBus, connectionName, path,
181d1bde9e5SKrzysztof Grobelny                 "xyz.openbmc_project.Inventory.Decorator.Asset",
182a85afbe1SWilly Tu                 [asyncResp, index](
183a85afbe1SWilly Tu                     const boost::system::error_code ec2,
184a85afbe1SWilly Tu                     const std::vector<
185a85afbe1SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
1867e860f15SJohn Edward Broadbent                         propertiesList) {
1877e860f15SJohn Edward Broadbent                 if (ec2)
1887e860f15SJohn Edward Broadbent                 {
1897e860f15SJohn Edward Broadbent                     // this interface isn't necessary
1907e860f15SJohn Edward Broadbent                     return;
1917e860f15SJohn Edward Broadbent                 }
192d1bde9e5SKrzysztof Grobelny 
193d1bde9e5SKrzysztof Grobelny                 const std::string* partNumber = nullptr;
194d1bde9e5SKrzysztof Grobelny                 const std::string* serialNumber = nullptr;
195d1bde9e5SKrzysztof Grobelny                 const std::string* manufacturer = nullptr;
196d1bde9e5SKrzysztof Grobelny                 const std::string* model = nullptr;
197d1bde9e5SKrzysztof Grobelny 
198d1bde9e5SKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
199d1bde9e5SKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), propertiesList,
200d1bde9e5SKrzysztof Grobelny                     "PartNumber", partNumber, "SerialNumber", serialNumber,
201d1bde9e5SKrzysztof Grobelny                     "Manufacturer", manufacturer, "Model", model);
202d1bde9e5SKrzysztof Grobelny 
203d1bde9e5SKrzysztof Grobelny                 if (!success)
2047e860f15SJohn Edward Broadbent                 {
205002d39b4SEd Tanous                     messages::internalError(asyncResp->res);
2067e860f15SJohn Edward Broadbent                     return;
2077e860f15SJohn Edward Broadbent                 }
208d1bde9e5SKrzysztof Grobelny 
209d1bde9e5SKrzysztof Grobelny                 nlohmann::json& controller =
210d1bde9e5SKrzysztof Grobelny                     asyncResp->res.jsonValue["StorageControllers"][index];
211d1bde9e5SKrzysztof Grobelny 
212d1bde9e5SKrzysztof Grobelny                 if (partNumber != nullptr)
213d1bde9e5SKrzysztof Grobelny                 {
214d1bde9e5SKrzysztof Grobelny                     controller["PartNumber"] = *partNumber;
2157e860f15SJohn Edward Broadbent                 }
216d1bde9e5SKrzysztof Grobelny 
217d1bde9e5SKrzysztof Grobelny                 if (serialNumber != nullptr)
218d1bde9e5SKrzysztof Grobelny                 {
219d1bde9e5SKrzysztof Grobelny                     controller["SerialNumber"] = *serialNumber;
2207e860f15SJohn Edward Broadbent                 }
221d1bde9e5SKrzysztof Grobelny 
222d1bde9e5SKrzysztof Grobelny                 if (manufacturer != nullptr)
223d1bde9e5SKrzysztof Grobelny                 {
224d1bde9e5SKrzysztof Grobelny                     controller["Manufacturer"] = *manufacturer;
225d1bde9e5SKrzysztof Grobelny                 }
226d1bde9e5SKrzysztof Grobelny 
227d1bde9e5SKrzysztof Grobelny                 if (model != nullptr)
228d1bde9e5SKrzysztof Grobelny                 {
229d1bde9e5SKrzysztof Grobelny                     controller["Model"] = *model;
230d1bde9e5SKrzysztof Grobelny                 }
231d1bde9e5SKrzysztof Grobelny                 });
2327e860f15SJohn Edward Broadbent         }
2337e860f15SJohn Edward Broadbent 
2347e860f15SJohn Edward Broadbent         // this is done after we know the json array will no longer
2357e860f15SJohn Edward Broadbent         // be resized, as json::array uses vector underneath and we
2367e860f15SJohn Edward Broadbent         // need references to its members that won't change
2377e860f15SJohn Edward Broadbent         size_t count = 0;
238dfababfcSNan Zhou         // Pointer based on |asyncResp->res.jsonValue|
239dfababfcSNan Zhou         nlohmann::json::json_pointer rootPtr =
240dfababfcSNan Zhou             "/StorageControllers"_json_pointer;
2417e860f15SJohn Edward Broadbent         for (const auto& [path, interfaceDict] : subtree)
2427e860f15SJohn Edward Broadbent         {
2437e860f15SJohn Edward Broadbent             auto subHealth = std::make_shared<HealthPopulate>(
244dfababfcSNan Zhou                 asyncResp, rootPtr / count / "Status");
2457e860f15SJohn Edward Broadbent             subHealth->inventory.emplace_back(path);
2467e860f15SJohn Edward Broadbent             health->inventory.emplace_back(path);
2477e860f15SJohn Edward Broadbent             health->children.emplace_back(subHealth);
2487e860f15SJohn Edward Broadbent             count++;
2497e860f15SJohn Edward Broadbent         }
2507e860f15SJohn Edward Broadbent         },
2517e860f15SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
2527e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
2537e860f15SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2547e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", int32_t(0),
2557e860f15SJohn Edward Broadbent         std::array<const char*, 1>{
2567e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.StorageController"});
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,
3481e1e598dSJonathan Doman                           const bool enabled) {
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 
3561e1e598dSJonathan Doman         if (!enabled)
35703913171SWilly Tu         {
35803913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
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 
5757e860f15SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
576002d39b4SEd Tanous             [asyncResp,
577002d39b4SEd Tanous              driveId](const boost::system::error_code ec,
578b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
5797e860f15SJohn Edward Broadbent             if (ec)
5807e860f15SJohn Edward Broadbent             {
5817e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Drive mapper call error";
5827e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
5837e860f15SJohn Edward Broadbent                 return;
5847e860f15SJohn Edward Broadbent             }
5857e860f15SJohn Edward Broadbent 
58603913171SWilly Tu             auto drive = std::find_if(
5877e860f15SJohn Edward Broadbent                 subtree.begin(), subtree.end(),
588002d39b4SEd Tanous                 [&driveId](
5898cb65f8aSNan Zhou                     const std::pair<std::string,
5908cb65f8aSNan Zhou                                     dbus::utility::MapperServiceMap>& object) {
59103913171SWilly Tu                 return sdbusplus::message::object_path(object.first)
59203913171SWilly Tu                            .filename() == driveId;
5937e860f15SJohn Edward Broadbent                 });
5947e860f15SJohn Edward Broadbent 
59503913171SWilly Tu             if (drive == subtree.end())
5967e860f15SJohn Edward Broadbent             {
597002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
5987e860f15SJohn Edward Broadbent                 return;
5997e860f15SJohn Edward Broadbent             }
6007e860f15SJohn Edward Broadbent 
60103913171SWilly Tu             const std::string& path = drive->first;
6028cb65f8aSNan Zhou             const dbus::utility::MapperServiceMap& connectionNames =
6038cb65f8aSNan Zhou                 drive->second;
6047e860f15SJohn Edward Broadbent 
605002d39b4SEd Tanous             asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
6067e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
607002d39b4SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId;
6087e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = driveId;
6097e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = driveId;
6107e860f15SJohn Edward Broadbent 
6117e860f15SJohn Edward Broadbent             if (connectionNames.size() != 1)
6127e860f15SJohn Edward Broadbent             {
613002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
61403913171SWilly Tu                                  << ", not equal to 1";
6157e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
6167e860f15SJohn Edward Broadbent                 return;
6177e860f15SJohn Edward Broadbent             }
6187e860f15SJohn Edward Broadbent 
6197e860f15SJohn Edward Broadbent             getMainChassisId(
620002d39b4SEd Tanous                 asyncResp, [](const std::string& chassisId,
6217e860f15SJohn Edward Broadbent                               const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
622002d39b4SEd Tanous                     aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
6231476687dSEd Tanous                         "/redfish/v1/Chassis/" + chassisId;
6247e860f15SJohn Edward Broadbent                 });
6257e860f15SJohn Edward Broadbent 
626a25aeccfSNikhil Potade             // default it to Enabled
627a25aeccfSNikhil Potade             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
628a25aeccfSNikhil Potade 
6292ad9c2f6SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
630e284a7c1SJames Feist             health->inventory.emplace_back(path);
6312ad9c2f6SJames Feist             health->populate();
6322ad9c2f6SJames Feist 
633e56ed6b9SJohn Edward Broadbent             addAllDriveInfo(asyncResp, connectionNames[0].first, path,
634e56ed6b9SJohn Edward Broadbent                             connectionNames[0].second);
635a25aeccfSNikhil Potade             },
636a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper",
637a25aeccfSNikhil Potade             "/xyz/openbmc_project/object_mapper",
638a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
639a25aeccfSNikhil Potade             "/xyz/openbmc_project/inventory", int32_t(0),
640a25aeccfSNikhil Potade             std::array<const char*, 1>{
641a25aeccfSNikhil Potade                 "xyz.openbmc_project.Inventory.Item.Drive"});
6427e860f15SJohn Edward Broadbent         });
643a25aeccfSNikhil Potade }
64492903bd4SJohn Edward Broadbent 
64592903bd4SJohn Edward Broadbent /**
64692903bd4SJohn Edward Broadbent  * Chassis drives, this URL will show all the DriveCollection
64792903bd4SJohn Edward Broadbent  * information
64892903bd4SJohn Edward Broadbent  */
649b53dcd9dSNan Zhou inline void chassisDriveCollectionGet(
65092903bd4SJohn Edward Broadbent     crow::App& app, const crow::Request& req,
65192903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
65292903bd4SJohn Edward Broadbent     const std::string& chassisId)
65392903bd4SJohn Edward Broadbent {
6543ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
65592903bd4SJohn Edward Broadbent     {
65692903bd4SJohn Edward Broadbent         return;
65792903bd4SJohn Edward Broadbent     }
65892903bd4SJohn Edward Broadbent 
65992903bd4SJohn Edward Broadbent     // mapper call lambda
66092903bd4SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
66192903bd4SJohn Edward Broadbent         [asyncResp,
66292903bd4SJohn Edward Broadbent          chassisId](const boost::system::error_code ec,
66392903bd4SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
66492903bd4SJohn Edward Broadbent         if (ec)
66592903bd4SJohn Edward Broadbent         {
66692903bd4SJohn Edward Broadbent             if (ec == boost::system::errc::host_unreachable)
66792903bd4SJohn Edward Broadbent             {
66892903bd4SJohn Edward Broadbent                 messages::resourceNotFound(asyncResp->res, "Chassis",
66992903bd4SJohn Edward Broadbent                                            chassisId);
67092903bd4SJohn Edward Broadbent                 return;
67192903bd4SJohn Edward Broadbent             }
67292903bd4SJohn Edward Broadbent             messages::internalError(asyncResp->res);
67392903bd4SJohn Edward Broadbent             return;
67492903bd4SJohn Edward Broadbent         }
67592903bd4SJohn Edward Broadbent 
67692903bd4SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
6778cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
67892903bd4SJohn Edward Broadbent         {
67992903bd4SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
68092903bd4SJohn Edward Broadbent             if (objPath.filename() != chassisId)
68192903bd4SJohn Edward Broadbent             {
68292903bd4SJohn Edward Broadbent                 continue;
68392903bd4SJohn Edward Broadbent             }
68492903bd4SJohn Edward Broadbent 
68592903bd4SJohn Edward Broadbent             if (connectionNames.empty())
68692903bd4SJohn Edward Broadbent             {
68792903bd4SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
68892903bd4SJohn Edward Broadbent                 continue;
68992903bd4SJohn Edward Broadbent             }
69092903bd4SJohn Edward Broadbent 
69192903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.type"] =
69292903bd4SJohn Edward Broadbent                 "#DriveCollection.DriveCollection";
69392903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
69414bd7d9aSJohn Edward Broadbent                 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
69514bd7d9aSJohn Edward Broadbent                                              chassisId, "Drives");
69692903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "Drive Collection";
69792903bd4SJohn Edward Broadbent 
69892903bd4SJohn Edward Broadbent             // Association lambda
69992903bd4SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
70092903bd4SJohn Edward Broadbent                 *crow::connections::systemBus,
70192903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
70292903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
70392903bd4SJohn Edward Broadbent                 [asyncResp, chassisId](const boost::system::error_code ec3,
70492903bd4SJohn Edward Broadbent                                        const std::vector<std::string>& resp) {
70592903bd4SJohn Edward Broadbent                 if (ec3)
70692903bd4SJohn Edward Broadbent                 {
70792903bd4SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
70892903bd4SJohn Edward Broadbent                 }
70992903bd4SJohn Edward Broadbent                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
71092903bd4SJohn Edward Broadbent                 // important if array is empty
71192903bd4SJohn Edward Broadbent                 members = nlohmann::json::array();
71292903bd4SJohn Edward Broadbent 
71392903bd4SJohn Edward Broadbent                 std::vector<std::string> leafNames;
71492903bd4SJohn Edward Broadbent                 for (const auto& drive : resp)
71592903bd4SJohn Edward Broadbent                 {
7168a592810SEd Tanous                     sdbusplus::message::object_path drivePath(drive);
7178a592810SEd Tanous                     leafNames.push_back(drivePath.filename());
71892903bd4SJohn Edward Broadbent                 }
71992903bd4SJohn Edward Broadbent 
72092903bd4SJohn Edward Broadbent                 std::sort(leafNames.begin(), leafNames.end(),
72192903bd4SJohn Edward Broadbent                           AlphanumLess<std::string>());
72292903bd4SJohn Edward Broadbent 
72392903bd4SJohn Edward Broadbent                 for (const auto& leafName : leafNames)
72492903bd4SJohn Edward Broadbent                 {
72592903bd4SJohn Edward Broadbent                     nlohmann::json::object_t member;
72692903bd4SJohn Edward Broadbent                     member["@odata.id"] = crow::utility::urlFromPieces(
72792903bd4SJohn Edward Broadbent                         "redfish", "v1", "Chassis", chassisId, "Drives",
72892903bd4SJohn Edward Broadbent                         leafName);
72992903bd4SJohn Edward Broadbent                     members.push_back(std::move(member));
73092903bd4SJohn Edward Broadbent                     // navigation links will be registered in next patch set
73192903bd4SJohn Edward Broadbent                 }
73292903bd4SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
73392903bd4SJohn Edward Broadbent                 }); // end association lambda
73492903bd4SJohn Edward Broadbent 
73592903bd4SJohn Edward Broadbent         } // end Iterate over all retrieved ObjectPaths
73692903bd4SJohn Edward Broadbent         },
73792903bd4SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
73892903bd4SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
73992903bd4SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
74092903bd4SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", 0,
74192903bd4SJohn Edward Broadbent         std::array<const char*, 2>{
74292903bd4SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Board",
74392903bd4SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Chassis"});
74492903bd4SJohn Edward Broadbent }
74592903bd4SJohn Edward Broadbent 
74692903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
74792903bd4SJohn Edward Broadbent {
74892903bd4SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
74992903bd4SJohn Edward Broadbent         .privileges(redfish::privileges::getDriveCollection)
75092903bd4SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
75192903bd4SJohn Edward Broadbent             std::bind_front(chassisDriveCollectionGet, std::ref(app)));
75292903bd4SJohn Edward Broadbent }
75392903bd4SJohn Edward Broadbent 
754b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
755b53dcd9dSNan Zhou                        const std::string& chassisId,
756b53dcd9dSNan Zhou                        const std::string& driveName,
757e56ed6b9SJohn Edward Broadbent                        const boost::system::error_code ec,
758e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
759e56ed6b9SJohn Edward Broadbent {
760e56ed6b9SJohn Edward Broadbent 
761e56ed6b9SJohn Edward Broadbent     if (ec)
762e56ed6b9SJohn Edward Broadbent     {
763e56ed6b9SJohn Edward Broadbent         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
764e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
765e56ed6b9SJohn Edward Broadbent         return;
766e56ed6b9SJohn Edward Broadbent     }
767e56ed6b9SJohn Edward Broadbent 
768e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
7698cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
770e56ed6b9SJohn Edward Broadbent     {
771e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
772e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
773e56ed6b9SJohn Edward Broadbent         {
774e56ed6b9SJohn Edward Broadbent             continue;
775e56ed6b9SJohn Edward Broadbent         }
776e56ed6b9SJohn Edward Broadbent 
777e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
778e56ed6b9SJohn Edward Broadbent         {
779e56ed6b9SJohn Edward Broadbent             BMCWEB_LOG_ERROR << "Got 0 Connection names";
780e56ed6b9SJohn Edward Broadbent             continue;
781e56ed6b9SJohn Edward Broadbent         }
782e56ed6b9SJohn Edward Broadbent 
783e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
784a0cb40cbSJohn Edward Broadbent             "redfish", "v1", "Chassis", chassisId, "Drives", driveName);
785e56ed6b9SJohn Edward Broadbent 
786e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
787a0cb40cbSJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = driveName;
788e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
789e56ed6b9SJohn Edward Broadbent         // default it to Enabled
790e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
791e56ed6b9SJohn Edward Broadbent 
792e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
793e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
794e56ed6b9SJohn Edward Broadbent             crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId);
795e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
796e56ed6b9SJohn Edward Broadbent 
797e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
798e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
799e56ed6b9SJohn Edward Broadbent     }
800e56ed6b9SJohn Edward Broadbent }
801e56ed6b9SJohn Edward Broadbent 
802b53dcd9dSNan Zhou inline void
803b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
804e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
805e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
806e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
807e56ed6b9SJohn Edward Broadbent {
808e56ed6b9SJohn Edward Broadbent 
809e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
810e56ed6b9SJohn Edward Broadbent     {
811e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
812e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
813e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
814e56ed6b9SJohn Edward Broadbent         {
815e56ed6b9SJohn Edward Broadbent             continue;
816e56ed6b9SJohn Edward Broadbent         }
817e56ed6b9SJohn Edward Broadbent         //  mapper call drive
818e56ed6b9SJohn Edward Broadbent         const std::array<const char*, 1> driveInterface = {
819e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
820e56ed6b9SJohn Edward Broadbent 
821e56ed6b9SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
822e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
823e56ed6b9SJohn Edward Broadbent                 const boost::system::error_code ec,
824e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
825e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
826e56ed6b9SJohn Edward Broadbent             },
827e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper",
828e56ed6b9SJohn Edward Broadbent             "/xyz/openbmc_project/object_mapper",
829e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
830e56ed6b9SJohn Edward Broadbent             "/xyz/openbmc_project/inventory", 0, driveInterface);
831e56ed6b9SJohn Edward Broadbent     }
832e56ed6b9SJohn Edward Broadbent }
833e56ed6b9SJohn Edward Broadbent 
834b53dcd9dSNan Zhou inline void
835b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
836e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
837e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
838e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
839e56ed6b9SJohn Edward Broadbent {
84003810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
841e56ed6b9SJohn Edward Broadbent     {
842e56ed6b9SJohn Edward Broadbent         return;
843e56ed6b9SJohn Edward Broadbent     }
844e56ed6b9SJohn Edward Broadbent     const std::array<const char*, 2> interfaces = {
845e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
846e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
847e56ed6b9SJohn Edward Broadbent 
848e56ed6b9SJohn Edward Broadbent     // mapper call chassis
849e56ed6b9SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
850e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
851e56ed6b9SJohn Edward Broadbent          driveName](const boost::system::error_code ec,
852e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
853e56ed6b9SJohn Edward Broadbent         if (ec)
854e56ed6b9SJohn Edward Broadbent         {
855e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
856e56ed6b9SJohn Edward Broadbent             return;
857e56ed6b9SJohn Edward Broadbent         }
858e56ed6b9SJohn Edward Broadbent 
859e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
8608cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
861e56ed6b9SJohn Edward Broadbent         {
862e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
863e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
864e56ed6b9SJohn Edward Broadbent             {
865e56ed6b9SJohn Edward Broadbent                 continue;
866e56ed6b9SJohn Edward Broadbent             }
867e56ed6b9SJohn Edward Broadbent 
868e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
869e56ed6b9SJohn Edward Broadbent             {
870e56ed6b9SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
871e56ed6b9SJohn Edward Broadbent                 continue;
872e56ed6b9SJohn Edward Broadbent             }
873e56ed6b9SJohn Edward Broadbent 
874e56ed6b9SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
875e56ed6b9SJohn Edward Broadbent                 *crow::connections::systemBus,
876e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
877e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
878e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
879e56ed6b9SJohn Edward Broadbent                  driveName](const boost::system::error_code ec3,
880e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& resp) {
881e56ed6b9SJohn Edward Broadbent                 if (ec3)
882e56ed6b9SJohn Edward Broadbent                 {
883e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
884e56ed6b9SJohn Edward Broadbent                 }
885e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
886e56ed6b9SJohn Edward Broadbent                 });
887e56ed6b9SJohn Edward Broadbent             break;
888e56ed6b9SJohn Edward Broadbent         }
889e56ed6b9SJohn Edward Broadbent         },
890e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
891e56ed6b9SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
892e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
893e56ed6b9SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", 0, interfaces);
894e56ed6b9SJohn Edward Broadbent }
895e56ed6b9SJohn Edward Broadbent 
896e56ed6b9SJohn Edward Broadbent /**
897e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
898e56ed6b9SJohn Edward Broadbent  */
899e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
900e56ed6b9SJohn Edward Broadbent {
901e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
902e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
903e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
904e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
905e56ed6b9SJohn Edward Broadbent }
906e56ed6b9SJohn Edward Broadbent 
907a25aeccfSNikhil Potade } // namespace redfish
908