blob: a33c8e01e5cb2c6a5e1a1d4e524c68573ae16530 [file] [log] [blame]
Borawski.Lukasz86e1b662018-01-19 14:22:14 +01001/*
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.Lukasz86e1b662018-01-19 14:22:14 +010018#include "privileges.hpp"
19#include "token_authorization_middleware.hpp"
Borawski.Lukaszc1a46bd2018-02-08 13:31:59 +010020#include "webserver_common.hpp"
Borawski.Lukaszb6df6dc2018-01-24 10:20:45 +010021#include "crow.h"
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010022
23namespace redfish {
24
25/**
26 * @brief Abstract class used for implementing Redfish nodes.
27 *
28 */
29class Node {
30 public:
Borawski.Lukaszc1a46bd2018-02-08 13:31:59 +010031 template <typename... Params>
Ed Tanous3ebd75f2018-03-05 18:20:01 -080032 Node(CrowApp& app, std::string&& entityUrl, Params... params) {
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010033 app.route_dynamic(entityUrl.c_str())
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010034 .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.Lukaszaecb47a2018-01-25 12:14:14 +010042 virtual ~Node() = default;
43
Borawski.Lukaszc1a46bd2018-02-08 13:31:59 +010044 std::string getUrl() const {
45 auto odataId = json.find("@odata.id");
46 if (odataId != json.end() && odataId->is_string()) {
47 return odataId->get<std::string>();
48 }
49 return std::string();
50 }
51
52 /**
53 * @brief Inserts subroute fields into for the node's json in the form:
54 * "subroute_name" : { "odata.id": "node_url/subroute_name/" }
55 * Excludes metadata urls starting with "$" and child urls having
56 * more than one level.
57 *
58 * @return None
59 */
60 void getSubRoutes(const std::vector<std::unique_ptr<Node>>& allNodes) {
61 std::string url = getUrl();
62
63 for (const auto& node : allNodes) {
64 auto route = node->getUrl();
65
66 if (boost::starts_with(route, url)) {
67 auto subRoute = route.substr(url.size());
68 if (subRoute.empty()) {
69 continue;
70 }
71
72 if (subRoute.at(0) == '/') {
73 subRoute = subRoute.substr(1);
74 }
75
76 if (subRoute.at(subRoute.size() - 1) == '/') {
77 subRoute = subRoute.substr(0, subRoute.size() - 1);
78 }
79
80 if (subRoute[0] != '$' && subRoute.find('/') == std::string::npos) {
81 json[subRoute] = nlohmann::json{{"@odata.id", route}};
82 }
83 }
84 }
85 }
86
Ed Tanous3ebd75f2018-03-05 18:20:01 -080087 OperationMap entityPrivileges;
88
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010089 protected:
90 // Node is designed to be an abstract class, so doGet is pure virtual
91 virtual void doGet(crow::response& res, const crow::request& req,
92 const std::vector<std::string>& params) = 0;
93
94 virtual void doPatch(crow::response& res, const crow::request& req,
95 const std::vector<std::string>& params) {
96 res.code = static_cast<int>(HttpRespCode::METHOD_NOT_ALLOWED);
97 res.end();
98 }
99
100 virtual void doPost(crow::response& res, const crow::request& req,
101 const std::vector<std::string>& params) {
102 res.code = static_cast<int>(HttpRespCode::METHOD_NOT_ALLOWED);
103 res.end();
104 }
105
106 virtual void doDelete(crow::response& res, const crow::request& req,
107 const std::vector<std::string>& params) {
108 res.code = static_cast<int>(HttpRespCode::METHOD_NOT_ALLOWED);
109 res.end();
110 }
111
Borawski.Lukaszc1a46bd2018-02-08 13:31:59 +0100112 nlohmann::json json;
113
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100114 private:
Borawski.Lukasz86e1b662018-01-19 14:22:14 +0100115 void dispatchRequest(CrowApp& app, const crow::request& req,
116 crow::response& res,
117 const std::vector<std::string>& params) {
Borawski.Lukasz86e1b662018-01-19 14:22:14 +0100118 auto ctx =
119 app.template get_context<crow::TokenAuthorization::Middleware>(req);
120
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800121 if (!isMethodAllowedForUser(req.method, entityPrivileges,
122 ctx.session->username)) {
Borawski.Lukasz86e1b662018-01-19 14:22:14 +0100123 res.code = static_cast<int>(HttpRespCode::METHOD_NOT_ALLOWED);
124 res.end();
125 return;
126 }
127
128 switch (req.method) {
129 case "GET"_method:
130 doGet(res, req, params);
131 break;
132
133 case "PATCH"_method:
134 doPatch(res, req, params);
135 break;
136
137 case "POST"_method:
138 doPost(res, req, params);
139 break;
140
141 case "DELETE"_method:
142 doDelete(res, req, params);
143 break;
144
145 default:
146 res.code = static_cast<int>(HttpRespCode::NOT_FOUND);
147 res.end();
148 }
149 return;
150 }
Borawski.Lukasz86e1b662018-01-19 14:22:14 +0100151};
152
Borawski.Lukasz86e1b662018-01-19 14:22:14 +0100153} // namespace redfish