blob: 5b2d78e5b7b4b1ffefa1f1f6c9f2ac15b19a9eee [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3// SPDX-FileCopyrightText: Copyright 2019 Intel Corporation
Nikhil Potadea25aecc2019-08-23 16:35:26 -07004#pragma once
5
Willy Tu13451e32023-05-24 16:08:18 -07006#include "bmcweb_config.h"
7
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08008#include "app.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08009#include "async_resp.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080010#include "dbus_utility.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080011#include "error_messages.hpp"
John Edward Broadbente5029d82022-06-08 14:35:21 -070012#include "generated/enums/drive.hpp"
George Liudde9bc12023-02-22 09:35:51 +080013#include "generated/enums/protocol.hpp"
Ed Tanous539d8c62024-06-19 14:38:27 -070014#include "generated/enums/resource.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080015#include "http_request.hpp"
Ed Tanousa8e884f2023-01-13 17:40:03 -080016#include "human_sort.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080017#include "logging.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "query.hpp"
Ed Tanousa8e884f2023-01-13 17:40:03 -080019#include "redfish_util.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "registries/privilege_registry.hpp"
Myung Bae3f95a272024-03-13 07:32:02 -070021#include "utils/chassis_utils.hpp"
Willy Tu5e577bc2022-07-26 00:41:55 +000022#include "utils/collection.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080023#include "utils/dbus_utils.hpp"
James Feist2ad9c2f2019-10-29 16:26:48 -070024
Ed Tanousd7857202025-01-28 15:32:26 -080025#include <boost/beast/http/verb.hpp>
George Liue99073f2022-12-09 11:06:16 +080026#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070027#include <boost/url/format.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080028#include <sdbusplus/message/native_types.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020029#include <sdbusplus/unpack_properties.hpp>
Nikhil Potadea25aecc2019-08-23 16:35:26 -070030
Ed Tanousd7857202025-01-28 15:32:26 -080031#include <algorithm>
George Liu7a1dbc42022-12-07 16:03:22 +080032#include <array>
Ed Tanousd7857202025-01-28 15:32:26 -080033#include <cstdint>
34#include <format>
35#include <functional>
36#include <memory>
37#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070038#include <ranges>
Ed Tanousd7857202025-01-28 15:32:26 -080039#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080040#include <string_view>
Ed Tanousd7857202025-01-28 15:32:26 -080041#include <utility>
42#include <variant>
43#include <vector>
George Liu7a1dbc42022-12-07 16:03:22 +080044
Nikhil Potadea25aecc2019-08-23 16:35:26 -070045namespace redfish
46{
Ed Tanous36d52332023-06-09 13:18:40 -070047
48inline void handleSystemsStorageCollectionGet(
49 App& app, const crow::Request& req,
50 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
51 const std::string& systemName)
52{
53 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
54 {
55 return;
56 }
Ed Tanous253f11b2024-05-16 09:38:31 -070057 if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
Ed Tanous36d52332023-06-09 13:18:40 -070058 {
59 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
60 systemName);
61 return;
62 }
63
64 asyncResp->res.jsonValue["@odata.type"] =
65 "#StorageCollection.StorageCollection";
Ed Tanous253f11b2024-05-16 09:38:31 -070066 asyncResp->res.jsonValue["@odata.id"] = std::format(
67 "/redfish/v1/Systems/{}/Storage", BMCWEB_REDFISH_SYSTEM_URI_NAME);
Ed Tanous36d52332023-06-09 13:18:40 -070068 asyncResp->res.jsonValue["Name"] = "Storage Collection";
Willy Tu5e577bc2022-07-26 00:41:55 +000069
Patrick Williams5a39f772023-10-20 11:20:21 -050070 constexpr std::array<std::string_view, 1> interface{
71 "xyz.openbmc_project.Inventory.Item.Storage"};
Willy Tu5e577bc2022-07-26 00:41:55 +000072 collection_util::getCollectionMembers(
Ed Tanous253f11b2024-05-16 09:38:31 -070073 asyncResp,
74 boost::urls::format("/redfish/v1/Systems/{}/Storage",
75 BMCWEB_REDFISH_SYSTEM_URI_NAME),
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050076 interface, "/xyz/openbmc_project/inventory");
Willy Tu5e577bc2022-07-26 00:41:55 +000077}
78
79inline void handleStorageCollectionGet(
80 App& app, const crow::Request& req,
81 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
82{
83 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
84 {
85 return;
86 }
87 asyncResp->res.jsonValue["@odata.type"] =
88 "#StorageCollection.StorageCollection";
89 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Storage";
90 asyncResp->res.jsonValue["Name"] = "Storage Collection";
Patrick Williams5a39f772023-10-20 11:20:21 -050091 constexpr std::array<std::string_view, 1> interface{
92 "xyz.openbmc_project.Inventory.Item.Storage"};
Willy Tu5e577bc2022-07-26 00:41:55 +000093 collection_util::getCollectionMembers(
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050094 asyncResp, boost::urls::format("/redfish/v1/Storage"), interface,
95 "/xyz/openbmc_project/inventory");
Ed Tanous36d52332023-06-09 13:18:40 -070096}
97
John Edward Broadbent7e860f12021-04-08 15:57:16 -070098inline void requestRoutesStorageCollection(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -070099{
Ed Tanous22d268c2022-05-19 09:39:07 -0700100 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
Ed Tanoused398212021-06-09 17:05:54 -0700101 .privileges(redfish::privileges::getStorageCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700102 .methods(boost::beast::http::verb::get)(
Ed Tanous36d52332023-06-09 13:18:40 -0700103 std::bind_front(handleSystemsStorageCollectionGet, std::ref(app)));
Willy Tu5e577bc2022-07-26 00:41:55 +0000104 BMCWEB_ROUTE(app, "/redfish/v1/Storage/")
105 .privileges(redfish::privileges::getStorageCollection)
106 .methods(boost::beast::http::verb::get)(
107 std::bind_front(handleStorageCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700108}
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700109
Ed Tanous36d52332023-06-09 13:18:40 -0700110inline void afterChassisDriveCollectionSubtree(
111 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous36d52332023-06-09 13:18:40 -0700112 const boost::system::error_code& ec,
113 const dbus::utility::MapperGetSubTreePathsResponse& driveList)
114{
115 if (ec)
116 {
Ed Tanous62598e32023-07-17 17:06:25 -0700117 BMCWEB_LOG_ERROR("Drive mapper call error");
Ed Tanous36d52332023-06-09 13:18:40 -0700118 messages::internalError(asyncResp->res);
119 return;
120 }
121
122 nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
123 driveArray = nlohmann::json::array();
124 auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
125 count = 0;
126
Ed Tanous36d52332023-06-09 13:18:40 -0700127 for (const std::string& drive : driveList)
128 {
129 sdbusplus::message::object_path object(drive);
130 if (object.filename().empty())
131 {
Ed Tanous62598e32023-07-17 17:06:25 -0700132 BMCWEB_LOG_ERROR("Failed to find filename in {}", drive);
Ed Tanous36d52332023-06-09 13:18:40 -0700133 return;
134 }
135
136 nlohmann::json::object_t driveJson;
137 driveJson["@odata.id"] = boost::urls::format(
Ed Tanous253f11b2024-05-16 09:38:31 -0700138 "/redfish/v1/Systems/{}/Storage/1/Drives/{}",
139 BMCWEB_REDFISH_SYSTEM_URI_NAME, object.filename());
Ed Tanous36d52332023-06-09 13:18:40 -0700140 driveArray.emplace_back(std::move(driveJson));
141 }
142
143 count = driveArray.size();
144}
Gunnar Mills7ac13cc2024-04-01 16:05:21 -0500145inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Willy Tua85afbe2021-12-28 14:43:47 -0800146{
George Liu7a1dbc42022-12-07 16:03:22 +0800147 const std::array<std::string_view, 1> interfaces = {
148 "xyz.openbmc_project.Inventory.Item.Drive"};
149 dbus::utility::getSubTreePaths(
150 "/xyz/openbmc_project/inventory", 0, interfaces,
Gunnar Mills7ac13cc2024-04-01 16:05:21 -0500151 std::bind_front(afterChassisDriveCollectionSubtree, asyncResp));
Ed Tanous36d52332023-06-09 13:18:40 -0700152}
Willy Tua85afbe2021-12-28 14:43:47 -0800153
Willy Tu5e577bc2022-07-26 00:41:55 +0000154inline void afterSystemsStorageGetSubtree(
155 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
156 const std::string& storageId, const boost::system::error_code& ec,
157 const dbus::utility::MapperGetSubTreeResponse& subtree)
Ed Tanous36d52332023-06-09 13:18:40 -0700158{
Willy Tu5e577bc2022-07-26 00:41:55 +0000159 if (ec)
Ed Tanous36d52332023-06-09 13:18:40 -0700160 {
Ed Tanous62598e32023-07-17 17:06:25 -0700161 BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
Willy Tu5e577bc2022-07-26 00:41:55 +0000162 messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
163 storageId);
Ed Tanous36d52332023-06-09 13:18:40 -0700164 return;
165 }
Ed Tanous3544d2a2023-08-06 18:12:20 -0700166 auto storage = std::ranges::find_if(
167 subtree,
Willy Tu5e577bc2022-07-26 00:41:55 +0000168 [&storageId](const std::pair<std::string,
169 dbus::utility::MapperServiceMap>& object) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400170 return sdbusplus::message::object_path(object.first).filename() ==
171 storageId;
172 });
Willy Tu5e577bc2022-07-26 00:41:55 +0000173 if (storage == subtree.end())
174 {
175 messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
176 storageId);
177 return;
178 }
179
Ed Tanous36d52332023-06-09 13:18:40 -0700180 asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
181 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -0700182 boost::urls::format("/redfish/v1/Systems/{}/Storage/{}",
183 BMCWEB_REDFISH_SYSTEM_URI_NAME, storageId);
Ed Tanous36d52332023-06-09 13:18:40 -0700184 asyncResp->res.jsonValue["Name"] = "Storage";
Willy Tu5e577bc2022-07-26 00:41:55 +0000185 asyncResp->res.jsonValue["Id"] = storageId;
Ed Tanous539d8c62024-06-19 14:38:27 -0700186 asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
Willy Tua85afbe2021-12-28 14:43:47 -0800187
Gunnar Mills7ac13cc2024-04-01 16:05:21 -0500188 getDrives(asyncResp);
Ed Tanous253f11b2024-05-16 09:38:31 -0700189 asyncResp->res.jsonValue["Controllers"]["@odata.id"] =
190 boost::urls::format("/redfish/v1/Systems/{}/Storage/{}/Controllers",
191 BMCWEB_REDFISH_SYSTEM_URI_NAME, storageId);
Willy Tu5e577bc2022-07-26 00:41:55 +0000192}
193
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400194inline void handleSystemsStorageGet(
195 App& app, const crow::Request& req,
196 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
197 const std::string& systemName, const std::string& storageId)
Willy Tu5e577bc2022-07-26 00:41:55 +0000198{
199 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
200 {
201 return;
202 }
Ed Tanous25b54db2024-04-17 15:40:31 -0700203 if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800204 {
205 // Option currently returns no systems. TBD
206 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
207 systemName);
208 return;
209 }
Willy Tu5e577bc2022-07-26 00:41:55 +0000210
211 constexpr std::array<std::string_view, 1> interfaces = {
212 "xyz.openbmc_project.Inventory.Item.Storage"};
213 dbus::utility::getSubTree(
214 "/xyz/openbmc_project/inventory", 0, interfaces,
215 std::bind_front(afterSystemsStorageGetSubtree, asyncResp, storageId));
216}
217
218inline void afterSubtree(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
219 const std::string& storageId,
220 const boost::system::error_code& ec,
221 const dbus::utility::MapperGetSubTreeResponse& subtree)
222{
223 if (ec)
224 {
Ed Tanous62598e32023-07-17 17:06:25 -0700225 BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
Willy Tu5e577bc2022-07-26 00:41:55 +0000226 messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
227 storageId);
228 return;
229 }
Ed Tanous3544d2a2023-08-06 18:12:20 -0700230 auto storage = std::ranges::find_if(
231 subtree,
Willy Tu5e577bc2022-07-26 00:41:55 +0000232 [&storageId](const std::pair<std::string,
233 dbus::utility::MapperServiceMap>& object) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400234 return sdbusplus::message::object_path(object.first).filename() ==
235 storageId;
236 });
Willy Tu5e577bc2022-07-26 00:41:55 +0000237 if (storage == subtree.end())
238 {
239 messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
240 storageId);
241 return;
242 }
243
244 asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
245 asyncResp->res.jsonValue["@odata.id"] =
246 boost::urls::format("/redfish/v1/Storage/{}", storageId);
247 asyncResp->res.jsonValue["Name"] = "Storage";
248 asyncResp->res.jsonValue["Id"] = storageId;
Ed Tanous539d8c62024-06-19 14:38:27 -0700249 asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
Willy Tu5e577bc2022-07-26 00:41:55 +0000250
251 // Storage subsystem to Storage link.
252 nlohmann::json::array_t storageServices;
253 nlohmann::json::object_t storageService;
254 storageService["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -0700255 boost::urls::format("/redfish/v1/Systems/{}/Storage/{}",
256 BMCWEB_REDFISH_SYSTEM_URI_NAME, storageId);
Willy Tu5e577bc2022-07-26 00:41:55 +0000257 storageServices.emplace_back(storageService);
258 asyncResp->res.jsonValue["Links"]["StorageServices"] =
259 std::move(storageServices);
260 asyncResp->res.jsonValue["Links"]["StorageServices@odata.count"] = 1;
261}
262
Patrick Williams504af5a2025-02-03 14:29:03 -0500263inline void handleStorageGet(
264 App& app, const crow::Request& req,
265 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
266 const std::string& storageId)
Willy Tu5e577bc2022-07-26 00:41:55 +0000267{
268 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
269 {
Ed Tanous62598e32023-07-17 17:06:25 -0700270 BMCWEB_LOG_DEBUG("requestRoutesStorage setUpRedfishRoute failed");
Willy Tu5e577bc2022-07-26 00:41:55 +0000271 return;
272 }
273
274 constexpr std::array<std::string_view, 1> interfaces = {
275 "xyz.openbmc_project.Inventory.Item.Storage"};
276 dbus::utility::getSubTree(
277 "/xyz/openbmc_project/inventory", 0, interfaces,
278 std::bind_front(afterSubtree, asyncResp, storageId));
Willy Tua85afbe2021-12-28 14:43:47 -0800279}
280
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700281inline void requestRoutesStorage(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700282{
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800283 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700284 .privileges(redfish::privileges::getStorage)
Ed Tanous002d39b2022-05-31 08:59:27 -0700285 .methods(boost::beast::http::verb::get)(
Ed Tanous36d52332023-06-09 13:18:40 -0700286 std::bind_front(handleSystemsStorageGet, std::ref(app)));
Willy Tu5e577bc2022-07-26 00:41:55 +0000287
288 BMCWEB_ROUTE(app, "/redfish/v1/Storage/<str>/")
289 .privileges(redfish::privileges::getStorage)
290 .methods(boost::beast::http::verb::get)(
291 std::bind_front(handleStorageGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700292}
293
Willy Tu03913172021-11-08 02:03:19 -0800294inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
295 const std::string& connectionName,
296 const std::string& path)
297{
Ed Tanousdeae6a72024-11-11 21:58:57 -0800298 dbus::utility::getAllProperties(
299 connectionName, path, "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800300 [asyncResp](const boost::system::error_code& ec,
Ed Tanous168e20c2021-12-13 14:39:53 -0800301 const std::vector<
302 std::pair<std::string, dbus::utility::DbusVariantType>>&
303 propertiesList) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400304 if (ec)
305 {
306 // this interface isn't necessary
307 return;
308 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200309
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400310 const std::string* partNumber = nullptr;
311 const std::string* serialNumber = nullptr;
312 const std::string* manufacturer = nullptr;
313 const std::string* model = nullptr;
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200314
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400315 const bool success = sdbusplus::unpackPropertiesNoThrow(
316 dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
317 partNumber, "SerialNumber", serialNumber, "Manufacturer",
318 manufacturer, "Model", model);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200319
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400320 if (!success)
321 {
322 messages::internalError(asyncResp->res);
323 return;
324 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200325
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400326 if (partNumber != nullptr)
327 {
328 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
329 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200330
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400331 if (serialNumber != nullptr)
332 {
333 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
334 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200335
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400336 if (manufacturer != nullptr)
337 {
338 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
339 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200340
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400341 if (model != nullptr)
342 {
343 asyncResp->res.jsonValue["Model"] = *model;
344 }
345 });
Willy Tu03913172021-11-08 02:03:19 -0800346}
347
348inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
349 const std::string& connectionName,
350 const std::string& path)
351{
Ed Tanousdeae6a72024-11-11 21:58:57 -0800352 dbus::utility::getProperty<bool>(
353 connectionName, path, "xyz.openbmc_project.Inventory.Item", "Present",
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400354 [asyncResp,
355 path](const boost::system::error_code& ec, const bool isPresent) {
356 // this interface isn't necessary, only check it if
357 // we get a good return
358 if (ec)
359 {
360 return;
361 }
Willy Tu03913172021-11-08 02:03:19 -0800362
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400363 if (!isPresent)
364 {
365 asyncResp->res.jsonValue["Status"]["State"] =
366 resource::State::Absent;
367 }
368 });
Willy Tu03913172021-11-08 02:03:19 -0800369}
370
371inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
372 const std::string& connectionName,
373 const std::string& path)
374{
Ed Tanousdeae6a72024-11-11 21:58:57 -0800375 dbus::utility::getProperty<bool>(
376 connectionName, path, "xyz.openbmc_project.State.Drive", "Rebuilding",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800377 [asyncResp](const boost::system::error_code& ec, const bool updating) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400378 // this interface isn't necessary, only check it
379 // if we get a good return
380 if (ec)
381 {
382 return;
383 }
Willy Tu03913172021-11-08 02:03:19 -0800384
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400385 // updating and disabled in the backend shouldn't be
386 // able to be set at the same time, so we don't need
387 // to check for the race condition of these two
388 // calls
389 if (updating)
390 {
391 asyncResp->res.jsonValue["Status"]["State"] =
392 resource::State::Updating;
393 }
394 });
Willy Tu03913172021-11-08 02:03:19 -0800395}
396
George Liudde9bc12023-02-22 09:35:51 +0800397inline std::optional<drive::MediaType> convertDriveType(std::string_view type)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800398{
399 if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
400 {
George Liudde9bc12023-02-22 09:35:51 +0800401 return drive::MediaType::HDD;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800402 }
403 if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
404 {
George Liudde9bc12023-02-22 09:35:51 +0800405 return drive::MediaType::SSD;
406 }
407 if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.Unknown")
408 {
409 return std::nullopt;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800410 }
411
George Liudde9bc12023-02-22 09:35:51 +0800412 return drive::MediaType::Invalid;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800413}
414
Patrick Williams504af5a2025-02-03 14:29:03 -0500415inline std::optional<protocol::Protocol> convertDriveProtocol(
416 std::string_view proto)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800417{
418 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
419 {
George Liudde9bc12023-02-22 09:35:51 +0800420 return protocol::Protocol::SAS;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800421 }
422 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
423 {
George Liudde9bc12023-02-22 09:35:51 +0800424 return protocol::Protocol::SATA;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800425 }
426 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
427 {
George Liudde9bc12023-02-22 09:35:51 +0800428 return protocol::Protocol::NVMe;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800429 }
430 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
431 {
George Liudde9bc12023-02-22 09:35:51 +0800432 return protocol::Protocol::FC;
433 }
434 if (proto ==
435 "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.Unknown")
436 {
437 return std::nullopt;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800438 }
439
George Liudde9bc12023-02-22 09:35:51 +0800440 return protocol::Protocol::Invalid;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800441}
442
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400443inline void getDriveItemProperties(
444 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
445 const std::string& connectionName, const std::string& path)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800446{
Ed Tanousdeae6a72024-11-11 21:58:57 -0800447 dbus::utility::getAllProperties(
448 connectionName, path, "xyz.openbmc_project.Inventory.Item.Drive",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800449 [asyncResp](const boost::system::error_code& ec,
Willy Tu19b8e9a2021-11-08 02:55:03 -0800450 const std::vector<
451 std::pair<std::string, dbus::utility::DbusVariantType>>&
452 propertiesList) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400453 if (ec)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800454 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400455 // this interface isn't required
456 return;
457 }
458 const std::string* encryptionStatus = nullptr;
459 const bool* isLocked = nullptr;
460 for (const std::pair<std::string, dbus::utility::DbusVariantType>&
461 property : propertiesList)
462 {
463 const std::string& propertyName = property.first;
464 if (propertyName == "Type")
Ed Tanous002d39b2022-05-31 08:59:27 -0700465 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400466 const std::string* value =
467 std::get_if<std::string>(&property.second);
468 if (value == nullptr)
469 {
470 // illegal property
471 BMCWEB_LOG_ERROR("Illegal property: Type");
472 messages::internalError(asyncResp->res);
473 return;
474 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700475
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400476 std::optional<drive::MediaType> mediaType =
477 convertDriveType(*value);
478 if (!mediaType)
479 {
480 BMCWEB_LOG_WARNING("UnknownDriveType Interface: {}",
481 *value);
482 continue;
483 }
484 if (*mediaType == drive::MediaType::Invalid)
485 {
486 messages::internalError(asyncResp->res);
487 return;
488 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700489
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400490 asyncResp->res.jsonValue["MediaType"] = *mediaType;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800491 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400492 else if (propertyName == "Capacity")
Willy Tu19b8e9a2021-11-08 02:55:03 -0800493 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400494 const uint64_t* capacity =
495 std::get_if<uint64_t>(&property.second);
496 if (capacity == nullptr)
497 {
498 BMCWEB_LOG_ERROR("Illegal property: Capacity");
499 messages::internalError(asyncResp->res);
500 return;
501 }
502 if (*capacity == 0)
503 {
504 // drive capacity not known
505 continue;
506 }
Willy Tu19b8e9a2021-11-08 02:55:03 -0800507
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400508 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
Ed Tanous002d39b2022-05-31 08:59:27 -0700509 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400510 else if (propertyName == "Protocol")
511 {
512 const std::string* value =
513 std::get_if<std::string>(&property.second);
514 if (value == nullptr)
515 {
516 BMCWEB_LOG_ERROR("Illegal property: Protocol");
517 messages::internalError(asyncResp->res);
518 return;
519 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700520
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400521 std::optional<protocol::Protocol> proto =
522 convertDriveProtocol(*value);
523 if (!proto)
524 {
525 BMCWEB_LOG_WARNING(
526 "Unknown DrivePrototype Interface: {}", *value);
527 continue;
528 }
529 if (*proto == protocol::Protocol::Invalid)
530 {
531 messages::internalError(asyncResp->res);
532 return;
533 }
534 asyncResp->res.jsonValue["Protocol"] = *proto;
George Liudde9bc12023-02-22 09:35:51 +0800535 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400536 else if (propertyName == "PredictedMediaLifeLeftPercent")
George Liudde9bc12023-02-22 09:35:51 +0800537 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400538 const uint8_t* lifeLeft =
539 std::get_if<uint8_t>(&property.second);
540 if (lifeLeft == nullptr)
541 {
542 BMCWEB_LOG_ERROR(
543 "Illegal property: PredictedMediaLifeLeftPercent");
544 messages::internalError(asyncResp->res);
545 return;
546 }
547 // 255 means reading the value is not supported
548 if (*lifeLeft != 255)
549 {
550 asyncResp->res
551 .jsonValue["PredictedMediaLifeLeftPercent"] =
552 *lifeLeft;
553 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700554 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400555 else if (propertyName == "EncryptionStatus")
John Edward Broadbent3fe4d5c2022-05-06 14:42:35 -0700556 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400557 encryptionStatus =
558 std::get_if<std::string>(&property.second);
559 if (encryptionStatus == nullptr)
560 {
561 BMCWEB_LOG_ERROR("Illegal property: EncryptionStatus");
562 messages::internalError(asyncResp->res);
563 return;
564 }
John Edward Broadbent3fe4d5c2022-05-06 14:42:35 -0700565 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400566 else if (propertyName == "Locked")
John Edward Broadbent3fe4d5c2022-05-06 14:42:35 -0700567 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400568 isLocked = std::get_if<bool>(&property.second);
569 if (isLocked == nullptr)
570 {
571 BMCWEB_LOG_ERROR("Illegal property: Locked");
572 messages::internalError(asyncResp->res);
573 return;
574 }
John Edward Broadbent3fe4d5c2022-05-06 14:42:35 -0700575 }
576 }
John Edward Broadbente5029d82022-06-08 14:35:21 -0700577
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400578 if (encryptionStatus == nullptr || isLocked == nullptr ||
579 *encryptionStatus ==
580 "xyz.openbmc_project.Inventory.Item.Drive.DriveEncryptionState.Unknown")
581 {
582 return;
583 }
584 if (*encryptionStatus !=
585 "xyz.openbmc_project.Inventory.Item.Drive.DriveEncryptionState.Encrypted")
586 {
587 //"The drive is not currently encrypted."
588 asyncResp->res.jsonValue["EncryptionStatus"] =
589 drive::EncryptionStatus::Unencrypted;
590 return;
591 }
592 if (*isLocked)
593 {
594 //"The drive is currently encrypted and the data is not
595 // accessible to the user."
596 asyncResp->res.jsonValue["EncryptionStatus"] =
597 drive::EncryptionStatus::Locked;
598 return;
599 }
600 // if not locked
601 // "The drive is currently encrypted but the data is accessible
602 // to the user in unencrypted form."
John Edward Broadbente5029d82022-06-08 14:35:21 -0700603 asyncResp->res.jsonValue["EncryptionStatus"] =
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400604 drive::EncryptionStatus::Unlocked;
605 });
Willy Tu19b8e9a2021-11-08 02:55:03 -0800606}
607
Ed Tanous4ff0f1f2024-09-04 17:27:37 -0700608inline void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Nan Zhoub53dcd92022-06-21 17:47:50 +0000609 const std::string& connectionName,
610 const std::string& path,
611 const std::vector<std::string>& interfaces)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700612{
613 for (const std::string& interface : interfaces)
614 {
615 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
616 {
617 getDriveAsset(asyncResp, connectionName, path);
618 }
619 else if (interface == "xyz.openbmc_project.Inventory.Item")
620 {
621 getDrivePresent(asyncResp, connectionName, path);
622 }
623 else if (interface == "xyz.openbmc_project.State.Drive")
624 {
625 getDriveState(asyncResp, connectionName, path);
626 }
627 else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
628 {
629 getDriveItemProperties(asyncResp, connectionName, path);
630 }
631 }
632}
633
Ed Tanous36d52332023-06-09 13:18:40 -0700634inline void afterGetSubtreeSystemsStorageDrive(
635 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
636 const std::string& driveId, const boost::system::error_code& ec,
637 const dbus::utility::MapperGetSubTreeResponse& subtree)
638{
639 if (ec)
640 {
Ed Tanous62598e32023-07-17 17:06:25 -0700641 BMCWEB_LOG_ERROR("Drive mapper call error");
Ed Tanous36d52332023-06-09 13:18:40 -0700642 messages::internalError(asyncResp->res);
643 return;
644 }
645
Ed Tanous3544d2a2023-08-06 18:12:20 -0700646 auto drive = std::ranges::find_if(
647 subtree,
Ed Tanous36d52332023-06-09 13:18:40 -0700648 [&driveId](const std::pair<std::string,
649 dbus::utility::MapperServiceMap>& object) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400650 return sdbusplus::message::object_path(object.first).filename() ==
651 driveId;
652 });
Ed Tanous36d52332023-06-09 13:18:40 -0700653
654 if (drive == subtree.end())
655 {
656 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
657 return;
658 }
659
660 const std::string& path = drive->first;
661 const dbus::utility::MapperServiceMap& connectionNames = drive->second;
662
663 asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
Ed Tanous253f11b2024-05-16 09:38:31 -0700664 asyncResp->res.jsonValue["@odata.id"] =
665 boost::urls::format("/redfish/v1/Systems/{}/Storage/1/Drives/{}",
666 BMCWEB_REDFISH_SYSTEM_URI_NAME, driveId);
Ed Tanous36d52332023-06-09 13:18:40 -0700667 asyncResp->res.jsonValue["Name"] = driveId;
668 asyncResp->res.jsonValue["Id"] = driveId;
669
670 if (connectionNames.size() != 1)
671 {
Ed Tanous62598e32023-07-17 17:06:25 -0700672 BMCWEB_LOG_ERROR("Connection size {}, not equal to 1",
673 connectionNames.size());
Ed Tanous36d52332023-06-09 13:18:40 -0700674 messages::internalError(asyncResp->res);
675 return;
676 }
677
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400678 getMainChassisId(
679 asyncResp, [](const std::string& chassisId,
680 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
681 aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
682 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
683 });
Ed Tanous36d52332023-06-09 13:18:40 -0700684
685 // default it to Enabled
Ed Tanous539d8c62024-06-19 14:38:27 -0700686 asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
Ed Tanous36d52332023-06-09 13:18:40 -0700687
Ed Tanous36d52332023-06-09 13:18:40 -0700688 addAllDriveInfo(asyncResp, connectionNames[0].first, path,
689 connectionNames[0].second);
690}
691
692inline void handleSystemsStorageDriveGet(
693 App& app, const crow::Request& req,
694 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
695 const std::string& systemName, const std::string& driveId)
696{
697 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
698 {
699 return;
700 }
Ed Tanous25b54db2024-04-17 15:40:31 -0700701 if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800702 {
703 // Option currently returns no systems. TBD
704 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
705 systemName);
706 return;
707 }
708
Ed Tanous253f11b2024-05-16 09:38:31 -0700709 if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
Ed Tanous36d52332023-06-09 13:18:40 -0700710 {
711 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
712 systemName);
713 return;
714 }
715
716 constexpr std::array<std::string_view, 1> interfaces = {
717 "xyz.openbmc_project.Inventory.Item.Drive"};
718 dbus::utility::getSubTree(
719 "/xyz/openbmc_project/inventory", 0, interfaces,
720 std::bind_front(afterGetSubtreeSystemsStorageDrive, asyncResp,
721 driveId));
722}
723
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700724inline void requestRoutesDrive(App& app)
725{
Ed Tanous22d268c2022-05-19 09:39:07 -0700726 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700727 .privileges(redfish::privileges::getDrive)
Ed Tanous002d39b2022-05-31 08:59:27 -0700728 .methods(boost::beast::http::verb::get)(
Ed Tanous36d52332023-06-09 13:18:40 -0700729 std::bind_front(handleSystemsStorageDriveGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700730}
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700731
Ed Tanous36d52332023-06-09 13:18:40 -0700732inline void afterChassisDriveCollectionSubtreeGet(
733 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
734 const std::string& chassisId, const boost::system::error_code& ec,
735 const dbus::utility::MapperGetSubTreeResponse& subtree)
736{
737 if (ec)
738 {
739 if (ec == boost::system::errc::host_unreachable)
740 {
741 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
742 return;
743 }
744 messages::internalError(asyncResp->res);
745 return;
746 }
747
748 // Iterate over all retrieved ObjectPaths.
749 for (const auto& [path, connectionNames] : subtree)
750 {
751 sdbusplus::message::object_path objPath(path);
752 if (objPath.filename() != chassisId)
753 {
754 continue;
755 }
756
757 if (connectionNames.empty())
758 {
Ed Tanous62598e32023-07-17 17:06:25 -0700759 BMCWEB_LOG_ERROR("Got 0 Connection names");
Ed Tanous36d52332023-06-09 13:18:40 -0700760 continue;
761 }
762
763 asyncResp->res.jsonValue["@odata.type"] =
764 "#DriveCollection.DriveCollection";
765 asyncResp->res.jsonValue["@odata.id"] =
766 boost::urls::format("/redfish/v1/Chassis/{}/Drives", chassisId);
767 asyncResp->res.jsonValue["Name"] = "Drive Collection";
768
769 // Association lambda
770 dbus::utility::getAssociationEndPoints(
771 path + "/drive",
772 [asyncResp, chassisId](const boost::system::error_code& ec3,
773 const dbus::utility::MapperEndPoints& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400774 if (ec3)
775 {
776 BMCWEB_LOG_ERROR("Error in chassis Drive association ");
777 }
778 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
779 // important if array is empty
780 members = nlohmann::json::array();
Ed Tanous36d52332023-06-09 13:18:40 -0700781
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400782 std::vector<std::string> leafNames;
783 for (const auto& drive : resp)
784 {
785 sdbusplus::message::object_path drivePath(drive);
786 leafNames.push_back(drivePath.filename());
787 }
Ed Tanous36d52332023-06-09 13:18:40 -0700788
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400789 std::ranges::sort(leafNames, AlphanumLess<std::string>());
Ed Tanous36d52332023-06-09 13:18:40 -0700790
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400791 for (const auto& leafName : leafNames)
792 {
793 nlohmann::json::object_t member;
794 member["@odata.id"] =
795 boost::urls::format("/redfish/v1/Chassis/{}/Drives/{}",
796 chassisId, leafName);
797 members.emplace_back(std::move(member));
798 // navigation links will be registered in next patch set
799 }
800 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
801 }); // end association lambda
Ed Tanous36d52332023-06-09 13:18:40 -0700802
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400803 } // end Iterate over all retrieved ObjectPaths
Ed Tanous36d52332023-06-09 13:18:40 -0700804}
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700805/**
806 * Chassis drives, this URL will show all the DriveCollection
807 * information
808 */
Nan Zhoub53dcd92022-06-21 17:47:50 +0000809inline void chassisDriveCollectionGet(
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700810 crow::App& app, const crow::Request& req,
811 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
812 const std::string& chassisId)
813{
Carson Labrado3ba00072022-06-06 19:40:56 +0000814 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700815 {
816 return;
817 }
818
819 // mapper call lambda
George Liue99073f2022-12-09 11:06:16 +0800820 dbus::utility::getSubTree(
Myung Bae3f95a272024-03-13 07:32:02 -0700821 "/xyz/openbmc_project/inventory", 0, chassisInterfaces,
Ed Tanous36d52332023-06-09 13:18:40 -0700822 std::bind_front(afterChassisDriveCollectionSubtreeGet, asyncResp,
823 chassisId));
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700824}
825
826inline void requestRoutesChassisDrive(App& app)
827{
828 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
829 .privileges(redfish::privileges::getDriveCollection)
830 .methods(boost::beast::http::verb::get)(
831 std::bind_front(chassisDriveCollectionGet, std::ref(app)));
832}
833
Nan Zhoub53dcd92022-06-21 17:47:50 +0000834inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
835 const std::string& chassisId,
836 const std::string& driveName,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800837 const boost::system::error_code& ec,
Nan Zhoub53dcd92022-06-21 17:47:50 +0000838 const dbus::utility::MapperGetSubTreeResponse& subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700839{
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700840 if (ec)
841 {
Ed Tanous62598e32023-07-17 17:06:25 -0700842 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700843 messages::internalError(asyncResp->res);
844 return;
845 }
846
847 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000848 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700849 {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700850 sdbusplus::message::object_path objPath(path);
851 if (objPath.filename() != driveName)
852 {
853 continue;
854 }
855
856 if (connectionNames.empty())
857 {
Ed Tanous62598e32023-07-17 17:06:25 -0700858 BMCWEB_LOG_ERROR("Got 0 Connection names");
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700859 continue;
860 }
861
Ed Tanousef4c65b2023-04-24 15:28:50 -0700862 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
863 "/redfish/v1/Chassis/{}/Drives/{}", chassisId, driveName);
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700864
865 asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
John Edward Broadbenta0cb40c2022-06-29 17:27:38 -0700866 asyncResp->res.jsonValue["Name"] = driveName;
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700867 asyncResp->res.jsonValue["Id"] = driveName;
868 // default it to Enabled
Ed Tanous539d8c62024-06-19 14:38:27 -0700869 asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700870
871 nlohmann::json::object_t linkChassisNav;
872 linkChassisNav["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700873 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700874 asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
875
876 addAllDriveInfo(asyncResp, connectionNames[0].first, path,
877 connectionNames[0].second);
878 }
879}
880
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400881inline void matchAndFillDrive(
882 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
883 const std::string& chassisId, const std::string& driveName,
884 const std::vector<std::string>& resp)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700885{
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700886 for (const std::string& drivePath : resp)
887 {
888 sdbusplus::message::object_path path(drivePath);
889 std::string leaf = path.filename();
890 if (leaf != driveName)
891 {
892 continue;
893 }
894 // mapper call drive
George Liue99073f2022-12-09 11:06:16 +0800895 constexpr std::array<std::string_view, 1> driveInterface = {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700896 "xyz.openbmc_project.Inventory.Item.Drive"};
George Liue99073f2022-12-09 11:06:16 +0800897 dbus::utility::getSubTree(
898 "/xyz/openbmc_project/inventory", 0, driveInterface,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700899 [asyncResp, chassisId, driveName](
George Liue99073f2022-12-09 11:06:16 +0800900 const boost::system::error_code& ec,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700901 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400902 buildDrive(asyncResp, chassisId, driveName, ec, subtree);
903 });
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700904 }
905}
906
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400907inline void handleChassisDriveGet(
908 crow::App& app, const crow::Request& req,
909 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
910 const std::string& chassisId, const std::string& driveName)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700911{
Michal Orzel03810a12022-06-15 14:04:28 +0200912 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700913 {
914 return;
915 }
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700916
917 // mapper call chassis
George Liue99073f2022-12-09 11:06:16 +0800918 dbus::utility::getSubTree(
Myung Bae3f95a272024-03-13 07:32:02 -0700919 "/xyz/openbmc_project/inventory", 0, chassisInterfaces,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700920 [asyncResp, chassisId,
George Liue99073f2022-12-09 11:06:16 +0800921 driveName](const boost::system::error_code& ec,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700922 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400923 if (ec)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700924 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400925 messages::internalError(asyncResp->res);
926 return;
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700927 }
928
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400929 // Iterate over all retrieved ObjectPaths.
930 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700931 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400932 sdbusplus::message::object_path objPath(path);
933 if (objPath.filename() != chassisId)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700934 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400935 continue;
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700936 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400937
938 if (connectionNames.empty())
939 {
940 BMCWEB_LOG_ERROR("Got 0 Connection names");
941 continue;
942 }
943
944 dbus::utility::getAssociationEndPoints(
945 path + "/drive",
946 [asyncResp, chassisId,
947 driveName](const boost::system::error_code& ec3,
948 const dbus::utility::MapperEndPoints& resp) {
949 if (ec3)
950 {
951 return; // no drives = no failures
952 }
953 matchAndFillDrive(asyncResp, chassisId, driveName,
954 resp);
955 });
956 break;
957 }
958 });
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700959}
960
961/**
962 * This URL will show the drive interface for the specific drive in the chassis
963 */
964inline void requestRoutesChassisDriveName(App& app)
965{
966 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
967 .privileges(redfish::privileges::getChassis)
968 .methods(boost::beast::http::verb::get)(
969 std::bind_front(handleChassisDriveGet, std::ref(app)));
970}
971
Willy Tu61b1eb22023-03-14 11:29:50 -0700972inline void getStorageControllerAsset(
973 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
974 const boost::system::error_code& ec,
975 const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
976 propertiesList)
977{
978 if (ec)
979 {
980 // this interface isn't necessary
Ed Tanous62598e32023-07-17 17:06:25 -0700981 BMCWEB_LOG_DEBUG("Failed to get StorageControllerAsset");
Willy Tu61b1eb22023-03-14 11:29:50 -0700982 return;
983 }
984
985 const std::string* partNumber = nullptr;
986 const std::string* serialNumber = nullptr;
987 const std::string* manufacturer = nullptr;
988 const std::string* model = nullptr;
989 if (!sdbusplus::unpackPropertiesNoThrow(
990 dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
991 partNumber, "SerialNumber", serialNumber, "Manufacturer",
992 manufacturer, "Model", model))
993 {
994 messages::internalError(asyncResp->res);
995 return;
996 }
997
998 if (partNumber != nullptr)
999 {
1000 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
1001 }
1002
1003 if (serialNumber != nullptr)
1004 {
1005 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
1006 }
1007
1008 if (manufacturer != nullptr)
1009 {
1010 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
1011 }
1012
1013 if (model != nullptr)
1014 {
1015 asyncResp->res.jsonValue["Model"] = *model;
1016 }
1017}
1018
1019inline void populateStorageController(
1020 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1021 const std::string& controllerId, const std::string& connectionName,
1022 const std::string& path)
1023{
1024 asyncResp->res.jsonValue["@odata.type"] =
1025 "#StorageController.v1_6_0.StorageController";
Ed Tanous253f11b2024-05-16 09:38:31 -07001026 asyncResp->res.jsonValue["@odata.id"] =
1027 boost::urls::format("/redfish/v1/Systems/{}/Storage/1/Controllers/{}",
1028 BMCWEB_REDFISH_SYSTEM_URI_NAME, controllerId);
Willy Tu61b1eb22023-03-14 11:29:50 -07001029 asyncResp->res.jsonValue["Name"] = controllerId;
1030 asyncResp->res.jsonValue["Id"] = controllerId;
Ed Tanous539d8c62024-06-19 14:38:27 -07001031 asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
Willy Tu61b1eb22023-03-14 11:29:50 -07001032
Ed Tanousdeae6a72024-11-11 21:58:57 -08001033 dbus::utility::getProperty<bool>(
1034 connectionName, path, "xyz.openbmc_project.Inventory.Item", "Present",
Willy Tu61b1eb22023-03-14 11:29:50 -07001035 [asyncResp](const boost::system::error_code& ec, bool isPresent) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001036 // this interface isn't necessary, only check it
1037 // if we get a good return
1038 if (ec)
1039 {
1040 BMCWEB_LOG_DEBUG("Failed to get Present property");
1041 return;
1042 }
1043 if (!isPresent)
1044 {
1045 asyncResp->res.jsonValue["Status"]["State"] =
1046 resource::State::Absent;
1047 }
1048 });
Willy Tu61b1eb22023-03-14 11:29:50 -07001049
Ed Tanousdeae6a72024-11-11 21:58:57 -08001050 dbus::utility::getAllProperties(
1051 connectionName, path, "xyz.openbmc_project.Inventory.Decorator.Asset",
Willy Tu61b1eb22023-03-14 11:29:50 -07001052 [asyncResp](const boost::system::error_code& ec,
1053 const std::vector<
1054 std::pair<std::string, dbus::utility::DbusVariantType>>&
1055 propertiesList) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001056 getStorageControllerAsset(asyncResp, ec, propertiesList);
1057 });
Willy Tu61b1eb22023-03-14 11:29:50 -07001058}
1059
1060inline void getStorageControllerHandler(
1061 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1062 const std::string& controllerId, const boost::system::error_code& ec,
1063 const dbus::utility::MapperGetSubTreeResponse& subtree)
1064{
1065 if (ec || subtree.empty())
1066 {
1067 // doesn't have to be there
Ed Tanous62598e32023-07-17 17:06:25 -07001068 BMCWEB_LOG_DEBUG("Failed to handle StorageController");
Willy Tu61b1eb22023-03-14 11:29:50 -07001069 return;
1070 }
1071
1072 for (const auto& [path, interfaceDict] : subtree)
1073 {
1074 sdbusplus::message::object_path object(path);
1075 std::string id = object.filename();
1076 if (id.empty())
1077 {
Ed Tanous62598e32023-07-17 17:06:25 -07001078 BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
Willy Tu61b1eb22023-03-14 11:29:50 -07001079 return;
1080 }
1081 if (id != controllerId)
1082 {
1083 continue;
1084 }
1085
1086 if (interfaceDict.size() != 1)
1087 {
Ed Tanous62598e32023-07-17 17:06:25 -07001088 BMCWEB_LOG_ERROR("Connection size {}, greater than 1",
1089 interfaceDict.size());
Willy Tu61b1eb22023-03-14 11:29:50 -07001090 messages::internalError(asyncResp->res);
1091 return;
1092 }
1093
1094 const std::string& connectionName = interfaceDict.front().first;
1095 populateStorageController(asyncResp, controllerId, connectionName,
1096 path);
1097 }
1098}
1099
1100inline void populateStorageControllerCollection(
1101 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1102 const boost::system::error_code& ec,
1103 const dbus::utility::MapperGetSubTreePathsResponse& controllerList)
1104{
1105 nlohmann::json::array_t members;
1106 if (ec || controllerList.empty())
1107 {
1108 asyncResp->res.jsonValue["Members"] = std::move(members);
1109 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous62598e32023-07-17 17:06:25 -07001110 BMCWEB_LOG_DEBUG("Failed to find any StorageController");
Willy Tu61b1eb22023-03-14 11:29:50 -07001111 return;
1112 }
1113
1114 for (const std::string& path : controllerList)
1115 {
1116 std::string id = sdbusplus::message::object_path(path).filename();
1117 if (id.empty())
1118 {
Ed Tanous62598e32023-07-17 17:06:25 -07001119 BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
Willy Tu61b1eb22023-03-14 11:29:50 -07001120 return;
1121 }
1122 nlohmann::json::object_t member;
1123 member["@odata.id"] = boost::urls::format(
Ed Tanous253f11b2024-05-16 09:38:31 -07001124 "/redfish/v1/Systems/{}/Storage/1/Controllers/{}",
1125 BMCWEB_REDFISH_SYSTEM_URI_NAME, id);
Willy Tu61b1eb22023-03-14 11:29:50 -07001126 members.emplace_back(member);
1127 }
1128 asyncResp->res.jsonValue["Members@odata.count"] = members.size();
1129 asyncResp->res.jsonValue["Members"] = std::move(members);
1130}
1131
Ed Tanous36d52332023-06-09 13:18:40 -07001132inline void handleSystemsStorageControllerCollectionGet(
Willy Tu61b1eb22023-03-14 11:29:50 -07001133 App& app, const crow::Request& req,
1134 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1135 const std::string& systemName)
1136{
1137 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1138 {
Ed Tanous62598e32023-07-17 17:06:25 -07001139 BMCWEB_LOG_DEBUG(
1140 "Failed to setup Redfish Route for StorageController Collection");
Willy Tu61b1eb22023-03-14 11:29:50 -07001141 return;
1142 }
Ed Tanous253f11b2024-05-16 09:38:31 -07001143 if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
Willy Tu61b1eb22023-03-14 11:29:50 -07001144 {
1145 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1146 systemName);
Ed Tanous62598e32023-07-17 17:06:25 -07001147 BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
Willy Tu61b1eb22023-03-14 11:29:50 -07001148 return;
1149 }
1150
1151 asyncResp->res.jsonValue["@odata.type"] =
1152 "#StorageControllerCollection.StorageControllerCollection";
1153 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001154 std::format("/redfish/v1/Systems/{}/Storage/1/Controllers",
1155 BMCWEB_REDFISH_SYSTEM_URI_NAME);
Willy Tu61b1eb22023-03-14 11:29:50 -07001156 asyncResp->res.jsonValue["Name"] = "Storage Controller Collection";
1157
1158 constexpr std::array<std::string_view, 1> interfaces = {
1159 "xyz.openbmc_project.Inventory.Item.StorageController"};
1160 dbus::utility::getSubTreePaths(
1161 "/xyz/openbmc_project/inventory", 0, interfaces,
1162 [asyncResp](const boost::system::error_code& ec,
1163 const dbus::utility::MapperGetSubTreePathsResponse&
1164 controllerList) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001165 populateStorageControllerCollection(asyncResp, ec, controllerList);
1166 });
Willy Tu61b1eb22023-03-14 11:29:50 -07001167}
1168
Ed Tanous36d52332023-06-09 13:18:40 -07001169inline void handleSystemsStorageControllerGet(
Willy Tu61b1eb22023-03-14 11:29:50 -07001170 App& app, const crow::Request& req,
1171 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1172 const std::string& systemName, const std::string& controllerId)
1173{
1174 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1175 {
Ed Tanous62598e32023-07-17 17:06:25 -07001176 BMCWEB_LOG_DEBUG("Failed to setup Redfish Route for StorageController");
Willy Tu61b1eb22023-03-14 11:29:50 -07001177 return;
1178 }
Ed Tanous253f11b2024-05-16 09:38:31 -07001179 if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
Willy Tu61b1eb22023-03-14 11:29:50 -07001180 {
1181 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1182 systemName);
Ed Tanous62598e32023-07-17 17:06:25 -07001183 BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
Willy Tu61b1eb22023-03-14 11:29:50 -07001184 return;
1185 }
1186 constexpr std::array<std::string_view, 1> interfaces = {
1187 "xyz.openbmc_project.Inventory.Item.StorageController"};
1188 dbus::utility::getSubTree(
1189 "/xyz/openbmc_project/inventory", 0, interfaces,
1190 [asyncResp,
1191 controllerId](const boost::system::error_code& ec,
1192 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001193 getStorageControllerHandler(asyncResp, controllerId, ec, subtree);
1194 });
Willy Tu61b1eb22023-03-14 11:29:50 -07001195}
1196
1197inline void requestRoutesStorageControllerCollection(App& app)
1198{
1199 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/")
1200 .privileges(redfish::privileges::getStorageControllerCollection)
Ed Tanous36d52332023-06-09 13:18:40 -07001201 .methods(boost::beast::http::verb::get)(std::bind_front(
1202 handleSystemsStorageControllerCollectionGet, std::ref(app)));
Willy Tu61b1eb22023-03-14 11:29:50 -07001203}
1204
1205inline void requestRoutesStorageController(App& app)
1206{
1207 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/<str>")
1208 .privileges(redfish::privileges::getStorageController)
1209 .methods(boost::beast::http::verb::get)(
Ed Tanous36d52332023-06-09 13:18:40 -07001210 std::bind_front(handleSystemsStorageControllerGet, std::ref(app)));
Willy Tu61b1eb22023-03-14 11:29:50 -07001211}
1212
Nikhil Potadea25aecc2019-08-23 16:35:26 -07001213} // namespace redfish