xref: /openbmc/bmcweb/features/redfish/lib/redfish_v1.hpp (revision ef4c65b741724d724452a3a0efe8dff0d450514a)
14c25d66eSEd Tanous #pragma once
24c25d66eSEd Tanous 
33ccb3adbSEd Tanous #include "app.hpp"
481d523a7SEd Tanous #include "error_messages.hpp"
53ccb3adbSEd Tanous #include "http_request.hpp"
63ccb3adbSEd Tanous #include "http_response.hpp"
73ccb3adbSEd Tanous #include "query.hpp"
83ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
93ccb3adbSEd Tanous #include "schemas.hpp"
1081d523a7SEd Tanous #include "utility.hpp"
1181d523a7SEd Tanous 
12*ef4c65b7SEd Tanous #include <boost/url/format.hpp>
13*ef4c65b7SEd Tanous 
144c25d66eSEd Tanous #include <string>
154c25d66eSEd Tanous 
164c25d66eSEd Tanous namespace redfish
174c25d66eSEd Tanous {
184c25d66eSEd Tanous 
19d3355c5cSEd Tanous inline void redfishGet(App& app, const crow::Request& req,
20d3355c5cSEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
214c25d66eSEd Tanous {
223ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
231e925c84SEd Tanous     {
241e925c84SEd Tanous         return;
251e925c84SEd Tanous     }
261476687dSEd Tanous     asyncResp->res.jsonValue["v1"] = "/redfish/v1/";
27d3355c5cSEd Tanous }
28d3355c5cSEd Tanous 
298c623a96SEd Tanous inline void redfish404(App& app, const crow::Request& req,
308c623a96SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
318c623a96SEd Tanous                        const std::string& path)
328c623a96SEd Tanous {
338c623a96SEd Tanous     asyncResp->res.addHeader(boost::beast::http::field::allow, "");
348c623a96SEd Tanous 
358c623a96SEd Tanous     // If we fall to this route, we didn't have a more specific route, so return
368c623a96SEd Tanous     // 404
37686b7093SNan Zhou     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
388c623a96SEd Tanous     {
398c623a96SEd Tanous         return;
408c623a96SEd Tanous     }
418c623a96SEd Tanous 
4248f3ffcbSGunnar Mills     BMCWEB_LOG_WARNING << "404 on path " << path;
438c623a96SEd Tanous 
4439662a3bSEd Tanous     std::string name = req.url().segments().back();
458c623a96SEd Tanous     // Note, if we hit the wildcard route, we don't know the "type" the user was
468c623a96SEd Tanous     // actually requesting, but giving them a return with an empty string is
478c623a96SEd Tanous     // still better than nothing.
48079360aeSEd Tanous     messages::resourceNotFound(asyncResp->res, "", name);
498c623a96SEd Tanous }
508c623a96SEd Tanous 
5144c70412SEd Tanous inline void redfish405(App& app, const crow::Request& req,
5244c70412SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5344c70412SEd Tanous                        const std::string& path)
5444c70412SEd Tanous {
5544c70412SEd Tanous     // If we fall to this route, we didn't have a more specific route, so return
5644c70412SEd Tanous     // 405
5744c70412SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
5844c70412SEd Tanous     {
5944c70412SEd Tanous         return;
6044c70412SEd Tanous     }
6144c70412SEd Tanous 
6248f3ffcbSGunnar Mills     BMCWEB_LOG_WARNING << "405 on path " << path;
6344c70412SEd Tanous     asyncResp->res.result(boost::beast::http::status::method_not_allowed);
6444c70412SEd Tanous     if (req.method() == boost::beast::http::verb::delete_)
6544c70412SEd Tanous     {
6644c70412SEd Tanous         messages::resourceCannotBeDeleted(asyncResp->res);
6744c70412SEd Tanous     }
6844c70412SEd Tanous     else
6944c70412SEd Tanous     {
7044c70412SEd Tanous         messages::operationNotAllowed(asyncResp->res);
7144c70412SEd Tanous     }
7244c70412SEd Tanous }
7344c70412SEd Tanous 
7481d523a7SEd Tanous inline void
7581d523a7SEd Tanous     jsonSchemaIndexGet(App& app, const crow::Request& req,
7681d523a7SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7781d523a7SEd Tanous {
7881d523a7SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
7981d523a7SEd Tanous     {
8081d523a7SEd Tanous         return;
8181d523a7SEd Tanous     }
8281d523a7SEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
8381d523a7SEd Tanous     json["@odata.id"] = "/redfish/v1/JsonSchemas";
8481d523a7SEd Tanous     json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection";
8581d523a7SEd Tanous     json["Name"] = "JsonSchemaFile Collection";
8681d523a7SEd Tanous     json["Description"] = "Collection of JsonSchemaFiles";
8781d523a7SEd Tanous     nlohmann::json::array_t members;
8826ccae32SEd Tanous     for (std::string_view schema : schemas)
8981d523a7SEd Tanous     {
9081d523a7SEd Tanous         nlohmann::json::object_t member;
91*ef4c65b7SEd Tanous         member["@odata.id"] = boost::urls::format("/redfish/v1/JsonSchemas/{}",
92*ef4c65b7SEd Tanous                                                   schema);
93ad539545SPatrick Williams         members.emplace_back(std::move(member));
9481d523a7SEd Tanous     }
9581d523a7SEd Tanous     json["Members"] = std::move(members);
9681d523a7SEd Tanous     json["Members@odata.count"] = schemas.size();
9781d523a7SEd Tanous }
9881d523a7SEd Tanous 
9981d523a7SEd Tanous inline void jsonSchemaGet(App& app, const crow::Request& req,
10081d523a7SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10181d523a7SEd Tanous                           const std::string& schema)
10281d523a7SEd Tanous {
10381d523a7SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
10481d523a7SEd Tanous     {
10581d523a7SEd Tanous         return;
10681d523a7SEd Tanous     }
10781d523a7SEd Tanous 
10881d523a7SEd Tanous     if (std::find(schemas.begin(), schemas.end(), schema) == schemas.end())
10981d523a7SEd Tanous     {
110d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema);
11181d523a7SEd Tanous         return;
11281d523a7SEd Tanous     }
11381d523a7SEd Tanous 
11481d523a7SEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
115*ef4c65b7SEd Tanous     json["@odata.id"] = boost::urls::format("/redfish/v1/JsonSchemas/{}",
116*ef4c65b7SEd Tanous                                             schema);
11781d523a7SEd Tanous     json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile";
11881d523a7SEd Tanous     json["Name"] = schema + " Schema File";
11981d523a7SEd Tanous     json["Description"] = schema + " Schema File Location";
12081d523a7SEd Tanous     json["Id"] = schema;
12181d523a7SEd Tanous     std::string schemaName = "#";
12281d523a7SEd Tanous     schemaName += schema;
12381d523a7SEd Tanous     schemaName += ".";
12481d523a7SEd Tanous     schemaName += schema;
12581d523a7SEd Tanous     json["Schema"] = std::move(schemaName);
12681d523a7SEd Tanous     constexpr std::array<std::string_view, 1> languages{"en"};
12781d523a7SEd Tanous     json["Languages"] = languages;
12881d523a7SEd Tanous     json["Languages@odata.count"] = languages.size();
12981d523a7SEd Tanous 
13081d523a7SEd Tanous     nlohmann::json::array_t locationArray;
13181d523a7SEd Tanous     nlohmann::json::object_t locationEntry;
13281d523a7SEd Tanous     locationEntry["Language"] = "en";
13389492a15SPatrick Williams     locationEntry["PublicationUri"] = "http://redfish.dmtf.org/schemas/v1/" +
13489492a15SPatrick Williams                                       schema + ".json";
135*ef4c65b7SEd Tanous     locationEntry["Uri"] = boost::urls::format(
136*ef4c65b7SEd Tanous         "/redfish/v1/JsonSchemas/{}/{}", schema, std::string(schema) + ".json");
13781d523a7SEd Tanous 
13881d523a7SEd Tanous     locationArray.emplace_back(locationEntry);
13981d523a7SEd Tanous 
14081d523a7SEd Tanous     json["Location"] = std::move(locationArray);
14181d523a7SEd Tanous     json["Location@odata.count"] = 1;
14281d523a7SEd Tanous }
14381d523a7SEd Tanous 
144f65fca6aSEd Tanous inline void requestRoutesRedfish(App& app)
145d3355c5cSEd Tanous {
146d3355c5cSEd Tanous     BMCWEB_ROUTE(app, "/redfish/")
147d3355c5cSEd Tanous         .methods(boost::beast::http::verb::get)(
148d3355c5cSEd Tanous             std::bind_front(redfishGet, std::ref(app)));
1498c623a96SEd Tanous 
15081d523a7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/")
1510ea4b4e2SEd Tanous         .privileges(redfish::privileges::getJsonSchemaFileCollection)
15281d523a7SEd Tanous         .methods(boost::beast::http::verb::get)(
15381d523a7SEd Tanous             std::bind_front(jsonSchemaGet, std::ref(app)));
15481d523a7SEd Tanous 
15581d523a7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/")
1560ea4b4e2SEd Tanous         .privileges(redfish::privileges::getJsonSchemaFile)
15781d523a7SEd Tanous         .methods(boost::beast::http::verb::get)(
15881d523a7SEd Tanous             std::bind_front(jsonSchemaIndexGet, std::ref(app)));
159e9dd1d31SEd Tanous 
160e9dd1d31SEd Tanous     // Note, this route must always be registered last
161e9dd1d31SEd Tanous     BMCWEB_ROUTE(app, "/redfish/<path>")
1620ea4b4e2SEd Tanous         .notFound()
1630ea4b4e2SEd Tanous         .privileges(redfish::privileges::privilegeSetLogin)(
1640ea4b4e2SEd Tanous             std::bind_front(redfish404, std::ref(app)));
16544c70412SEd Tanous 
16644c70412SEd Tanous     BMCWEB_ROUTE(app, "/redfish/<path>")
1670ea4b4e2SEd Tanous         .methodNotAllowed()
1680ea4b4e2SEd Tanous         .privileges(redfish::privileges::privilegeSetLogin)(
1690ea4b4e2SEd Tanous             std::bind_front(redfish405, std::ref(app)));
1704c25d66eSEd Tanous }
171f65fca6aSEd Tanous 
1724c25d66eSEd Tanous } // namespace redfish
173