blob: 35cd6e14f1b5b58e71521af66c0c8e07b19bf378 [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
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080019#include "dbus_utility.hpp"
James Feist2ad9c2f2019-10-29 16:26:48 -070020#include "health.hpp"
James Feiste284a7c2019-11-20 16:20:23 -080021#include "openbmc_dbus_rest.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080022#include "query.hpp"
23#include "registries/privilege_registry.hpp"
24#include "utils/dbus_utils.hpp"
James Feist2ad9c2f2019-10-29 16:26:48 -070025
George Liue99073f2022-12-09 11:06:16 +080026#include <boost/system/error_code.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070027#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020028#include <sdbusplus/unpack_properties.hpp>
Nikhil Potadea25aecc2019-08-23 16:35:26 -070029
George Liu7a1dbc42022-12-07 16:03:22 +080030#include <array>
31#include <string_view>
32
Nikhil Potadea25aecc2019-08-23 16:35:26 -070033namespace redfish
34{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070035inline void requestRoutesStorageCollection(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -070036{
Ed Tanous22d268c2022-05-19 09:39:07 -070037 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
Ed Tanoused398212021-06-09 17:05:54 -070038 .privileges(redfish::privileges::getStorageCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070039 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -070040 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -070041 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
42 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +000043 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -070044 {
45 return;
46 }
Ed Tanous22d268c2022-05-19 09:39:07 -070047 if (systemName != "system")
48 {
49 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
50 systemName);
51 return;
52 }
53
Ed Tanous002d39b2022-05-31 08:59:27 -070054 asyncResp->res.jsonValue["@odata.type"] =
55 "#StorageCollection.StorageCollection";
56 asyncResp->res.jsonValue["@odata.id"] =
57 "/redfish/v1/Systems/system/Storage";
58 asyncResp->res.jsonValue["Name"] = "Storage Collection";
59 nlohmann::json::array_t members;
60 nlohmann::json::object_t member;
61 member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
62 members.emplace_back(member);
63 asyncResp->res.jsonValue["Members"] = std::move(members);
64 asyncResp->res.jsonValue["Members@odata.count"] = 1;
65 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -070066}
Nikhil Potadea25aecc2019-08-23 16:35:26 -070067
Willy Tua85afbe2021-12-28 14:43:47 -080068inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
69 const std::shared_ptr<HealthPopulate>& health)
70{
George Liu7a1dbc42022-12-07 16:03:22 +080071 const std::array<std::string_view, 1> interfaces = {
72 "xyz.openbmc_project.Inventory.Item.Drive"};
73 dbus::utility::getSubTreePaths(
74 "/xyz/openbmc_project/inventory", 0, interfaces,
Willy Tua85afbe2021-12-28 14:43:47 -080075 [asyncResp, health](
George Liu7a1dbc42022-12-07 16:03:22 +080076 const boost::system::error_code& ec,
Willy Tua85afbe2021-12-28 14:43:47 -080077 const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
78 if (ec)
79 {
80 BMCWEB_LOG_ERROR << "Drive mapper call error";
81 messages::internalError(asyncResp->res);
82 return;
83 }
84
85 nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
86 driveArray = nlohmann::json::array();
87 auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
88 count = 0;
89
90 health->inventory.insert(health->inventory.end(), driveList.begin(),
91 driveList.end());
92
93 for (const std::string& drive : driveList)
94 {
95 sdbusplus::message::object_path object(drive);
96 if (object.filename().empty())
97 {
98 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
99 return;
100 }
101
102 nlohmann::json::object_t driveJson;
103 driveJson["@odata.id"] =
104 "/redfish/v1/Systems/system/Storage/1/Drives/" +
105 object.filename();
106 driveArray.push_back(std::move(driveJson));
107 }
108
109 count = driveArray.size();
George Liu7a1dbc42022-12-07 16:03:22 +0800110 });
Willy Tua85afbe2021-12-28 14:43:47 -0800111}
112
113inline void
114 getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
115 const std::shared_ptr<HealthPopulate>& health)
116{
George Liue99073f2022-12-09 11:06:16 +0800117 constexpr std::array<std::string_view, 1> interfaces = {
118 "xyz.openbmc_project.Inventory.Item.StorageController"};
119 dbus::utility::getSubTree(
120 "/xyz/openbmc_project/inventory", 0, interfaces,
Willy Tua85afbe2021-12-28 14:43:47 -0800121 [asyncResp,
George Liue99073f2022-12-09 11:06:16 +0800122 health](const boost::system::error_code& ec,
Willy Tua85afbe2021-12-28 14:43:47 -0800123 const dbus::utility::MapperGetSubTreeResponse& subtree) {
124 if (ec || subtree.empty())
125 {
126 // doesn't have to be there
127 return;
128 }
129
130 nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
131 root = nlohmann::json::array();
132 for (const auto& [path, interfaceDict] : subtree)
133 {
134 sdbusplus::message::object_path object(path);
135 std::string id = object.filename();
136 if (id.empty())
137 {
138 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
139 return;
140 }
141
142 if (interfaceDict.size() != 1)
143 {
144 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
145 << ", greater than 1";
146 messages::internalError(asyncResp->res);
147 return;
148 }
149
150 const std::string& connectionName = interfaceDict.front().first;
151
152 size_t index = root.size();
153 nlohmann::json& storageController =
154 root.emplace_back(nlohmann::json::object());
155
156 storageController["@odata.type"] =
157 "#Storage.v1_7_0.StorageController";
158 storageController["@odata.id"] =
159 "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
160 std::to_string(index);
161 storageController["Name"] = id;
162 storageController["MemberId"] = id;
163 storageController["Status"]["State"] = "Enabled";
164
165 sdbusplus::asio::getProperty<bool>(
166 *crow::connections::systemBus, connectionName, path,
167 "xyz.openbmc_project.Inventory.Item", "Present",
168 [asyncResp, index](const boost::system::error_code ec2,
Willy Tucef57e82022-12-15 16:42:02 -0800169 bool isPresent) {
Willy Tua85afbe2021-12-28 14:43:47 -0800170 // this interface isn't necessary, only check it
171 // if we get a good return
172 if (ec2)
173 {
174 return;
175 }
Willy Tucef57e82022-12-15 16:42:02 -0800176 if (!isPresent)
Willy Tua85afbe2021-12-28 14:43:47 -0800177 {
178 asyncResp->res.jsonValue["StorageControllers"][index]
Willy Tucef57e82022-12-15 16:42:02 -0800179 ["Status"]["State"] = "Absent";
Willy Tua85afbe2021-12-28 14:43:47 -0800180 }
181 });
182
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200183 sdbusplus::asio::getAllProperties(
184 *crow::connections::systemBus, connectionName, path,
185 "xyz.openbmc_project.Inventory.Decorator.Asset",
Willy Tua85afbe2021-12-28 14:43:47 -0800186 [asyncResp, index](
187 const boost::system::error_code ec2,
188 const std::vector<
189 std::pair<std::string, dbus::utility::DbusVariantType>>&
190 propertiesList) {
191 if (ec2)
192 {
193 // this interface isn't necessary
194 return;
195 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200196
197 const std::string* partNumber = nullptr;
198 const std::string* serialNumber = nullptr;
199 const std::string* manufacturer = nullptr;
200 const std::string* model = nullptr;
201
202 const bool success = sdbusplus::unpackPropertiesNoThrow(
203 dbus_utils::UnpackErrorPrinter(), propertiesList,
204 "PartNumber", partNumber, "SerialNumber", serialNumber,
205 "Manufacturer", manufacturer, "Model", model);
206
207 if (!success)
Willy Tua85afbe2021-12-28 14:43:47 -0800208 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200209 messages::internalError(asyncResp->res);
210 return;
Willy Tua85afbe2021-12-28 14:43:47 -0800211 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200212
213 nlohmann::json& controller =
214 asyncResp->res.jsonValue["StorageControllers"][index];
215
216 if (partNumber != nullptr)
217 {
218 controller["PartNumber"] = *partNumber;
219 }
220
221 if (serialNumber != nullptr)
222 {
223 controller["SerialNumber"] = *serialNumber;
224 }
225
226 if (manufacturer != nullptr)
227 {
228 controller["Manufacturer"] = *manufacturer;
229 }
230
231 if (model != nullptr)
232 {
233 controller["Model"] = *model;
234 }
235 });
Willy Tua85afbe2021-12-28 14:43:47 -0800236 }
237
238 // this is done after we know the json array will no longer
239 // be resized, as json::array uses vector underneath and we
240 // need references to its members that won't change
241 size_t count = 0;
242 // Pointer based on |asyncResp->res.jsonValue|
243 nlohmann::json::json_pointer rootPtr =
244 "/StorageControllers"_json_pointer;
245 for (const auto& [path, interfaceDict] : subtree)
246 {
247 auto subHealth = std::make_shared<HealthPopulate>(
248 asyncResp, rootPtr / count / "Status");
249 subHealth->inventory.emplace_back(path);
250 health->inventory.emplace_back(path);
251 health->children.emplace_back(subHealth);
252 count++;
253 }
George Liue99073f2022-12-09 11:06:16 +0800254 });
Willy Tua85afbe2021-12-28 14:43:47 -0800255}
256
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700257inline void requestRoutesStorage(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700258{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700259 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
Ed Tanoused398212021-06-09 17:05:54 -0700260 .privileges(redfish::privileges::getStorage)
Ed Tanous002d39b2022-05-31 08:59:27 -0700261 .methods(boost::beast::http::verb::get)(
262 [&app](const crow::Request& req,
263 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000264 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700265 {
266 return;
267 }
268 asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
269 asyncResp->res.jsonValue["@odata.id"] =
270 "/redfish/v1/Systems/system/Storage/1";
271 asyncResp->res.jsonValue["Name"] = "Storage";
272 asyncResp->res.jsonValue["Id"] = "1";
273 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
274
275 auto health = std::make_shared<HealthPopulate>(asyncResp);
276 health->populate();
277
Willy Tua85afbe2021-12-28 14:43:47 -0800278 getDrives(asyncResp, health);
279 getStorageControllers(asyncResp, health);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700280 });
281}
282
Willy Tu03913172021-11-08 02:03:19 -0800283inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
284 const std::string& connectionName,
285 const std::string& path)
286{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200287 sdbusplus::asio::getAllProperties(
288 *crow::connections::systemBus, connectionName, path,
289 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous168e20c2021-12-13 14:39:53 -0800290 [asyncResp](const boost::system::error_code ec,
291 const std::vector<
292 std::pair<std::string, dbus::utility::DbusVariantType>>&
293 propertiesList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700294 if (ec)
295 {
296 // this interface isn't necessary
297 return;
298 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200299
300 const std::string* partNumber = nullptr;
301 const std::string* serialNumber = nullptr;
302 const std::string* manufacturer = nullptr;
303 const std::string* model = nullptr;
304
305 const bool success = sdbusplus::unpackPropertiesNoThrow(
306 dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
307 partNumber, "SerialNumber", serialNumber, "Manufacturer",
308 manufacturer, "Model", model);
309
310 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700311 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200312 messages::internalError(asyncResp->res);
313 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700314 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200315
316 if (partNumber != nullptr)
317 {
318 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
319 }
320
321 if (serialNumber != nullptr)
322 {
323 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
324 }
325
326 if (manufacturer != nullptr)
327 {
328 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
329 }
330
331 if (model != nullptr)
332 {
333 asyncResp->res.jsonValue["Model"] = *model;
334 }
335 });
Willy Tu03913172021-11-08 02:03:19 -0800336}
337
338inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
339 const std::string& connectionName,
340 const std::string& path)
341{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700342 sdbusplus::asio::getProperty<bool>(
343 *crow::connections::systemBus, connectionName, path,
344 "xyz.openbmc_project.Inventory.Item", "Present",
Willy Tu03913172021-11-08 02:03:19 -0800345 [asyncResp, path](const boost::system::error_code ec,
Willy Tucef57e82022-12-15 16:42:02 -0800346 const bool isPresent) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700347 // this interface isn't necessary, only check it if
348 // we get a good return
349 if (ec)
350 {
351 return;
352 }
Willy Tu03913172021-11-08 02:03:19 -0800353
Willy Tucef57e82022-12-15 16:42:02 -0800354 if (!isPresent)
Ed Tanous002d39b2022-05-31 08:59:27 -0700355 {
Willy Tucef57e82022-12-15 16:42:02 -0800356 asyncResp->res.jsonValue["Status"]["State"] = "Absent";
Ed Tanous002d39b2022-05-31 08:59:27 -0700357 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700358 });
Willy Tu03913172021-11-08 02:03:19 -0800359}
360
361inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
362 const std::string& connectionName,
363 const std::string& path)
364{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700365 sdbusplus::asio::getProperty<bool>(
366 *crow::connections::systemBus, connectionName, path,
367 "xyz.openbmc_project.State.Drive", "Rebuilding",
368 [asyncResp](const boost::system::error_code ec, const bool updating) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700369 // this interface isn't necessary, only check it
370 // if we get a good return
371 if (ec)
372 {
373 return;
374 }
Willy Tu03913172021-11-08 02:03:19 -0800375
Ed Tanous002d39b2022-05-31 08:59:27 -0700376 // updating and disabled in the backend shouldn't be
377 // able to be set at the same time, so we don't need
378 // to check for the race condition of these two
379 // calls
380 if (updating)
381 {
382 asyncResp->res.jsonValue["Status"]["State"] = "Updating";
383 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700384 });
Willy Tu03913172021-11-08 02:03:19 -0800385}
386
Willy Tu19b8e9a2021-11-08 02:55:03 -0800387inline std::optional<std::string> convertDriveType(const std::string& type)
388{
389 if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
390 {
391 return "HDD";
392 }
393 if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
394 {
395 return "SSD";
396 }
397
398 return std::nullopt;
399}
400
401inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
402{
403 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
404 {
405 return "SAS";
406 }
407 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
408 {
409 return "SATA";
410 }
411 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
412 {
413 return "NVMe";
414 }
415 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
416 {
417 return "FC";
418 }
419
420 return std::nullopt;
421}
422
423inline void
424 getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
425 const std::string& connectionName,
426 const std::string& path)
427{
428 sdbusplus::asio::getAllProperties(
429 *crow::connections::systemBus, connectionName, path,
430 "xyz.openbmc_project.Inventory.Item.Drive",
431 [asyncResp](const boost::system::error_code ec,
432 const std::vector<
433 std::pair<std::string, dbus::utility::DbusVariantType>>&
434 propertiesList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700435 if (ec)
436 {
437 // this interface isn't required
438 return;
439 }
440 for (const std::pair<std::string, dbus::utility::DbusVariantType>&
441 property : propertiesList)
442 {
443 const std::string& propertyName = property.first;
444 if (propertyName == "Type")
Willy Tu19b8e9a2021-11-08 02:55:03 -0800445 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700446 const std::string* value =
447 std::get_if<std::string>(&property.second);
448 if (value == nullptr)
449 {
450 // illegal property
451 BMCWEB_LOG_ERROR << "Illegal property: Type";
452 messages::internalError(asyncResp->res);
453 return;
454 }
455
456 std::optional<std::string> mediaType = convertDriveType(*value);
457 if (!mediaType)
458 {
459 BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
460 << *value;
461 messages::internalError(asyncResp->res);
462 return;
463 }
464
465 asyncResp->res.jsonValue["MediaType"] = *mediaType;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800466 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700467 else if (propertyName == "Capacity")
Willy Tu19b8e9a2021-11-08 02:55:03 -0800468 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700469 const uint64_t* capacity =
470 std::get_if<uint64_t>(&property.second);
471 if (capacity == nullptr)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800472 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700473 BMCWEB_LOG_ERROR << "Illegal property: Capacity";
474 messages::internalError(asyncResp->res);
475 return;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800476 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700477 if (*capacity == 0)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800478 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700479 // drive capacity not known
480 continue;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800481 }
Willy Tu19b8e9a2021-11-08 02:55:03 -0800482
Ed Tanous002d39b2022-05-31 08:59:27 -0700483 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800484 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700485 else if (propertyName == "Protocol")
486 {
487 const std::string* value =
488 std::get_if<std::string>(&property.second);
489 if (value == nullptr)
490 {
491 BMCWEB_LOG_ERROR << "Illegal property: Protocol";
492 messages::internalError(asyncResp->res);
493 return;
494 }
495
496 std::optional<std::string> proto = convertDriveProtocol(*value);
497 if (!proto)
498 {
499 BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: "
500 << *value;
501 messages::internalError(asyncResp->res);
502 return;
503 }
504 asyncResp->res.jsonValue["Protocol"] = *proto;
505 }
John Edward Broadbent3fe4d5c2022-05-06 14:42:35 -0700506 else if (propertyName == "PredictedMediaLifeLeftPercent")
507 {
508 const uint8_t* lifeLeft =
509 std::get_if<uint8_t>(&property.second);
510 if (lifeLeft == nullptr)
511 {
512 BMCWEB_LOG_ERROR
513 << "Illegal property: PredictedMediaLifeLeftPercent";
514 messages::internalError(asyncResp->res);
515 return;
516 }
517 // 255 means reading the value is not supported
518 if (*lifeLeft != 255)
519 {
520 asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
521 *lifeLeft;
522 }
523 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700524 }
Willy Tu19b8e9a2021-11-08 02:55:03 -0800525 });
526}
527
Nan Zhoub53dcd92022-06-21 17:47:50 +0000528static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
529 const std::string& connectionName,
530 const std::string& path,
531 const std::vector<std::string>& interfaces)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700532{
533 for (const std::string& interface : interfaces)
534 {
535 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
536 {
537 getDriveAsset(asyncResp, connectionName, path);
538 }
539 else if (interface == "xyz.openbmc_project.Inventory.Item")
540 {
541 getDrivePresent(asyncResp, connectionName, path);
542 }
543 else if (interface == "xyz.openbmc_project.State.Drive")
544 {
545 getDriveState(asyncResp, connectionName, path);
546 }
547 else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
548 {
549 getDriveItemProperties(asyncResp, connectionName, path);
550 }
551 }
552}
553
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700554inline void requestRoutesDrive(App& app)
555{
Ed Tanous22d268c2022-05-19 09:39:07 -0700556 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700557 .privileges(redfish::privileges::getDrive)
Ed Tanous002d39b2022-05-31 08:59:27 -0700558 .methods(boost::beast::http::verb::get)(
559 [&app](const crow::Request& req,
560 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -0700561 const std::string& systemName, const std::string& driveId) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000562 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700563 {
564 return;
565 }
Ed Tanous22d268c2022-05-19 09:39:07 -0700566 if (systemName != "system")
567 {
568 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
569 systemName);
570 return;
571 }
572
George Liue99073f2022-12-09 11:06:16 +0800573 constexpr std::array<std::string_view, 1> interfaces = {
574 "xyz.openbmc_project.Inventory.Item.Drive"};
575 dbus::utility::getSubTree(
576 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -0700577 [asyncResp,
George Liue99073f2022-12-09 11:06:16 +0800578 driveId](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700579 const dbus::utility::MapperGetSubTreeResponse& subtree) {
580 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700581 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700582 BMCWEB_LOG_ERROR << "Drive mapper call error";
583 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -0700584 return;
585 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700586
Ed Tanous002d39b2022-05-31 08:59:27 -0700587 auto drive = std::find_if(
588 subtree.begin(), subtree.end(),
589 [&driveId](
Nan Zhou8cb65f82022-06-15 05:12:24 +0000590 const std::pair<std::string,
591 dbus::utility::MapperServiceMap>& object) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700592 return sdbusplus::message::object_path(object.first)
593 .filename() == driveId;
594 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700595
Ed Tanous002d39b2022-05-31 08:59:27 -0700596 if (drive == subtree.end())
597 {
598 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
599 return;
600 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700601
Ed Tanous002d39b2022-05-31 08:59:27 -0700602 const std::string& path = drive->first;
Nan Zhou8cb65f82022-06-15 05:12:24 +0000603 const dbus::utility::MapperServiceMap& connectionNames =
604 drive->second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700605
Ed Tanous002d39b2022-05-31 08:59:27 -0700606 asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
607 asyncResp->res.jsonValue["@odata.id"] =
608 "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId;
609 asyncResp->res.jsonValue["Name"] = driveId;
610 asyncResp->res.jsonValue["Id"] = driveId;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700611
Ed Tanous002d39b2022-05-31 08:59:27 -0700612 if (connectionNames.size() != 1)
613 {
614 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
615 << ", not equal to 1";
616 messages::internalError(asyncResp->res);
617 return;
618 }
James Feiste284a7c2019-11-20 16:20:23 -0800619
Ed Tanous002d39b2022-05-31 08:59:27 -0700620 getMainChassisId(
621 asyncResp, [](const std::string& chassisId,
622 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
623 aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
624 "/redfish/v1/Chassis/" + chassisId;
625 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700626
Ed Tanous002d39b2022-05-31 08:59:27 -0700627 // default it to Enabled
628 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700629
Ed Tanous002d39b2022-05-31 08:59:27 -0700630 auto health = std::make_shared<HealthPopulate>(asyncResp);
631 health->inventory.emplace_back(path);
632 health->populate();
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700633
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700634 addAllDriveInfo(asyncResp, connectionNames[0].first, path,
635 connectionNames[0].second);
George Liue99073f2022-12-09 11:06:16 +0800636 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700637 });
638}
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700639
640/**
641 * Chassis drives, this URL will show all the DriveCollection
642 * information
643 */
Nan Zhoub53dcd92022-06-21 17:47:50 +0000644inline void chassisDriveCollectionGet(
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700645 crow::App& app, const crow::Request& req,
646 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
647 const std::string& chassisId)
648{
Carson Labrado3ba00072022-06-06 19:40:56 +0000649 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700650 {
651 return;
652 }
653
654 // mapper call lambda
George Liue99073f2022-12-09 11:06:16 +0800655 constexpr std::array<std::string_view, 2> interfaces = {
656 "xyz.openbmc_project.Inventory.Item.Board",
657 "xyz.openbmc_project.Inventory.Item.Chassis"};
658 dbus::utility::getSubTree(
659 "/xyz/openbmc_project/inventory", 0, interfaces,
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700660 [asyncResp,
George Liue99073f2022-12-09 11:06:16 +0800661 chassisId](const boost::system::error_code& ec,
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700662 const dbus::utility::MapperGetSubTreeResponse& subtree) {
663 if (ec)
664 {
665 if (ec == boost::system::errc::host_unreachable)
666 {
667 messages::resourceNotFound(asyncResp->res, "Chassis",
668 chassisId);
669 return;
670 }
671 messages::internalError(asyncResp->res);
672 return;
673 }
674
675 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000676 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700677 {
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700678 sdbusplus::message::object_path objPath(path);
679 if (objPath.filename() != chassisId)
680 {
681 continue;
682 }
683
684 if (connectionNames.empty())
685 {
686 BMCWEB_LOG_ERROR << "Got 0 Connection names";
687 continue;
688 }
689
690 asyncResp->res.jsonValue["@odata.type"] =
691 "#DriveCollection.DriveCollection";
692 asyncResp->res.jsonValue["@odata.id"] =
John Edward Broadbent14bd7d92022-06-14 17:17:43 -0700693 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
694 chassisId, "Drives");
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700695 asyncResp->res.jsonValue["Name"] = "Drive Collection";
696
697 // Association lambda
698 sdbusplus::asio::getProperty<std::vector<std::string>>(
699 *crow::connections::systemBus,
700 "xyz.openbmc_project.ObjectMapper", path + "/drive",
701 "xyz.openbmc_project.Association", "endpoints",
702 [asyncResp, chassisId](const boost::system::error_code ec3,
703 const std::vector<std::string>& resp) {
704 if (ec3)
705 {
706 BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
707 }
708 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
709 // important if array is empty
710 members = nlohmann::json::array();
711
712 std::vector<std::string> leafNames;
713 for (const auto& drive : resp)
714 {
Ed Tanous8a592812022-06-04 09:06:59 -0700715 sdbusplus::message::object_path drivePath(drive);
716 leafNames.push_back(drivePath.filename());
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700717 }
718
719 std::sort(leafNames.begin(), leafNames.end(),
720 AlphanumLess<std::string>());
721
722 for (const auto& leafName : leafNames)
723 {
724 nlohmann::json::object_t member;
725 member["@odata.id"] = crow::utility::urlFromPieces(
726 "redfish", "v1", "Chassis", chassisId, "Drives",
727 leafName);
728 members.push_back(std::move(member));
729 // navigation links will be registered in next patch set
730 }
731 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
732 }); // end association lambda
733
734 } // end Iterate over all retrieved ObjectPaths
George Liue99073f2022-12-09 11:06:16 +0800735 });
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700736}
737
738inline void requestRoutesChassisDrive(App& app)
739{
740 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
741 .privileges(redfish::privileges::getDriveCollection)
742 .methods(boost::beast::http::verb::get)(
743 std::bind_front(chassisDriveCollectionGet, std::ref(app)));
744}
745
Nan Zhoub53dcd92022-06-21 17:47:50 +0000746inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
747 const std::string& chassisId,
748 const std::string& driveName,
749 const boost::system::error_code ec,
750 const dbus::utility::MapperGetSubTreeResponse& subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700751{
752
753 if (ec)
754 {
755 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
756 messages::internalError(asyncResp->res);
757 return;
758 }
759
760 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000761 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700762 {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700763 sdbusplus::message::object_path objPath(path);
764 if (objPath.filename() != driveName)
765 {
766 continue;
767 }
768
769 if (connectionNames.empty())
770 {
771 BMCWEB_LOG_ERROR << "Got 0 Connection names";
772 continue;
773 }
774
775 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
John Edward Broadbenta0cb40c2022-06-29 17:27:38 -0700776 "redfish", "v1", "Chassis", chassisId, "Drives", driveName);
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700777
778 asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
John Edward Broadbenta0cb40c2022-06-29 17:27:38 -0700779 asyncResp->res.jsonValue["Name"] = driveName;
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700780 asyncResp->res.jsonValue["Id"] = driveName;
781 // default it to Enabled
782 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
783
784 nlohmann::json::object_t linkChassisNav;
785 linkChassisNav["@odata.id"] =
786 crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId);
787 asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
788
789 addAllDriveInfo(asyncResp, connectionNames[0].first, path,
790 connectionNames[0].second);
791 }
792}
793
Nan Zhoub53dcd92022-06-21 17:47:50 +0000794inline void
795 matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
796 const std::string& chassisId,
797 const std::string& driveName,
798 const std::vector<std::string>& resp)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700799{
800
801 for (const std::string& drivePath : resp)
802 {
803 sdbusplus::message::object_path path(drivePath);
804 std::string leaf = path.filename();
805 if (leaf != driveName)
806 {
807 continue;
808 }
809 // mapper call drive
George Liue99073f2022-12-09 11:06:16 +0800810 constexpr std::array<std::string_view, 1> driveInterface = {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700811 "xyz.openbmc_project.Inventory.Item.Drive"};
George Liue99073f2022-12-09 11:06:16 +0800812 dbus::utility::getSubTree(
813 "/xyz/openbmc_project/inventory", 0, driveInterface,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700814 [asyncResp, chassisId, driveName](
George Liue99073f2022-12-09 11:06:16 +0800815 const boost::system::error_code& ec,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700816 const dbus::utility::MapperGetSubTreeResponse& subtree) {
817 buildDrive(asyncResp, chassisId, driveName, ec, subtree);
George Liue99073f2022-12-09 11:06:16 +0800818 });
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700819 }
820}
821
Nan Zhoub53dcd92022-06-21 17:47:50 +0000822inline void
823 handleChassisDriveGet(crow::App& app, const crow::Request& req,
824 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
825 const std::string& chassisId,
826 const std::string& driveName)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700827{
Michal Orzel03810a12022-06-15 14:04:28 +0200828 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700829 {
830 return;
831 }
George Liue99073f2022-12-09 11:06:16 +0800832 constexpr std::array<std::string_view, 2> interfaces = {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700833 "xyz.openbmc_project.Inventory.Item.Board",
834 "xyz.openbmc_project.Inventory.Item.Chassis"};
835
836 // mapper call chassis
George Liue99073f2022-12-09 11:06:16 +0800837 dbus::utility::getSubTree(
838 "/xyz/openbmc_project/inventory", 0, interfaces,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700839 [asyncResp, chassisId,
George Liue99073f2022-12-09 11:06:16 +0800840 driveName](const boost::system::error_code& ec,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700841 const dbus::utility::MapperGetSubTreeResponse& subtree) {
842 if (ec)
843 {
844 messages::internalError(asyncResp->res);
845 return;
846 }
847
848 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000849 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700850 {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700851 sdbusplus::message::object_path objPath(path);
852 if (objPath.filename() != chassisId)
853 {
854 continue;
855 }
856
857 if (connectionNames.empty())
858 {
859 BMCWEB_LOG_ERROR << "Got 0 Connection names";
860 continue;
861 }
862
863 sdbusplus::asio::getProperty<std::vector<std::string>>(
864 *crow::connections::systemBus,
865 "xyz.openbmc_project.ObjectMapper", path + "/drive",
866 "xyz.openbmc_project.Association", "endpoints",
867 [asyncResp, chassisId,
868 driveName](const boost::system::error_code ec3,
869 const std::vector<std::string>& resp) {
870 if (ec3)
871 {
872 return; // no drives = no failures
873 }
874 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
875 });
876 break;
877 }
George Liue99073f2022-12-09 11:06:16 +0800878 });
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700879}
880
881/**
882 * This URL will show the drive interface for the specific drive in the chassis
883 */
884inline void requestRoutesChassisDriveName(App& app)
885{
886 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
887 .privileges(redfish::privileges::getChassis)
888 .methods(boost::beast::http::verb::get)(
889 std::bind_front(handleChassisDriveGet, std::ref(app)));
890}
891
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700892} // namespace redfish