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