xref: /openbmc/bmcweb/redfish-core/include/subscription.hpp (revision 99ff0ddcc083c149d3a64190e6c1b0caab1d77f2)
15ffbc9aeSGunnar Mills // SPDX-License-Identifier: Apache-2.0
25ffbc9aeSGunnar Mills // SPDX-FileCopyrightText: Copyright OpenBMC Authors
35ffbc9aeSGunnar Mills // SPDX-FileCopyrightText: Copyright 2020 Intel Corporation
402c1e29fSAlexander Hansen #pragma once
55ffbc9aeSGunnar Mills 
602c1e29fSAlexander Hansen #include "event_logs_object_type.hpp"
702c1e29fSAlexander Hansen #include "event_service_store.hpp"
802c1e29fSAlexander Hansen #include "filter_expr_parser_ast.hpp"
9d7857201SEd Tanous #include "http_client.hpp"
10d7857201SEd Tanous #include "http_response.hpp"
1102c1e29fSAlexander Hansen #include "metric_report.hpp"
1202c1e29fSAlexander Hansen #include "server_sent_event.hpp"
1302c1e29fSAlexander Hansen 
1402c1e29fSAlexander Hansen #include <boost/asio/io_context.hpp>
15d7857201SEd Tanous #include <boost/asio/steady_timer.hpp>
1602c1e29fSAlexander Hansen #include <boost/url/url_view_base.hpp>
1702c1e29fSAlexander Hansen 
18d7857201SEd Tanous #include <cstdint>
19d7857201SEd Tanous #include <functional>
2002c1e29fSAlexander Hansen #include <memory>
21d7857201SEd Tanous #include <optional>
2202c1e29fSAlexander Hansen #include <string>
23d7857201SEd Tanous #include <vector>
2402c1e29fSAlexander Hansen 
2502c1e29fSAlexander Hansen namespace redfish
2602c1e29fSAlexander Hansen {
2702c1e29fSAlexander Hansen 
2802c1e29fSAlexander Hansen static constexpr const char* subscriptionTypeSSE = "SSE";
2902c1e29fSAlexander Hansen 
3002c1e29fSAlexander Hansen static constexpr const uint8_t maxNoOfSubscriptions = 20;
3102c1e29fSAlexander Hansen static constexpr const uint8_t maxNoOfSSESubscriptions = 10;
3281ee0e74SChandramohan Harkude struct TestEvent
3381ee0e74SChandramohan Harkude {
3481ee0e74SChandramohan Harkude     std::optional<int64_t> eventGroupId;
3581ee0e74SChandramohan Harkude     std::optional<std::string> eventTimestamp;
3681ee0e74SChandramohan Harkude     std::optional<std::string> message;
3781ee0e74SChandramohan Harkude     std::optional<std::vector<std::string>> messageArgs;
3881ee0e74SChandramohan Harkude     std::optional<std::string> messageId;
3981ee0e74SChandramohan Harkude     std::optional<std::string> originOfCondition;
4081ee0e74SChandramohan Harkude     std::optional<std::string> resolution;
4181ee0e74SChandramohan Harkude     std::optional<std::string> severity;
4281ee0e74SChandramohan Harkude };
4302c1e29fSAlexander Hansen 
4402c1e29fSAlexander Hansen class Subscription : public std::enable_shared_from_this<Subscription>
4502c1e29fSAlexander Hansen {
4602c1e29fSAlexander Hansen   public:
4702c1e29fSAlexander Hansen     Subscription(const Subscription&) = delete;
4802c1e29fSAlexander Hansen     Subscription& operator=(const Subscription&) = delete;
4902c1e29fSAlexander Hansen     Subscription(Subscription&&) = delete;
5002c1e29fSAlexander Hansen     Subscription& operator=(Subscription&&) = delete;
5102c1e29fSAlexander Hansen 
5202c1e29fSAlexander Hansen     Subscription(std::shared_ptr<persistent_data::UserSubscription> userSubIn,
5302c1e29fSAlexander Hansen                  const boost::urls::url_view_base& url,
5402c1e29fSAlexander Hansen                  boost::asio::io_context& ioc);
5502c1e29fSAlexander Hansen 
5602c1e29fSAlexander Hansen     explicit Subscription(crow::sse_socket::Connection& connIn);
5702c1e29fSAlexander Hansen 
5802c1e29fSAlexander Hansen     ~Subscription() = default;
5902c1e29fSAlexander Hansen 
6002c1e29fSAlexander Hansen     // callback for subscription sendData
61*99ff0ddcSMyung Bae     void resHandler(const std::shared_ptr<Subscription>& /*self*/,
62*99ff0ddcSMyung Bae                     const crow::Response& res);
6302c1e29fSAlexander Hansen 
64fb546105SMyung Bae     void sendHeartbeatEvent();
65fb546105SMyung Bae     void scheduleNextHeartbeatEvent();
66fb546105SMyung Bae     void heartbeatParametersChanged();
67fb546105SMyung Bae     void onHbTimeout(const std::weak_ptr<Subscription>& weakSelf,
68fb546105SMyung Bae                      const boost::system::error_code& ec);
69fb546105SMyung Bae 
704a19a7b5SEd Tanous     bool sendEventToSubscriber(uint64_t eventId, std::string&& msg);
7102c1e29fSAlexander Hansen 
7202c1e29fSAlexander Hansen     void filterAndSendEventLogs(
734a19a7b5SEd Tanous         uint64_t eventId, const std::vector<EventLogObjectsType>& eventRecords);
7402c1e29fSAlexander Hansen 
754a19a7b5SEd Tanous     void filterAndSendReports(uint64_t eventId, const std::string& reportId,
7602c1e29fSAlexander Hansen                               const telemetry::TimestampReadings& var);
7702c1e29fSAlexander Hansen 
7802c1e29fSAlexander Hansen     void updateRetryConfig(uint32_t retryAttempts,
7902c1e29fSAlexander Hansen                            uint32_t retryTimeoutInterval);
8002c1e29fSAlexander Hansen 
8102c1e29fSAlexander Hansen     bool matchSseId(const crow::sse_socket::Connection& thisConn);
8202c1e29fSAlexander Hansen 
8302c1e29fSAlexander Hansen     // Check used to indicate what response codes are valid as part of our retry
8402c1e29fSAlexander Hansen     // policy.  2XX is considered acceptable
8502c1e29fSAlexander Hansen     static boost::system::error_code retryRespHandler(unsigned int respCode);
8602c1e29fSAlexander Hansen 
8702c1e29fSAlexander Hansen     std::shared_ptr<persistent_data::UserSubscription> userSub;
8802c1e29fSAlexander Hansen     std::function<void()> deleter;
8902c1e29fSAlexander Hansen 
9002c1e29fSAlexander Hansen   private:
9102c1e29fSAlexander Hansen     boost::urls::url host;
9202c1e29fSAlexander Hansen     std::shared_ptr<crow::ConnectionPolicy> policy;
9302c1e29fSAlexander Hansen     crow::sse_socket::Connection* sseConn = nullptr;
9402c1e29fSAlexander Hansen 
95fb546105SMyung Bae     boost::asio::steady_timer hbTimer;
96*99ff0ddcSMyung Bae     std::optional<crow::HttpClient> client;
9702c1e29fSAlexander Hansen 
9802c1e29fSAlexander Hansen   public:
9902c1e29fSAlexander Hansen     std::optional<filter_ast::LogicalAnd> filter;
10002c1e29fSAlexander Hansen };
10102c1e29fSAlexander Hansen 
10202c1e29fSAlexander Hansen } // namespace redfish
103