xref: /openbmc/bmcweb/features/redfish/include/subscription.hpp (revision 5ffbc9ae424658c15c2b6d93b86b752f09d2b6dc)
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 crow::Response& res);
62 
63     void sendHeartbeatEvent();
64     void scheduleNextHeartbeatEvent();
65     void heartbeatParametersChanged();
66     void onHbTimeout(const std::weak_ptr<Subscription>& weakSelf,
67                      const boost::system::error_code& ec);
68 
69     bool sendEventToSubscriber(uint64_t eventId, std::string&& msg);
70 
71     void filterAndSendEventLogs(
72         uint64_t eventId, const std::vector<EventLogObjectsType>& eventRecords);
73 
74     void filterAndSendReports(uint64_t eventId, const std::string& reportId,
75                               const telemetry::TimestampReadings& var);
76 
77     void updateRetryConfig(uint32_t retryAttempts,
78                            uint32_t retryTimeoutInterval);
79 
80     bool matchSseId(const crow::sse_socket::Connection& thisConn);
81 
82     // Check used to indicate what response codes are valid as part of our retry
83     // policy.  2XX is considered acceptable
84     static boost::system::error_code retryRespHandler(unsigned int respCode);
85 
86     std::shared_ptr<persistent_data::UserSubscription> userSub;
87     std::function<void()> deleter;
88 
89   private:
90     boost::urls::url host;
91     std::shared_ptr<crow::ConnectionPolicy> policy;
92     crow::sse_socket::Connection* sseConn = nullptr;
93 
94     std::optional<crow::HttpClient> client;
95     boost::asio::steady_timer hbTimer;
96 
97   public:
98     std::optional<filter_ast::LogicalAnd> filter;
99 };
100 
101 } // namespace redfish
102