blob: 3cbca3bc8ba60e0d291c20d34f65bd33d8d839af [file] [log] [blame]
AppaRao Puli5e44e3d2021-03-16 15:37:24 +00001#pragma once
2
Ed Tanous5b904292024-04-16 11:10:17 -07003#include "privileges.hpp"
4#include "registries/privilege_registry.hpp"
5
AppaRao Puli5e44e3d2021-03-16 15:37:24 +00006#include <app.hpp>
7#include <event_service_manager.hpp>
8
Ed Tanous5b904292024-04-16 11:10:17 -07009#include <memory>
10#include <string>
11
AppaRao Puli5e44e3d2021-03-16 15:37:24 +000012namespace redfish
13{
14
15inline 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 {
Ed Tanous62598e32023-07-17 17:06:25 -070022 BMCWEB_LOG_WARNING("Max SSE subscriptions reached");
AppaRao Puli5e44e3d2021-03-16 15:37:24 +000023 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
43inline void deleteSubscription(crow::sse_socket::Connection& conn)
44{
45 redfish::EventServiceManager::getInstance(&conn.getIoContext())
46 .deleteSseSubscription(conn);
47}
48
49inline 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