blob: 4cae985c2121366f5803c890d419a61d0e80772d [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 Tanousa8c4ce92021-03-24 08:44:51 -070012#include <string>
Ed Tanous1abe55e2018-09-05 08:30:59 -070013
Ed Tanous1abe55e2018-09-05 08:30:59 -070014namespace redfish
15{
Ed Tanousa8c4ce92021-03-24 08:44:51 -070016
Ed Tanousd3355c52022-05-11 14:40:49 -070017inline void redfishGet(App& app, const crow::Request& req,
18 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
19{
Carson Labrado3ba00072022-06-06 19:40:56 +000020 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanousd3355c52022-05-11 14:40:49 -070021 {
22 return;
23 }
24 asyncResp->res.jsonValue["v1"] = "/redfish/v1/";
25}
26
Ed Tanous8c623a92022-05-24 11:54:51 -070027inline void redfish404(App& app, const crow::Request& req,
28 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29 const std::string& path)
30{
31 asyncResp->res.addHeader(boost::beast::http::field::allow, "");
32
33 // If we fall to this route, we didn't have a more specific route, so return
34 // 404
Nan Zhou686b7092022-06-15 20:02:27 +000035 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous8c623a92022-05-24 11:54:51 -070036 {
37 return;
38 }
39
40 BMCWEB_LOG_ERROR << "404 on path " << path;
41
Ed Tanous079360a2022-06-29 10:05:19 -070042 std::string name = req.urlView.segments().back();
Ed Tanous8c623a92022-05-24 11:54:51 -070043 // Note, if we hit the wildcard route, we don't know the "type" the user was
44 // actually requesting, but giving them a return with an empty string is
45 // still better than nothing.
Ed Tanous079360a2022-06-29 10:05:19 -070046 messages::resourceNotFound(asyncResp->res, "", name);
Ed Tanous8c623a92022-05-24 11:54:51 -070047}
48
Ed Tanous44c70412022-07-31 16:48:29 -070049inline void redfish405(App& app, const crow::Request& req,
50 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
51 const std::string& path)
52{
53 // If we fall to this route, we didn't have a more specific route, so return
54 // 405
55 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
56 {
57 return;
58 }
59
60 BMCWEB_LOG_ERROR << "405 on path " << path;
61 asyncResp->res.result(boost::beast::http::status::method_not_allowed);
62 if (req.method() == boost::beast::http::verb::delete_)
63 {
64 messages::resourceCannotBeDeleted(asyncResp->res);
65 }
66 else
67 {
68 messages::operationNotAllowed(asyncResp->res);
69 }
70}
71
Ed Tanous81d523a2022-05-25 12:00:51 -070072inline void
73 jsonSchemaIndexGet(App& app, const crow::Request& req,
74 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
75{
76 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
77 {
78 return;
79 }
80 nlohmann::json& json = asyncResp->res.jsonValue;
81 json["@odata.id"] = "/redfish/v1/JsonSchemas";
Ed Tanous81d523a2022-05-25 12:00:51 -070082 json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection";
83 json["Name"] = "JsonSchemaFile Collection";
84 json["Description"] = "Collection of JsonSchemaFiles";
85 nlohmann::json::array_t members;
Ed Tanous26ccae32023-02-16 10:28:44 -080086 for (std::string_view schema : schemas)
Ed Tanous81d523a2022-05-25 12:00:51 -070087 {
88 nlohmann::json::object_t member;
89 member["@odata.id"] = crow::utility::urlFromPieces(
90 "redfish", "v1", "JsonSchemas", schema);
91 members.push_back(std::move(member));
92 }
93 json["Members"] = std::move(members);
94 json["Members@odata.count"] = schemas.size();
95}
96
97inline void jsonSchemaGet(App& app, const crow::Request& req,
98 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
99 const std::string& schema)
100{
101 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
102 {
103 return;
104 }
105
106 if (std::find(schemas.begin(), schemas.end(), schema) == schemas.end())
107 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800108 messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema);
Ed Tanous81d523a2022-05-25 12:00:51 -0700109 return;
110 }
111
112 nlohmann::json& json = asyncResp->res.jsonValue;
Ed Tanous81d523a2022-05-25 12:00:51 -0700113 json["@odata.id"] =
114 crow::utility::urlFromPieces("redfish", "v1", "JsonSchemas", schema);
115 json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile";
116 json["Name"] = schema + " Schema File";
117 json["Description"] = schema + " Schema File Location";
118 json["Id"] = schema;
119 std::string schemaName = "#";
120 schemaName += schema;
121 schemaName += ".";
122 schemaName += schema;
123 json["Schema"] = std::move(schemaName);
124 constexpr std::array<std::string_view, 1> languages{"en"};
125 json["Languages"] = languages;
126 json["Languages@odata.count"] = languages.size();
127
128 nlohmann::json::array_t locationArray;
129 nlohmann::json::object_t locationEntry;
130 locationEntry["Language"] = "en";
131 locationEntry["PublicationUri"] =
132 "http://redfish.dmtf.org/schemas/v1/" + schema + ".json";
133 locationEntry["Uri"] = crow::utility::urlFromPieces(
134 "redfish", "v1", "JsonSchemas", schema, std::string(schema) + ".json");
135
136 locationArray.emplace_back(locationEntry);
137
138 json["Location"] = std::move(locationArray);
139 json["Location@odata.count"] = 1;
140}
141
Ed Tanousf65fca62022-05-24 12:49:41 -0700142inline void requestRoutesRedfish(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700143{
144 BMCWEB_ROUTE(app, "/redfish/")
Ed Tanousb41187f2019-10-24 16:30:02 -0700145 .methods(boost::beast::http::verb::get)(
Ed Tanousd3355c52022-05-11 14:40:49 -0700146 std::bind_front(redfishGet, std::ref(app)));
Ed Tanous8c623a92022-05-24 11:54:51 -0700147
Ed Tanous81d523a2022-05-25 12:00:51 -0700148 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700149 .privileges(redfish::privileges::getJsonSchemaFileCollection)
Ed Tanous81d523a2022-05-25 12:00:51 -0700150 .methods(boost::beast::http::verb::get)(
151 std::bind_front(jsonSchemaGet, std::ref(app)));
152
153 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700154 .privileges(redfish::privileges::getJsonSchemaFile)
Ed Tanous81d523a2022-05-25 12:00:51 -0700155 .methods(boost::beast::http::verb::get)(
156 std::bind_front(jsonSchemaIndexGet, std::ref(app)));
Ed Tanouse9dd1d32022-06-17 11:56:00 -0700157
158 // Note, this route must always be registered last
159 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700160 .notFound()
161 .privileges(redfish::privileges::privilegeSetLogin)(
162 std::bind_front(redfish404, std::ref(app)));
Ed Tanous44c70412022-07-31 16:48:29 -0700163
164 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700165 .methodNotAllowed()
166 .privileges(redfish::privileges::privilegeSetLogin)(
167 std::bind_front(redfish405, std::ref(app)));
Ed Tanous3dac7492017-08-02 13:46:20 -0700168}
Ed Tanousf65fca62022-05-24 12:49:41 -0700169
Ed Tanous1abe55e2018-09-05 08:30:59 -0700170} // namespace redfish