xref: /openbmc/bmcweb/features/redfish/lib/redfish_v1.hpp (revision 81d523a7eeb2b5f9e066a9055faf436e7086873b)
14c25d66eSEd Tanous #pragma once
24c25d66eSEd Tanous 
3*81d523a7SEd Tanous #include "error_messages.hpp"
4*81d523a7SEd Tanous #include "utility.hpp"
5*81d523a7SEd Tanous 
64c25d66eSEd Tanous #include <app.hpp>
74c25d66eSEd Tanous #include <http_request.hpp>
84c25d66eSEd Tanous #include <http_response.hpp>
9*81d523a7SEd 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*81d523a7SEd Tanous inline void
50*81d523a7SEd Tanous     jsonSchemaIndexGet(App& app, const crow::Request& req,
51*81d523a7SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
52*81d523a7SEd Tanous {
53*81d523a7SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
54*81d523a7SEd Tanous     {
55*81d523a7SEd Tanous         return;
56*81d523a7SEd Tanous     }
57*81d523a7SEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
58*81d523a7SEd Tanous     json["@odata.id"] = "/redfish/v1/JsonSchemas";
59*81d523a7SEd Tanous     json["@odata.context"] =
60*81d523a7SEd Tanous         "/redfish/v1/$metadata#JsonSchemaFileCollection.JsonSchemaFileCollection";
61*81d523a7SEd Tanous     json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection";
62*81d523a7SEd Tanous     json["Name"] = "JsonSchemaFile Collection";
63*81d523a7SEd Tanous     json["Description"] = "Collection of JsonSchemaFiles";
64*81d523a7SEd Tanous     nlohmann::json::array_t members;
65*81d523a7SEd Tanous     for (const std::string_view schema : schemas)
66*81d523a7SEd Tanous     {
67*81d523a7SEd Tanous         nlohmann::json::object_t member;
68*81d523a7SEd Tanous         member["@odata.id"] = crow::utility::urlFromPieces(
69*81d523a7SEd Tanous             "redfish", "v1", "JsonSchemas", schema);
70*81d523a7SEd Tanous         members.push_back(std::move(member));
71*81d523a7SEd Tanous     }
72*81d523a7SEd Tanous     json["Members"] = std::move(members);
73*81d523a7SEd Tanous     json["Members@odata.count"] = schemas.size();
74*81d523a7SEd Tanous }
75*81d523a7SEd Tanous 
76*81d523a7SEd Tanous inline void jsonSchemaGet(App& app, const crow::Request& req,
77*81d523a7SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
78*81d523a7SEd Tanous                           const std::string& schema)
79*81d523a7SEd Tanous {
80*81d523a7SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
81*81d523a7SEd Tanous     {
82*81d523a7SEd Tanous         return;
83*81d523a7SEd Tanous     }
84*81d523a7SEd Tanous 
85*81d523a7SEd Tanous     if (std::find(schemas.begin(), schemas.end(), schema) == schemas.end())
86*81d523a7SEd Tanous     {
87*81d523a7SEd Tanous         messages::resourceNotFound(asyncResp->res,
88*81d523a7SEd Tanous                                    "JsonSchemaFile.JsonSchemaFile", schema);
89*81d523a7SEd Tanous         return;
90*81d523a7SEd Tanous     }
91*81d523a7SEd Tanous 
92*81d523a7SEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
93*81d523a7SEd Tanous     json["@odata.context"] =
94*81d523a7SEd Tanous         "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile";
95*81d523a7SEd Tanous     json["@odata.id"] =
96*81d523a7SEd Tanous         crow::utility::urlFromPieces("redfish", "v1", "JsonSchemas", schema);
97*81d523a7SEd Tanous     json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile";
98*81d523a7SEd Tanous     json["Name"] = schema + " Schema File";
99*81d523a7SEd Tanous     json["Description"] = schema + " Schema File Location";
100*81d523a7SEd Tanous     json["Id"] = schema;
101*81d523a7SEd Tanous     std::string schemaName = "#";
102*81d523a7SEd Tanous     schemaName += schema;
103*81d523a7SEd Tanous     schemaName += ".";
104*81d523a7SEd Tanous     schemaName += schema;
105*81d523a7SEd Tanous     json["Schema"] = std::move(schemaName);
106*81d523a7SEd Tanous     constexpr std::array<std::string_view, 1> languages{"en"};
107*81d523a7SEd Tanous     json["Languages"] = languages;
108*81d523a7SEd Tanous     json["Languages@odata.count"] = languages.size();
109*81d523a7SEd Tanous 
110*81d523a7SEd Tanous     nlohmann::json::array_t locationArray;
111*81d523a7SEd Tanous     nlohmann::json::object_t locationEntry;
112*81d523a7SEd Tanous     locationEntry["Language"] = "en";
113*81d523a7SEd Tanous     locationEntry["PublicationUri"] =
114*81d523a7SEd Tanous         "http://redfish.dmtf.org/schemas/v1/" + schema + ".json";
115*81d523a7SEd Tanous     locationEntry["Uri"] = crow::utility::urlFromPieces(
116*81d523a7SEd Tanous         "redfish", "v1", "JsonSchemas", schema, std::string(schema) + ".json");
117*81d523a7SEd Tanous 
118*81d523a7SEd Tanous     locationArray.emplace_back(locationEntry);
119*81d523a7SEd Tanous 
120*81d523a7SEd Tanous     json["Location"] = std::move(locationArray);
121*81d523a7SEd Tanous     json["Location@odata.count"] = 1;
122*81d523a7SEd Tanous }
123*81d523a7SEd 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 
1308c623a96SEd Tanous     BMCWEB_ROUTE(app, "/redfish/<path>")
1318c623a96SEd Tanous     (std::bind_front(redfish404, std::ref(app)));
132*81d523a7SEd Tanous 
133*81d523a7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/")
134*81d523a7SEd Tanous         .methods(boost::beast::http::verb::get)(
135*81d523a7SEd Tanous             std::bind_front(jsonSchemaGet, std::ref(app)));
136*81d523a7SEd Tanous 
137*81d523a7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/")
138*81d523a7SEd Tanous         .methods(boost::beast::http::verb::get)(
139*81d523a7SEd Tanous             std::bind_front(jsonSchemaIndexGet, std::ref(app)));
1404c25d66eSEd Tanous }
141f65fca6aSEd Tanous 
1424c25d66eSEd Tanous } // namespace redfish
143