blob: cf1b441d0b2f396fb7de9a5a357aa52d809356d2 [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
James Feist2ad9c2f2019-10-29 16:26:48 -070018#include "health.hpp"
James Feiste284a7c2019-11-20 16:20:23 -080019#include "openbmc_dbus_rest.hpp"
James Feist2ad9c2f2019-10-29 16:26:48 -070020
John Edward Broadbent7e860f12021-04-08 15:57:16 -070021#include <app.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080022#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070023#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070024#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070025#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020026#include <sdbusplus/unpack_properties.hpp>
27#include <utils/dbus_utils.hpp>
Nikhil Potadea25aecc2019-08-23 16:35:26 -070028
29namespace redfish
30{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070031inline void requestRoutesStorageCollection(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -070032{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070033 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/")
Ed Tanoused398212021-06-09 17:05:54 -070034 .privileges(redfish::privileges::getStorageCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070035 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -070036 [&app](const crow::Request& req,
37 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +000038 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -070039 {
40 return;
41 }
42 asyncResp->res.jsonValue["@odata.type"] =
43 "#StorageCollection.StorageCollection";
44 asyncResp->res.jsonValue["@odata.id"] =
45 "/redfish/v1/Systems/system/Storage";
46 asyncResp->res.jsonValue["Name"] = "Storage Collection";
47 nlohmann::json::array_t members;
48 nlohmann::json::object_t member;
49 member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
50 members.emplace_back(member);
51 asyncResp->res.jsonValue["Members"] = std::move(members);
52 asyncResp->res.jsonValue["Members@odata.count"] = 1;
53 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -070054}
Nikhil Potadea25aecc2019-08-23 16:35:26 -070055
Willy Tua85afbe2021-12-28 14:43:47 -080056inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
57 const std::shared_ptr<HealthPopulate>& health)
58{
59 crow::connections::systemBus->async_method_call(
60 [asyncResp, health](
61 const boost::system::error_code ec,
62 const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
63 if (ec)
64 {
65 BMCWEB_LOG_ERROR << "Drive mapper call error";
66 messages::internalError(asyncResp->res);
67 return;
68 }
69
70 nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
71 driveArray = nlohmann::json::array();
72 auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
73 count = 0;
74
75 health->inventory.insert(health->inventory.end(), driveList.begin(),
76 driveList.end());
77
78 for (const std::string& drive : driveList)
79 {
80 sdbusplus::message::object_path object(drive);
81 if (object.filename().empty())
82 {
83 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
84 return;
85 }
86
87 nlohmann::json::object_t driveJson;
88 driveJson["@odata.id"] =
89 "/redfish/v1/Systems/system/Storage/1/Drives/" +
90 object.filename();
91 driveArray.push_back(std::move(driveJson));
92 }
93
94 count = driveArray.size();
95 },
96 "xyz.openbmc_project.ObjectMapper",
97 "/xyz/openbmc_project/object_mapper",
98 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
99 "/xyz/openbmc_project/inventory", int32_t(0),
100 std::array<const char*, 1>{"xyz.openbmc_project.Inventory.Item.Drive"});
101}
102
103inline void
104 getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
105 const std::shared_ptr<HealthPopulate>& health)
106{
107 crow::connections::systemBus->async_method_call(
108 [asyncResp,
109 health](const boost::system::error_code ec,
110 const dbus::utility::MapperGetSubTreeResponse& subtree) {
111 if (ec || subtree.empty())
112 {
113 // doesn't have to be there
114 return;
115 }
116
117 nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
118 root = nlohmann::json::array();
119 for (const auto& [path, interfaceDict] : subtree)
120 {
121 sdbusplus::message::object_path object(path);
122 std::string id = object.filename();
123 if (id.empty())
124 {
125 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
126 return;
127 }
128
129 if (interfaceDict.size() != 1)
130 {
131 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
132 << ", greater than 1";
133 messages::internalError(asyncResp->res);
134 return;
135 }
136
137 const std::string& connectionName = interfaceDict.front().first;
138
139 size_t index = root.size();
140 nlohmann::json& storageController =
141 root.emplace_back(nlohmann::json::object());
142
143 storageController["@odata.type"] =
144 "#Storage.v1_7_0.StorageController";
145 storageController["@odata.id"] =
146 "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
147 std::to_string(index);
148 storageController["Name"] = id;
149 storageController["MemberId"] = id;
150 storageController["Status"]["State"] = "Enabled";
151
152 sdbusplus::asio::getProperty<bool>(
153 *crow::connections::systemBus, connectionName, path,
154 "xyz.openbmc_project.Inventory.Item", "Present",
155 [asyncResp, index](const boost::system::error_code ec2,
156 bool enabled) {
157 // this interface isn't necessary, only check it
158 // if we get a good return
159 if (ec2)
160 {
161 return;
162 }
163 if (!enabled)
164 {
165 asyncResp->res.jsonValue["StorageControllers"][index]
166 ["Status"]["State"] = "Disabled";
167 }
168 });
169
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200170 sdbusplus::asio::getAllProperties(
171 *crow::connections::systemBus, connectionName, path,
172 "xyz.openbmc_project.Inventory.Decorator.Asset",
Willy Tua85afbe2021-12-28 14:43:47 -0800173 [asyncResp, index](
174 const boost::system::error_code ec2,
175 const std::vector<
176 std::pair<std::string, dbus::utility::DbusVariantType>>&
177 propertiesList) {
178 if (ec2)
179 {
180 // this interface isn't necessary
181 return;
182 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200183
184 const std::string* partNumber = nullptr;
185 const std::string* serialNumber = nullptr;
186 const std::string* manufacturer = nullptr;
187 const std::string* model = nullptr;
188
189 const bool success = sdbusplus::unpackPropertiesNoThrow(
190 dbus_utils::UnpackErrorPrinter(), propertiesList,
191 "PartNumber", partNumber, "SerialNumber", serialNumber,
192 "Manufacturer", manufacturer, "Model", model);
193
194 if (!success)
Willy Tua85afbe2021-12-28 14:43:47 -0800195 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200196 messages::internalError(asyncResp->res);
197 return;
Willy Tua85afbe2021-12-28 14:43:47 -0800198 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200199
200 nlohmann::json& controller =
201 asyncResp->res.jsonValue["StorageControllers"][index];
202
203 if (partNumber != nullptr)
204 {
205 controller["PartNumber"] = *partNumber;
206 }
207
208 if (serialNumber != nullptr)
209 {
210 controller["SerialNumber"] = *serialNumber;
211 }
212
213 if (manufacturer != nullptr)
214 {
215 controller["Manufacturer"] = *manufacturer;
216 }
217
218 if (model != nullptr)
219 {
220 controller["Model"] = *model;
221 }
222 });
Willy Tua85afbe2021-12-28 14:43:47 -0800223 }
224
225 // this is done after we know the json array will no longer
226 // be resized, as json::array uses vector underneath and we
227 // need references to its members that won't change
228 size_t count = 0;
229 // Pointer based on |asyncResp->res.jsonValue|
230 nlohmann::json::json_pointer rootPtr =
231 "/StorageControllers"_json_pointer;
232 for (const auto& [path, interfaceDict] : subtree)
233 {
234 auto subHealth = std::make_shared<HealthPopulate>(
235 asyncResp, rootPtr / count / "Status");
236 subHealth->inventory.emplace_back(path);
237 health->inventory.emplace_back(path);
238 health->children.emplace_back(subHealth);
239 count++;
240 }
241 },
242 "xyz.openbmc_project.ObjectMapper",
243 "/xyz/openbmc_project/object_mapper",
244 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
245 "/xyz/openbmc_project/inventory", int32_t(0),
246 std::array<const char*, 1>{
247 "xyz.openbmc_project.Inventory.Item.StorageController"});
248}
249
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700250inline void requestRoutesStorage(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700251{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700252 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
Ed Tanoused398212021-06-09 17:05:54 -0700253 .privileges(redfish::privileges::getStorage)
Ed Tanous002d39b2022-05-31 08:59:27 -0700254 .methods(boost::beast::http::verb::get)(
255 [&app](const crow::Request& req,
256 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000257 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700258 {
259 return;
260 }
261 asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
262 asyncResp->res.jsonValue["@odata.id"] =
263 "/redfish/v1/Systems/system/Storage/1";
264 asyncResp->res.jsonValue["Name"] = "Storage";
265 asyncResp->res.jsonValue["Id"] = "1";
266 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
267
268 auto health = std::make_shared<HealthPopulate>(asyncResp);
269 health->populate();
270
Willy Tua85afbe2021-12-28 14:43:47 -0800271 getDrives(asyncResp, health);
272 getStorageControllers(asyncResp, health);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700273 });
274}
275
Willy Tu03913172021-11-08 02:03:19 -0800276inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
277 const std::string& connectionName,
278 const std::string& path)
279{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200280 sdbusplus::asio::getAllProperties(
281 *crow::connections::systemBus, connectionName, path,
282 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous168e20c2021-12-13 14:39:53 -0800283 [asyncResp](const boost::system::error_code ec,
284 const std::vector<
285 std::pair<std::string, dbus::utility::DbusVariantType>>&
286 propertiesList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700287 if (ec)
288 {
289 // this interface isn't necessary
290 return;
291 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200292
293 const std::string* partNumber = nullptr;
294 const std::string* serialNumber = nullptr;
295 const std::string* manufacturer = nullptr;
296 const std::string* model = nullptr;
297
298 const bool success = sdbusplus::unpackPropertiesNoThrow(
299 dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
300 partNumber, "SerialNumber", serialNumber, "Manufacturer",
301 manufacturer, "Model", model);
302
303 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700304 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200305 messages::internalError(asyncResp->res);
306 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700307 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200308
309 if (partNumber != nullptr)
310 {
311 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
312 }
313
314 if (serialNumber != nullptr)
315 {
316 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
317 }
318
319 if (manufacturer != nullptr)
320 {
321 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
322 }
323
324 if (model != nullptr)
325 {
326 asyncResp->res.jsonValue["Model"] = *model;
327 }
328 });
Willy Tu03913172021-11-08 02:03:19 -0800329}
330
331inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
332 const std::string& connectionName,
333 const std::string& path)
334{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700335 sdbusplus::asio::getProperty<bool>(
336 *crow::connections::systemBus, connectionName, path,
337 "xyz.openbmc_project.Inventory.Item", "Present",
Willy Tu03913172021-11-08 02:03:19 -0800338 [asyncResp, path](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700339 const bool enabled) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700340 // this interface isn't necessary, only check it if
341 // we get a good return
342 if (ec)
343 {
344 return;
345 }
Willy Tu03913172021-11-08 02:03:19 -0800346
Ed Tanous002d39b2022-05-31 08:59:27 -0700347 if (!enabled)
348 {
349 asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
350 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700351 });
Willy Tu03913172021-11-08 02:03:19 -0800352}
353
354inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
355 const std::string& connectionName,
356 const std::string& path)
357{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700358 sdbusplus::asio::getProperty<bool>(
359 *crow::connections::systemBus, connectionName, path,
360 "xyz.openbmc_project.State.Drive", "Rebuilding",
361 [asyncResp](const boost::system::error_code ec, const bool updating) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700362 // this interface isn't necessary, only check it
363 // if we get a good return
364 if (ec)
365 {
366 return;
367 }
Willy Tu03913172021-11-08 02:03:19 -0800368
Ed Tanous002d39b2022-05-31 08:59:27 -0700369 // updating and disabled in the backend shouldn't be
370 // able to be set at the same time, so we don't need
371 // to check for the race condition of these two
372 // calls
373 if (updating)
374 {
375 asyncResp->res.jsonValue["Status"]["State"] = "Updating";
376 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700377 });
Willy Tu03913172021-11-08 02:03:19 -0800378}
379
Willy Tu19b8e9a2021-11-08 02:55:03 -0800380inline std::optional<std::string> convertDriveType(const std::string& type)
381{
382 if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
383 {
384 return "HDD";
385 }
386 if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
387 {
388 return "SSD";
389 }
390
391 return std::nullopt;
392}
393
394inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
395{
396 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
397 {
398 return "SAS";
399 }
400 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
401 {
402 return "SATA";
403 }
404 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
405 {
406 return "NVMe";
407 }
408 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
409 {
410 return "FC";
411 }
412
413 return std::nullopt;
414}
415
416inline void
417 getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
418 const std::string& connectionName,
419 const std::string& path)
420{
421 sdbusplus::asio::getAllProperties(
422 *crow::connections::systemBus, connectionName, path,
423 "xyz.openbmc_project.Inventory.Item.Drive",
424 [asyncResp](const boost::system::error_code ec,
425 const std::vector<
426 std::pair<std::string, dbus::utility::DbusVariantType>>&
427 propertiesList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700428 if (ec)
429 {
430 // this interface isn't required
431 return;
432 }
433 for (const std::pair<std::string, dbus::utility::DbusVariantType>&
434 property : propertiesList)
435 {
436 const std::string& propertyName = property.first;
437 if (propertyName == "Type")
Willy Tu19b8e9a2021-11-08 02:55:03 -0800438 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700439 const std::string* value =
440 std::get_if<std::string>(&property.second);
441 if (value == nullptr)
442 {
443 // illegal property
444 BMCWEB_LOG_ERROR << "Illegal property: Type";
445 messages::internalError(asyncResp->res);
446 return;
447 }
448
449 std::optional<std::string> mediaType = convertDriveType(*value);
450 if (!mediaType)
451 {
452 BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
453 << *value;
454 messages::internalError(asyncResp->res);
455 return;
456 }
457
458 asyncResp->res.jsonValue["MediaType"] = *mediaType;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800459 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700460 else if (propertyName == "Capacity")
Willy Tu19b8e9a2021-11-08 02:55:03 -0800461 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700462 const uint64_t* capacity =
463 std::get_if<uint64_t>(&property.second);
464 if (capacity == nullptr)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800465 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700466 BMCWEB_LOG_ERROR << "Illegal property: Capacity";
467 messages::internalError(asyncResp->res);
468 return;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800469 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700470 if (*capacity == 0)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800471 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700472 // drive capacity not known
473 continue;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800474 }
Willy Tu19b8e9a2021-11-08 02:55:03 -0800475
Ed Tanous002d39b2022-05-31 08:59:27 -0700476 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800477 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700478 else if (propertyName == "Protocol")
479 {
480 const std::string* value =
481 std::get_if<std::string>(&property.second);
482 if (value == nullptr)
483 {
484 BMCWEB_LOG_ERROR << "Illegal property: Protocol";
485 messages::internalError(asyncResp->res);
486 return;
487 }
488
489 std::optional<std::string> proto = convertDriveProtocol(*value);
490 if (!proto)
491 {
492 BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: "
493 << *value;
494 messages::internalError(asyncResp->res);
495 return;
496 }
497 asyncResp->res.jsonValue["Protocol"] = *proto;
498 }
John Edward Broadbent3fe4d5c2022-05-06 14:42:35 -0700499 else if (propertyName == "PredictedMediaLifeLeftPercent")
500 {
501 const uint8_t* lifeLeft =
502 std::get_if<uint8_t>(&property.second);
503 if (lifeLeft == nullptr)
504 {
505 BMCWEB_LOG_ERROR
506 << "Illegal property: PredictedMediaLifeLeftPercent";
507 messages::internalError(asyncResp->res);
508 return;
509 }
510 // 255 means reading the value is not supported
511 if (*lifeLeft != 255)
512 {
513 asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
514 *lifeLeft;
515 }
516 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700517 }
Willy Tu19b8e9a2021-11-08 02:55:03 -0800518 });
519}
520
Nan Zhoub53dcd92022-06-21 17:47:50 +0000521static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
522 const std::string& connectionName,
523 const std::string& path,
524 const std::vector<std::string>& interfaces)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700525{
526 for (const std::string& interface : interfaces)
527 {
528 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
529 {
530 getDriveAsset(asyncResp, connectionName, path);
531 }
532 else if (interface == "xyz.openbmc_project.Inventory.Item")
533 {
534 getDrivePresent(asyncResp, connectionName, path);
535 }
536 else if (interface == "xyz.openbmc_project.State.Drive")
537 {
538 getDriveState(asyncResp, connectionName, path);
539 }
540 else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
541 {
542 getDriveItemProperties(asyncResp, connectionName, path);
543 }
544 }
545}
546
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700547inline void requestRoutesDrive(App& app)
548{
549 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/Drives/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700550 .privileges(redfish::privileges::getDrive)
Ed Tanous002d39b2022-05-31 08:59:27 -0700551 .methods(boost::beast::http::verb::get)(
552 [&app](const crow::Request& req,
553 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
554 const std::string& driveId) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000555 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700556 {
557 return;
558 }
559 crow::connections::systemBus->async_method_call(
560 [asyncResp,
561 driveId](const boost::system::error_code ec,
562 const dbus::utility::MapperGetSubTreeResponse& subtree) {
563 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700564 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700565 BMCWEB_LOG_ERROR << "Drive mapper call error";
566 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -0700567 return;
568 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700569
Ed Tanous002d39b2022-05-31 08:59:27 -0700570 auto drive = std::find_if(
571 subtree.begin(), subtree.end(),
572 [&driveId](
Nan Zhou8cb65f82022-06-15 05:12:24 +0000573 const std::pair<std::string,
574 dbus::utility::MapperServiceMap>& object) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700575 return sdbusplus::message::object_path(object.first)
576 .filename() == driveId;
577 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700578
Ed Tanous002d39b2022-05-31 08:59:27 -0700579 if (drive == subtree.end())
580 {
581 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
582 return;
583 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700584
Ed Tanous002d39b2022-05-31 08:59:27 -0700585 const std::string& path = drive->first;
Nan Zhou8cb65f82022-06-15 05:12:24 +0000586 const dbus::utility::MapperServiceMap& connectionNames =
587 drive->second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700588
Ed Tanous002d39b2022-05-31 08:59:27 -0700589 asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
590 asyncResp->res.jsonValue["@odata.id"] =
591 "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId;
592 asyncResp->res.jsonValue["Name"] = driveId;
593 asyncResp->res.jsonValue["Id"] = driveId;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700594
Ed Tanous002d39b2022-05-31 08:59:27 -0700595 if (connectionNames.size() != 1)
596 {
597 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
598 << ", not equal to 1";
599 messages::internalError(asyncResp->res);
600 return;
601 }
James Feiste284a7c2019-11-20 16:20:23 -0800602
Ed Tanous002d39b2022-05-31 08:59:27 -0700603 getMainChassisId(
604 asyncResp, [](const std::string& chassisId,
605 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
606 aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
607 "/redfish/v1/Chassis/" + chassisId;
608 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700609
Ed Tanous002d39b2022-05-31 08:59:27 -0700610 // default it to Enabled
611 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700612
Ed Tanous002d39b2022-05-31 08:59:27 -0700613 auto health = std::make_shared<HealthPopulate>(asyncResp);
614 health->inventory.emplace_back(path);
615 health->populate();
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700616
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700617 addAllDriveInfo(asyncResp, connectionNames[0].first, path,
618 connectionNames[0].second);
Ed Tanous002d39b2022-05-31 08:59:27 -0700619 },
620 "xyz.openbmc_project.ObjectMapper",
621 "/xyz/openbmc_project/object_mapper",
622 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
623 "/xyz/openbmc_project/inventory", int32_t(0),
624 std::array<const char*, 1>{
625 "xyz.openbmc_project.Inventory.Item.Drive"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700626 });
627}
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700628
629/**
630 * Chassis drives, this URL will show all the DriveCollection
631 * information
632 */
Nan Zhoub53dcd92022-06-21 17:47:50 +0000633inline void chassisDriveCollectionGet(
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700634 crow::App& app, const crow::Request& req,
635 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
636 const std::string& chassisId)
637{
Carson Labrado3ba00072022-06-06 19:40:56 +0000638 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700639 {
640 return;
641 }
642
643 // mapper call lambda
644 crow::connections::systemBus->async_method_call(
645 [asyncResp,
646 chassisId](const boost::system::error_code ec,
647 const dbus::utility::MapperGetSubTreeResponse& subtree) {
648 if (ec)
649 {
650 if (ec == boost::system::errc::host_unreachable)
651 {
652 messages::resourceNotFound(asyncResp->res, "Chassis",
653 chassisId);
654 return;
655 }
656 messages::internalError(asyncResp->res);
657 return;
658 }
659
660 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000661 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700662 {
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700663 sdbusplus::message::object_path objPath(path);
664 if (objPath.filename() != chassisId)
665 {
666 continue;
667 }
668
669 if (connectionNames.empty())
670 {
671 BMCWEB_LOG_ERROR << "Got 0 Connection names";
672 continue;
673 }
674
675 asyncResp->res.jsonValue["@odata.type"] =
676 "#DriveCollection.DriveCollection";
677 asyncResp->res.jsonValue["@odata.id"] =
John Edward Broadbent14bd7d92022-06-14 17:17:43 -0700678 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
679 chassisId, "Drives");
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700680 asyncResp->res.jsonValue["Name"] = "Drive Collection";
681
682 // Association lambda
683 sdbusplus::asio::getProperty<std::vector<std::string>>(
684 *crow::connections::systemBus,
685 "xyz.openbmc_project.ObjectMapper", path + "/drive",
686 "xyz.openbmc_project.Association", "endpoints",
687 [asyncResp, chassisId](const boost::system::error_code ec3,
688 const std::vector<std::string>& resp) {
689 if (ec3)
690 {
691 BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
692 }
693 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
694 // important if array is empty
695 members = nlohmann::json::array();
696
697 std::vector<std::string> leafNames;
698 for (const auto& drive : resp)
699 {
Ed Tanous8a592812022-06-04 09:06:59 -0700700 sdbusplus::message::object_path drivePath(drive);
701 leafNames.push_back(drivePath.filename());
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700702 }
703
704 std::sort(leafNames.begin(), leafNames.end(),
705 AlphanumLess<std::string>());
706
707 for (const auto& leafName : leafNames)
708 {
709 nlohmann::json::object_t member;
710 member["@odata.id"] = crow::utility::urlFromPieces(
711 "redfish", "v1", "Chassis", chassisId, "Drives",
712 leafName);
713 members.push_back(std::move(member));
714 // navigation links will be registered in next patch set
715 }
716 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
717 }); // end association lambda
718
719 } // end Iterate over all retrieved ObjectPaths
720 },
721 "xyz.openbmc_project.ObjectMapper",
722 "/xyz/openbmc_project/object_mapper",
723 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
724 "/xyz/openbmc_project/inventory", 0,
725 std::array<const char*, 2>{
726 "xyz.openbmc_project.Inventory.Item.Board",
727 "xyz.openbmc_project.Inventory.Item.Chassis"});
728}
729
730inline void requestRoutesChassisDrive(App& app)
731{
732 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
733 .privileges(redfish::privileges::getDriveCollection)
734 .methods(boost::beast::http::verb::get)(
735 std::bind_front(chassisDriveCollectionGet, std::ref(app)));
736}
737
Nan Zhoub53dcd92022-06-21 17:47:50 +0000738inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
739 const std::string& chassisId,
740 const std::string& driveName,
741 const boost::system::error_code ec,
742 const dbus::utility::MapperGetSubTreeResponse& subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700743{
744
745 if (ec)
746 {
747 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
748 messages::internalError(asyncResp->res);
749 return;
750 }
751
752 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000753 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700754 {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700755 sdbusplus::message::object_path objPath(path);
756 if (objPath.filename() != driveName)
757 {
758 continue;
759 }
760
761 if (connectionNames.empty())
762 {
763 BMCWEB_LOG_ERROR << "Got 0 Connection names";
764 continue;
765 }
766
767 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
John Edward Broadbenta0cb40c2022-06-29 17:27:38 -0700768 "redfish", "v1", "Chassis", chassisId, "Drives", driveName);
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700769
770 asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
John Edward Broadbenta0cb40c2022-06-29 17:27:38 -0700771 asyncResp->res.jsonValue["Name"] = driveName;
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700772 asyncResp->res.jsonValue["Id"] = driveName;
773 // default it to Enabled
774 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
775
776 nlohmann::json::object_t linkChassisNav;
777 linkChassisNav["@odata.id"] =
778 crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId);
779 asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
780
781 addAllDriveInfo(asyncResp, connectionNames[0].first, path,
782 connectionNames[0].second);
783 }
784}
785
Nan Zhoub53dcd92022-06-21 17:47:50 +0000786inline void
787 matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
788 const std::string& chassisId,
789 const std::string& driveName,
790 const std::vector<std::string>& resp)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700791{
792
793 for (const std::string& drivePath : resp)
794 {
795 sdbusplus::message::object_path path(drivePath);
796 std::string leaf = path.filename();
797 if (leaf != driveName)
798 {
799 continue;
800 }
801 // mapper call drive
802 const std::array<const char*, 1> driveInterface = {
803 "xyz.openbmc_project.Inventory.Item.Drive"};
804
805 crow::connections::systemBus->async_method_call(
806 [asyncResp, chassisId, driveName](
807 const boost::system::error_code ec,
808 const dbus::utility::MapperGetSubTreeResponse& subtree) {
809 buildDrive(asyncResp, chassisId, driveName, ec, subtree);
810 },
811 "xyz.openbmc_project.ObjectMapper",
812 "/xyz/openbmc_project/object_mapper",
813 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
814 "/xyz/openbmc_project/inventory", 0, driveInterface);
815 }
816}
817
Nan Zhoub53dcd92022-06-21 17:47:50 +0000818inline void
819 handleChassisDriveGet(crow::App& app, const crow::Request& req,
820 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
821 const std::string& chassisId,
822 const std::string& driveName)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700823{
Michal Orzel03810a12022-06-15 14:04:28 +0200824 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700825 {
826 return;
827 }
828 const std::array<const char*, 2> interfaces = {
829 "xyz.openbmc_project.Inventory.Item.Board",
830 "xyz.openbmc_project.Inventory.Item.Chassis"};
831
832 // mapper call chassis
833 crow::connections::systemBus->async_method_call(
834 [asyncResp, chassisId,
835 driveName](const boost::system::error_code ec,
836 const dbus::utility::MapperGetSubTreeResponse& subtree) {
837 if (ec)
838 {
839 messages::internalError(asyncResp->res);
840 return;
841 }
842
843 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000844 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700845 {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700846 sdbusplus::message::object_path objPath(path);
847 if (objPath.filename() != chassisId)
848 {
849 continue;
850 }
851
852 if (connectionNames.empty())
853 {
854 BMCWEB_LOG_ERROR << "Got 0 Connection names";
855 continue;
856 }
857
858 sdbusplus::asio::getProperty<std::vector<std::string>>(
859 *crow::connections::systemBus,
860 "xyz.openbmc_project.ObjectMapper", path + "/drive",
861 "xyz.openbmc_project.Association", "endpoints",
862 [asyncResp, chassisId,
863 driveName](const boost::system::error_code ec3,
864 const std::vector<std::string>& resp) {
865 if (ec3)
866 {
867 return; // no drives = no failures
868 }
869 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
870 });
871 break;
872 }
873 },
874 "xyz.openbmc_project.ObjectMapper",
875 "/xyz/openbmc_project/object_mapper",
876 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
877 "/xyz/openbmc_project/inventory", 0, interfaces);
878}
879
880/**
881 * This URL will show the drive interface for the specific drive in the chassis
882 */
883inline void requestRoutesChassisDriveName(App& app)
884{
885 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
886 .privileges(redfish::privileges::getChassis)
887 .methods(boost::beast::http::verb::get)(
888 std::bind_front(handleChassisDriveGet, std::ref(app)));
889}
890
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700891} // namespace redfish