AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 5b90429 | 2024-04-16 11:10:17 -0700 | [diff] [blame] | 3 | #include "privileges.hpp" |
| 4 | #include "registries/privilege_registry.hpp" |
| 5 | |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 6 | #include <app.hpp> |
| 7 | #include <event_service_manager.hpp> |
| 8 | |
Ed Tanous | 5b90429 | 2024-04-16 11:10:17 -0700 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <string> |
| 11 | |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 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 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 22 | BMCWEB_LOG_WARNING("Max SSE subscriptions reached"); |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 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 |