blob: 44ed7ff93c4a0d750f7b410d8f9c411bd0729a7c [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
George Liu7a1dbc42022-12-07 16:03:22 +080018#include "dbus_utility.hpp"
James Feist2ad9c2f2019-10-29 16:26:48 -070019#include "health.hpp"
James Feiste284a7c2019-11-20 16:20:23 -080020#include "openbmc_dbus_rest.hpp"
James Feist2ad9c2f2019-10-29 16:26:48 -070021
John Edward Broadbent7e860f12021-04-08 15:57:16 -070022#include <app.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
George Liu7a1dbc42022-12-07 16:03:22 +080029#include <array>
30#include <string_view>
31
Nikhil Potadea25aecc2019-08-23 16:35:26 -070032namespace redfish
33{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070034inline void requestRoutesStorageCollection(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -070035{
Ed Tanous22d268c2022-05-19 09:39:07 -070036 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
Ed Tanoused398212021-06-09 17:05:54 -070037 .privileges(redfish::privileges::getStorageCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070038 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -070039 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -070040 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
41 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +000042 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -070043 {
44 return;
45 }
Ed Tanous22d268c2022-05-19 09:39:07 -070046 if (systemName != "system")
47 {
48 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
49 systemName);
50 return;
51 }
52
Ed Tanous002d39b2022-05-31 08:59:27 -070053 asyncResp->res.jsonValue["@odata.type"] =
54 "#StorageCollection.StorageCollection";
55 asyncResp->res.jsonValue["@odata.id"] =
56 "/redfish/v1/Systems/system/Storage";
57 asyncResp->res.jsonValue["Name"] = "Storage Collection";
58 nlohmann::json::array_t members;
59 nlohmann::json::object_t member;
60 member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
61 members.emplace_back(member);
62 asyncResp->res.jsonValue["Members"] = std::move(members);
63 asyncResp->res.jsonValue["Members@odata.count"] = 1;
64 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -070065}
Nikhil Potadea25aecc2019-08-23 16:35:26 -070066
Willy Tua85afbe2021-12-28 14:43:47 -080067inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
68 const std::shared_ptr<HealthPopulate>& health)
69{
George Liu7a1dbc42022-12-07 16:03:22 +080070 const std::array<std::string_view, 1> interfaces = {
71 "xyz.openbmc_project.Inventory.Item.Drive"};
72 dbus::utility::getSubTreePaths(
73 "/xyz/openbmc_project/inventory", 0, interfaces,
Willy Tua85afbe2021-12-28 14:43:47 -080074 [asyncResp, health](
George Liu7a1dbc42022-12-07 16:03:22 +080075 const boost::system::error_code& ec,
Willy Tua85afbe2021-12-28 14:43:47 -080076 const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
77 if (ec)
78 {
79 BMCWEB_LOG_ERROR << "Drive mapper call error";
80 messages::internalError(asyncResp->res);
81 return;
82 }
83
84 nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
85 driveArray = nlohmann::json::array();
86 auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
87 count = 0;
88
89 health->inventory.insert(health->inventory.end(), driveList.begin(),
90 driveList.end());
91
92 for (const std::string& drive : driveList)
93 {
94 sdbusplus::message::object_path object(drive);
95 if (object.filename().empty())
96 {
97 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
98 return;
99 }
100
101 nlohmann::json::object_t driveJson;
102 driveJson["@odata.id"] =
103 "/redfish/v1/Systems/system/Storage/1/Drives/" +
104 object.filename();
105 driveArray.push_back(std::move(driveJson));
106 }
107
108 count = driveArray.size();
George Liu7a1dbc42022-12-07 16:03:22 +0800109 });
Willy Tua85afbe2021-12-28 14:43:47 -0800110}
111
112inline void
113 getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
114 const std::shared_ptr<HealthPopulate>& health)
115{
116 crow::connections::systemBus->async_method_call(
117 [asyncResp,
118 health](const boost::system::error_code ec,
119 const dbus::utility::MapperGetSubTreeResponse& subtree) {
120 if (ec || subtree.empty())
121 {
122 // doesn't have to be there
123 return;
124 }
125
126 nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
127 root = nlohmann::json::array();
128 for (const auto& [path, interfaceDict] : subtree)
129 {
130 sdbusplus::message::object_path object(path);
131 std::string id = object.filename();
132 if (id.empty())
133 {
134 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
135 return;
136 }
137
138 if (interfaceDict.size() != 1)
139 {
140 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
141 << ", greater than 1";
142 messages::internalError(asyncResp->res);
143 return;
144 }
145
146 const std::string& connectionName = interfaceDict.front().first;
147
148 size_t index = root.size();
149 nlohmann::json& storageController =
150 root.emplace_back(nlohmann::json::object());
151
152 storageController["@odata.type"] =
153 "#Storage.v1_7_0.StorageController";
154 storageController["@odata.id"] =
155 "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
156 std::to_string(index);
157 storageController["Name"] = id;
158 storageController["MemberId"] = id;
159 storageController["Status"]["State"] = "Enabled";
160
161 sdbusplus::asio::getProperty<bool>(
162 *crow::connections::systemBus, connectionName, path,
163 "xyz.openbmc_project.Inventory.Item", "Present",
164 [asyncResp, index](const boost::system::error_code ec2,
165 bool enabled) {
166 // this interface isn't necessary, only check it
167 // if we get a good return
168 if (ec2)
169 {
170 return;
171 }
172 if (!enabled)
173 {
174 asyncResp->res.jsonValue["StorageControllers"][index]
175 ["Status"]["State"] = "Disabled";
176 }
177 });
178
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200179 sdbusplus::asio::getAllProperties(
180 *crow::connections::systemBus, connectionName, path,
181 "xyz.openbmc_project.Inventory.Decorator.Asset",
Willy Tua85afbe2021-12-28 14:43:47 -0800182 [asyncResp, index](
183 const boost::system::error_code ec2,
184 const std::vector<
185 std::pair<std::string, dbus::utility::DbusVariantType>>&
186 propertiesList) {
187 if (ec2)
188 {
189 // this interface isn't necessary
190 return;
191 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200192
193 const std::string* partNumber = nullptr;
194 const std::string* serialNumber = nullptr;
195 const std::string* manufacturer = nullptr;
196 const std::string* model = nullptr;
197
198 const bool success = sdbusplus::unpackPropertiesNoThrow(
199 dbus_utils::UnpackErrorPrinter(), propertiesList,
200 "PartNumber", partNumber, "SerialNumber", serialNumber,
201 "Manufacturer", manufacturer, "Model", model);
202
203 if (!success)
Willy Tua85afbe2021-12-28 14:43:47 -0800204 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200205 messages::internalError(asyncResp->res);
206 return;
Willy Tua85afbe2021-12-28 14:43:47 -0800207 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200208
209 nlohmann::json& controller =
210 asyncResp->res.jsonValue["StorageControllers"][index];
211
212 if (partNumber != nullptr)
213 {
214 controller["PartNumber"] = *partNumber;
215 }
216
217 if (serialNumber != nullptr)
218 {
219 controller["SerialNumber"] = *serialNumber;
220 }
221
222 if (manufacturer != nullptr)
223 {
224 controller["Manufacturer"] = *manufacturer;
225 }
226
227 if (model != nullptr)
228 {
229 controller["Model"] = *model;
230 }
231 });
Willy Tua85afbe2021-12-28 14:43:47 -0800232 }
233
234 // this is done after we know the json array will no longer
235 // be resized, as json::array uses vector underneath and we
236 // need references to its members that won't change
237 size_t count = 0;
238 // Pointer based on |asyncResp->res.jsonValue|
239 nlohmann::json::json_pointer rootPtr =
240 "/StorageControllers"_json_pointer;
241 for (const auto& [path, interfaceDict] : subtree)
242 {
243 auto subHealth = std::make_shared<HealthPopulate>(
244 asyncResp, rootPtr / count / "Status");
245 subHealth->inventory.emplace_back(path);
246 health->inventory.emplace_back(path);
247 health->children.emplace_back(subHealth);
248 count++;
249 }
250 },
251 "xyz.openbmc_project.ObjectMapper",
252 "/xyz/openbmc_project/object_mapper",
253 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
254 "/xyz/openbmc_project/inventory", int32_t(0),
255 std::array<const char*, 1>{
256 "xyz.openbmc_project.Inventory.Item.StorageController"});
257}
258
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700259inline void requestRoutesStorage(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700260{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700261 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
Ed Tanoused398212021-06-09 17:05:54 -0700262 .privileges(redfish::privileges::getStorage)
Ed Tanous002d39b2022-05-31 08:59:27 -0700263 .methods(boost::beast::http::verb::get)(
264 [&app](const crow::Request& req,
265 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000266 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700267 {
268 return;
269 }
270 asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
271 asyncResp->res.jsonValue["@odata.id"] =
272 "/redfish/v1/Systems/system/Storage/1";
273 asyncResp->res.jsonValue["Name"] = "Storage";
274 asyncResp->res.jsonValue["Id"] = "1";
275 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
276
277 auto health = std::make_shared<HealthPopulate>(asyncResp);
278 health->populate();
279
Willy Tua85afbe2021-12-28 14:43:47 -0800280 getDrives(asyncResp, health);
281 getStorageControllers(asyncResp, health);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700282 });
283}
284
Willy Tu03913172021-11-08 02:03:19 -0800285inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
286 const std::string& connectionName,
287 const std::string& path)
288{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200289 sdbusplus::asio::getAllProperties(
290 *crow::connections::systemBus, connectionName, path,
291 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous168e20c2021-12-13 14:39:53 -0800292 [asyncResp](const boost::system::error_code ec,
293 const std::vector<
294 std::pair<std::string, dbus::utility::DbusVariantType>>&
295 propertiesList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700296 if (ec)
297 {
298 // this interface isn't necessary
299 return;
300 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200301
302 const std::string* partNumber = nullptr;
303 const std::string* serialNumber = nullptr;
304 const std::string* manufacturer = nullptr;
305 const std::string* model = nullptr;
306
307 const bool success = sdbusplus::unpackPropertiesNoThrow(
308 dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
309 partNumber, "SerialNumber", serialNumber, "Manufacturer",
310 manufacturer, "Model", model);
311
312 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -0700313 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200314 messages::internalError(asyncResp->res);
315 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700316 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200317
318 if (partNumber != nullptr)
319 {
320 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
321 }
322
323 if (serialNumber != nullptr)
324 {
325 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
326 }
327
328 if (manufacturer != nullptr)
329 {
330 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
331 }
332
333 if (model != nullptr)
334 {
335 asyncResp->res.jsonValue["Model"] = *model;
336 }
337 });
Willy Tu03913172021-11-08 02:03:19 -0800338}
339
340inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
341 const std::string& connectionName,
342 const std::string& path)
343{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700344 sdbusplus::asio::getProperty<bool>(
345 *crow::connections::systemBus, connectionName, path,
346 "xyz.openbmc_project.Inventory.Item", "Present",
Willy Tu03913172021-11-08 02:03:19 -0800347 [asyncResp, path](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700348 const bool enabled) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700349 // this interface isn't necessary, only check it if
350 // we get a good return
351 if (ec)
352 {
353 return;
354 }
Willy Tu03913172021-11-08 02:03:19 -0800355
Ed Tanous002d39b2022-05-31 08:59:27 -0700356 if (!enabled)
357 {
358 asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
359 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700360 });
Willy Tu03913172021-11-08 02:03:19 -0800361}
362
363inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
364 const std::string& connectionName,
365 const std::string& path)
366{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700367 sdbusplus::asio::getProperty<bool>(
368 *crow::connections::systemBus, connectionName, path,
369 "xyz.openbmc_project.State.Drive", "Rebuilding",
370 [asyncResp](const boost::system::error_code ec, const bool updating) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700371 // this interface isn't necessary, only check it
372 // if we get a good return
373 if (ec)
374 {
375 return;
376 }
Willy Tu03913172021-11-08 02:03:19 -0800377
Ed Tanous002d39b2022-05-31 08:59:27 -0700378 // updating and disabled in the backend shouldn't be
379 // able to be set at the same time, so we don't need
380 // to check for the race condition of these two
381 // calls
382 if (updating)
383 {
384 asyncResp->res.jsonValue["Status"]["State"] = "Updating";
385 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700386 });
Willy Tu03913172021-11-08 02:03:19 -0800387}
388
Willy Tu19b8e9a2021-11-08 02:55:03 -0800389inline std::optional<std::string> convertDriveType(const std::string& type)
390{
391 if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
392 {
393 return "HDD";
394 }
395 if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
396 {
397 return "SSD";
398 }
399
400 return std::nullopt;
401}
402
403inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
404{
405 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
406 {
407 return "SAS";
408 }
409 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
410 {
411 return "SATA";
412 }
413 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
414 {
415 return "NVMe";
416 }
417 if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
418 {
419 return "FC";
420 }
421
422 return std::nullopt;
423}
424
425inline void
426 getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
427 const std::string& connectionName,
428 const std::string& path)
429{
430 sdbusplus::asio::getAllProperties(
431 *crow::connections::systemBus, connectionName, path,
432 "xyz.openbmc_project.Inventory.Item.Drive",
433 [asyncResp](const boost::system::error_code ec,
434 const std::vector<
435 std::pair<std::string, dbus::utility::DbusVariantType>>&
436 propertiesList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700437 if (ec)
438 {
439 // this interface isn't required
440 return;
441 }
442 for (const std::pair<std::string, dbus::utility::DbusVariantType>&
443 property : propertiesList)
444 {
445 const std::string& propertyName = property.first;
446 if (propertyName == "Type")
Willy Tu19b8e9a2021-11-08 02:55:03 -0800447 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700448 const std::string* value =
449 std::get_if<std::string>(&property.second);
450 if (value == nullptr)
451 {
452 // illegal property
453 BMCWEB_LOG_ERROR << "Illegal property: Type";
454 messages::internalError(asyncResp->res);
455 return;
456 }
457
458 std::optional<std::string> mediaType = convertDriveType(*value);
459 if (!mediaType)
460 {
461 BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
462 << *value;
463 messages::internalError(asyncResp->res);
464 return;
465 }
466
467 asyncResp->res.jsonValue["MediaType"] = *mediaType;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800468 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700469 else if (propertyName == "Capacity")
Willy Tu19b8e9a2021-11-08 02:55:03 -0800470 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700471 const uint64_t* capacity =
472 std::get_if<uint64_t>(&property.second);
473 if (capacity == nullptr)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800474 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700475 BMCWEB_LOG_ERROR << "Illegal property: Capacity";
476 messages::internalError(asyncResp->res);
477 return;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800478 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700479 if (*capacity == 0)
Willy Tu19b8e9a2021-11-08 02:55:03 -0800480 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700481 // drive capacity not known
482 continue;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800483 }
Willy Tu19b8e9a2021-11-08 02:55:03 -0800484
Ed Tanous002d39b2022-05-31 08:59:27 -0700485 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
Willy Tu19b8e9a2021-11-08 02:55:03 -0800486 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700487 else if (propertyName == "Protocol")
488 {
489 const std::string* value =
490 std::get_if<std::string>(&property.second);
491 if (value == nullptr)
492 {
493 BMCWEB_LOG_ERROR << "Illegal property: Protocol";
494 messages::internalError(asyncResp->res);
495 return;
496 }
497
498 std::optional<std::string> proto = convertDriveProtocol(*value);
499 if (!proto)
500 {
501 BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: "
502 << *value;
503 messages::internalError(asyncResp->res);
504 return;
505 }
506 asyncResp->res.jsonValue["Protocol"] = *proto;
507 }
John Edward Broadbent3fe4d5c2022-05-06 14:42:35 -0700508 else if (propertyName == "PredictedMediaLifeLeftPercent")
509 {
510 const uint8_t* lifeLeft =
511 std::get_if<uint8_t>(&property.second);
512 if (lifeLeft == nullptr)
513 {
514 BMCWEB_LOG_ERROR
515 << "Illegal property: PredictedMediaLifeLeftPercent";
516 messages::internalError(asyncResp->res);
517 return;
518 }
519 // 255 means reading the value is not supported
520 if (*lifeLeft != 255)
521 {
522 asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
523 *lifeLeft;
524 }
525 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700526 }
Willy Tu19b8e9a2021-11-08 02:55:03 -0800527 });
528}
529
Nan Zhoub53dcd92022-06-21 17:47:50 +0000530static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
531 const std::string& connectionName,
532 const std::string& path,
533 const std::vector<std::string>& interfaces)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700534{
535 for (const std::string& interface : interfaces)
536 {
537 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
538 {
539 getDriveAsset(asyncResp, connectionName, path);
540 }
541 else if (interface == "xyz.openbmc_project.Inventory.Item")
542 {
543 getDrivePresent(asyncResp, connectionName, path);
544 }
545 else if (interface == "xyz.openbmc_project.State.Drive")
546 {
547 getDriveState(asyncResp, connectionName, path);
548 }
549 else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
550 {
551 getDriveItemProperties(asyncResp, connectionName, path);
552 }
553 }
554}
555
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700556inline void requestRoutesDrive(App& app)
557{
Ed Tanous22d268c2022-05-19 09:39:07 -0700558 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700559 .privileges(redfish::privileges::getDrive)
Ed Tanous002d39b2022-05-31 08:59:27 -0700560 .methods(boost::beast::http::verb::get)(
561 [&app](const crow::Request& req,
562 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -0700563 const std::string& systemName, const std::string& driveId) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000564 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700565 {
566 return;
567 }
Ed Tanous22d268c2022-05-19 09:39:07 -0700568 if (systemName != "system")
569 {
570 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
571 systemName);
572 return;
573 }
574
Ed Tanous002d39b2022-05-31 08:59:27 -0700575 crow::connections::systemBus->async_method_call(
576 [asyncResp,
577 driveId](const boost::system::error_code ec,
578 const dbus::utility::MapperGetSubTreeResponse& subtree) {
579 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700580 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700581 BMCWEB_LOG_ERROR << "Drive mapper call error";
582 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -0700583 return;
584 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700585
Ed Tanous002d39b2022-05-31 08:59:27 -0700586 auto drive = std::find_if(
587 subtree.begin(), subtree.end(),
588 [&driveId](
Nan Zhou8cb65f82022-06-15 05:12:24 +0000589 const std::pair<std::string,
590 dbus::utility::MapperServiceMap>& object) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700591 return sdbusplus::message::object_path(object.first)
592 .filename() == driveId;
593 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700594
Ed Tanous002d39b2022-05-31 08:59:27 -0700595 if (drive == subtree.end())
596 {
597 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
598 return;
599 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700600
Ed Tanous002d39b2022-05-31 08:59:27 -0700601 const std::string& path = drive->first;
Nan Zhou8cb65f82022-06-15 05:12:24 +0000602 const dbus::utility::MapperServiceMap& connectionNames =
603 drive->second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700604
Ed Tanous002d39b2022-05-31 08:59:27 -0700605 asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
606 asyncResp->res.jsonValue["@odata.id"] =
607 "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId;
608 asyncResp->res.jsonValue["Name"] = driveId;
609 asyncResp->res.jsonValue["Id"] = driveId;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700610
Ed Tanous002d39b2022-05-31 08:59:27 -0700611 if (connectionNames.size() != 1)
612 {
613 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
614 << ", not equal to 1";
615 messages::internalError(asyncResp->res);
616 return;
617 }
James Feiste284a7c2019-11-20 16:20:23 -0800618
Ed Tanous002d39b2022-05-31 08:59:27 -0700619 getMainChassisId(
620 asyncResp, [](const std::string& chassisId,
621 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
622 aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
623 "/redfish/v1/Chassis/" + chassisId;
624 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700625
Ed Tanous002d39b2022-05-31 08:59:27 -0700626 // default it to Enabled
627 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700628
Ed Tanous002d39b2022-05-31 08:59:27 -0700629 auto health = std::make_shared<HealthPopulate>(asyncResp);
630 health->inventory.emplace_back(path);
631 health->populate();
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700632
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700633 addAllDriveInfo(asyncResp, connectionNames[0].first, path,
634 connectionNames[0].second);
Ed Tanous002d39b2022-05-31 08:59:27 -0700635 },
636 "xyz.openbmc_project.ObjectMapper",
637 "/xyz/openbmc_project/object_mapper",
638 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
639 "/xyz/openbmc_project/inventory", int32_t(0),
640 std::array<const char*, 1>{
641 "xyz.openbmc_project.Inventory.Item.Drive"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700642 });
643}
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700644
645/**
646 * Chassis drives, this URL will show all the DriveCollection
647 * information
648 */
Nan Zhoub53dcd92022-06-21 17:47:50 +0000649inline void chassisDriveCollectionGet(
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700650 crow::App& app, const crow::Request& req,
651 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
652 const std::string& chassisId)
653{
Carson Labrado3ba00072022-06-06 19:40:56 +0000654 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700655 {
656 return;
657 }
658
659 // mapper call lambda
660 crow::connections::systemBus->async_method_call(
661 [asyncResp,
662 chassisId](const boost::system::error_code ec,
663 const dbus::utility::MapperGetSubTreeResponse& subtree) {
664 if (ec)
665 {
666 if (ec == boost::system::errc::host_unreachable)
667 {
668 messages::resourceNotFound(asyncResp->res, "Chassis",
669 chassisId);
670 return;
671 }
672 messages::internalError(asyncResp->res);
673 return;
674 }
675
676 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000677 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700678 {
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700679 sdbusplus::message::object_path objPath(path);
680 if (objPath.filename() != chassisId)
681 {
682 continue;
683 }
684
685 if (connectionNames.empty())
686 {
687 BMCWEB_LOG_ERROR << "Got 0 Connection names";
688 continue;
689 }
690
691 asyncResp->res.jsonValue["@odata.type"] =
692 "#DriveCollection.DriveCollection";
693 asyncResp->res.jsonValue["@odata.id"] =
John Edward Broadbent14bd7d92022-06-14 17:17:43 -0700694 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
695 chassisId, "Drives");
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700696 asyncResp->res.jsonValue["Name"] = "Drive Collection";
697
698 // Association lambda
699 sdbusplus::asio::getProperty<std::vector<std::string>>(
700 *crow::connections::systemBus,
701 "xyz.openbmc_project.ObjectMapper", path + "/drive",
702 "xyz.openbmc_project.Association", "endpoints",
703 [asyncResp, chassisId](const boost::system::error_code ec3,
704 const std::vector<std::string>& resp) {
705 if (ec3)
706 {
707 BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
708 }
709 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
710 // important if array is empty
711 members = nlohmann::json::array();
712
713 std::vector<std::string> leafNames;
714 for (const auto& drive : resp)
715 {
Ed Tanous8a592812022-06-04 09:06:59 -0700716 sdbusplus::message::object_path drivePath(drive);
717 leafNames.push_back(drivePath.filename());
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700718 }
719
720 std::sort(leafNames.begin(), leafNames.end(),
721 AlphanumLess<std::string>());
722
723 for (const auto& leafName : leafNames)
724 {
725 nlohmann::json::object_t member;
726 member["@odata.id"] = crow::utility::urlFromPieces(
727 "redfish", "v1", "Chassis", chassisId, "Drives",
728 leafName);
729 members.push_back(std::move(member));
730 // navigation links will be registered in next patch set
731 }
732 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
733 }); // end association lambda
734
735 } // end Iterate over all retrieved ObjectPaths
736 },
737 "xyz.openbmc_project.ObjectMapper",
738 "/xyz/openbmc_project/object_mapper",
739 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
740 "/xyz/openbmc_project/inventory", 0,
741 std::array<const char*, 2>{
742 "xyz.openbmc_project.Inventory.Item.Board",
743 "xyz.openbmc_project.Inventory.Item.Chassis"});
744}
745
746inline void requestRoutesChassisDrive(App& app)
747{
748 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
749 .privileges(redfish::privileges::getDriveCollection)
750 .methods(boost::beast::http::verb::get)(
751 std::bind_front(chassisDriveCollectionGet, std::ref(app)));
752}
753
Nan Zhoub53dcd92022-06-21 17:47:50 +0000754inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
755 const std::string& chassisId,
756 const std::string& driveName,
757 const boost::system::error_code ec,
758 const dbus::utility::MapperGetSubTreeResponse& subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700759{
760
761 if (ec)
762 {
763 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
764 messages::internalError(asyncResp->res);
765 return;
766 }
767
768 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000769 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700770 {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700771 sdbusplus::message::object_path objPath(path);
772 if (objPath.filename() != driveName)
773 {
774 continue;
775 }
776
777 if (connectionNames.empty())
778 {
779 BMCWEB_LOG_ERROR << "Got 0 Connection names";
780 continue;
781 }
782
783 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
John Edward Broadbenta0cb40c2022-06-29 17:27:38 -0700784 "redfish", "v1", "Chassis", chassisId, "Drives", driveName);
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700785
786 asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
John Edward Broadbenta0cb40c2022-06-29 17:27:38 -0700787 asyncResp->res.jsonValue["Name"] = driveName;
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700788 asyncResp->res.jsonValue["Id"] = driveName;
789 // default it to Enabled
790 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
791
792 nlohmann::json::object_t linkChassisNav;
793 linkChassisNav["@odata.id"] =
794 crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId);
795 asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
796
797 addAllDriveInfo(asyncResp, connectionNames[0].first, path,
798 connectionNames[0].second);
799 }
800}
801
Nan Zhoub53dcd92022-06-21 17:47:50 +0000802inline void
803 matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
804 const std::string& chassisId,
805 const std::string& driveName,
806 const std::vector<std::string>& resp)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700807{
808
809 for (const std::string& drivePath : resp)
810 {
811 sdbusplus::message::object_path path(drivePath);
812 std::string leaf = path.filename();
813 if (leaf != driveName)
814 {
815 continue;
816 }
817 // mapper call drive
818 const std::array<const char*, 1> driveInterface = {
819 "xyz.openbmc_project.Inventory.Item.Drive"};
820
821 crow::connections::systemBus->async_method_call(
822 [asyncResp, chassisId, driveName](
823 const boost::system::error_code ec,
824 const dbus::utility::MapperGetSubTreeResponse& subtree) {
825 buildDrive(asyncResp, chassisId, driveName, ec, subtree);
826 },
827 "xyz.openbmc_project.ObjectMapper",
828 "/xyz/openbmc_project/object_mapper",
829 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
830 "/xyz/openbmc_project/inventory", 0, driveInterface);
831 }
832}
833
Nan Zhoub53dcd92022-06-21 17:47:50 +0000834inline void
835 handleChassisDriveGet(crow::App& app, const crow::Request& req,
836 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
837 const std::string& chassisId,
838 const std::string& driveName)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700839{
Michal Orzel03810a12022-06-15 14:04:28 +0200840 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700841 {
842 return;
843 }
844 const std::array<const char*, 2> interfaces = {
845 "xyz.openbmc_project.Inventory.Item.Board",
846 "xyz.openbmc_project.Inventory.Item.Chassis"};
847
848 // mapper call chassis
849 crow::connections::systemBus->async_method_call(
850 [asyncResp, chassisId,
851 driveName](const boost::system::error_code ec,
852 const dbus::utility::MapperGetSubTreeResponse& subtree) {
853 if (ec)
854 {
855 messages::internalError(asyncResp->res);
856 return;
857 }
858
859 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000860 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700861 {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700862 sdbusplus::message::object_path objPath(path);
863 if (objPath.filename() != chassisId)
864 {
865 continue;
866 }
867
868 if (connectionNames.empty())
869 {
870 BMCWEB_LOG_ERROR << "Got 0 Connection names";
871 continue;
872 }
873
874 sdbusplus::asio::getProperty<std::vector<std::string>>(
875 *crow::connections::systemBus,
876 "xyz.openbmc_project.ObjectMapper", path + "/drive",
877 "xyz.openbmc_project.Association", "endpoints",
878 [asyncResp, chassisId,
879 driveName](const boost::system::error_code ec3,
880 const std::vector<std::string>& resp) {
881 if (ec3)
882 {
883 return; // no drives = no failures
884 }
885 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
886 });
887 break;
888 }
889 },
890 "xyz.openbmc_project.ObjectMapper",
891 "/xyz/openbmc_project/object_mapper",
892 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
893 "/xyz/openbmc_project/inventory", 0, interfaces);
894}
895
896/**
897 * This URL will show the drive interface for the specific drive in the chassis
898 */
899inline void requestRoutesChassisDriveName(App& app)
900{
901 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
902 .privileges(redfish::privileges::getChassis)
903 .methods(boost::beast::http::verb::get)(
904 std::bind_front(handleChassisDriveGet, std::ref(app)));
905}
906
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700907} // namespace redfish