blob: 049393fd7f590298678635e3d1e29e01c573bff9 [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>
23
Borawski.Lukaszb6df6dc2018-01-24 10:20:45 +010024#include "crow.h"
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010025
Ed Tanous1abe55e2018-09-05 08:30:59 -070026namespace redfish
27{
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010028
29/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020030 * AsyncResp
31 * Gathers data needed for response processing after async calls are done
32 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070033class AsyncResp
34{
35 public:
36 AsyncResp(crow::Response& response) : res(response)
37 {
38 }
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020039
Ed Tanous1abe55e2018-09-05 08:30:59 -070040 ~AsyncResp()
41 {
42 res.end();
43 }
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020044
Ed Tanous1abe55e2018-09-05 08:30:59 -070045 crow::Response& res;
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020046};
47
48/**
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010049 * @brief Abstract class used for implementing Redfish nodes.
50 *
51 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070052class Node
53{
54 public:
55 template <typename... Params>
56 Node(CrowApp& app, std::string&& entityUrl, Params... params)
57 {
58 app.routeDynamic(entityUrl.c_str())
59 .methods("GET"_method, "PATCH"_method, "POST"_method,
60 "DELETE"_method)([&](const crow::Request& req,
61 crow::Response& res,
62 Params... params) {
63 std::vector<std::string> paramVec = {params...};
64 dispatchRequest(app, req, res, paramVec);
65 });
Borawski.Lukaszc1a46bd2018-02-08 13:31:59 +010066 }
Ed Tanouscbbfa962018-03-13 16:46:28 -070067
Ed Tanous1abe55e2018-09-05 08:30:59 -070068 virtual ~Node() = default;
Borawski.Lukaszc1a46bd2018-02-08 13:31:59 +010069
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 OperationMap entityPrivileges;
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010071
Ed Tanous1abe55e2018-09-05 08:30:59 -070072 protected:
73 // Node is designed to be an abstract class, so doGet is pure virtual
74 virtual void doGet(crow::Response& res, const crow::Request& req,
75 const std::vector<std::string>& params)
76 {
77 res.result(boost::beast::http::status::method_not_allowed);
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010078 res.end();
79 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070080
81 virtual void doPatch(crow::Response& res, const crow::Request& req,
82 const std::vector<std::string>& params)
83 {
84 res.result(boost::beast::http::status::method_not_allowed);
85 res.end();
86 }
87
88 virtual void doPost(crow::Response& res, const crow::Request& req,
89 const std::vector<std::string>& params)
90 {
91 res.result(boost::beast::http::status::method_not_allowed);
92 res.end();
93 }
94
95 virtual void doDelete(crow::Response& res, const crow::Request& req,
96 const std::vector<std::string>& params)
97 {
98 res.result(boost::beast::http::status::method_not_allowed);
99 res.end();
100 }
101
Ed Tanous1abe55e2018-09-05 08:30:59 -0700102 private:
103 void dispatchRequest(CrowApp& app, const crow::Request& req,
104 crow::Response& res,
105 const std::vector<std::string>& params)
106 {
107 auto ctx =
108 app.template getContext<crow::token_authorization::Middleware>(req);
109
110 if (!isMethodAllowedForUser(req.method(), entityPrivileges,
111 ctx.session->username))
112 {
113 res.result(boost::beast::http::status::method_not_allowed);
114 res.end();
115 return;
116 }
117
118 switch (req.method())
119 {
120 case "GET"_method:
121 doGet(res, req, params);
122 break;
123
124 case "PATCH"_method:
125 doPatch(res, req, params);
126 break;
127
128 case "POST"_method:
129 doPost(res, req, params);
130 break;
131
132 case "DELETE"_method:
133 doDelete(res, req, params);
134 break;
135
136 default:
137 res.result(boost::beast::http::status::not_found);
138 res.end();
139 }
140 return;
141 }
Borawski.Lukasz86e1b662018-01-19 14:22:14 +0100142};
143
Ed Tanous1abe55e2018-09-05 08:30:59 -0700144} // namespace redfish