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> |
| 21 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 22 | namespace redfish |
| 23 | { |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * DBus types primitives for several generic DBus interfaces |
| 27 | * TODO(Pawel) consider move this to separate file into boost::dbus |
| 28 | */ |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 29 | // 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] | 30 | // values, it should be as simple as possible |
| 31 | // TODO(ed) invent a nullvariant type |
Ed Tanous | 04a258f | 2018-10-15 08:00:41 -0700 | [diff] [blame] | 32 | using VariantType = sdbusplus::message::variant<bool, std::string, uint64_t>; |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 33 | using ManagedObjectsType = std::vector<std::pair< |
| 34 | sdbusplus::message::object_path, |
| 35 | std::vector<std::pair<std::string, |
| 36 | std::vector<std::pair<std::string, VariantType>>>>>>; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 37 | |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 38 | using PropertiesType = boost::container::flat_map<std::string, VariantType>; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 39 | |
| 40 | /** |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 41 | * ChassisCollection derived class for delivering Chassis Collection Schema |
| 42 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 43 | class ChassisCollection : public Node |
| 44 | { |
| 45 | public: |
| 46 | ChassisCollection(CrowApp &app) : Node(app, "/redfish/v1/Chassis/") |
| 47 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 48 | entityPrivileges = { |
| 49 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 50 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 51 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 52 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 53 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 54 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
| 55 | } |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 56 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 57 | private: |
| 58 | /** |
| 59 | * Functions triggers appropriate requests on DBus |
| 60 | */ |
| 61 | void doGet(crow::Response &res, const crow::Request &req, |
| 62 | const std::vector<std::string> ¶ms) override |
| 63 | { |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 64 | const std::array<const char *, 4> interfaces = { |
| 65 | "xyz.openbmc_project.Inventory.Item.Board", |
| 66 | "xyz.openbmc_project.Inventory.Item.Chassis", |
| 67 | "xyz.openbmc_project.Inventory.Item.PowerSupply", |
| 68 | "xyz.openbmc_project.Inventory.Item.System", |
| 69 | }; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 70 | res.jsonValue["@odata.type"] = "#ChassisCollection.ChassisCollection"; |
| 71 | res.jsonValue["@odata.id"] = "/redfish/v1/Chassis"; |
| 72 | res.jsonValue["@odata.context"] = |
| 73 | "/redfish/v1/$metadata#ChassisCollection.ChassisCollection"; |
| 74 | res.jsonValue["Name"] = "Chassis Collection"; |
| 75 | |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 76 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 77 | crow::connections::systemBus->async_method_call( |
| 78 | [asyncResp](const boost::system::error_code ec, |
| 79 | const std::vector<std::string> &chassisList) { |
| 80 | if (ec) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 81 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 82 | messages::internalError(asyncResp->res); |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 83 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 84 | } |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 85 | nlohmann::json &chassisArray = |
| 86 | asyncResp->res.jsonValue["Members"]; |
| 87 | chassisArray = nlohmann::json::array(); |
| 88 | for (const std::string &objpath : chassisList) |
| 89 | { |
| 90 | std::size_t lastPos = objpath.rfind("/"); |
| 91 | if (lastPos == std::string::npos) |
| 92 | { |
| 93 | BMCWEB_LOG_ERROR << "Failed to find '/' in " << objpath; |
| 94 | continue; |
| 95 | } |
| 96 | chassisArray.push_back( |
| 97 | {{"@odata.id", "/redfish/v1/Chassis/" + |
| 98 | objpath.substr(lastPos + 1)}}); |
| 99 | } |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 100 | |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 101 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 102 | chassisArray.size(); |
| 103 | }, |
| 104 | "xyz.openbmc_project.ObjectMapper", |
| 105 | "/xyz/openbmc_project/object_mapper", |
| 106 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", |
| 107 | "/xyz/openbmc_project/inventory", int32_t(3), interfaces); |
| 108 | } |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | /** |
| 112 | * Chassis override class for delivering Chassis Schema |
| 113 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | class Chassis : public Node |
| 115 | { |
| 116 | public: |
| 117 | Chassis(CrowApp &app) : |
| 118 | Node(app, "/redfish/v1/Chassis/<str>/", std::string()) |
| 119 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 120 | entityPrivileges = { |
| 121 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 122 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 123 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 124 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 125 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 126 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 127 | } |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 128 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 129 | private: |
| 130 | /** |
| 131 | * Functions triggers appropriate requests on DBus |
| 132 | */ |
| 133 | void doGet(crow::Response &res, const crow::Request &req, |
| 134 | const std::vector<std::string> ¶ms) override |
| 135 | { |
| 136 | // Check if there is required param, truly entering this shall be |
| 137 | // impossible. |
| 138 | if (params.size() != 1) |
| 139 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 140 | messages::internalError(res); |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 141 | res.end(); |
| 142 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 143 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 144 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 145 | res.jsonValue["@odata.type"] = "#Chassis.v1_4_0.Chassis"; |
| 146 | res.jsonValue["@odata.id"] = "/redfish/v1/Chassis"; |
| 147 | res.jsonValue["@odata.context"] = |
| 148 | "/redfish/v1/$metadata#Chassis.Chassis"; |
| 149 | res.jsonValue["Name"] = "Chassis Collection"; |
| 150 | res.jsonValue["ChassisType"] = "RackMount"; |
| 151 | res.jsonValue["PowerState"] = "On"; |
| 152 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 153 | const std::string &chassisId = params[0]; |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 154 | auto asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 155 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 156 | [asyncResp, chassisId(std::string(chassisId))]( |
| 157 | const boost::system::error_code ec, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 158 | const std::vector<std::pair< |
| 159 | std::string, std::vector<std::pair< |
| 160 | std::string, std::vector<std::string>>>>> |
| 161 | &subtree) { |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 162 | if (ec) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 163 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 164 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | // Iterate over all retrieved ObjectPaths. |
| 168 | for (const std::pair< |
| 169 | std::string, |
| 170 | std::vector< |
| 171 | std::pair<std::string, std::vector<std::string>>>> |
| 172 | &object : subtree) |
| 173 | { |
| 174 | const std::string &path = object.first; |
| 175 | const std::vector< |
| 176 | std::pair<std::string, std::vector<std::string>>> |
| 177 | &connectionNames = object.second; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 178 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 179 | if (!boost::ends_with(path, chassisId)) |
| 180 | { |
| 181 | continue; |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 182 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 183 | if (connectionNames.size() < 1) |
| 184 | { |
| 185 | BMCWEB_LOG_ERROR << "Only got " |
| 186 | << connectionNames.size() |
| 187 | << " Connection names"; |
| 188 | continue; |
| 189 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 190 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 191 | const std::string connectionName = connectionNames[0].first; |
| 192 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 193 | [asyncResp, chassisId(std::string(chassisId))]( |
| 194 | const boost::system::error_code ec, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 195 | const std::vector<std::pair< |
| 196 | std::string, VariantType>> &propertiesList) { |
| 197 | for (const std::pair<std::string, VariantType> |
| 198 | &property : propertiesList) |
| 199 | { |
| 200 | const std::string *value = |
| 201 | mapbox::getPtr<const std::string>( |
| 202 | property.second); |
| 203 | if (value != nullptr) |
| 204 | { |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 205 | asyncResp->res.jsonValue[property.first] = |
| 206 | *value; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 209 | asyncResp->res.jsonValue["Name"] = chassisId; |
| 210 | asyncResp->res.jsonValue["Id"] = chassisId; |
| 211 | asyncResp->res.jsonValue["Thermal"] = { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 212 | {"@odata.id", "/redfish/v1/Chassis/" + |
| 213 | chassisId + "/Thermal"}}; |
Ed Tanous | 2474adf | 2018-09-05 16:31:16 -0700 | [diff] [blame] | 214 | // Power object |
| 215 | asyncResp->res.jsonValue["Power"] = { |
| 216 | {"@odata.id", "/redfish/v1/Chassis/" + |
| 217 | chassisId + "/Power"}}; |
| 218 | |
| 219 | // TODO: An array of references to computer systems |
| 220 | // contained in this chassis. |
| 221 | // res.jsonValue["Links"]["ComputerSystems"] |
| 222 | // = |
| 223 | // {{{"@odata.id", |
| 224 | // "/redfish/v1/Systems/1"}}}; |
| 225 | // An array of references to the Managers |
| 226 | // responsible for managing this chassis. |
| 227 | // res.jsonValue["Links"]["ManagedBy"] = |
| 228 | // {{{"@odata.id", |
| 229 | // "/redfish/v1/Managers/1"}}}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 230 | }, |
| 231 | connectionName, path, "org.freedesktop.DBus.Properties", |
| 232 | "GetAll", |
| 233 | "xyz.openbmc_project.Inventory.Decorator.Asset"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 234 | return; |
| 235 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 236 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 237 | // Couldn't find an object with that name. return an error |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 238 | messages::resourceNotFound( |
| 239 | asyncResp->res, "#Chassis.v1_4_0.Chassis", chassisId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 240 | }, |
| 241 | "xyz.openbmc_project.ObjectMapper", |
| 242 | "/xyz/openbmc_project/object_mapper", |
| 243 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", |
| 244 | "/xyz/openbmc_project/inventory", int32_t(0), |
| 245 | std::array<const char *, 1>{ |
| 246 | "xyz.openbmc_project.Inventory.Decorator.Asset"}); |
| 247 | } |
Ed Tanous | 62d5e2e | 2018-09-05 16:17:25 -0700 | [diff] [blame] | 248 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 249 | } // namespace redfish |