xref: /openbmc/bmcweb/redfish-core/include/subscription.hpp (revision 99ff0ddcc083c149d3a64190e6c1b0caab1d77f2)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 // SPDX-FileCopyrightText: Copyright 2020 Intel Corporation
4 #pragma once
5 
6 #include "event_logs_object_type.hpp"
7 #include "event_service_store.hpp"
8 #include "filter_expr_parser_ast.hpp"
9 #include "http_client.hpp"
10 #include "http_response.hpp"
11 #include "metric_report.hpp"
12 #include "server_sent_event.hpp"
13 
14 #include <boost/asio/io_context.hpp>
15 #include <boost/asio/steady_timer.hpp>
16 #include <boost/url/url_view_base.hpp>
17 
18 #include <cstdint>
19 #include <functional>
20 #include <memory>
21 #include <optional>
22 #include <string>
23 #include <vector>
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;
32 struct TestEvent
33 {
34     std::optional<int64_t> eventGroupId;
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;
42 };
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
61     void resHandler(const std::shared_ptr<Subscription>& /*self*/,
62                     const crow::Response& res);
63 
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 
70     bool sendEventToSubscriber(uint64_t eventId, std::string&& msg);
71 
72     void filterAndSendEventLogs(
73         uint64_t eventId, const std::vector<EventLogObjectsType>& eventRecords);
74 
75     void filterAndSendReports(uint64_t eventId, const std::string& reportId,
76                               const telemetry::TimestampReadings& var);
77 
78     void updateRetryConfig(uint32_t retryAttempts,
79                            uint32_t retryTimeoutInterval);
80 
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:
91     boost::urls::url host;
92     std::shared_ptr<crow::ConnectionPolicy> policy;
93     crow::sse_socket::Connection* sseConn = nullptr;
94 
95     boost::asio::steady_timer hbTimer;
96     std::optional<crow::HttpClient> client;
97 
98   public:
99     std::optional<filter_ast::LogicalAnd> filter;
100 };
101 
102 } // namespace redfish
103