-
Notifications
You must be signed in to change notification settings - Fork 76
/
KafkaTopicSelector.h
192 lines (166 loc) · 8.52 KB
/
KafkaTopicSelector.h
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright (c) 2013-2016 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
*/
#ifndef OPENBMP_KAFKATOPICSELECTOR_H
#define OPENBMP_KAFKATOPICSELECTOR_H
#include <librdkafka/rdkafkacpp.h>
#include "Config.h"
#include "Logger.h"
#include "KafkaPeerPartitionerCallback.h"
class KafkaTopicSelector {
public:
/**
* MSGBUS_TOPIC_* defines the default topic names
*/
#define MSGBUS_TOPIC_COLLECTOR "openbmp.parsed.collector"
#define MSGBUS_TOPIC_ROUTER "openbmp.parsed.router"
#define MSGBUS_TOPIC_PEER "openbmp.parsed.peer"
#define MSGBUS_TOPIC_BASE_ATTRIBUTE "openbmp.parsed.base_attribute"
#define MSGBUS_TOPIC_UNICAST_PREFIX "openbmp.parsed.unicast_prefix"
#define MSGBUS_TOPIC_L3VPN "openbmp.parsed.l3vpn"
#define MSGBUS_TOPIC_EVPN "openbmp.parsed.evpn"
#define MSGBUS_TOPIC_LS_NODE "openbmp.parsed.ls_node"
#define MSGBUS_TOPIC_LS_LINK "openbmp.parsed.ls_link"
#define MSGBUS_TOPIC_LS_PREFIX "openbmp.parsed.ls_prefix"
#define MSGBUS_TOPIC_BMP_STAT "openbmp.parsed.bmp_stat"
#define MSGBUS_TOPIC_BMP_RAW "openbmp.bmp_raw"
/**
* MSGBUS_TOPIC_VAR_* defines the topic var/key for the topic maps.
* This matches the config topic var name, which is the map key.
*/
#define MSGBUS_TOPIC_VAR_COLLECTOR "collector"
#define MSGBUS_TOPIC_VAR_ROUTER "router"
#define MSGBUS_TOPIC_VAR_PEER "peer"
#define MSGBUS_TOPIC_VAR_BASE_ATTRIBUTE "base_attribute"
#define MSGBUS_TOPIC_VAR_UNICAST_PREFIX "unicast_prefix"
#define MSGBUS_TOPIC_VAR_L3VPN "l3vpn"
#define MSGBUS_TOPIC_VAR_EVPN "evpn"
#define MSGBUS_TOPIC_VAR_LS_NODE "ls_node"
#define MSGBUS_TOPIC_VAR_LS_LINK "ls_link"
#define MSGBUS_TOPIC_VAR_LS_PREFIX "ls_prefix"
#define MSGBUS_TOPIC_VAR_BMP_STAT "bmp_stat"
#define MSGBUS_TOPIC_VAR_BMP_RAW "bmp_raw"
/*********************************************************************//**
* Constructor for class
*
* \param [in] logPtr Pointer to Logger instance
* \param [in] cfg Pointer to the config instance
* \param [in] producer Pointer to the kafka producer
***********************************************************************/
KafkaTopicSelector(Logger *logPtr, Config *cfg, RdKafka::Producer *producer);
/*********************************************************************//**
* Destructor for class
***********************************************************************/
~KafkaTopicSelector();
/*********************************************************************//**
* Gets topic pointer by topic var name, router and peer group. If the topic doesn't exist, a new entry
* will be initialized.
*
* \param [in] topic_var MSGBUS_TOPIC_VAR_<name>
* \param [in] router_group Router group - empty/NULL means no router group
* \param [in] peer_group Peer group - empty/NULL means no peer group
* \param [in] peer_asn Peer asn (remote asn)
*
* \return (RdKafka::Topic *) pointer or NULL if error
***********************************************************************/
RdKafka::Topic * getTopic(const std::string &topic_var, const std::string *router_group,
const std::string *peer_group,
uint32_t peer_asn);
/*********************************************************************//**
* Check if a topic is enabled
*
* \param [in] topic_var MSGBUS_TOPIC_VAR_<name>
*
* \return bool true if the topic is enabled, false otherwise
***********************************************************************/
bool topicEnabled(const std::string &topic_var);
/*********************************************************************//**
* Lookup router group
*
* \param [in] hostname hostname/fqdn of the router
* \param [in] ip_addr IP address of the peer (printed form)
* \param [out] router_group_name Reference to string where router group will be updated
*
* \return bool true if matched, false if no matched peer group
***********************************************************************/
void lookupRouterGroup(std::string hostname, std::string ip_addr, std::string &router_group_name);
/*********************************************************************//**
* Lookup peer group
*
* \param [in] hostname hostname/fqdn of the peer
* \param [in] ip_addr IP address of the peer (printed form)
* \param [in] peer_asn Peer ASN
* \param [out] peer_group_name Reference to string where peer group will be updated
*
* \return bool true if matched, false if no matched peer group
***********************************************************************/
void lookupPeerGroup(std::string hostname, std::string ip_addr, uint32_t peer_asn,
std::string &peer_group_name);
private:
Config *cfg; ///< Configuration instance
Logger *logger; ///< Logging class pointer
bool debug; ///< debug flag to indicate debugging
RdKafka::Producer *producer; ///< Kafka Producer instance
RdKafka::Conf *tconf; ///< rdkafka topic level configuration
///< Partition callback for peer
KafkaPeerPartitionerCallback *peer_partitioner_callback;
/**
* Topic name to rdkafka pointer map (key=Name, value=topic pointer)
*
* Key will be MSGBUS_TOPIC_VAR_<topic>_<router_group>_<peer_group>[_<peer_asn>]
* Keys will not contain the optional values unless topic_flags_map includes them.
*
* If router_group or peer group is empty, the key will include them as empty values.
* Eg. unicast_prefix___ (both router and peer groups are empty)
* unicast_prefix_routergrp1__ (peer group is empty but router group is defined)
* unicast_prefix__peergrp1_ (router group is empty but peer group is defined)
* unicast_prefix_routergrp1_peergroup1_ (both router and peer groups are defiend)
*/
typedef std::map<std::string, RdKafka::Topic *> topic_map;
std::map<std::string, RdKafka::Topic*> topic;
/**
* Topic flags and map define various flags per topic var
*/
struct topic_flags {
bool include_peerAsn; ///< Indicates if peer ASN should be included in the topic key
};
std::map<std::string, topic_flags> topic_flags_map; ///< Map key is one of MSGBUS_TOPIC_VAR_<topic>
/**
* Free allocated topic map pointers
*/
void freeTopicMap();
/**
* Initialize topic
* Producer must be initialized and connected prior to calling this method.
* Topic map will be updated.
*
* \param [in] topic_var MSGBUS_TOPIC_VAR_<name>
* \param [in] router_group Router group - empty/NULL means no router group
* \param [in] peer_group Peer group - empty/NULL means no peer group
* \param [in] peer_asn Peer asn (remote asn)
*
* \return (RdKafka::Topic *) pointer or NULL if error
*/
RdKafka::Topic * initTopic(const std::string &topic_var,
const std::string *router_group, const std::string *peer_group,
uint32_t peer_asn);
/**
* Get the topic map key name
*
* \param [in] topic_var MSGBUS_TOPIC_VAR_<name>
* \param [in] router_group Router group - empty/NULL means no router group
* \param [in] peer_group Peer group - empty/NULL means no peer group
* \param [in] peer_asn Peer asn (remote asn)
*
* \return string value of the topic key to be used with the topic map
*/
std::string getTopicKey(const std::string &topic_var,
const std::string *router_group, const std::string *peer_group,
uint32_t peer_asn);
};
#endif //OPENBMP_KAFKATOPICSELECTOR_H