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 | a8c4ce9 | 2021-03-24 08:44:51 -0700 | [diff] [blame] | 14 | #include <string> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 15 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 16 | namespace redfish |
| 17 | { |
Ed Tanous | a8c4ce9 | 2021-03-24 08:44:51 -0700 | [diff] [blame] | 18 | |
Ed Tanous | d3355c5 | 2022-05-11 14:40:49 -0700 | [diff] [blame] | 19 | inline void redfishGet(App& app, const crow::Request& req, |
| 20 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 21 | { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 22 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | d3355c5 | 2022-05-11 14:40:49 -0700 | [diff] [blame] | 23 | { |
| 24 | return; |
| 25 | } |
| 26 | asyncResp->res.jsonValue["v1"] = "/redfish/v1/"; |
| 27 | } |
| 28 | |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 29 | inline 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 Zhou | 686b709 | 2022-06-15 20:02:27 +0000 | [diff] [blame] | 37 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 38 | { |
| 39 | return; |
| 40 | } |
| 41 | |
Gunnar Mills | 48f3ffc | 2023-02-22 12:38:10 -0600 | [diff] [blame] | 42 | BMCWEB_LOG_WARNING << "404 on path " << path; |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 43 | |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 44 | std::string name = req.url().segments().back(); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 45 | // 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 Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 48 | messages::resourceNotFound(asyncResp->res, "", name); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Ed Tanous | 44c7041 | 2022-07-31 16:48:29 -0700 | [diff] [blame] | 51 | inline 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 Mills | 48f3ffc | 2023-02-22 12:38:10 -0600 | [diff] [blame] | 62 | BMCWEB_LOG_WARNING << "405 on path " << path; |
Ed Tanous | 44c7041 | 2022-07-31 16:48:29 -0700 | [diff] [blame] | 63 | 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 Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 74 | inline 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 Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 84 | json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection"; |
| 85 | json["Name"] = "JsonSchemaFile Collection"; |
| 86 | json["Description"] = "Collection of JsonSchemaFiles"; |
| 87 | nlohmann::json::array_t members; |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 88 | for (std::string_view schema : schemas) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 89 | { |
| 90 | nlohmann::json::object_t member; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 91 | member["@odata.id"] = boost::urls::format("/redfish/v1/JsonSchemas/{}", |
| 92 | schema); |
Patrick Williams | ad53954 | 2023-05-12 10:10:08 -0500 | [diff] [blame] | 93 | members.emplace_back(std::move(member)); |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 94 | } |
| 95 | json["Members"] = std::move(members); |
| 96 | json["Members@odata.count"] = schemas.size(); |
| 97 | } |
| 98 | |
| 99 | inline 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 Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 110 | messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 111 | return; |
| 112 | } |
| 113 | |
| 114 | nlohmann::json& json = asyncResp->res.jsonValue; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 115 | json["@odata.id"] = boost::urls::format("/redfish/v1/JsonSchemas/{}", |
| 116 | schema); |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 117 | 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 Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 133 | locationEntry["PublicationUri"] = "http://redfish.dmtf.org/schemas/v1/" + |
| 134 | schema + ".json"; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 135 | locationEntry["Uri"] = boost::urls::format( |
| 136 | "/redfish/v1/JsonSchemas/{}/{}", schema, std::string(schema) + ".json"); |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 137 | |
| 138 | locationArray.emplace_back(locationEntry); |
| 139 | |
| 140 | json["Location"] = std::move(locationArray); |
| 141 | json["Location@odata.count"] = 1; |
| 142 | } |
| 143 | |
Ed Tanous | f65fca6 | 2022-05-24 12:49:41 -0700 | [diff] [blame] | 144 | inline void requestRoutesRedfish(App& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 145 | { |
| 146 | BMCWEB_ROUTE(app, "/redfish/") |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 147 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | d3355c5 | 2022-05-11 14:40:49 -0700 | [diff] [blame] | 148 | std::bind_front(redfishGet, std::ref(app))); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 149 | |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 150 | BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 151 | .privileges(redfish::privileges::getJsonSchemaFileCollection) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 152 | .methods(boost::beast::http::verb::get)( |
| 153 | std::bind_front(jsonSchemaGet, std::ref(app))); |
| 154 | |
| 155 | BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 156 | .privileges(redfish::privileges::getJsonSchemaFile) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 157 | .methods(boost::beast::http::verb::get)( |
| 158 | std::bind_front(jsonSchemaIndexGet, std::ref(app))); |
Ed Tanous | e9dd1d3 | 2022-06-17 11:56:00 -0700 | [diff] [blame] | 159 | |
| 160 | // Note, this route must always be registered last |
| 161 | BMCWEB_ROUTE(app, "/redfish/<path>") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 162 | .notFound() |
| 163 | .privileges(redfish::privileges::privilegeSetLogin)( |
| 164 | std::bind_front(redfish404, std::ref(app))); |
Ed Tanous | 44c7041 | 2022-07-31 16:48:29 -0700 | [diff] [blame] | 165 | |
| 166 | BMCWEB_ROUTE(app, "/redfish/<path>") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 167 | .methodNotAllowed() |
| 168 | .privileges(redfish::privileges::privilegeSetLogin)( |
| 169 | std::bind_front(redfish405, std::ref(app))); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 170 | } |
Ed Tanous | f65fca6 | 2022-05-24 12:49:41 -0700 | [diff] [blame] | 171 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 172 | } // namespace redfish |