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