xref: /openbmc/bmcweb/features/redfish/lib/eventservice_sse.hpp (revision 5fe4ef35821f946c91d9c20cee01e632d6d3ffd4)
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 
474b712a29SEd Tanous     std::shared_ptr<Subscription> subValue =
484b712a29SEd Tanous         std::make_shared<Subscription>(conn);
495e44e3d8SAppaRao Puli 
50*5fe4ef35SMyung Bae     if (subValue->userSub == nullptr)
51*5fe4ef35SMyung Bae     {
52*5fe4ef35SMyung Bae         BMCWEB_LOG_ERROR("Subscription data is null");
53*5fe4ef35SMyung Bae         conn.close("Internal Error");
54*5fe4ef35SMyung Bae         return;
55*5fe4ef35SMyung Bae     }
565e44e3d8SAppaRao Puli 
57*5fe4ef35SMyung Bae     // GET on this URI means, Its SSE subscriptionType.
58*5fe4ef35SMyung Bae     subValue->userSub->subscriptionType = redfish::subscriptionTypeSSE;
59*5fe4ef35SMyung Bae 
60*5fe4ef35SMyung Bae     subValue->userSub->protocol = "Redfish";
61*5fe4ef35SMyung Bae     subValue->userSub->retryPolicy = "TerminateAfterRetries";
62*5fe4ef35SMyung Bae     subValue->userSub->eventFormatType = "Event";
635e44e3d8SAppaRao Puli 
64f80a87f2SEd Tanous     std::string id = manager.addSSESubscription(subValue, lastEventId);
655e44e3d8SAppaRao Puli     if (id.empty())
665e44e3d8SAppaRao Puli     {
675e44e3d8SAppaRao Puli         conn.close("Internal Error");
685e44e3d8SAppaRao Puli     }
695e44e3d8SAppaRao Puli }
705e44e3d8SAppaRao Puli 
715e44e3d8SAppaRao Puli inline void deleteSubscription(crow::sse_socket::Connection& conn)
725e44e3d8SAppaRao Puli {
735e44e3d8SAppaRao Puli     redfish::EventServiceManager::getInstance(&conn.getIoContext())
745e44e3d8SAppaRao Puli         .deleteSseSubscription(conn);
755e44e3d8SAppaRao Puli }
765e44e3d8SAppaRao Puli 
775e44e3d8SAppaRao Puli inline void requestRoutesEventServiceSse(App& app)
785e44e3d8SAppaRao Puli {
795e44e3d8SAppaRao Puli     // Note, this endpoint is given the same privilege level as creating a
805e44e3d8SAppaRao Puli     // subscription, because functionally, that's the operation being done
815e44e3d8SAppaRao Puli     BMCWEB_ROUTE(app, "/redfish/v1/EventService/SSE")
825e44e3d8SAppaRao Puli         .privileges(redfish::privileges::postEventDestinationCollection)
835e44e3d8SAppaRao Puli         .serverSentEvent()
845e44e3d8SAppaRao Puli         .onopen(createSubscription)
855e44e3d8SAppaRao Puli         .onclose(deleteSubscription);
865e44e3d8SAppaRao Puli }
875e44e3d8SAppaRao Puli } // namespace redfish
88