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