Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 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 | |
| 18 | #include "node.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 19 | |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 20 | #include <boost/container/flat_map.hpp> |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 21 | #include <variant> |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 22 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 23 | namespace redfish |
| 24 | { |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * DBus types primitives for several generic DBus interfaces |
| 28 | * TODO(Pawel) consider move this to separate file into boost::dbus |
| 29 | */ |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 30 | // Note, this is not a very useful Variant, but because it isn't used to get |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 31 | // values, it should be as simple as possible |
| 32 | // TODO(ed) invent a nullvariant type |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 33 | using VariantType = std::variant<bool, std::string, uint64_t>; |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 34 | using ManagedObjectsType = std::vector<std::pair< |
| 35 | sdbusplus::message::object_path, |
| 36 | std::vector<std::pair<std::string, |
| 37 | std::vector<std::pair<std::string, VariantType>>>>>>; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 38 | |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 39 | using PropertiesType = boost::container::flat_map<std::string, VariantType>; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 40 | |
Qiang XU | c181942 | 2019-02-27 13:51:32 +0800 | [diff] [blame^] | 41 | void getIntrusionByService(std::shared_ptr<AsyncResp> aResp, |
| 42 | const std::string &service, |
| 43 | const std::string &objPath) |
| 44 | { |
| 45 | BMCWEB_LOG_DEBUG << "Get intrusion status by service \n"; |
| 46 | |
| 47 | crow::connections::systemBus->async_method_call( |
| 48 | [aResp{std::move(aResp)}](const boost::system::error_code ec, |
| 49 | const std::variant<std::string> &value) { |
| 50 | if (ec) |
| 51 | { |
| 52 | // do not add err msg in redfish response, becaues this is not |
| 53 | // mandatory property |
| 54 | BMCWEB_LOG_ERROR << "DBUS response error " << ec << "\n"; |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | const std::string *status = std::get_if<std::string>(&value); |
| 59 | |
| 60 | if (status == nullptr) |
| 61 | { |
| 62 | BMCWEB_LOG_ERROR << "intrusion status read error \n"; |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | aResp->res.jsonValue["PhysicalSecurity"] = { |
| 67 | {"IntrusionSensorNumber", 1}, {"IntrusionSensor", *status}}; |
| 68 | }, |
| 69 | service, objPath, "org.freedesktop.DBus.Properties", "Get", |
| 70 | "xyz.openbmc_project.Chassis.Intrusion", "Status"); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Retrieves physical security properties over dbus |
| 75 | */ |
| 76 | void getPhysicalSecurityData(std::shared_ptr<AsyncResp> aResp) |
| 77 | { |
| 78 | crow::connections::systemBus->async_method_call( |
| 79 | [aResp{std::move(aResp)}]( |
| 80 | const boost::system::error_code ec, |
| 81 | const std::vector<std::pair< |
| 82 | std::string, |
| 83 | std::vector<std::pair<std::string, std::vector<std::string>>>>> |
| 84 | &subtree) { |
| 85 | if (ec) |
| 86 | { |
| 87 | // do not add err msg in redfish response, becaues this is not |
| 88 | // mandatory property |
| 89 | BMCWEB_LOG_ERROR << "DBUS error: no matched iface " << ec |
| 90 | << "\n"; |
| 91 | return; |
| 92 | } |
| 93 | // Iterate over all retrieved ObjectPaths. |
| 94 | for (const auto &object : subtree) |
| 95 | { |
| 96 | for (const auto &service : object.second) |
| 97 | { |
| 98 | getIntrusionByService(aResp, service.first, object.first); |
| 99 | return; |
| 100 | } |
| 101 | } |
| 102 | }, |
| 103 | "xyz.openbmc_project.ObjectMapper", |
| 104 | "/xyz/openbmc_project/object_mapper", |
| 105 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", |
| 106 | "/xyz/openbmc_project/Intrusion", int32_t(1), |
| 107 | std::array<const char *, 1>{"xyz.openbmc_project.Chassis.Intrusion"}); |
| 108 | } |
| 109 | |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 110 | /** |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 111 | * ChassisCollection derived class for delivering Chassis Collection Schema |
| 112 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 113 | class ChassisCollection : public Node |
| 114 | { |
| 115 | public: |
| 116 | ChassisCollection(CrowApp &app) : Node(app, "/redfish/v1/Chassis/") |
| 117 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 118 | entityPrivileges = { |
| 119 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 120 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 121 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 122 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 123 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 124 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
| 125 | } |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 126 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 127 | private: |
| 128 | /** |
| 129 | * Functions triggers appropriate requests on DBus |
| 130 | */ |
| 131 | void doGet(crow::Response &res, const crow::Request &req, |
| 132 | const std::vector<std::string> ¶ms) override |
| 133 | { |
Gunnar Mills | 8f1ac8e | 2018-12-06 15:52:46 -0600 | [diff] [blame] | 134 | const std::array<const char *, 3> interfaces = { |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 135 | "xyz.openbmc_project.Inventory.Item.Board", |
| 136 | "xyz.openbmc_project.Inventory.Item.Chassis", |
Gunnar Mills | 8f1ac8e | 2018-12-06 15:52:46 -0600 | [diff] [blame] | 137 | "xyz.openbmc_project.Inventory.Item.PowerSupply"}; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 138 | res.jsonValue["@odata.type"] = "#ChassisCollection.ChassisCollection"; |
| 139 | res.jsonValue["@odata.id"] = "/redfish/v1/Chassis"; |
| 140 | res.jsonValue["@odata.context"] = |
| 141 | "/redfish/v1/$metadata#ChassisCollection.ChassisCollection"; |
| 142 | res.jsonValue["Name"] = "Chassis Collection"; |
| 143 | |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 144 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 145 | crow::connections::systemBus->async_method_call( |
| 146 | [asyncResp](const boost::system::error_code ec, |
| 147 | const std::vector<std::string> &chassisList) { |
| 148 | if (ec) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 149 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 150 | messages::internalError(asyncResp->res); |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 151 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 152 | } |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 153 | nlohmann::json &chassisArray = |
| 154 | asyncResp->res.jsonValue["Members"]; |
| 155 | chassisArray = nlohmann::json::array(); |
| 156 | for (const std::string &objpath : chassisList) |
| 157 | { |
| 158 | std::size_t lastPos = objpath.rfind("/"); |
| 159 | if (lastPos == std::string::npos) |
| 160 | { |
| 161 | BMCWEB_LOG_ERROR << "Failed to find '/' in " << objpath; |
| 162 | continue; |
| 163 | } |
| 164 | chassisArray.push_back( |
| 165 | {{"@odata.id", "/redfish/v1/Chassis/" + |
| 166 | objpath.substr(lastPos + 1)}}); |
| 167 | } |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 168 | |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 169 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 170 | chassisArray.size(); |
| 171 | }, |
| 172 | "xyz.openbmc_project.ObjectMapper", |
| 173 | "/xyz/openbmc_project/object_mapper", |
| 174 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", |
Gunnar Mills | 734bfe9 | 2019-01-21 16:33:50 -0600 | [diff] [blame] | 175 | "/xyz/openbmc_project/inventory", int32_t(0), interfaces); |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 176 | } |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | /** |
| 180 | * Chassis override class for delivering Chassis Schema |
| 181 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 182 | class Chassis : public Node |
| 183 | { |
| 184 | public: |
| 185 | Chassis(CrowApp &app) : |
| 186 | Node(app, "/redfish/v1/Chassis/<str>/", std::string()) |
| 187 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 188 | entityPrivileges = { |
| 189 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 190 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 191 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 192 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 193 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 194 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 195 | } |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 196 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 197 | private: |
| 198 | /** |
| 199 | * Functions triggers appropriate requests on DBus |
| 200 | */ |
| 201 | void doGet(crow::Response &res, const crow::Request &req, |
| 202 | const std::vector<std::string> ¶ms) override |
| 203 | { |
Gunnar Mills | 734bfe9 | 2019-01-21 16:33:50 -0600 | [diff] [blame] | 204 | const std::array<const char *, 3> interfaces = { |
| 205 | "xyz.openbmc_project.Inventory.Item.Board", |
| 206 | "xyz.openbmc_project.Inventory.Item.Chassis", |
| 207 | "xyz.openbmc_project.Inventory.Item.PowerSupply"}; |
| 208 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 209 | // Check if there is required param, truly entering this shall be |
| 210 | // impossible. |
| 211 | if (params.size() != 1) |
| 212 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 213 | messages::internalError(res); |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 214 | res.end(); |
| 215 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 216 | } |
Shawn McCarney | 99cffd7 | 2019-03-01 10:46:20 -0600 | [diff] [blame] | 217 | const std::string &chassisId = params[0]; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 218 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 219 | res.jsonValue["@odata.type"] = "#Chassis.v1_4_0.Chassis"; |
Shawn McCarney | 99cffd7 | 2019-03-01 10:46:20 -0600 | [diff] [blame] | 220 | res.jsonValue["@odata.id"] = "/redfish/v1/Chassis/" + chassisId; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 221 | res.jsonValue["@odata.context"] = |
| 222 | "/redfish/v1/$metadata#Chassis.Chassis"; |
| 223 | res.jsonValue["Name"] = "Chassis Collection"; |
| 224 | res.jsonValue["ChassisType"] = "RackMount"; |
| 225 | res.jsonValue["PowerState"] = "On"; |
| 226 | |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 227 | auto asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 228 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 229 | [asyncResp, chassisId(std::string(chassisId))]( |
| 230 | const boost::system::error_code ec, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 231 | const std::vector<std::pair< |
| 232 | std::string, std::vector<std::pair< |
| 233 | std::string, std::vector<std::string>>>>> |
| 234 | &subtree) { |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 235 | if (ec) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 236 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 237 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 238 | return; |
| 239 | } |
| 240 | // Iterate over all retrieved ObjectPaths. |
| 241 | for (const std::pair< |
| 242 | std::string, |
| 243 | std::vector< |
| 244 | std::pair<std::string, std::vector<std::string>>>> |
| 245 | &object : subtree) |
| 246 | { |
| 247 | const std::string &path = object.first; |
| 248 | const std::vector< |
| 249 | std::pair<std::string, std::vector<std::string>>> |
| 250 | &connectionNames = object.second; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 251 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 252 | if (!boost::ends_with(path, chassisId)) |
| 253 | { |
| 254 | continue; |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 255 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 256 | if (connectionNames.size() < 1) |
| 257 | { |
| 258 | BMCWEB_LOG_ERROR << "Only got " |
| 259 | << connectionNames.size() |
| 260 | << " Connection names"; |
| 261 | continue; |
| 262 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 263 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 264 | const std::string connectionName = connectionNames[0].first; |
| 265 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 266 | [asyncResp, chassisId(std::string(chassisId))]( |
| 267 | const boost::system::error_code ec, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 268 | const std::vector<std::pair< |
| 269 | std::string, VariantType>> &propertiesList) { |
| 270 | for (const std::pair<std::string, VariantType> |
| 271 | &property : propertiesList) |
| 272 | { |
Shawn McCarney | 99cffd7 | 2019-03-01 10:46:20 -0600 | [diff] [blame] | 273 | // Store DBus properties that are also Redfish |
| 274 | // properties with same name and a string value |
| 275 | const std::string &propertyName = |
| 276 | property.first; |
| 277 | if ((propertyName == "PartNumber") || |
| 278 | (propertyName == "SerialNumber") || |
| 279 | (propertyName == "Manufacturer") || |
| 280 | (propertyName == "Model")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 281 | { |
Shawn McCarney | 99cffd7 | 2019-03-01 10:46:20 -0600 | [diff] [blame] | 282 | const std::string *value = |
| 283 | std::get_if<std::string>( |
| 284 | &property.second); |
| 285 | if (value != nullptr) |
| 286 | { |
| 287 | asyncResp->res.jsonValue[propertyName] = |
| 288 | *value; |
| 289 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 292 | asyncResp->res.jsonValue["Name"] = chassisId; |
| 293 | asyncResp->res.jsonValue["Id"] = chassisId; |
| 294 | asyncResp->res.jsonValue["Thermal"] = { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 295 | {"@odata.id", "/redfish/v1/Chassis/" + |
| 296 | chassisId + "/Thermal"}}; |
Ed Tanous | 2474adf | 2018-09-05 16:31:16 -0700 | [diff] [blame] | 297 | // Power object |
| 298 | asyncResp->res.jsonValue["Power"] = { |
| 299 | {"@odata.id", "/redfish/v1/Chassis/" + |
| 300 | chassisId + "/Power"}}; |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 301 | asyncResp->res.jsonValue["Status"] = { |
| 302 | {"Health", "OK"}, |
| 303 | {"State", "Enabled"}, |
| 304 | }; |
Ed Tanous | 2474adf | 2018-09-05 16:31:16 -0700 | [diff] [blame] | 305 | |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 306 | asyncResp->res |
| 307 | .jsonValue["Links"]["ComputerSystems"] = { |
| 308 | {{"@odata.id", "/redfish/v1/Systems/system"}}}; |
| 309 | asyncResp->res.jsonValue["Links"]["ManagedBy"] = { |
| 310 | {{"@odata.id", "/redfish/v1/Managers/bmc"}}}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 311 | }, |
| 312 | connectionName, path, "org.freedesktop.DBus.Properties", |
| 313 | "GetAll", |
| 314 | "xyz.openbmc_project.Inventory.Decorator.Asset"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 315 | return; |
| 316 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 317 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 318 | // Couldn't find an object with that name. return an error |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 319 | messages::resourceNotFound( |
| 320 | asyncResp->res, "#Chassis.v1_4_0.Chassis", chassisId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 321 | }, |
| 322 | "xyz.openbmc_project.ObjectMapper", |
| 323 | "/xyz/openbmc_project/object_mapper", |
| 324 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", |
Gunnar Mills | 734bfe9 | 2019-01-21 16:33:50 -0600 | [diff] [blame] | 325 | "/xyz/openbmc_project/inventory", int32_t(0), interfaces); |
Qiang XU | c181942 | 2019-02-27 13:51:32 +0800 | [diff] [blame^] | 326 | |
| 327 | getPhysicalSecurityData(asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 328 | } |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 329 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 330 | } // namespace redfish |