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