blob: 7fe2fdc202216e5bfc189038ab202e2ba9b14c63 [file] [log] [blame]
Gunnar Mills5ffbc9a2025-04-24 08:41:49 -05001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3// SPDX-FileCopyrightText: Copyright 2020 Intel Corporation
Alexander Hansen02c1e292024-11-15 14:30:40 +01004#pragma once
Gunnar Mills5ffbc9a2025-04-24 08:41:49 -05005
Alexander Hansen02c1e292024-11-15 14:30:40 +01006#include "event_logs_object_type.hpp"
7#include "event_service_store.hpp"
8#include "filter_expr_parser_ast.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08009#include "http_client.hpp"
10#include "http_response.hpp"
Alexander Hansen02c1e292024-11-15 14:30:40 +010011#include "metric_report.hpp"
12#include "server_sent_event.hpp"
13
14#include <boost/asio/io_context.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080015#include <boost/asio/steady_timer.hpp>
Alexander Hansen02c1e292024-11-15 14:30:40 +010016#include <boost/url/url_view_base.hpp>
17
Ed Tanousd7857202025-01-28 15:32:26 -080018#include <cstdint>
19#include <functional>
Alexander Hansen02c1e292024-11-15 14:30:40 +010020#include <memory>
Ed Tanousd7857202025-01-28 15:32:26 -080021#include <optional>
Alexander Hansen02c1e292024-11-15 14:30:40 +010022#include <string>
Ed Tanousd7857202025-01-28 15:32:26 -080023#include <vector>
Alexander Hansen02c1e292024-11-15 14:30:40 +010024
25namespace redfish
26{
27
28static constexpr const char* subscriptionTypeSSE = "SSE";
29
30static constexpr const uint8_t maxNoOfSubscriptions = 20;
31static constexpr const uint8_t maxNoOfSSESubscriptions = 10;
Chandramohan Harkude81ee0e72024-12-20 19:22:12 +053032struct TestEvent
33{
34 std::optional<int64_t> eventGroupId;
Chandramohan Harkude81ee0e72024-12-20 19:22:12 +053035 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 Harkude81ee0e72024-12-20 19:22:12 +053042};
Alexander Hansen02c1e292024-11-15 14:30:40 +010043
44class 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 Bae99ff0dd2025-04-22 14:38:36 -050061 void resHandler(const std::shared_ptr<Subscription>& /*self*/,
62 const crow::Response& res);
Alexander Hansen02c1e292024-11-15 14:30:40 +010063
Myung Baefb546102024-10-29 10:21:26 -050064 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 Tanous4a19a7b2025-01-27 10:44:15 -080070 bool sendEventToSubscriber(uint64_t eventId, std::string&& msg);
Alexander Hansen02c1e292024-11-15 14:30:40 +010071
72 void filterAndSendEventLogs(
Ed Tanous4a19a7b2025-01-27 10:44:15 -080073 uint64_t eventId, const std::vector<EventLogObjectsType>& eventRecords);
Alexander Hansen02c1e292024-11-15 14:30:40 +010074
Ed Tanous4a19a7b2025-01-27 10:44:15 -080075 void filterAndSendReports(uint64_t eventId, const std::string& reportId,
Alexander Hansen02c1e292024-11-15 14:30:40 +010076 const telemetry::TimestampReadings& var);
77
78 void updateRetryConfig(uint32_t retryAttempts,
79 uint32_t retryTimeoutInterval);
80
Alexander Hansen02c1e292024-11-15 14:30:40 +010081 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 Hansen02c1e292024-11-15 14:30:40 +010091 boost::urls::url host;
92 std::shared_ptr<crow::ConnectionPolicy> policy;
93 crow::sse_socket::Connection* sseConn = nullptr;
94
Myung Baefb546102024-10-29 10:21:26 -050095 boost::asio::steady_timer hbTimer;
Myung Bae99ff0dd2025-04-22 14:38:36 -050096 std::optional<crow::HttpClient> client;
Alexander Hansen02c1e292024-11-15 14:30:40 +010097
98 public:
99 std::optional<filter_ast::LogicalAnd> filter;
100};
101
102} // namespace redfish