Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +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 | |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 18 | #include "privileges.hpp" |
| 19 | #include "token_authorization_middleware.hpp" |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 20 | #include "webserver_common.hpp" |
Borawski.Lukasz | b6df6dc | 2018-01-24 10:20:45 +0100 | [diff] [blame] | 21 | #include "crow.h" |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 22 | |
| 23 | namespace redfish { |
| 24 | |
| 25 | /** |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame^] | 26 | * AsyncResp |
| 27 | * Gathers data needed for response processing after async calls are done |
| 28 | */ |
| 29 | class AsyncResp { |
| 30 | public: |
| 31 | AsyncResp(crow::response& response) : res(response) {} |
| 32 | |
| 33 | ~AsyncResp() { res.end(); } |
| 34 | |
| 35 | crow::response& res; |
| 36 | }; |
| 37 | |
| 38 | /** |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 39 | * @brief Abstract class used for implementing Redfish nodes. |
| 40 | * |
| 41 | */ |
| 42 | class Node { |
| 43 | public: |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 44 | template <typename... Params> |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 45 | Node(CrowApp& app, std::string&& entityUrl, Params... params) { |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 46 | app.route_dynamic(entityUrl.c_str()) |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 47 | .methods("GET"_method, "PATCH"_method, "POST"_method, |
| 48 | "DELETE"_method)([&](const crow::request& req, |
| 49 | crow::response& res, Params... params) { |
| 50 | std::vector<std::string> paramVec = {params...}; |
| 51 | dispatchRequest(app, req, res, paramVec); |
| 52 | }); |
| 53 | } |
| 54 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 55 | virtual ~Node() = default; |
| 56 | |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 57 | const std::string* getUrl() const { |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 58 | auto odataId = json.find("@odata.id"); |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 59 | if (odataId == json.end()) { |
| 60 | return nullptr; |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 61 | } |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 62 | |
| 63 | return odataId->get_ptr<const std::string*>(); |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @brief Inserts subroute fields into for the node's json in the form: |
| 68 | * "subroute_name" : { "odata.id": "node_url/subroute_name/" } |
| 69 | * Excludes metadata urls starting with "$" and child urls having |
| 70 | * more than one level. |
| 71 | * |
| 72 | * @return None |
| 73 | */ |
| 74 | void getSubRoutes(const std::vector<std::unique_ptr<Node>>& allNodes) { |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 75 | const std::string* url = getUrl(); |
| 76 | if (url == nullptr) { |
| 77 | CROW_LOG_CRITICAL << "Unable to get url for route"; |
| 78 | return; |
| 79 | } |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 80 | |
| 81 | for (const auto& node : allNodes) { |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 82 | const std::string* route = node->getUrl(); |
| 83 | if (route == nullptr) { |
| 84 | CROW_LOG_CRITICAL << "Unable to get url for route"; |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 85 | continue; |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 86 | } |
| 87 | if (boost::starts_with(*route, *url)) { |
| 88 | std::string subRoute = route->substr(url->size()); |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 89 | if (subRoute.empty()) { |
| 90 | continue; |
| 91 | } |
| 92 | |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 93 | if (boost::starts_with(subRoute, "/")) { |
| 94 | subRoute.erase(0, 1); |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 97 | if (boost::ends_with(subRoute, "/")) { |
| 98 | subRoute.pop_back(); |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 101 | if (!boost::starts_with(subRoute, "$") && |
| 102 | subRoute.find('/') == std::string::npos) { |
| 103 | json[subRoute] = nlohmann::json{{"@odata.id", *route}}; |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 109 | OperationMap entityPrivileges; |
| 110 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 111 | protected: |
| 112 | // Node is designed to be an abstract class, so doGet is pure virtual |
| 113 | virtual void doGet(crow::response& res, const crow::request& req, |
| 114 | const std::vector<std::string>& params) = 0; |
| 115 | |
| 116 | virtual void doPatch(crow::response& res, const crow::request& req, |
| 117 | const std::vector<std::string>& params) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 118 | res.result(boost::beast::http::status::method_not_allowed); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 119 | res.end(); |
| 120 | } |
| 121 | |
| 122 | virtual void doPost(crow::response& res, const crow::request& req, |
| 123 | const std::vector<std::string>& params) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 124 | res.result(boost::beast::http::status::method_not_allowed); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 125 | res.end(); |
| 126 | } |
| 127 | |
| 128 | virtual void doDelete(crow::response& res, const crow::request& req, |
| 129 | const std::vector<std::string>& params) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 130 | res.result(boost::beast::http::status::method_not_allowed); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 131 | res.end(); |
| 132 | } |
| 133 | |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 134 | nlohmann::json json; |
| 135 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 136 | private: |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 137 | void dispatchRequest(CrowApp& app, const crow::request& req, |
| 138 | crow::response& res, |
| 139 | const std::vector<std::string>& params) { |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 140 | auto ctx = |
| 141 | app.template get_context<crow::TokenAuthorization::Middleware>(req); |
| 142 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 143 | if (!isMethodAllowedForUser(req.method(), entityPrivileges, |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 144 | ctx.session->username)) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 145 | res.result(boost::beast::http::status::method_not_allowed); |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 146 | res.end(); |
| 147 | return; |
| 148 | } |
| 149 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 150 | switch (req.method()) { |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 151 | case "GET"_method: |
| 152 | doGet(res, req, params); |
| 153 | break; |
| 154 | |
| 155 | case "PATCH"_method: |
| 156 | doPatch(res, req, params); |
| 157 | break; |
| 158 | |
| 159 | case "POST"_method: |
| 160 | doPost(res, req, params); |
| 161 | break; |
| 162 | |
| 163 | case "DELETE"_method: |
| 164 | doDelete(res, req, params); |
| 165 | break; |
| 166 | |
| 167 | default: |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 168 | res.result(boost::beast::http::status::not_found); |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 169 | res.end(); |
| 170 | } |
| 171 | return; |
| 172 | } |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 173 | }; |
| 174 | |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 175 | } // namespace redfish |