xref: /openbmc/bmcweb/features/redfish/lib/eventservice_sse.hpp (revision 4b712a29debc1a0860cc04850b262203cad402a5)
15e44e3d8SAppaRao Puli #pragma once
25e44e3d8SAppaRao Puli 
3f80a87f2SEd Tanous #include "filter_expr_executor.hpp"
45b90429aSEd Tanous #include "privileges.hpp"
55b90429aSEd Tanous #include "registries/privilege_registry.hpp"
65b90429aSEd Tanous 
75e44e3d8SAppaRao Puli #include <app.hpp>
85e44e3d8SAppaRao Puli #include <event_service_manager.hpp>
95e44e3d8SAppaRao Puli 
105b90429aSEd Tanous #include <memory>
115b90429aSEd Tanous #include <string>
125b90429aSEd Tanous 
135e44e3d8SAppaRao Puli namespace redfish
145e44e3d8SAppaRao Puli {
155e44e3d8SAppaRao Puli 
16f80a87f2SEd Tanous inline void createSubscription(crow::sse_socket::Connection& conn,
17f80a87f2SEd Tanous                                const crow::Request& req)
185e44e3d8SAppaRao Puli {
195e44e3d8SAppaRao Puli     EventServiceManager& manager =
205e44e3d8SAppaRao Puli         EventServiceManager::getInstance(&conn.getIoContext());
215e44e3d8SAppaRao Puli     if ((manager.getNumberOfSubscriptions() >= maxNoOfSubscriptions) ||
225e44e3d8SAppaRao Puli         manager.getNumberOfSSESubscriptions() >= maxNoOfSSESubscriptions)
235e44e3d8SAppaRao Puli     {
2462598e31SEd Tanous         BMCWEB_LOG_WARNING("Max SSE subscriptions reached");
255e44e3d8SAppaRao Puli         conn.close("Max SSE subscriptions reached");
265e44e3d8SAppaRao Puli         return;
275e44e3d8SAppaRao Puli     }
28f80a87f2SEd Tanous 
29f80a87f2SEd Tanous     std::optional<filter_ast::LogicalAnd> filter;
30f80a87f2SEd Tanous 
31f80a87f2SEd Tanous     boost::urls::params_base::iterator filterIt =
32f80a87f2SEd Tanous         req.url().params().find("$filter");
33f80a87f2SEd Tanous 
34f80a87f2SEd Tanous     if (filterIt != req.url().params().end())
35f80a87f2SEd Tanous     {
36f80a87f2SEd Tanous         std::string_view filterValue = (*filterIt).value;
37f80a87f2SEd Tanous         filter = parseFilter(filterValue);
38f80a87f2SEd Tanous         if (!filter)
39f80a87f2SEd Tanous         {
40f80a87f2SEd Tanous             conn.close(std::format("Bad $filter param: {}", filterValue));
41f80a87f2SEd Tanous             return;
42f80a87f2SEd Tanous         }
43f80a87f2SEd Tanous     }
44f80a87f2SEd Tanous 
45f80a87f2SEd Tanous     std::string lastEventId(req.getHeaderValue("Last-Event-Id"));
46f80a87f2SEd Tanous 
47*4b712a29SEd Tanous     std::shared_ptr<Subscription> subValue =
48*4b712a29SEd Tanous         std::make_shared<Subscription>(conn);
495e44e3d8SAppaRao Puli 
505e44e3d8SAppaRao Puli     // GET on this URI means, Its SSE subscriptionType.
51*4b712a29SEd Tanous     subValue->userSub.subscriptionType = redfish::subscriptionTypeSSE;
525e44e3d8SAppaRao Puli 
53*4b712a29SEd Tanous     subValue->userSub.protocol = "Redfish";
54*4b712a29SEd Tanous     subValue->userSub.retryPolicy = "TerminateAfterRetries";
55*4b712a29SEd Tanous     subValue->userSub.eventFormatType = "Event";
565e44e3d8SAppaRao Puli 
57f80a87f2SEd Tanous     std::string id = manager.addSSESubscription(subValue, lastEventId);
585e44e3d8SAppaRao Puli     if (id.empty())
595e44e3d8SAppaRao Puli     {
605e44e3d8SAppaRao Puli         conn.close("Internal Error");
615e44e3d8SAppaRao Puli     }
625e44e3d8SAppaRao Puli }
635e44e3d8SAppaRao Puli 
645e44e3d8SAppaRao Puli inline void deleteSubscription(crow::sse_socket::Connection& conn)
655e44e3d8SAppaRao Puli {
665e44e3d8SAppaRao Puli     redfish::EventServiceManager::getInstance(&conn.getIoContext())
675e44e3d8SAppaRao Puli         .deleteSseSubscription(conn);
685e44e3d8SAppaRao Puli }
695e44e3d8SAppaRao Puli 
705e44e3d8SAppaRao Puli inline void requestRoutesEventServiceSse(App& app)
715e44e3d8SAppaRao Puli {
725e44e3d8SAppaRao Puli     // Note, this endpoint is given the same privilege level as creating a
735e44e3d8SAppaRao Puli     // subscription, because functionally, that's the operation being done
745e44e3d8SAppaRao Puli     BMCWEB_ROUTE(app, "/redfish/v1/EventService/SSE")
755e44e3d8SAppaRao Puli         .privileges(redfish::privileges::postEventDestinationCollection)
765e44e3d8SAppaRao Puli         .serverSentEvent()
775e44e3d8SAppaRao Puli         .onopen(createSubscription)
785e44e3d8SAppaRao Puli         .onclose(deleteSubscription);
795e44e3d8SAppaRao Puli }
805e44e3d8SAppaRao Puli } // namespace redfish
81