AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | f80a87f | 2024-06-16 12:10:33 -0700 | [diff] [blame^] | 3 | #include "filter_expr_executor.hpp" |
Ed Tanous | 5b90429 | 2024-04-16 11:10:17 -0700 | [diff] [blame] | 4 | #include "privileges.hpp" |
| 5 | #include "registries/privilege_registry.hpp" |
| 6 | |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 7 | #include <app.hpp> |
| 8 | #include <event_service_manager.hpp> |
| 9 | |
Ed Tanous | 5b90429 | 2024-04-16 11:10:17 -0700 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <string> |
| 12 | |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 13 | namespace redfish |
| 14 | { |
| 15 | |
Ed Tanous | f80a87f | 2024-06-16 12:10:33 -0700 | [diff] [blame^] | 16 | inline void createSubscription(crow::sse_socket::Connection& conn, |
| 17 | const crow::Request& req) |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 18 | { |
| 19 | EventServiceManager& manager = |
| 20 | EventServiceManager::getInstance(&conn.getIoContext()); |
| 21 | if ((manager.getNumberOfSubscriptions() >= maxNoOfSubscriptions) || |
| 22 | manager.getNumberOfSSESubscriptions() >= maxNoOfSSESubscriptions) |
| 23 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 24 | BMCWEB_LOG_WARNING("Max SSE subscriptions reached"); |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 25 | conn.close("Max SSE subscriptions reached"); |
| 26 | return; |
| 27 | } |
Ed Tanous | f80a87f | 2024-06-16 12:10:33 -0700 | [diff] [blame^] | 28 | |
| 29 | std::optional<filter_ast::LogicalAnd> filter; |
| 30 | |
| 31 | boost::urls::params_base::iterator filterIt = |
| 32 | req.url().params().find("$filter"); |
| 33 | |
| 34 | if (filterIt != req.url().params().end()) |
| 35 | { |
| 36 | std::string_view filterValue = (*filterIt).value; |
| 37 | filter = parseFilter(filterValue); |
| 38 | if (!filter) |
| 39 | { |
| 40 | conn.close(std::format("Bad $filter param: {}", filterValue)); |
| 41 | return; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | std::string lastEventId(req.getHeaderValue("Last-Event-Id")); |
| 46 | |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 47 | std::shared_ptr<redfish::Subscription> subValue = |
| 48 | std::make_shared<redfish::Subscription>(conn); |
| 49 | |
| 50 | // GET on this URI means, Its SSE subscriptionType. |
| 51 | subValue->subscriptionType = redfish::subscriptionTypeSSE; |
| 52 | |
| 53 | subValue->protocol = "Redfish"; |
| 54 | subValue->retryPolicy = "TerminateAfterRetries"; |
| 55 | subValue->eventFormatType = "Event"; |
Ed Tanous | f80a87f | 2024-06-16 12:10:33 -0700 | [diff] [blame^] | 56 | subValue->filter = filter; |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 57 | |
Ed Tanous | f80a87f | 2024-06-16 12:10:33 -0700 | [diff] [blame^] | 58 | std::string id = manager.addSSESubscription(subValue, lastEventId); |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 59 | if (id.empty()) |
| 60 | { |
| 61 | conn.close("Internal Error"); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | inline void deleteSubscription(crow::sse_socket::Connection& conn) |
| 66 | { |
| 67 | redfish::EventServiceManager::getInstance(&conn.getIoContext()) |
| 68 | .deleteSseSubscription(conn); |
| 69 | } |
| 70 | |
| 71 | inline void requestRoutesEventServiceSse(App& app) |
| 72 | { |
| 73 | // Note, this endpoint is given the same privilege level as creating a |
| 74 | // subscription, because functionally, that's the operation being done |
| 75 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/SSE") |
| 76 | .privileges(redfish::privileges::postEventDestinationCollection) |
| 77 | .serverSentEvent() |
| 78 | .onopen(createSubscription) |
| 79 | .onclose(deleteSubscription); |
| 80 | } |
| 81 | } // namespace redfish |