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 | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 12 | #include <boost/url/format.hpp> |
| 13 | |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 14 | #include <ranges> |
Ed Tanous | a8c4ce9 | 2021-03-24 08:44:51 -0700 | [diff] [blame] | 15 | #include <string> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 16 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 17 | namespace redfish |
| 18 | { |
Ed Tanous | a8c4ce9 | 2021-03-24 08:44:51 -0700 | [diff] [blame] | 19 | |
Ed Tanous | d3355c5 | 2022-05-11 14:40:49 -0700 | [diff] [blame] | 20 | inline void redfishGet(App& app, const crow::Request& req, |
| 21 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 22 | { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 23 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | d3355c5 | 2022-05-11 14:40:49 -0700 | [diff] [blame] | 24 | { |
| 25 | return; |
| 26 | } |
| 27 | asyncResp->res.jsonValue["v1"] = "/redfish/v1/"; |
| 28 | } |
| 29 | |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 30 | inline void redfish404(App& app, const crow::Request& req, |
| 31 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 32 | const std::string& path) |
| 33 | { |
| 34 | asyncResp->res.addHeader(boost::beast::http::field::allow, ""); |
| 35 | |
| 36 | // If we fall to this route, we didn't have a more specific route, so return |
| 37 | // 404 |
Nan Zhou | 686b709 | 2022-06-15 20:02:27 +0000 | [diff] [blame] | 38 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 39 | { |
| 40 | return; |
| 41 | } |
| 42 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 43 | BMCWEB_LOG_WARNING("404 on path {}", path); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 44 | |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 45 | std::string name = req.url().segments().back(); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 46 | // Note, if we hit the wildcard route, we don't know the "type" the user was |
| 47 | // actually requesting, but giving them a return with an empty string is |
| 48 | // still better than nothing. |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 49 | messages::resourceNotFound(asyncResp->res, "", name); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Ed Tanous | 44c7041 | 2022-07-31 16:48:29 -0700 | [diff] [blame] | 52 | inline void redfish405(App& app, const crow::Request& req, |
| 53 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 54 | const std::string& path) |
| 55 | { |
| 56 | // If we fall to this route, we didn't have a more specific route, so return |
| 57 | // 405 |
| 58 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 59 | { |
| 60 | return; |
| 61 | } |
| 62 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 63 | BMCWEB_LOG_WARNING("405 on path {}", path); |
Ed Tanous | 44c7041 | 2022-07-31 16:48:29 -0700 | [diff] [blame] | 64 | asyncResp->res.result(boost::beast::http::status::method_not_allowed); |
| 65 | if (req.method() == boost::beast::http::verb::delete_) |
| 66 | { |
| 67 | messages::resourceCannotBeDeleted(asyncResp->res); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | messages::operationNotAllowed(asyncResp->res); |
| 72 | } |
| 73 | } |
| 74 | |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 75 | inline void |
| 76 | jsonSchemaIndexGet(App& app, const crow::Request& req, |
| 77 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 78 | { |
| 79 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 80 | { |
| 81 | return; |
| 82 | } |
| 83 | nlohmann::json& json = asyncResp->res.jsonValue; |
| 84 | json["@odata.id"] = "/redfish/v1/JsonSchemas"; |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 85 | json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection"; |
| 86 | json["Name"] = "JsonSchemaFile Collection"; |
| 87 | json["Description"] = "Collection of JsonSchemaFiles"; |
| 88 | nlohmann::json::array_t members; |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 89 | for (std::string_view schema : schemas) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 90 | { |
| 91 | nlohmann::json::object_t member; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 92 | member["@odata.id"] = boost::urls::format("/redfish/v1/JsonSchemas/{}", |
| 93 | schema); |
Patrick Williams | ad53954 | 2023-05-12 10:10:08 -0500 | [diff] [blame] | 94 | members.emplace_back(std::move(member)); |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 95 | } |
| 96 | json["Members"] = std::move(members); |
| 97 | json["Members@odata.count"] = schemas.size(); |
| 98 | } |
| 99 | |
| 100 | inline void jsonSchemaGet(App& app, const crow::Request& req, |
| 101 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 102 | const std::string& schema) |
| 103 | { |
| 104 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 105 | { |
| 106 | return; |
| 107 | } |
| 108 | |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 109 | if (std::ranges::find(schemas, schema) == schemas.end()) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 110 | { |
Jiaqing Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 111 | messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
| 115 | nlohmann::json& json = asyncResp->res.jsonValue; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 116 | json["@odata.id"] = boost::urls::format("/redfish/v1/JsonSchemas/{}", |
| 117 | schema); |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 118 | json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile"; |
| 119 | json["Name"] = schema + " Schema File"; |
| 120 | json["Description"] = schema + " Schema File Location"; |
| 121 | json["Id"] = schema; |
| 122 | std::string schemaName = "#"; |
| 123 | schemaName += schema; |
| 124 | schemaName += "."; |
| 125 | schemaName += schema; |
| 126 | json["Schema"] = std::move(schemaName); |
| 127 | constexpr std::array<std::string_view, 1> languages{"en"}; |
| 128 | json["Languages"] = languages; |
| 129 | json["Languages@odata.count"] = languages.size(); |
| 130 | |
| 131 | nlohmann::json::array_t locationArray; |
| 132 | nlohmann::json::object_t locationEntry; |
| 133 | locationEntry["Language"] = "en"; |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 134 | locationEntry["PublicationUri"] = "http://redfish.dmtf.org/schemas/v1/" + |
| 135 | schema + ".json"; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 136 | locationEntry["Uri"] = boost::urls::format( |
| 137 | "/redfish/v1/JsonSchemas/{}/{}", schema, std::string(schema) + ".json"); |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 138 | |
| 139 | locationArray.emplace_back(locationEntry); |
| 140 | |
| 141 | json["Location"] = std::move(locationArray); |
| 142 | json["Location@odata.count"] = 1; |
| 143 | } |
| 144 | |
Ed Tanous | f65fca6 | 2022-05-24 12:49:41 -0700 | [diff] [blame] | 145 | inline void requestRoutesRedfish(App& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 146 | { |
| 147 | BMCWEB_ROUTE(app, "/redfish/") |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 148 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | d3355c5 | 2022-05-11 14:40:49 -0700 | [diff] [blame] | 149 | std::bind_front(redfishGet, std::ref(app))); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 150 | |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 151 | BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 152 | .privileges(redfish::privileges::getJsonSchemaFileCollection) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 153 | .methods(boost::beast::http::verb::get)( |
| 154 | std::bind_front(jsonSchemaGet, std::ref(app))); |
| 155 | |
| 156 | BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 157 | .privileges(redfish::privileges::getJsonSchemaFile) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 158 | .methods(boost::beast::http::verb::get)( |
| 159 | std::bind_front(jsonSchemaIndexGet, std::ref(app))); |
Ed Tanous | e9dd1d3 | 2022-06-17 11:56:00 -0700 | [diff] [blame] | 160 | |
| 161 | // Note, this route must always be registered last |
| 162 | BMCWEB_ROUTE(app, "/redfish/<path>") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 163 | .notFound() |
| 164 | .privileges(redfish::privileges::privilegeSetLogin)( |
| 165 | std::bind_front(redfish404, std::ref(app))); |
Ed Tanous | 44c7041 | 2022-07-31 16:48:29 -0700 | [diff] [blame] | 166 | |
| 167 | BMCWEB_ROUTE(app, "/redfish/<path>") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 168 | .methodNotAllowed() |
| 169 | .privileges(redfish::privileges::privilegeSetLogin)( |
| 170 | std::bind_front(redfish405, std::ref(app))); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 171 | } |
Ed Tanous | f65fca6 | 2022-05-24 12:49:41 -0700 | [diff] [blame] | 172 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 173 | } // namespace redfish |