blob: 4ad29a6707e30fbe33c59cadd75ab78b2fd22ebd [file] [log] [blame]
AppaRao Puli5e44e3d2021-03-16 15:37:24 +00001#pragma once
2
3#include <app.hpp>
4#include <event_service_manager.hpp>
5
6namespace redfish
7{
8
9inline 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 {
Ed Tanous62598e32023-07-17 17:06:25 -070016 BMCWEB_LOG_WARNING("Max SSE subscriptions reached");
AppaRao Puli5e44e3d2021-03-16 15:37:24 +000017 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
37inline void deleteSubscription(crow::sse_socket::Connection& conn)
38{
39 redfish::EventServiceManager::getInstance(&conn.getIoContext())
40 .deleteSseSubscription(conn);
41}
42
43inline 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