1 #pragma once
2 
3 #include <app.hpp>
4 #include <event_service_manager.hpp>
5 
6 namespace redfish
7 {
8 
9 inline void createSubscription(crow::sse_socket::Connection& conn)
10 {
11     EventServiceManager& manager =
12         EventServiceManager::getInstance(&conn.getIoContext());
13     if ((manager.getNumberOfSubscriptions() >= maxNoOfSubscriptions) ||
14         manager.getNumberOfSSESubscriptions() >= maxNoOfSSESubscriptions)
15     {
16         BMCWEB_LOG_WARNING("Max SSE subscriptions reached");
17         conn.close("Max SSE subscriptions reached");
18         return;
19     }
20     std::shared_ptr<redfish::Subscription> subValue =
21         std::make_shared<redfish::Subscription>(conn);
22 
23     // GET on this URI means, Its SSE subscriptionType.
24     subValue->subscriptionType = redfish::subscriptionTypeSSE;
25 
26     subValue->protocol = "Redfish";
27     subValue->retryPolicy = "TerminateAfterRetries";
28     subValue->eventFormatType = "Event";
29 
30     std::string id = manager.addSubscription(subValue, false);
31     if (id.empty())
32     {
33         conn.close("Internal Error");
34     }
35 }
36 
37 inline void deleteSubscription(crow::sse_socket::Connection& conn)
38 {
39     redfish::EventServiceManager::getInstance(&conn.getIoContext())
40         .deleteSseSubscription(conn);
41 }
42 
43 inline void requestRoutesEventServiceSse(App& app)
44 {
45     // Note, this endpoint is given the same privilege level as creating a
46     // subscription, because functionally, that's the operation being done
47     BMCWEB_ROUTE(app, "/redfish/v1/EventService/SSE")
48         .privileges(redfish::privileges::postEventDestinationCollection)
49         .serverSentEvent()
50         .onopen(createSubscription)
51         .onclose(deleteSubscription);
52 }
53 } // namespace redfish
54