blob: 93911e5996ddd61917492f462bb8e1eb0e6a10f1 [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"
Ed Tanousa8e884f2023-01-13 17:40:03 -080021#include "human_sort.hpp"
James Feiste284a7c2019-11-20 16:20:23 -080022#include "openbmc_dbus_rest.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080023#include "query.hpp"
Ed Tanousa8e884f2023-01-13 17:40:03 -080024#include "redfish_util.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080025#include "registries/privilege_registry.hpp"
26#include "utils/dbus_utils.hpp"
James Feist2ad9c2f2019-10-29 16:26:48 -070027
George Liue99073f2022-12-09 11:06:16 +080028#include <boost/system/error_code.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070029#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020030#include <sdbusplus/unpack_properties.hpp>
Nikhil Potadea25aecc2019-08-23 16:35:26 -070031
George Liu7a1dbc42022-12-07 16:03:22 +080032#include <array>
33#include <string_view>
34
Nikhil Potadea25aecc2019-08-23 16:35:26 -070035namespace redfish
36{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070037inline void requestRoutesStorageCollection(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -070038{
Ed Tanous22d268c2022-05-19 09:39:07 -070039 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/")
Ed Tanoused398212021-06-09 17:05:54 -070040 .privileges(redfish::privileges::getStorageCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070041 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -070042 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -070043 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
44 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +000045 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -070046 {
47 return;
48 }
Ed Tanous22d268c2022-05-19 09:39:07 -070049 if (systemName != "system")
50 {
51 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
52 systemName);
53 return;
54 }
55
Ed Tanous002d39b2022-05-31 08:59:27 -070056 asyncResp->res.jsonValue["@odata.type"] =
57 "#StorageCollection.StorageCollection";
58 asyncResp->res.jsonValue["@odata.id"] =
59 "/redfish/v1/Systems/system/Storage";
60 asyncResp->res.jsonValue["Name"] = "Storage Collection";
61 nlohmann::json::array_t members;
62 nlohmann::json::object_t member;
63 member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
64 members.emplace_back(member);
65 asyncResp->res.jsonValue["Members"] = std::move(members);
66 asyncResp->res.jsonValue["Members@odata.count"] = 1;
67 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -070068}
Nikhil Potadea25aecc2019-08-23 16:35:26 -070069
Willy Tua85afbe2021-12-28 14:43:47 -080070inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
71 const std::shared_ptr<HealthPopulate>& health)
72{
George Liu7a1dbc42022-12-07 16:03:22 +080073 const std::array<std::string_view, 1> interfaces = {
74 "xyz.openbmc_project.Inventory.Item.Drive"};
75 dbus::utility::getSubTreePaths(
76 "/xyz/openbmc_project/inventory", 0, interfaces,
Willy Tua85afbe2021-12-28 14:43:47 -080077 [asyncResp, health](
George Liu7a1dbc42022-12-07 16:03:22 +080078 const boost::system::error_code& ec,
Willy Tua85afbe2021-12-28 14:43:47 -080079 const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
80 if (ec)
81 {
82 BMCWEB_LOG_ERROR << "Drive mapper call error";
83 messages::internalError(asyncResp->res);
84 return;
85 }
86
87 nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
88 driveArray = nlohmann::json::array();
89 auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
90 count = 0;
91
92 health->inventory.insert(health->inventory.end(), driveList.begin(),
93 driveList.end());
94
95 for (const std::string& drive : driveList)
96 {
97 sdbusplus::message::object_path object(drive);
98 if (object.filename().empty())
99 {
100 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
101 return;
102 }
103
104 nlohmann::json::object_t driveJson;
105 driveJson["@odata.id"] =
106 "/redfish/v1/Systems/system/Storage/1/Drives/" +
107 object.filename();
108 driveArray.push_back(std::move(driveJson));
109 }
110
111 count = driveArray.size();
George Liu7a1dbc42022-12-07 16:03:22 +0800112 });
Willy Tua85afbe2021-12-28 14:43:47 -0800113}
114
115inline void
116 getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
117 const std::shared_ptr<HealthPopulate>& health)
118{
George Liue99073f2022-12-09 11:06:16 +0800119 constexpr std::array<std::string_view, 1> interfaces = {
120 "xyz.openbmc_project.Inventory.Item.StorageController"};
121 dbus::utility::getSubTree(
122 "/xyz/openbmc_project/inventory", 0, interfaces,
Willy Tua85afbe2021-12-28 14:43:47 -0800123 [asyncResp,
George Liue99073f2022-12-09 11:06:16 +0800124 health](const boost::system::error_code& ec,
Willy Tua85afbe2021-12-28 14:43:47 -0800125 const dbus::utility::MapperGetSubTreeResponse& subtree) {
126 if (ec || subtree.empty())
127 {
128 // doesn't have to be there
129 return;
130 }
131
132 nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
133 root = nlohmann::json::array();
134 for (const auto& [path, interfaceDict] : subtree)
135 {
136 sdbusplus::message::object_path object(path);
137 std::string id = object.filename();
138 if (id.empty())
139 {
140 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
141 return;
142 }
143
144 if (interfaceDict.size() != 1)
145 {
146 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
147 << ", greater than 1";
148 messages::internalError(asyncResp->res);
149 return;
150 }
151
152 const std::string& connectionName = interfaceDict.front().first;
153
154 size_t index = root.size();
155 nlohmann::json& storageController =
156 root.emplace_back(nlohmann::json::object());
157
158 storageController["@odata.type"] =
159 "#Storage.v1_7_0.StorageController";
160 storageController["@odata.id"] =
161 "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
162 std::to_string(index);
163 storageController["Name"] = id;
164 storageController["MemberId"] = id;
165 storageController["Status"]["State"] = "Enabled";
166
167 sdbusplus::asio::getProperty<bool>(
168 *crow::connections::systemBus, connectionName, path,
169 "xyz.openbmc_project.Inventory.Item", "Present",
170 [asyncResp, index](const boost::system::error_code ec2,
Willy Tucef57e82022-12-15 16:42:02 -0800171 bool isPresent) {
Willy Tua85afbe2021-12-28 14:43:47 -0800172 // this interface isn't necessary, only check it
173 // if we get a good return
174 if (ec2)
175 {
176 return;
177 }
Willy Tucef57e82022-12-15 16:42:02 -0800178 if (!isPresent)
Willy Tua85afbe2021-12-28 14:43:47 -0800179 {
180 asyncResp->res.jsonValue["StorageControllers"][index]
Willy Tucef57e82022-12-15 16:42:02 -0800181 ["Status"]["State"] = "Absent";
Willy Tua85afbe2021-12-28 14:43:47 -0800182 }
183 });
184
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200185 sdbusplus::asio::getAllProperties(
186 *crow::connections::systemBus, connectionName, path,
187 "xyz.openbmc_project.Inventory.Decorator.Asset",
Willy Tua85afbe2021-12-28 14:43:47 -0800188 [asyncResp, index](
189 const boost::system::error_code ec2,
190 const std::vector<
191 std::pair<std::string, dbus::utility::DbusVariantType>>&
192 propertiesList) {
193 if (ec2)
194 {
195 // this interface isn't necessary
196 return;
197 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200198
199 const std::string* partNumber = nullptr;
200 const std::string* serialNumber = nullptr;
201 const std::string* manufacturer = nullptr;
202 const std::string* model = nullptr;
203
204 const bool success = sdbusplus::unpackPropertiesNoThrow(
205 dbus_utils::UnpackErrorPrinter(), propertiesList,
206 "PartNumber", partNumber, "SerialNumber", serialNumber,
207 "Manufacturer", manufacturer, "Model", model);
208
209 if (!success)
Willy Tua85afbe2021-12-28 14:43:47 -0800210 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200211 messages::internalError(asyncResp->res);
212 return;
Willy Tua85afbe2021-12-28 14:43:47 -0800213 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200214
215 nlohmann::json& controller =
216 asyncResp->res.jsonValue["StorageControllers"][index];
217
218 if (partNumber != nullptr)
219 {
220 controller["PartNumber"] = *partNumber;
221 }
222
223 if (serialNumber != nullptr)
224 {
225 controller["SerialNumber"] = *serialNumber;
226 }
227
228 if (manufacturer != nullptr)
229 {
230 controller["Manufacturer"] = *manufacturer;
231 }
232
233 if (model != nullptr)
234 {
235 controller["Model"] = *model;
236 }
237 });
Willy Tua85afbe2021-12-28 14:43:47 -0800238 }
239
240 // this is done after we know the json array will no longer
241 // be resized, as json::array uses vector underneath and we
242 // need references to its members that won't change
243 size_t count = 0;
244 // Pointer based on |asyncResp->res.jsonValue|
245 nlohmann::json::json_pointer rootPtr =
246 "/StorageControllers"_json_pointer;
247 for (const auto& [path, interfaceDict] : subtree)
248 {
249 auto subHealth = std::make_shared<HealthPopulate>(
250 asyncResp, rootPtr / count / "Status");
251 subHealth->inventory.emplace_back(path);
252 health->inventory.emplace_back(path);
253 health->children.emplace_back(subHealth);
254 count++;
255 }
George Liue99073f2022-12-09 11:06:16 +0800256 });
Willy Tua85afbe2021-12-28 14:43:47 -0800257}
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,
Willy Tucef57e82022-12-15 16:42:02 -0800348 const bool isPresent) {
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
Willy Tucef57e82022-12-15 16:42:02 -0800356 if (!isPresent)
Ed Tanous002d39b2022-05-31 08:59:27 -0700357 {
Willy Tucef57e82022-12-15 16:42:02 -0800358 asyncResp->res.jsonValue["Status"]["State"] = "Absent";
Ed Tanous002d39b2022-05-31 08:59:27 -0700359 }
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
George Liue99073f2022-12-09 11:06:16 +0800575 constexpr std::array<std::string_view, 1> interfaces = {
576 "xyz.openbmc_project.Inventory.Item.Drive"};
577 dbus::utility::getSubTree(
578 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -0700579 [asyncResp,
George Liue99073f2022-12-09 11:06:16 +0800580 driveId](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700581 const dbus::utility::MapperGetSubTreeResponse& subtree) {
582 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700583 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700584 BMCWEB_LOG_ERROR << "Drive mapper call error";
585 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -0700586 return;
587 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700588
Ed Tanous002d39b2022-05-31 08:59:27 -0700589 auto drive = std::find_if(
590 subtree.begin(), subtree.end(),
591 [&driveId](
Nan Zhou8cb65f82022-06-15 05:12:24 +0000592 const std::pair<std::string,
593 dbus::utility::MapperServiceMap>& object) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700594 return sdbusplus::message::object_path(object.first)
595 .filename() == driveId;
596 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700597
Ed Tanous002d39b2022-05-31 08:59:27 -0700598 if (drive == subtree.end())
599 {
600 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
601 return;
602 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700603
Ed Tanous002d39b2022-05-31 08:59:27 -0700604 const std::string& path = drive->first;
Nan Zhou8cb65f82022-06-15 05:12:24 +0000605 const dbus::utility::MapperServiceMap& connectionNames =
606 drive->second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700607
Ed Tanous002d39b2022-05-31 08:59:27 -0700608 asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
609 asyncResp->res.jsonValue["@odata.id"] =
610 "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId;
611 asyncResp->res.jsonValue["Name"] = driveId;
612 asyncResp->res.jsonValue["Id"] = driveId;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700613
Ed Tanous002d39b2022-05-31 08:59:27 -0700614 if (connectionNames.size() != 1)
615 {
616 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
617 << ", not equal to 1";
618 messages::internalError(asyncResp->res);
619 return;
620 }
James Feiste284a7c2019-11-20 16:20:23 -0800621
Ed Tanous002d39b2022-05-31 08:59:27 -0700622 getMainChassisId(
623 asyncResp, [](const std::string& chassisId,
624 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
625 aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
626 "/redfish/v1/Chassis/" + chassisId;
627 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700628
Ed Tanous002d39b2022-05-31 08:59:27 -0700629 // default it to Enabled
630 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700631
Ed Tanous002d39b2022-05-31 08:59:27 -0700632 auto health = std::make_shared<HealthPopulate>(asyncResp);
633 health->inventory.emplace_back(path);
634 health->populate();
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700635
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700636 addAllDriveInfo(asyncResp, connectionNames[0].first, path,
637 connectionNames[0].second);
George Liue99073f2022-12-09 11:06:16 +0800638 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700639 });
640}
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700641
642/**
643 * Chassis drives, this URL will show all the DriveCollection
644 * information
645 */
Nan Zhoub53dcd92022-06-21 17:47:50 +0000646inline void chassisDriveCollectionGet(
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700647 crow::App& app, const crow::Request& req,
648 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
649 const std::string& chassisId)
650{
Carson Labrado3ba00072022-06-06 19:40:56 +0000651 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700652 {
653 return;
654 }
655
656 // mapper call lambda
George Liue99073f2022-12-09 11:06:16 +0800657 constexpr std::array<std::string_view, 2> interfaces = {
658 "xyz.openbmc_project.Inventory.Item.Board",
659 "xyz.openbmc_project.Inventory.Item.Chassis"};
660 dbus::utility::getSubTree(
661 "/xyz/openbmc_project/inventory", 0, interfaces,
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700662 [asyncResp,
George Liue99073f2022-12-09 11:06:16 +0800663 chassisId](const boost::system::error_code& ec,
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700664 const dbus::utility::MapperGetSubTreeResponse& subtree) {
665 if (ec)
666 {
667 if (ec == boost::system::errc::host_unreachable)
668 {
669 messages::resourceNotFound(asyncResp->res, "Chassis",
670 chassisId);
671 return;
672 }
673 messages::internalError(asyncResp->res);
674 return;
675 }
676
677 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000678 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700679 {
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700680 sdbusplus::message::object_path objPath(path);
681 if (objPath.filename() != chassisId)
682 {
683 continue;
684 }
685
686 if (connectionNames.empty())
687 {
688 BMCWEB_LOG_ERROR << "Got 0 Connection names";
689 continue;
690 }
691
692 asyncResp->res.jsonValue["@odata.type"] =
693 "#DriveCollection.DriveCollection";
694 asyncResp->res.jsonValue["@odata.id"] =
John Edward Broadbent14bd7d92022-06-14 17:17:43 -0700695 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
696 chassisId, "Drives");
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700697 asyncResp->res.jsonValue["Name"] = "Drive Collection";
698
699 // Association lambda
700 sdbusplus::asio::getProperty<std::vector<std::string>>(
701 *crow::connections::systemBus,
702 "xyz.openbmc_project.ObjectMapper", path + "/drive",
703 "xyz.openbmc_project.Association", "endpoints",
704 [asyncResp, chassisId](const boost::system::error_code ec3,
705 const std::vector<std::string>& resp) {
706 if (ec3)
707 {
708 BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
709 }
710 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
711 // important if array is empty
712 members = nlohmann::json::array();
713
714 std::vector<std::string> leafNames;
715 for (const auto& drive : resp)
716 {
Ed Tanous8a592812022-06-04 09:06:59 -0700717 sdbusplus::message::object_path drivePath(drive);
718 leafNames.push_back(drivePath.filename());
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700719 }
720
721 std::sort(leafNames.begin(), leafNames.end(),
722 AlphanumLess<std::string>());
723
724 for (const auto& leafName : leafNames)
725 {
726 nlohmann::json::object_t member;
727 member["@odata.id"] = crow::utility::urlFromPieces(
728 "redfish", "v1", "Chassis", chassisId, "Drives",
729 leafName);
730 members.push_back(std::move(member));
731 // navigation links will be registered in next patch set
732 }
733 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
734 }); // end association lambda
735
736 } // end Iterate over all retrieved ObjectPaths
George Liue99073f2022-12-09 11:06:16 +0800737 });
John Edward Broadbent92903bd2022-04-26 13:40:59 -0700738}
739
740inline void requestRoutesChassisDrive(App& app)
741{
742 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
743 .privileges(redfish::privileges::getDriveCollection)
744 .methods(boost::beast::http::verb::get)(
745 std::bind_front(chassisDriveCollectionGet, std::ref(app)));
746}
747
Nan Zhoub53dcd92022-06-21 17:47:50 +0000748inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
749 const std::string& chassisId,
750 const std::string& driveName,
751 const boost::system::error_code ec,
752 const dbus::utility::MapperGetSubTreeResponse& subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700753{
754
755 if (ec)
756 {
757 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
758 messages::internalError(asyncResp->res);
759 return;
760 }
761
762 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000763 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700764 {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700765 sdbusplus::message::object_path objPath(path);
766 if (objPath.filename() != driveName)
767 {
768 continue;
769 }
770
771 if (connectionNames.empty())
772 {
773 BMCWEB_LOG_ERROR << "Got 0 Connection names";
774 continue;
775 }
776
777 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
John Edward Broadbenta0cb40c2022-06-29 17:27:38 -0700778 "redfish", "v1", "Chassis", chassisId, "Drives", driveName);
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700779
780 asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
John Edward Broadbenta0cb40c2022-06-29 17:27:38 -0700781 asyncResp->res.jsonValue["Name"] = driveName;
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700782 asyncResp->res.jsonValue["Id"] = driveName;
783 // default it to Enabled
784 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
785
786 nlohmann::json::object_t linkChassisNav;
787 linkChassisNav["@odata.id"] =
788 crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId);
789 asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
790
791 addAllDriveInfo(asyncResp, connectionNames[0].first, path,
792 connectionNames[0].second);
793 }
794}
795
Nan Zhoub53dcd92022-06-21 17:47:50 +0000796inline void
797 matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
798 const std::string& chassisId,
799 const std::string& driveName,
800 const std::vector<std::string>& resp)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700801{
802
803 for (const std::string& drivePath : resp)
804 {
805 sdbusplus::message::object_path path(drivePath);
806 std::string leaf = path.filename();
807 if (leaf != driveName)
808 {
809 continue;
810 }
811 // mapper call drive
George Liue99073f2022-12-09 11:06:16 +0800812 constexpr std::array<std::string_view, 1> driveInterface = {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700813 "xyz.openbmc_project.Inventory.Item.Drive"};
George Liue99073f2022-12-09 11:06:16 +0800814 dbus::utility::getSubTree(
815 "/xyz/openbmc_project/inventory", 0, driveInterface,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700816 [asyncResp, chassisId, driveName](
George Liue99073f2022-12-09 11:06:16 +0800817 const boost::system::error_code& ec,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700818 const dbus::utility::MapperGetSubTreeResponse& subtree) {
819 buildDrive(asyncResp, chassisId, driveName, ec, subtree);
George Liue99073f2022-12-09 11:06:16 +0800820 });
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700821 }
822}
823
Nan Zhoub53dcd92022-06-21 17:47:50 +0000824inline void
825 handleChassisDriveGet(crow::App& app, const crow::Request& req,
826 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
827 const std::string& chassisId,
828 const std::string& driveName)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700829{
Michal Orzel03810a12022-06-15 14:04:28 +0200830 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700831 {
832 return;
833 }
George Liue99073f2022-12-09 11:06:16 +0800834 constexpr std::array<std::string_view, 2> interfaces = {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700835 "xyz.openbmc_project.Inventory.Item.Board",
836 "xyz.openbmc_project.Inventory.Item.Chassis"};
837
838 // mapper call chassis
George Liue99073f2022-12-09 11:06:16 +0800839 dbus::utility::getSubTree(
840 "/xyz/openbmc_project/inventory", 0, interfaces,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700841 [asyncResp, chassisId,
George Liue99073f2022-12-09 11:06:16 +0800842 driveName](const boost::system::error_code& ec,
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700843 const dbus::utility::MapperGetSubTreeResponse& subtree) {
844 if (ec)
845 {
846 messages::internalError(asyncResp->res);
847 return;
848 }
849
850 // Iterate over all retrieved ObjectPaths.
Nan Zhou8cb65f82022-06-15 05:12:24 +0000851 for (const auto& [path, connectionNames] : subtree)
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700852 {
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700853 sdbusplus::message::object_path objPath(path);
854 if (objPath.filename() != chassisId)
855 {
856 continue;
857 }
858
859 if (connectionNames.empty())
860 {
861 BMCWEB_LOG_ERROR << "Got 0 Connection names";
862 continue;
863 }
864
865 sdbusplus::asio::getProperty<std::vector<std::string>>(
866 *crow::connections::systemBus,
867 "xyz.openbmc_project.ObjectMapper", path + "/drive",
868 "xyz.openbmc_project.Association", "endpoints",
869 [asyncResp, chassisId,
870 driveName](const boost::system::error_code ec3,
871 const std::vector<std::string>& resp) {
872 if (ec3)
873 {
874 return; // no drives = no failures
875 }
876 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
877 });
878 break;
879 }
George Liue99073f2022-12-09 11:06:16 +0800880 });
John Edward Broadbente56ed6b2022-04-26 13:40:59 -0700881}
882
883/**
884 * This URL will show the drive interface for the specific drive in the chassis
885 */
886inline void requestRoutesChassisDriveName(App& app)
887{
888 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
889 .privileges(redfish::privileges::getChassis)
890 .methods(boost::beast::http::verb::get)(
891 std::bind_front(handleChassisDriveGet, std::ref(app)));
892}
893
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700894} // namespace redfish