xref: /openbmc/bmcweb/features/redfish/lib/redfish_v1.hpp (revision 44c70412e763a63310ef9451f24714c4decef91a)
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 
418c623a96SEd Tanous     boost::urls::string_value name = req.urlView.segments().back();
428c623a96SEd Tanous     std::string_view nameStr(name.data(), name.size());
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.
468c623a96SEd Tanous     messages::resourceNotFound(asyncResp->res, "", nameStr);
478c623a96SEd Tanous }
488c623a96SEd Tanous 
49*44c70412SEd Tanous inline void redfish405(App& app, const crow::Request& req,
50*44c70412SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
51*44c70412SEd Tanous                        const std::string& path)
52*44c70412SEd Tanous {
53*44c70412SEd Tanous     // If we fall to this route, we didn't have a more specific route, so return
54*44c70412SEd Tanous     // 405
55*44c70412SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
56*44c70412SEd Tanous     {
57*44c70412SEd Tanous         return;
58*44c70412SEd Tanous     }
59*44c70412SEd Tanous 
60*44c70412SEd Tanous     BMCWEB_LOG_ERROR << "405 on path " << path;
61*44c70412SEd Tanous     asyncResp->res.result(boost::beast::http::status::method_not_allowed);
62*44c70412SEd Tanous     if (req.method() == boost::beast::http::verb::delete_)
63*44c70412SEd Tanous     {
64*44c70412SEd Tanous         messages::resourceCannotBeDeleted(asyncResp->res);
65*44c70412SEd Tanous     }
66*44c70412SEd Tanous     else
67*44c70412SEd Tanous     {
68*44c70412SEd Tanous         messages::operationNotAllowed(asyncResp->res);
69*44c70412SEd Tanous     }
70*44c70412SEd Tanous }
71*44c70412SEd 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.context"] =
8381d523a7SEd Tanous         "/redfish/v1/$metadata#JsonSchemaFileCollection.JsonSchemaFileCollection";
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;
8881d523a7SEd Tanous     for (const std::string_view schema : schemas)
8981d523a7SEd Tanous     {
9081d523a7SEd Tanous         nlohmann::json::object_t member;
9181d523a7SEd Tanous         member["@odata.id"] = crow::utility::urlFromPieces(
9281d523a7SEd Tanous             "redfish", "v1", "JsonSchemas", schema);
9381d523a7SEd Tanous         members.push_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     {
11081d523a7SEd Tanous         messages::resourceNotFound(asyncResp->res,
11181d523a7SEd Tanous                                    "JsonSchemaFile.JsonSchemaFile", schema);
11281d523a7SEd Tanous         return;
11381d523a7SEd Tanous     }
11481d523a7SEd Tanous 
11581d523a7SEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
11681d523a7SEd Tanous     json["@odata.context"] =
11781d523a7SEd Tanous         "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile";
11881d523a7SEd Tanous     json["@odata.id"] =
11981d523a7SEd Tanous         crow::utility::urlFromPieces("redfish", "v1", "JsonSchemas", schema);
12081d523a7SEd Tanous     json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile";
12181d523a7SEd Tanous     json["Name"] = schema + " Schema File";
12281d523a7SEd Tanous     json["Description"] = schema + " Schema File Location";
12381d523a7SEd Tanous     json["Id"] = schema;
12481d523a7SEd Tanous     std::string schemaName = "#";
12581d523a7SEd Tanous     schemaName += schema;
12681d523a7SEd Tanous     schemaName += ".";
12781d523a7SEd Tanous     schemaName += schema;
12881d523a7SEd Tanous     json["Schema"] = std::move(schemaName);
12981d523a7SEd Tanous     constexpr std::array<std::string_view, 1> languages{"en"};
13081d523a7SEd Tanous     json["Languages"] = languages;
13181d523a7SEd Tanous     json["Languages@odata.count"] = languages.size();
13281d523a7SEd Tanous 
13381d523a7SEd Tanous     nlohmann::json::array_t locationArray;
13481d523a7SEd Tanous     nlohmann::json::object_t locationEntry;
13581d523a7SEd Tanous     locationEntry["Language"] = "en";
13681d523a7SEd Tanous     locationEntry["PublicationUri"] =
13781d523a7SEd Tanous         "http://redfish.dmtf.org/schemas/v1/" + schema + ".json";
13881d523a7SEd Tanous     locationEntry["Uri"] = crow::utility::urlFromPieces(
13981d523a7SEd Tanous         "redfish", "v1", "JsonSchemas", schema, std::string(schema) + ".json");
14081d523a7SEd Tanous 
14181d523a7SEd Tanous     locationArray.emplace_back(locationEntry);
14281d523a7SEd Tanous 
14381d523a7SEd Tanous     json["Location"] = std::move(locationArray);
14481d523a7SEd Tanous     json["Location@odata.count"] = 1;
14581d523a7SEd Tanous }
14681d523a7SEd Tanous 
147f65fca6aSEd Tanous inline void requestRoutesRedfish(App& app)
148d3355c5cSEd Tanous {
149d3355c5cSEd Tanous     BMCWEB_ROUTE(app, "/redfish/")
150d3355c5cSEd Tanous         .methods(boost::beast::http::verb::get)(
151d3355c5cSEd Tanous             std::bind_front(redfishGet, std::ref(app)));
1528c623a96SEd Tanous 
15381d523a7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/")
15481d523a7SEd Tanous         .methods(boost::beast::http::verb::get)(
15581d523a7SEd Tanous             std::bind_front(jsonSchemaGet, std::ref(app)));
15681d523a7SEd Tanous 
15781d523a7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/")
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>")
16344e4518bSEd Tanous         .notFound()(std::bind_front(redfish404, std::ref(app)));
164*44c70412SEd Tanous 
165*44c70412SEd Tanous     BMCWEB_ROUTE(app, "/redfish/<path>")
166*44c70412SEd Tanous         .methodNotAllowed()(std::bind_front(redfish405, std::ref(app)));
1674c25d66eSEd Tanous }
168f65fca6aSEd Tanous 
1694c25d66eSEd Tanous } // namespace redfish
170