xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision 13451e3913c29bb380ef6778f11749d337ddd442)
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*13451e39SWilly Tu #include "bmcweb_config.h"
19*13451e39SWilly Tu 
203ccb3adbSEd Tanous #include "app.hpp"
217a1dbc48SGeorge Liu #include "dbus_utility.hpp"
22e5029d88SJohn Edward Broadbent #include "generated/enums/drive.hpp"
23dde9bc12SGeorge Liu #include "generated/enums/protocol.hpp"
242ad9c2f6SJames Feist #include "health.hpp"
25a8e884fcSEd Tanous #include "human_sort.hpp"
26e284a7c1SJames Feist #include "openbmc_dbus_rest.hpp"
273ccb3adbSEd Tanous #include "query.hpp"
28a8e884fcSEd Tanous #include "redfish_util.hpp"
293ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
303ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
312ad9c2f6SJames Feist 
32e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
33ef4c65b7SEd Tanous #include <boost/url/format.hpp>
341e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
35d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
36a25aeccfSNikhil Potade 
377a1dbc48SGeorge Liu #include <array>
387a1dbc48SGeorge Liu #include <string_view>
397a1dbc48SGeorge Liu 
40a25aeccfSNikhil Potade namespace redfish
41a25aeccfSNikhil Potade {
427e860f15SJohn Edward Broadbent inline void requestRoutesStorageCollection(App& app)
43a25aeccfSNikhil Potade {
4422d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
45ed398213SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
467e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
4745ca1b86SEd Tanous             [&app](const crow::Request& req,
4822d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4922d268cbSEd Tanous                    const std::string& systemName) {
503ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
5145ca1b86SEd Tanous         {
5245ca1b86SEd Tanous             return;
5345ca1b86SEd Tanous         }
5422d268cbSEd Tanous         if (systemName != "system")
5522d268cbSEd Tanous         {
5622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
5722d268cbSEd Tanous                                        systemName);
5822d268cbSEd Tanous             return;
5922d268cbSEd Tanous         }
6022d268cbSEd Tanous 
618d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
628d1b46d7Szhanghch05             "#StorageCollection.StorageCollection";
638d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
648d1b46d7Szhanghch05             "/redfish/v1/Systems/system/Storage";
658d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Storage Collection";
661476687dSEd Tanous         nlohmann::json::array_t members;
671476687dSEd Tanous         nlohmann::json::object_t member;
681476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
691476687dSEd Tanous         members.emplace_back(member);
701476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
718d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
727e860f15SJohn Edward Broadbent         });
73a25aeccfSNikhil Potade }
74a25aeccfSNikhil Potade 
75a85afbe1SWilly Tu inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
76a85afbe1SWilly Tu                       const std::shared_ptr<HealthPopulate>& health)
77a25aeccfSNikhil Potade {
787a1dbc48SGeorge Liu     const std::array<std::string_view, 1> interfaces = {
797a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Drive"};
807a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
817a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
82a85afbe1SWilly Tu         [asyncResp, health](
837a1dbc48SGeorge Liu             const boost::system::error_code& ec,
84a85afbe1SWilly Tu             const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
85a25aeccfSNikhil Potade         if (ec)
86a25aeccfSNikhil Potade         {
87a25aeccfSNikhil Potade             BMCWEB_LOG_ERROR << "Drive mapper call error";
88a25aeccfSNikhil Potade             messages::internalError(asyncResp->res);
89a25aeccfSNikhil Potade             return;
90a25aeccfSNikhil Potade         }
912ad9c2f6SJames Feist 
92a85afbe1SWilly Tu         nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
93a85afbe1SWilly Tu         driveArray = nlohmann::json::array();
94a85afbe1SWilly Tu         auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
95a85afbe1SWilly Tu         count = 0;
962ad9c2f6SJames Feist 
97*13451e39SWilly Tu         if constexpr (bmcwebEnableHealthPopulate)
98*13451e39SWilly Tu         {
99a85afbe1SWilly Tu             health->inventory.insert(health->inventory.end(), driveList.begin(),
100a85afbe1SWilly Tu                                      driveList.end());
101*13451e39SWilly Tu         }
102a85afbe1SWilly Tu 
103a85afbe1SWilly Tu         for (const std::string& drive : driveList)
104a25aeccfSNikhil Potade         {
105a85afbe1SWilly Tu             sdbusplus::message::object_path object(drive);
106a85afbe1SWilly Tu             if (object.filename().empty())
107a25aeccfSNikhil Potade             {
108a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
109a85afbe1SWilly Tu                 return;
110a25aeccfSNikhil Potade             }
111a85afbe1SWilly Tu 
112a85afbe1SWilly Tu             nlohmann::json::object_t driveJson;
113ef4c65b7SEd Tanous             driveJson["@odata.id"] = boost::urls::format(
114ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/{}",
115eddfc437SWilly Tu                 object.filename());
116b2ba3072SPatrick Williams             driveArray.emplace_back(std::move(driveJson));
117a25aeccfSNikhil Potade         }
118a25aeccfSNikhil Potade 
119a85afbe1SWilly Tu         count = driveArray.size();
1207a1dbc48SGeorge Liu         });
121a85afbe1SWilly Tu }
122e284a7c1SJames Feist 
123a85afbe1SWilly Tu inline void
124a85afbe1SWilly Tu     getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
125a85afbe1SWilly Tu                           const std::shared_ptr<HealthPopulate>& health)
126a85afbe1SWilly Tu {
127e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
128e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.StorageController"};
129e99073f5SGeorge Liu     dbus::utility::getSubTree(
130e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
131002d39b4SEd Tanous         [asyncResp,
132e99073f5SGeorge Liu          health](const boost::system::error_code& ec,
133b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreeResponse& subtree) {
13426f6976fSEd Tanous         if (ec || subtree.empty())
135e284a7c1SJames Feist         {
136d819a420SJames Feist             // doesn't have to be there
137e284a7c1SJames Feist             return;
138e284a7c1SJames Feist         }
139e284a7c1SJames Feist 
140a85afbe1SWilly Tu         nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
141e284a7c1SJames Feist         root = nlohmann::json::array();
142e284a7c1SJames Feist         for (const auto& [path, interfaceDict] : subtree)
143e284a7c1SJames Feist         {
144a85afbe1SWilly Tu             sdbusplus::message::object_path object(path);
145a85afbe1SWilly Tu             std::string id = object.filename();
146a85afbe1SWilly Tu             if (id.empty())
147e284a7c1SJames Feist             {
148a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
149e284a7c1SJames Feist                 return;
150e284a7c1SJames Feist             }
151e284a7c1SJames Feist 
152e284a7c1SJames Feist             if (interfaceDict.size() != 1)
153e284a7c1SJames Feist             {
154a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
155e284a7c1SJames Feist                                  << ", greater than 1";
156e284a7c1SJames Feist                 messages::internalError(asyncResp->res);
157e284a7c1SJames Feist                 return;
158e284a7c1SJames Feist             }
159e284a7c1SJames Feist 
160002d39b4SEd Tanous             const std::string& connectionName = interfaceDict.front().first;
161e284a7c1SJames Feist 
162e284a7c1SJames Feist             size_t index = root.size();
163e284a7c1SJames Feist             nlohmann::json& storageController =
164e284a7c1SJames Feist                 root.emplace_back(nlohmann::json::object());
165e284a7c1SJames Feist 
166e284a7c1SJames Feist             storageController["@odata.type"] =
167e284a7c1SJames Feist                 "#Storage.v1_7_0.StorageController";
168ef4c65b7SEd Tanous             storageController["@odata.id"] = boost::urls::format(
169ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/Storage/1#{}",
170ef4c65b7SEd Tanous                 ("/StorageControllers"_json_pointer / index).to_string());
171e284a7c1SJames Feist             storageController["Name"] = id;
172e284a7c1SJames Feist             storageController["MemberId"] = id;
173e284a7c1SJames Feist             storageController["Status"]["State"] = "Enabled";
174e284a7c1SJames Feist 
1751e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
1761e1e598dSJonathan Doman                 *crow::connections::systemBus, connectionName, path,
1771e1e598dSJonathan Doman                 "xyz.openbmc_project.Inventory.Item", "Present",
1785e7e2dc5SEd Tanous                 [asyncResp, index](const boost::system::error_code& ec2,
179cef57e85SWilly Tu                                    bool isPresent) {
1807e860f15SJohn Edward Broadbent                 // this interface isn't necessary, only check it
1817e860f15SJohn Edward Broadbent                 // if we get a good return
18223a21a1cSEd Tanous                 if (ec2)
183e284a7c1SJames Feist                 {
184e284a7c1SJames Feist                     return;
185e284a7c1SJames Feist                 }
186cef57e85SWilly Tu                 if (!isPresent)
187e284a7c1SJames Feist                 {
188002d39b4SEd Tanous                     asyncResp->res.jsonValue["StorageControllers"][index]
189cef57e85SWilly Tu                                             ["Status"]["State"] = "Absent";
190e284a7c1SJames Feist                 }
1911e1e598dSJonathan Doman                 });
192e284a7c1SJames Feist 
193d1bde9e5SKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
194d1bde9e5SKrzysztof Grobelny                 *crow::connections::systemBus, connectionName, path,
195d1bde9e5SKrzysztof Grobelny                 "xyz.openbmc_project.Inventory.Decorator.Asset",
196a85afbe1SWilly Tu                 [asyncResp, index](
1975e7e2dc5SEd Tanous                     const boost::system::error_code& ec2,
198a85afbe1SWilly Tu                     const std::vector<
199a85afbe1SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
2007e860f15SJohn Edward Broadbent                         propertiesList) {
2017e860f15SJohn Edward Broadbent                 if (ec2)
2027e860f15SJohn Edward Broadbent                 {
2037e860f15SJohn Edward Broadbent                     // this interface isn't necessary
2047e860f15SJohn Edward Broadbent                     return;
2057e860f15SJohn Edward Broadbent                 }
206d1bde9e5SKrzysztof Grobelny 
207d1bde9e5SKrzysztof Grobelny                 const std::string* partNumber = nullptr;
208d1bde9e5SKrzysztof Grobelny                 const std::string* serialNumber = nullptr;
209d1bde9e5SKrzysztof Grobelny                 const std::string* manufacturer = nullptr;
210d1bde9e5SKrzysztof Grobelny                 const std::string* model = nullptr;
211d1bde9e5SKrzysztof Grobelny 
212d1bde9e5SKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
213d1bde9e5SKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), propertiesList,
214d1bde9e5SKrzysztof Grobelny                     "PartNumber", partNumber, "SerialNumber", serialNumber,
215d1bde9e5SKrzysztof Grobelny                     "Manufacturer", manufacturer, "Model", model);
216d1bde9e5SKrzysztof Grobelny 
217d1bde9e5SKrzysztof Grobelny                 if (!success)
2187e860f15SJohn Edward Broadbent                 {
219002d39b4SEd Tanous                     messages::internalError(asyncResp->res);
2207e860f15SJohn Edward Broadbent                     return;
2217e860f15SJohn Edward Broadbent                 }
222d1bde9e5SKrzysztof Grobelny 
223d1bde9e5SKrzysztof Grobelny                 nlohmann::json& controller =
224d1bde9e5SKrzysztof Grobelny                     asyncResp->res.jsonValue["StorageControllers"][index];
225d1bde9e5SKrzysztof Grobelny 
226d1bde9e5SKrzysztof Grobelny                 if (partNumber != nullptr)
227d1bde9e5SKrzysztof Grobelny                 {
228d1bde9e5SKrzysztof Grobelny                     controller["PartNumber"] = *partNumber;
2297e860f15SJohn Edward Broadbent                 }
230d1bde9e5SKrzysztof Grobelny 
231d1bde9e5SKrzysztof Grobelny                 if (serialNumber != nullptr)
232d1bde9e5SKrzysztof Grobelny                 {
233d1bde9e5SKrzysztof Grobelny                     controller["SerialNumber"] = *serialNumber;
2347e860f15SJohn Edward Broadbent                 }
235d1bde9e5SKrzysztof Grobelny 
236d1bde9e5SKrzysztof Grobelny                 if (manufacturer != nullptr)
237d1bde9e5SKrzysztof Grobelny                 {
238d1bde9e5SKrzysztof Grobelny                     controller["Manufacturer"] = *manufacturer;
239d1bde9e5SKrzysztof Grobelny                 }
240d1bde9e5SKrzysztof Grobelny 
241d1bde9e5SKrzysztof Grobelny                 if (model != nullptr)
242d1bde9e5SKrzysztof Grobelny                 {
243d1bde9e5SKrzysztof Grobelny                     controller["Model"] = *model;
244d1bde9e5SKrzysztof Grobelny                 }
245d1bde9e5SKrzysztof Grobelny                 });
2467e860f15SJohn Edward Broadbent         }
2477e860f15SJohn Edward Broadbent 
248*13451e39SWilly Tu         if constexpr (bmcwebEnableHealthPopulate)
249*13451e39SWilly Tu         {
2507e860f15SJohn Edward Broadbent             // this is done after we know the json array will no longer
2517e860f15SJohn Edward Broadbent             // be resized, as json::array uses vector underneath and we
2527e860f15SJohn Edward Broadbent             // need references to its members that won't change
2537e860f15SJohn Edward Broadbent             size_t count = 0;
254dfababfcSNan Zhou             // Pointer based on |asyncResp->res.jsonValue|
255dfababfcSNan Zhou             nlohmann::json::json_pointer rootPtr =
256dfababfcSNan Zhou                 "/StorageControllers"_json_pointer;
2577e860f15SJohn Edward Broadbent             for (const auto& [path, interfaceDict] : subtree)
2587e860f15SJohn Edward Broadbent             {
2597e860f15SJohn Edward Broadbent                 auto subHealth = std::make_shared<HealthPopulate>(
260dfababfcSNan Zhou                     asyncResp, rootPtr / count / "Status");
2617e860f15SJohn Edward Broadbent                 subHealth->inventory.emplace_back(path);
2627e860f15SJohn Edward Broadbent                 health->inventory.emplace_back(path);
2637e860f15SJohn Edward Broadbent                 health->children.emplace_back(subHealth);
2647e860f15SJohn Edward Broadbent                 count++;
2657e860f15SJohn Edward Broadbent             }
266*13451e39SWilly Tu         }
267e99073f5SGeorge Liu         });
268a85afbe1SWilly Tu }
269a85afbe1SWilly Tu 
270a85afbe1SWilly Tu inline void requestRoutesStorage(App& app)
271a85afbe1SWilly Tu {
272a85afbe1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
273a85afbe1SWilly Tu         .privileges(redfish::privileges::getStorage)
274a85afbe1SWilly Tu         .methods(boost::beast::http::verb::get)(
275a85afbe1SWilly Tu             [&app](const crow::Request& req,
276a85afbe1SWilly Tu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2773ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
278a85afbe1SWilly Tu         {
279a85afbe1SWilly Tu             return;
280a85afbe1SWilly Tu         }
281a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
282a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
283a85afbe1SWilly Tu             "/redfish/v1/Systems/system/Storage/1";
284a85afbe1SWilly Tu         asyncResp->res.jsonValue["Name"] = "Storage";
285a85afbe1SWilly Tu         asyncResp->res.jsonValue["Id"] = "1";
286a85afbe1SWilly Tu         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
287a85afbe1SWilly Tu 
288a85afbe1SWilly Tu         auto health = std::make_shared<HealthPopulate>(asyncResp);
289*13451e39SWilly Tu         if constexpr (bmcwebEnableHealthPopulate)
290*13451e39SWilly Tu         {
291a85afbe1SWilly Tu             health->populate();
292*13451e39SWilly Tu         }
293a85afbe1SWilly Tu 
294a85afbe1SWilly Tu         getDrives(asyncResp, health);
295a85afbe1SWilly Tu         getStorageControllers(asyncResp, health);
2967e860f15SJohn Edward Broadbent         });
2977e860f15SJohn Edward Broadbent }
2987e860f15SJohn Edward Broadbent 
29903913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30003913171SWilly Tu                           const std::string& connectionName,
30103913171SWilly Tu                           const std::string& path)
30203913171SWilly Tu {
303d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
304d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, path,
305d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Asset",
3065e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
307168e20c1SEd Tanous                     const std::vector<
308168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
30903913171SWilly Tu                         propertiesList) {
31003913171SWilly Tu         if (ec)
31103913171SWilly Tu         {
31203913171SWilly Tu             // this interface isn't necessary
31303913171SWilly Tu             return;
31403913171SWilly Tu         }
315d1bde9e5SKrzysztof Grobelny 
316d1bde9e5SKrzysztof Grobelny         const std::string* partNumber = nullptr;
317d1bde9e5SKrzysztof Grobelny         const std::string* serialNumber = nullptr;
318d1bde9e5SKrzysztof Grobelny         const std::string* manufacturer = nullptr;
319d1bde9e5SKrzysztof Grobelny         const std::string* model = nullptr;
320d1bde9e5SKrzysztof Grobelny 
321d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
322d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
323d1bde9e5SKrzysztof Grobelny             partNumber, "SerialNumber", serialNumber, "Manufacturer",
324d1bde9e5SKrzysztof Grobelny             manufacturer, "Model", model);
325d1bde9e5SKrzysztof Grobelny 
326d1bde9e5SKrzysztof Grobelny         if (!success)
32703913171SWilly Tu         {
32803913171SWilly Tu             messages::internalError(asyncResp->res);
32903913171SWilly Tu             return;
33003913171SWilly Tu         }
331d1bde9e5SKrzysztof Grobelny 
332d1bde9e5SKrzysztof Grobelny         if (partNumber != nullptr)
333d1bde9e5SKrzysztof Grobelny         {
334d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["PartNumber"] = *partNumber;
33503913171SWilly Tu         }
336d1bde9e5SKrzysztof Grobelny 
337d1bde9e5SKrzysztof Grobelny         if (serialNumber != nullptr)
338d1bde9e5SKrzysztof Grobelny         {
339d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
34003913171SWilly Tu         }
341d1bde9e5SKrzysztof Grobelny 
342d1bde9e5SKrzysztof Grobelny         if (manufacturer != nullptr)
343d1bde9e5SKrzysztof Grobelny         {
344d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
345d1bde9e5SKrzysztof Grobelny         }
346d1bde9e5SKrzysztof Grobelny 
347d1bde9e5SKrzysztof Grobelny         if (model != nullptr)
348d1bde9e5SKrzysztof Grobelny         {
349d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Model"] = *model;
350d1bde9e5SKrzysztof Grobelny         }
351d1bde9e5SKrzysztof Grobelny         });
35203913171SWilly Tu }
35303913171SWilly Tu 
35403913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
35503913171SWilly Tu                             const std::string& connectionName,
35603913171SWilly Tu                             const std::string& path)
35703913171SWilly Tu {
3581e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3591e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3601e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
3615e7e2dc5SEd Tanous         [asyncResp, path](const boost::system::error_code& ec,
362cef57e85SWilly Tu                           const bool isPresent) {
36303913171SWilly Tu         // this interface isn't necessary, only check it if
36403913171SWilly Tu         // we get a good return
36503913171SWilly Tu         if (ec)
36603913171SWilly Tu         {
36703913171SWilly Tu             return;
36803913171SWilly Tu         }
36903913171SWilly Tu 
370cef57e85SWilly Tu         if (!isPresent)
37103913171SWilly Tu         {
372cef57e85SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Absent";
37303913171SWilly Tu         }
3741e1e598dSJonathan Doman         });
37503913171SWilly Tu }
37603913171SWilly Tu 
37703913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
37803913171SWilly Tu                           const std::string& connectionName,
37903913171SWilly Tu                           const std::string& path)
38003913171SWilly Tu {
3811e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3821e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3831e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3845e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec, const bool updating) {
38503913171SWilly Tu         // this interface isn't necessary, only check it
38603913171SWilly Tu         // if we get a good return
38703913171SWilly Tu         if (ec)
38803913171SWilly Tu         {
38903913171SWilly Tu             return;
39003913171SWilly Tu         }
39103913171SWilly Tu 
39203913171SWilly Tu         // updating and disabled in the backend shouldn't be
39303913171SWilly Tu         // able to be set at the same time, so we don't need
39403913171SWilly Tu         // to check for the race condition of these two
39503913171SWilly Tu         // calls
3961e1e598dSJonathan Doman         if (updating)
39703913171SWilly Tu         {
39803913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Updating";
39903913171SWilly Tu         }
4001e1e598dSJonathan Doman         });
40103913171SWilly Tu }
40203913171SWilly Tu 
403dde9bc12SGeorge Liu inline std::optional<drive::MediaType> convertDriveType(std::string_view type)
40419b8e9a0SWilly Tu {
40519b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
40619b8e9a0SWilly Tu     {
407dde9bc12SGeorge Liu         return drive::MediaType::HDD;
40819b8e9a0SWilly Tu     }
40919b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
41019b8e9a0SWilly Tu     {
411dde9bc12SGeorge Liu         return drive::MediaType::SSD;
41219b8e9a0SWilly Tu     }
413dde9bc12SGeorge Liu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.Unknown")
414dde9bc12SGeorge Liu     {
41519b8e9a0SWilly Tu         return std::nullopt;
41619b8e9a0SWilly Tu     }
41719b8e9a0SWilly Tu 
418dde9bc12SGeorge Liu     return drive::MediaType::Invalid;
419dde9bc12SGeorge Liu }
420dde9bc12SGeorge Liu 
421dde9bc12SGeorge Liu inline std::optional<protocol::Protocol>
422dde9bc12SGeorge Liu     convertDriveProtocol(std::string_view proto)
42319b8e9a0SWilly Tu {
42419b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
42519b8e9a0SWilly Tu     {
426dde9bc12SGeorge Liu         return protocol::Protocol::SAS;
42719b8e9a0SWilly Tu     }
42819b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
42919b8e9a0SWilly Tu     {
430dde9bc12SGeorge Liu         return protocol::Protocol::SATA;
43119b8e9a0SWilly Tu     }
43219b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
43319b8e9a0SWilly Tu     {
434dde9bc12SGeorge Liu         return protocol::Protocol::NVMe;
43519b8e9a0SWilly Tu     }
43619b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
43719b8e9a0SWilly Tu     {
438dde9bc12SGeorge Liu         return protocol::Protocol::FC;
439dde9bc12SGeorge Liu     }
440dde9bc12SGeorge Liu     if (proto ==
441dde9bc12SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.Unknown")
442dde9bc12SGeorge Liu     {
443dde9bc12SGeorge Liu         return std::nullopt;
44419b8e9a0SWilly Tu     }
44519b8e9a0SWilly Tu 
446dde9bc12SGeorge Liu     return protocol::Protocol::Invalid;
44719b8e9a0SWilly Tu }
44819b8e9a0SWilly Tu 
44919b8e9a0SWilly Tu inline void
45019b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
45119b8e9a0SWilly Tu                            const std::string& connectionName,
45219b8e9a0SWilly Tu                            const std::string& path)
45319b8e9a0SWilly Tu {
45419b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
45519b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
45619b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
4575e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
45819b8e9a0SWilly Tu                     const std::vector<
45919b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
46019b8e9a0SWilly Tu                         propertiesList) {
46119b8e9a0SWilly Tu         if (ec)
46219b8e9a0SWilly Tu         {
46319b8e9a0SWilly Tu             // this interface isn't required
46419b8e9a0SWilly Tu             return;
46519b8e9a0SWilly Tu         }
466e5029d88SJohn Edward Broadbent         const std::string* encryptionStatus = nullptr;
467e5029d88SJohn Edward Broadbent         const bool* isLocked = nullptr;
46819b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
46919b8e9a0SWilly Tu                  property : propertiesList)
47019b8e9a0SWilly Tu         {
47119b8e9a0SWilly Tu             const std::string& propertyName = property.first;
47219b8e9a0SWilly Tu             if (propertyName == "Type")
47319b8e9a0SWilly Tu             {
47419b8e9a0SWilly Tu                 const std::string* value =
47519b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
47619b8e9a0SWilly Tu                 if (value == nullptr)
47719b8e9a0SWilly Tu                 {
47819b8e9a0SWilly Tu                     // illegal property
47919b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Type";
48019b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
48119b8e9a0SWilly Tu                     return;
48219b8e9a0SWilly Tu                 }
48319b8e9a0SWilly Tu 
484dde9bc12SGeorge Liu                 std::optional<drive::MediaType> mediaType =
485dde9bc12SGeorge Liu                     convertDriveType(*value);
48619b8e9a0SWilly Tu                 if (!mediaType)
48719b8e9a0SWilly Tu                 {
488dde9bc12SGeorge Liu                     BMCWEB_LOG_WARNING << "UnknownDriveType Interface: "
48919b8e9a0SWilly Tu                                        << *value;
490dde9bc12SGeorge Liu                     continue;
491dde9bc12SGeorge Liu                 }
492dde9bc12SGeorge Liu                 if (*mediaType == drive::MediaType::Invalid)
493dde9bc12SGeorge Liu                 {
49419b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
49519b8e9a0SWilly Tu                     return;
49619b8e9a0SWilly Tu                 }
49719b8e9a0SWilly Tu 
49819b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
49919b8e9a0SWilly Tu             }
50019b8e9a0SWilly Tu             else if (propertyName == "Capacity")
50119b8e9a0SWilly Tu             {
50219b8e9a0SWilly Tu                 const uint64_t* capacity =
50319b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
50419b8e9a0SWilly Tu                 if (capacity == nullptr)
50519b8e9a0SWilly Tu                 {
50619b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Capacity";
50719b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
50819b8e9a0SWilly Tu                     return;
50919b8e9a0SWilly Tu                 }
51019b8e9a0SWilly Tu                 if (*capacity == 0)
51119b8e9a0SWilly Tu                 {
51219b8e9a0SWilly Tu                     // drive capacity not known
51319b8e9a0SWilly Tu                     continue;
51419b8e9a0SWilly Tu                 }
51519b8e9a0SWilly Tu 
51619b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
51719b8e9a0SWilly Tu             }
51819b8e9a0SWilly Tu             else if (propertyName == "Protocol")
51919b8e9a0SWilly Tu             {
52019b8e9a0SWilly Tu                 const std::string* value =
52119b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
52219b8e9a0SWilly Tu                 if (value == nullptr)
52319b8e9a0SWilly Tu                 {
52419b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Protocol";
52519b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
52619b8e9a0SWilly Tu                     return;
52719b8e9a0SWilly Tu                 }
52819b8e9a0SWilly Tu 
529dde9bc12SGeorge Liu                 std::optional<protocol::Protocol> proto =
530dde9bc12SGeorge Liu                     convertDriveProtocol(*value);
53119b8e9a0SWilly Tu                 if (!proto)
53219b8e9a0SWilly Tu                 {
533dde9bc12SGeorge Liu                     BMCWEB_LOG_WARNING << "Unknown DrivePrototype Interface: "
53419b8e9a0SWilly Tu                                        << *value;
535dde9bc12SGeorge Liu                     continue;
536dde9bc12SGeorge Liu                 }
537dde9bc12SGeorge Liu                 if (*proto == protocol::Protocol::Invalid)
538dde9bc12SGeorge Liu                 {
53919b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
54019b8e9a0SWilly Tu                     return;
54119b8e9a0SWilly Tu                 }
54219b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
54319b8e9a0SWilly Tu             }
5443fe4d5ccSJohn Edward Broadbent             else if (propertyName == "PredictedMediaLifeLeftPercent")
5453fe4d5ccSJohn Edward Broadbent             {
5463fe4d5ccSJohn Edward Broadbent                 const uint8_t* lifeLeft =
5473fe4d5ccSJohn Edward Broadbent                     std::get_if<uint8_t>(&property.second);
5483fe4d5ccSJohn Edward Broadbent                 if (lifeLeft == nullptr)
5493fe4d5ccSJohn Edward Broadbent                 {
5503fe4d5ccSJohn Edward Broadbent                     BMCWEB_LOG_ERROR
5513fe4d5ccSJohn Edward Broadbent                         << "Illegal property: PredictedMediaLifeLeftPercent";
5523fe4d5ccSJohn Edward Broadbent                     messages::internalError(asyncResp->res);
5533fe4d5ccSJohn Edward Broadbent                     return;
5543fe4d5ccSJohn Edward Broadbent                 }
5553fe4d5ccSJohn Edward Broadbent                 // 255 means reading the value is not supported
5563fe4d5ccSJohn Edward Broadbent                 if (*lifeLeft != 255)
5573fe4d5ccSJohn Edward Broadbent                 {
5583fe4d5ccSJohn Edward Broadbent                     asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
5593fe4d5ccSJohn Edward Broadbent                         *lifeLeft;
5603fe4d5ccSJohn Edward Broadbent                 }
5613fe4d5ccSJohn Edward Broadbent             }
562e5029d88SJohn Edward Broadbent             else if (propertyName == "EncryptionStatus")
563e5029d88SJohn Edward Broadbent             {
564e5029d88SJohn Edward Broadbent                 encryptionStatus = std::get_if<std::string>(&property.second);
565e5029d88SJohn Edward Broadbent                 if (encryptionStatus == nullptr)
566e5029d88SJohn Edward Broadbent                 {
567e5029d88SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Illegal property: EncryptionStatus";
568e5029d88SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
569e5029d88SJohn Edward Broadbent                     return;
57019b8e9a0SWilly Tu                 }
571e5029d88SJohn Edward Broadbent             }
572e5029d88SJohn Edward Broadbent             else if (propertyName == "Locked")
573e5029d88SJohn Edward Broadbent             {
574e5029d88SJohn Edward Broadbent                 isLocked = std::get_if<bool>(&property.second);
575e5029d88SJohn Edward Broadbent                 if (isLocked == nullptr)
576e5029d88SJohn Edward Broadbent                 {
577e5029d88SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Illegal property: Locked";
578e5029d88SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
579e5029d88SJohn Edward Broadbent                     return;
580e5029d88SJohn Edward Broadbent                 }
581e5029d88SJohn Edward Broadbent             }
582e5029d88SJohn Edward Broadbent         }
583e5029d88SJohn Edward Broadbent 
584e5029d88SJohn Edward Broadbent         if (encryptionStatus == nullptr || isLocked == nullptr ||
585e5029d88SJohn Edward Broadbent             *encryptionStatus ==
586e5029d88SJohn Edward Broadbent                 "xyz.openbmc_project.Drive.DriveEncryptionState.Unknown")
587e5029d88SJohn Edward Broadbent         {
588e5029d88SJohn Edward Broadbent             return;
589e5029d88SJohn Edward Broadbent         }
590e5029d88SJohn Edward Broadbent         if (*encryptionStatus !=
591e5029d88SJohn Edward Broadbent             "xyz.openbmc_project.Drive.DriveEncryptionState.Encrypted")
592e5029d88SJohn Edward Broadbent         {
593e5029d88SJohn Edward Broadbent             //"The drive is not currently encrypted."
594e5029d88SJohn Edward Broadbent             asyncResp->res.jsonValue["EncryptionStatus"] =
595e5029d88SJohn Edward Broadbent                 drive::EncryptionStatus::Unencrypted;
596e5029d88SJohn Edward Broadbent             return;
597e5029d88SJohn Edward Broadbent         }
598e5029d88SJohn Edward Broadbent         if (*isLocked)
599e5029d88SJohn Edward Broadbent         {
600e5029d88SJohn Edward Broadbent             //"The drive is currently encrypted and the data is not
601e5029d88SJohn Edward Broadbent             // accessible to the user."
602e5029d88SJohn Edward Broadbent             asyncResp->res.jsonValue["EncryptionStatus"] =
603e5029d88SJohn Edward Broadbent                 drive::EncryptionStatus::Locked;
604e5029d88SJohn Edward Broadbent             return;
605e5029d88SJohn Edward Broadbent         }
606e5029d88SJohn Edward Broadbent         // if not locked
607e5029d88SJohn Edward Broadbent         // "The drive is currently encrypted but the data is accessible
608e5029d88SJohn Edward Broadbent         // to the user in unencrypted form."
609e5029d88SJohn Edward Broadbent         asyncResp->res.jsonValue["EncryptionStatus"] =
610e5029d88SJohn Edward Broadbent             drive::EncryptionStatus::Unlocked;
61119b8e9a0SWilly Tu         });
61219b8e9a0SWilly Tu }
61319b8e9a0SWilly Tu 
614b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
615b53dcd9dSNan Zhou                             const std::string& connectionName,
616b53dcd9dSNan Zhou                             const std::string& path,
617e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
618e56ed6b9SJohn Edward Broadbent {
619e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
620e56ed6b9SJohn Edward Broadbent     {
621e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
622e56ed6b9SJohn Edward Broadbent         {
623e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
624e56ed6b9SJohn Edward Broadbent         }
625e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
626e56ed6b9SJohn Edward Broadbent         {
627e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
628e56ed6b9SJohn Edward Broadbent         }
629e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
630e56ed6b9SJohn Edward Broadbent         {
631e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
632e56ed6b9SJohn Edward Broadbent         }
633e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
634e56ed6b9SJohn Edward Broadbent         {
635e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
636e56ed6b9SJohn Edward Broadbent         }
637e56ed6b9SJohn Edward Broadbent     }
638e56ed6b9SJohn Edward Broadbent }
639e56ed6b9SJohn Edward Broadbent 
6407e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
6417e860f15SJohn Edward Broadbent {
64222d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
643ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
644002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
645002d39b4SEd Tanous             [&app](const crow::Request& req,
64645ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
64722d268cbSEd Tanous                    const std::string& systemName, const std::string& driveId) {
6483ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
64945ca1b86SEd Tanous         {
65045ca1b86SEd Tanous             return;
65145ca1b86SEd Tanous         }
65222d268cbSEd Tanous         if (systemName != "system")
65322d268cbSEd Tanous         {
65422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
65522d268cbSEd Tanous                                        systemName);
65622d268cbSEd Tanous             return;
65722d268cbSEd Tanous         }
65822d268cbSEd Tanous 
659e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
660e99073f5SGeorge Liu             "xyz.openbmc_project.Inventory.Item.Drive"};
661e99073f5SGeorge Liu         dbus::utility::getSubTree(
662e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, interfaces,
663002d39b4SEd Tanous             [asyncResp,
664e99073f5SGeorge Liu              driveId](const boost::system::error_code& ec,
665b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
6667e860f15SJohn Edward Broadbent             if (ec)
6677e860f15SJohn Edward Broadbent             {
6687e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Drive mapper call error";
6697e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
6707e860f15SJohn Edward Broadbent                 return;
6717e860f15SJohn Edward Broadbent             }
6727e860f15SJohn Edward Broadbent 
67303913171SWilly Tu             auto drive = std::find_if(
6747e860f15SJohn Edward Broadbent                 subtree.begin(), subtree.end(),
675002d39b4SEd Tanous                 [&driveId](
6768cb65f8aSNan Zhou                     const std::pair<std::string,
6778cb65f8aSNan Zhou                                     dbus::utility::MapperServiceMap>& object) {
67803913171SWilly Tu                 return sdbusplus::message::object_path(object.first)
67903913171SWilly Tu                            .filename() == driveId;
6807e860f15SJohn Edward Broadbent                 });
6817e860f15SJohn Edward Broadbent 
68203913171SWilly Tu             if (drive == subtree.end())
6837e860f15SJohn Edward Broadbent             {
684002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
6857e860f15SJohn Edward Broadbent                 return;
6867e860f15SJohn Edward Broadbent             }
6877e860f15SJohn Edward Broadbent 
68803913171SWilly Tu             const std::string& path = drive->first;
6898cb65f8aSNan Zhou             const dbus::utility::MapperServiceMap& connectionNames =
6908cb65f8aSNan Zhou                 drive->second;
6917e860f15SJohn Edward Broadbent 
692002d39b4SEd Tanous             asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
693ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
694ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/{}", driveId);
6957e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = driveId;
6967e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = driveId;
6977e860f15SJohn Edward Broadbent 
6987e860f15SJohn Edward Broadbent             if (connectionNames.size() != 1)
6997e860f15SJohn Edward Broadbent             {
700002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
70103913171SWilly Tu                                  << ", not equal to 1";
7027e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
7037e860f15SJohn Edward Broadbent                 return;
7047e860f15SJohn Edward Broadbent             }
7057e860f15SJohn Edward Broadbent 
7067e860f15SJohn Edward Broadbent             getMainChassisId(
707ef4c65b7SEd Tanous                 asyncResp,
708ef4c65b7SEd Tanous                 [](const std::string& chassisId,
7097e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
710002d39b4SEd Tanous                 aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
711ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
7127e860f15SJohn Edward Broadbent                 });
7137e860f15SJohn Edward Broadbent 
714a25aeccfSNikhil Potade             // default it to Enabled
715a25aeccfSNikhil Potade             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
716a25aeccfSNikhil Potade 
717*13451e39SWilly Tu             if constexpr (bmcwebEnableHealthPopulate)
718*13451e39SWilly Tu             {
7192ad9c2f6SJames Feist                 auto health = std::make_shared<HealthPopulate>(asyncResp);
720e284a7c1SJames Feist                 health->inventory.emplace_back(path);
7212ad9c2f6SJames Feist                 health->populate();
722*13451e39SWilly Tu             }
7232ad9c2f6SJames Feist 
724e56ed6b9SJohn Edward Broadbent             addAllDriveInfo(asyncResp, connectionNames[0].first, path,
725e56ed6b9SJohn Edward Broadbent                             connectionNames[0].second);
726e99073f5SGeorge Liu             });
7277e860f15SJohn Edward Broadbent         });
728a25aeccfSNikhil Potade }
72992903bd4SJohn Edward Broadbent 
73092903bd4SJohn Edward Broadbent /**
73192903bd4SJohn Edward Broadbent  * Chassis drives, this URL will show all the DriveCollection
73292903bd4SJohn Edward Broadbent  * information
73392903bd4SJohn Edward Broadbent  */
734b53dcd9dSNan Zhou inline void chassisDriveCollectionGet(
73592903bd4SJohn Edward Broadbent     crow::App& app, const crow::Request& req,
73692903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
73792903bd4SJohn Edward Broadbent     const std::string& chassisId)
73892903bd4SJohn Edward Broadbent {
7393ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
74092903bd4SJohn Edward Broadbent     {
74192903bd4SJohn Edward Broadbent         return;
74292903bd4SJohn Edward Broadbent     }
74392903bd4SJohn Edward Broadbent 
74492903bd4SJohn Edward Broadbent     // mapper call lambda
745e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
746e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Board",
747e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Chassis"};
748e99073f5SGeorge Liu     dbus::utility::getSubTree(
749e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
75092903bd4SJohn Edward Broadbent         [asyncResp,
751e99073f5SGeorge Liu          chassisId](const boost::system::error_code& ec,
75292903bd4SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
75392903bd4SJohn Edward Broadbent         if (ec)
75492903bd4SJohn Edward Broadbent         {
75592903bd4SJohn Edward Broadbent             if (ec == boost::system::errc::host_unreachable)
75692903bd4SJohn Edward Broadbent             {
75792903bd4SJohn Edward Broadbent                 messages::resourceNotFound(asyncResp->res, "Chassis",
75892903bd4SJohn Edward Broadbent                                            chassisId);
75992903bd4SJohn Edward Broadbent                 return;
76092903bd4SJohn Edward Broadbent             }
76192903bd4SJohn Edward Broadbent             messages::internalError(asyncResp->res);
76292903bd4SJohn Edward Broadbent             return;
76392903bd4SJohn Edward Broadbent         }
76492903bd4SJohn Edward Broadbent 
76592903bd4SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
7668cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
76792903bd4SJohn Edward Broadbent         {
76892903bd4SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
76992903bd4SJohn Edward Broadbent             if (objPath.filename() != chassisId)
77092903bd4SJohn Edward Broadbent             {
77192903bd4SJohn Edward Broadbent                 continue;
77292903bd4SJohn Edward Broadbent             }
77392903bd4SJohn Edward Broadbent 
77492903bd4SJohn Edward Broadbent             if (connectionNames.empty())
77592903bd4SJohn Edward Broadbent             {
77692903bd4SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
77792903bd4SJohn Edward Broadbent                 continue;
77892903bd4SJohn Edward Broadbent             }
77992903bd4SJohn Edward Broadbent 
78092903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.type"] =
78192903bd4SJohn Edward Broadbent                 "#DriveCollection.DriveCollection";
78292903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
783ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}/Drives", chassisId);
78492903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "Drive Collection";
78592903bd4SJohn Edward Broadbent 
78692903bd4SJohn Edward Broadbent             // Association lambda
7876c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
7886c3e9451SGeorge Liu                 path + "/drive",
7896c3e9451SGeorge Liu                 [asyncResp,
7906c3e9451SGeorge Liu                  chassisId](const boost::system::error_code& ec3,
7916c3e9451SGeorge Liu                             const dbus::utility::MapperEndPoints& resp) {
79292903bd4SJohn Edward Broadbent                 if (ec3)
79392903bd4SJohn Edward Broadbent                 {
79492903bd4SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
79592903bd4SJohn Edward Broadbent                 }
79692903bd4SJohn Edward Broadbent                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
79792903bd4SJohn Edward Broadbent                 // important if array is empty
79892903bd4SJohn Edward Broadbent                 members = nlohmann::json::array();
79992903bd4SJohn Edward Broadbent 
80092903bd4SJohn Edward Broadbent                 std::vector<std::string> leafNames;
80192903bd4SJohn Edward Broadbent                 for (const auto& drive : resp)
80292903bd4SJohn Edward Broadbent                 {
8038a592810SEd Tanous                     sdbusplus::message::object_path drivePath(drive);
8048a592810SEd Tanous                     leafNames.push_back(drivePath.filename());
80592903bd4SJohn Edward Broadbent                 }
80692903bd4SJohn Edward Broadbent 
80792903bd4SJohn Edward Broadbent                 std::sort(leafNames.begin(), leafNames.end(),
80892903bd4SJohn Edward Broadbent                           AlphanumLess<std::string>());
80992903bd4SJohn Edward Broadbent 
81092903bd4SJohn Edward Broadbent                 for (const auto& leafName : leafNames)
81192903bd4SJohn Edward Broadbent                 {
81292903bd4SJohn Edward Broadbent                     nlohmann::json::object_t member;
813ef4c65b7SEd Tanous                     member["@odata.id"] =
814ef4c65b7SEd Tanous                         boost::urls::format("/redfish/v1/Chassis/{}/Drives/{}",
815ef4c65b7SEd Tanous                                             chassisId, leafName);
816b2ba3072SPatrick Williams                     members.emplace_back(std::move(member));
81792903bd4SJohn Edward Broadbent                     // navigation links will be registered in next patch set
81892903bd4SJohn Edward Broadbent                 }
81992903bd4SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
82092903bd4SJohn Edward Broadbent                 }); // end association lambda
82192903bd4SJohn Edward Broadbent 
82292903bd4SJohn Edward Broadbent         }           // end Iterate over all retrieved ObjectPaths
823e99073f5SGeorge Liu         });
82492903bd4SJohn Edward Broadbent }
82592903bd4SJohn Edward Broadbent 
82692903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
82792903bd4SJohn Edward Broadbent {
82892903bd4SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
82992903bd4SJohn Edward Broadbent         .privileges(redfish::privileges::getDriveCollection)
83092903bd4SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
83192903bd4SJohn Edward Broadbent             std::bind_front(chassisDriveCollectionGet, std::ref(app)));
83292903bd4SJohn Edward Broadbent }
83392903bd4SJohn Edward Broadbent 
834b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
835b53dcd9dSNan Zhou                        const std::string& chassisId,
836b53dcd9dSNan Zhou                        const std::string& driveName,
8375e7e2dc5SEd Tanous                        const boost::system::error_code& ec,
838e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
839e56ed6b9SJohn Edward Broadbent {
840e56ed6b9SJohn Edward Broadbent     if (ec)
841e56ed6b9SJohn Edward Broadbent     {
842e56ed6b9SJohn Edward Broadbent         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
843e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
844e56ed6b9SJohn Edward Broadbent         return;
845e56ed6b9SJohn Edward Broadbent     }
846e56ed6b9SJohn Edward Broadbent 
847e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
8488cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
849e56ed6b9SJohn Edward Broadbent     {
850e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
851e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
852e56ed6b9SJohn Edward Broadbent         {
853e56ed6b9SJohn Edward Broadbent             continue;
854e56ed6b9SJohn Edward Broadbent         }
855e56ed6b9SJohn Edward Broadbent 
856e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
857e56ed6b9SJohn Edward Broadbent         {
858e56ed6b9SJohn Edward Broadbent             BMCWEB_LOG_ERROR << "Got 0 Connection names";
859e56ed6b9SJohn Edward Broadbent             continue;
860e56ed6b9SJohn Edward Broadbent         }
861e56ed6b9SJohn Edward Broadbent 
862ef4c65b7SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
863ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/Drives/{}", chassisId, driveName);
864e56ed6b9SJohn Edward Broadbent 
865e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
866a0cb40cbSJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = driveName;
867e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
868e56ed6b9SJohn Edward Broadbent         // default it to Enabled
869e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
870e56ed6b9SJohn Edward Broadbent 
871e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
872e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
873ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
874e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
875e56ed6b9SJohn Edward Broadbent 
876e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
877e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
878e56ed6b9SJohn Edward Broadbent     }
879e56ed6b9SJohn Edward Broadbent }
880e56ed6b9SJohn Edward Broadbent 
881b53dcd9dSNan Zhou inline void
882b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
883e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
884e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
885e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
886e56ed6b9SJohn Edward Broadbent {
887e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
888e56ed6b9SJohn Edward Broadbent     {
889e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
890e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
891e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
892e56ed6b9SJohn Edward Broadbent         {
893e56ed6b9SJohn Edward Broadbent             continue;
894e56ed6b9SJohn Edward Broadbent         }
895e56ed6b9SJohn Edward Broadbent         //  mapper call drive
896e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> driveInterface = {
897e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
898e99073f5SGeorge Liu         dbus::utility::getSubTree(
899e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, driveInterface,
900e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
901e99073f5SGeorge Liu                 const boost::system::error_code& ec,
902e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
903e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
904e99073f5SGeorge Liu             });
905e56ed6b9SJohn Edward Broadbent     }
906e56ed6b9SJohn Edward Broadbent }
907e56ed6b9SJohn Edward Broadbent 
908b53dcd9dSNan Zhou inline void
909b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
910e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
911e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
912e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
913e56ed6b9SJohn Edward Broadbent {
91403810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
915e56ed6b9SJohn Edward Broadbent     {
916e56ed6b9SJohn Edward Broadbent         return;
917e56ed6b9SJohn Edward Broadbent     }
918e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
919e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
920e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
921e56ed6b9SJohn Edward Broadbent 
922e56ed6b9SJohn Edward Broadbent     // mapper call chassis
923e99073f5SGeorge Liu     dbus::utility::getSubTree(
924e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
925e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
926e99073f5SGeorge Liu          driveName](const boost::system::error_code& ec,
927e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
928e56ed6b9SJohn Edward Broadbent         if (ec)
929e56ed6b9SJohn Edward Broadbent         {
930e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
931e56ed6b9SJohn Edward Broadbent             return;
932e56ed6b9SJohn Edward Broadbent         }
933e56ed6b9SJohn Edward Broadbent 
934e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
9358cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
936e56ed6b9SJohn Edward Broadbent         {
937e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
938e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
939e56ed6b9SJohn Edward Broadbent             {
940e56ed6b9SJohn Edward Broadbent                 continue;
941e56ed6b9SJohn Edward Broadbent             }
942e56ed6b9SJohn Edward Broadbent 
943e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
944e56ed6b9SJohn Edward Broadbent             {
945e56ed6b9SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
946e56ed6b9SJohn Edward Broadbent                 continue;
947e56ed6b9SJohn Edward Broadbent             }
948e56ed6b9SJohn Edward Broadbent 
9496c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
9506c3e9451SGeorge Liu                 path + "/drive",
951e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
9525e7e2dc5SEd Tanous                  driveName](const boost::system::error_code& ec3,
9536c3e9451SGeorge Liu                             const dbus::utility::MapperEndPoints& resp) {
954e56ed6b9SJohn Edward Broadbent                 if (ec3)
955e56ed6b9SJohn Edward Broadbent                 {
956e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
957e56ed6b9SJohn Edward Broadbent                 }
958e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
959e56ed6b9SJohn Edward Broadbent                 });
960e56ed6b9SJohn Edward Broadbent             break;
961e56ed6b9SJohn Edward Broadbent         }
962e99073f5SGeorge Liu         });
963e56ed6b9SJohn Edward Broadbent }
964e56ed6b9SJohn Edward Broadbent 
965e56ed6b9SJohn Edward Broadbent /**
966e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
967e56ed6b9SJohn Edward Broadbent  */
968e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
969e56ed6b9SJohn Edward Broadbent {
970e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
971e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
972e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
973e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
974e56ed6b9SJohn Edward Broadbent }
975e56ed6b9SJohn Edward Broadbent 
976a25aeccfSNikhil Potade } // namespace redfish
977