xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision dde9bc12ed98a89a0ce662be580738efadf2578c)
1a25aeccfSNikhil Potade /*
2a25aeccfSNikhil Potade // Copyright (c) 2019 Intel Corporation
3a25aeccfSNikhil Potade //
4a25aeccfSNikhil Potade // Licensed under the Apache License, Version 2.0 (the "License");
5a25aeccfSNikhil Potade // you may not use this file except in compliance with the License.
6a25aeccfSNikhil Potade // You may obtain a copy of the License at
7a25aeccfSNikhil Potade //
8a25aeccfSNikhil Potade //      http://www.apache.org/licenses/LICENSE-2.0
9a25aeccfSNikhil Potade //
10a25aeccfSNikhil Potade // Unless required by applicable law or agreed to in writing, software
11a25aeccfSNikhil Potade // distributed under the License is distributed on an "AS IS" BASIS,
12a25aeccfSNikhil Potade // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a25aeccfSNikhil Potade // See the License for the specific language governing permissions and
14a25aeccfSNikhil Potade // limitations under the License.
15a25aeccfSNikhil Potade */
16a25aeccfSNikhil Potade #pragma once
17a25aeccfSNikhil Potade 
183ccb3adbSEd Tanous #include "app.hpp"
197a1dbc48SGeorge Liu #include "dbus_utility.hpp"
20e5029d88SJohn Edward Broadbent #include "generated/enums/drive.hpp"
21*dde9bc12SGeorge Liu #include "generated/enums/protocol.hpp"
222ad9c2f6SJames Feist #include "health.hpp"
23a8e884fcSEd Tanous #include "human_sort.hpp"
24e284a7c1SJames Feist #include "openbmc_dbus_rest.hpp"
253ccb3adbSEd Tanous #include "query.hpp"
26a8e884fcSEd Tanous #include "redfish_util.hpp"
273ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
283ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
292ad9c2f6SJames Feist 
30e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
31ef4c65b7SEd Tanous #include <boost/url/format.hpp>
321e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
33d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
34a25aeccfSNikhil Potade 
357a1dbc48SGeorge Liu #include <array>
367a1dbc48SGeorge Liu #include <string_view>
377a1dbc48SGeorge Liu 
38a25aeccfSNikhil Potade namespace redfish
39a25aeccfSNikhil Potade {
407e860f15SJohn Edward Broadbent inline void requestRoutesStorageCollection(App& app)
41a25aeccfSNikhil Potade {
4222d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
43ed398213SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
447e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
4545ca1b86SEd Tanous             [&app](const crow::Request& req,
4622d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4722d268cbSEd Tanous                    const std::string& systemName) {
483ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
4945ca1b86SEd Tanous         {
5045ca1b86SEd Tanous             return;
5145ca1b86SEd Tanous         }
5222d268cbSEd Tanous         if (systemName != "system")
5322d268cbSEd Tanous         {
5422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
5522d268cbSEd Tanous                                        systemName);
5622d268cbSEd Tanous             return;
5722d268cbSEd Tanous         }
5822d268cbSEd Tanous 
598d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
608d1b46d7Szhanghch05             "#StorageCollection.StorageCollection";
618d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
628d1b46d7Szhanghch05             "/redfish/v1/Systems/system/Storage";
638d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Storage Collection";
641476687dSEd Tanous         nlohmann::json::array_t members;
651476687dSEd Tanous         nlohmann::json::object_t member;
661476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
671476687dSEd Tanous         members.emplace_back(member);
681476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
698d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
707e860f15SJohn Edward Broadbent         });
71a25aeccfSNikhil Potade }
72a25aeccfSNikhil Potade 
73a85afbe1SWilly Tu inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
74a85afbe1SWilly Tu                       const std::shared_ptr<HealthPopulate>& health)
75a25aeccfSNikhil Potade {
767a1dbc48SGeorge Liu     const std::array<std::string_view, 1> interfaces = {
777a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Drive"};
787a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
797a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
80a85afbe1SWilly Tu         [asyncResp, health](
817a1dbc48SGeorge Liu             const boost::system::error_code& ec,
82a85afbe1SWilly Tu             const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
83a25aeccfSNikhil Potade         if (ec)
84a25aeccfSNikhil Potade         {
85a25aeccfSNikhil Potade             BMCWEB_LOG_ERROR << "Drive mapper call error";
86a25aeccfSNikhil Potade             messages::internalError(asyncResp->res);
87a25aeccfSNikhil Potade             return;
88a25aeccfSNikhil Potade         }
892ad9c2f6SJames Feist 
90a85afbe1SWilly Tu         nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
91a85afbe1SWilly Tu         driveArray = nlohmann::json::array();
92a85afbe1SWilly Tu         auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
93a85afbe1SWilly Tu         count = 0;
942ad9c2f6SJames Feist 
95a85afbe1SWilly Tu         health->inventory.insert(health->inventory.end(), driveList.begin(),
96a85afbe1SWilly Tu                                  driveList.end());
97a85afbe1SWilly Tu 
98a85afbe1SWilly Tu         for (const std::string& drive : driveList)
99a25aeccfSNikhil Potade         {
100a85afbe1SWilly Tu             sdbusplus::message::object_path object(drive);
101a85afbe1SWilly Tu             if (object.filename().empty())
102a25aeccfSNikhil Potade             {
103a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
104a85afbe1SWilly Tu                 return;
105a25aeccfSNikhil Potade             }
106a85afbe1SWilly Tu 
107a85afbe1SWilly Tu             nlohmann::json::object_t driveJson;
108ef4c65b7SEd Tanous             driveJson["@odata.id"] = boost::urls::format(
109ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/{}",
110eddfc437SWilly Tu                 object.filename());
111b2ba3072SPatrick Williams             driveArray.emplace_back(std::move(driveJson));
112a25aeccfSNikhil Potade         }
113a25aeccfSNikhil Potade 
114a85afbe1SWilly Tu         count = driveArray.size();
1157a1dbc48SGeorge Liu         });
116a85afbe1SWilly Tu }
117e284a7c1SJames Feist 
118a85afbe1SWilly Tu inline void
119a85afbe1SWilly Tu     getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
120a85afbe1SWilly Tu                           const std::shared_ptr<HealthPopulate>& health)
121a85afbe1SWilly Tu {
122e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
123e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.StorageController"};
124e99073f5SGeorge Liu     dbus::utility::getSubTree(
125e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
126002d39b4SEd Tanous         [asyncResp,
127e99073f5SGeorge Liu          health](const boost::system::error_code& ec,
128b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreeResponse& subtree) {
12926f6976fSEd Tanous         if (ec || subtree.empty())
130e284a7c1SJames Feist         {
131d819a420SJames Feist             // doesn't have to be there
132e284a7c1SJames Feist             return;
133e284a7c1SJames Feist         }
134e284a7c1SJames Feist 
135a85afbe1SWilly Tu         nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
136e284a7c1SJames Feist         root = nlohmann::json::array();
137e284a7c1SJames Feist         for (const auto& [path, interfaceDict] : subtree)
138e284a7c1SJames Feist         {
139a85afbe1SWilly Tu             sdbusplus::message::object_path object(path);
140a85afbe1SWilly Tu             std::string id = object.filename();
141a85afbe1SWilly Tu             if (id.empty())
142e284a7c1SJames Feist             {
143a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
144e284a7c1SJames Feist                 return;
145e284a7c1SJames Feist             }
146e284a7c1SJames Feist 
147e284a7c1SJames Feist             if (interfaceDict.size() != 1)
148e284a7c1SJames Feist             {
149a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
150e284a7c1SJames Feist                                  << ", greater than 1";
151e284a7c1SJames Feist                 messages::internalError(asyncResp->res);
152e284a7c1SJames Feist                 return;
153e284a7c1SJames Feist             }
154e284a7c1SJames Feist 
155002d39b4SEd Tanous             const std::string& connectionName = interfaceDict.front().first;
156e284a7c1SJames Feist 
157e284a7c1SJames Feist             size_t index = root.size();
158e284a7c1SJames Feist             nlohmann::json& storageController =
159e284a7c1SJames Feist                 root.emplace_back(nlohmann::json::object());
160e284a7c1SJames Feist 
161e284a7c1SJames Feist             storageController["@odata.type"] =
162e284a7c1SJames Feist                 "#Storage.v1_7_0.StorageController";
163ef4c65b7SEd Tanous             storageController["@odata.id"] = boost::urls::format(
164ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/Storage/1#{}",
165ef4c65b7SEd Tanous                 ("/StorageControllers"_json_pointer / index).to_string());
166e284a7c1SJames Feist             storageController["Name"] = id;
167e284a7c1SJames Feist             storageController["MemberId"] = id;
168e284a7c1SJames Feist             storageController["Status"]["State"] = "Enabled";
169e284a7c1SJames Feist 
1701e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
1711e1e598dSJonathan Doman                 *crow::connections::systemBus, connectionName, path,
1721e1e598dSJonathan Doman                 "xyz.openbmc_project.Inventory.Item", "Present",
1735e7e2dc5SEd Tanous                 [asyncResp, index](const boost::system::error_code& ec2,
174cef57e85SWilly Tu                                    bool isPresent) {
1757e860f15SJohn Edward Broadbent                 // this interface isn't necessary, only check it
1767e860f15SJohn Edward Broadbent                 // if we get a good return
17723a21a1cSEd Tanous                 if (ec2)
178e284a7c1SJames Feist                 {
179e284a7c1SJames Feist                     return;
180e284a7c1SJames Feist                 }
181cef57e85SWilly Tu                 if (!isPresent)
182e284a7c1SJames Feist                 {
183002d39b4SEd Tanous                     asyncResp->res.jsonValue["StorageControllers"][index]
184cef57e85SWilly Tu                                             ["Status"]["State"] = "Absent";
185e284a7c1SJames Feist                 }
1861e1e598dSJonathan Doman                 });
187e284a7c1SJames Feist 
188d1bde9e5SKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
189d1bde9e5SKrzysztof Grobelny                 *crow::connections::systemBus, connectionName, path,
190d1bde9e5SKrzysztof Grobelny                 "xyz.openbmc_project.Inventory.Decorator.Asset",
191a85afbe1SWilly Tu                 [asyncResp, index](
1925e7e2dc5SEd Tanous                     const boost::system::error_code& ec2,
193a85afbe1SWilly Tu                     const std::vector<
194a85afbe1SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
1957e860f15SJohn Edward Broadbent                         propertiesList) {
1967e860f15SJohn Edward Broadbent                 if (ec2)
1977e860f15SJohn Edward Broadbent                 {
1987e860f15SJohn Edward Broadbent                     // this interface isn't necessary
1997e860f15SJohn Edward Broadbent                     return;
2007e860f15SJohn Edward Broadbent                 }
201d1bde9e5SKrzysztof Grobelny 
202d1bde9e5SKrzysztof Grobelny                 const std::string* partNumber = nullptr;
203d1bde9e5SKrzysztof Grobelny                 const std::string* serialNumber = nullptr;
204d1bde9e5SKrzysztof Grobelny                 const std::string* manufacturer = nullptr;
205d1bde9e5SKrzysztof Grobelny                 const std::string* model = nullptr;
206d1bde9e5SKrzysztof Grobelny 
207d1bde9e5SKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
208d1bde9e5SKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), propertiesList,
209d1bde9e5SKrzysztof Grobelny                     "PartNumber", partNumber, "SerialNumber", serialNumber,
210d1bde9e5SKrzysztof Grobelny                     "Manufacturer", manufacturer, "Model", model);
211d1bde9e5SKrzysztof Grobelny 
212d1bde9e5SKrzysztof Grobelny                 if (!success)
2137e860f15SJohn Edward Broadbent                 {
214002d39b4SEd Tanous                     messages::internalError(asyncResp->res);
2157e860f15SJohn Edward Broadbent                     return;
2167e860f15SJohn Edward Broadbent                 }
217d1bde9e5SKrzysztof Grobelny 
218d1bde9e5SKrzysztof Grobelny                 nlohmann::json& controller =
219d1bde9e5SKrzysztof Grobelny                     asyncResp->res.jsonValue["StorageControllers"][index];
220d1bde9e5SKrzysztof Grobelny 
221d1bde9e5SKrzysztof Grobelny                 if (partNumber != nullptr)
222d1bde9e5SKrzysztof Grobelny                 {
223d1bde9e5SKrzysztof Grobelny                     controller["PartNumber"] = *partNumber;
2247e860f15SJohn Edward Broadbent                 }
225d1bde9e5SKrzysztof Grobelny 
226d1bde9e5SKrzysztof Grobelny                 if (serialNumber != nullptr)
227d1bde9e5SKrzysztof Grobelny                 {
228d1bde9e5SKrzysztof Grobelny                     controller["SerialNumber"] = *serialNumber;
2297e860f15SJohn Edward Broadbent                 }
230d1bde9e5SKrzysztof Grobelny 
231d1bde9e5SKrzysztof Grobelny                 if (manufacturer != nullptr)
232d1bde9e5SKrzysztof Grobelny                 {
233d1bde9e5SKrzysztof Grobelny                     controller["Manufacturer"] = *manufacturer;
234d1bde9e5SKrzysztof Grobelny                 }
235d1bde9e5SKrzysztof Grobelny 
236d1bde9e5SKrzysztof Grobelny                 if (model != nullptr)
237d1bde9e5SKrzysztof Grobelny                 {
238d1bde9e5SKrzysztof Grobelny                     controller["Model"] = *model;
239d1bde9e5SKrzysztof Grobelny                 }
240d1bde9e5SKrzysztof Grobelny                 });
2417e860f15SJohn Edward Broadbent         }
2427e860f15SJohn Edward Broadbent 
2437e860f15SJohn Edward Broadbent         // this is done after we know the json array will no longer
2447e860f15SJohn Edward Broadbent         // be resized, as json::array uses vector underneath and we
2457e860f15SJohn Edward Broadbent         // need references to its members that won't change
2467e860f15SJohn Edward Broadbent         size_t count = 0;
247dfababfcSNan Zhou         // Pointer based on |asyncResp->res.jsonValue|
248dfababfcSNan Zhou         nlohmann::json::json_pointer rootPtr =
249dfababfcSNan Zhou             "/StorageControllers"_json_pointer;
2507e860f15SJohn Edward Broadbent         for (const auto& [path, interfaceDict] : subtree)
2517e860f15SJohn Edward Broadbent         {
2527e860f15SJohn Edward Broadbent             auto subHealth = std::make_shared<HealthPopulate>(
253dfababfcSNan Zhou                 asyncResp, rootPtr / count / "Status");
2547e860f15SJohn Edward Broadbent             subHealth->inventory.emplace_back(path);
2557e860f15SJohn Edward Broadbent             health->inventory.emplace_back(path);
2567e860f15SJohn Edward Broadbent             health->children.emplace_back(subHealth);
2577e860f15SJohn Edward Broadbent             count++;
2587e860f15SJohn Edward Broadbent         }
259e99073f5SGeorge Liu         });
260a85afbe1SWilly Tu }
261a85afbe1SWilly Tu 
262a85afbe1SWilly Tu inline void requestRoutesStorage(App& app)
263a85afbe1SWilly Tu {
264a85afbe1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
265a85afbe1SWilly Tu         .privileges(redfish::privileges::getStorage)
266a85afbe1SWilly Tu         .methods(boost::beast::http::verb::get)(
267a85afbe1SWilly Tu             [&app](const crow::Request& req,
268a85afbe1SWilly Tu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2693ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
270a85afbe1SWilly Tu         {
271a85afbe1SWilly Tu             return;
272a85afbe1SWilly Tu         }
273a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
274a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
275a85afbe1SWilly Tu             "/redfish/v1/Systems/system/Storage/1";
276a85afbe1SWilly Tu         asyncResp->res.jsonValue["Name"] = "Storage";
277a85afbe1SWilly Tu         asyncResp->res.jsonValue["Id"] = "1";
278a85afbe1SWilly Tu         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
279a85afbe1SWilly Tu 
280a85afbe1SWilly Tu         auto health = std::make_shared<HealthPopulate>(asyncResp);
281a85afbe1SWilly Tu         health->populate();
282a85afbe1SWilly Tu 
283a85afbe1SWilly Tu         getDrives(asyncResp, health);
284a85afbe1SWilly Tu         getStorageControllers(asyncResp, health);
2857e860f15SJohn Edward Broadbent         });
2867e860f15SJohn Edward Broadbent }
2877e860f15SJohn Edward Broadbent 
28803913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28903913171SWilly Tu                           const std::string& connectionName,
29003913171SWilly Tu                           const std::string& path)
29103913171SWilly Tu {
292d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
293d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, path,
294d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Asset",
2955e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
296168e20c1SEd Tanous                     const std::vector<
297168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
29803913171SWilly Tu                         propertiesList) {
29903913171SWilly Tu         if (ec)
30003913171SWilly Tu         {
30103913171SWilly Tu             // this interface isn't necessary
30203913171SWilly Tu             return;
30303913171SWilly Tu         }
304d1bde9e5SKrzysztof Grobelny 
305d1bde9e5SKrzysztof Grobelny         const std::string* partNumber = nullptr;
306d1bde9e5SKrzysztof Grobelny         const std::string* serialNumber = nullptr;
307d1bde9e5SKrzysztof Grobelny         const std::string* manufacturer = nullptr;
308d1bde9e5SKrzysztof Grobelny         const std::string* model = nullptr;
309d1bde9e5SKrzysztof Grobelny 
310d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
311d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
312d1bde9e5SKrzysztof Grobelny             partNumber, "SerialNumber", serialNumber, "Manufacturer",
313d1bde9e5SKrzysztof Grobelny             manufacturer, "Model", model);
314d1bde9e5SKrzysztof Grobelny 
315d1bde9e5SKrzysztof Grobelny         if (!success)
31603913171SWilly Tu         {
31703913171SWilly Tu             messages::internalError(asyncResp->res);
31803913171SWilly Tu             return;
31903913171SWilly Tu         }
320d1bde9e5SKrzysztof Grobelny 
321d1bde9e5SKrzysztof Grobelny         if (partNumber != nullptr)
322d1bde9e5SKrzysztof Grobelny         {
323d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["PartNumber"] = *partNumber;
32403913171SWilly Tu         }
325d1bde9e5SKrzysztof Grobelny 
326d1bde9e5SKrzysztof Grobelny         if (serialNumber != nullptr)
327d1bde9e5SKrzysztof Grobelny         {
328d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
32903913171SWilly Tu         }
330d1bde9e5SKrzysztof Grobelny 
331d1bde9e5SKrzysztof Grobelny         if (manufacturer != nullptr)
332d1bde9e5SKrzysztof Grobelny         {
333d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
334d1bde9e5SKrzysztof Grobelny         }
335d1bde9e5SKrzysztof Grobelny 
336d1bde9e5SKrzysztof Grobelny         if (model != nullptr)
337d1bde9e5SKrzysztof Grobelny         {
338d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Model"] = *model;
339d1bde9e5SKrzysztof Grobelny         }
340d1bde9e5SKrzysztof Grobelny         });
34103913171SWilly Tu }
34203913171SWilly Tu 
34303913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34403913171SWilly Tu                             const std::string& connectionName,
34503913171SWilly Tu                             const std::string& path)
34603913171SWilly Tu {
3471e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3481e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3491e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
3505e7e2dc5SEd Tanous         [asyncResp, path](const boost::system::error_code& ec,
351cef57e85SWilly Tu                           const bool isPresent) {
35203913171SWilly Tu         // this interface isn't necessary, only check it if
35303913171SWilly Tu         // we get a good return
35403913171SWilly Tu         if (ec)
35503913171SWilly Tu         {
35603913171SWilly Tu             return;
35703913171SWilly Tu         }
35803913171SWilly Tu 
359cef57e85SWilly Tu         if (!isPresent)
36003913171SWilly Tu         {
361cef57e85SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Absent";
36203913171SWilly Tu         }
3631e1e598dSJonathan Doman         });
36403913171SWilly Tu }
36503913171SWilly Tu 
36603913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36703913171SWilly Tu                           const std::string& connectionName,
36803913171SWilly Tu                           const std::string& path)
36903913171SWilly Tu {
3701e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3711e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3721e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3735e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec, const bool updating) {
37403913171SWilly Tu         // this interface isn't necessary, only check it
37503913171SWilly Tu         // if we get a good return
37603913171SWilly Tu         if (ec)
37703913171SWilly Tu         {
37803913171SWilly Tu             return;
37903913171SWilly Tu         }
38003913171SWilly Tu 
38103913171SWilly Tu         // updating and disabled in the backend shouldn't be
38203913171SWilly Tu         // able to be set at the same time, so we don't need
38303913171SWilly Tu         // to check for the race condition of these two
38403913171SWilly Tu         // calls
3851e1e598dSJonathan Doman         if (updating)
38603913171SWilly Tu         {
38703913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Updating";
38803913171SWilly Tu         }
3891e1e598dSJonathan Doman         });
39003913171SWilly Tu }
39103913171SWilly Tu 
392*dde9bc12SGeorge Liu inline std::optional<drive::MediaType> convertDriveType(std::string_view type)
39319b8e9a0SWilly Tu {
39419b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
39519b8e9a0SWilly Tu     {
396*dde9bc12SGeorge Liu         return drive::MediaType::HDD;
39719b8e9a0SWilly Tu     }
39819b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
39919b8e9a0SWilly Tu     {
400*dde9bc12SGeorge Liu         return drive::MediaType::SSD;
40119b8e9a0SWilly Tu     }
402*dde9bc12SGeorge Liu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.Unknown")
403*dde9bc12SGeorge Liu     {
40419b8e9a0SWilly Tu         return std::nullopt;
40519b8e9a0SWilly Tu     }
40619b8e9a0SWilly Tu 
407*dde9bc12SGeorge Liu     return drive::MediaType::Invalid;
408*dde9bc12SGeorge Liu }
409*dde9bc12SGeorge Liu 
410*dde9bc12SGeorge Liu inline std::optional<protocol::Protocol>
411*dde9bc12SGeorge Liu     convertDriveProtocol(std::string_view proto)
41219b8e9a0SWilly Tu {
41319b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
41419b8e9a0SWilly Tu     {
415*dde9bc12SGeorge Liu         return protocol::Protocol::SAS;
41619b8e9a0SWilly Tu     }
41719b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
41819b8e9a0SWilly Tu     {
419*dde9bc12SGeorge Liu         return protocol::Protocol::SATA;
42019b8e9a0SWilly Tu     }
42119b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
42219b8e9a0SWilly Tu     {
423*dde9bc12SGeorge Liu         return protocol::Protocol::NVMe;
42419b8e9a0SWilly Tu     }
42519b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
42619b8e9a0SWilly Tu     {
427*dde9bc12SGeorge Liu         return protocol::Protocol::FC;
428*dde9bc12SGeorge Liu     }
429*dde9bc12SGeorge Liu     if (proto ==
430*dde9bc12SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.Unknown")
431*dde9bc12SGeorge Liu     {
432*dde9bc12SGeorge Liu         return std::nullopt;
43319b8e9a0SWilly Tu     }
43419b8e9a0SWilly Tu 
435*dde9bc12SGeorge Liu     return protocol::Protocol::Invalid;
43619b8e9a0SWilly Tu }
43719b8e9a0SWilly Tu 
43819b8e9a0SWilly Tu inline void
43919b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
44019b8e9a0SWilly Tu                            const std::string& connectionName,
44119b8e9a0SWilly Tu                            const std::string& path)
44219b8e9a0SWilly Tu {
44319b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
44419b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
44519b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
4465e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
44719b8e9a0SWilly Tu                     const std::vector<
44819b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
44919b8e9a0SWilly Tu                         propertiesList) {
45019b8e9a0SWilly Tu         if (ec)
45119b8e9a0SWilly Tu         {
45219b8e9a0SWilly Tu             // this interface isn't required
45319b8e9a0SWilly Tu             return;
45419b8e9a0SWilly Tu         }
455e5029d88SJohn Edward Broadbent         const std::string* encryptionStatus = nullptr;
456e5029d88SJohn Edward Broadbent         const bool* isLocked = nullptr;
45719b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
45819b8e9a0SWilly Tu                  property : propertiesList)
45919b8e9a0SWilly Tu         {
46019b8e9a0SWilly Tu             const std::string& propertyName = property.first;
46119b8e9a0SWilly Tu             if (propertyName == "Type")
46219b8e9a0SWilly Tu             {
46319b8e9a0SWilly Tu                 const std::string* value =
46419b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
46519b8e9a0SWilly Tu                 if (value == nullptr)
46619b8e9a0SWilly Tu                 {
46719b8e9a0SWilly Tu                     // illegal property
46819b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Type";
46919b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
47019b8e9a0SWilly Tu                     return;
47119b8e9a0SWilly Tu                 }
47219b8e9a0SWilly Tu 
473*dde9bc12SGeorge Liu                 std::optional<drive::MediaType> mediaType =
474*dde9bc12SGeorge Liu                     convertDriveType(*value);
47519b8e9a0SWilly Tu                 if (!mediaType)
47619b8e9a0SWilly Tu                 {
477*dde9bc12SGeorge Liu                     BMCWEB_LOG_WARNING << "UnknownDriveType Interface: "
47819b8e9a0SWilly Tu                                        << *value;
479*dde9bc12SGeorge Liu                     continue;
480*dde9bc12SGeorge Liu                 }
481*dde9bc12SGeorge Liu                 if (*mediaType == drive::MediaType::Invalid)
482*dde9bc12SGeorge Liu                 {
48319b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
48419b8e9a0SWilly Tu                     return;
48519b8e9a0SWilly Tu                 }
48619b8e9a0SWilly Tu 
48719b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
48819b8e9a0SWilly Tu             }
48919b8e9a0SWilly Tu             else if (propertyName == "Capacity")
49019b8e9a0SWilly Tu             {
49119b8e9a0SWilly Tu                 const uint64_t* capacity =
49219b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
49319b8e9a0SWilly Tu                 if (capacity == nullptr)
49419b8e9a0SWilly Tu                 {
49519b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Capacity";
49619b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
49719b8e9a0SWilly Tu                     return;
49819b8e9a0SWilly Tu                 }
49919b8e9a0SWilly Tu                 if (*capacity == 0)
50019b8e9a0SWilly Tu                 {
50119b8e9a0SWilly Tu                     // drive capacity not known
50219b8e9a0SWilly Tu                     continue;
50319b8e9a0SWilly Tu                 }
50419b8e9a0SWilly Tu 
50519b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
50619b8e9a0SWilly Tu             }
50719b8e9a0SWilly Tu             else if (propertyName == "Protocol")
50819b8e9a0SWilly Tu             {
50919b8e9a0SWilly Tu                 const std::string* value =
51019b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
51119b8e9a0SWilly Tu                 if (value == nullptr)
51219b8e9a0SWilly Tu                 {
51319b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Protocol";
51419b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
51519b8e9a0SWilly Tu                     return;
51619b8e9a0SWilly Tu                 }
51719b8e9a0SWilly Tu 
518*dde9bc12SGeorge Liu                 std::optional<protocol::Protocol> proto =
519*dde9bc12SGeorge Liu                     convertDriveProtocol(*value);
52019b8e9a0SWilly Tu                 if (!proto)
52119b8e9a0SWilly Tu                 {
522*dde9bc12SGeorge Liu                     BMCWEB_LOG_WARNING << "Unknown DrivePrototype Interface: "
52319b8e9a0SWilly Tu                                        << *value;
524*dde9bc12SGeorge Liu                     continue;
525*dde9bc12SGeorge Liu                 }
526*dde9bc12SGeorge Liu                 if (*proto == protocol::Protocol::Invalid)
527*dde9bc12SGeorge Liu                 {
52819b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
52919b8e9a0SWilly Tu                     return;
53019b8e9a0SWilly Tu                 }
53119b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
53219b8e9a0SWilly Tu             }
5333fe4d5ccSJohn Edward Broadbent             else if (propertyName == "PredictedMediaLifeLeftPercent")
5343fe4d5ccSJohn Edward Broadbent             {
5353fe4d5ccSJohn Edward Broadbent                 const uint8_t* lifeLeft =
5363fe4d5ccSJohn Edward Broadbent                     std::get_if<uint8_t>(&property.second);
5373fe4d5ccSJohn Edward Broadbent                 if (lifeLeft == nullptr)
5383fe4d5ccSJohn Edward Broadbent                 {
5393fe4d5ccSJohn Edward Broadbent                     BMCWEB_LOG_ERROR
5403fe4d5ccSJohn Edward Broadbent                         << "Illegal property: PredictedMediaLifeLeftPercent";
5413fe4d5ccSJohn Edward Broadbent                     messages::internalError(asyncResp->res);
5423fe4d5ccSJohn Edward Broadbent                     return;
5433fe4d5ccSJohn Edward Broadbent                 }
5443fe4d5ccSJohn Edward Broadbent                 // 255 means reading the value is not supported
5453fe4d5ccSJohn Edward Broadbent                 if (*lifeLeft != 255)
5463fe4d5ccSJohn Edward Broadbent                 {
5473fe4d5ccSJohn Edward Broadbent                     asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
5483fe4d5ccSJohn Edward Broadbent                         *lifeLeft;
5493fe4d5ccSJohn Edward Broadbent                 }
5503fe4d5ccSJohn Edward Broadbent             }
551e5029d88SJohn Edward Broadbent             else if (propertyName == "EncryptionStatus")
552e5029d88SJohn Edward Broadbent             {
553e5029d88SJohn Edward Broadbent                 encryptionStatus = std::get_if<std::string>(&property.second);
554e5029d88SJohn Edward Broadbent                 if (encryptionStatus == nullptr)
555e5029d88SJohn Edward Broadbent                 {
556e5029d88SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Illegal property: EncryptionStatus";
557e5029d88SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
558e5029d88SJohn Edward Broadbent                     return;
55919b8e9a0SWilly Tu                 }
560e5029d88SJohn Edward Broadbent             }
561e5029d88SJohn Edward Broadbent             else if (propertyName == "Locked")
562e5029d88SJohn Edward Broadbent             {
563e5029d88SJohn Edward Broadbent                 isLocked = std::get_if<bool>(&property.second);
564e5029d88SJohn Edward Broadbent                 if (isLocked == nullptr)
565e5029d88SJohn Edward Broadbent                 {
566e5029d88SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Illegal property: Locked";
567e5029d88SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
568e5029d88SJohn Edward Broadbent                     return;
569e5029d88SJohn Edward Broadbent                 }
570e5029d88SJohn Edward Broadbent             }
571e5029d88SJohn Edward Broadbent         }
572e5029d88SJohn Edward Broadbent 
573e5029d88SJohn Edward Broadbent         if (encryptionStatus == nullptr || isLocked == nullptr ||
574e5029d88SJohn Edward Broadbent             *encryptionStatus ==
575e5029d88SJohn Edward Broadbent                 "xyz.openbmc_project.Drive.DriveEncryptionState.Unknown")
576e5029d88SJohn Edward Broadbent         {
577e5029d88SJohn Edward Broadbent             return;
578e5029d88SJohn Edward Broadbent         }
579e5029d88SJohn Edward Broadbent         if (*encryptionStatus !=
580e5029d88SJohn Edward Broadbent             "xyz.openbmc_project.Drive.DriveEncryptionState.Encrypted")
581e5029d88SJohn Edward Broadbent         {
582e5029d88SJohn Edward Broadbent             //"The drive is not currently encrypted."
583e5029d88SJohn Edward Broadbent             asyncResp->res.jsonValue["EncryptionStatus"] =
584e5029d88SJohn Edward Broadbent                 drive::EncryptionStatus::Unencrypted;
585e5029d88SJohn Edward Broadbent             return;
586e5029d88SJohn Edward Broadbent         }
587e5029d88SJohn Edward Broadbent         if (*isLocked)
588e5029d88SJohn Edward Broadbent         {
589e5029d88SJohn Edward Broadbent             //"The drive is currently encrypted and the data is not
590e5029d88SJohn Edward Broadbent             // accessible to the user."
591e5029d88SJohn Edward Broadbent             asyncResp->res.jsonValue["EncryptionStatus"] =
592e5029d88SJohn Edward Broadbent                 drive::EncryptionStatus::Locked;
593e5029d88SJohn Edward Broadbent             return;
594e5029d88SJohn Edward Broadbent         }
595e5029d88SJohn Edward Broadbent         // if not locked
596e5029d88SJohn Edward Broadbent         // "The drive is currently encrypted but the data is accessible
597e5029d88SJohn Edward Broadbent         // to the user in unencrypted form."
598e5029d88SJohn Edward Broadbent         asyncResp->res.jsonValue["EncryptionStatus"] =
599e5029d88SJohn Edward Broadbent             drive::EncryptionStatus::Unlocked;
60019b8e9a0SWilly Tu         });
60119b8e9a0SWilly Tu }
60219b8e9a0SWilly Tu 
603b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
604b53dcd9dSNan Zhou                             const std::string& connectionName,
605b53dcd9dSNan Zhou                             const std::string& path,
606e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
607e56ed6b9SJohn Edward Broadbent {
608e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
609e56ed6b9SJohn Edward Broadbent     {
610e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
611e56ed6b9SJohn Edward Broadbent         {
612e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
613e56ed6b9SJohn Edward Broadbent         }
614e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
615e56ed6b9SJohn Edward Broadbent         {
616e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
617e56ed6b9SJohn Edward Broadbent         }
618e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
619e56ed6b9SJohn Edward Broadbent         {
620e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
621e56ed6b9SJohn Edward Broadbent         }
622e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
623e56ed6b9SJohn Edward Broadbent         {
624e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
625e56ed6b9SJohn Edward Broadbent         }
626e56ed6b9SJohn Edward Broadbent     }
627e56ed6b9SJohn Edward Broadbent }
628e56ed6b9SJohn Edward Broadbent 
6297e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
6307e860f15SJohn Edward Broadbent {
63122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
632ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
633002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
634002d39b4SEd Tanous             [&app](const crow::Request& req,
63545ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
63622d268cbSEd Tanous                    const std::string& systemName, const std::string& driveId) {
6373ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
63845ca1b86SEd Tanous         {
63945ca1b86SEd Tanous             return;
64045ca1b86SEd Tanous         }
64122d268cbSEd Tanous         if (systemName != "system")
64222d268cbSEd Tanous         {
64322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
64422d268cbSEd Tanous                                        systemName);
64522d268cbSEd Tanous             return;
64622d268cbSEd Tanous         }
64722d268cbSEd Tanous 
648e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
649e99073f5SGeorge Liu             "xyz.openbmc_project.Inventory.Item.Drive"};
650e99073f5SGeorge Liu         dbus::utility::getSubTree(
651e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, interfaces,
652002d39b4SEd Tanous             [asyncResp,
653e99073f5SGeorge Liu              driveId](const boost::system::error_code& ec,
654b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
6557e860f15SJohn Edward Broadbent             if (ec)
6567e860f15SJohn Edward Broadbent             {
6577e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Drive mapper call error";
6587e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
6597e860f15SJohn Edward Broadbent                 return;
6607e860f15SJohn Edward Broadbent             }
6617e860f15SJohn Edward Broadbent 
66203913171SWilly Tu             auto drive = std::find_if(
6637e860f15SJohn Edward Broadbent                 subtree.begin(), subtree.end(),
664002d39b4SEd Tanous                 [&driveId](
6658cb65f8aSNan Zhou                     const std::pair<std::string,
6668cb65f8aSNan Zhou                                     dbus::utility::MapperServiceMap>& object) {
66703913171SWilly Tu                 return sdbusplus::message::object_path(object.first)
66803913171SWilly Tu                            .filename() == driveId;
6697e860f15SJohn Edward Broadbent                 });
6707e860f15SJohn Edward Broadbent 
67103913171SWilly Tu             if (drive == subtree.end())
6727e860f15SJohn Edward Broadbent             {
673002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
6747e860f15SJohn Edward Broadbent                 return;
6757e860f15SJohn Edward Broadbent             }
6767e860f15SJohn Edward Broadbent 
67703913171SWilly Tu             const std::string& path = drive->first;
6788cb65f8aSNan Zhou             const dbus::utility::MapperServiceMap& connectionNames =
6798cb65f8aSNan Zhou                 drive->second;
6807e860f15SJohn Edward Broadbent 
681002d39b4SEd Tanous             asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
682ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
683ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/{}", driveId);
6847e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = driveId;
6857e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = driveId;
6867e860f15SJohn Edward Broadbent 
6877e860f15SJohn Edward Broadbent             if (connectionNames.size() != 1)
6887e860f15SJohn Edward Broadbent             {
689002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
69003913171SWilly Tu                                  << ", not equal to 1";
6917e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
6927e860f15SJohn Edward Broadbent                 return;
6937e860f15SJohn Edward Broadbent             }
6947e860f15SJohn Edward Broadbent 
6957e860f15SJohn Edward Broadbent             getMainChassisId(
696ef4c65b7SEd Tanous                 asyncResp,
697ef4c65b7SEd Tanous                 [](const std::string& chassisId,
6987e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
699002d39b4SEd Tanous                 aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
700ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
7017e860f15SJohn Edward Broadbent                 });
7027e860f15SJohn Edward Broadbent 
703a25aeccfSNikhil Potade             // default it to Enabled
704a25aeccfSNikhil Potade             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
705a25aeccfSNikhil Potade 
7062ad9c2f6SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
707e284a7c1SJames Feist             health->inventory.emplace_back(path);
7082ad9c2f6SJames Feist             health->populate();
7092ad9c2f6SJames Feist 
710e56ed6b9SJohn Edward Broadbent             addAllDriveInfo(asyncResp, connectionNames[0].first, path,
711e56ed6b9SJohn Edward Broadbent                             connectionNames[0].second);
712e99073f5SGeorge Liu             });
7137e860f15SJohn Edward Broadbent         });
714a25aeccfSNikhil Potade }
71592903bd4SJohn Edward Broadbent 
71692903bd4SJohn Edward Broadbent /**
71792903bd4SJohn Edward Broadbent  * Chassis drives, this URL will show all the DriveCollection
71892903bd4SJohn Edward Broadbent  * information
71992903bd4SJohn Edward Broadbent  */
720b53dcd9dSNan Zhou inline void chassisDriveCollectionGet(
72192903bd4SJohn Edward Broadbent     crow::App& app, const crow::Request& req,
72292903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
72392903bd4SJohn Edward Broadbent     const std::string& chassisId)
72492903bd4SJohn Edward Broadbent {
7253ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
72692903bd4SJohn Edward Broadbent     {
72792903bd4SJohn Edward Broadbent         return;
72892903bd4SJohn Edward Broadbent     }
72992903bd4SJohn Edward Broadbent 
73092903bd4SJohn Edward Broadbent     // mapper call lambda
731e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
732e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Board",
733e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Chassis"};
734e99073f5SGeorge Liu     dbus::utility::getSubTree(
735e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
73692903bd4SJohn Edward Broadbent         [asyncResp,
737e99073f5SGeorge Liu          chassisId](const boost::system::error_code& ec,
73892903bd4SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
73992903bd4SJohn Edward Broadbent         if (ec)
74092903bd4SJohn Edward Broadbent         {
74192903bd4SJohn Edward Broadbent             if (ec == boost::system::errc::host_unreachable)
74292903bd4SJohn Edward Broadbent             {
74392903bd4SJohn Edward Broadbent                 messages::resourceNotFound(asyncResp->res, "Chassis",
74492903bd4SJohn Edward Broadbent                                            chassisId);
74592903bd4SJohn Edward Broadbent                 return;
74692903bd4SJohn Edward Broadbent             }
74792903bd4SJohn Edward Broadbent             messages::internalError(asyncResp->res);
74892903bd4SJohn Edward Broadbent             return;
74992903bd4SJohn Edward Broadbent         }
75092903bd4SJohn Edward Broadbent 
75192903bd4SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
7528cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
75392903bd4SJohn Edward Broadbent         {
75492903bd4SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
75592903bd4SJohn Edward Broadbent             if (objPath.filename() != chassisId)
75692903bd4SJohn Edward Broadbent             {
75792903bd4SJohn Edward Broadbent                 continue;
75892903bd4SJohn Edward Broadbent             }
75992903bd4SJohn Edward Broadbent 
76092903bd4SJohn Edward Broadbent             if (connectionNames.empty())
76192903bd4SJohn Edward Broadbent             {
76292903bd4SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
76392903bd4SJohn Edward Broadbent                 continue;
76492903bd4SJohn Edward Broadbent             }
76592903bd4SJohn Edward Broadbent 
76692903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.type"] =
76792903bd4SJohn Edward Broadbent                 "#DriveCollection.DriveCollection";
76892903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
769ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}/Drives", chassisId);
77092903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "Drive Collection";
77192903bd4SJohn Edward Broadbent 
77292903bd4SJohn Edward Broadbent             // Association lambda
7736c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
7746c3e9451SGeorge Liu                 path + "/drive",
7756c3e9451SGeorge Liu                 [asyncResp,
7766c3e9451SGeorge Liu                  chassisId](const boost::system::error_code& ec3,
7776c3e9451SGeorge Liu                             const dbus::utility::MapperEndPoints& resp) {
77892903bd4SJohn Edward Broadbent                 if (ec3)
77992903bd4SJohn Edward Broadbent                 {
78092903bd4SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
78192903bd4SJohn Edward Broadbent                 }
78292903bd4SJohn Edward Broadbent                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
78392903bd4SJohn Edward Broadbent                 // important if array is empty
78492903bd4SJohn Edward Broadbent                 members = nlohmann::json::array();
78592903bd4SJohn Edward Broadbent 
78692903bd4SJohn Edward Broadbent                 std::vector<std::string> leafNames;
78792903bd4SJohn Edward Broadbent                 for (const auto& drive : resp)
78892903bd4SJohn Edward Broadbent                 {
7898a592810SEd Tanous                     sdbusplus::message::object_path drivePath(drive);
7908a592810SEd Tanous                     leafNames.push_back(drivePath.filename());
79192903bd4SJohn Edward Broadbent                 }
79292903bd4SJohn Edward Broadbent 
79392903bd4SJohn Edward Broadbent                 std::sort(leafNames.begin(), leafNames.end(),
79492903bd4SJohn Edward Broadbent                           AlphanumLess<std::string>());
79592903bd4SJohn Edward Broadbent 
79692903bd4SJohn Edward Broadbent                 for (const auto& leafName : leafNames)
79792903bd4SJohn Edward Broadbent                 {
79892903bd4SJohn Edward Broadbent                     nlohmann::json::object_t member;
799ef4c65b7SEd Tanous                     member["@odata.id"] =
800ef4c65b7SEd Tanous                         boost::urls::format("/redfish/v1/Chassis/{}/Drives/{}",
801ef4c65b7SEd Tanous                                             chassisId, leafName);
802b2ba3072SPatrick Williams                     members.emplace_back(std::move(member));
80392903bd4SJohn Edward Broadbent                     // navigation links will be registered in next patch set
80492903bd4SJohn Edward Broadbent                 }
80592903bd4SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
80692903bd4SJohn Edward Broadbent                 }); // end association lambda
80792903bd4SJohn Edward Broadbent 
80892903bd4SJohn Edward Broadbent         }           // end Iterate over all retrieved ObjectPaths
809e99073f5SGeorge Liu         });
81092903bd4SJohn Edward Broadbent }
81192903bd4SJohn Edward Broadbent 
81292903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
81392903bd4SJohn Edward Broadbent {
81492903bd4SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
81592903bd4SJohn Edward Broadbent         .privileges(redfish::privileges::getDriveCollection)
81692903bd4SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
81792903bd4SJohn Edward Broadbent             std::bind_front(chassisDriveCollectionGet, std::ref(app)));
81892903bd4SJohn Edward Broadbent }
81992903bd4SJohn Edward Broadbent 
820b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
821b53dcd9dSNan Zhou                        const std::string& chassisId,
822b53dcd9dSNan Zhou                        const std::string& driveName,
8235e7e2dc5SEd Tanous                        const boost::system::error_code& ec,
824e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
825e56ed6b9SJohn Edward Broadbent {
826e56ed6b9SJohn Edward Broadbent     if (ec)
827e56ed6b9SJohn Edward Broadbent     {
828e56ed6b9SJohn Edward Broadbent         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
829e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
830e56ed6b9SJohn Edward Broadbent         return;
831e56ed6b9SJohn Edward Broadbent     }
832e56ed6b9SJohn Edward Broadbent 
833e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
8348cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
835e56ed6b9SJohn Edward Broadbent     {
836e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
837e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
838e56ed6b9SJohn Edward Broadbent         {
839e56ed6b9SJohn Edward Broadbent             continue;
840e56ed6b9SJohn Edward Broadbent         }
841e56ed6b9SJohn Edward Broadbent 
842e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
843e56ed6b9SJohn Edward Broadbent         {
844e56ed6b9SJohn Edward Broadbent             BMCWEB_LOG_ERROR << "Got 0 Connection names";
845e56ed6b9SJohn Edward Broadbent             continue;
846e56ed6b9SJohn Edward Broadbent         }
847e56ed6b9SJohn Edward Broadbent 
848ef4c65b7SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
849ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/Drives/{}", chassisId, driveName);
850e56ed6b9SJohn Edward Broadbent 
851e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
852a0cb40cbSJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = driveName;
853e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
854e56ed6b9SJohn Edward Broadbent         // default it to Enabled
855e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
856e56ed6b9SJohn Edward Broadbent 
857e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
858e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
859ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
860e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
861e56ed6b9SJohn Edward Broadbent 
862e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
863e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
864e56ed6b9SJohn Edward Broadbent     }
865e56ed6b9SJohn Edward Broadbent }
866e56ed6b9SJohn Edward Broadbent 
867b53dcd9dSNan Zhou inline void
868b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
869e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
870e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
871e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
872e56ed6b9SJohn Edward Broadbent {
873e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
874e56ed6b9SJohn Edward Broadbent     {
875e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
876e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
877e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
878e56ed6b9SJohn Edward Broadbent         {
879e56ed6b9SJohn Edward Broadbent             continue;
880e56ed6b9SJohn Edward Broadbent         }
881e56ed6b9SJohn Edward Broadbent         //  mapper call drive
882e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> driveInterface = {
883e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
884e99073f5SGeorge Liu         dbus::utility::getSubTree(
885e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, driveInterface,
886e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
887e99073f5SGeorge Liu                 const boost::system::error_code& ec,
888e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
889e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
890e99073f5SGeorge Liu             });
891e56ed6b9SJohn Edward Broadbent     }
892e56ed6b9SJohn Edward Broadbent }
893e56ed6b9SJohn Edward Broadbent 
894b53dcd9dSNan Zhou inline void
895b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
896e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
897e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
898e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
899e56ed6b9SJohn Edward Broadbent {
90003810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
901e56ed6b9SJohn Edward Broadbent     {
902e56ed6b9SJohn Edward Broadbent         return;
903e56ed6b9SJohn Edward Broadbent     }
904e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
905e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
906e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
907e56ed6b9SJohn Edward Broadbent 
908e56ed6b9SJohn Edward Broadbent     // mapper call chassis
909e99073f5SGeorge Liu     dbus::utility::getSubTree(
910e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
911e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
912e99073f5SGeorge Liu          driveName](const boost::system::error_code& ec,
913e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
914e56ed6b9SJohn Edward Broadbent         if (ec)
915e56ed6b9SJohn Edward Broadbent         {
916e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
917e56ed6b9SJohn Edward Broadbent             return;
918e56ed6b9SJohn Edward Broadbent         }
919e56ed6b9SJohn Edward Broadbent 
920e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
9218cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
922e56ed6b9SJohn Edward Broadbent         {
923e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
924e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
925e56ed6b9SJohn Edward Broadbent             {
926e56ed6b9SJohn Edward Broadbent                 continue;
927e56ed6b9SJohn Edward Broadbent             }
928e56ed6b9SJohn Edward Broadbent 
929e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
930e56ed6b9SJohn Edward Broadbent             {
931e56ed6b9SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
932e56ed6b9SJohn Edward Broadbent                 continue;
933e56ed6b9SJohn Edward Broadbent             }
934e56ed6b9SJohn Edward Broadbent 
9356c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
9366c3e9451SGeorge Liu                 path + "/drive",
937e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
9385e7e2dc5SEd Tanous                  driveName](const boost::system::error_code& ec3,
9396c3e9451SGeorge Liu                             const dbus::utility::MapperEndPoints& resp) {
940e56ed6b9SJohn Edward Broadbent                 if (ec3)
941e56ed6b9SJohn Edward Broadbent                 {
942e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
943e56ed6b9SJohn Edward Broadbent                 }
944e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
945e56ed6b9SJohn Edward Broadbent                 });
946e56ed6b9SJohn Edward Broadbent             break;
947e56ed6b9SJohn Edward Broadbent         }
948e99073f5SGeorge Liu         });
949e56ed6b9SJohn Edward Broadbent }
950e56ed6b9SJohn Edward Broadbent 
951e56ed6b9SJohn Edward Broadbent /**
952e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
953e56ed6b9SJohn Edward Broadbent  */
954e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
955e56ed6b9SJohn Edward Broadbent {
956e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
957e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
958e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
959e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
960e56ed6b9SJohn Edward Broadbent }
961e56ed6b9SJohn Edward Broadbent 
962a25aeccfSNikhil Potade } // namespace redfish
963