xref: /openbmc/bmcweb/features/redfish/lib/redfish_v1.hpp (revision e9dd1d31584b98689ceef4963067e6fc5709b1d3)
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 
4981d523a7SEd Tanous inline void
5081d523a7SEd Tanous     jsonSchemaIndexGet(App& app, const crow::Request& req,
5181d523a7SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
5281d523a7SEd Tanous {
5381d523a7SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
5481d523a7SEd Tanous     {
5581d523a7SEd Tanous         return;
5681d523a7SEd Tanous     }
5781d523a7SEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
5881d523a7SEd Tanous     json["@odata.id"] = "/redfish/v1/JsonSchemas";
5981d523a7SEd Tanous     json["@odata.context"] =
6081d523a7SEd Tanous         "/redfish/v1/$metadata#JsonSchemaFileCollection.JsonSchemaFileCollection";
6181d523a7SEd Tanous     json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection";
6281d523a7SEd Tanous     json["Name"] = "JsonSchemaFile Collection";
6381d523a7SEd Tanous     json["Description"] = "Collection of JsonSchemaFiles";
6481d523a7SEd Tanous     nlohmann::json::array_t members;
6581d523a7SEd Tanous     for (const std::string_view schema : schemas)
6681d523a7SEd Tanous     {
6781d523a7SEd Tanous         nlohmann::json::object_t member;
6881d523a7SEd Tanous         member["@odata.id"] = crow::utility::urlFromPieces(
6981d523a7SEd Tanous             "redfish", "v1", "JsonSchemas", schema);
7081d523a7SEd Tanous         members.push_back(std::move(member));
7181d523a7SEd Tanous     }
7281d523a7SEd Tanous     json["Members"] = std::move(members);
7381d523a7SEd Tanous     json["Members@odata.count"] = schemas.size();
7481d523a7SEd Tanous }
7581d523a7SEd Tanous 
7681d523a7SEd Tanous inline void jsonSchemaGet(App& app, const crow::Request& req,
7781d523a7SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7881d523a7SEd Tanous                           const std::string& schema)
7981d523a7SEd Tanous {
8081d523a7SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
8181d523a7SEd Tanous     {
8281d523a7SEd Tanous         return;
8381d523a7SEd Tanous     }
8481d523a7SEd Tanous 
8581d523a7SEd Tanous     if (std::find(schemas.begin(), schemas.end(), schema) == schemas.end())
8681d523a7SEd Tanous     {
8781d523a7SEd Tanous         messages::resourceNotFound(asyncResp->res,
8881d523a7SEd Tanous                                    "JsonSchemaFile.JsonSchemaFile", schema);
8981d523a7SEd Tanous         return;
9081d523a7SEd Tanous     }
9181d523a7SEd Tanous 
9281d523a7SEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
9381d523a7SEd Tanous     json["@odata.context"] =
9481d523a7SEd Tanous         "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile";
9581d523a7SEd Tanous     json["@odata.id"] =
9681d523a7SEd Tanous         crow::utility::urlFromPieces("redfish", "v1", "JsonSchemas", schema);
9781d523a7SEd Tanous     json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile";
9881d523a7SEd Tanous     json["Name"] = schema + " Schema File";
9981d523a7SEd Tanous     json["Description"] = schema + " Schema File Location";
10081d523a7SEd Tanous     json["Id"] = schema;
10181d523a7SEd Tanous     std::string schemaName = "#";
10281d523a7SEd Tanous     schemaName += schema;
10381d523a7SEd Tanous     schemaName += ".";
10481d523a7SEd Tanous     schemaName += schema;
10581d523a7SEd Tanous     json["Schema"] = std::move(schemaName);
10681d523a7SEd Tanous     constexpr std::array<std::string_view, 1> languages{"en"};
10781d523a7SEd Tanous     json["Languages"] = languages;
10881d523a7SEd Tanous     json["Languages@odata.count"] = languages.size();
10981d523a7SEd Tanous 
11081d523a7SEd Tanous     nlohmann::json::array_t locationArray;
11181d523a7SEd Tanous     nlohmann::json::object_t locationEntry;
11281d523a7SEd Tanous     locationEntry["Language"] = "en";
11381d523a7SEd Tanous     locationEntry["PublicationUri"] =
11481d523a7SEd Tanous         "http://redfish.dmtf.org/schemas/v1/" + schema + ".json";
11581d523a7SEd Tanous     locationEntry["Uri"] = crow::utility::urlFromPieces(
11681d523a7SEd Tanous         "redfish", "v1", "JsonSchemas", schema, std::string(schema) + ".json");
11781d523a7SEd Tanous 
11881d523a7SEd Tanous     locationArray.emplace_back(locationEntry);
11981d523a7SEd Tanous 
12081d523a7SEd Tanous     json["Location"] = std::move(locationArray);
12181d523a7SEd Tanous     json["Location@odata.count"] = 1;
12281d523a7SEd Tanous }
12381d523a7SEd Tanous 
124f65fca6aSEd Tanous inline void requestRoutesRedfish(App& app)
125d3355c5cSEd Tanous {
126d3355c5cSEd Tanous     BMCWEB_ROUTE(app, "/redfish/")
127d3355c5cSEd Tanous         .methods(boost::beast::http::verb::get)(
128d3355c5cSEd Tanous             std::bind_front(redfishGet, std::ref(app)));
1298c623a96SEd Tanous 
13081d523a7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/")
13181d523a7SEd Tanous         .methods(boost::beast::http::verb::get)(
13281d523a7SEd Tanous             std::bind_front(jsonSchemaGet, std::ref(app)));
13381d523a7SEd Tanous 
13481d523a7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/")
13581d523a7SEd Tanous         .methods(boost::beast::http::verb::get)(
13681d523a7SEd Tanous             std::bind_front(jsonSchemaIndexGet, std::ref(app)));
137*e9dd1d31SEd Tanous 
138*e9dd1d31SEd Tanous     // Note, this route must always be registered last
139*e9dd1d31SEd Tanous     BMCWEB_ROUTE(app, "/redfish/<path>")
140*e9dd1d31SEd Tanous     (std::bind_front(redfish404, std::ref(app)));
1414c25d66eSEd Tanous }
142f65fca6aSEd Tanous 
1434c25d66eSEd Tanous } // namespace redfish
144