blob: 9c48dbbc6db2299189fbb3e3cef89f99030c15e7 [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"
Ed Tanousd7857202025-01-28 15:32:26 -080020#include "http_client.hpp"
21#include "http_response.hpp"
Alexander Hansen02c1e292024-11-15 14:30:40 +010022#include "metric_report.hpp"
23#include "server_sent_event.hpp"
24
25#include <boost/asio/io_context.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080026#include <boost/asio/steady_timer.hpp>
Alexander Hansen02c1e292024-11-15 14:30:40 +010027#include <boost/url/url_view_base.hpp>
28
Ed Tanousd7857202025-01-28 15:32:26 -080029#include <cstdint>
30#include <functional>
Alexander Hansen02c1e292024-11-15 14:30:40 +010031#include <memory>
Ed Tanousd7857202025-01-28 15:32:26 -080032#include <optional>
Alexander Hansen02c1e292024-11-15 14:30:40 +010033#include <string>
Ed Tanousd7857202025-01-28 15:32:26 -080034#include <vector>
Alexander Hansen02c1e292024-11-15 14:30:40 +010035
36namespace redfish
37{
38
39static constexpr const char* subscriptionTypeSSE = "SSE";
40
41static constexpr const uint8_t maxNoOfSubscriptions = 20;
42static constexpr const uint8_t maxNoOfSSESubscriptions = 10;
Chandramohan Harkude81ee0e72024-12-20 19:22:12 +053043struct TestEvent
44{
45 std::optional<int64_t> eventGroupId;
46 std::optional<std::string> eventId;
47 std::optional<std::string> eventTimestamp;
48 std::optional<std::string> message;
49 std::optional<std::vector<std::string>> messageArgs;
50 std::optional<std::string> messageId;
51 std::optional<std::string> originOfCondition;
52 std::optional<std::string> resolution;
53 std::optional<std::string> severity;
54 // default constructor
55 TestEvent() = default;
56 // default assignment operator
57 TestEvent& operator=(const TestEvent&) = default;
58 // default copy constructor
59 TestEvent(const TestEvent&) = default;
60 // default move constructor
61 TestEvent(TestEvent&&) = default;
62 // default move assignment operator
63 TestEvent& operator=(TestEvent&&) = default;
64 // default destructor
65 ~TestEvent() = default;
66};
Alexander Hansen02c1e292024-11-15 14:30:40 +010067
68class Subscription : public std::enable_shared_from_this<Subscription>
69{
70 public:
71 Subscription(const Subscription&) = delete;
72 Subscription& operator=(const Subscription&) = delete;
73 Subscription(Subscription&&) = delete;
74 Subscription& operator=(Subscription&&) = delete;
75
76 Subscription(std::shared_ptr<persistent_data::UserSubscription> userSubIn,
77 const boost::urls::url_view_base& url,
78 boost::asio::io_context& ioc);
79
80 explicit Subscription(crow::sse_socket::Connection& connIn);
81
82 ~Subscription() = default;
83
84 // callback for subscription sendData
Alexander Hansenf2656d12025-01-13 15:14:29 +010085 void resHandler(const crow::Response& res);
Alexander Hansen02c1e292024-11-15 14:30:40 +010086
Myung Baefb546102024-10-29 10:21:26 -050087 void sendHeartbeatEvent();
88 void scheduleNextHeartbeatEvent();
89 void heartbeatParametersChanged();
90 void onHbTimeout(const std::weak_ptr<Subscription>& weakSelf,
91 const boost::system::error_code& ec);
92
Alexander Hansen02c1e292024-11-15 14:30:40 +010093 bool sendEventToSubscriber(std::string&& msg);
94
Chandramohan Harkude81ee0e72024-12-20 19:22:12 +053095 bool sendTestEventLog(TestEvent& testEvent);
Alexander Hansen02c1e292024-11-15 14:30:40 +010096
97 void filterAndSendEventLogs(
98 const std::vector<EventLogObjectsType>& eventRecords);
99
100 void filterAndSendReports(const std::string& reportId,
101 const telemetry::TimestampReadings& var);
102
103 void updateRetryConfig(uint32_t retryAttempts,
104 uint32_t retryTimeoutInterval);
105
106 uint64_t getEventSeqNum() const;
107
108 bool matchSseId(const crow::sse_socket::Connection& thisConn);
109
110 // Check used to indicate what response codes are valid as part of our retry
111 // policy. 2XX is considered acceptable
112 static boost::system::error_code retryRespHandler(unsigned int respCode);
113
114 std::shared_ptr<persistent_data::UserSubscription> userSub;
115 std::function<void()> deleter;
116
117 private:
118 uint64_t eventSeqNum = 1;
119 boost::urls::url host;
120 std::shared_ptr<crow::ConnectionPolicy> policy;
121 crow::sse_socket::Connection* sseConn = nullptr;
122
123 std::optional<crow::HttpClient> client;
Myung Baefb546102024-10-29 10:21:26 -0500124 boost::asio::steady_timer hbTimer;
Alexander Hansen02c1e292024-11-15 14:30:40 +0100125
126 public:
127 std::optional<filter_ast::LogicalAnd> filter;
128};
129
130} // namespace redfish