xref: /openbmc/bmcweb/redfish-core/include/subscription.hpp (revision 56b81992ba8a8e644f2e75251a94df4f4d0d0880)
1 /*
2 Copyright (c) 2020 Intel Corporation
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8       http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations 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 
29 namespace redfish
30 {
31 
32 static constexpr const char* subscriptionTypeSSE = "SSE";
33 
34 static constexpr const uint8_t maxNoOfSubscriptions = 20;
35 static constexpr const uint8_t maxNoOfSSESubscriptions = 10;
36 
37 class Subscription : public std::enable_shared_from_this<Subscription>
38 {
39   public:
40     Subscription(const Subscription&) = delete;
41     Subscription& operator=(const Subscription&) = delete;
42     Subscription(Subscription&&) = delete;
43     Subscription& operator=(Subscription&&) = delete;
44 
45     Subscription(std::shared_ptr<persistent_data::UserSubscription> userSubIn,
46                  const boost::urls::url_view_base& url,
47                  boost::asio::io_context& ioc);
48 
49     explicit Subscription(crow::sse_socket::Connection& connIn);
50 
51     ~Subscription() = default;
52 
53     // callback for subscription sendData
54     void resHandler(const std::shared_ptr<Subscription>& /*unused*/,
55                     const crow::Response& res);
56 
57     void sendHeartbeatEvent();
58     void scheduleNextHeartbeatEvent();
59     void heartbeatParametersChanged();
60     void onHbTimeout(const std::weak_ptr<Subscription>& weakSelf,
61                      const boost::system::error_code& ec);
62 
63     bool sendEventToSubscriber(std::string&& msg);
64 
65     bool sendTestEventLog();
66 
67     void filterAndSendEventLogs(
68         const std::vector<EventLogObjectsType>& eventRecords);
69 
70     void filterAndSendReports(const std::string& reportId,
71                               const telemetry::TimestampReadings& var);
72 
73     void updateRetryConfig(uint32_t retryAttempts,
74                            uint32_t retryTimeoutInterval);
75 
76     uint64_t getEventSeqNum() const;
77 
78     bool matchSseId(const crow::sse_socket::Connection& thisConn);
79 
80     // Check used to indicate what response codes are valid as part of our retry
81     // policy.  2XX is considered acceptable
82     static boost::system::error_code retryRespHandler(unsigned int respCode);
83 
84     std::shared_ptr<persistent_data::UserSubscription> userSub;
85     std::function<void()> deleter;
86 
87   private:
88     uint64_t eventSeqNum = 1;
89     boost::urls::url host;
90     std::shared_ptr<crow::ConnectionPolicy> policy;
91     crow::sse_socket::Connection* sseConn = nullptr;
92 
93     std::optional<crow::HttpClient> client;
94     boost::asio::steady_timer hbTimer;
95 
96   public:
97     std::optional<filter_ast::LogicalAnd> filter;
98 };
99 
100 } // namespace redfish
101