blob: 8e94a6f0117eec998d8d660de7df71fdf035e7c2 [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"
Ed Tanous1abe55e2018-09-05 08:30:59 -070021
Ed Tanousa0803ef2018-08-29 13:29:23 -070022#include <error_messages.hpp>
Ed Tanous770841b2019-02-27 10:25:45 -080023#include <vector>
Ed Tanousa0803ef2018-08-29 13:29:23 -070024
Ed Tanous770841b2019-02-27 10:25:45 -080025#include "crow/http_request.h"
26#include "crow/http_response.h"
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028namespace redfish
29{
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010030
31/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020032 * AsyncResp
33 * Gathers data needed for response processing after async calls are done
34 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070035class AsyncResp
36{
37 public:
38 AsyncResp(crow::Response& response) : res(response)
39 {
40 }
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020041
Ed Tanous1abe55e2018-09-05 08:30:59 -070042 ~AsyncResp()
43 {
44 res.end();
45 }
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020046
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 crow::Response& res;
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020048};
49
50/**
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010051 * @brief Abstract class used for implementing Redfish nodes.
52 *
53 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070054class Node
55{
56 public:
57 template <typename... Params>
58 Node(CrowApp& app, std::string&& entityUrl, Params... params)
59 {
60 app.routeDynamic(entityUrl.c_str())
61 .methods("GET"_method, "PATCH"_method, "POST"_method,
62 "DELETE"_method)([&](const crow::Request& req,
63 crow::Response& res,
64 Params... params) {
65 std::vector<std::string> paramVec = {params...};
66 dispatchRequest(app, req, res, paramVec);
67 });
Borawski.Lukaszc1a46bd2018-02-08 13:31:59 +010068 }
Ed Tanouscbbfa962018-03-13 16:46:28 -070069
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 virtual ~Node() = default;
Borawski.Lukaszc1a46bd2018-02-08 13:31:59 +010071
Ed Tanous1abe55e2018-09-05 08:30:59 -070072 OperationMap entityPrivileges;
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010073
Ed Tanous1abe55e2018-09-05 08:30:59 -070074 protected:
75 // Node is designed to be an abstract class, so doGet is pure virtual
76 virtual void doGet(crow::Response& res, const crow::Request& req,
77 const std::vector<std::string>& params)
78 {
79 res.result(boost::beast::http::status::method_not_allowed);
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010080 res.end();
81 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070082
83 virtual void doPatch(crow::Response& res, const crow::Request& req,
84 const std::vector<std::string>& params)
85 {
86 res.result(boost::beast::http::status::method_not_allowed);
87 res.end();
88 }
89
90 virtual void doPost(crow::Response& res, const crow::Request& req,
91 const std::vector<std::string>& params)
92 {
93 res.result(boost::beast::http::status::method_not_allowed);
94 res.end();
95 }
96
97 virtual void doDelete(crow::Response& res, const crow::Request& req,
98 const std::vector<std::string>& params)
99 {
100 res.result(boost::beast::http::status::method_not_allowed);
101 res.end();
102 }
103
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104 private:
105 void dispatchRequest(CrowApp& app, const crow::Request& req,
106 crow::Response& res,
107 const std::vector<std::string>& params)
108 {
109 auto ctx =
110 app.template getContext<crow::token_authorization::Middleware>(req);
111
112 if (!isMethodAllowedForUser(req.method(), entityPrivileges,
113 ctx.session->username))
114 {
115 res.result(boost::beast::http::status::method_not_allowed);
116 res.end();
117 return;
118 }
119
120 switch (req.method())
121 {
122 case "GET"_method:
123 doGet(res, req, params);
124 break;
125
126 case "PATCH"_method:
127 doPatch(res, req, params);
128 break;
129
130 case "POST"_method:
131 doPost(res, req, params);
132 break;
133
134 case "DELETE"_method:
135 doDelete(res, req, params);
136 break;
137
138 default:
139 res.result(boost::beast::http::status::not_found);
140 res.end();
141 }
142 return;
143 }
Borawski.Lukasz86e1b662018-01-19 14:22:14 +0100144};
145
Ed Tanous1abe55e2018-09-05 08:30:59 -0700146} // namespace redfish