Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2020 Intel Corporation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | #include "event_logs_object_type.hpp" |
| 18 | #include "event_service_store.hpp" |
| 19 | #include "filter_expr_parser_ast.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 20 | #include "http_client.hpp" |
| 21 | #include "http_response.hpp" |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 22 | #include "metric_report.hpp" |
| 23 | #include "server_sent_event.hpp" |
| 24 | |
| 25 | #include <boost/asio/io_context.hpp> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 26 | #include <boost/asio/steady_timer.hpp> |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 27 | #include <boost/url/url_view_base.hpp> |
| 28 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 29 | #include <cstdint> |
| 30 | #include <functional> |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 31 | #include <memory> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 32 | #include <optional> |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 33 | #include <string> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 34 | #include <vector> |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 35 | |
| 36 | namespace redfish |
| 37 | { |
| 38 | |
| 39 | static constexpr const char* subscriptionTypeSSE = "SSE"; |
| 40 | |
| 41 | static constexpr const uint8_t maxNoOfSubscriptions = 20; |
| 42 | static constexpr const uint8_t maxNoOfSSESubscriptions = 10; |
Chandramohan Harkude | 81ee0e7 | 2024-12-20 19:22:12 +0530 | [diff] [blame] | 43 | struct TestEvent |
| 44 | { |
| 45 | std::optional<int64_t> eventGroupId; |
Chandramohan Harkude | 81ee0e7 | 2024-12-20 19:22:12 +0530 | [diff] [blame] | 46 | std::optional<std::string> eventTimestamp; |
| 47 | std::optional<std::string> message; |
| 48 | std::optional<std::vector<std::string>> messageArgs; |
| 49 | std::optional<std::string> messageId; |
| 50 | std::optional<std::string> originOfCondition; |
| 51 | std::optional<std::string> resolution; |
| 52 | std::optional<std::string> severity; |
Chandramohan Harkude | 81ee0e7 | 2024-12-20 19:22:12 +0530 | [diff] [blame] | 53 | }; |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 54 | |
| 55 | class Subscription : public std::enable_shared_from_this<Subscription> |
| 56 | { |
| 57 | public: |
| 58 | Subscription(const Subscription&) = delete; |
| 59 | Subscription& operator=(const Subscription&) = delete; |
| 60 | Subscription(Subscription&&) = delete; |
| 61 | Subscription& operator=(Subscription&&) = delete; |
| 62 | |
| 63 | Subscription(std::shared_ptr<persistent_data::UserSubscription> userSubIn, |
| 64 | const boost::urls::url_view_base& url, |
| 65 | boost::asio::io_context& ioc); |
| 66 | |
| 67 | explicit Subscription(crow::sse_socket::Connection& connIn); |
| 68 | |
| 69 | ~Subscription() = default; |
| 70 | |
| 71 | // callback for subscription sendData |
Alexander Hansen | f2656d1 | 2025-01-13 15:14:29 +0100 | [diff] [blame] | 72 | void resHandler(const crow::Response& res); |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 73 | |
Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 74 | void sendHeartbeatEvent(); |
| 75 | void scheduleNextHeartbeatEvent(); |
| 76 | void heartbeatParametersChanged(); |
| 77 | void onHbTimeout(const std::weak_ptr<Subscription>& weakSelf, |
| 78 | const boost::system::error_code& ec); |
| 79 | |
Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame^] | 80 | bool sendEventToSubscriber(uint64_t eventId, std::string&& msg); |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 81 | |
| 82 | void filterAndSendEventLogs( |
Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame^] | 83 | uint64_t eventId, const std::vector<EventLogObjectsType>& eventRecords); |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 84 | |
Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame^] | 85 | void filterAndSendReports(uint64_t eventId, const std::string& reportId, |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 86 | const telemetry::TimestampReadings& var); |
| 87 | |
| 88 | void updateRetryConfig(uint32_t retryAttempts, |
| 89 | uint32_t retryTimeoutInterval); |
| 90 | |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 91 | bool matchSseId(const crow::sse_socket::Connection& thisConn); |
| 92 | |
| 93 | // Check used to indicate what response codes are valid as part of our retry |
| 94 | // policy. 2XX is considered acceptable |
| 95 | static boost::system::error_code retryRespHandler(unsigned int respCode); |
| 96 | |
| 97 | std::shared_ptr<persistent_data::UserSubscription> userSub; |
| 98 | std::function<void()> deleter; |
| 99 | |
| 100 | private: |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 101 | boost::urls::url host; |
| 102 | std::shared_ptr<crow::ConnectionPolicy> policy; |
| 103 | crow::sse_socket::Connection* sseConn = nullptr; |
| 104 | |
| 105 | std::optional<crow::HttpClient> client; |
Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 106 | boost::asio::steady_timer hbTimer; |
Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 107 | |
| 108 | public: |
| 109 | std::optional<filter_ast::LogicalAnd> filter; |
| 110 | }; |
| 111 | |
| 112 | } // namespace redfish |