blob: cb5e752a50141caa378e3c9a454a2c6ca719f546 [file] [log] [blame]
Nikhil Potadea25aecc2019-08-23 16:35:26 -07001/*
2// Copyright (c) 2019 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Willy Tu13451e32023-05-24 16:08:18 -070018#include "bmcweb_config.h"
19
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "app.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080021#include "dbus_utility.hpp"
John Edward Broadbente5029d82022-06-08 14:35:21 -070022#include "generated/enums/drive.hpp"
George Liudde9bc12023-02-22 09:35:51 +080023#include "generated/enums/protocol.hpp"
Ed Tanous539d8c62024-06-19 14:38:27 -070024#include "generated/enums/resource.hpp"
Ed Tanousa8e884f2023-01-13 17:40:03 -080025#include "human_sort.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080026#include "query.hpp"
Ed Tanousa8e884f2023-01-13 17:40:03 -080027#include "redfish_util.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080028#include "registries/privilege_registry.hpp"
Willy Tu5e577bc2022-07-26 00:41:55 +000029#include "utils/collection.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080030#include "utils/dbus_utils.hpp"
James Feist2ad9c2f2019-10-29 16:26:48 -070031
George Liue99073f2022-12-09 11:06:16 +080032#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070033#include <boost/url/format.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070034#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020035#include <sdbusplus/unpack_properties.hpp>
Nikhil Potadea25aecc2019-08-23 16:35:26 -070036
George Liu7a1dbc42022-12-07 16:03:22 +080037#include <array>
Ed Tanous3544d2a2023-08-06 18:12:20 -070038#include <ranges>
George Liu7a1dbc42022-12-07 16:03:22 +080039#include <string_view>
40
Nikhil Potadea25aecc2019-08-23 16:35:26 -070041namespace redfish
42{
Ed Tanous36d52332023-06-09 13:18:40 -070043
44inline void handleSystemsStorageCollectionGet(
45 App& app, const crow::Request& req,
46 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
47 const std::string& systemName)
48{
49 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
50 {
51 return;
52 }
Ed Tanous253f11b2024-05-16 09:38:31 -070053 if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
Ed Tanous36d52332023-06-09 13:18:40 -070054 {
55 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
56 systemName);
57 return;
58 }
59
60 asyncResp->res.jsonValue["@odata.type"] =
61 "#StorageCollection.StorageCollection";
Ed Tanous253f11b2024-05-16 09:38:31 -070062 asyncResp->res.jsonValue["@odata.id"] = std::format(
63 "/redfish/v1/Systems/{}/Storage", BMCWEB_REDFISH_SYSTEM_URI_NAME);
Ed Tanous36d52332023-06-09 13:18:40 -070064 asyncResp->res.jsonValue["Name"] = "Storage Collection";
Willy Tu5e577bc2022-07-26 00:41:55 +000065
Patrick Williams5a39f772023-10-20 11:20:21 -050066 constexpr std::array<std::string_view, 1> interface{
67 "xyz.openbmc_project.Inventory.Item.Storage"};
Willy Tu5e577bc2022-07-26 00:41:55 +000068 collection_util::getCollectionMembers(
Ed Tanous253f11b2024-05-16 09:38:31 -070069 asyncResp,
70 boost::urls::format("/redfish/v1/Systems/{}/Storage",
71 BMCWEB_REDFISH_SYSTEM_URI_NAME),
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050072 interface, "/xyz/openbmc_project/inventory");
Willy Tu5e577bc2022-07-26 00:41:55 +000073}
74
75inline void handleStorageCollectionGet(
76 App& app, const crow::Request& req,
77 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
78{
79 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
80 {
81 return;
82 }
83 asyncResp->res.jsonValue["@odata.type"] =
84 "#StorageCollection.StorageCollection";
85 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Storage";
86 asyncResp->res.jsonValue["Name"] = "Storage Collection";
Patrick Williams5a39f772023-10-20 11:20:21 -050087 constexpr std::array<std::string_view, 1> interface{
88 "xyz.openbmc_project.Inventory.Item.Storage"};
Willy Tu5e577bc2022-07-26 00:41:55 +000089 collection_util::getCollectionMembers(
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050090 asyncResp, boost::urls::format("/redfish/v1/Storage"), interface,
91 "/xyz/openbmc_project/inventory");
Ed Tanous36d52332023-06-09 13:18:40 -070092}
93
John Edward Broadbent7e860f12021-04-08 15:57:16 -070094inline void requestRoutesStorageCollection(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -070095{
Ed Tanous22d268c2022-05-19 09:39:07 -070096 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
Ed Tanoused398212021-06-09 17:05:54 -070097 .privileges(redfish::privileges::getStorageCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070098 .methods(boost::beast::http::verb::get)(
Ed Tanous36d52332023-06-09 13:18:40 -070099 std::bind_front(handleSystemsStorageCollectionGet, std::ref(app)));
Willy Tu5e577bc2022-07-26 00:41:55 +0000100 BMCWEB_ROUTE(app, "/redfish/v1/Storage/")
101 .privileges(redfish::privileges::getStorageCollection)
102 .methods(boost::beast::http::verb::get)(
103 std::bind_front(handleStorageCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700104}
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700105
Ed Tanous36d52332023-06-09 13:18:40 -0700106inline void afterChassisDriveCollectionSubtree(
107 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous36d52332023-06-09 13:18:40 -0700108 const boost::system::error_code& ec,
109 const dbus::utility::MapperGetSubTreePathsResponse& driveList)
110{
111 if (ec)
112 {
Ed Tanous62598e32023-07-17 17:06:25 -0700113 BMCWEB_LOG_ERROR("Drive mapper call error");
Ed Tanous36d52332023-06-09 13:18:40 -0700114 messages::internalError(asyncResp->res);
115 return;
116 }
117
118 nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
119 driveArray = nlohmann::json::array();
120 auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
121 count = 0;
122
Ed Tanous36d52332023-06-09 13:18:40 -0700123 for (const std::string& drive : driveList)
124 {
125 sdbusplus::message::object_path object(drive);
126 if (object.filename().empty())
127 {
Ed Tanous62598e32023-07-17 17:06:25 -0700128 BMCWEB_LOG_ERROR("Failed to find filename in {}", drive);
Ed Tanous36d52332023-06-09 13:18:40 -0700129 return;
130 }
131
132 nlohmann::json::object_t driveJson;
133 driveJson["@odata.id"] = boost::urls::format(
Ed Tanous253f11b2024-05-16 09:38:31 -0700134 "/redfish/v1/Systems/{}/Storage/1/Drives/{}",
135 BMCWEB_REDFISH_SYSTEM_URI_NAME, object.filename());
Ed Tanous36d52332023-06-09 13:18:40 -0700136 driveArray.emplace_back(std::move(driveJson));
137 }
138
139 count = driveArray.size();
140}
Gunnar Mills7ac13cc2024-04-01 16:05:21 -0500141inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Willy Tua85afbe2021-12-28 14:43:47 -0800142{
George Liu7a1dbc42022-12-07 16:03:22 +0800143 const std::array<std::string_view, 1> interfaces = {
144 "xyz.openbmc_project.Inventory.Item.Drive"};
145 dbus::utility::getSubTreePaths(
146 "/xyz/openbmc_project/inventory", 0, interfaces,
Gunnar Mills7ac13cc2024-04-01 16:05:21 -0500147 std::bind_front(afterChassisDriveCollectionSubtree, asyncResp));
Ed Tanous36d52332023-06-09 13:18:40 -0700148}
Willy Tua85afbe2021-12-28 14:43:47 -0800149
Willy Tu5e577bc2022-07-26 00:41:55 +0000150inline void afterSystemsStorageGetSubtree(
151 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
152 const std::string& storageId, const boost::system::error_code& ec,
153 const dbus::utility::MapperGetSubTreeResponse& subtree)
Ed Tanous36d52332023-06-09 13:18:40 -0700154{
Willy Tu5e577bc2022-07-26 00:41:55 +0000155 if (ec)
Ed Tanous36d52332023-06-09 13:18:40 -0700156 {
Ed Tanous62598e32023-07-17 17:06:25 -0700157 BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
Willy Tu5e577bc2022-07-26 00:41:55 +0000158 messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
159 storageId);
Ed Tanous36d52332023-06-09 13:18:40 -0700160 return;
161 }
Ed Tanous3544d2a2023-08-06 18:12:20 -0700162 auto storage = std::ranges::find_if(
163 subtree,
Willy Tu5e577bc2022-07-26 00:41:55 +0000164 [&storageId](const std::pair<std::string,
165 dbus::utility::MapperServiceMap>& object) {
166 return sdbusplus::message::object_path(object.first).filename() ==
167 storageId;
Patrick Williams5a39f772023-10-20 11:20:21 -0500168 });
Willy Tu5e577bc2022-07-26 00:41:55 +0000169 if (storage == subtree.end())
170 {
171 messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
172 storageId);
173 return;
174 }
175
Ed Tanous36d52332023-06-09 13:18:40 -0700176 asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
177 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -0700178 boost::urls::format("/redfish/v1/Systems/{}/Storage/{}",
179 BMCWEB_REDFISH_SYSTEM_URI_NAME, storageId);
Ed Tanous36d52332023-06-09 13:18:40 -0700180 asyncResp->res.jsonValue["Name"] = "Storage";
Willy Tu5e577bc2022-07-26 00:41:55 +0000181 asyncResp->res.jsonValue["Id"] = storageId;
Ed Tanous539d8c62024-06-19 14:38:27 -0700182 asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
Willy Tua85afbe2021-12-28 14:43:47 -0800183
Gunnar Mills7ac13cc2024-04-01 16:05:21 -0500184 getDrives(asyncResp);
Ed Tanous253f11b2024-05-16 09:38:31 -0700185 asyncResp->res.jsonValue["Controllers"]["@odata.id"] =
186 boost::urls::format("/redfish/v1/Systems/{}/Storage/{}/Controllers",
187 BMCWEB_REDFISH_SYSTEM_URI_NAME, storageId);
Willy Tu5e577bc2022-07-26 00:41:55 +0000188}
189
190inline void
191 handleSystemsStorageGet(App& app, const crow::Request& req,
192 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800193 const std::string& systemName,
Willy Tu5e577bc2022-07-26 00:41:55 +0000194 const std::string& storageId)
195{
196 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
197 {
198 return;
199 }
Ed Tanous25b54db2024-04-17 15:40:31 -0700200 if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800201 {
202 // Option currently returns no systems. TBD
203 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
204 systemName);
205 return;
206 }
Willy Tu5e577bc2022-07-26 00:41:55 +0000207
208 constexpr std::array<std::string_view, 1> interfaces = {
209 "xyz.openbmc_project.Inventory.Item.Storage"};
210 dbus::utility::getSubTree(
211 "/xyz/openbmc_project/inventory", 0, interfaces,
212 std::bind_front(afterSystemsStorageGetSubtree, asyncResp, storageId));
213}
214
215inline void afterSubtree(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
216 const std::string& storageId,
217 const boost::system::error_code& ec,
218 const dbus::utility::MapperGetSubTreeResponse& subtree)
219{
220 if (ec)
221 {
Ed Tanous62598e32023-07-17 17:06:25 -0700222 BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
Willy Tu5e577bc2022-07-26 00:41:55 +0000223 messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
224 storageId);
225 return;
226 }
Ed Tanous3544d2a2023-08-06 18:12:20 -0700227 auto storage = std::ranges::find_if(
228 subtree,
Willy Tu5e577bc2022-07-26 00:41:55 +0000229 [&storageId](const std::pair<std::string,
230 dbus::utility::MapperServiceMap>& object) {
231 return sdbusplus::message::object_path(object.first).filename() ==
232 storageId;
Patrick Williams5a39f772023-10-20 11:20:21 -0500233 });
Willy Tu5e577bc2022-07-26 00:41:55 +0000234 if (storage == subtree.end())
235 {
236 messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
237 storageId);
238 return;
239 }
240
241 asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
242 asyncResp->res.jsonValue["@odata.id"] =
243 boost::urls::format("/redfish/v1/Storage/{}", storageId);
244 asyncResp->res.jsonValue["Name"] = "Storage";
245 asyncResp->res.jsonValue["Id"] = storageId;
Ed Tanous539d8c62024-06-19 14:38:27 -0700246 asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
Willy Tu5e577bc2022-07-26 00:41:55 +0000247
248 // Storage subsystem to Storage link.
249 nlohmann::json::array_t storageServices;
250 nlohmann::json::object_t storageService;
251 storageService["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -0700252 boost::urls::format("/redfish/v1/Systems/{}/Storage/{}",
253 BMCWEB_REDFISH_SYSTEM_URI_NAME, storageId);
Willy Tu5e577bc2022-07-26 00:41:55 +0000254 storageServices.emplace_back(storageService);
255 asyncResp->res.jsonValue["Links"]["StorageServices"] =
256 std::move(storageServices);
257 asyncResp->res.jsonValue["Links"]["StorageServices@odata.count"] = 1;
258}
259
260inline void
261 handleStorageGet(App& app, const crow::Request& req,
262 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
263 const std::string& storageId)
264{
265 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
266 {
Ed Tanous62598e32023-07-17 17:06:25 -0700267 BMCWEB_LOG_DEBUG("requestRoutesStorage setUpRedfishRoute failed");
Willy Tu5e577bc2022-07-26 00:41:55 +0000268 return;
269 }
270
271 constexpr std::array<std::string_view, 1> interfaces = {
272 "xyz.openbmc_project.Inventory.Item.Storage"};
273 dbus::utility::getSubTree(
274 "/xyz/openbmc_project/inventory", 0, interfaces,
275 std::bind_front(afterSubtree, asyncResp, storageId));
Willy Tua85afbe2021-12-28 14:43:47 -0800276}
277
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700278inline void requestRoutesStorage(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700279{
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800280 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700281 .privileges(redfish::privileges::getStorage)
Ed Tanous002d39b2022-05-31 08:59:27 -0700282 .methods(boost::beast::http::verb::get)(
Ed Tanous36d52332023-06-09 13:18:40 -0700283 std::bind_front(handleSystemsStorageGet, std::ref(app)));
Willy Tu5e577bc2022-07-26 00:41:55 +0000284
285 BMCWEB_ROUTE(app, "/redfish/v1/Storage/<str>/")
286 .privileges(redfish::privileges::getStorage)
287 .methods(boost::beast::http::verb::get)(
288 std::bind_front(handleStorageGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700289}
290
Willy Tu03913172021-11-08 02:03:19 -0800291inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
292 const std::string& connectionName,
293 const std::string& path)
294{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200295 sdbusplus::asio::getAllProperties(
296 *crow::connections::systemBus, connectionName, path,
297 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800298 [asyncResp](const boost::system::error_code& ec,
Ed Tanous168e20c2021-12-13 14:39:53 -0800299 const std::vector<
300 std::pair<std::string, dbus::utility::DbusVariantType>>&
301 propertiesList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700302 if (ec)
303 {
304 // this interface isn't necessary
305 return;
306 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200307
308 const std::string* partNumber = nullptr;
309 const std::string* serialNumber = nullptr;
310 const std::string* manufacturer = nullptr;
311 const std::string* model = nullptr;
312
313 const bool success = sdbusplus::unpackPropertiesNoThrow(
314 dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
315 partNumber, "SerialNumber", serialNumber, "Manufacturer",
316 manufacturer, "Model", model);
317
318 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700319 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200320 messages::internalError(asyncResp->res);
321 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700322 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200323
324 if (partNumber != nullptr)
325 {
326 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
327 }
328
329 if (serialNumber != nullptr)
330 {
331 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
332 }
333
334 if (manufacturer != nullptr)
335 {
336 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
337 }
338
339 if (model != nullptr)
340 {
341 asyncResp->res.jsonValue["Model"] = *model;
342 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500343 });
Willy Tu03913172021-11-08 02:03:19 -0800344}
345
346inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
347 const std::string& connectionName,
348 const std::string& path)
349{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700350 sdbusplus::asio::getProperty<bool>(
351 *crow::connections::systemBus, connectionName, path,
352 "xyz.openbmc_project.Inventory.Item", "Present",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800353 [asyncResp, path](const boost::system::error_code& ec,
Willy Tucef57e82022-12-15 16:42:02 -0800354 const bool isPresent) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700355 // this interface isn't necessary, only check it if
356 // we get a good return
357 if (ec)
358 {
359 return;
360 }
Willy Tu03913172021-11-08 02:03:19 -0800361
Willy Tucef57e82022-12-15 16:42:02 -0800362 if (!isPresent)
Ed Tanous002d39b2022-05-31 08:59:27 -0700363 {
Ed Tanous539d8c62024-06-19 14:38:27 -0700364 asyncResp->res.jsonValue["Status"]["State"] =
365 resource::State::Absent;
Ed Tanous002d39b2022-05-31 08:59:27 -0700366 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500367 });
Willy Tu03913172021-11-08 02:03:19 -0800368}
369
370inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
371 const std::string& connectionName,
372 const std::string& path)
373{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700374 sdbusplus::asio::getProperty<bool>(
375 *crow::connections::systemBus, connectionName, path,
376 "xyz.openbmc_project.State.Drive", "Rebuilding",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800377 [asyncResp](const boost::system::error_code& ec, const bool updating) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700378 // 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
Ed Tanous002d39b2022-05-31 08:59:27 -0700385 // 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 {
Ed Tanous539d8c62024-06-19 14:38:27 -0700391 asyncResp->res.jsonValue["Status"]["State"] =
392 resource::State::Updating;
Ed Tanous002d39b2022-05-31 08:59:27 -0700393 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500394 });
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
George Liudde9bc12023-02-22 09:35:51 +0800415inline std::optional<protocol::Protocol>
416 convertDriveProtocol(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
443inline void
444 getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
445 const std::string& connectionName,
446 const std::string& path)
447{
448 sdbusplus::asio::getAllProperties(
449 *crow::connections::systemBus, connectionName, path,
450 "xyz.openbmc_project.Inventory.Item.Drive",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800451 [asyncResp](const boost::system::error_code& ec,
Willy Tu19b8e9a2021-11-08 02:55:03 -0800452 const std::vector<
453 std::pair<std::string, dbus::utility::DbusVariantType>>&
454 propertiesList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700455 if (ec)
456 {
457 // this interface isn't required
458 return;
459 }
John Edward Broadbente5029d82022-06-08 14:35:21 -0700460 const std::string* encryptionStatus = nullptr;
461 const bool* isLocked = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -0700462 for (const std::pair<std::string, dbus::utility::DbusVariantType>&
463 property : propertiesList)
464 {
465 const std::string& propertyName = property.first;
466 if (propertyName == "Type")
Willy Tu19b8e9a2021-11-08 02:55:03 -0800467 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700468 const std::string* value =
469 std::get_if<std::string>(&property.second);
470 if (value == nullptr)
471 {
472 // illegal property
Ed Tanous62598e32023-07-17 17:06:25 -0700473 BMCWEB_LOG_ERROR("Illegal property: Type");
Ed Tanous002d39b2022-05-31 08:59:27 -0700474 messages::internalError(asyncResp->res);
475 return;
476 }
477
George Liudde9bc12023-02-22 09:35:51 +0800478 std::optional<drive::MediaType> mediaType =
479 convertDriveType(*value);
Ed Tanous002d39b2022-05-31 08:59:27 -0700480 if (!mediaType)
481 {
Ed Tanous62598e32023-07-17 17:06:25 -0700482 BMCWEB_LOG_WARNING("UnknownDriveType Interface: {}",
483 *value);
George Liudde9bc12023-02-22 09:35:51 +0800484 continue;
485 }
486 if (*mediaType == drive::MediaType::Invalid)
487 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700488 messages::internalError(asyncResp->res);
489 return;
490 }
491
492 asyncResp->res.jsonValue["MediaType"] = *mediaType;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800493 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700494 else if (propertyName == "Capacity")
Willy Tu19b8e9a2021-11-08 02:55:03 -0800495 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700496 const uint64_t* capacity =
497 std::get_if<uint64_t>(&property.second);
498 if (capacity == nullptr)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800499 {
Ed Tanous62598e32023-07-17 17:06:25 -0700500 BMCWEB_LOG_ERROR("Illegal property: Capacity");
Ed Tanous002d39b2022-05-31 08:59:27 -0700501 messages::internalError(asyncResp->res);
502 return;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800503 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700504 if (*capacity == 0)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800505 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700506 // drive capacity not known
507 continue;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800508 }
Willy Tu19b8e9a2021-11-08 02:55:03 -0800509
Ed Tanous002d39b2022-05-31 08:59:27 -0700510 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800511 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700512 else if (propertyName == "Protocol")
513 {
514 const std::string* value =
515 std::get_if<std::string>(&property.second);
516 if (value == nullptr)
517 {
Ed Tanous62598e32023-07-17 17:06:25 -0700518 BMCWEB_LOG_ERROR("Illegal property: Protocol");
Ed Tanous002d39b2022-05-31 08:59:27 -0700519 messages::internalError(asyncResp->res);
520 return;
521 }
522
George Liudde9bc12023-02-22 09:35:51 +0800523 std::optional<protocol::Protocol> proto =
524 convertDriveProtocol(*value);
Ed Tanous002d39b2022-05-31 08:59:27 -0700525 if (!proto)
526 {
Ed Tanous62598e32023-07-17 17:06:25 -0700527 BMCWEB_LOG_WARNING("Unknown DrivePrototype Interface: {}",
528 *value);
George Liudde9bc12023-02-22 09:35:51 +0800529 continue;
530 }
531 if (*proto == protocol::Protocol::Invalid)
532 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700533 messages::internalError(asyncResp->res);
534 return;
535 }
536 asyncResp->res.jsonValue["Protocol"] = *proto;
537 }
John Edward Broadbent3fe4d5c2022-05-06 14:42:35 -0700538 else if (propertyName == "PredictedMediaLifeLeftPercent")
539 {
540 const uint8_t* lifeLeft =
541 std::get_if<uint8_t>(&property.second);
542 if (lifeLeft == nullptr)
543 {
Ed Tanous62598e32023-07-17 17:06:25 -0700544 BMCWEB_LOG_ERROR(
545 "Illegal property: PredictedMediaLifeLeftPercent");
John Edward Broadbent3fe4d5c2022-05-06 14:42:35 -0700546 messages::internalError(asyncResp->res);
547 return;
548 }
549 // 255 means reading the value is not supported
550 if (*lifeLeft != 255)
551 {
552 asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
553 *lifeLeft;
554 }
555 }
John Edward Broadbente5029d82022-06-08 14:35:21 -0700556 else if (propertyName == "EncryptionStatus")
557 {
558 encryptionStatus = std::get_if<std::string>(&property.second);
559 if (encryptionStatus == nullptr)
560 {
Ed Tanous62598e32023-07-17 17:06:25 -0700561 BMCWEB_LOG_ERROR("Illegal property: EncryptionStatus");
John Edward Broadbente5029d82022-06-08 14:35:21 -0700562 messages::internalError(asyncResp->res);
563 return;
564 }
565 }
566 else if (propertyName == "Locked")
567 {
568 isLocked = std::get_if<bool>(&property.second);
569 if (isLocked == nullptr)
570 {
Ed Tanous62598e32023-07-17 17:06:25 -0700571 BMCWEB_LOG_ERROR("Illegal property: Locked");
John Edward Broadbente5029d82022-06-08 14:35:21 -0700572 messages::internalError(asyncResp->res);
573 return;
574 }
575 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700576 }
John Edward Broadbente5029d82022-06-08 14:35:21 -0700577
578 if (encryptionStatus == nullptr || isLocked == nullptr ||
579 *encryptionStatus ==
Konda Reddy Kachanaa684c222024-01-09 02:04:37 +0000580 "xyz.openbmc_project.Inventory.Item.Drive.DriveEncryptionState.Unknown")
John Edward Broadbente5029d82022-06-08 14:35:21 -0700581 {
582 return;
583 }
584 if (*encryptionStatus !=
Konda Reddy Kachanaa684c222024-01-09 02:04:37 +0000585 "xyz.openbmc_project.Inventory.Item.Drive.DriveEncryptionState.Encrypted")
John Edward Broadbente5029d82022-06-08 14:35:21 -0700586 {
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."
603 asyncResp->res.jsonValue["EncryptionStatus"] =
604 drive::EncryptionStatus::Unlocked;
Patrick Williams5a39f772023-10-20 11:20:21 -0500605 });
Willy Tu19b8e9a2021-11-08 02:55:03 -0800606}
607
Nan Zhoub53dcd92022-06-21 17:47:50 +0000608static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
609 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) {
650 return sdbusplus::message::object_path(object.first).filename() ==
651 driveId;
Patrick Williams5a39f772023-10-20 11:20:21 -0500652 });
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
678 getMainChassisId(asyncResp,
679 [](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 });
684
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) {
774 if (ec3)
775 {
Ed Tanous62598e32023-07-17 17:06:25 -0700776 BMCWEB_LOG_ERROR("Error in chassis Drive association ");
Ed Tanous36d52332023-06-09 13:18:40 -0700777 }
778 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
779 // important if array is empty
780 members = nlohmann::json::array();
781
782 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 }
788
Ed Tanous3544d2a2023-08-06 18:12:20 -0700789 std::ranges::sort(leafNames, AlphanumLess<std::string>());
Ed Tanous36d52332023-06-09 13:18:40 -0700790
791 for (const auto& leafName : leafNames)
792 {
793 nlohmann::json::object_t member;
794 member["@odata.id"] = boost::urls::format(
795 "/redfish/v1/Chassis/{}/Drives/{}", chassisId, leafName);
796 members.emplace_back(std::move(member));
797 // navigation links will be registered in next patch set
798 }
799 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
Patrick Williams5a39f772023-10-20 11:20:21 -0500800 }); // end association lambda
Ed Tanous36d52332023-06-09 13:18:40 -0700801
Patrick Williams5a39f772023-10-20 11:20:21 -0500802 } // end Iterate over all retrieved ObjectPaths
Ed Tanous36d52332023-06-09 13:18:40 -0700803}
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700804/**
805 * Chassis drives, this URL will show all the DriveCollection
806 * information
807 */
Nan Zhoub53dcd92022-06-21 17:47:50 +0000808inline void chassisDriveCollectionGet(
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700809 crow::App& app, const crow::Request& req,
810 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
811 const std::string& chassisId)
812{
Carson Labrado3ba00072022-06-06 19:40:56 +0000813 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700814 {
815 return;
816 }
817
818 // mapper call lambda
George Liue99073f2022-12-09 11:06:16 +0800819 constexpr std::array<std::string_view, 2> interfaces = {
820 "xyz.openbmc_project.Inventory.Item.Board",
821 "xyz.openbmc_project.Inventory.Item.Chassis"};
822 dbus::utility::getSubTree(
823 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous36d52332023-06-09 13:18:40 -0700824 std::bind_front(afterChassisDriveCollectionSubtreeGet, asyncResp,
825 chassisId));
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700826}
827
828inline void requestRoutesChassisDrive(App& app)
829{
830 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
831 .privileges(redfish::privileges::getDriveCollection)
832 .methods(boost::beast::http::verb::get)(
833 std::bind_front(chassisDriveCollectionGet, std::ref(app)));
834}
835
Nan Zhoub53dcd92022-06-21 17:47:50 +0000836inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
837 const std::string& chassisId,
838 const std::string& driveName,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800839 const boost::system::error_code& ec,
Nan Zhoub53dcd92022-06-21 17:47:50 +0000840 const dbus::utility::MapperGetSubTreeResponse& subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700841{
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700842 if (ec)
843 {
Ed Tanous62598e32023-07-17 17:06:25 -0700844 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700845 messages::internalError(asyncResp->res);
846 return;
847 }
848
849 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000850 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700851 {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700852 sdbusplus::message::object_path objPath(path);
853 if (objPath.filename() != driveName)
854 {
855 continue;
856 }
857
858 if (connectionNames.empty())
859 {
Ed Tanous62598e32023-07-17 17:06:25 -0700860 BMCWEB_LOG_ERROR("Got 0 Connection names");
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700861 continue;
862 }
863
Ed Tanousef4c65b2023-04-24 15:28:50 -0700864 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
865 "/redfish/v1/Chassis/{}/Drives/{}", chassisId, driveName);
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700866
867 asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
John Edward Broadbenta0cb40c2022-06-29 17:27:38 -0700868 asyncResp->res.jsonValue["Name"] = driveName;
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700869 asyncResp->res.jsonValue["Id"] = driveName;
870 // default it to Enabled
Ed Tanous539d8c62024-06-19 14:38:27 -0700871 asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700872
873 nlohmann::json::object_t linkChassisNav;
874 linkChassisNav["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700875 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700876 asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
877
878 addAllDriveInfo(asyncResp, connectionNames[0].first, path,
879 connectionNames[0].second);
880 }
881}
882
Nan Zhoub53dcd92022-06-21 17:47:50 +0000883inline void
884 matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
885 const std::string& chassisId,
886 const std::string& driveName,
887 const std::vector<std::string>& resp)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700888{
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700889 for (const std::string& drivePath : resp)
890 {
891 sdbusplus::message::object_path path(drivePath);
892 std::string leaf = path.filename();
893 if (leaf != driveName)
894 {
895 continue;
896 }
897 // mapper call drive
George Liue99073f2022-12-09 11:06:16 +0800898 constexpr std::array<std::string_view, 1> driveInterface = {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700899 "xyz.openbmc_project.Inventory.Item.Drive"};
George Liue99073f2022-12-09 11:06:16 +0800900 dbus::utility::getSubTree(
901 "/xyz/openbmc_project/inventory", 0, driveInterface,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700902 [asyncResp, chassisId, driveName](
George Liue99073f2022-12-09 11:06:16 +0800903 const boost::system::error_code& ec,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700904 const dbus::utility::MapperGetSubTreeResponse& subtree) {
905 buildDrive(asyncResp, chassisId, driveName, ec, subtree);
Patrick Williams5a39f772023-10-20 11:20:21 -0500906 });
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700907 }
908}
909
Nan Zhoub53dcd92022-06-21 17:47:50 +0000910inline void
911 handleChassisDriveGet(crow::App& app, const crow::Request& req,
912 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
913 const std::string& chassisId,
914 const std::string& driveName)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700915{
Michal Orzel03810a12022-06-15 14:04:28 +0200916 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700917 {
918 return;
919 }
George Liue99073f2022-12-09 11:06:16 +0800920 constexpr std::array<std::string_view, 2> interfaces = {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700921 "xyz.openbmc_project.Inventory.Item.Board",
922 "xyz.openbmc_project.Inventory.Item.Chassis"};
923
924 // mapper call chassis
George Liue99073f2022-12-09 11:06:16 +0800925 dbus::utility::getSubTree(
926 "/xyz/openbmc_project/inventory", 0, interfaces,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700927 [asyncResp, chassisId,
George Liue99073f2022-12-09 11:06:16 +0800928 driveName](const boost::system::error_code& ec,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700929 const dbus::utility::MapperGetSubTreeResponse& subtree) {
930 if (ec)
931 {
932 messages::internalError(asyncResp->res);
933 return;
934 }
935
936 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000937 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700938 {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700939 sdbusplus::message::object_path objPath(path);
940 if (objPath.filename() != chassisId)
941 {
942 continue;
943 }
944
945 if (connectionNames.empty())
946 {
Ed Tanous62598e32023-07-17 17:06:25 -0700947 BMCWEB_LOG_ERROR("Got 0 Connection names");
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700948 continue;
949 }
950
George Liu6c3e9452023-03-03 13:55:29 +0800951 dbus::utility::getAssociationEndPoints(
952 path + "/drive",
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700953 [asyncResp, chassisId,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800954 driveName](const boost::system::error_code& ec3,
George Liu6c3e9452023-03-03 13:55:29 +0800955 const dbus::utility::MapperEndPoints& resp) {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700956 if (ec3)
957 {
958 return; // no drives = no failures
959 }
960 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
Patrick Williams5a39f772023-10-20 11:20:21 -0500961 });
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700962 break;
963 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500964 });
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700965}
966
967/**
968 * This URL will show the drive interface for the specific drive in the chassis
969 */
970inline void requestRoutesChassisDriveName(App& app)
971{
972 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
973 .privileges(redfish::privileges::getChassis)
974 .methods(boost::beast::http::verb::get)(
975 std::bind_front(handleChassisDriveGet, std::ref(app)));
976}
977
Willy Tu61b1eb22023-03-14 11:29:50 -0700978inline void getStorageControllerAsset(
979 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
980 const boost::system::error_code& ec,
981 const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
982 propertiesList)
983{
984 if (ec)
985 {
986 // this interface isn't necessary
Ed Tanous62598e32023-07-17 17:06:25 -0700987 BMCWEB_LOG_DEBUG("Failed to get StorageControllerAsset");
Willy Tu61b1eb22023-03-14 11:29:50 -0700988 return;
989 }
990
991 const std::string* partNumber = nullptr;
992 const std::string* serialNumber = nullptr;
993 const std::string* manufacturer = nullptr;
994 const std::string* model = nullptr;
995 if (!sdbusplus::unpackPropertiesNoThrow(
996 dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
997 partNumber, "SerialNumber", serialNumber, "Manufacturer",
998 manufacturer, "Model", model))
999 {
1000 messages::internalError(asyncResp->res);
1001 return;
1002 }
1003
1004 if (partNumber != nullptr)
1005 {
1006 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
1007 }
1008
1009 if (serialNumber != nullptr)
1010 {
1011 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
1012 }
1013
1014 if (manufacturer != nullptr)
1015 {
1016 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
1017 }
1018
1019 if (model != nullptr)
1020 {
1021 asyncResp->res.jsonValue["Model"] = *model;
1022 }
1023}
1024
1025inline void populateStorageController(
1026 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1027 const std::string& controllerId, const std::string& connectionName,
1028 const std::string& path)
1029{
1030 asyncResp->res.jsonValue["@odata.type"] =
1031 "#StorageController.v1_6_0.StorageController";
Ed Tanous253f11b2024-05-16 09:38:31 -07001032 asyncResp->res.jsonValue["@odata.id"] =
1033 boost::urls::format("/redfish/v1/Systems/{}/Storage/1/Controllers/{}",
1034 BMCWEB_REDFISH_SYSTEM_URI_NAME, controllerId);
Willy Tu61b1eb22023-03-14 11:29:50 -07001035 asyncResp->res.jsonValue["Name"] = controllerId;
1036 asyncResp->res.jsonValue["Id"] = controllerId;
Ed Tanous539d8c62024-06-19 14:38:27 -07001037 asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
Willy Tu61b1eb22023-03-14 11:29:50 -07001038
1039 sdbusplus::asio::getProperty<bool>(
1040 *crow::connections::systemBus, connectionName, path,
1041 "xyz.openbmc_project.Inventory.Item", "Present",
1042 [asyncResp](const boost::system::error_code& ec, bool isPresent) {
1043 // this interface isn't necessary, only check it
1044 // if we get a good return
1045 if (ec)
1046 {
Ed Tanous62598e32023-07-17 17:06:25 -07001047 BMCWEB_LOG_DEBUG("Failed to get Present property");
Willy Tu61b1eb22023-03-14 11:29:50 -07001048 return;
1049 }
1050 if (!isPresent)
1051 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001052 asyncResp->res.jsonValue["Status"]["State"] =
1053 resource::State::Absent;
Willy Tu61b1eb22023-03-14 11:29:50 -07001054 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001055 });
Willy Tu61b1eb22023-03-14 11:29:50 -07001056
1057 sdbusplus::asio::getAllProperties(
1058 *crow::connections::systemBus, connectionName, path,
1059 "xyz.openbmc_project.Inventory.Decorator.Asset",
1060 [asyncResp](const boost::system::error_code& ec,
1061 const std::vector<
1062 std::pair<std::string, dbus::utility::DbusVariantType>>&
1063 propertiesList) {
1064 getStorageControllerAsset(asyncResp, ec, propertiesList);
Patrick Williams5a39f772023-10-20 11:20:21 -05001065 });
Willy Tu61b1eb22023-03-14 11:29:50 -07001066}
1067
1068inline void getStorageControllerHandler(
1069 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1070 const std::string& controllerId, const boost::system::error_code& ec,
1071 const dbus::utility::MapperGetSubTreeResponse& subtree)
1072{
1073 if (ec || subtree.empty())
1074 {
1075 // doesn't have to be there
Ed Tanous62598e32023-07-17 17:06:25 -07001076 BMCWEB_LOG_DEBUG("Failed to handle StorageController");
Willy Tu61b1eb22023-03-14 11:29:50 -07001077 return;
1078 }
1079
1080 for (const auto& [path, interfaceDict] : subtree)
1081 {
1082 sdbusplus::message::object_path object(path);
1083 std::string id = object.filename();
1084 if (id.empty())
1085 {
Ed Tanous62598e32023-07-17 17:06:25 -07001086 BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
Willy Tu61b1eb22023-03-14 11:29:50 -07001087 return;
1088 }
1089 if (id != controllerId)
1090 {
1091 continue;
1092 }
1093
1094 if (interfaceDict.size() != 1)
1095 {
Ed Tanous62598e32023-07-17 17:06:25 -07001096 BMCWEB_LOG_ERROR("Connection size {}, greater than 1",
1097 interfaceDict.size());
Willy Tu61b1eb22023-03-14 11:29:50 -07001098 messages::internalError(asyncResp->res);
1099 return;
1100 }
1101
1102 const std::string& connectionName = interfaceDict.front().first;
1103 populateStorageController(asyncResp, controllerId, connectionName,
1104 path);
1105 }
1106}
1107
1108inline void populateStorageControllerCollection(
1109 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1110 const boost::system::error_code& ec,
1111 const dbus::utility::MapperGetSubTreePathsResponse& controllerList)
1112{
1113 nlohmann::json::array_t members;
1114 if (ec || controllerList.empty())
1115 {
1116 asyncResp->res.jsonValue["Members"] = std::move(members);
1117 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous62598e32023-07-17 17:06:25 -07001118 BMCWEB_LOG_DEBUG("Failed to find any StorageController");
Willy Tu61b1eb22023-03-14 11:29:50 -07001119 return;
1120 }
1121
1122 for (const std::string& path : controllerList)
1123 {
1124 std::string id = sdbusplus::message::object_path(path).filename();
1125 if (id.empty())
1126 {
Ed Tanous62598e32023-07-17 17:06:25 -07001127 BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
Willy Tu61b1eb22023-03-14 11:29:50 -07001128 return;
1129 }
1130 nlohmann::json::object_t member;
1131 member["@odata.id"] = boost::urls::format(
Ed Tanous253f11b2024-05-16 09:38:31 -07001132 "/redfish/v1/Systems/{}/Storage/1/Controllers/{}",
1133 BMCWEB_REDFISH_SYSTEM_URI_NAME, id);
Willy Tu61b1eb22023-03-14 11:29:50 -07001134 members.emplace_back(member);
1135 }
1136 asyncResp->res.jsonValue["Members@odata.count"] = members.size();
1137 asyncResp->res.jsonValue["Members"] = std::move(members);
1138}
1139
Ed Tanous36d52332023-06-09 13:18:40 -07001140inline void handleSystemsStorageControllerCollectionGet(
Willy Tu61b1eb22023-03-14 11:29:50 -07001141 App& app, const crow::Request& req,
1142 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1143 const std::string& systemName)
1144{
1145 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1146 {
Ed Tanous62598e32023-07-17 17:06:25 -07001147 BMCWEB_LOG_DEBUG(
1148 "Failed to setup Redfish Route for StorageController Collection");
Willy Tu61b1eb22023-03-14 11:29:50 -07001149 return;
1150 }
Ed Tanous253f11b2024-05-16 09:38:31 -07001151 if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
Willy Tu61b1eb22023-03-14 11:29:50 -07001152 {
1153 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1154 systemName);
Ed Tanous62598e32023-07-17 17:06:25 -07001155 BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
Willy Tu61b1eb22023-03-14 11:29:50 -07001156 return;
1157 }
1158
1159 asyncResp->res.jsonValue["@odata.type"] =
1160 "#StorageControllerCollection.StorageControllerCollection";
1161 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001162 std::format("/redfish/v1/Systems/{}/Storage/1/Controllers",
1163 BMCWEB_REDFISH_SYSTEM_URI_NAME);
Willy Tu61b1eb22023-03-14 11:29:50 -07001164 asyncResp->res.jsonValue["Name"] = "Storage Controller Collection";
1165
1166 constexpr std::array<std::string_view, 1> interfaces = {
1167 "xyz.openbmc_project.Inventory.Item.StorageController"};
1168 dbus::utility::getSubTreePaths(
1169 "/xyz/openbmc_project/inventory", 0, interfaces,
1170 [asyncResp](const boost::system::error_code& ec,
1171 const dbus::utility::MapperGetSubTreePathsResponse&
1172 controllerList) {
1173 populateStorageControllerCollection(asyncResp, ec, controllerList);
Patrick Williams5a39f772023-10-20 11:20:21 -05001174 });
Willy Tu61b1eb22023-03-14 11:29:50 -07001175}
1176
Ed Tanous36d52332023-06-09 13:18:40 -07001177inline void handleSystemsStorageControllerGet(
Willy Tu61b1eb22023-03-14 11:29:50 -07001178 App& app, const crow::Request& req,
1179 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1180 const std::string& systemName, const std::string& controllerId)
1181{
1182 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1183 {
Ed Tanous62598e32023-07-17 17:06:25 -07001184 BMCWEB_LOG_DEBUG("Failed to setup Redfish Route for StorageController");
Willy Tu61b1eb22023-03-14 11:29:50 -07001185 return;
1186 }
Ed Tanous253f11b2024-05-16 09:38:31 -07001187 if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
Willy Tu61b1eb22023-03-14 11:29:50 -07001188 {
1189 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1190 systemName);
Ed Tanous62598e32023-07-17 17:06:25 -07001191 BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
Willy Tu61b1eb22023-03-14 11:29:50 -07001192 return;
1193 }
1194 constexpr std::array<std::string_view, 1> interfaces = {
1195 "xyz.openbmc_project.Inventory.Item.StorageController"};
1196 dbus::utility::getSubTree(
1197 "/xyz/openbmc_project/inventory", 0, interfaces,
1198 [asyncResp,
1199 controllerId](const boost::system::error_code& ec,
1200 const dbus::utility::MapperGetSubTreeResponse& subtree) {
1201 getStorageControllerHandler(asyncResp, controllerId, ec, subtree);
Patrick Williams5a39f772023-10-20 11:20:21 -05001202 });
Willy Tu61b1eb22023-03-14 11:29:50 -07001203}
1204
1205inline void requestRoutesStorageControllerCollection(App& app)
1206{
1207 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/")
1208 .privileges(redfish::privileges::getStorageControllerCollection)
Ed Tanous36d52332023-06-09 13:18:40 -07001209 .methods(boost::beast::http::verb::get)(std::bind_front(
1210 handleSystemsStorageControllerCollectionGet, std::ref(app)));
Willy Tu61b1eb22023-03-14 11:29:50 -07001211}
1212
1213inline void requestRoutesStorageController(App& app)
1214{
1215 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/<str>")
1216 .privileges(redfish::privileges::getStorageController)
1217 .methods(boost::beast::http::verb::get)(
Ed Tanous36d52332023-06-09 13:18:40 -07001218 std::bind_front(handleSystemsStorageControllerGet, std::ref(app)));
Willy Tu61b1eb22023-03-14 11:29:50 -07001219}
1220
Nikhil Potadea25aecc2019-08-23 16:35:26 -07001221} // namespace redfish