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