blob: c0a9527e94b3ea23f28418fed81a91b46fe591f0 [file] [log] [blame]
AppaRao Puli5e44e3d2021-03-16 15:37:24 +00001#pragma once
2
Ed Tanousf80a87f2024-06-16 12:10:33 -07003#include "filter_expr_executor.hpp"
Ed Tanous5b904292024-04-16 11:10:17 -07004#include "privileges.hpp"
5#include "registries/privilege_registry.hpp"
6
AppaRao Puli5e44e3d2021-03-16 15:37:24 +00007#include <app.hpp>
8#include <event_service_manager.hpp>
9
Ed Tanous5b904292024-04-16 11:10:17 -070010#include <memory>
11#include <string>
12
AppaRao Puli5e44e3d2021-03-16 15:37:24 +000013namespace redfish
14{
15
Ed Tanousf80a87f2024-06-16 12:10:33 -070016inline void createSubscription(crow::sse_socket::Connection& conn,
17 const crow::Request& req)
AppaRao Puli5e44e3d2021-03-16 15:37:24 +000018{
19 EventServiceManager& manager =
20 EventServiceManager::getInstance(&conn.getIoContext());
21 if ((manager.getNumberOfSubscriptions() >= maxNoOfSubscriptions) ||
22 manager.getNumberOfSSESubscriptions() >= maxNoOfSSESubscriptions)
23 {
Ed Tanous62598e32023-07-17 17:06:25 -070024 BMCWEB_LOG_WARNING("Max SSE subscriptions reached");
AppaRao Puli5e44e3d2021-03-16 15:37:24 +000025 conn.close("Max SSE subscriptions reached");
26 return;
27 }
Ed Tanousf80a87f2024-06-16 12:10:33 -070028
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 Puli5e44e3d2021-03-16 15:37:24 +000047 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 Tanousf80a87f2024-06-16 12:10:33 -070056 subValue->filter = filter;
AppaRao Puli5e44e3d2021-03-16 15:37:24 +000057
Ed Tanousf80a87f2024-06-16 12:10:33 -070058 std::string id = manager.addSSESubscription(subValue, lastEventId);
AppaRao Puli5e44e3d2021-03-16 15:37:24 +000059 if (id.empty())
60 {
61 conn.close("Internal Error");
62 }
63}
64
65inline void deleteSubscription(crow::sse_socket::Connection& conn)
66{
67 redfish::EventServiceManager::getInstance(&conn.getIoContext())
68 .deleteSseSubscription(conn);
69}
70
71inline 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