Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 1 | /* |
| 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 Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 18 | #include "dbus_utility.hpp" |
James Feist | 2ad9c2f | 2019-10-29 16:26:48 -0700 | [diff] [blame] | 19 | #include "health.hpp" |
James Feist | e284a7c | 2019-11-20 16:20:23 -0800 | [diff] [blame] | 20 | #include "openbmc_dbus_rest.hpp" |
James Feist | 2ad9c2f | 2019-10-29 16:26:48 -0700 | [diff] [blame] | 21 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 22 | #include <app.hpp> |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 23 | #include <query.hpp> |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 24 | #include <registries/privilege_registry.hpp> |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 25 | #include <sdbusplus/asio/property.hpp> |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 26 | #include <sdbusplus/unpack_properties.hpp> |
| 27 | #include <utils/dbus_utils.hpp> |
Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 28 | |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 29 | #include <array> |
| 30 | #include <string_view> |
| 31 | |
Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 32 | namespace redfish |
| 33 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 34 | inline void requestRoutesStorageCollection(App& app) |
Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 35 | { |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 36 | BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 37 | .privileges(redfish::privileges::getStorageCollection) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 38 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 39 | [&app](const crow::Request& req, |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 40 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 41 | const std::string& systemName) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 42 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 43 | { |
| 44 | return; |
| 45 | } |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 46 | if (systemName != "system") |
| 47 | { |
| 48 | messages::resourceNotFound(asyncResp->res, "ComputerSystem", |
| 49 | systemName); |
| 50 | return; |
| 51 | } |
| 52 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 53 | 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 Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 65 | } |
Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 66 | |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 67 | inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 68 | const std::shared_ptr<HealthPopulate>& health) |
| 69 | { |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 70 | 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 Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 74 | [asyncResp, health]( |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 75 | const boost::system::error_code& ec, |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 76 | 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 Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 109 | }); |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | inline 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 Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 179 | sdbusplus::asio::getAllProperties( |
| 180 | *crow::connections::systemBus, connectionName, path, |
| 181 | "xyz.openbmc_project.Inventory.Decorator.Asset", |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 182 | [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 Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 192 | |
| 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 Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 204 | { |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 205 | messages::internalError(asyncResp->res); |
| 206 | return; |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 207 | } |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 208 | |
| 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 Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 232 | } |
| 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 Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 259 | inline void requestRoutesStorage(App& app) |
Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 260 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 261 | BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 262 | .privileges(redfish::privileges::getStorage) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 263 | .methods(boost::beast::http::verb::get)( |
| 264 | [&app](const crow::Request& req, |
| 265 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 266 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 267 | { |
| 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 Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 280 | getDrives(asyncResp, health); |
| 281 | getStorageControllers(asyncResp, health); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 282 | }); |
| 283 | } |
| 284 | |
Willy Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 285 | inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 286 | const std::string& connectionName, |
| 287 | const std::string& path) |
| 288 | { |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 289 | sdbusplus::asio::getAllProperties( |
| 290 | *crow::connections::systemBus, connectionName, path, |
| 291 | "xyz.openbmc_project.Inventory.Decorator.Asset", |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 292 | [asyncResp](const boost::system::error_code ec, |
| 293 | const std::vector< |
| 294 | std::pair<std::string, dbus::utility::DbusVariantType>>& |
| 295 | propertiesList) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 296 | if (ec) |
| 297 | { |
| 298 | // this interface isn't necessary |
| 299 | return; |
| 300 | } |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 301 | |
| 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 Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 313 | { |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 314 | messages::internalError(asyncResp->res); |
| 315 | return; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 316 | } |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 317 | |
| 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 Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 341 | const std::string& connectionName, |
| 342 | const std::string& path) |
| 343 | { |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 344 | sdbusplus::asio::getProperty<bool>( |
| 345 | *crow::connections::systemBus, connectionName, path, |
| 346 | "xyz.openbmc_project.Inventory.Item", "Present", |
Willy Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 347 | [asyncResp, path](const boost::system::error_code ec, |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 348 | const bool enabled) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 349 | // this interface isn't necessary, only check it if |
| 350 | // we get a good return |
| 351 | if (ec) |
| 352 | { |
| 353 | return; |
| 354 | } |
Willy Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 355 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 356 | if (!enabled) |
| 357 | { |
| 358 | asyncResp->res.jsonValue["Status"]["State"] = "Disabled"; |
| 359 | } |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 360 | }); |
Willy Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 364 | const std::string& connectionName, |
| 365 | const std::string& path) |
| 366 | { |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 367 | 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 Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 371 | // this interface isn't necessary, only check it |
| 372 | // if we get a good return |
| 373 | if (ec) |
| 374 | { |
| 375 | return; |
| 376 | } |
Willy Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 377 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 378 | // 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 Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 386 | }); |
Willy Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 389 | inline 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 | |
| 403 | inline 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 | |
| 425 | inline 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 Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 437 | 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 Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 447 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 448 | 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 Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 468 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 469 | else if (propertyName == "Capacity") |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 470 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 471 | const uint64_t* capacity = |
| 472 | std::get_if<uint64_t>(&property.second); |
| 473 | if (capacity == nullptr) |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 474 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 475 | BMCWEB_LOG_ERROR << "Illegal property: Capacity"; |
| 476 | messages::internalError(asyncResp->res); |
| 477 | return; |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 478 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 479 | if (*capacity == 0) |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 480 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 481 | // drive capacity not known |
| 482 | continue; |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 483 | } |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 484 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 485 | asyncResp->res.jsonValue["CapacityBytes"] = *capacity; |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 486 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 487 | 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 Broadbent | 3fe4d5c | 2022-05-06 14:42:35 -0700 | [diff] [blame] | 508 | 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 Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 526 | } |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 527 | }); |
| 528 | } |
| 529 | |
Nan Zhou | b53dcd9 | 2022-06-21 17:47:50 +0000 | [diff] [blame] | 530 | static 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 Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 534 | { |
| 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 Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 556 | inline void requestRoutesDrive(App& app) |
| 557 | { |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 558 | BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 559 | .privileges(redfish::privileges::getDrive) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 560 | .methods(boost::beast::http::verb::get)( |
| 561 | [&app](const crow::Request& req, |
| 562 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 563 | const std::string& systemName, const std::string& driveId) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 564 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 565 | { |
| 566 | return; |
| 567 | } |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 568 | if (systemName != "system") |
| 569 | { |
| 570 | messages::resourceNotFound(asyncResp->res, "ComputerSystem", |
| 571 | systemName); |
| 572 | return; |
| 573 | } |
| 574 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 575 | 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 Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 580 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 581 | BMCWEB_LOG_ERROR << "Drive mapper call error"; |
| 582 | messages::internalError(asyncResp->res); |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 583 | return; |
| 584 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 585 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 586 | auto drive = std::find_if( |
| 587 | subtree.begin(), subtree.end(), |
| 588 | [&driveId]( |
Nan Zhou | 8cb65f8 | 2022-06-15 05:12:24 +0000 | [diff] [blame] | 589 | const std::pair<std::string, |
| 590 | dbus::utility::MapperServiceMap>& object) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 591 | return sdbusplus::message::object_path(object.first) |
| 592 | .filename() == driveId; |
| 593 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 594 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 595 | if (drive == subtree.end()) |
| 596 | { |
| 597 | messages::resourceNotFound(asyncResp->res, "Drive", driveId); |
| 598 | return; |
| 599 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 600 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 601 | const std::string& path = drive->first; |
Nan Zhou | 8cb65f8 | 2022-06-15 05:12:24 +0000 | [diff] [blame] | 602 | const dbus::utility::MapperServiceMap& connectionNames = |
| 603 | drive->second; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 604 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 605 | 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 Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 610 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 611 | 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 Feist | e284a7c | 2019-11-20 16:20:23 -0800 | [diff] [blame] | 618 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 619 | 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 Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 625 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 626 | // default it to Enabled |
| 627 | asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 628 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 629 | auto health = std::make_shared<HealthPopulate>(asyncResp); |
| 630 | health->inventory.emplace_back(path); |
| 631 | health->populate(); |
Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 632 | |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 633 | addAllDriveInfo(asyncResp, connectionNames[0].first, path, |
| 634 | connectionNames[0].second); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 635 | }, |
| 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 Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 642 | }); |
| 643 | } |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 644 | |
| 645 | /** |
| 646 | * Chassis drives, this URL will show all the DriveCollection |
| 647 | * information |
| 648 | */ |
Nan Zhou | b53dcd9 | 2022-06-21 17:47:50 +0000 | [diff] [blame] | 649 | inline void chassisDriveCollectionGet( |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 650 | crow::App& app, const crow::Request& req, |
| 651 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 652 | const std::string& chassisId) |
| 653 | { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 654 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 655 | { |
| 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 Zhou | 8cb65f8 | 2022-06-15 05:12:24 +0000 | [diff] [blame] | 677 | for (const auto& [path, connectionNames] : subtree) |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 678 | { |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 679 | 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 Broadbent | 14bd7d9 | 2022-06-14 17:17:43 -0700 | [diff] [blame] | 694 | crow::utility::urlFromPieces("redfish", "v1", "Chassis", |
| 695 | chassisId, "Drives"); |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 696 | 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 Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 716 | sdbusplus::message::object_path drivePath(drive); |
| 717 | leafNames.push_back(drivePath.filename()); |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 718 | } |
| 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 | |
| 746 | inline 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 Zhou | b53dcd9 | 2022-06-21 17:47:50 +0000 | [diff] [blame] | 754 | inline 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 Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 759 | { |
| 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 Zhou | 8cb65f8 | 2022-06-15 05:12:24 +0000 | [diff] [blame] | 769 | for (const auto& [path, connectionNames] : subtree) |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 770 | { |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 771 | 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 Broadbent | a0cb40c | 2022-06-29 17:27:38 -0700 | [diff] [blame] | 784 | "redfish", "v1", "Chassis", chassisId, "Drives", driveName); |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 785 | |
| 786 | asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive"; |
John Edward Broadbent | a0cb40c | 2022-06-29 17:27:38 -0700 | [diff] [blame] | 787 | asyncResp->res.jsonValue["Name"] = driveName; |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 788 | 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 Zhou | b53dcd9 | 2022-06-21 17:47:50 +0000 | [diff] [blame] | 802 | inline 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 Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 807 | { |
| 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 Zhou | b53dcd9 | 2022-06-21 17:47:50 +0000 | [diff] [blame] | 834 | inline 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 Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 839 | { |
Michal Orzel | 03810a1 | 2022-06-15 14:04:28 +0200 | [diff] [blame] | 840 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 841 | { |
| 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 Zhou | 8cb65f8 | 2022-06-15 05:12:24 +0000 | [diff] [blame] | 860 | for (const auto& [path, connectionNames] : subtree) |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 861 | { |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 862 | 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 | */ |
| 899 | inline 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 Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 907 | } // namespace redfish |