xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision e5029d880f8232d20e6dcac36728594f88ac0466)
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"
20*e5029d88SJohn Edward Broadbent #include "generated/enums/drive.hpp"
212ad9c2f6SJames Feist #include "health.hpp"
22a8e884fcSEd Tanous #include "human_sort.hpp"
23e284a7c1SJames Feist #include "openbmc_dbus_rest.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
25a8e884fcSEd Tanous #include "redfish_util.hpp"
263ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
273ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
282ad9c2f6SJames Feist 
29e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
30ef4c65b7SEd Tanous #include <boost/url/format.hpp>
311e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
32d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
33a25aeccfSNikhil Potade 
347a1dbc48SGeorge Liu #include <array>
357a1dbc48SGeorge Liu #include <string_view>
367a1dbc48SGeorge Liu 
37a25aeccfSNikhil Potade namespace redfish
38a25aeccfSNikhil Potade {
397e860f15SJohn Edward Broadbent inline void requestRoutesStorageCollection(App& app)
40a25aeccfSNikhil Potade {
4122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
42ed398213SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
437e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
4445ca1b86SEd Tanous             [&app](const crow::Request& req,
4522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4622d268cbSEd Tanous                    const std::string& systemName) {
473ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
4845ca1b86SEd Tanous         {
4945ca1b86SEd Tanous             return;
5045ca1b86SEd Tanous         }
5122d268cbSEd Tanous         if (systemName != "system")
5222d268cbSEd Tanous         {
5322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
5422d268cbSEd Tanous                                        systemName);
5522d268cbSEd Tanous             return;
5622d268cbSEd Tanous         }
5722d268cbSEd Tanous 
588d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
598d1b46d7Szhanghch05             "#StorageCollection.StorageCollection";
608d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
618d1b46d7Szhanghch05             "/redfish/v1/Systems/system/Storage";
628d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Storage Collection";
631476687dSEd Tanous         nlohmann::json::array_t members;
641476687dSEd Tanous         nlohmann::json::object_t member;
651476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
661476687dSEd Tanous         members.emplace_back(member);
671476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
688d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
697e860f15SJohn Edward Broadbent         });
70a25aeccfSNikhil Potade }
71a25aeccfSNikhil Potade 
72a85afbe1SWilly Tu inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
73a85afbe1SWilly Tu                       const std::shared_ptr<HealthPopulate>& health)
74a25aeccfSNikhil Potade {
757a1dbc48SGeorge Liu     const std::array<std::string_view, 1> interfaces = {
767a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Drive"};
777a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
787a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
79a85afbe1SWilly Tu         [asyncResp, health](
807a1dbc48SGeorge Liu             const boost::system::error_code& ec,
81a85afbe1SWilly Tu             const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
82a25aeccfSNikhil Potade         if (ec)
83a25aeccfSNikhil Potade         {
84a25aeccfSNikhil Potade             BMCWEB_LOG_ERROR << "Drive mapper call error";
85a25aeccfSNikhil Potade             messages::internalError(asyncResp->res);
86a25aeccfSNikhil Potade             return;
87a25aeccfSNikhil Potade         }
882ad9c2f6SJames Feist 
89a85afbe1SWilly Tu         nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
90a85afbe1SWilly Tu         driveArray = nlohmann::json::array();
91a85afbe1SWilly Tu         auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
92a85afbe1SWilly Tu         count = 0;
932ad9c2f6SJames Feist 
94a85afbe1SWilly Tu         health->inventory.insert(health->inventory.end(), driveList.begin(),
95a85afbe1SWilly Tu                                  driveList.end());
96a85afbe1SWilly Tu 
97a85afbe1SWilly Tu         for (const std::string& drive : driveList)
98a25aeccfSNikhil Potade         {
99a85afbe1SWilly Tu             sdbusplus::message::object_path object(drive);
100a85afbe1SWilly Tu             if (object.filename().empty())
101a25aeccfSNikhil Potade             {
102a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
103a85afbe1SWilly Tu                 return;
104a25aeccfSNikhil Potade             }
105a85afbe1SWilly Tu 
106a85afbe1SWilly Tu             nlohmann::json::object_t driveJson;
107ef4c65b7SEd Tanous             driveJson["@odata.id"] = boost::urls::format(
108ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/{}",
109eddfc437SWilly Tu                 object.filename());
110b2ba3072SPatrick Williams             driveArray.emplace_back(std::move(driveJson));
111a25aeccfSNikhil Potade         }
112a25aeccfSNikhil Potade 
113a85afbe1SWilly Tu         count = driveArray.size();
1147a1dbc48SGeorge Liu         });
115a85afbe1SWilly Tu }
116e284a7c1SJames Feist 
117a85afbe1SWilly Tu inline void
118a85afbe1SWilly Tu     getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
119a85afbe1SWilly Tu                           const std::shared_ptr<HealthPopulate>& health)
120a85afbe1SWilly Tu {
121e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
122e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.StorageController"};
123e99073f5SGeorge Liu     dbus::utility::getSubTree(
124e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
125002d39b4SEd Tanous         [asyncResp,
126e99073f5SGeorge Liu          health](const boost::system::error_code& ec,
127b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreeResponse& subtree) {
12826f6976fSEd Tanous         if (ec || subtree.empty())
129e284a7c1SJames Feist         {
130d819a420SJames Feist             // doesn't have to be there
131e284a7c1SJames Feist             return;
132e284a7c1SJames Feist         }
133e284a7c1SJames Feist 
134a85afbe1SWilly Tu         nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
135e284a7c1SJames Feist         root = nlohmann::json::array();
136e284a7c1SJames Feist         for (const auto& [path, interfaceDict] : subtree)
137e284a7c1SJames Feist         {
138a85afbe1SWilly Tu             sdbusplus::message::object_path object(path);
139a85afbe1SWilly Tu             std::string id = object.filename();
140a85afbe1SWilly Tu             if (id.empty())
141e284a7c1SJames Feist             {
142a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
143e284a7c1SJames Feist                 return;
144e284a7c1SJames Feist             }
145e284a7c1SJames Feist 
146e284a7c1SJames Feist             if (interfaceDict.size() != 1)
147e284a7c1SJames Feist             {
148a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
149e284a7c1SJames Feist                                  << ", greater than 1";
150e284a7c1SJames Feist                 messages::internalError(asyncResp->res);
151e284a7c1SJames Feist                 return;
152e284a7c1SJames Feist             }
153e284a7c1SJames Feist 
154002d39b4SEd Tanous             const std::string& connectionName = interfaceDict.front().first;
155e284a7c1SJames Feist 
156e284a7c1SJames Feist             size_t index = root.size();
157e284a7c1SJames Feist             nlohmann::json& storageController =
158e284a7c1SJames Feist                 root.emplace_back(nlohmann::json::object());
159e284a7c1SJames Feist 
160e284a7c1SJames Feist             storageController["@odata.type"] =
161e284a7c1SJames Feist                 "#Storage.v1_7_0.StorageController";
162ef4c65b7SEd Tanous             storageController["@odata.id"] = boost::urls::format(
163ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/Storage/1#{}",
164ef4c65b7SEd Tanous                 ("/StorageControllers"_json_pointer / index).to_string());
165e284a7c1SJames Feist             storageController["Name"] = id;
166e284a7c1SJames Feist             storageController["MemberId"] = id;
167e284a7c1SJames Feist             storageController["Status"]["State"] = "Enabled";
168e284a7c1SJames Feist 
1691e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
1701e1e598dSJonathan Doman                 *crow::connections::systemBus, connectionName, path,
1711e1e598dSJonathan Doman                 "xyz.openbmc_project.Inventory.Item", "Present",
1725e7e2dc5SEd Tanous                 [asyncResp, index](const boost::system::error_code& ec2,
173cef57e85SWilly Tu                                    bool isPresent) {
1747e860f15SJohn Edward Broadbent                 // this interface isn't necessary, only check it
1757e860f15SJohn Edward Broadbent                 // if we get a good return
17623a21a1cSEd Tanous                 if (ec2)
177e284a7c1SJames Feist                 {
178e284a7c1SJames Feist                     return;
179e284a7c1SJames Feist                 }
180cef57e85SWilly Tu                 if (!isPresent)
181e284a7c1SJames Feist                 {
182002d39b4SEd Tanous                     asyncResp->res.jsonValue["StorageControllers"][index]
183cef57e85SWilly Tu                                             ["Status"]["State"] = "Absent";
184e284a7c1SJames Feist                 }
1851e1e598dSJonathan Doman                 });
186e284a7c1SJames Feist 
187d1bde9e5SKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
188d1bde9e5SKrzysztof Grobelny                 *crow::connections::systemBus, connectionName, path,
189d1bde9e5SKrzysztof Grobelny                 "xyz.openbmc_project.Inventory.Decorator.Asset",
190a85afbe1SWilly Tu                 [asyncResp, index](
1915e7e2dc5SEd Tanous                     const boost::system::error_code& ec2,
192a85afbe1SWilly Tu                     const std::vector<
193a85afbe1SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
1947e860f15SJohn Edward Broadbent                         propertiesList) {
1957e860f15SJohn Edward Broadbent                 if (ec2)
1967e860f15SJohn Edward Broadbent                 {
1977e860f15SJohn Edward Broadbent                     // this interface isn't necessary
1987e860f15SJohn Edward Broadbent                     return;
1997e860f15SJohn Edward Broadbent                 }
200d1bde9e5SKrzysztof Grobelny 
201d1bde9e5SKrzysztof Grobelny                 const std::string* partNumber = nullptr;
202d1bde9e5SKrzysztof Grobelny                 const std::string* serialNumber = nullptr;
203d1bde9e5SKrzysztof Grobelny                 const std::string* manufacturer = nullptr;
204d1bde9e5SKrzysztof Grobelny                 const std::string* model = nullptr;
205d1bde9e5SKrzysztof Grobelny 
206d1bde9e5SKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
207d1bde9e5SKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), propertiesList,
208d1bde9e5SKrzysztof Grobelny                     "PartNumber", partNumber, "SerialNumber", serialNumber,
209d1bde9e5SKrzysztof Grobelny                     "Manufacturer", manufacturer, "Model", model);
210d1bde9e5SKrzysztof Grobelny 
211d1bde9e5SKrzysztof Grobelny                 if (!success)
2127e860f15SJohn Edward Broadbent                 {
213002d39b4SEd Tanous                     messages::internalError(asyncResp->res);
2147e860f15SJohn Edward Broadbent                     return;
2157e860f15SJohn Edward Broadbent                 }
216d1bde9e5SKrzysztof Grobelny 
217d1bde9e5SKrzysztof Grobelny                 nlohmann::json& controller =
218d1bde9e5SKrzysztof Grobelny                     asyncResp->res.jsonValue["StorageControllers"][index];
219d1bde9e5SKrzysztof Grobelny 
220d1bde9e5SKrzysztof Grobelny                 if (partNumber != nullptr)
221d1bde9e5SKrzysztof Grobelny                 {
222d1bde9e5SKrzysztof Grobelny                     controller["PartNumber"] = *partNumber;
2237e860f15SJohn Edward Broadbent                 }
224d1bde9e5SKrzysztof Grobelny 
225d1bde9e5SKrzysztof Grobelny                 if (serialNumber != nullptr)
226d1bde9e5SKrzysztof Grobelny                 {
227d1bde9e5SKrzysztof Grobelny                     controller["SerialNumber"] = *serialNumber;
2287e860f15SJohn Edward Broadbent                 }
229d1bde9e5SKrzysztof Grobelny 
230d1bde9e5SKrzysztof Grobelny                 if (manufacturer != nullptr)
231d1bde9e5SKrzysztof Grobelny                 {
232d1bde9e5SKrzysztof Grobelny                     controller["Manufacturer"] = *manufacturer;
233d1bde9e5SKrzysztof Grobelny                 }
234d1bde9e5SKrzysztof Grobelny 
235d1bde9e5SKrzysztof Grobelny                 if (model != nullptr)
236d1bde9e5SKrzysztof Grobelny                 {
237d1bde9e5SKrzysztof Grobelny                     controller["Model"] = *model;
238d1bde9e5SKrzysztof Grobelny                 }
239d1bde9e5SKrzysztof Grobelny                 });
2407e860f15SJohn Edward Broadbent         }
2417e860f15SJohn Edward Broadbent 
2427e860f15SJohn Edward Broadbent         // this is done after we know the json array will no longer
2437e860f15SJohn Edward Broadbent         // be resized, as json::array uses vector underneath and we
2447e860f15SJohn Edward Broadbent         // need references to its members that won't change
2457e860f15SJohn Edward Broadbent         size_t count = 0;
246dfababfcSNan Zhou         // Pointer based on |asyncResp->res.jsonValue|
247dfababfcSNan Zhou         nlohmann::json::json_pointer rootPtr =
248dfababfcSNan Zhou             "/StorageControllers"_json_pointer;
2497e860f15SJohn Edward Broadbent         for (const auto& [path, interfaceDict] : subtree)
2507e860f15SJohn Edward Broadbent         {
2517e860f15SJohn Edward Broadbent             auto subHealth = std::make_shared<HealthPopulate>(
252dfababfcSNan Zhou                 asyncResp, rootPtr / count / "Status");
2537e860f15SJohn Edward Broadbent             subHealth->inventory.emplace_back(path);
2547e860f15SJohn Edward Broadbent             health->inventory.emplace_back(path);
2557e860f15SJohn Edward Broadbent             health->children.emplace_back(subHealth);
2567e860f15SJohn Edward Broadbent             count++;
2577e860f15SJohn Edward Broadbent         }
258e99073f5SGeorge Liu         });
259a85afbe1SWilly Tu }
260a85afbe1SWilly Tu 
261a85afbe1SWilly Tu inline void requestRoutesStorage(App& app)
262a85afbe1SWilly Tu {
263a85afbe1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
264a85afbe1SWilly Tu         .privileges(redfish::privileges::getStorage)
265a85afbe1SWilly Tu         .methods(boost::beast::http::verb::get)(
266a85afbe1SWilly Tu             [&app](const crow::Request& req,
267a85afbe1SWilly Tu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2683ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
269a85afbe1SWilly Tu         {
270a85afbe1SWilly Tu             return;
271a85afbe1SWilly Tu         }
272a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
273a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
274a85afbe1SWilly Tu             "/redfish/v1/Systems/system/Storage/1";
275a85afbe1SWilly Tu         asyncResp->res.jsonValue["Name"] = "Storage";
276a85afbe1SWilly Tu         asyncResp->res.jsonValue["Id"] = "1";
277a85afbe1SWilly Tu         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
278a85afbe1SWilly Tu 
279a85afbe1SWilly Tu         auto health = std::make_shared<HealthPopulate>(asyncResp);
280a85afbe1SWilly Tu         health->populate();
281a85afbe1SWilly Tu 
282a85afbe1SWilly Tu         getDrives(asyncResp, health);
283a85afbe1SWilly Tu         getStorageControllers(asyncResp, health);
2847e860f15SJohn Edward Broadbent         });
2857e860f15SJohn Edward Broadbent }
2867e860f15SJohn Edward Broadbent 
28703913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28803913171SWilly Tu                           const std::string& connectionName,
28903913171SWilly Tu                           const std::string& path)
29003913171SWilly Tu {
291d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
292d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, path,
293d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Asset",
2945e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
295168e20c1SEd Tanous                     const std::vector<
296168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
29703913171SWilly Tu                         propertiesList) {
29803913171SWilly Tu         if (ec)
29903913171SWilly Tu         {
30003913171SWilly Tu             // this interface isn't necessary
30103913171SWilly Tu             return;
30203913171SWilly Tu         }
303d1bde9e5SKrzysztof Grobelny 
304d1bde9e5SKrzysztof Grobelny         const std::string* partNumber = nullptr;
305d1bde9e5SKrzysztof Grobelny         const std::string* serialNumber = nullptr;
306d1bde9e5SKrzysztof Grobelny         const std::string* manufacturer = nullptr;
307d1bde9e5SKrzysztof Grobelny         const std::string* model = nullptr;
308d1bde9e5SKrzysztof Grobelny 
309d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
310d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
311d1bde9e5SKrzysztof Grobelny             partNumber, "SerialNumber", serialNumber, "Manufacturer",
312d1bde9e5SKrzysztof Grobelny             manufacturer, "Model", model);
313d1bde9e5SKrzysztof Grobelny 
314d1bde9e5SKrzysztof Grobelny         if (!success)
31503913171SWilly Tu         {
31603913171SWilly Tu             messages::internalError(asyncResp->res);
31703913171SWilly Tu             return;
31803913171SWilly Tu         }
319d1bde9e5SKrzysztof Grobelny 
320d1bde9e5SKrzysztof Grobelny         if (partNumber != nullptr)
321d1bde9e5SKrzysztof Grobelny         {
322d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["PartNumber"] = *partNumber;
32303913171SWilly Tu         }
324d1bde9e5SKrzysztof Grobelny 
325d1bde9e5SKrzysztof Grobelny         if (serialNumber != nullptr)
326d1bde9e5SKrzysztof Grobelny         {
327d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
32803913171SWilly Tu         }
329d1bde9e5SKrzysztof Grobelny 
330d1bde9e5SKrzysztof Grobelny         if (manufacturer != nullptr)
331d1bde9e5SKrzysztof Grobelny         {
332d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
333d1bde9e5SKrzysztof Grobelny         }
334d1bde9e5SKrzysztof Grobelny 
335d1bde9e5SKrzysztof Grobelny         if (model != nullptr)
336d1bde9e5SKrzysztof Grobelny         {
337d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["Model"] = *model;
338d1bde9e5SKrzysztof Grobelny         }
339d1bde9e5SKrzysztof Grobelny         });
34003913171SWilly Tu }
34103913171SWilly Tu 
34203913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34303913171SWilly Tu                             const std::string& connectionName,
34403913171SWilly Tu                             const std::string& path)
34503913171SWilly Tu {
3461e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3471e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3481e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
3495e7e2dc5SEd Tanous         [asyncResp, path](const boost::system::error_code& ec,
350cef57e85SWilly Tu                           const bool isPresent) {
35103913171SWilly Tu         // this interface isn't necessary, only check it if
35203913171SWilly Tu         // we get a good return
35303913171SWilly Tu         if (ec)
35403913171SWilly Tu         {
35503913171SWilly Tu             return;
35603913171SWilly Tu         }
35703913171SWilly Tu 
358cef57e85SWilly Tu         if (!isPresent)
35903913171SWilly Tu         {
360cef57e85SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Absent";
36103913171SWilly Tu         }
3621e1e598dSJonathan Doman         });
36303913171SWilly Tu }
36403913171SWilly Tu 
36503913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36603913171SWilly Tu                           const std::string& connectionName,
36703913171SWilly Tu                           const std::string& path)
36803913171SWilly Tu {
3691e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3701e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3711e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3725e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec, const bool updating) {
37303913171SWilly Tu         // this interface isn't necessary, only check it
37403913171SWilly Tu         // if we get a good return
37503913171SWilly Tu         if (ec)
37603913171SWilly Tu         {
37703913171SWilly Tu             return;
37803913171SWilly Tu         }
37903913171SWilly Tu 
38003913171SWilly Tu         // updating and disabled in the backend shouldn't be
38103913171SWilly Tu         // able to be set at the same time, so we don't need
38203913171SWilly Tu         // to check for the race condition of these two
38303913171SWilly Tu         // calls
3841e1e598dSJonathan Doman         if (updating)
38503913171SWilly Tu         {
38603913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Updating";
38703913171SWilly Tu         }
3881e1e598dSJonathan Doman         });
38903913171SWilly Tu }
39003913171SWilly Tu 
39119b8e9a0SWilly Tu inline std::optional<std::string> convertDriveType(const std::string& type)
39219b8e9a0SWilly Tu {
39319b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
39419b8e9a0SWilly Tu     {
39519b8e9a0SWilly Tu         return "HDD";
39619b8e9a0SWilly Tu     }
39719b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
39819b8e9a0SWilly Tu     {
39919b8e9a0SWilly Tu         return "SSD";
40019b8e9a0SWilly Tu     }
40119b8e9a0SWilly Tu 
40219b8e9a0SWilly Tu     return std::nullopt;
40319b8e9a0SWilly Tu }
40419b8e9a0SWilly Tu 
40519b8e9a0SWilly Tu inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
40619b8e9a0SWilly Tu {
40719b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
40819b8e9a0SWilly Tu     {
40919b8e9a0SWilly Tu         return "SAS";
41019b8e9a0SWilly Tu     }
41119b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
41219b8e9a0SWilly Tu     {
41319b8e9a0SWilly Tu         return "SATA";
41419b8e9a0SWilly Tu     }
41519b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
41619b8e9a0SWilly Tu     {
41719b8e9a0SWilly Tu         return "NVMe";
41819b8e9a0SWilly Tu     }
41919b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
42019b8e9a0SWilly Tu     {
42119b8e9a0SWilly Tu         return "FC";
42219b8e9a0SWilly Tu     }
42319b8e9a0SWilly Tu 
42419b8e9a0SWilly Tu     return std::nullopt;
42519b8e9a0SWilly Tu }
42619b8e9a0SWilly Tu 
42719b8e9a0SWilly Tu inline void
42819b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
42919b8e9a0SWilly Tu                            const std::string& connectionName,
43019b8e9a0SWilly Tu                            const std::string& path)
43119b8e9a0SWilly Tu {
43219b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
43319b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
43419b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
4355e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
43619b8e9a0SWilly Tu                     const std::vector<
43719b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
43819b8e9a0SWilly Tu                         propertiesList) {
43919b8e9a0SWilly Tu         if (ec)
44019b8e9a0SWilly Tu         {
44119b8e9a0SWilly Tu             // this interface isn't required
44219b8e9a0SWilly Tu             return;
44319b8e9a0SWilly Tu         }
444*e5029d88SJohn Edward Broadbent         const std::string* encryptionStatus = nullptr;
445*e5029d88SJohn Edward Broadbent         const bool* isLocked = nullptr;
44619b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
44719b8e9a0SWilly Tu                  property : propertiesList)
44819b8e9a0SWilly Tu         {
44919b8e9a0SWilly Tu             const std::string& propertyName = property.first;
45019b8e9a0SWilly Tu             if (propertyName == "Type")
45119b8e9a0SWilly Tu             {
45219b8e9a0SWilly Tu                 const std::string* value =
45319b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
45419b8e9a0SWilly Tu                 if (value == nullptr)
45519b8e9a0SWilly Tu                 {
45619b8e9a0SWilly Tu                     // illegal property
45719b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Type";
45819b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
45919b8e9a0SWilly Tu                     return;
46019b8e9a0SWilly Tu                 }
46119b8e9a0SWilly Tu 
462002d39b4SEd Tanous                 std::optional<std::string> mediaType = convertDriveType(*value);
46319b8e9a0SWilly Tu                 if (!mediaType)
46419b8e9a0SWilly Tu                 {
46519b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
46619b8e9a0SWilly Tu                                      << *value;
46719b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
46819b8e9a0SWilly Tu                     return;
46919b8e9a0SWilly Tu                 }
47019b8e9a0SWilly Tu 
47119b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
47219b8e9a0SWilly Tu             }
47319b8e9a0SWilly Tu             else if (propertyName == "Capacity")
47419b8e9a0SWilly Tu             {
47519b8e9a0SWilly Tu                 const uint64_t* capacity =
47619b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
47719b8e9a0SWilly Tu                 if (capacity == nullptr)
47819b8e9a0SWilly Tu                 {
47919b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Capacity";
48019b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
48119b8e9a0SWilly Tu                     return;
48219b8e9a0SWilly Tu                 }
48319b8e9a0SWilly Tu                 if (*capacity == 0)
48419b8e9a0SWilly Tu                 {
48519b8e9a0SWilly Tu                     // drive capacity not known
48619b8e9a0SWilly Tu                     continue;
48719b8e9a0SWilly Tu                 }
48819b8e9a0SWilly Tu 
48919b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
49019b8e9a0SWilly Tu             }
49119b8e9a0SWilly Tu             else if (propertyName == "Protocol")
49219b8e9a0SWilly Tu             {
49319b8e9a0SWilly Tu                 const std::string* value =
49419b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
49519b8e9a0SWilly Tu                 if (value == nullptr)
49619b8e9a0SWilly Tu                 {
49719b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Protocol";
49819b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
49919b8e9a0SWilly Tu                     return;
50019b8e9a0SWilly Tu                 }
50119b8e9a0SWilly Tu 
502002d39b4SEd Tanous                 std::optional<std::string> proto = convertDriveProtocol(*value);
50319b8e9a0SWilly Tu                 if (!proto)
50419b8e9a0SWilly Tu                 {
505002d39b4SEd Tanous                     BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: "
50619b8e9a0SWilly Tu                                      << *value;
50719b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
50819b8e9a0SWilly Tu                     return;
50919b8e9a0SWilly Tu                 }
51019b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
51119b8e9a0SWilly Tu             }
5123fe4d5ccSJohn Edward Broadbent             else if (propertyName == "PredictedMediaLifeLeftPercent")
5133fe4d5ccSJohn Edward Broadbent             {
5143fe4d5ccSJohn Edward Broadbent                 const uint8_t* lifeLeft =
5153fe4d5ccSJohn Edward Broadbent                     std::get_if<uint8_t>(&property.second);
5163fe4d5ccSJohn Edward Broadbent                 if (lifeLeft == nullptr)
5173fe4d5ccSJohn Edward Broadbent                 {
5183fe4d5ccSJohn Edward Broadbent                     BMCWEB_LOG_ERROR
5193fe4d5ccSJohn Edward Broadbent                         << "Illegal property: PredictedMediaLifeLeftPercent";
5203fe4d5ccSJohn Edward Broadbent                     messages::internalError(asyncResp->res);
5213fe4d5ccSJohn Edward Broadbent                     return;
5223fe4d5ccSJohn Edward Broadbent                 }
5233fe4d5ccSJohn Edward Broadbent                 // 255 means reading the value is not supported
5243fe4d5ccSJohn Edward Broadbent                 if (*lifeLeft != 255)
5253fe4d5ccSJohn Edward Broadbent                 {
5263fe4d5ccSJohn Edward Broadbent                     asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
5273fe4d5ccSJohn Edward Broadbent                         *lifeLeft;
5283fe4d5ccSJohn Edward Broadbent                 }
5293fe4d5ccSJohn Edward Broadbent             }
530*e5029d88SJohn Edward Broadbent             else if (propertyName == "EncryptionStatus")
531*e5029d88SJohn Edward Broadbent             {
532*e5029d88SJohn Edward Broadbent                 encryptionStatus = std::get_if<std::string>(&property.second);
533*e5029d88SJohn Edward Broadbent                 if (encryptionStatus == nullptr)
534*e5029d88SJohn Edward Broadbent                 {
535*e5029d88SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Illegal property: EncryptionStatus";
536*e5029d88SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
537*e5029d88SJohn Edward Broadbent                     return;
53819b8e9a0SWilly Tu                 }
539*e5029d88SJohn Edward Broadbent             }
540*e5029d88SJohn Edward Broadbent             else if (propertyName == "Locked")
541*e5029d88SJohn Edward Broadbent             {
542*e5029d88SJohn Edward Broadbent                 isLocked = std::get_if<bool>(&property.second);
543*e5029d88SJohn Edward Broadbent                 if (isLocked == nullptr)
544*e5029d88SJohn Edward Broadbent                 {
545*e5029d88SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Illegal property: Locked";
546*e5029d88SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
547*e5029d88SJohn Edward Broadbent                     return;
548*e5029d88SJohn Edward Broadbent                 }
549*e5029d88SJohn Edward Broadbent             }
550*e5029d88SJohn Edward Broadbent         }
551*e5029d88SJohn Edward Broadbent 
552*e5029d88SJohn Edward Broadbent         if (encryptionStatus == nullptr || isLocked == nullptr ||
553*e5029d88SJohn Edward Broadbent             *encryptionStatus ==
554*e5029d88SJohn Edward Broadbent                 "xyz.openbmc_project.Drive.DriveEncryptionState.Unknown")
555*e5029d88SJohn Edward Broadbent         {
556*e5029d88SJohn Edward Broadbent             return;
557*e5029d88SJohn Edward Broadbent         }
558*e5029d88SJohn Edward Broadbent         if (*encryptionStatus !=
559*e5029d88SJohn Edward Broadbent             "xyz.openbmc_project.Drive.DriveEncryptionState.Encrypted")
560*e5029d88SJohn Edward Broadbent         {
561*e5029d88SJohn Edward Broadbent             //"The drive is not currently encrypted."
562*e5029d88SJohn Edward Broadbent             asyncResp->res.jsonValue["EncryptionStatus"] =
563*e5029d88SJohn Edward Broadbent                 drive::EncryptionStatus::Unencrypted;
564*e5029d88SJohn Edward Broadbent             return;
565*e5029d88SJohn Edward Broadbent         }
566*e5029d88SJohn Edward Broadbent         if (*isLocked)
567*e5029d88SJohn Edward Broadbent         {
568*e5029d88SJohn Edward Broadbent             //"The drive is currently encrypted and the data is not
569*e5029d88SJohn Edward Broadbent             // accessible to the user."
570*e5029d88SJohn Edward Broadbent             asyncResp->res.jsonValue["EncryptionStatus"] =
571*e5029d88SJohn Edward Broadbent                 drive::EncryptionStatus::Locked;
572*e5029d88SJohn Edward Broadbent             return;
573*e5029d88SJohn Edward Broadbent         }
574*e5029d88SJohn Edward Broadbent         // if not locked
575*e5029d88SJohn Edward Broadbent         // "The drive is currently encrypted but the data is accessible
576*e5029d88SJohn Edward Broadbent         // to the user in unencrypted form."
577*e5029d88SJohn Edward Broadbent         asyncResp->res.jsonValue["EncryptionStatus"] =
578*e5029d88SJohn Edward Broadbent             drive::EncryptionStatus::Unlocked;
57919b8e9a0SWilly Tu         });
58019b8e9a0SWilly Tu }
58119b8e9a0SWilly Tu 
582b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
583b53dcd9dSNan Zhou                             const std::string& connectionName,
584b53dcd9dSNan Zhou                             const std::string& path,
585e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
586e56ed6b9SJohn Edward Broadbent {
587e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
588e56ed6b9SJohn Edward Broadbent     {
589e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
590e56ed6b9SJohn Edward Broadbent         {
591e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
592e56ed6b9SJohn Edward Broadbent         }
593e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
594e56ed6b9SJohn Edward Broadbent         {
595e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
596e56ed6b9SJohn Edward Broadbent         }
597e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
598e56ed6b9SJohn Edward Broadbent         {
599e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
600e56ed6b9SJohn Edward Broadbent         }
601e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
602e56ed6b9SJohn Edward Broadbent         {
603e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
604e56ed6b9SJohn Edward Broadbent         }
605e56ed6b9SJohn Edward Broadbent     }
606e56ed6b9SJohn Edward Broadbent }
607e56ed6b9SJohn Edward Broadbent 
6087e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
6097e860f15SJohn Edward Broadbent {
61022d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
611ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
612002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
613002d39b4SEd Tanous             [&app](const crow::Request& req,
61445ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
61522d268cbSEd Tanous                    const std::string& systemName, const std::string& driveId) {
6163ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
61745ca1b86SEd Tanous         {
61845ca1b86SEd Tanous             return;
61945ca1b86SEd Tanous         }
62022d268cbSEd Tanous         if (systemName != "system")
62122d268cbSEd Tanous         {
62222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
62322d268cbSEd Tanous                                        systemName);
62422d268cbSEd Tanous             return;
62522d268cbSEd Tanous         }
62622d268cbSEd Tanous 
627e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
628e99073f5SGeorge Liu             "xyz.openbmc_project.Inventory.Item.Drive"};
629e99073f5SGeorge Liu         dbus::utility::getSubTree(
630e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, interfaces,
631002d39b4SEd Tanous             [asyncResp,
632e99073f5SGeorge Liu              driveId](const boost::system::error_code& ec,
633b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
6347e860f15SJohn Edward Broadbent             if (ec)
6357e860f15SJohn Edward Broadbent             {
6367e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Drive mapper call error";
6377e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
6387e860f15SJohn Edward Broadbent                 return;
6397e860f15SJohn Edward Broadbent             }
6407e860f15SJohn Edward Broadbent 
64103913171SWilly Tu             auto drive = std::find_if(
6427e860f15SJohn Edward Broadbent                 subtree.begin(), subtree.end(),
643002d39b4SEd Tanous                 [&driveId](
6448cb65f8aSNan Zhou                     const std::pair<std::string,
6458cb65f8aSNan Zhou                                     dbus::utility::MapperServiceMap>& object) {
64603913171SWilly Tu                 return sdbusplus::message::object_path(object.first)
64703913171SWilly Tu                            .filename() == driveId;
6487e860f15SJohn Edward Broadbent                 });
6497e860f15SJohn Edward Broadbent 
65003913171SWilly Tu             if (drive == subtree.end())
6517e860f15SJohn Edward Broadbent             {
652002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
6537e860f15SJohn Edward Broadbent                 return;
6547e860f15SJohn Edward Broadbent             }
6557e860f15SJohn Edward Broadbent 
65603913171SWilly Tu             const std::string& path = drive->first;
6578cb65f8aSNan Zhou             const dbus::utility::MapperServiceMap& connectionNames =
6588cb65f8aSNan Zhou                 drive->second;
6597e860f15SJohn Edward Broadbent 
660002d39b4SEd Tanous             asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
661ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
662ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/{}", driveId);
6637e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = driveId;
6647e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = driveId;
6657e860f15SJohn Edward Broadbent 
6667e860f15SJohn Edward Broadbent             if (connectionNames.size() != 1)
6677e860f15SJohn Edward Broadbent             {
668002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
66903913171SWilly Tu                                  << ", not equal to 1";
6707e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
6717e860f15SJohn Edward Broadbent                 return;
6727e860f15SJohn Edward Broadbent             }
6737e860f15SJohn Edward Broadbent 
6747e860f15SJohn Edward Broadbent             getMainChassisId(
675ef4c65b7SEd Tanous                 asyncResp,
676ef4c65b7SEd Tanous                 [](const std::string& chassisId,
6777e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
678002d39b4SEd Tanous                 aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
679ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
6807e860f15SJohn Edward Broadbent                 });
6817e860f15SJohn Edward Broadbent 
682a25aeccfSNikhil Potade             // default it to Enabled
683a25aeccfSNikhil Potade             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
684a25aeccfSNikhil Potade 
6852ad9c2f6SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
686e284a7c1SJames Feist             health->inventory.emplace_back(path);
6872ad9c2f6SJames Feist             health->populate();
6882ad9c2f6SJames Feist 
689e56ed6b9SJohn Edward Broadbent             addAllDriveInfo(asyncResp, connectionNames[0].first, path,
690e56ed6b9SJohn Edward Broadbent                             connectionNames[0].second);
691e99073f5SGeorge Liu             });
6927e860f15SJohn Edward Broadbent         });
693a25aeccfSNikhil Potade }
69492903bd4SJohn Edward Broadbent 
69592903bd4SJohn Edward Broadbent /**
69692903bd4SJohn Edward Broadbent  * Chassis drives, this URL will show all the DriveCollection
69792903bd4SJohn Edward Broadbent  * information
69892903bd4SJohn Edward Broadbent  */
699b53dcd9dSNan Zhou inline void chassisDriveCollectionGet(
70092903bd4SJohn Edward Broadbent     crow::App& app, const crow::Request& req,
70192903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
70292903bd4SJohn Edward Broadbent     const std::string& chassisId)
70392903bd4SJohn Edward Broadbent {
7043ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
70592903bd4SJohn Edward Broadbent     {
70692903bd4SJohn Edward Broadbent         return;
70792903bd4SJohn Edward Broadbent     }
70892903bd4SJohn Edward Broadbent 
70992903bd4SJohn Edward Broadbent     // mapper call lambda
710e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
711e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Board",
712e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Chassis"};
713e99073f5SGeorge Liu     dbus::utility::getSubTree(
714e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
71592903bd4SJohn Edward Broadbent         [asyncResp,
716e99073f5SGeorge Liu          chassisId](const boost::system::error_code& ec,
71792903bd4SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
71892903bd4SJohn Edward Broadbent         if (ec)
71992903bd4SJohn Edward Broadbent         {
72092903bd4SJohn Edward Broadbent             if (ec == boost::system::errc::host_unreachable)
72192903bd4SJohn Edward Broadbent             {
72292903bd4SJohn Edward Broadbent                 messages::resourceNotFound(asyncResp->res, "Chassis",
72392903bd4SJohn Edward Broadbent                                            chassisId);
72492903bd4SJohn Edward Broadbent                 return;
72592903bd4SJohn Edward Broadbent             }
72692903bd4SJohn Edward Broadbent             messages::internalError(asyncResp->res);
72792903bd4SJohn Edward Broadbent             return;
72892903bd4SJohn Edward Broadbent         }
72992903bd4SJohn Edward Broadbent 
73092903bd4SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
7318cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
73292903bd4SJohn Edward Broadbent         {
73392903bd4SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
73492903bd4SJohn Edward Broadbent             if (objPath.filename() != chassisId)
73592903bd4SJohn Edward Broadbent             {
73692903bd4SJohn Edward Broadbent                 continue;
73792903bd4SJohn Edward Broadbent             }
73892903bd4SJohn Edward Broadbent 
73992903bd4SJohn Edward Broadbent             if (connectionNames.empty())
74092903bd4SJohn Edward Broadbent             {
74192903bd4SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
74292903bd4SJohn Edward Broadbent                 continue;
74392903bd4SJohn Edward Broadbent             }
74492903bd4SJohn Edward Broadbent 
74592903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.type"] =
74692903bd4SJohn Edward Broadbent                 "#DriveCollection.DriveCollection";
74792903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
748ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}/Drives", chassisId);
74992903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "Drive Collection";
75092903bd4SJohn Edward Broadbent 
75192903bd4SJohn Edward Broadbent             // Association lambda
7526c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
7536c3e9451SGeorge Liu                 path + "/drive",
7546c3e9451SGeorge Liu                 [asyncResp,
7556c3e9451SGeorge Liu                  chassisId](const boost::system::error_code& ec3,
7566c3e9451SGeorge Liu                             const dbus::utility::MapperEndPoints& resp) {
75792903bd4SJohn Edward Broadbent                 if (ec3)
75892903bd4SJohn Edward Broadbent                 {
75992903bd4SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
76092903bd4SJohn Edward Broadbent                 }
76192903bd4SJohn Edward Broadbent                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
76292903bd4SJohn Edward Broadbent                 // important if array is empty
76392903bd4SJohn Edward Broadbent                 members = nlohmann::json::array();
76492903bd4SJohn Edward Broadbent 
76592903bd4SJohn Edward Broadbent                 std::vector<std::string> leafNames;
76692903bd4SJohn Edward Broadbent                 for (const auto& drive : resp)
76792903bd4SJohn Edward Broadbent                 {
7688a592810SEd Tanous                     sdbusplus::message::object_path drivePath(drive);
7698a592810SEd Tanous                     leafNames.push_back(drivePath.filename());
77092903bd4SJohn Edward Broadbent                 }
77192903bd4SJohn Edward Broadbent 
77292903bd4SJohn Edward Broadbent                 std::sort(leafNames.begin(), leafNames.end(),
77392903bd4SJohn Edward Broadbent                           AlphanumLess<std::string>());
77492903bd4SJohn Edward Broadbent 
77592903bd4SJohn Edward Broadbent                 for (const auto& leafName : leafNames)
77692903bd4SJohn Edward Broadbent                 {
77792903bd4SJohn Edward Broadbent                     nlohmann::json::object_t member;
778ef4c65b7SEd Tanous                     member["@odata.id"] =
779ef4c65b7SEd Tanous                         boost::urls::format("/redfish/v1/Chassis/{}/Drives/{}",
780ef4c65b7SEd Tanous                                             chassisId, leafName);
781b2ba3072SPatrick Williams                     members.emplace_back(std::move(member));
78292903bd4SJohn Edward Broadbent                     // navigation links will be registered in next patch set
78392903bd4SJohn Edward Broadbent                 }
78492903bd4SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
78592903bd4SJohn Edward Broadbent                 }); // end association lambda
78692903bd4SJohn Edward Broadbent 
78792903bd4SJohn Edward Broadbent         }           // end Iterate over all retrieved ObjectPaths
788e99073f5SGeorge Liu         });
78992903bd4SJohn Edward Broadbent }
79092903bd4SJohn Edward Broadbent 
79192903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
79292903bd4SJohn Edward Broadbent {
79392903bd4SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
79492903bd4SJohn Edward Broadbent         .privileges(redfish::privileges::getDriveCollection)
79592903bd4SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
79692903bd4SJohn Edward Broadbent             std::bind_front(chassisDriveCollectionGet, std::ref(app)));
79792903bd4SJohn Edward Broadbent }
79892903bd4SJohn Edward Broadbent 
799b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
800b53dcd9dSNan Zhou                        const std::string& chassisId,
801b53dcd9dSNan Zhou                        const std::string& driveName,
8025e7e2dc5SEd Tanous                        const boost::system::error_code& ec,
803e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
804e56ed6b9SJohn Edward Broadbent {
805e56ed6b9SJohn Edward Broadbent     if (ec)
806e56ed6b9SJohn Edward Broadbent     {
807e56ed6b9SJohn Edward Broadbent         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
808e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
809e56ed6b9SJohn Edward Broadbent         return;
810e56ed6b9SJohn Edward Broadbent     }
811e56ed6b9SJohn Edward Broadbent 
812e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
8138cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
814e56ed6b9SJohn Edward Broadbent     {
815e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
816e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
817e56ed6b9SJohn Edward Broadbent         {
818e56ed6b9SJohn Edward Broadbent             continue;
819e56ed6b9SJohn Edward Broadbent         }
820e56ed6b9SJohn Edward Broadbent 
821e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
822e56ed6b9SJohn Edward Broadbent         {
823e56ed6b9SJohn Edward Broadbent             BMCWEB_LOG_ERROR << "Got 0 Connection names";
824e56ed6b9SJohn Edward Broadbent             continue;
825e56ed6b9SJohn Edward Broadbent         }
826e56ed6b9SJohn Edward Broadbent 
827ef4c65b7SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
828ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/Drives/{}", chassisId, driveName);
829e56ed6b9SJohn Edward Broadbent 
830e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
831a0cb40cbSJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = driveName;
832e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
833e56ed6b9SJohn Edward Broadbent         // default it to Enabled
834e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
835e56ed6b9SJohn Edward Broadbent 
836e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
837e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
838ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
839e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
840e56ed6b9SJohn Edward Broadbent 
841e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
842e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
843e56ed6b9SJohn Edward Broadbent     }
844e56ed6b9SJohn Edward Broadbent }
845e56ed6b9SJohn Edward Broadbent 
846b53dcd9dSNan Zhou inline void
847b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
848e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
849e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
850e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
851e56ed6b9SJohn Edward Broadbent {
852e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
853e56ed6b9SJohn Edward Broadbent     {
854e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
855e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
856e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
857e56ed6b9SJohn Edward Broadbent         {
858e56ed6b9SJohn Edward Broadbent             continue;
859e56ed6b9SJohn Edward Broadbent         }
860e56ed6b9SJohn Edward Broadbent         //  mapper call drive
861e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> driveInterface = {
862e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
863e99073f5SGeorge Liu         dbus::utility::getSubTree(
864e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, driveInterface,
865e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
866e99073f5SGeorge Liu                 const boost::system::error_code& ec,
867e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
868e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
869e99073f5SGeorge Liu             });
870e56ed6b9SJohn Edward Broadbent     }
871e56ed6b9SJohn Edward Broadbent }
872e56ed6b9SJohn Edward Broadbent 
873b53dcd9dSNan Zhou inline void
874b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
875e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
876e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
877e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
878e56ed6b9SJohn Edward Broadbent {
87903810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
880e56ed6b9SJohn Edward Broadbent     {
881e56ed6b9SJohn Edward Broadbent         return;
882e56ed6b9SJohn Edward Broadbent     }
883e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
884e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
885e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
886e56ed6b9SJohn Edward Broadbent 
887e56ed6b9SJohn Edward Broadbent     // mapper call chassis
888e99073f5SGeorge Liu     dbus::utility::getSubTree(
889e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
890e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
891e99073f5SGeorge Liu          driveName](const boost::system::error_code& ec,
892e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
893e56ed6b9SJohn Edward Broadbent         if (ec)
894e56ed6b9SJohn Edward Broadbent         {
895e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
896e56ed6b9SJohn Edward Broadbent             return;
897e56ed6b9SJohn Edward Broadbent         }
898e56ed6b9SJohn Edward Broadbent 
899e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
9008cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
901e56ed6b9SJohn Edward Broadbent         {
902e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
903e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
904e56ed6b9SJohn Edward Broadbent             {
905e56ed6b9SJohn Edward Broadbent                 continue;
906e56ed6b9SJohn Edward Broadbent             }
907e56ed6b9SJohn Edward Broadbent 
908e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
909e56ed6b9SJohn Edward Broadbent             {
910e56ed6b9SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
911e56ed6b9SJohn Edward Broadbent                 continue;
912e56ed6b9SJohn Edward Broadbent             }
913e56ed6b9SJohn Edward Broadbent 
9146c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
9156c3e9451SGeorge Liu                 path + "/drive",
916e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
9175e7e2dc5SEd Tanous                  driveName](const boost::system::error_code& ec3,
9186c3e9451SGeorge Liu                             const dbus::utility::MapperEndPoints& resp) {
919e56ed6b9SJohn Edward Broadbent                 if (ec3)
920e56ed6b9SJohn Edward Broadbent                 {
921e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
922e56ed6b9SJohn Edward Broadbent                 }
923e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
924e56ed6b9SJohn Edward Broadbent                 });
925e56ed6b9SJohn Edward Broadbent             break;
926e56ed6b9SJohn Edward Broadbent         }
927e99073f5SGeorge Liu         });
928e56ed6b9SJohn Edward Broadbent }
929e56ed6b9SJohn Edward Broadbent 
930e56ed6b9SJohn Edward Broadbent /**
931e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
932e56ed6b9SJohn Edward Broadbent  */
933e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
934e56ed6b9SJohn Edward Broadbent {
935e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
936e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
937e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
938e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
939e56ed6b9SJohn Edward Broadbent }
940e56ed6b9SJohn Edward Broadbent 
941a25aeccfSNikhil Potade } // namespace redfish
942