xref: /openbmc/bmcweb/features/redfish/lib/redfish_v1.hpp (revision 3ccb3adb9a14783f6bef601506de9f8bcae22d51)
14c25d66eSEd Tanous #pragma once
24c25d66eSEd Tanous 
3*3ccb3adbSEd Tanous #include "app.hpp"
481d523a7SEd Tanous #include "error_messages.hpp"
5*3ccb3adbSEd Tanous #include "http_request.hpp"
6*3ccb3adbSEd Tanous #include "http_response.hpp"
7*3ccb3adbSEd Tanous #include "query.hpp"
8*3ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
9*3ccb3adbSEd Tanous #include "schemas.hpp"
1081d523a7SEd Tanous #include "utility.hpp"
1181d523a7SEd Tanous 
124c25d66eSEd Tanous #include <string>
134c25d66eSEd Tanous 
144c25d66eSEd Tanous namespace redfish
154c25d66eSEd Tanous {
164c25d66eSEd Tanous 
17d3355c5cSEd Tanous inline void redfishGet(App& app, const crow::Request& req,
18d3355c5cSEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
194c25d66eSEd Tanous {
203ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
211e925c84SEd Tanous     {
221e925c84SEd Tanous         return;
231e925c84SEd Tanous     }
241476687dSEd Tanous     asyncResp->res.jsonValue["v1"] = "/redfish/v1/";
25d3355c5cSEd Tanous }
26d3355c5cSEd Tanous 
278c623a96SEd Tanous inline void redfish404(App& app, const crow::Request& req,
288c623a96SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
298c623a96SEd Tanous                        const std::string& path)
308c623a96SEd Tanous {
318c623a96SEd Tanous     asyncResp->res.addHeader(boost::beast::http::field::allow, "");
328c623a96SEd Tanous 
338c623a96SEd Tanous     // If we fall to this route, we didn't have a more specific route, so return
348c623a96SEd Tanous     // 404
35686b7093SNan Zhou     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
368c623a96SEd Tanous     {
378c623a96SEd Tanous         return;
388c623a96SEd Tanous     }
398c623a96SEd Tanous 
408c623a96SEd Tanous     BMCWEB_LOG_ERROR << "404 on path " << path;
418c623a96SEd Tanous 
42079360aeSEd Tanous     std::string name = req.urlView.segments().back();
438c623a96SEd Tanous     // Note, if we hit the wildcard route, we don't know the "type" the user was
448c623a96SEd Tanous     // actually requesting, but giving them a return with an empty string is
458c623a96SEd Tanous     // still better than nothing.
46079360aeSEd Tanous     messages::resourceNotFound(asyncResp->res, "", name);
478c623a96SEd Tanous }
488c623a96SEd Tanous 
4944c70412SEd Tanous inline void redfish405(App& app, const crow::Request& req,
5044c70412SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5144c70412SEd Tanous                        const std::string& path)
5244c70412SEd Tanous {
5344c70412SEd Tanous     // If we fall to this route, we didn't have a more specific route, so return
5444c70412SEd Tanous     // 405
5544c70412SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
5644c70412SEd Tanous     {
5744c70412SEd Tanous         return;
5844c70412SEd Tanous     }
5944c70412SEd Tanous 
6044c70412SEd Tanous     BMCWEB_LOG_ERROR << "405 on path " << path;
6144c70412SEd Tanous     asyncResp->res.result(boost::beast::http::status::method_not_allowed);
6244c70412SEd Tanous     if (req.method() == boost::beast::http::verb::delete_)
6344c70412SEd Tanous     {
6444c70412SEd Tanous         messages::resourceCannotBeDeleted(asyncResp->res);
6544c70412SEd Tanous     }
6644c70412SEd Tanous     else
6744c70412SEd Tanous     {
6844c70412SEd Tanous         messages::operationNotAllowed(asyncResp->res);
6944c70412SEd Tanous     }
7044c70412SEd Tanous }
7144c70412SEd Tanous 
7281d523a7SEd Tanous inline void
7381d523a7SEd Tanous     jsonSchemaIndexGet(App& app, const crow::Request& req,
7481d523a7SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7581d523a7SEd Tanous {
7681d523a7SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
7781d523a7SEd Tanous     {
7881d523a7SEd Tanous         return;
7981d523a7SEd Tanous     }
8081d523a7SEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
8181d523a7SEd Tanous     json["@odata.id"] = "/redfish/v1/JsonSchemas";
8281d523a7SEd Tanous     json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection";
8381d523a7SEd Tanous     json["Name"] = "JsonSchemaFile Collection";
8481d523a7SEd Tanous     json["Description"] = "Collection of JsonSchemaFiles";
8581d523a7SEd Tanous     nlohmann::json::array_t members;
8681d523a7SEd Tanous     for (const std::string_view schema : schemas)
8781d523a7SEd Tanous     {
8881d523a7SEd Tanous         nlohmann::json::object_t member;
8981d523a7SEd Tanous         member["@odata.id"] = crow::utility::urlFromPieces(
9081d523a7SEd Tanous             "redfish", "v1", "JsonSchemas", schema);
9181d523a7SEd Tanous         members.push_back(std::move(member));
9281d523a7SEd Tanous     }
9381d523a7SEd Tanous     json["Members"] = std::move(members);
9481d523a7SEd Tanous     json["Members@odata.count"] = schemas.size();
9581d523a7SEd Tanous }
9681d523a7SEd Tanous 
9781d523a7SEd Tanous inline void jsonSchemaGet(App& app, const crow::Request& req,
9881d523a7SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9981d523a7SEd Tanous                           const std::string& schema)
10081d523a7SEd Tanous {
10181d523a7SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
10281d523a7SEd Tanous     {
10381d523a7SEd Tanous         return;
10481d523a7SEd Tanous     }
10581d523a7SEd Tanous 
10681d523a7SEd Tanous     if (std::find(schemas.begin(), schemas.end(), schema) == schemas.end())
10781d523a7SEd Tanous     {
108d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema);
10981d523a7SEd Tanous         return;
11081d523a7SEd Tanous     }
11181d523a7SEd Tanous 
11281d523a7SEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
11381d523a7SEd Tanous     json["@odata.id"] =
11481d523a7SEd Tanous         crow::utility::urlFromPieces("redfish", "v1", "JsonSchemas", schema);
11581d523a7SEd Tanous     json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile";
11681d523a7SEd Tanous     json["Name"] = schema + " Schema File";
11781d523a7SEd Tanous     json["Description"] = schema + " Schema File Location";
11881d523a7SEd Tanous     json["Id"] = schema;
11981d523a7SEd Tanous     std::string schemaName = "#";
12081d523a7SEd Tanous     schemaName += schema;
12181d523a7SEd Tanous     schemaName += ".";
12281d523a7SEd Tanous     schemaName += schema;
12381d523a7SEd Tanous     json["Schema"] = std::move(schemaName);
12481d523a7SEd Tanous     constexpr std::array<std::string_view, 1> languages{"en"};
12581d523a7SEd Tanous     json["Languages"] = languages;
12681d523a7SEd Tanous     json["Languages@odata.count"] = languages.size();
12781d523a7SEd Tanous 
12881d523a7SEd Tanous     nlohmann::json::array_t locationArray;
12981d523a7SEd Tanous     nlohmann::json::object_t locationEntry;
13081d523a7SEd Tanous     locationEntry["Language"] = "en";
13181d523a7SEd Tanous     locationEntry["PublicationUri"] =
13281d523a7SEd Tanous         "http://redfish.dmtf.org/schemas/v1/" + schema + ".json";
13381d523a7SEd Tanous     locationEntry["Uri"] = crow::utility::urlFromPieces(
13481d523a7SEd Tanous         "redfish", "v1", "JsonSchemas", schema, std::string(schema) + ".json");
13581d523a7SEd Tanous 
13681d523a7SEd Tanous     locationArray.emplace_back(locationEntry);
13781d523a7SEd Tanous 
13881d523a7SEd Tanous     json["Location"] = std::move(locationArray);
13981d523a7SEd Tanous     json["Location@odata.count"] = 1;
14081d523a7SEd Tanous }
14181d523a7SEd Tanous 
142f65fca6aSEd Tanous inline void requestRoutesRedfish(App& app)
143d3355c5cSEd Tanous {
144d3355c5cSEd Tanous     BMCWEB_ROUTE(app, "/redfish/")
145d3355c5cSEd Tanous         .methods(boost::beast::http::verb::get)(
146d3355c5cSEd Tanous             std::bind_front(redfishGet, std::ref(app)));
1478c623a96SEd Tanous 
14881d523a7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/")
1490ea4b4e2SEd Tanous         .privileges(redfish::privileges::getJsonSchemaFileCollection)
15081d523a7SEd Tanous         .methods(boost::beast::http::verb::get)(
15181d523a7SEd Tanous             std::bind_front(jsonSchemaGet, std::ref(app)));
15281d523a7SEd Tanous 
15381d523a7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/")
1540ea4b4e2SEd Tanous         .privileges(redfish::privileges::getJsonSchemaFile)
15581d523a7SEd Tanous         .methods(boost::beast::http::verb::get)(
15681d523a7SEd Tanous             std::bind_front(jsonSchemaIndexGet, std::ref(app)));
157e9dd1d31SEd Tanous 
158e9dd1d31SEd Tanous     // Note, this route must always be registered last
159e9dd1d31SEd Tanous     BMCWEB_ROUTE(app, "/redfish/<path>")
1600ea4b4e2SEd Tanous         .notFound()
1610ea4b4e2SEd Tanous         .privileges(redfish::privileges::privilegeSetLogin)(
1620ea4b4e2SEd Tanous             std::bind_front(redfish404, std::ref(app)));
16344c70412SEd Tanous 
16444c70412SEd Tanous     BMCWEB_ROUTE(app, "/redfish/<path>")
1650ea4b4e2SEd Tanous         .methodNotAllowed()
1660ea4b4e2SEd Tanous         .privileges(redfish::privileges::privilegeSetLogin)(
1670ea4b4e2SEd Tanous             std::bind_front(redfish405, std::ref(app)));
1684c25d66eSEd Tanous }
169f65fca6aSEd Tanous 
1704c25d66eSEd Tanous } // namespace redfish
171