Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 3 | #include "app.hpp" |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 4 | #include "error_messages.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 5 | #include "http_request.hpp" |
| 6 | #include "http_response.hpp" |
| 7 | #include "query.hpp" |
| 8 | #include "registries/privilege_registry.hpp" |
| 9 | #include "schemas.hpp" |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 10 | #include "utility.hpp" |
| 11 | |
Ed Tanous | a8c4ce9 | 2021-03-24 08:44:51 -0700 | [diff] [blame] | 12 | #include <string> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 13 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 14 | namespace redfish |
| 15 | { |
Ed Tanous | a8c4ce9 | 2021-03-24 08:44:51 -0700 | [diff] [blame] | 16 | |
Ed Tanous | d3355c5 | 2022-05-11 14:40:49 -0700 | [diff] [blame] | 17 | inline void redfishGet(App& app, const crow::Request& req, |
| 18 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 19 | { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 20 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | d3355c5 | 2022-05-11 14:40:49 -0700 | [diff] [blame] | 21 | { |
| 22 | return; |
| 23 | } |
| 24 | asyncResp->res.jsonValue["v1"] = "/redfish/v1/"; |
| 25 | } |
| 26 | |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 27 | inline 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 Zhou | 686b709 | 2022-06-15 20:02:27 +0000 | [diff] [blame] | 35 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 36 | { |
| 37 | return; |
| 38 | } |
| 39 | |
Gunnar Mills | 48f3ffc | 2023-02-22 12:38:10 -0600 | [diff] [blame] | 40 | BMCWEB_LOG_WARNING << "404 on path " << path; |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 41 | |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 42 | std::string name = req.urlView.segments().back(); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 43 | // 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 Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 46 | messages::resourceNotFound(asyncResp->res, "", name); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Ed Tanous | 44c7041 | 2022-07-31 16:48:29 -0700 | [diff] [blame] | 49 | inline 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 | |
Gunnar Mills | 48f3ffc | 2023-02-22 12:38:10 -0600 | [diff] [blame] | 60 | BMCWEB_LOG_WARNING << "405 on path " << path; |
Ed Tanous | 44c7041 | 2022-07-31 16:48:29 -0700 | [diff] [blame] | 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 Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 72 | inline 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 Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 82 | json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection"; |
| 83 | json["Name"] = "JsonSchemaFile Collection"; |
| 84 | json["Description"] = "Collection of JsonSchemaFiles"; |
| 85 | nlohmann::json::array_t members; |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 86 | for (std::string_view schema : schemas) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 87 | { |
| 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 | |
| 97 | inline 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 Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 108 | messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | |
| 112 | nlohmann::json& json = asyncResp->res.jsonValue; |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 113 | 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 Tanous | f65fca6 | 2022-05-24 12:49:41 -0700 | [diff] [blame] | 142 | inline void requestRoutesRedfish(App& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 143 | { |
| 144 | BMCWEB_ROUTE(app, "/redfish/") |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 145 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | d3355c5 | 2022-05-11 14:40:49 -0700 | [diff] [blame] | 146 | std::bind_front(redfishGet, std::ref(app))); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 147 | |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 148 | BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 149 | .privileges(redfish::privileges::getJsonSchemaFileCollection) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 150 | .methods(boost::beast::http::verb::get)( |
| 151 | std::bind_front(jsonSchemaGet, std::ref(app))); |
| 152 | |
| 153 | BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 154 | .privileges(redfish::privileges::getJsonSchemaFile) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 155 | .methods(boost::beast::http::verb::get)( |
| 156 | std::bind_front(jsonSchemaIndexGet, std::ref(app))); |
Ed Tanous | e9dd1d3 | 2022-06-17 11:56:00 -0700 | [diff] [blame] | 157 | |
| 158 | // Note, this route must always be registered last |
| 159 | BMCWEB_ROUTE(app, "/redfish/<path>") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 160 | .notFound() |
| 161 | .privileges(redfish::privileges::privilegeSetLogin)( |
| 162 | std::bind_front(redfish404, std::ref(app))); |
Ed Tanous | 44c7041 | 2022-07-31 16:48:29 -0700 | [diff] [blame] | 163 | |
| 164 | BMCWEB_ROUTE(app, "/redfish/<path>") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 165 | .methodNotAllowed() |
| 166 | .privileges(redfish::privileges::privilegeSetLogin)( |
| 167 | std::bind_front(redfish405, std::ref(app))); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 168 | } |
Ed Tanous | f65fca6 | 2022-05-24 12:49:41 -0700 | [diff] [blame] | 169 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 170 | } // namespace redfish |