blob: 1f41ea86855078351845926401a31b688f53b4d0 [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"
James Feist2ad9c2f2019-10-29 16:26:48 -070024#include "health.hpp"
Ed Tanousa8e884f2023-01-13 17:40:03 -080025#include "human_sort.hpp"
James Feiste284a7c2019-11-20 16:20:23 -080026#include "openbmc_dbus_rest.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080027#include "query.hpp"
Ed Tanousa8e884f2023-01-13 17:40:03 -080028#include "redfish_util.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080029#include "registries/privilege_registry.hpp"
Willy Tu5e577bc2022-07-26 00:41:55 +000030#include "utils/collection.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080031#include "utils/dbus_utils.hpp"
James Feist2ad9c2f2019-10-29 16:26:48 -070032
George Liue99073f2022-12-09 11:06:16 +080033#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070034#include <boost/url/format.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070035#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020036#include <sdbusplus/unpack_properties.hpp>
Nikhil Potadea25aecc2019-08-23 16:35:26 -070037
George Liu7a1dbc42022-12-07 16:03:22 +080038#include <array>
39#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 }
53 if (systemName != "system")
54 {
55 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
56 systemName);
57 return;
58 }
59
60 asyncResp->res.jsonValue["@odata.type"] =
61 "#StorageCollection.StorageCollection";
62 asyncResp->res.jsonValue["@odata.id"] =
63 "/redfish/v1/Systems/system/Storage";
64 asyncResp->res.jsonValue["Name"] = "Storage Collection";
Willy Tu5e577bc2022-07-26 00:41:55 +000065
66 constexpr std::array<std::string_view, 1> interface {
67 "xyz.openbmc_project.Inventory.Item.Storage"
68 };
69 collection_util::getCollectionMembers(
70 asyncResp, boost::urls::format("/redfish/v1/Systems/system/Storage"),
71 interface);
72}
73
74inline void handleStorageCollectionGet(
75 App& app, const crow::Request& req,
76 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
77{
78 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
79 {
80 return;
81 }
82 asyncResp->res.jsonValue["@odata.type"] =
83 "#StorageCollection.StorageCollection";
84 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Storage";
85 asyncResp->res.jsonValue["Name"] = "Storage Collection";
86 constexpr std::array<std::string_view, 1> interface {
87 "xyz.openbmc_project.Inventory.Item.Storage"
88 };
89 collection_util::getCollectionMembers(
90 asyncResp, boost::urls::format("/redfish/v1/Storage"), interface);
Ed Tanous36d52332023-06-09 13:18:40 -070091}
92
John Edward Broadbent7e860f12021-04-08 15:57:16 -070093inline void requestRoutesStorageCollection(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -070094{
Ed Tanous22d268c2022-05-19 09:39:07 -070095 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
Ed Tanoused398212021-06-09 17:05:54 -070096 .privileges(redfish::privileges::getStorageCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070097 .methods(boost::beast::http::verb::get)(
Ed Tanous36d52332023-06-09 13:18:40 -070098 std::bind_front(handleSystemsStorageCollectionGet, std::ref(app)));
Willy Tu5e577bc2022-07-26 00:41:55 +000099 BMCWEB_ROUTE(app, "/redfish/v1/Storage/")
100 .privileges(redfish::privileges::getStorageCollection)
101 .methods(boost::beast::http::verb::get)(
102 std::bind_front(handleStorageCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700103}
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700104
Ed Tanous36d52332023-06-09 13:18:40 -0700105inline void afterChassisDriveCollectionSubtree(
106 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
107 const std::shared_ptr<HealthPopulate>& health,
108 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
123 if constexpr (bmcwebEnableHealthPopulate)
124 {
125 health->inventory.insert(health->inventory.end(), driveList.begin(),
126 driveList.end());
127 }
128
129 for (const std::string& drive : driveList)
130 {
131 sdbusplus::message::object_path object(drive);
132 if (object.filename().empty())
133 {
Ed Tanous62598e32023-07-17 17:06:25 -0700134 BMCWEB_LOG_ERROR("Failed to find filename in {}", drive);
Ed Tanous36d52332023-06-09 13:18:40 -0700135 return;
136 }
137
138 nlohmann::json::object_t driveJson;
139 driveJson["@odata.id"] = boost::urls::format(
140 "/redfish/v1/Systems/system/Storage/1/Drives/{}",
141 object.filename());
142 driveArray.emplace_back(std::move(driveJson));
143 }
144
145 count = driveArray.size();
146}
Willy Tua85afbe2021-12-28 14:43:47 -0800147inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
148 const std::shared_ptr<HealthPopulate>& health)
149{
George Liu7a1dbc42022-12-07 16:03:22 +0800150 const std::array<std::string_view, 1> interfaces = {
151 "xyz.openbmc_project.Inventory.Item.Drive"};
152 dbus::utility::getSubTreePaths(
153 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous36d52332023-06-09 13:18:40 -0700154 std::bind_front(afterChassisDriveCollectionSubtree, asyncResp, health));
155}
Willy Tua85afbe2021-12-28 14:43:47 -0800156
Willy Tu5e577bc2022-07-26 00:41:55 +0000157inline void afterSystemsStorageGetSubtree(
158 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
159 const std::string& storageId, const boost::system::error_code& ec,
160 const dbus::utility::MapperGetSubTreeResponse& subtree)
Ed Tanous36d52332023-06-09 13:18:40 -0700161{
Willy Tu5e577bc2022-07-26 00:41:55 +0000162 if (ec)
Ed Tanous36d52332023-06-09 13:18:40 -0700163 {
Ed Tanous62598e32023-07-17 17:06:25 -0700164 BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
Willy Tu5e577bc2022-07-26 00:41:55 +0000165 messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
166 storageId);
Ed Tanous36d52332023-06-09 13:18:40 -0700167 return;
168 }
Willy Tu5e577bc2022-07-26 00:41:55 +0000169 auto storage = std::find_if(
170 subtree.begin(), subtree.end(),
171 [&storageId](const std::pair<std::string,
172 dbus::utility::MapperServiceMap>& object) {
173 return sdbusplus::message::object_path(object.first).filename() ==
174 storageId;
175 });
176 if (storage == subtree.end())
177 {
178 messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
179 storageId);
180 return;
181 }
182
Ed Tanous36d52332023-06-09 13:18:40 -0700183 asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
184 asyncResp->res.jsonValue["@odata.id"] =
Willy Tu5e577bc2022-07-26 00:41:55 +0000185 boost::urls::format("/redfish/v1/Systems/system/Storage/{}", storageId);
Ed Tanous36d52332023-06-09 13:18:40 -0700186 asyncResp->res.jsonValue["Name"] = "Storage";
Willy Tu5e577bc2022-07-26 00:41:55 +0000187 asyncResp->res.jsonValue["Id"] = storageId;
Ed Tanous36d52332023-06-09 13:18:40 -0700188 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Willy Tua85afbe2021-12-28 14:43:47 -0800189
Ed Tanous36d52332023-06-09 13:18:40 -0700190 auto health = std::make_shared<HealthPopulate>(asyncResp);
191 if constexpr (bmcwebEnableHealthPopulate)
192 {
193 health->populate();
194 }
Willy Tua85afbe2021-12-28 14:43:47 -0800195
Ed Tanous36d52332023-06-09 13:18:40 -0700196 getDrives(asyncResp, health);
Willy Tu5e577bc2022-07-26 00:41:55 +0000197 asyncResp->res.jsonValue["Controllers"]["@odata.id"] = boost::urls::format(
198 "/redfish/v1/Systems/system/Storage/{}/Controllers", storageId);
199}
200
201inline void
202 handleSystemsStorageGet(App& app, const crow::Request& req,
203 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800204 const std::string& systemName,
Willy Tu5e577bc2022-07-26 00:41:55 +0000205 const std::string& storageId)
206{
207 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
208 {
209 return;
210 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800211 if constexpr (bmcwebEnableMultiHost)
212 {
213 // Option currently returns no systems. TBD
214 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
215 systemName);
216 return;
217 }
Willy Tu5e577bc2022-07-26 00:41:55 +0000218
219 constexpr std::array<std::string_view, 1> interfaces = {
220 "xyz.openbmc_project.Inventory.Item.Storage"};
221 dbus::utility::getSubTree(
222 "/xyz/openbmc_project/inventory", 0, interfaces,
223 std::bind_front(afterSystemsStorageGetSubtree, asyncResp, storageId));
224}
225
226inline void afterSubtree(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
227 const std::string& storageId,
228 const boost::system::error_code& ec,
229 const dbus::utility::MapperGetSubTreeResponse& subtree)
230{
231 if (ec)
232 {
Ed Tanous62598e32023-07-17 17:06:25 -0700233 BMCWEB_LOG_DEBUG("requestRoutesStorage DBUS response error");
Willy Tu5e577bc2022-07-26 00:41:55 +0000234 messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
235 storageId);
236 return;
237 }
238 auto storage = std::find_if(
239 subtree.begin(), subtree.end(),
240 [&storageId](const std::pair<std::string,
241 dbus::utility::MapperServiceMap>& object) {
242 return sdbusplus::message::object_path(object.first).filename() ==
243 storageId;
244 });
245 if (storage == subtree.end())
246 {
247 messages::resourceNotFound(asyncResp->res, "#Storage.v1_13_0.Storage",
248 storageId);
249 return;
250 }
251
252 asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_13_0.Storage";
253 asyncResp->res.jsonValue["@odata.id"] =
254 boost::urls::format("/redfish/v1/Storage/{}", storageId);
255 asyncResp->res.jsonValue["Name"] = "Storage";
256 asyncResp->res.jsonValue["Id"] = storageId;
257 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
258
259 // Storage subsystem to Storage link.
260 nlohmann::json::array_t storageServices;
261 nlohmann::json::object_t storageService;
262 storageService["@odata.id"] =
263 boost::urls::format("/redfish/v1/Systems/system/Storage/{}", storageId);
264 storageServices.emplace_back(storageService);
265 asyncResp->res.jsonValue["Links"]["StorageServices"] =
266 std::move(storageServices);
267 asyncResp->res.jsonValue["Links"]["StorageServices@odata.count"] = 1;
268}
269
270inline void
271 handleStorageGet(App& app, const crow::Request& req,
272 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
273 const std::string& storageId)
274{
275 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
276 {
Ed Tanous62598e32023-07-17 17:06:25 -0700277 BMCWEB_LOG_DEBUG("requestRoutesStorage setUpRedfishRoute failed");
Willy Tu5e577bc2022-07-26 00:41:55 +0000278 return;
279 }
280
281 constexpr std::array<std::string_view, 1> interfaces = {
282 "xyz.openbmc_project.Inventory.Item.Storage"};
283 dbus::utility::getSubTree(
284 "/xyz/openbmc_project/inventory", 0, interfaces,
285 std::bind_front(afterSubtree, asyncResp, storageId));
Willy Tua85afbe2021-12-28 14:43:47 -0800286}
287
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700288inline void requestRoutesStorage(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700289{
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800290 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700291 .privileges(redfish::privileges::getStorage)
Ed Tanous002d39b2022-05-31 08:59:27 -0700292 .methods(boost::beast::http::verb::get)(
Ed Tanous36d52332023-06-09 13:18:40 -0700293 std::bind_front(handleSystemsStorageGet, std::ref(app)));
Willy Tu5e577bc2022-07-26 00:41:55 +0000294
295 BMCWEB_ROUTE(app, "/redfish/v1/Storage/<str>/")
296 .privileges(redfish::privileges::getStorage)
297 .methods(boost::beast::http::verb::get)(
298 std::bind_front(handleStorageGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700299}
300
Willy Tu03913172021-11-08 02:03:19 -0800301inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
302 const std::string& connectionName,
303 const std::string& path)
304{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200305 sdbusplus::asio::getAllProperties(
306 *crow::connections::systemBus, connectionName, path,
307 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800308 [asyncResp](const boost::system::error_code& ec,
Ed Tanous168e20c2021-12-13 14:39:53 -0800309 const std::vector<
310 std::pair<std::string, dbus::utility::DbusVariantType>>&
311 propertiesList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700312 if (ec)
313 {
314 // this interface isn't necessary
315 return;
316 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200317
318 const std::string* partNumber = nullptr;
319 const std::string* serialNumber = nullptr;
320 const std::string* manufacturer = nullptr;
321 const std::string* model = nullptr;
322
323 const bool success = sdbusplus::unpackPropertiesNoThrow(
324 dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
325 partNumber, "SerialNumber", serialNumber, "Manufacturer",
326 manufacturer, "Model", model);
327
328 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700329 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200330 messages::internalError(asyncResp->res);
331 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700332 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200333
334 if (partNumber != nullptr)
335 {
336 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
337 }
338
339 if (serialNumber != nullptr)
340 {
341 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
342 }
343
344 if (manufacturer != nullptr)
345 {
346 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
347 }
348
349 if (model != nullptr)
350 {
351 asyncResp->res.jsonValue["Model"] = *model;
352 }
353 });
Willy Tu03913172021-11-08 02:03:19 -0800354}
355
356inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
357 const std::string& connectionName,
358 const std::string& path)
359{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700360 sdbusplus::asio::getProperty<bool>(
361 *crow::connections::systemBus, connectionName, path,
362 "xyz.openbmc_project.Inventory.Item", "Present",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800363 [asyncResp, path](const boost::system::error_code& ec,
Willy Tucef57e82022-12-15 16:42:02 -0800364 const bool isPresent) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700365 // this interface isn't necessary, only check it if
366 // we get a good return
367 if (ec)
368 {
369 return;
370 }
Willy Tu03913172021-11-08 02:03:19 -0800371
Willy Tucef57e82022-12-15 16:42:02 -0800372 if (!isPresent)
Ed Tanous002d39b2022-05-31 08:59:27 -0700373 {
Willy Tucef57e82022-12-15 16:42:02 -0800374 asyncResp->res.jsonValue["Status"]["State"] = "Absent";
Ed Tanous002d39b2022-05-31 08:59:27 -0700375 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700376 });
Willy Tu03913172021-11-08 02:03:19 -0800377}
378
379inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
380 const std::string& connectionName,
381 const std::string& path)
382{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700383 sdbusplus::asio::getProperty<bool>(
384 *crow::connections::systemBus, connectionName, path,
385 "xyz.openbmc_project.State.Drive", "Rebuilding",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800386 [asyncResp](const boost::system::error_code& ec, const bool updating) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700387 // this interface isn't necessary, only check it
388 // if we get a good return
389 if (ec)
390 {
391 return;
392 }
Willy Tu03913172021-11-08 02:03:19 -0800393
Ed Tanous002d39b2022-05-31 08:59:27 -0700394 // updating and disabled in the backend shouldn't be
395 // able to be set at the same time, so we don't need
396 // to check for the race condition of these two
397 // calls
398 if (updating)
399 {
400 asyncResp->res.jsonValue["Status"]["State"] = "Updating";
401 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700402 });
Willy Tu03913172021-11-08 02:03:19 -0800403}
404
George Liudde9bc12023-02-22 09:35:51 +0800405inline std::optional<drive::MediaType> convertDriveType(std::string_view type)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800406{
407 if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
408 {
George Liudde9bc12023-02-22 09:35:51 +0800409 return drive::MediaType::HDD;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800410 }
411 if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
412 {
George Liudde9bc12023-02-22 09:35:51 +0800413 return drive::MediaType::SSD;
414 }
415 if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.Unknown")
416 {
417 return std::nullopt;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800418 }
419
George Liudde9bc12023-02-22 09:35:51 +0800420 return drive::MediaType::Invalid;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800421}
422
George Liudde9bc12023-02-22 09:35:51 +0800423inline std::optional<protocol::Protocol>
424 convertDriveProtocol(std::string_view proto)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800425{
426 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
427 {
George Liudde9bc12023-02-22 09:35:51 +0800428 return protocol::Protocol::SAS;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800429 }
430 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
431 {
George Liudde9bc12023-02-22 09:35:51 +0800432 return protocol::Protocol::SATA;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800433 }
434 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
435 {
George Liudde9bc12023-02-22 09:35:51 +0800436 return protocol::Protocol::NVMe;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800437 }
438 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
439 {
George Liudde9bc12023-02-22 09:35:51 +0800440 return protocol::Protocol::FC;
441 }
442 if (proto ==
443 "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.Unknown")
444 {
445 return std::nullopt;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800446 }
447
George Liudde9bc12023-02-22 09:35:51 +0800448 return protocol::Protocol::Invalid;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800449}
450
451inline void
452 getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
453 const std::string& connectionName,
454 const std::string& path)
455{
456 sdbusplus::asio::getAllProperties(
457 *crow::connections::systemBus, connectionName, path,
458 "xyz.openbmc_project.Inventory.Item.Drive",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800459 [asyncResp](const boost::system::error_code& ec,
Willy Tu19b8e9a2021-11-08 02:55:03 -0800460 const std::vector<
461 std::pair<std::string, dbus::utility::DbusVariantType>>&
462 propertiesList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700463 if (ec)
464 {
465 // this interface isn't required
466 return;
467 }
John Edward Broadbente5029d82022-06-08 14:35:21 -0700468 const std::string* encryptionStatus = nullptr;
469 const bool* isLocked = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -0700470 for (const std::pair<std::string, dbus::utility::DbusVariantType>&
471 property : propertiesList)
472 {
473 const std::string& propertyName = property.first;
474 if (propertyName == "Type")
Willy Tu19b8e9a2021-11-08 02:55:03 -0800475 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700476 const std::string* value =
477 std::get_if<std::string>(&property.second);
478 if (value == nullptr)
479 {
480 // illegal property
Ed Tanous62598e32023-07-17 17:06:25 -0700481 BMCWEB_LOG_ERROR("Illegal property: Type");
Ed Tanous002d39b2022-05-31 08:59:27 -0700482 messages::internalError(asyncResp->res);
483 return;
484 }
485
George Liudde9bc12023-02-22 09:35:51 +0800486 std::optional<drive::MediaType> mediaType =
487 convertDriveType(*value);
Ed Tanous002d39b2022-05-31 08:59:27 -0700488 if (!mediaType)
489 {
Ed Tanous62598e32023-07-17 17:06:25 -0700490 BMCWEB_LOG_WARNING("UnknownDriveType Interface: {}",
491 *value);
George Liudde9bc12023-02-22 09:35:51 +0800492 continue;
493 }
494 if (*mediaType == drive::MediaType::Invalid)
495 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700496 messages::internalError(asyncResp->res);
497 return;
498 }
499
500 asyncResp->res.jsonValue["MediaType"] = *mediaType;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800501 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700502 else if (propertyName == "Capacity")
Willy Tu19b8e9a2021-11-08 02:55:03 -0800503 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700504 const uint64_t* capacity =
505 std::get_if<uint64_t>(&property.second);
506 if (capacity == nullptr)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800507 {
Ed Tanous62598e32023-07-17 17:06:25 -0700508 BMCWEB_LOG_ERROR("Illegal property: Capacity");
Ed Tanous002d39b2022-05-31 08:59:27 -0700509 messages::internalError(asyncResp->res);
510 return;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800511 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700512 if (*capacity == 0)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800513 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700514 // drive capacity not known
515 continue;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800516 }
Willy Tu19b8e9a2021-11-08 02:55:03 -0800517
Ed Tanous002d39b2022-05-31 08:59:27 -0700518 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800519 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700520 else if (propertyName == "Protocol")
521 {
522 const std::string* value =
523 std::get_if<std::string>(&property.second);
524 if (value == nullptr)
525 {
Ed Tanous62598e32023-07-17 17:06:25 -0700526 BMCWEB_LOG_ERROR("Illegal property: Protocol");
Ed Tanous002d39b2022-05-31 08:59:27 -0700527 messages::internalError(asyncResp->res);
528 return;
529 }
530
George Liudde9bc12023-02-22 09:35:51 +0800531 std::optional<protocol::Protocol> proto =
532 convertDriveProtocol(*value);
Ed Tanous002d39b2022-05-31 08:59:27 -0700533 if (!proto)
534 {
Ed Tanous62598e32023-07-17 17:06:25 -0700535 BMCWEB_LOG_WARNING("Unknown DrivePrototype Interface: {}",
536 *value);
George Liudde9bc12023-02-22 09:35:51 +0800537 continue;
538 }
539 if (*proto == protocol::Protocol::Invalid)
540 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700541 messages::internalError(asyncResp->res);
542 return;
543 }
544 asyncResp->res.jsonValue["Protocol"] = *proto;
545 }
John Edward Broadbent3fe4d5c2022-05-06 14:42:35 -0700546 else if (propertyName == "PredictedMediaLifeLeftPercent")
547 {
548 const uint8_t* lifeLeft =
549 std::get_if<uint8_t>(&property.second);
550 if (lifeLeft == nullptr)
551 {
Ed Tanous62598e32023-07-17 17:06:25 -0700552 BMCWEB_LOG_ERROR(
553 "Illegal property: PredictedMediaLifeLeftPercent");
John Edward Broadbent3fe4d5c2022-05-06 14:42:35 -0700554 messages::internalError(asyncResp->res);
555 return;
556 }
557 // 255 means reading the value is not supported
558 if (*lifeLeft != 255)
559 {
560 asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
561 *lifeLeft;
562 }
563 }
John Edward Broadbente5029d82022-06-08 14:35:21 -0700564 else if (propertyName == "EncryptionStatus")
565 {
566 encryptionStatus = std::get_if<std::string>(&property.second);
567 if (encryptionStatus == nullptr)
568 {
Ed Tanous62598e32023-07-17 17:06:25 -0700569 BMCWEB_LOG_ERROR("Illegal property: EncryptionStatus");
John Edward Broadbente5029d82022-06-08 14:35:21 -0700570 messages::internalError(asyncResp->res);
571 return;
572 }
573 }
574 else if (propertyName == "Locked")
575 {
576 isLocked = std::get_if<bool>(&property.second);
577 if (isLocked == nullptr)
578 {
Ed Tanous62598e32023-07-17 17:06:25 -0700579 BMCWEB_LOG_ERROR("Illegal property: Locked");
John Edward Broadbente5029d82022-06-08 14:35:21 -0700580 messages::internalError(asyncResp->res);
581 return;
582 }
583 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700584 }
John Edward Broadbente5029d82022-06-08 14:35:21 -0700585
586 if (encryptionStatus == nullptr || isLocked == nullptr ||
587 *encryptionStatus ==
588 "xyz.openbmc_project.Drive.DriveEncryptionState.Unknown")
589 {
590 return;
591 }
592 if (*encryptionStatus !=
593 "xyz.openbmc_project.Drive.DriveEncryptionState.Encrypted")
594 {
595 //"The drive is not currently encrypted."
596 asyncResp->res.jsonValue["EncryptionStatus"] =
597 drive::EncryptionStatus::Unencrypted;
598 return;
599 }
600 if (*isLocked)
601 {
602 //"The drive is currently encrypted and the data is not
603 // accessible to the user."
604 asyncResp->res.jsonValue["EncryptionStatus"] =
605 drive::EncryptionStatus::Locked;
606 return;
607 }
608 // if not locked
609 // "The drive is currently encrypted but the data is accessible
610 // to the user in unencrypted form."
611 asyncResp->res.jsonValue["EncryptionStatus"] =
612 drive::EncryptionStatus::Unlocked;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800613 });
614}
615
Nan Zhoub53dcd92022-06-21 17:47:50 +0000616static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
617 const std::string& connectionName,
618 const std::string& path,
619 const std::vector<std::string>& interfaces)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700620{
621 for (const std::string& interface : interfaces)
622 {
623 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
624 {
625 getDriveAsset(asyncResp, connectionName, path);
626 }
627 else if (interface == "xyz.openbmc_project.Inventory.Item")
628 {
629 getDrivePresent(asyncResp, connectionName, path);
630 }
631 else if (interface == "xyz.openbmc_project.State.Drive")
632 {
633 getDriveState(asyncResp, connectionName, path);
634 }
635 else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
636 {
637 getDriveItemProperties(asyncResp, connectionName, path);
638 }
639 }
640}
641
Ed Tanous36d52332023-06-09 13:18:40 -0700642inline void afterGetSubtreeSystemsStorageDrive(
643 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
644 const std::string& driveId, const boost::system::error_code& ec,
645 const dbus::utility::MapperGetSubTreeResponse& subtree)
646{
647 if (ec)
648 {
Ed Tanous62598e32023-07-17 17:06:25 -0700649 BMCWEB_LOG_ERROR("Drive mapper call error");
Ed Tanous36d52332023-06-09 13:18:40 -0700650 messages::internalError(asyncResp->res);
651 return;
652 }
653
654 auto drive = std::find_if(
655 subtree.begin(), subtree.end(),
656 [&driveId](const std::pair<std::string,
657 dbus::utility::MapperServiceMap>& object) {
658 return sdbusplus::message::object_path(object.first).filename() ==
659 driveId;
660 });
661
662 if (drive == subtree.end())
663 {
664 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
665 return;
666 }
667
668 const std::string& path = drive->first;
669 const dbus::utility::MapperServiceMap& connectionNames = drive->second;
670
671 asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
672 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
673 "/redfish/v1/Systems/system/Storage/1/Drives/{}", driveId);
674 asyncResp->res.jsonValue["Name"] = driveId;
675 asyncResp->res.jsonValue["Id"] = driveId;
676
677 if (connectionNames.size() != 1)
678 {
Ed Tanous62598e32023-07-17 17:06:25 -0700679 BMCWEB_LOG_ERROR("Connection size {}, not equal to 1",
680 connectionNames.size());
Ed Tanous36d52332023-06-09 13:18:40 -0700681 messages::internalError(asyncResp->res);
682 return;
683 }
684
685 getMainChassisId(asyncResp,
686 [](const std::string& chassisId,
687 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
688 aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
689 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
690 });
691
692 // default it to Enabled
693 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
694
695 if constexpr (bmcwebEnableHealthPopulate)
696 {
697 auto health = std::make_shared<HealthPopulate>(asyncResp);
698 health->inventory.emplace_back(path);
699 health->populate();
700 }
701
702 addAllDriveInfo(asyncResp, connectionNames[0].first, path,
703 connectionNames[0].second);
704}
705
706inline void handleSystemsStorageDriveGet(
707 App& app, const crow::Request& req,
708 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
709 const std::string& systemName, const std::string& driveId)
710{
711 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
712 {
713 return;
714 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800715 if constexpr (bmcwebEnableMultiHost)
716 {
717 // Option currently returns no systems. TBD
718 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
719 systemName);
720 return;
721 }
722
Ed Tanous36d52332023-06-09 13:18:40 -0700723 if (systemName != "system")
724 {
725 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
726 systemName);
727 return;
728 }
729
730 constexpr std::array<std::string_view, 1> interfaces = {
731 "xyz.openbmc_project.Inventory.Item.Drive"};
732 dbus::utility::getSubTree(
733 "/xyz/openbmc_project/inventory", 0, interfaces,
734 std::bind_front(afterGetSubtreeSystemsStorageDrive, asyncResp,
735 driveId));
736}
737
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700738inline void requestRoutesDrive(App& app)
739{
Ed Tanous22d268c2022-05-19 09:39:07 -0700740 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700741 .privileges(redfish::privileges::getDrive)
Ed Tanous002d39b2022-05-31 08:59:27 -0700742 .methods(boost::beast::http::verb::get)(
Ed Tanous36d52332023-06-09 13:18:40 -0700743 std::bind_front(handleSystemsStorageDriveGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700744}
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700745
Ed Tanous36d52332023-06-09 13:18:40 -0700746inline void afterChassisDriveCollectionSubtreeGet(
747 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
748 const std::string& chassisId, const boost::system::error_code& ec,
749 const dbus::utility::MapperGetSubTreeResponse& subtree)
750{
751 if (ec)
752 {
753 if (ec == boost::system::errc::host_unreachable)
754 {
755 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
756 return;
757 }
758 messages::internalError(asyncResp->res);
759 return;
760 }
761
762 // Iterate over all retrieved ObjectPaths.
763 for (const auto& [path, connectionNames] : subtree)
764 {
765 sdbusplus::message::object_path objPath(path);
766 if (objPath.filename() != chassisId)
767 {
768 continue;
769 }
770
771 if (connectionNames.empty())
772 {
Ed Tanous62598e32023-07-17 17:06:25 -0700773 BMCWEB_LOG_ERROR("Got 0 Connection names");
Ed Tanous36d52332023-06-09 13:18:40 -0700774 continue;
775 }
776
777 asyncResp->res.jsonValue["@odata.type"] =
778 "#DriveCollection.DriveCollection";
779 asyncResp->res.jsonValue["@odata.id"] =
780 boost::urls::format("/redfish/v1/Chassis/{}/Drives", chassisId);
781 asyncResp->res.jsonValue["Name"] = "Drive Collection";
782
783 // Association lambda
784 dbus::utility::getAssociationEndPoints(
785 path + "/drive",
786 [asyncResp, chassisId](const boost::system::error_code& ec3,
787 const dbus::utility::MapperEndPoints& resp) {
788 if (ec3)
789 {
Ed Tanous62598e32023-07-17 17:06:25 -0700790 BMCWEB_LOG_ERROR("Error in chassis Drive association ");
Ed Tanous36d52332023-06-09 13:18:40 -0700791 }
792 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
793 // important if array is empty
794 members = nlohmann::json::array();
795
796 std::vector<std::string> leafNames;
797 for (const auto& drive : resp)
798 {
799 sdbusplus::message::object_path drivePath(drive);
800 leafNames.push_back(drivePath.filename());
801 }
802
803 std::sort(leafNames.begin(), leafNames.end(),
804 AlphanumLess<std::string>());
805
806 for (const auto& leafName : leafNames)
807 {
808 nlohmann::json::object_t member;
809 member["@odata.id"] = boost::urls::format(
810 "/redfish/v1/Chassis/{}/Drives/{}", chassisId, leafName);
811 members.emplace_back(std::move(member));
812 // navigation links will be registered in next patch set
813 }
814 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
815 }); // end association lambda
816
817 } // end Iterate over all retrieved ObjectPaths
818}
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700819/**
820 * Chassis drives, this URL will show all the DriveCollection
821 * information
822 */
Nan Zhoub53dcd92022-06-21 17:47:50 +0000823inline void chassisDriveCollectionGet(
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700824 crow::App& app, const crow::Request& req,
825 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
826 const std::string& chassisId)
827{
Carson Labrado3ba00072022-06-06 19:40:56 +0000828 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700829 {
830 return;
831 }
832
833 // mapper call lambda
George Liue99073f2022-12-09 11:06:16 +0800834 constexpr std::array<std::string_view, 2> interfaces = {
835 "xyz.openbmc_project.Inventory.Item.Board",
836 "xyz.openbmc_project.Inventory.Item.Chassis"};
837 dbus::utility::getSubTree(
838 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous36d52332023-06-09 13:18:40 -0700839 std::bind_front(afterChassisDriveCollectionSubtreeGet, asyncResp,
840 chassisId));
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700841}
842
843inline void requestRoutesChassisDrive(App& app)
844{
845 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
846 .privileges(redfish::privileges::getDriveCollection)
847 .methods(boost::beast::http::verb::get)(
848 std::bind_front(chassisDriveCollectionGet, std::ref(app)));
849}
850
Nan Zhoub53dcd92022-06-21 17:47:50 +0000851inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
852 const std::string& chassisId,
853 const std::string& driveName,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800854 const boost::system::error_code& ec,
Nan Zhoub53dcd92022-06-21 17:47:50 +0000855 const dbus::utility::MapperGetSubTreeResponse& subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700856{
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700857 if (ec)
858 {
Ed Tanous62598e32023-07-17 17:06:25 -0700859 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700860 messages::internalError(asyncResp->res);
861 return;
862 }
863
864 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000865 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700866 {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700867 sdbusplus::message::object_path objPath(path);
868 if (objPath.filename() != driveName)
869 {
870 continue;
871 }
872
873 if (connectionNames.empty())
874 {
Ed Tanous62598e32023-07-17 17:06:25 -0700875 BMCWEB_LOG_ERROR("Got 0 Connection names");
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700876 continue;
877 }
878
Ed Tanousef4c65b2023-04-24 15:28:50 -0700879 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
880 "/redfish/v1/Chassis/{}/Drives/{}", chassisId, driveName);
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700881
882 asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
John Edward Broadbenta0cb40c2022-06-29 17:27:38 -0700883 asyncResp->res.jsonValue["Name"] = driveName;
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700884 asyncResp->res.jsonValue["Id"] = driveName;
885 // default it to Enabled
886 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
887
888 nlohmann::json::object_t linkChassisNav;
889 linkChassisNav["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700890 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700891 asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
892
893 addAllDriveInfo(asyncResp, connectionNames[0].first, path,
894 connectionNames[0].second);
895 }
896}
897
Nan Zhoub53dcd92022-06-21 17:47:50 +0000898inline void
899 matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
900 const std::string& chassisId,
901 const std::string& driveName,
902 const std::vector<std::string>& resp)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700903{
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700904 for (const std::string& drivePath : resp)
905 {
906 sdbusplus::message::object_path path(drivePath);
907 std::string leaf = path.filename();
908 if (leaf != driveName)
909 {
910 continue;
911 }
912 // mapper call drive
George Liue99073f2022-12-09 11:06:16 +0800913 constexpr std::array<std::string_view, 1> driveInterface = {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700914 "xyz.openbmc_project.Inventory.Item.Drive"};
George Liue99073f2022-12-09 11:06:16 +0800915 dbus::utility::getSubTree(
916 "/xyz/openbmc_project/inventory", 0, driveInterface,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700917 [asyncResp, chassisId, driveName](
George Liue99073f2022-12-09 11:06:16 +0800918 const boost::system::error_code& ec,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700919 const dbus::utility::MapperGetSubTreeResponse& subtree) {
920 buildDrive(asyncResp, chassisId, driveName, ec, subtree);
George Liue99073f2022-12-09 11:06:16 +0800921 });
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700922 }
923}
924
Nan Zhoub53dcd92022-06-21 17:47:50 +0000925inline void
926 handleChassisDriveGet(crow::App& app, const crow::Request& req,
927 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
928 const std::string& chassisId,
929 const std::string& driveName)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700930{
Michal Orzel03810a12022-06-15 14:04:28 +0200931 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700932 {
933 return;
934 }
George Liue99073f2022-12-09 11:06:16 +0800935 constexpr std::array<std::string_view, 2> interfaces = {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700936 "xyz.openbmc_project.Inventory.Item.Board",
937 "xyz.openbmc_project.Inventory.Item.Chassis"};
938
939 // mapper call chassis
George Liue99073f2022-12-09 11:06:16 +0800940 dbus::utility::getSubTree(
941 "/xyz/openbmc_project/inventory", 0, interfaces,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700942 [asyncResp, chassisId,
George Liue99073f2022-12-09 11:06:16 +0800943 driveName](const boost::system::error_code& ec,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700944 const dbus::utility::MapperGetSubTreeResponse& subtree) {
945 if (ec)
946 {
947 messages::internalError(asyncResp->res);
948 return;
949 }
950
951 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000952 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700953 {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700954 sdbusplus::message::object_path objPath(path);
955 if (objPath.filename() != chassisId)
956 {
957 continue;
958 }
959
960 if (connectionNames.empty())
961 {
Ed Tanous62598e32023-07-17 17:06:25 -0700962 BMCWEB_LOG_ERROR("Got 0 Connection names");
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700963 continue;
964 }
965
George Liu6c3e9452023-03-03 13:55:29 +0800966 dbus::utility::getAssociationEndPoints(
967 path + "/drive",
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700968 [asyncResp, chassisId,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800969 driveName](const boost::system::error_code& ec3,
George Liu6c3e9452023-03-03 13:55:29 +0800970 const dbus::utility::MapperEndPoints& resp) {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700971 if (ec3)
972 {
973 return; // no drives = no failures
974 }
975 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
976 });
977 break;
978 }
George Liue99073f2022-12-09 11:06:16 +0800979 });
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700980}
981
982/**
983 * This URL will show the drive interface for the specific drive in the chassis
984 */
985inline void requestRoutesChassisDriveName(App& app)
986{
987 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
988 .privileges(redfish::privileges::getChassis)
989 .methods(boost::beast::http::verb::get)(
990 std::bind_front(handleChassisDriveGet, std::ref(app)));
991}
992
Willy Tu61b1eb22023-03-14 11:29:50 -0700993inline void getStorageControllerAsset(
994 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
995 const boost::system::error_code& ec,
996 const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
997 propertiesList)
998{
999 if (ec)
1000 {
1001 // this interface isn't necessary
Ed Tanous62598e32023-07-17 17:06:25 -07001002 BMCWEB_LOG_DEBUG("Failed to get StorageControllerAsset");
Willy Tu61b1eb22023-03-14 11:29:50 -07001003 return;
1004 }
1005
1006 const std::string* partNumber = nullptr;
1007 const std::string* serialNumber = nullptr;
1008 const std::string* manufacturer = nullptr;
1009 const std::string* model = nullptr;
1010 if (!sdbusplus::unpackPropertiesNoThrow(
1011 dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
1012 partNumber, "SerialNumber", serialNumber, "Manufacturer",
1013 manufacturer, "Model", model))
1014 {
1015 messages::internalError(asyncResp->res);
1016 return;
1017 }
1018
1019 if (partNumber != nullptr)
1020 {
1021 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
1022 }
1023
1024 if (serialNumber != nullptr)
1025 {
1026 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
1027 }
1028
1029 if (manufacturer != nullptr)
1030 {
1031 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
1032 }
1033
1034 if (model != nullptr)
1035 {
1036 asyncResp->res.jsonValue["Model"] = *model;
1037 }
1038}
1039
1040inline void populateStorageController(
1041 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1042 const std::string& controllerId, const std::string& connectionName,
1043 const std::string& path)
1044{
1045 asyncResp->res.jsonValue["@odata.type"] =
1046 "#StorageController.v1_6_0.StorageController";
1047 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1048 "/redfish/v1/Systems/system/Storage/1/Controllers/{}", controllerId);
1049 asyncResp->res.jsonValue["Name"] = controllerId;
1050 asyncResp->res.jsonValue["Id"] = controllerId;
1051 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
1052
1053 sdbusplus::asio::getProperty<bool>(
1054 *crow::connections::systemBus, connectionName, path,
1055 "xyz.openbmc_project.Inventory.Item", "Present",
1056 [asyncResp](const boost::system::error_code& ec, bool isPresent) {
1057 // this interface isn't necessary, only check it
1058 // if we get a good return
1059 if (ec)
1060 {
Ed Tanous62598e32023-07-17 17:06:25 -07001061 BMCWEB_LOG_DEBUG("Failed to get Present property");
Willy Tu61b1eb22023-03-14 11:29:50 -07001062 return;
1063 }
1064 if (!isPresent)
1065 {
1066 asyncResp->res.jsonValue["Status"]["State"] = "Absent";
1067 }
1068 });
1069
1070 sdbusplus::asio::getAllProperties(
1071 *crow::connections::systemBus, connectionName, path,
1072 "xyz.openbmc_project.Inventory.Decorator.Asset",
1073 [asyncResp](const boost::system::error_code& ec,
1074 const std::vector<
1075 std::pair<std::string, dbus::utility::DbusVariantType>>&
1076 propertiesList) {
1077 getStorageControllerAsset(asyncResp, ec, propertiesList);
1078 });
1079}
1080
1081inline void getStorageControllerHandler(
1082 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1083 const std::string& controllerId, const boost::system::error_code& ec,
1084 const dbus::utility::MapperGetSubTreeResponse& subtree)
1085{
1086 if (ec || subtree.empty())
1087 {
1088 // doesn't have to be there
Ed Tanous62598e32023-07-17 17:06:25 -07001089 BMCWEB_LOG_DEBUG("Failed to handle StorageController");
Willy Tu61b1eb22023-03-14 11:29:50 -07001090 return;
1091 }
1092
1093 for (const auto& [path, interfaceDict] : subtree)
1094 {
1095 sdbusplus::message::object_path object(path);
1096 std::string id = object.filename();
1097 if (id.empty())
1098 {
Ed Tanous62598e32023-07-17 17:06:25 -07001099 BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
Willy Tu61b1eb22023-03-14 11:29:50 -07001100 return;
1101 }
1102 if (id != controllerId)
1103 {
1104 continue;
1105 }
1106
1107 if (interfaceDict.size() != 1)
1108 {
Ed Tanous62598e32023-07-17 17:06:25 -07001109 BMCWEB_LOG_ERROR("Connection size {}, greater than 1",
1110 interfaceDict.size());
Willy Tu61b1eb22023-03-14 11:29:50 -07001111 messages::internalError(asyncResp->res);
1112 return;
1113 }
1114
1115 const std::string& connectionName = interfaceDict.front().first;
1116 populateStorageController(asyncResp, controllerId, connectionName,
1117 path);
1118 }
1119}
1120
1121inline void populateStorageControllerCollection(
1122 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1123 const boost::system::error_code& ec,
1124 const dbus::utility::MapperGetSubTreePathsResponse& controllerList)
1125{
1126 nlohmann::json::array_t members;
1127 if (ec || controllerList.empty())
1128 {
1129 asyncResp->res.jsonValue["Members"] = std::move(members);
1130 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous62598e32023-07-17 17:06:25 -07001131 BMCWEB_LOG_DEBUG("Failed to find any StorageController");
Willy Tu61b1eb22023-03-14 11:29:50 -07001132 return;
1133 }
1134
1135 for (const std::string& path : controllerList)
1136 {
1137 std::string id = sdbusplus::message::object_path(path).filename();
1138 if (id.empty())
1139 {
Ed Tanous62598e32023-07-17 17:06:25 -07001140 BMCWEB_LOG_ERROR("Failed to find filename in {}", path);
Willy Tu61b1eb22023-03-14 11:29:50 -07001141 return;
1142 }
1143 nlohmann::json::object_t member;
1144 member["@odata.id"] = boost::urls::format(
1145 "/redfish/v1/Systems/system/Storage/1/Controllers/{}", id);
1146 members.emplace_back(member);
1147 }
1148 asyncResp->res.jsonValue["Members@odata.count"] = members.size();
1149 asyncResp->res.jsonValue["Members"] = std::move(members);
1150}
1151
Ed Tanous36d52332023-06-09 13:18:40 -07001152inline void handleSystemsStorageControllerCollectionGet(
Willy Tu61b1eb22023-03-14 11:29:50 -07001153 App& app, const crow::Request& req,
1154 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1155 const std::string& systemName)
1156{
1157 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1158 {
Ed Tanous62598e32023-07-17 17:06:25 -07001159 BMCWEB_LOG_DEBUG(
1160 "Failed to setup Redfish Route for StorageController Collection");
Willy Tu61b1eb22023-03-14 11:29:50 -07001161 return;
1162 }
1163 if (systemName != "system")
1164 {
1165 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1166 systemName);
Ed Tanous62598e32023-07-17 17:06:25 -07001167 BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
Willy Tu61b1eb22023-03-14 11:29:50 -07001168 return;
1169 }
1170
1171 asyncResp->res.jsonValue["@odata.type"] =
1172 "#StorageControllerCollection.StorageControllerCollection";
1173 asyncResp->res.jsonValue["@odata.id"] =
1174 "/redfish/v1/Systems/system/Storage/1/Controllers";
1175 asyncResp->res.jsonValue["Name"] = "Storage Controller Collection";
1176
1177 constexpr std::array<std::string_view, 1> interfaces = {
1178 "xyz.openbmc_project.Inventory.Item.StorageController"};
1179 dbus::utility::getSubTreePaths(
1180 "/xyz/openbmc_project/inventory", 0, interfaces,
1181 [asyncResp](const boost::system::error_code& ec,
1182 const dbus::utility::MapperGetSubTreePathsResponse&
1183 controllerList) {
1184 populateStorageControllerCollection(asyncResp, ec, controllerList);
1185 });
1186}
1187
Ed Tanous36d52332023-06-09 13:18:40 -07001188inline void handleSystemsStorageControllerGet(
Willy Tu61b1eb22023-03-14 11:29:50 -07001189 App& app, const crow::Request& req,
1190 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1191 const std::string& systemName, const std::string& controllerId)
1192{
1193 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1194 {
Ed Tanous62598e32023-07-17 17:06:25 -07001195 BMCWEB_LOG_DEBUG("Failed to setup Redfish Route for StorageController");
Willy Tu61b1eb22023-03-14 11:29:50 -07001196 return;
1197 }
1198 if (systemName != "system")
1199 {
1200 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1201 systemName);
Ed Tanous62598e32023-07-17 17:06:25 -07001202 BMCWEB_LOG_DEBUG("Failed to find ComputerSystem of {}", systemName);
Willy Tu61b1eb22023-03-14 11:29:50 -07001203 return;
1204 }
1205 constexpr std::array<std::string_view, 1> interfaces = {
1206 "xyz.openbmc_project.Inventory.Item.StorageController"};
1207 dbus::utility::getSubTree(
1208 "/xyz/openbmc_project/inventory", 0, interfaces,
1209 [asyncResp,
1210 controllerId](const boost::system::error_code& ec,
1211 const dbus::utility::MapperGetSubTreeResponse& subtree) {
1212 getStorageControllerHandler(asyncResp, controllerId, ec, subtree);
1213 });
1214}
1215
1216inline void requestRoutesStorageControllerCollection(App& app)
1217{
1218 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/")
1219 .privileges(redfish::privileges::getStorageControllerCollection)
Ed Tanous36d52332023-06-09 13:18:40 -07001220 .methods(boost::beast::http::verb::get)(std::bind_front(
1221 handleSystemsStorageControllerCollectionGet, std::ref(app)));
Willy Tu61b1eb22023-03-14 11:29:50 -07001222}
1223
1224inline void requestRoutesStorageController(App& app)
1225{
1226 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Controllers/<str>")
1227 .privileges(redfish::privileges::getStorageController)
1228 .methods(boost::beast::http::verb::get)(
Ed Tanous36d52332023-06-09 13:18:40 -07001229 std::bind_front(handleSystemsStorageControllerGet, std::ref(app)));
Willy Tu61b1eb22023-03-14 11:29:50 -07001230}
1231
Nikhil Potadea25aecc2019-08-23 16:35:26 -07001232} // namespace redfish