blob: 487fb1cf75305f7ccf7b98873c130721d1884552 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
Ed Tanous81d523a2022-05-25 12:00:51 -07003#include "error_messages.hpp"
4#include "utility.hpp"
5
Ed Tanous04e438c2020-10-03 08:06:26 -07006#include <app.hpp>
Ed Tanousa8c4ce92021-03-24 08:44:51 -07007#include <http_request.hpp>
8#include <http_response.hpp>
Ed Tanous81d523a2022-05-25 12:00:51 -07009#include <schemas.hpp>
Ed Tanousa8c4ce92021-03-24 08:44:51 -070010
11#include <string>
Ed Tanous1abe55e2018-09-05 08:30:59 -070012
Ed Tanous1abe55e2018-09-05 08:30:59 -070013namespace redfish
14{
Ed Tanousa8c4ce92021-03-24 08:44:51 -070015
Ed Tanousd3355c52022-05-11 14:40:49 -070016inline void redfishGet(App& app, const crow::Request& req,
17 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
18{
Carson Labrado3ba00072022-06-06 19:40:56 +000019 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanousd3355c52022-05-11 14:40:49 -070020 {
21 return;
22 }
23 asyncResp->res.jsonValue["v1"] = "/redfish/v1/";
24}
25
Ed Tanous8c623a92022-05-24 11:54:51 -070026inline void redfish404(App& app, const crow::Request& req,
27 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28 const std::string& path)
29{
30 asyncResp->res.addHeader(boost::beast::http::field::allow, "");
31
32 // If we fall to this route, we didn't have a more specific route, so return
33 // 404
Nan Zhou686b7092022-06-15 20:02:27 +000034 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous8c623a92022-05-24 11:54:51 -070035 {
36 return;
37 }
38
39 BMCWEB_LOG_ERROR << "404 on path " << path;
40
Ed Tanous079360a2022-06-29 10:05:19 -070041 std::string name = req.urlView.segments().back();
Ed Tanous8c623a92022-05-24 11:54:51 -070042 // Note, if we hit the wildcard route, we don't know the "type" the user was
43 // actually requesting, but giving them a return with an empty string is
44 // still better than nothing.
Ed Tanous079360a2022-06-29 10:05:19 -070045 messages::resourceNotFound(asyncResp->res, "", name);
Ed Tanous8c623a92022-05-24 11:54:51 -070046}
47
Ed Tanous44c70412022-07-31 16:48:29 -070048inline void redfish405(App& app, const crow::Request& req,
49 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
50 const std::string& path)
51{
52 // If we fall to this route, we didn't have a more specific route, so return
53 // 405
54 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
55 {
56 return;
57 }
58
59 BMCWEB_LOG_ERROR << "405 on path " << path;
60 asyncResp->res.result(boost::beast::http::status::method_not_allowed);
61 if (req.method() == boost::beast::http::verb::delete_)
62 {
63 messages::resourceCannotBeDeleted(asyncResp->res);
64 }
65 else
66 {
67 messages::operationNotAllowed(asyncResp->res);
68 }
69}
70
Ed Tanous81d523a2022-05-25 12:00:51 -070071inline void
72 jsonSchemaIndexGet(App& app, const crow::Request& req,
73 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
74{
75 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
76 {
77 return;
78 }
79 nlohmann::json& json = asyncResp->res.jsonValue;
80 json["@odata.id"] = "/redfish/v1/JsonSchemas";
Ed Tanous81d523a2022-05-25 12:00:51 -070081 json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection";
82 json["Name"] = "JsonSchemaFile Collection";
83 json["Description"] = "Collection of JsonSchemaFiles";
84 nlohmann::json::array_t members;
85 for (const std::string_view schema : schemas)
86 {
87 nlohmann::json::object_t member;
88 member["@odata.id"] = crow::utility::urlFromPieces(
89 "redfish", "v1", "JsonSchemas", schema);
90 members.push_back(std::move(member));
91 }
92 json["Members"] = std::move(members);
93 json["Members@odata.count"] = schemas.size();
94}
95
96inline void jsonSchemaGet(App& app, const crow::Request& req,
97 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
98 const std::string& schema)
99{
100 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
101 {
102 return;
103 }
104
105 if (std::find(schemas.begin(), schemas.end(), schema) == schemas.end())
106 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800107 messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema);
Ed Tanous81d523a2022-05-25 12:00:51 -0700108 return;
109 }
110
111 nlohmann::json& json = asyncResp->res.jsonValue;
Ed Tanous81d523a2022-05-25 12:00:51 -0700112 json["@odata.id"] =
113 crow::utility::urlFromPieces("redfish", "v1", "JsonSchemas", schema);
114 json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile";
115 json["Name"] = schema + " Schema File";
116 json["Description"] = schema + " Schema File Location";
117 json["Id"] = schema;
118 std::string schemaName = "#";
119 schemaName += schema;
120 schemaName += ".";
121 schemaName += schema;
122 json["Schema"] = std::move(schemaName);
123 constexpr std::array<std::string_view, 1> languages{"en"};
124 json["Languages"] = languages;
125 json["Languages@odata.count"] = languages.size();
126
127 nlohmann::json::array_t locationArray;
128 nlohmann::json::object_t locationEntry;
129 locationEntry["Language"] = "en";
130 locationEntry["PublicationUri"] =
131 "http://redfish.dmtf.org/schemas/v1/" + schema + ".json";
132 locationEntry["Uri"] = crow::utility::urlFromPieces(
133 "redfish", "v1", "JsonSchemas", schema, std::string(schema) + ".json");
134
135 locationArray.emplace_back(locationEntry);
136
137 json["Location"] = std::move(locationArray);
138 json["Location@odata.count"] = 1;
139}
140
Ed Tanousf65fca62022-05-24 12:49:41 -0700141inline void requestRoutesRedfish(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700142{
143 BMCWEB_ROUTE(app, "/redfish/")
Ed Tanousb41187f2019-10-24 16:30:02 -0700144 .methods(boost::beast::http::verb::get)(
Ed Tanousd3355c52022-05-11 14:40:49 -0700145 std::bind_front(redfishGet, std::ref(app)));
Ed Tanous8c623a92022-05-24 11:54:51 -0700146
Ed Tanous81d523a2022-05-25 12:00:51 -0700147 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700148 .privileges(redfish::privileges::getJsonSchemaFileCollection)
Ed Tanous81d523a2022-05-25 12:00:51 -0700149 .methods(boost::beast::http::verb::get)(
150 std::bind_front(jsonSchemaGet, std::ref(app)));
151
152 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700153 .privileges(redfish::privileges::getJsonSchemaFile)
Ed Tanous81d523a2022-05-25 12:00:51 -0700154 .methods(boost::beast::http::verb::get)(
155 std::bind_front(jsonSchemaIndexGet, std::ref(app)));
Ed Tanouse9dd1d32022-06-17 11:56:00 -0700156
157 // Note, this route must always be registered last
158 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700159 .notFound()
160 .privileges(redfish::privileges::privilegeSetLogin)(
161 std::bind_front(redfish404, std::ref(app)));
Ed Tanous44c70412022-07-31 16:48:29 -0700162
163 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700164 .methodNotAllowed()
165 .privileges(redfish::privileges::privilegeSetLogin)(
166 std::bind_front(redfish405, std::ref(app)));
Ed Tanous3dac7492017-08-02 13:46:20 -0700167}
Ed Tanousf65fca62022-05-24 12:49:41 -0700168
Ed Tanous1abe55e2018-09-05 08:30:59 -0700169} // namespace redfish