blob: b7a1f4cb214748299ea1f4ba63967e2535f5cf47 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "app.hpp"
Ed Tanous81d523a2022-05-25 12:00:51 -07004#include "error_messages.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "http_request.hpp"
6#include "http_response.hpp"
7#include "query.hpp"
8#include "registries/privilege_registry.hpp"
9#include "schemas.hpp"
Ed Tanous81d523a2022-05-25 12:00:51 -070010#include "utility.hpp"
11
Ed Tanousef4c65b2023-04-24 15:28:50 -070012#include <boost/url/format.hpp>
13
Ed Tanousa8c4ce92021-03-24 08:44:51 -070014#include <string>
Ed Tanous1abe55e2018-09-05 08:30:59 -070015
Ed Tanous1abe55e2018-09-05 08:30:59 -070016namespace redfish
17{
Ed Tanousa8c4ce92021-03-24 08:44:51 -070018
Ed Tanousd3355c52022-05-11 14:40:49 -070019inline void redfishGet(App& app, const crow::Request& req,
20 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21{
Carson Labrado3ba00072022-06-06 19:40:56 +000022 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanousd3355c52022-05-11 14:40:49 -070023 {
24 return;
25 }
26 asyncResp->res.jsonValue["v1"] = "/redfish/v1/";
27}
28
Ed Tanous8c623a92022-05-24 11:54:51 -070029inline void redfish404(App& app, const crow::Request& req,
30 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
31 const std::string& path)
32{
33 asyncResp->res.addHeader(boost::beast::http::field::allow, "");
34
35 // If we fall to this route, we didn't have a more specific route, so return
36 // 404
Nan Zhou686b7092022-06-15 20:02:27 +000037 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous8c623a92022-05-24 11:54:51 -070038 {
39 return;
40 }
41
Gunnar Mills48f3ffc2023-02-22 12:38:10 -060042 BMCWEB_LOG_WARNING << "404 on path " << path;
Ed Tanous8c623a92022-05-24 11:54:51 -070043
Ed Tanous39662a32023-02-06 15:09:46 -080044 std::string name = req.url().segments().back();
Ed Tanous8c623a92022-05-24 11:54:51 -070045 // Note, if we hit the wildcard route, we don't know the "type" the user was
46 // actually requesting, but giving them a return with an empty string is
47 // still better than nothing.
Ed Tanous079360a2022-06-29 10:05:19 -070048 messages::resourceNotFound(asyncResp->res, "", name);
Ed Tanous8c623a92022-05-24 11:54:51 -070049}
50
Ed Tanous44c70412022-07-31 16:48:29 -070051inline void redfish405(App& app, const crow::Request& req,
52 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
53 const std::string& path)
54{
55 // If we fall to this route, we didn't have a more specific route, so return
56 // 405
57 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
58 {
59 return;
60 }
61
Gunnar Mills48f3ffc2023-02-22 12:38:10 -060062 BMCWEB_LOG_WARNING << "405 on path " << path;
Ed Tanous44c70412022-07-31 16:48:29 -070063 asyncResp->res.result(boost::beast::http::status::method_not_allowed);
64 if (req.method() == boost::beast::http::verb::delete_)
65 {
66 messages::resourceCannotBeDeleted(asyncResp->res);
67 }
68 else
69 {
70 messages::operationNotAllowed(asyncResp->res);
71 }
72}
73
Ed Tanous81d523a2022-05-25 12:00:51 -070074inline void
75 jsonSchemaIndexGet(App& app, const crow::Request& req,
76 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
77{
78 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
79 {
80 return;
81 }
82 nlohmann::json& json = asyncResp->res.jsonValue;
83 json["@odata.id"] = "/redfish/v1/JsonSchemas";
Ed Tanous81d523a2022-05-25 12:00:51 -070084 json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection";
85 json["Name"] = "JsonSchemaFile Collection";
86 json["Description"] = "Collection of JsonSchemaFiles";
87 nlohmann::json::array_t members;
Ed Tanous26ccae32023-02-16 10:28:44 -080088 for (std::string_view schema : schemas)
Ed Tanous81d523a2022-05-25 12:00:51 -070089 {
90 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -070091 member["@odata.id"] = boost::urls::format("/redfish/v1/JsonSchemas/{}",
92 schema);
Patrick Williamsad539542023-05-12 10:10:08 -050093 members.emplace_back(std::move(member));
Ed Tanous81d523a2022-05-25 12:00:51 -070094 }
95 json["Members"] = std::move(members);
96 json["Members@odata.count"] = schemas.size();
97}
98
99inline void jsonSchemaGet(App& app, const crow::Request& req,
100 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
101 const std::string& schema)
102{
103 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
104 {
105 return;
106 }
107
108 if (std::find(schemas.begin(), schemas.end(), schema) == schemas.end())
109 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800110 messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema);
Ed Tanous81d523a2022-05-25 12:00:51 -0700111 return;
112 }
113
114 nlohmann::json& json = asyncResp->res.jsonValue;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700115 json["@odata.id"] = boost::urls::format("/redfish/v1/JsonSchemas/{}",
116 schema);
Ed Tanous81d523a2022-05-25 12:00:51 -0700117 json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile";
118 json["Name"] = schema + " Schema File";
119 json["Description"] = schema + " Schema File Location";
120 json["Id"] = schema;
121 std::string schemaName = "#";
122 schemaName += schema;
123 schemaName += ".";
124 schemaName += schema;
125 json["Schema"] = std::move(schemaName);
126 constexpr std::array<std::string_view, 1> languages{"en"};
127 json["Languages"] = languages;
128 json["Languages@odata.count"] = languages.size();
129
130 nlohmann::json::array_t locationArray;
131 nlohmann::json::object_t locationEntry;
132 locationEntry["Language"] = "en";
Patrick Williams89492a12023-05-10 07:51:34 -0500133 locationEntry["PublicationUri"] = "http://redfish.dmtf.org/schemas/v1/" +
134 schema + ".json";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700135 locationEntry["Uri"] = boost::urls::format(
136 "/redfish/v1/JsonSchemas/{}/{}", schema, std::string(schema) + ".json");
Ed Tanous81d523a2022-05-25 12:00:51 -0700137
138 locationArray.emplace_back(locationEntry);
139
140 json["Location"] = std::move(locationArray);
141 json["Location@odata.count"] = 1;
142}
143
Ed Tanousf65fca62022-05-24 12:49:41 -0700144inline void requestRoutesRedfish(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700145{
146 BMCWEB_ROUTE(app, "/redfish/")
Ed Tanousb41187f2019-10-24 16:30:02 -0700147 .methods(boost::beast::http::verb::get)(
Ed Tanousd3355c52022-05-11 14:40:49 -0700148 std::bind_front(redfishGet, std::ref(app)));
Ed Tanous8c623a92022-05-24 11:54:51 -0700149
Ed Tanous81d523a2022-05-25 12:00:51 -0700150 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700151 .privileges(redfish::privileges::getJsonSchemaFileCollection)
Ed Tanous81d523a2022-05-25 12:00:51 -0700152 .methods(boost::beast::http::verb::get)(
153 std::bind_front(jsonSchemaGet, std::ref(app)));
154
155 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700156 .privileges(redfish::privileges::getJsonSchemaFile)
Ed Tanous81d523a2022-05-25 12:00:51 -0700157 .methods(boost::beast::http::verb::get)(
158 std::bind_front(jsonSchemaIndexGet, std::ref(app)));
Ed Tanouse9dd1d32022-06-17 11:56:00 -0700159
160 // Note, this route must always be registered last
161 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700162 .notFound()
163 .privileges(redfish::privileges::privilegeSetLogin)(
164 std::bind_front(redfish404, std::ref(app)));
Ed Tanous44c70412022-07-31 16:48:29 -0700165
166 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700167 .methodNotAllowed()
168 .privileges(redfish::privileges::privilegeSetLogin)(
169 std::bind_front(redfish405, std::ref(app)));
Ed Tanous3dac7492017-08-02 13:46:20 -0700170}
Ed Tanousf65fca62022-05-24 12:49:41 -0700171
Ed Tanous1abe55e2018-09-05 08:30:59 -0700172} // namespace redfish