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 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 18 | #include "app.hpp" |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 19 | #include "dbus_utility.hpp" |
James Feist | 2ad9c2f | 2019-10-29 16:26:48 -0700 | [diff] [blame] | 20 | #include "health.hpp" |
James Feist | e284a7c | 2019-11-20 16:20:23 -0800 | [diff] [blame] | 21 | #include "openbmc_dbus_rest.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 22 | #include "query.hpp" |
| 23 | #include "registries/privilege_registry.hpp" |
| 24 | #include "utils/dbus_utils.hpp" |
James Feist | 2ad9c2f | 2019-10-29 16:26:48 -0700 | [diff] [blame] | 25 | |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 26 | #include <boost/system/error_code.hpp> |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 27 | #include <sdbusplus/asio/property.hpp> |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 28 | #include <sdbusplus/unpack_properties.hpp> |
Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 29 | |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 30 | #include <array> |
| 31 | #include <string_view> |
| 32 | |
Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 33 | namespace redfish |
| 34 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 35 | inline void requestRoutesStorageCollection(App& app) |
Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 36 | { |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 37 | BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 38 | .privileges(redfish::privileges::getStorageCollection) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 39 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 40 | [&app](const crow::Request& req, |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 41 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 42 | const std::string& systemName) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 43 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 44 | { |
| 45 | return; |
| 46 | } |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 47 | if (systemName != "system") |
| 48 | { |
| 49 | messages::resourceNotFound(asyncResp->res, "ComputerSystem", |
| 50 | systemName); |
| 51 | return; |
| 52 | } |
| 53 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 54 | asyncResp->res.jsonValue["@odata.type"] = |
| 55 | "#StorageCollection.StorageCollection"; |
| 56 | asyncResp->res.jsonValue["@odata.id"] = |
| 57 | "/redfish/v1/Systems/system/Storage"; |
| 58 | asyncResp->res.jsonValue["Name"] = "Storage Collection"; |
| 59 | nlohmann::json::array_t members; |
| 60 | nlohmann::json::object_t member; |
| 61 | member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1"; |
| 62 | members.emplace_back(member); |
| 63 | asyncResp->res.jsonValue["Members"] = std::move(members); |
| 64 | asyncResp->res.jsonValue["Members@odata.count"] = 1; |
| 65 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 66 | } |
Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 67 | |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 68 | inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 69 | const std::shared_ptr<HealthPopulate>& health) |
| 70 | { |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 71 | const std::array<std::string_view, 1> interfaces = { |
| 72 | "xyz.openbmc_project.Inventory.Item.Drive"}; |
| 73 | dbus::utility::getSubTreePaths( |
| 74 | "/xyz/openbmc_project/inventory", 0, interfaces, |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 75 | [asyncResp, health]( |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 76 | const boost::system::error_code& ec, |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 77 | const dbus::utility::MapperGetSubTreePathsResponse& driveList) { |
| 78 | if (ec) |
| 79 | { |
| 80 | BMCWEB_LOG_ERROR << "Drive mapper call error"; |
| 81 | messages::internalError(asyncResp->res); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"]; |
| 86 | driveArray = nlohmann::json::array(); |
| 87 | auto& count = asyncResp->res.jsonValue["Drives@odata.count"]; |
| 88 | count = 0; |
| 89 | |
| 90 | health->inventory.insert(health->inventory.end(), driveList.begin(), |
| 91 | driveList.end()); |
| 92 | |
| 93 | for (const std::string& drive : driveList) |
| 94 | { |
| 95 | sdbusplus::message::object_path object(drive); |
| 96 | if (object.filename().empty()) |
| 97 | { |
| 98 | BMCWEB_LOG_ERROR << "Failed to find filename in " << drive; |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | nlohmann::json::object_t driveJson; |
| 103 | driveJson["@odata.id"] = |
| 104 | "/redfish/v1/Systems/system/Storage/1/Drives/" + |
| 105 | object.filename(); |
| 106 | driveArray.push_back(std::move(driveJson)); |
| 107 | } |
| 108 | |
| 109 | count = driveArray.size(); |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 110 | }); |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | inline void |
| 114 | getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 115 | const std::shared_ptr<HealthPopulate>& health) |
| 116 | { |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 117 | constexpr std::array<std::string_view, 1> interfaces = { |
| 118 | "xyz.openbmc_project.Inventory.Item.StorageController"}; |
| 119 | dbus::utility::getSubTree( |
| 120 | "/xyz/openbmc_project/inventory", 0, interfaces, |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 121 | [asyncResp, |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 122 | health](const boost::system::error_code& ec, |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 123 | const dbus::utility::MapperGetSubTreeResponse& subtree) { |
| 124 | if (ec || subtree.empty()) |
| 125 | { |
| 126 | // doesn't have to be there |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"]; |
| 131 | root = nlohmann::json::array(); |
| 132 | for (const auto& [path, interfaceDict] : subtree) |
| 133 | { |
| 134 | sdbusplus::message::object_path object(path); |
| 135 | std::string id = object.filename(); |
| 136 | if (id.empty()) |
| 137 | { |
| 138 | BMCWEB_LOG_ERROR << "Failed to find filename in " << path; |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | if (interfaceDict.size() != 1) |
| 143 | { |
| 144 | BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size() |
| 145 | << ", greater than 1"; |
| 146 | messages::internalError(asyncResp->res); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | const std::string& connectionName = interfaceDict.front().first; |
| 151 | |
| 152 | size_t index = root.size(); |
| 153 | nlohmann::json& storageController = |
| 154 | root.emplace_back(nlohmann::json::object()); |
| 155 | |
| 156 | storageController["@odata.type"] = |
| 157 | "#Storage.v1_7_0.StorageController"; |
| 158 | storageController["@odata.id"] = |
| 159 | "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" + |
| 160 | std::to_string(index); |
| 161 | storageController["Name"] = id; |
| 162 | storageController["MemberId"] = id; |
| 163 | storageController["Status"]["State"] = "Enabled"; |
| 164 | |
| 165 | sdbusplus::asio::getProperty<bool>( |
| 166 | *crow::connections::systemBus, connectionName, path, |
| 167 | "xyz.openbmc_project.Inventory.Item", "Present", |
| 168 | [asyncResp, index](const boost::system::error_code ec2, |
Willy Tu | cef57e8 | 2022-12-15 16:42:02 -0800 | [diff] [blame^] | 169 | bool isPresent) { |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 170 | // this interface isn't necessary, only check it |
| 171 | // if we get a good return |
| 172 | if (ec2) |
| 173 | { |
| 174 | return; |
| 175 | } |
Willy Tu | cef57e8 | 2022-12-15 16:42:02 -0800 | [diff] [blame^] | 176 | if (!isPresent) |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 177 | { |
| 178 | asyncResp->res.jsonValue["StorageControllers"][index] |
Willy Tu | cef57e8 | 2022-12-15 16:42:02 -0800 | [diff] [blame^] | 179 | ["Status"]["State"] = "Absent"; |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 180 | } |
| 181 | }); |
| 182 | |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 183 | sdbusplus::asio::getAllProperties( |
| 184 | *crow::connections::systemBus, connectionName, path, |
| 185 | "xyz.openbmc_project.Inventory.Decorator.Asset", |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 186 | [asyncResp, index]( |
| 187 | const boost::system::error_code ec2, |
| 188 | const std::vector< |
| 189 | std::pair<std::string, dbus::utility::DbusVariantType>>& |
| 190 | propertiesList) { |
| 191 | if (ec2) |
| 192 | { |
| 193 | // this interface isn't necessary |
| 194 | return; |
| 195 | } |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 196 | |
| 197 | const std::string* partNumber = nullptr; |
| 198 | const std::string* serialNumber = nullptr; |
| 199 | const std::string* manufacturer = nullptr; |
| 200 | const std::string* model = nullptr; |
| 201 | |
| 202 | const bool success = sdbusplus::unpackPropertiesNoThrow( |
| 203 | dbus_utils::UnpackErrorPrinter(), propertiesList, |
| 204 | "PartNumber", partNumber, "SerialNumber", serialNumber, |
| 205 | "Manufacturer", manufacturer, "Model", model); |
| 206 | |
| 207 | if (!success) |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 208 | { |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 209 | messages::internalError(asyncResp->res); |
| 210 | return; |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 211 | } |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 212 | |
| 213 | nlohmann::json& controller = |
| 214 | asyncResp->res.jsonValue["StorageControllers"][index]; |
| 215 | |
| 216 | if (partNumber != nullptr) |
| 217 | { |
| 218 | controller["PartNumber"] = *partNumber; |
| 219 | } |
| 220 | |
| 221 | if (serialNumber != nullptr) |
| 222 | { |
| 223 | controller["SerialNumber"] = *serialNumber; |
| 224 | } |
| 225 | |
| 226 | if (manufacturer != nullptr) |
| 227 | { |
| 228 | controller["Manufacturer"] = *manufacturer; |
| 229 | } |
| 230 | |
| 231 | if (model != nullptr) |
| 232 | { |
| 233 | controller["Model"] = *model; |
| 234 | } |
| 235 | }); |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // this is done after we know the json array will no longer |
| 239 | // be resized, as json::array uses vector underneath and we |
| 240 | // need references to its members that won't change |
| 241 | size_t count = 0; |
| 242 | // Pointer based on |asyncResp->res.jsonValue| |
| 243 | nlohmann::json::json_pointer rootPtr = |
| 244 | "/StorageControllers"_json_pointer; |
| 245 | for (const auto& [path, interfaceDict] : subtree) |
| 246 | { |
| 247 | auto subHealth = std::make_shared<HealthPopulate>( |
| 248 | asyncResp, rootPtr / count / "Status"); |
| 249 | subHealth->inventory.emplace_back(path); |
| 250 | health->inventory.emplace_back(path); |
| 251 | health->children.emplace_back(subHealth); |
| 252 | count++; |
| 253 | } |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 254 | }); |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 255 | } |
| 256 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 257 | inline void requestRoutesStorage(App& app) |
Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 258 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 259 | BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 260 | .privileges(redfish::privileges::getStorage) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 261 | .methods(boost::beast::http::verb::get)( |
| 262 | [&app](const crow::Request& req, |
| 263 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 264 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 265 | { |
| 266 | return; |
| 267 | } |
| 268 | asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage"; |
| 269 | asyncResp->res.jsonValue["@odata.id"] = |
| 270 | "/redfish/v1/Systems/system/Storage/1"; |
| 271 | asyncResp->res.jsonValue["Name"] = "Storage"; |
| 272 | asyncResp->res.jsonValue["Id"] = "1"; |
| 273 | asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; |
| 274 | |
| 275 | auto health = std::make_shared<HealthPopulate>(asyncResp); |
| 276 | health->populate(); |
| 277 | |
Willy Tu | a85afbe | 2021-12-28 14:43:47 -0800 | [diff] [blame] | 278 | getDrives(asyncResp, health); |
| 279 | getStorageControllers(asyncResp, health); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 280 | }); |
| 281 | } |
| 282 | |
Willy Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 283 | inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 284 | const std::string& connectionName, |
| 285 | const std::string& path) |
| 286 | { |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 287 | sdbusplus::asio::getAllProperties( |
| 288 | *crow::connections::systemBus, connectionName, path, |
| 289 | "xyz.openbmc_project.Inventory.Decorator.Asset", |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 290 | [asyncResp](const boost::system::error_code ec, |
| 291 | const std::vector< |
| 292 | std::pair<std::string, dbus::utility::DbusVariantType>>& |
| 293 | propertiesList) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 294 | if (ec) |
| 295 | { |
| 296 | // this interface isn't necessary |
| 297 | return; |
| 298 | } |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 299 | |
| 300 | const std::string* partNumber = nullptr; |
| 301 | const std::string* serialNumber = nullptr; |
| 302 | const std::string* manufacturer = nullptr; |
| 303 | const std::string* model = nullptr; |
| 304 | |
| 305 | const bool success = sdbusplus::unpackPropertiesNoThrow( |
| 306 | dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber", |
| 307 | partNumber, "SerialNumber", serialNumber, "Manufacturer", |
| 308 | manufacturer, "Model", model); |
| 309 | |
| 310 | if (!success) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 311 | { |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 312 | messages::internalError(asyncResp->res); |
| 313 | return; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 314 | } |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 315 | |
| 316 | if (partNumber != nullptr) |
| 317 | { |
| 318 | asyncResp->res.jsonValue["PartNumber"] = *partNumber; |
| 319 | } |
| 320 | |
| 321 | if (serialNumber != nullptr) |
| 322 | { |
| 323 | asyncResp->res.jsonValue["SerialNumber"] = *serialNumber; |
| 324 | } |
| 325 | |
| 326 | if (manufacturer != nullptr) |
| 327 | { |
| 328 | asyncResp->res.jsonValue["Manufacturer"] = *manufacturer; |
| 329 | } |
| 330 | |
| 331 | if (model != nullptr) |
| 332 | { |
| 333 | asyncResp->res.jsonValue["Model"] = *model; |
| 334 | } |
| 335 | }); |
Willy Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 339 | const std::string& connectionName, |
| 340 | const std::string& path) |
| 341 | { |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 342 | sdbusplus::asio::getProperty<bool>( |
| 343 | *crow::connections::systemBus, connectionName, path, |
| 344 | "xyz.openbmc_project.Inventory.Item", "Present", |
Willy Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 345 | [asyncResp, path](const boost::system::error_code ec, |
Willy Tu | cef57e8 | 2022-12-15 16:42:02 -0800 | [diff] [blame^] | 346 | const bool isPresent) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 347 | // this interface isn't necessary, only check it if |
| 348 | // we get a good return |
| 349 | if (ec) |
| 350 | { |
| 351 | return; |
| 352 | } |
Willy Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 353 | |
Willy Tu | cef57e8 | 2022-12-15 16:42:02 -0800 | [diff] [blame^] | 354 | if (!isPresent) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 355 | { |
Willy Tu | cef57e8 | 2022-12-15 16:42:02 -0800 | [diff] [blame^] | 356 | asyncResp->res.jsonValue["Status"]["State"] = "Absent"; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 357 | } |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 358 | }); |
Willy Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 362 | const std::string& connectionName, |
| 363 | const std::string& path) |
| 364 | { |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 365 | sdbusplus::asio::getProperty<bool>( |
| 366 | *crow::connections::systemBus, connectionName, path, |
| 367 | "xyz.openbmc_project.State.Drive", "Rebuilding", |
| 368 | [asyncResp](const boost::system::error_code ec, const bool updating) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 369 | // this interface isn't necessary, only check it |
| 370 | // if we get a good return |
| 371 | if (ec) |
| 372 | { |
| 373 | return; |
| 374 | } |
Willy Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 375 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 376 | // updating and disabled in the backend shouldn't be |
| 377 | // able to be set at the same time, so we don't need |
| 378 | // to check for the race condition of these two |
| 379 | // calls |
| 380 | if (updating) |
| 381 | { |
| 382 | asyncResp->res.jsonValue["Status"]["State"] = "Updating"; |
| 383 | } |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 384 | }); |
Willy Tu | 0391317 | 2021-11-08 02:03:19 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 387 | inline std::optional<std::string> convertDriveType(const std::string& type) |
| 388 | { |
| 389 | if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD") |
| 390 | { |
| 391 | return "HDD"; |
| 392 | } |
| 393 | if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD") |
| 394 | { |
| 395 | return "SSD"; |
| 396 | } |
| 397 | |
| 398 | return std::nullopt; |
| 399 | } |
| 400 | |
| 401 | inline std::optional<std::string> convertDriveProtocol(const std::string& proto) |
| 402 | { |
| 403 | if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS") |
| 404 | { |
| 405 | return "SAS"; |
| 406 | } |
| 407 | if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA") |
| 408 | { |
| 409 | return "SATA"; |
| 410 | } |
| 411 | if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe") |
| 412 | { |
| 413 | return "NVMe"; |
| 414 | } |
| 415 | if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC") |
| 416 | { |
| 417 | return "FC"; |
| 418 | } |
| 419 | |
| 420 | return std::nullopt; |
| 421 | } |
| 422 | |
| 423 | inline void |
| 424 | getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 425 | const std::string& connectionName, |
| 426 | const std::string& path) |
| 427 | { |
| 428 | sdbusplus::asio::getAllProperties( |
| 429 | *crow::connections::systemBus, connectionName, path, |
| 430 | "xyz.openbmc_project.Inventory.Item.Drive", |
| 431 | [asyncResp](const boost::system::error_code ec, |
| 432 | const std::vector< |
| 433 | std::pair<std::string, dbus::utility::DbusVariantType>>& |
| 434 | propertiesList) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 435 | if (ec) |
| 436 | { |
| 437 | // this interface isn't required |
| 438 | return; |
| 439 | } |
| 440 | for (const std::pair<std::string, dbus::utility::DbusVariantType>& |
| 441 | property : propertiesList) |
| 442 | { |
| 443 | const std::string& propertyName = property.first; |
| 444 | if (propertyName == "Type") |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 445 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 446 | const std::string* value = |
| 447 | std::get_if<std::string>(&property.second); |
| 448 | if (value == nullptr) |
| 449 | { |
| 450 | // illegal property |
| 451 | BMCWEB_LOG_ERROR << "Illegal property: Type"; |
| 452 | messages::internalError(asyncResp->res); |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | std::optional<std::string> mediaType = convertDriveType(*value); |
| 457 | if (!mediaType) |
| 458 | { |
| 459 | BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: " |
| 460 | << *value; |
| 461 | messages::internalError(asyncResp->res); |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | asyncResp->res.jsonValue["MediaType"] = *mediaType; |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 466 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 467 | else if (propertyName == "Capacity") |
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 | const uint64_t* capacity = |
| 470 | std::get_if<uint64_t>(&property.second); |
| 471 | if (capacity == nullptr) |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 472 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 473 | BMCWEB_LOG_ERROR << "Illegal property: Capacity"; |
| 474 | messages::internalError(asyncResp->res); |
| 475 | return; |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 476 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 477 | if (*capacity == 0) |
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 | // drive capacity not known |
| 480 | continue; |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 481 | } |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 482 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 483 | asyncResp->res.jsonValue["CapacityBytes"] = *capacity; |
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 | else if (propertyName == "Protocol") |
| 486 | { |
| 487 | const std::string* value = |
| 488 | std::get_if<std::string>(&property.second); |
| 489 | if (value == nullptr) |
| 490 | { |
| 491 | BMCWEB_LOG_ERROR << "Illegal property: Protocol"; |
| 492 | messages::internalError(asyncResp->res); |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | std::optional<std::string> proto = convertDriveProtocol(*value); |
| 497 | if (!proto) |
| 498 | { |
| 499 | BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: " |
| 500 | << *value; |
| 501 | messages::internalError(asyncResp->res); |
| 502 | return; |
| 503 | } |
| 504 | asyncResp->res.jsonValue["Protocol"] = *proto; |
| 505 | } |
John Edward Broadbent | 3fe4d5c | 2022-05-06 14:42:35 -0700 | [diff] [blame] | 506 | else if (propertyName == "PredictedMediaLifeLeftPercent") |
| 507 | { |
| 508 | const uint8_t* lifeLeft = |
| 509 | std::get_if<uint8_t>(&property.second); |
| 510 | if (lifeLeft == nullptr) |
| 511 | { |
| 512 | BMCWEB_LOG_ERROR |
| 513 | << "Illegal property: PredictedMediaLifeLeftPercent"; |
| 514 | messages::internalError(asyncResp->res); |
| 515 | return; |
| 516 | } |
| 517 | // 255 means reading the value is not supported |
| 518 | if (*lifeLeft != 255) |
| 519 | { |
| 520 | asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] = |
| 521 | *lifeLeft; |
| 522 | } |
| 523 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 524 | } |
Willy Tu | 19b8e9a | 2021-11-08 02:55:03 -0800 | [diff] [blame] | 525 | }); |
| 526 | } |
| 527 | |
Nan Zhou | b53dcd9 | 2022-06-21 17:47:50 +0000 | [diff] [blame] | 528 | static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 529 | const std::string& connectionName, |
| 530 | const std::string& path, |
| 531 | const std::vector<std::string>& interfaces) |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 532 | { |
| 533 | for (const std::string& interface : interfaces) |
| 534 | { |
| 535 | if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset") |
| 536 | { |
| 537 | getDriveAsset(asyncResp, connectionName, path); |
| 538 | } |
| 539 | else if (interface == "xyz.openbmc_project.Inventory.Item") |
| 540 | { |
| 541 | getDrivePresent(asyncResp, connectionName, path); |
| 542 | } |
| 543 | else if (interface == "xyz.openbmc_project.State.Drive") |
| 544 | { |
| 545 | getDriveState(asyncResp, connectionName, path); |
| 546 | } |
| 547 | else if (interface == "xyz.openbmc_project.Inventory.Item.Drive") |
| 548 | { |
| 549 | getDriveItemProperties(asyncResp, connectionName, path); |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 554 | inline void requestRoutesDrive(App& app) |
| 555 | { |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 556 | BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Storage/1/Drives/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 557 | .privileges(redfish::privileges::getDrive) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 558 | .methods(boost::beast::http::verb::get)( |
| 559 | [&app](const crow::Request& req, |
| 560 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 561 | const std::string& systemName, const std::string& driveId) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 562 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 563 | { |
| 564 | return; |
| 565 | } |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 566 | if (systemName != "system") |
| 567 | { |
| 568 | messages::resourceNotFound(asyncResp->res, "ComputerSystem", |
| 569 | systemName); |
| 570 | return; |
| 571 | } |
| 572 | |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 573 | constexpr std::array<std::string_view, 1> interfaces = { |
| 574 | "xyz.openbmc_project.Inventory.Item.Drive"}; |
| 575 | dbus::utility::getSubTree( |
| 576 | "/xyz/openbmc_project/inventory", 0, interfaces, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 577 | [asyncResp, |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 578 | driveId](const boost::system::error_code& ec, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 579 | const dbus::utility::MapperGetSubTreeResponse& subtree) { |
| 580 | if (ec) |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 581 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 582 | BMCWEB_LOG_ERROR << "Drive mapper call error"; |
| 583 | messages::internalError(asyncResp->res); |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 584 | return; |
| 585 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 586 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 587 | auto drive = std::find_if( |
| 588 | subtree.begin(), subtree.end(), |
| 589 | [&driveId]( |
Nan Zhou | 8cb65f8 | 2022-06-15 05:12:24 +0000 | [diff] [blame] | 590 | const std::pair<std::string, |
| 591 | dbus::utility::MapperServiceMap>& object) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 592 | return sdbusplus::message::object_path(object.first) |
| 593 | .filename() == driveId; |
| 594 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 595 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 596 | if (drive == subtree.end()) |
| 597 | { |
| 598 | messages::resourceNotFound(asyncResp->res, "Drive", driveId); |
| 599 | return; |
| 600 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 601 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 602 | const std::string& path = drive->first; |
Nan Zhou | 8cb65f8 | 2022-06-15 05:12:24 +0000 | [diff] [blame] | 603 | const dbus::utility::MapperServiceMap& connectionNames = |
| 604 | drive->second; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 605 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 606 | asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive"; |
| 607 | asyncResp->res.jsonValue["@odata.id"] = |
| 608 | "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId; |
| 609 | asyncResp->res.jsonValue["Name"] = driveId; |
| 610 | asyncResp->res.jsonValue["Id"] = driveId; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 611 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 612 | if (connectionNames.size() != 1) |
| 613 | { |
| 614 | BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size() |
| 615 | << ", not equal to 1"; |
| 616 | messages::internalError(asyncResp->res); |
| 617 | return; |
| 618 | } |
James Feist | e284a7c | 2019-11-20 16:20:23 -0800 | [diff] [blame] | 619 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 620 | getMainChassisId( |
| 621 | asyncResp, [](const std::string& chassisId, |
| 622 | const std::shared_ptr<bmcweb::AsyncResp>& aRsp) { |
| 623 | aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] = |
| 624 | "/redfish/v1/Chassis/" + chassisId; |
| 625 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 626 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 627 | // default it to Enabled |
| 628 | asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 629 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 630 | auto health = std::make_shared<HealthPopulate>(asyncResp); |
| 631 | health->inventory.emplace_back(path); |
| 632 | health->populate(); |
Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 633 | |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 634 | addAllDriveInfo(asyncResp, connectionNames[0].first, path, |
| 635 | connectionNames[0].second); |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 636 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 637 | }); |
| 638 | } |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 639 | |
| 640 | /** |
| 641 | * Chassis drives, this URL will show all the DriveCollection |
| 642 | * information |
| 643 | */ |
Nan Zhou | b53dcd9 | 2022-06-21 17:47:50 +0000 | [diff] [blame] | 644 | inline void chassisDriveCollectionGet( |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 645 | crow::App& app, const crow::Request& req, |
| 646 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 647 | const std::string& chassisId) |
| 648 | { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 649 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 650 | { |
| 651 | return; |
| 652 | } |
| 653 | |
| 654 | // mapper call lambda |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 655 | constexpr std::array<std::string_view, 2> interfaces = { |
| 656 | "xyz.openbmc_project.Inventory.Item.Board", |
| 657 | "xyz.openbmc_project.Inventory.Item.Chassis"}; |
| 658 | dbus::utility::getSubTree( |
| 659 | "/xyz/openbmc_project/inventory", 0, interfaces, |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 660 | [asyncResp, |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 661 | chassisId](const boost::system::error_code& ec, |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 662 | const dbus::utility::MapperGetSubTreeResponse& subtree) { |
| 663 | if (ec) |
| 664 | { |
| 665 | if (ec == boost::system::errc::host_unreachable) |
| 666 | { |
| 667 | messages::resourceNotFound(asyncResp->res, "Chassis", |
| 668 | chassisId); |
| 669 | return; |
| 670 | } |
| 671 | messages::internalError(asyncResp->res); |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | // Iterate over all retrieved ObjectPaths. |
Nan Zhou | 8cb65f8 | 2022-06-15 05:12:24 +0000 | [diff] [blame] | 676 | for (const auto& [path, connectionNames] : subtree) |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 677 | { |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 678 | sdbusplus::message::object_path objPath(path); |
| 679 | if (objPath.filename() != chassisId) |
| 680 | { |
| 681 | continue; |
| 682 | } |
| 683 | |
| 684 | if (connectionNames.empty()) |
| 685 | { |
| 686 | BMCWEB_LOG_ERROR << "Got 0 Connection names"; |
| 687 | continue; |
| 688 | } |
| 689 | |
| 690 | asyncResp->res.jsonValue["@odata.type"] = |
| 691 | "#DriveCollection.DriveCollection"; |
| 692 | asyncResp->res.jsonValue["@odata.id"] = |
John Edward Broadbent | 14bd7d9 | 2022-06-14 17:17:43 -0700 | [diff] [blame] | 693 | crow::utility::urlFromPieces("redfish", "v1", "Chassis", |
| 694 | chassisId, "Drives"); |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 695 | asyncResp->res.jsonValue["Name"] = "Drive Collection"; |
| 696 | |
| 697 | // Association lambda |
| 698 | sdbusplus::asio::getProperty<std::vector<std::string>>( |
| 699 | *crow::connections::systemBus, |
| 700 | "xyz.openbmc_project.ObjectMapper", path + "/drive", |
| 701 | "xyz.openbmc_project.Association", "endpoints", |
| 702 | [asyncResp, chassisId](const boost::system::error_code ec3, |
| 703 | const std::vector<std::string>& resp) { |
| 704 | if (ec3) |
| 705 | { |
| 706 | BMCWEB_LOG_ERROR << "Error in chassis Drive association "; |
| 707 | } |
| 708 | nlohmann::json& members = asyncResp->res.jsonValue["Members"]; |
| 709 | // important if array is empty |
| 710 | members = nlohmann::json::array(); |
| 711 | |
| 712 | std::vector<std::string> leafNames; |
| 713 | for (const auto& drive : resp) |
| 714 | { |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 715 | sdbusplus::message::object_path drivePath(drive); |
| 716 | leafNames.push_back(drivePath.filename()); |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | std::sort(leafNames.begin(), leafNames.end(), |
| 720 | AlphanumLess<std::string>()); |
| 721 | |
| 722 | for (const auto& leafName : leafNames) |
| 723 | { |
| 724 | nlohmann::json::object_t member; |
| 725 | member["@odata.id"] = crow::utility::urlFromPieces( |
| 726 | "redfish", "v1", "Chassis", chassisId, "Drives", |
| 727 | leafName); |
| 728 | members.push_back(std::move(member)); |
| 729 | // navigation links will be registered in next patch set |
| 730 | } |
| 731 | asyncResp->res.jsonValue["Members@odata.count"] = resp.size(); |
| 732 | }); // end association lambda |
| 733 | |
| 734 | } // end Iterate over all retrieved ObjectPaths |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 735 | }); |
John Edward Broadbent | 92903bd | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | inline void requestRoutesChassisDrive(App& app) |
| 739 | { |
| 740 | BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/") |
| 741 | .privileges(redfish::privileges::getDriveCollection) |
| 742 | .methods(boost::beast::http::verb::get)( |
| 743 | std::bind_front(chassisDriveCollectionGet, std::ref(app))); |
| 744 | } |
| 745 | |
Nan Zhou | b53dcd9 | 2022-06-21 17:47:50 +0000 | [diff] [blame] | 746 | inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 747 | const std::string& chassisId, |
| 748 | const std::string& driveName, |
| 749 | const boost::system::error_code ec, |
| 750 | const dbus::utility::MapperGetSubTreeResponse& subtree) |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 751 | { |
| 752 | |
| 753 | if (ec) |
| 754 | { |
| 755 | BMCWEB_LOG_DEBUG << "DBUS response error " << ec; |
| 756 | messages::internalError(asyncResp->res); |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | // Iterate over all retrieved ObjectPaths. |
Nan Zhou | 8cb65f8 | 2022-06-15 05:12:24 +0000 | [diff] [blame] | 761 | for (const auto& [path, connectionNames] : subtree) |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 762 | { |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 763 | sdbusplus::message::object_path objPath(path); |
| 764 | if (objPath.filename() != driveName) |
| 765 | { |
| 766 | continue; |
| 767 | } |
| 768 | |
| 769 | if (connectionNames.empty()) |
| 770 | { |
| 771 | BMCWEB_LOG_ERROR << "Got 0 Connection names"; |
| 772 | continue; |
| 773 | } |
| 774 | |
| 775 | asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces( |
John Edward Broadbent | a0cb40c | 2022-06-29 17:27:38 -0700 | [diff] [blame] | 776 | "redfish", "v1", "Chassis", chassisId, "Drives", driveName); |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 777 | |
| 778 | asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive"; |
John Edward Broadbent | a0cb40c | 2022-06-29 17:27:38 -0700 | [diff] [blame] | 779 | asyncResp->res.jsonValue["Name"] = driveName; |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 780 | asyncResp->res.jsonValue["Id"] = driveName; |
| 781 | // default it to Enabled |
| 782 | asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; |
| 783 | |
| 784 | nlohmann::json::object_t linkChassisNav; |
| 785 | linkChassisNav["@odata.id"] = |
| 786 | crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId); |
| 787 | asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav; |
| 788 | |
| 789 | addAllDriveInfo(asyncResp, connectionNames[0].first, path, |
| 790 | connectionNames[0].second); |
| 791 | } |
| 792 | } |
| 793 | |
Nan Zhou | b53dcd9 | 2022-06-21 17:47:50 +0000 | [diff] [blame] | 794 | inline void |
| 795 | matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 796 | const std::string& chassisId, |
| 797 | const std::string& driveName, |
| 798 | const std::vector<std::string>& resp) |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 799 | { |
| 800 | |
| 801 | for (const std::string& drivePath : resp) |
| 802 | { |
| 803 | sdbusplus::message::object_path path(drivePath); |
| 804 | std::string leaf = path.filename(); |
| 805 | if (leaf != driveName) |
| 806 | { |
| 807 | continue; |
| 808 | } |
| 809 | // mapper call drive |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 810 | constexpr std::array<std::string_view, 1> driveInterface = { |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 811 | "xyz.openbmc_project.Inventory.Item.Drive"}; |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 812 | dbus::utility::getSubTree( |
| 813 | "/xyz/openbmc_project/inventory", 0, driveInterface, |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 814 | [asyncResp, chassisId, driveName]( |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 815 | const boost::system::error_code& ec, |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 816 | const dbus::utility::MapperGetSubTreeResponse& subtree) { |
| 817 | buildDrive(asyncResp, chassisId, driveName, ec, subtree); |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 818 | }); |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
Nan Zhou | b53dcd9 | 2022-06-21 17:47:50 +0000 | [diff] [blame] | 822 | inline void |
| 823 | handleChassisDriveGet(crow::App& app, const crow::Request& req, |
| 824 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 825 | const std::string& chassisId, |
| 826 | const std::string& driveName) |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 827 | { |
Michal Orzel | 03810a1 | 2022-06-15 14:04:28 +0200 | [diff] [blame] | 828 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 829 | { |
| 830 | return; |
| 831 | } |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 832 | constexpr std::array<std::string_view, 2> interfaces = { |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 833 | "xyz.openbmc_project.Inventory.Item.Board", |
| 834 | "xyz.openbmc_project.Inventory.Item.Chassis"}; |
| 835 | |
| 836 | // mapper call chassis |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 837 | dbus::utility::getSubTree( |
| 838 | "/xyz/openbmc_project/inventory", 0, interfaces, |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 839 | [asyncResp, chassisId, |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 840 | driveName](const boost::system::error_code& ec, |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 841 | const dbus::utility::MapperGetSubTreeResponse& subtree) { |
| 842 | if (ec) |
| 843 | { |
| 844 | messages::internalError(asyncResp->res); |
| 845 | return; |
| 846 | } |
| 847 | |
| 848 | // Iterate over all retrieved ObjectPaths. |
Nan Zhou | 8cb65f8 | 2022-06-15 05:12:24 +0000 | [diff] [blame] | 849 | for (const auto& [path, connectionNames] : subtree) |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 850 | { |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 851 | sdbusplus::message::object_path objPath(path); |
| 852 | if (objPath.filename() != chassisId) |
| 853 | { |
| 854 | continue; |
| 855 | } |
| 856 | |
| 857 | if (connectionNames.empty()) |
| 858 | { |
| 859 | BMCWEB_LOG_ERROR << "Got 0 Connection names"; |
| 860 | continue; |
| 861 | } |
| 862 | |
| 863 | sdbusplus::asio::getProperty<std::vector<std::string>>( |
| 864 | *crow::connections::systemBus, |
| 865 | "xyz.openbmc_project.ObjectMapper", path + "/drive", |
| 866 | "xyz.openbmc_project.Association", "endpoints", |
| 867 | [asyncResp, chassisId, |
| 868 | driveName](const boost::system::error_code ec3, |
| 869 | const std::vector<std::string>& resp) { |
| 870 | if (ec3) |
| 871 | { |
| 872 | return; // no drives = no failures |
| 873 | } |
| 874 | matchAndFillDrive(asyncResp, chassisId, driveName, resp); |
| 875 | }); |
| 876 | break; |
| 877 | } |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 878 | }); |
John Edward Broadbent | e56ed6b | 2022-04-26 13:40:59 -0700 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | /** |
| 882 | * This URL will show the drive interface for the specific drive in the chassis |
| 883 | */ |
| 884 | inline void requestRoutesChassisDriveName(App& app) |
| 885 | { |
| 886 | BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/") |
| 887 | .privileges(redfish::privileges::getChassis) |
| 888 | .methods(boost::beast::http::verb::get)( |
| 889 | std::bind_front(handleChassisDriveGet, std::ref(app))); |
| 890 | } |
| 891 | |
Nikhil Potade | a25aecc | 2019-08-23 16:35:26 -0700 | [diff] [blame] | 892 | } // namespace redfish |