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