xref: /openbmc/bmcweb/redfish-core/include/redfish.hpp (revision 84aad24d55fb0e53128928961ac36242aabb799d)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include "app.hpp"
6 #include "async_resp.hpp"
7 #include "http_request.hpp"
8 #include "redfish_oem_routing.hpp"
9 #include "sub_request.hpp"
10 #include "verb.hpp"
11 
12 #include <memory>
13 
14 namespace redfish
15 {
16 /*
17  * @brief Top level class installing and providing Redfish services
18  */
19 class RedfishService
20 {
21   public:
22     /*
23      * @brief Redfish service constructor
24      *
25      * Loads Redfish configuration and installs schema resources
26      *
27      * @param[in] app   Crow app on which Redfish will initialize
28      */
29     explicit RedfishService(App& app);
30 
31     // Temporary change to make redfish instance available in other places
32     // like query delegation.
getInstance(App & app)33     static RedfishService& getInstance(App& app)
34     {
35         static RedfishService redfish(app);
36         return redfish;
37     }
38 
validate()39     void validate()
40     {
41         oemRouter.validate();
42     }
43 
44     template <StringLiteral Rule>
newRoute(HttpVerb method)45     auto& newRoute(HttpVerb method)
46     {
47         return oemRouter.newRule<Rule>(method);
48     }
49 
handleSubRoute(const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp) const50     void handleSubRoute(
51         const crow::Request& req,
52         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) const
53     {
54         auto subReq = std::make_shared<SubRequest>(req);
55         if (!subReq->needHandling())
56         {
57             return;
58         }
59         oemRouter.handle(subReq, asyncResp);
60     }
61 
62     OemRouter oemRouter;
63 };
64 
65 template <StringLiteral Path>
REDFISH_SUB_ROUTE(RedfishService & service,HttpVerb method)66 auto& REDFISH_SUB_ROUTE(RedfishService& service, HttpVerb method)
67 {
68     return service.newRoute<Path>(method);
69 }
70 
71 } // namespace redfish
72