summaryrefslogtreecommitdiff
path: root/redfish-core/include/subscription.hpp
blob: f4fb5e530b24c3ba9564e22943319e37fd94cdf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
// SPDX-FileCopyrightText: Copyright 2020 Intel Corporation
#pragma once

#include "event_logs_object_type.hpp"
#include "event_service_store.hpp"
#include "filter_expr_parser_ast.hpp"
#include "http_client.hpp"
#include "http_response.hpp"
#include "server_sent_event.hpp"
#include "telemetry_readings.hpp"

#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/url/url_view_base.hpp>

#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <vector>

namespace redfish
{

static constexpr const char* subscriptionTypeSSE = "SSE";

static constexpr const uint8_t maxNoOfSubscriptions = 20;
static constexpr const uint8_t maxNoOfSSESubscriptions = 10;
struct TestEvent
{
    std::optional<int64_t> eventGroupId;
    std::optional<std::string> eventTimestamp;
    std::optional<std::string> message;
    std::optional<std::vector<std::string>> messageArgs;
    std::optional<std::string> messageId;
    std::optional<std::string> originOfCondition;
    std::optional<std::string> resolution;
    std::optional<std::string> severity;
};

class Subscription : public std::enable_shared_from_this<Subscription>
{
  public:
    Subscription(const Subscription&) = delete;
    Subscription& operator=(const Subscription&) = delete;
    Subscription(Subscription&&) = delete;
    Subscription& operator=(Subscription&&) = delete;

    Subscription(std::shared_ptr<persistent_data::UserSubscription> userSubIn,
                 const boost::urls::url_view_base& url,
                 boost::asio::io_context& ioc);

    explicit Subscription(crow::sse_socket::Connection& connIn);

    ~Subscription() = default;

    // callback for subscription sendData
    void resHandler(const std::shared_ptr<Subscription>& /*self*/,
                    const crow::Response& res);

    void sendHeartbeatEvent();
    void scheduleNextHeartbeatEvent();
    void heartbeatParametersChanged();
    void onHbTimeout(const std::weak_ptr<Subscription>& weakSelf,
                     const boost::system::error_code& ec);

    bool sendEventToSubscriber(uint64_t eventId, std::string&& msg);

    void filterAndSendEventLogs(
        uint64_t eventId, const std::vector<EventLogObjectsType>& eventRecords);

    void filterAndSendReports(uint64_t eventId, const std::string& reportId,
                              const telemetry::TimestampReadings& var);

    void updateRetryConfig(uint32_t retryAttempts,
                           uint32_t retryTimeoutInterval);

    bool matchSseId(const crow::sse_socket::Connection& thisConn);

    // Check used to indicate what response codes are valid as part of our retry
    // policy.  2XX is considered acceptable
    static boost::system::error_code retryRespHandler(unsigned int respCode);

    std::shared_ptr<persistent_data::UserSubscription> userSub;
    std::function<void()> deleter;

  private:
    boost::urls::url host;
    std::shared_ptr<crow::ConnectionPolicy> policy;
    crow::sse_socket::Connection* sseConn = nullptr;

    boost::asio::steady_timer hbTimer;
    std::optional<crow::HttpClient> client;

  public:
    std::optional<filter_ast::LogicalAnd> filter;
};

} // namespace redfish