xref: /openbmc/bmcweb/features/redfish/lib/redfish_v1.hpp (revision 079360ae6e04d3f2245e00d70f83d15c5cad3630)
14c25d66eSEd Tanous #pragma once
24c25d66eSEd Tanous 
381d523a7SEd Tanous #include "error_messages.hpp"
481d523a7SEd Tanous #include "utility.hpp"
581d523a7SEd Tanous 
64c25d66eSEd Tanous #include <app.hpp>
74c25d66eSEd Tanous #include <http_request.hpp>
84c25d66eSEd Tanous #include <http_response.hpp>
981d523a7SEd Tanous #include <schemas.hpp>
104c25d66eSEd Tanous 
114c25d66eSEd Tanous #include <string>
124c25d66eSEd Tanous 
134c25d66eSEd Tanous namespace redfish
144c25d66eSEd Tanous {
154c25d66eSEd Tanous 
16d3355c5cSEd Tanous inline void redfishGet(App& app, const crow::Request& req,
17d3355c5cSEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
184c25d66eSEd Tanous {
193ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
201e925c84SEd Tanous     {
211e925c84SEd Tanous         return;
221e925c84SEd Tanous     }
231476687dSEd Tanous     asyncResp->res.jsonValue["v1"] = "/redfish/v1/";
24d3355c5cSEd Tanous }
25d3355c5cSEd Tanous 
268c623a96SEd Tanous inline void redfish404(App& app, const crow::Request& req,
278c623a96SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
288c623a96SEd Tanous                        const std::string& path)
298c623a96SEd Tanous {
308c623a96SEd Tanous     asyncResp->res.addHeader(boost::beast::http::field::allow, "");
318c623a96SEd Tanous 
328c623a96SEd Tanous     // If we fall to this route, we didn't have a more specific route, so return
338c623a96SEd Tanous     // 404
34686b7093SNan Zhou     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
358c623a96SEd Tanous     {
368c623a96SEd Tanous         return;
378c623a96SEd Tanous     }
388c623a96SEd Tanous 
398c623a96SEd Tanous     BMCWEB_LOG_ERROR << "404 on path " << path;
408c623a96SEd Tanous 
41*079360aeSEd Tanous     std::string name = req.urlView.segments().back();
428c623a96SEd Tanous     // Note, if we hit the wildcard route, we don't know the "type" the user was
438c623a96SEd Tanous     // actually requesting, but giving them a return with an empty string is
448c623a96SEd Tanous     // still better than nothing.
45*079360aeSEd Tanous     messages::resourceNotFound(asyncResp->res, "", name);
468c623a96SEd Tanous }
478c623a96SEd Tanous 
4844c70412SEd Tanous inline void redfish405(App& app, const crow::Request& req,
4944c70412SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5044c70412SEd Tanous                        const std::string& path)
5144c70412SEd Tanous {
5244c70412SEd Tanous     // If we fall to this route, we didn't have a more specific route, so return
5344c70412SEd Tanous     // 405
5444c70412SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
5544c70412SEd Tanous     {
5644c70412SEd Tanous         return;
5744c70412SEd Tanous     }
5844c70412SEd Tanous 
5944c70412SEd Tanous     BMCWEB_LOG_ERROR << "405 on path " << path;
6044c70412SEd Tanous     asyncResp->res.result(boost::beast::http::status::method_not_allowed);
6144c70412SEd Tanous     if (req.method() == boost::beast::http::verb::delete_)
6244c70412SEd Tanous     {
6344c70412SEd Tanous         messages::resourceCannotBeDeleted(asyncResp->res);
6444c70412SEd Tanous     }
6544c70412SEd Tanous     else
6644c70412SEd Tanous     {
6744c70412SEd Tanous         messages::operationNotAllowed(asyncResp->res);
6844c70412SEd Tanous     }
6944c70412SEd Tanous }
7044c70412SEd Tanous 
7181d523a7SEd Tanous inline void
7281d523a7SEd Tanous     jsonSchemaIndexGet(App& app, const crow::Request& req,
7381d523a7SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7481d523a7SEd Tanous {
7581d523a7SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
7681d523a7SEd Tanous     {
7781d523a7SEd Tanous         return;
7881d523a7SEd Tanous     }
7981d523a7SEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
8081d523a7SEd Tanous     json["@odata.id"] = "/redfish/v1/JsonSchemas";
8181d523a7SEd Tanous     json["@odata.context"] =
8281d523a7SEd Tanous         "/redfish/v1/$metadata#JsonSchemaFileCollection.JsonSchemaFileCollection";
8381d523a7SEd Tanous     json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection";
8481d523a7SEd Tanous     json["Name"] = "JsonSchemaFile Collection";
8581d523a7SEd Tanous     json["Description"] = "Collection of JsonSchemaFiles";
8681d523a7SEd Tanous     nlohmann::json::array_t members;
8781d523a7SEd Tanous     for (const std::string_view schema : schemas)
8881d523a7SEd Tanous     {
8981d523a7SEd Tanous         nlohmann::json::object_t member;
9081d523a7SEd Tanous         member["@odata.id"] = crow::utility::urlFromPieces(
9181d523a7SEd Tanous             "redfish", "v1", "JsonSchemas", schema);
9281d523a7SEd Tanous         members.push_back(std::move(member));
9381d523a7SEd Tanous     }
9481d523a7SEd Tanous     json["Members"] = std::move(members);
9581d523a7SEd Tanous     json["Members@odata.count"] = schemas.size();
9681d523a7SEd Tanous }
9781d523a7SEd Tanous 
9881d523a7SEd Tanous inline void jsonSchemaGet(App& app, const crow::Request& req,
9981d523a7SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10081d523a7SEd Tanous                           const std::string& schema)
10181d523a7SEd Tanous {
10281d523a7SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
10381d523a7SEd Tanous     {
10481d523a7SEd Tanous         return;
10581d523a7SEd Tanous     }
10681d523a7SEd Tanous 
10781d523a7SEd Tanous     if (std::find(schemas.begin(), schemas.end(), schema) == schemas.end())
10881d523a7SEd Tanous     {
109d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema);
11081d523a7SEd Tanous         return;
11181d523a7SEd Tanous     }
11281d523a7SEd Tanous 
11381d523a7SEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
11481d523a7SEd Tanous     json["@odata.context"] =
11581d523a7SEd Tanous         "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile";
11681d523a7SEd Tanous     json["@odata.id"] =
11781d523a7SEd Tanous         crow::utility::urlFromPieces("redfish", "v1", "JsonSchemas", schema);
11881d523a7SEd Tanous     json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile";
11981d523a7SEd Tanous     json["Name"] = schema + " Schema File";
12081d523a7SEd Tanous     json["Description"] = schema + " Schema File Location";
12181d523a7SEd Tanous     json["Id"] = schema;
12281d523a7SEd Tanous     std::string schemaName = "#";
12381d523a7SEd Tanous     schemaName += schema;
12481d523a7SEd Tanous     schemaName += ".";
12581d523a7SEd Tanous     schemaName += schema;
12681d523a7SEd Tanous     json["Schema"] = std::move(schemaName);
12781d523a7SEd Tanous     constexpr std::array<std::string_view, 1> languages{"en"};
12881d523a7SEd Tanous     json["Languages"] = languages;
12981d523a7SEd Tanous     json["Languages@odata.count"] = languages.size();
13081d523a7SEd Tanous 
13181d523a7SEd Tanous     nlohmann::json::array_t locationArray;
13281d523a7SEd Tanous     nlohmann::json::object_t locationEntry;
13381d523a7SEd Tanous     locationEntry["Language"] = "en";
13481d523a7SEd Tanous     locationEntry["PublicationUri"] =
13581d523a7SEd Tanous         "http://redfish.dmtf.org/schemas/v1/" + schema + ".json";
13681d523a7SEd Tanous     locationEntry["Uri"] = crow::utility::urlFromPieces(
13781d523a7SEd Tanous         "redfish", "v1", "JsonSchemas", schema, std::string(schema) + ".json");
13881d523a7SEd Tanous 
13981d523a7SEd Tanous     locationArray.emplace_back(locationEntry);
14081d523a7SEd Tanous 
14181d523a7SEd Tanous     json["Location"] = std::move(locationArray);
14281d523a7SEd Tanous     json["Location@odata.count"] = 1;
14381d523a7SEd Tanous }
14481d523a7SEd Tanous 
145f65fca6aSEd Tanous inline void requestRoutesRedfish(App& app)
146d3355c5cSEd Tanous {
147d3355c5cSEd Tanous     BMCWEB_ROUTE(app, "/redfish/")
148d3355c5cSEd Tanous         .methods(boost::beast::http::verb::get)(
149d3355c5cSEd Tanous             std::bind_front(redfishGet, std::ref(app)));
1508c623a96SEd Tanous 
15181d523a7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/")
1520ea4b4e2SEd Tanous         .privileges(redfish::privileges::getJsonSchemaFileCollection)
15381d523a7SEd Tanous         .methods(boost::beast::http::verb::get)(
15481d523a7SEd Tanous             std::bind_front(jsonSchemaGet, std::ref(app)));
15581d523a7SEd Tanous 
15681d523a7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/")
1570ea4b4e2SEd Tanous         .privileges(redfish::privileges::getJsonSchemaFile)
15881d523a7SEd Tanous         .methods(boost::beast::http::verb::get)(
15981d523a7SEd Tanous             std::bind_front(jsonSchemaIndexGet, std::ref(app)));
160e9dd1d31SEd Tanous 
161e9dd1d31SEd Tanous     // Note, this route must always be registered last
162e9dd1d31SEd Tanous     BMCWEB_ROUTE(app, "/redfish/<path>")
1630ea4b4e2SEd Tanous         .notFound()
1640ea4b4e2SEd Tanous         .privileges(redfish::privileges::privilegeSetLogin)(
1650ea4b4e2SEd Tanous             std::bind_front(redfish404, std::ref(app)));
16644c70412SEd Tanous 
16744c70412SEd Tanous     BMCWEB_ROUTE(app, "/redfish/<path>")
1680ea4b4e2SEd Tanous         .methodNotAllowed()
1690ea4b4e2SEd Tanous         .privileges(redfish::privileges::privilegeSetLogin)(
1700ea4b4e2SEd Tanous             std::bind_front(redfish405, std::ref(app)));
1714c25d66eSEd Tanous }
172f65fca6aSEd Tanous 
1734c25d66eSEd Tanous } // namespace redfish
174