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" |
| 19 | #include <boost/container/flat_map.hpp> |
| 20 | |
| 21 | namespace redfish { |
| 22 | |
| 23 | /** |
| 24 | * DBus types primitives for several generic DBus interfaces |
| 25 | * TODO(Pawel) consider move this to separate file into boost::dbus |
| 26 | */ |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 27 | // 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] | 28 | // values, it should be as simple as possible |
| 29 | // TODO(ed) invent a nullvariant type |
Lewanczyk, Dawid | c5b2abe | 2018-05-30 16:59:42 +0200 | [diff] [blame] | 30 | using VariantType = sdbusplus::message::variant<bool, std::string>; |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 31 | using ManagedObjectsType = std::vector<std::pair< |
| 32 | sdbusplus::message::object_path, |
| 33 | std::vector<std::pair<std::string, |
| 34 | std::vector<std::pair<std::string, VariantType>>>>>>; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 35 | |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 36 | using PropertiesType = boost::container::flat_map<std::string, VariantType>; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * OnDemandChassisProvider |
Gunnar Mills | 274fad5 | 2018-06-13 15:45:36 -0500 | [diff] [blame] | 40 | * Chassis provider class that retrieves data directly from dbus, before setting |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 41 | * it into JSON output. This does not cache any data. |
| 42 | * |
| 43 | * Class can be a good example on how to scale different data providing |
| 44 | * solutions to produce single schema output. |
| 45 | * |
| 46 | * TODO(Pawel) |
| 47 | * This perhaps shall be different file, which has to be chosen on compile time |
| 48 | * depending on OEM needs |
| 49 | */ |
| 50 | class OnDemandChassisProvider { |
| 51 | public: |
| 52 | /** |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 53 | * Function that retrieves all Chassis available through EntityManager. |
| 54 | * @param callback a function that shall be called to convert Dbus output into |
| 55 | * JSON. |
| 56 | */ |
| 57 | template <typename CallbackFunc> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 58 | void getChassisList(CallbackFunc &&callback) { |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 59 | const std::array<const char *, 4> interfaces = { |
| 60 | "xyz.openbmc_project.Inventory.Item.Board", |
| 61 | "xyz.openbmc_project.Inventory.Item.Chassis", |
| 62 | "xyz.openbmc_project.Inventory.Item.PowerSupply", |
| 63 | "xyz.openbmc_project.Inventory.Item.System", |
| 64 | }; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 65 | crow::connections::systemBus->async_method_call( |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 66 | [callback{std::move(callback)}]( |
| 67 | const boost::system::error_code error_code, |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 68 | const std::vector<std::string> &resp) { |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 69 | // Callback requires vector<string> to retrieve all available chassis |
| 70 | // list. |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 71 | std::vector<std::string> chassisList; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 72 | if (error_code) { |
| 73 | // Something wrong on DBus, the error_code is not important at this |
| 74 | // moment, just return success=false, and empty output. Since size |
| 75 | // of vector may vary depending on information from Entity Manager, |
| 76 | // and empty output could not be treated same way as error. |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 77 | callback(false, chassisList); |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 78 | return; |
| 79 | } |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 80 | // Iterate over all retrieved ObjectPaths. |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 81 | for (const std::string &objpath : resp) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 82 | std::size_t lastPos = objpath.rfind("/"); |
| 83 | if (lastPos != std::string::npos) { |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 84 | // and put it into output vector. |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 85 | chassisList.emplace_back(objpath.substr(lastPos + 1)); |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 86 | } |
| 87 | } |
Ed Tanous | a434f2b | 2018-07-27 13:04:22 -0700 | [diff] [blame] | 88 | // Finally make a callback with useful data |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 89 | callback(true, chassisList); |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 90 | }, |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 91 | "xyz.openbmc_project.ObjectMapper", |
| 92 | "/xyz/openbmc_project/object_mapper", |
| 93 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", |
| 94 | "/xyz/openbmc_project/inventory", int32_t(3), interfaces); |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 95 | }; |
| 96 | }; |
| 97 | |
| 98 | /** |
| 99 | * ChassisCollection derived class for delivering Chassis Collection Schema |
| 100 | */ |
| 101 | class ChassisCollection : public Node { |
| 102 | public: |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 103 | ChassisCollection(CrowApp &app) : Node(app, "/redfish/v1/Chassis/") { |
| 104 | Node::json["@odata.type"] = "#ChassisCollection.ChassisCollection"; |
| 105 | Node::json["@odata.id"] = "/redfish/v1/Chassis"; |
| 106 | Node::json["@odata.context"] = |
| 107 | "/redfish/v1/$metadata#ChassisCollection.ChassisCollection"; |
| 108 | Node::json["Name"] = "Chassis Collection"; |
| 109 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 110 | entityPrivileges = { |
| 111 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 112 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 113 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 114 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 115 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 116 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | private: |
| 120 | /** |
| 121 | * Functions triggers appropriate requests on DBus |
| 122 | */ |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 123 | void doGet(crow::Response &res, const crow::Request &req, |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 124 | const std::vector<std::string> ¶ms) override { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 125 | // get chassis list, and call the below callback for JSON preparation |
| 126 | chassisProvider.getChassisList( |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 127 | [&](const bool &success, const std::vector<std::string> &output) { |
| 128 | if (success) { |
| 129 | // ... prepare json array with appropriate @odata.id links |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 130 | nlohmann::json chassisArray = nlohmann::json::array(); |
| 131 | for (const std::string &chassisItem : output) { |
| 132 | chassisArray.push_back( |
| 133 | {{"@odata.id", "/redfish/v1/Chassis/" + chassisItem}}); |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 134 | } |
| 135 | // Then attach members, count size and return, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 136 | Node::json["Members"] = chassisArray; |
| 137 | Node::json["Members@odata.count"] = chassisArray.size(); |
| 138 | res.jsonValue = Node::json; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 139 | } else { |
| 140 | // ... otherwise, return INTERNALL ERROR |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 141 | res.result(boost::beast::http::status::internal_server_error); |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 142 | } |
| 143 | res.end(); |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | // Chassis Provider object |
| 148 | // TODO(Pawel) consider move it to singleton |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 149 | OnDemandChassisProvider chassisProvider; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | /** |
| 153 | * Chassis override class for delivering Chassis Schema |
| 154 | */ |
| 155 | class Chassis : public Node { |
| 156 | public: |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 157 | Chassis(CrowApp &app) |
| 158 | : Node(app, "/redfish/v1/Chassis/<str>/", std::string()) { |
| 159 | Node::json["@odata.type"] = "#Chassis.v1_4_0.Chassis"; |
| 160 | Node::json["@odata.id"] = "/redfish/v1/Chassis"; |
| 161 | Node::json["@odata.context"] = "/redfish/v1/$metadata#Chassis.Chassis"; |
| 162 | Node::json["Name"] = "Chassis Collection"; |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 163 | Node::json["ChassisType"] = "RackMount"; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 164 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 165 | entityPrivileges = { |
| 166 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 167 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 168 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 169 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 170 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 171 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | private: |
| 175 | /** |
| 176 | * Functions triggers appropriate requests on DBus |
| 177 | */ |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 178 | void doGet(crow::Response &res, const crow::Request &req, |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 179 | const std::vector<std::string> ¶ms) override { |
| 180 | // Check if there is required param, truly entering this shall be |
| 181 | // impossible. |
| 182 | if (params.size() != 1) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 183 | res.result(boost::beast::http::status::internal_server_error); |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 184 | res.end(); |
| 185 | return; |
| 186 | } |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 187 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 188 | res.jsonValue = Node::json; |
| 189 | const std::string &chassisId = params[0]; |
| 190 | crow::connections::systemBus->async_method_call( |
| 191 | [&res, chassisId(std::string(chassisId)) ]( |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 192 | const boost::system::error_code error_code, |
| 193 | const std::vector<std::pair< |
| 194 | std::string, |
| 195 | std::vector<std::pair<std::string, std::vector<std::string>>>>> |
| 196 | &subtree) { |
| 197 | if (error_code) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 198 | res.jsonValue = {}; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 199 | res.result(boost::beast::http::status::internal_server_error); |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 200 | res.end(); |
| 201 | return; |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 202 | } |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 203 | // Iterate over all retrieved ObjectPaths. |
| 204 | for (const std::pair<std::string, |
| 205 | std::vector<std::pair<std::string, |
| 206 | std::vector<std::string>>>> |
| 207 | &object : subtree) { |
| 208 | const std::string &path = object.first; |
| 209 | const std::vector<std::pair<std::string, std::vector<std::string>>> |
| 210 | &connectionNames = object.second; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 211 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 212 | if (!boost::ends_with(path, chassisId)) { |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 213 | continue; |
| 214 | } |
| 215 | if (connectionNames.size() < 1) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 216 | BMCWEB_LOG_ERROR << "Only got " << connectionNames.size() |
| 217 | << " Connection names"; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 218 | continue; |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 219 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 220 | |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 221 | const std::string connectionName = connectionNames[0].first; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 222 | crow::connections::systemBus->async_method_call( |
| 223 | [&res, chassisId(std::string(chassisId)) ]( |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 224 | const boost::system::error_code error_code, |
| 225 | const std::vector<std::pair<std::string, VariantType>> |
| 226 | &propertiesList) { |
| 227 | for (const std::pair<std::string, VariantType> &property : |
| 228 | propertiesList) { |
| 229 | const std::string *value = |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 230 | mapbox::getPtr<const std::string>(property.second); |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 231 | if (value != nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 232 | res.jsonValue[property.first] = *value; |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 235 | res.jsonValue["Name"] = chassisId; |
| 236 | res.jsonValue["Id"] = chassisId; |
| 237 | res.jsonValue["Thermal"] = { |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 238 | {"@odata.id", |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 239 | "/redfish/v1/Chassis/" + chassisId + "/Thermal"}}; |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 240 | res.end(); |
| 241 | }, |
| 242 | connectionName, path, "org.freedesktop.DBus.Properties", |
| 243 | "GetAll", "xyz.openbmc_project.Inventory.Decorator.Asset"); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 244 | // Found the Connection we were looking for, return |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 245 | return; |
| 246 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 247 | |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 248 | // Couldn't find an object with that name. return an error |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 249 | res.result(boost::beast::http::status::not_found); |
| 250 | |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 251 | res.end(); |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 252 | }, |
| 253 | "xyz.openbmc_project.ObjectMapper", |
| 254 | "/xyz/openbmc_project/object_mapper", |
| 255 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", |
| 256 | "/xyz/openbmc_project/inventory", int32_t(0), |
| 257 | std::array<const char *, 1>{ |
| 258 | "xyz.openbmc_project.Inventory.Decorator.Asset"}); |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Chassis Provider object |
| 262 | // TODO(Pawel) consider move it to singleton |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 263 | OnDemandChassisProvider chassisProvider; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 264 | }; // namespace redfish |
Rapkiewicz, Pawel | e37f845 | 2018-03-09 13:49:50 +0100 | [diff] [blame] | 265 | |
| 266 | } // namespace redfish |