Gunnar Mills | 5ffbc9a | 2025-04-24 08:41:49 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
| 3 | // SPDX-FileCopyrightText: Copyright 2020 Intel Corporation |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 4 | #pragma once |
Gunnar Mills | 5ffbc9a | 2025-04-24 08:41:49 -0500 | [diff] [blame] | 5 | |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 6 | #include "event_logs_object_type.hpp" |
| 7 | #include "event_service_store.hpp" |
| 8 | #include "filter_expr_parser_ast.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 9 | #include "http_client.hpp" |
| 10 | #include "http_response.hpp" |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 11 | #include "metric_report.hpp" |
| 12 | #include "server_sent_event.hpp" |
| 13 | |
| 14 | #include <boost/asio/io_context.hpp> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 15 | #include <boost/asio/steady_timer.hpp> |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 16 | #include <boost/url/url_view_base.hpp> |
| 17 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 18 | #include <cstdint> |
| 19 | #include <functional> |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 20 | #include <memory> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 21 | #include <optional> |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 22 | #include <string> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 23 | #include <vector> |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 24 | |
| 25 | namespace redfish |
| 26 | { |
| 27 | |
| 28 | static constexpr const char* subscriptionTypeSSE = "SSE"; |
| 29 | |
| 30 | static constexpr const uint8_t maxNoOfSubscriptions = 20; |
| 31 | static constexpr const uint8_t maxNoOfSSESubscriptions = 10; |
Chandramohan Harkude | 81ee0e7 | 2024-12-20 19:22:12 +0530 | [diff] [blame] | 32 | struct TestEvent |
| 33 | { |
| 34 | std::optional<int64_t> eventGroupId; |
Chandramohan Harkude | 81ee0e7 | 2024-12-20 19:22:12 +0530 | [diff] [blame] | 35 | std::optional<std::string> eventTimestamp; |
| 36 | std::optional<std::string> message; |
| 37 | std::optional<std::vector<std::string>> messageArgs; |
| 38 | std::optional<std::string> messageId; |
| 39 | std::optional<std::string> originOfCondition; |
| 40 | std::optional<std::string> resolution; |
| 41 | std::optional<std::string> severity; |
Chandramohan Harkude | 81ee0e7 | 2024-12-20 19:22:12 +0530 | [diff] [blame] | 42 | }; |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 43 | |
| 44 | class Subscription : public std::enable_shared_from_this<Subscription> |
| 45 | { |
| 46 | public: |
| 47 | Subscription(const Subscription&) = delete; |
| 48 | Subscription& operator=(const Subscription&) = delete; |
| 49 | Subscription(Subscription&&) = delete; |
| 50 | Subscription& operator=(Subscription&&) = delete; |
| 51 | |
| 52 | Subscription(std::shared_ptr<persistent_data::UserSubscription> userSubIn, |
| 53 | const boost::urls::url_view_base& url, |
| 54 | boost::asio::io_context& ioc); |
| 55 | |
| 56 | explicit Subscription(crow::sse_socket::Connection& connIn); |
| 57 | |
| 58 | ~Subscription() = default; |
| 59 | |
| 60 | // callback for subscription sendData |
Myung Bae | 99ff0dd | 2025-04-22 14:38:36 -0500 | [diff] [blame^] | 61 | void resHandler(const std::shared_ptr<Subscription>& /*self*/, |
| 62 | const crow::Response& res); |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 63 | |
Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 64 | void sendHeartbeatEvent(); |
| 65 | void scheduleNextHeartbeatEvent(); |
| 66 | void heartbeatParametersChanged(); |
| 67 | void onHbTimeout(const std::weak_ptr<Subscription>& weakSelf, |
| 68 | const boost::system::error_code& ec); |
| 69 | |
Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 70 | bool sendEventToSubscriber(uint64_t eventId, std::string&& msg); |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 71 | |
| 72 | void filterAndSendEventLogs( |
Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 73 | uint64_t eventId, const std::vector<EventLogObjectsType>& eventRecords); |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 74 | |
Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 75 | void filterAndSendReports(uint64_t eventId, const std::string& reportId, |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 76 | const telemetry::TimestampReadings& var); |
| 77 | |
| 78 | void updateRetryConfig(uint32_t retryAttempts, |
| 79 | uint32_t retryTimeoutInterval); |
| 80 | |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 81 | bool matchSseId(const crow::sse_socket::Connection& thisConn); |
| 82 | |
| 83 | // Check used to indicate what response codes are valid as part of our retry |
| 84 | // policy. 2XX is considered acceptable |
| 85 | static boost::system::error_code retryRespHandler(unsigned int respCode); |
| 86 | |
| 87 | std::shared_ptr<persistent_data::UserSubscription> userSub; |
| 88 | std::function<void()> deleter; |
| 89 | |
| 90 | private: |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 91 | boost::urls::url host; |
| 92 | std::shared_ptr<crow::ConnectionPolicy> policy; |
| 93 | crow::sse_socket::Connection* sseConn = nullptr; |
| 94 | |
Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 95 | boost::asio::steady_timer hbTimer; |
Myung Bae | 99ff0dd | 2025-04-22 14:38:36 -0500 | [diff] [blame^] | 96 | std::optional<crow::HttpClient> client; |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 97 | |
| 98 | public: |
| 99 | std::optional<filter_ast::LogicalAnd> filter; |
| 100 | }; |
| 101 | |
| 102 | } // namespace redfish |