Line data Source code
1 : /*
2 : * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 : #ifndef ANALYTICS_STRUCTURED_SYSLOG_CONFIG_H_
6 : #define ANALYTICS_STRUCTURED_SYSLOG_CONFIG_H_
7 :
8 :
9 : #include <map>
10 : #include <iostream>
11 : #include <string>
12 : #include <vector>
13 : #include <algorithm>
14 : #include <stdint.h>
15 : #include <cstdio>
16 : #include <boost/shared_ptr.hpp>
17 : #include <boost/regex.hpp>
18 : #include <boost/thread/mutex.hpp>
19 : #include <boost/algorithm/string.hpp>
20 : #include "config_client_collector.h"
21 : #include "parser_util.h"
22 :
23 : class Options;
24 :
25 :
26 : struct IPNetwork
27 : {
28 0 : IPNetwork(uint32_t lower, uint32_t upper, std::string net_name) {
29 0 : address_begin = lower;
30 0 : address_end = upper;
31 0 : id = net_name;
32 0 : }
33 :
34 0 : bool operator < (const IPNetwork& other) const{
35 0 : return address_begin < other.address_begin;
36 : }
37 :
38 0 : IPNetwork& operator = (const IPNetwork& other) {
39 0 : this->address_begin = other.address_begin;
40 0 : this->address_end = other.address_end;
41 0 : this->id = other.id;
42 0 : return *this;
43 : }
44 :
45 : uint32_t address_begin;
46 : uint32_t address_end;
47 : std::string id;
48 :
49 : };
50 :
51 : class HostnameRecord {
52 : public:
53 0 : explicit HostnameRecord(const std::string &name, const std::string &hostaddr,
54 : const std::string &tenant, const std::string &location,
55 : const std::string &device, const std::string &tags,
56 0 : const std::map< std::string, std::string > &linkmap):
57 0 : refreshed_(true), name_(name), hostaddr_(hostaddr), tenant_(tenant),
58 0 : location_(location), device_(device), tags_(tags), linkmap_(linkmap) {
59 0 : }
60 : bool operator==(const HostnameRecord &rhs) {
61 : return rhs.IsMe(name_) && rhs.hostaddr() == hostaddr_ &&
62 : rhs.tenant() == tenant_ && rhs.location() == location_ &&
63 : rhs.device() == device_ && rhs.tags() == tags_ &&
64 : rhs.linkmap().size() == linkmap_.size() &&
65 : std::equal(rhs.linkmap().begin(), rhs.linkmap().end(), linkmap_.begin());
66 : }
67 :
68 0 : const std::string name() { return name_; }
69 : const std::string hostaddr() const { return hostaddr_; }
70 0 : const std::string tenant() const { return tenant_; }
71 0 : const std::string location() const { return location_; }
72 0 : const std::string device() const { return device_; }
73 0 : const std::string tags() const { return tags_; }
74 0 : const std::map< std::string, std::string > linkmap() const { return linkmap_; }
75 :
76 : bool IsMe(const std::string &name) const { return name == name_; }
77 0 : void Refresh(const std::string &name, const std::string &hostaddr,
78 : const std::string &tenant, const std::string &location,
79 : const std::string &device, const std::string &tags,
80 : const std::map< std::string, std::string > &linkmap) {
81 0 : if (hostaddr_ != hostaddr)
82 0 : hostaddr_ = hostaddr;
83 0 : if (tenant_ != tenant)
84 0 : tenant_ = tenant;
85 0 : if (location_ != location)
86 0 : location_ = location;
87 0 : if (device_ != device)
88 0 : device_ = device;
89 0 : if (tags_ != tags)
90 0 : tags_ = tags;
91 0 : if (linkmap.size() != linkmap_.size()) {
92 0 : linkmap_ = linkmap;
93 0 : } else if (!(std::equal(linkmap.begin(), linkmap.end(), linkmap_.begin()))) {
94 0 : linkmap_ = linkmap;
95 : }
96 0 : refreshed_ = true;
97 0 : }
98 : bool GetandClearRefreshed() { bool r = refreshed_; refreshed_ = false; return r;}
99 : private:
100 : bool refreshed_;
101 : std::string name_;
102 : std::string hostaddr_;
103 : std::string tenant_;
104 : std::string location_;
105 : std::string device_;
106 : std::string tags_;
107 : std::map< std::string, std::string > linkmap_;
108 : };
109 :
110 : class TenantRecord {
111 : public:
112 0 : explicit TenantRecord(const std::string &name, const std::string &tenantaddr,
113 : const std::string &tenant, const std::string &tags,
114 : const std::map< std::string, std::string > &dscpmap_ipv4,
115 0 : const std::map< std::string, std::string > &dscpmap_ipv6):
116 0 : refreshed_(true), name_(name), tenantaddr_(tenantaddr), tenant_(tenant),
117 0 : tags_(tags), dscpmap_ipv4_(dscpmap_ipv4), dscpmap_ipv6_(dscpmap_ipv6) {
118 0 : }
119 : bool operator==(const TenantRecord &rhs) {
120 : return rhs.IsMe(name_) && rhs.tenantaddr() == tenantaddr_ &&
121 : rhs.tenant() == tenant_ && rhs.tags() == tags_ &&
122 : rhs.dscpmap_ipv4().size() == dscpmap_ipv4_.size() &&
123 : std::equal(rhs.dscpmap_ipv4().begin(), rhs.dscpmap_ipv4().end(), dscpmap_ipv4_.begin()) &&
124 : rhs.dscpmap_ipv6().size() == dscpmap_ipv6_.size() &&
125 : std::equal(rhs.dscpmap_ipv6().begin(), rhs.dscpmap_ipv6().end(), dscpmap_ipv6_.begin());
126 : }
127 :
128 0 : const std::string name() { return name_; }
129 0 : const std::string tenantaddr() const { return tenantaddr_; }
130 : const std::string tenant() const { return tenant_; }
131 0 : const std::string tags() const { return tags_; }
132 0 : const std::map< std::string, std::string > dscpmap_ipv4() const { return dscpmap_ipv4_; }
133 0 : const std::map< std::string, std::string > dscpmap_ipv6() const { return dscpmap_ipv6_; }
134 :
135 : bool IsMe(const std::string &name) const { return name == name_; }
136 0 : void Refresh(const std::string &name, const std::string &tenantaddr,
137 : const std::string &tenant, const std::string &tags,
138 : const std::map< std::string, std::string > &dscpmap_ipv4,
139 : const std::map< std::string, std::string > &dscpmap_ipv6) {
140 0 : if (tenantaddr_ != tenantaddr)
141 0 : tenantaddr_ = tenantaddr;
142 0 : if (tenant_ != tenant)
143 0 : tenant_ = tenant;
144 0 : if (tags_ != tags)
145 0 : tags_ = tags;
146 0 : if (dscpmap_ipv4.size() != dscpmap_ipv4_.size()) {
147 0 : dscpmap_ipv4_ = dscpmap_ipv4;
148 0 : } else if (!(std::equal(dscpmap_ipv4.begin(), dscpmap_ipv4.end(), dscpmap_ipv4_.begin()))) {
149 0 : dscpmap_ipv4_ = dscpmap_ipv4;
150 : }
151 0 : if (dscpmap_ipv6.size() != dscpmap_ipv6_.size()) {
152 0 : dscpmap_ipv6_ = dscpmap_ipv6;
153 0 : } else if (!(std::equal(dscpmap_ipv6.begin(), dscpmap_ipv6.end(), dscpmap_ipv6_.begin()))) {
154 0 : dscpmap_ipv6_ = dscpmap_ipv6;
155 : }
156 0 : refreshed_ = true;
157 0 : }
158 : bool GetandClearRefreshed() { bool r = refreshed_; refreshed_ = false; return r;}
159 : private:
160 : bool refreshed_;
161 : std::string name_;
162 : std::string tenantaddr_;
163 : std::string tenant_;
164 : std::string tags_;
165 : std::map< std::string, std::string > dscpmap_ipv4_;
166 : std::map< std::string, std::string > dscpmap_ipv6_;
167 : };
168 :
169 : class ApplicationRecord {
170 : public:
171 0 : explicit ApplicationRecord(const std::string &name, const std::string &app_category,
172 : const std::string &app_subcategory, const std::string &app_groups,
173 0 : const std::string &app_risk, const std::string &app_service_tags):
174 0 : refreshed_(true), name_(name), app_category_(app_category),
175 0 : app_subcategory_(app_subcategory), app_groups_(app_groups),
176 0 : app_risk_(app_risk), app_service_tags_(app_service_tags) {
177 0 : }
178 : bool operator==(const ApplicationRecord &rhs) {
179 : return rhs.IsMe(name_) && rhs.app_category() == app_category_ &&
180 : rhs.app_subcategory() == app_subcategory_ &&
181 : rhs.app_groups() == app_groups_ &&
182 : rhs.app_risk() == app_risk_ &&
183 : rhs.app_service_tags() == app_service_tags_;
184 : }
185 :
186 0 : const std::string name() { return name_; }
187 : const std::string app_category() const { return app_category_; }
188 : const std::string app_subcategory() const { return app_subcategory_; }
189 : const std::string app_groups() const { return app_groups_; }
190 : const std::string app_risk() const { return app_risk_; }
191 : const std::string app_service_tags() const { return app_service_tags_; }
192 :
193 : bool IsMe(const std::string &name) const { return name == name_; }
194 0 : void Refresh(const std::string &name, const std::string &app_category,
195 : const std::string &app_subcategory, const std::string &app_groups,
196 : const std::string &app_risk, const std::string &app_service_tags) {
197 0 : if (app_category_ != app_category)
198 0 : app_category_ = app_category;
199 0 : if (app_subcategory_ != app_subcategory)
200 0 : app_subcategory_ = app_subcategory;
201 0 : if (app_groups_ != app_groups)
202 0 : app_groups_ = app_groups;
203 0 : if (app_risk_ != app_risk)
204 0 : app_risk_ = app_risk;
205 0 : if (app_service_tags_ != app_service_tags)
206 0 : app_service_tags_ = app_service_tags;
207 :
208 0 : refreshed_ = true;
209 0 : }
210 : bool GetandClearRefreshed() { bool r = refreshed_; refreshed_ = false; return r;}
211 : private:
212 : bool refreshed_;
213 : std::string name_;
214 : std::string app_category_;
215 : std::string app_subcategory_;
216 : std::string app_groups_;
217 : std::string app_risk_;
218 : std::string app_service_tags_;
219 : };
220 :
221 : class TenantApplicationRecord {
222 : public:
223 0 : explicit TenantApplicationRecord(const std::string &name, const std::string &tenant_app_category,
224 : const std::string &tenant_app_subcategory, const std::string &tenant_app_groups,
225 0 : const std::string &tenant_app_risk, const std::string &tenant_app_service_tags):
226 0 : refreshed_(true), name_(name), tenant_app_category_(tenant_app_category),
227 0 : tenant_app_subcategory_(tenant_app_subcategory),
228 0 : tenant_app_groups_(tenant_app_groups), tenant_app_risk_(tenant_app_risk),
229 0 : tenant_app_service_tags_(tenant_app_service_tags) {
230 0 : }
231 : bool operator==(const TenantApplicationRecord &rhs) {
232 : return rhs.IsMe(name_) && rhs.tenant_app_category() == tenant_app_category_ &&
233 : rhs.tenant_app_subcategory() == tenant_app_subcategory_ &&
234 : rhs.tenant_app_groups() == tenant_app_groups_ &&
235 : rhs.tenant_app_risk() == tenant_app_risk_ &&
236 : rhs.tenant_app_service_tags() == tenant_app_service_tags_;
237 : }
238 :
239 0 : const std::string name() { return name_; }
240 : const std::string tenant_app_category() const { return tenant_app_category_; }
241 : const std::string tenant_app_subcategory() const { return tenant_app_subcategory_; }
242 : const std::string tenant_app_groups() const { return tenant_app_groups_; }
243 : const std::string tenant_app_risk() const { return tenant_app_risk_; }
244 : const std::string tenant_app_service_tags() const { return tenant_app_service_tags_; }
245 :
246 : bool IsMe(const std::string &name) const { return name == name_; }
247 0 : void Refresh(const std::string &name, const std::string &tenant_app_category,
248 : const std::string &tenant_app_subcategory, const std::string &tenant_app_groups,
249 : const std::string &tenant_app_risk, const std::string &tenant_app_service_tags) {
250 0 : if (tenant_app_category_ != tenant_app_category)
251 0 : tenant_app_category_ = tenant_app_category;
252 0 : if (tenant_app_subcategory_ != tenant_app_subcategory)
253 0 : tenant_app_subcategory_ = tenant_app_subcategory;
254 0 : if (tenant_app_groups_ != tenant_app_groups)
255 0 : tenant_app_groups_ = tenant_app_groups;
256 0 : if (tenant_app_risk_ != tenant_app_risk)
257 0 : tenant_app_risk_ = tenant_app_risk;
258 0 : if (tenant_app_service_tags_ != tenant_app_service_tags)
259 0 : tenant_app_service_tags_ = tenant_app_service_tags;
260 :
261 0 : refreshed_ = true;
262 0 : }
263 : bool GetandClearRefreshed() { bool r = refreshed_; refreshed_ = false; return r;}
264 : private:
265 : bool refreshed_;
266 : std::string name_;
267 : std::string tenant_app_category_;
268 : std::string tenant_app_subcategory_;
269 : std::string tenant_app_groups_;
270 : std::string tenant_app_risk_;
271 : std::string tenant_app_service_tags_;
272 : };
273 :
274 : class MessageConfig {
275 : public:
276 0 : explicit MessageConfig(const std::string &name, const std::vector< std::string > &tags,
277 : const std::vector< std::string > &ints, bool process_and_store, bool forward,
278 0 : bool process_before_forward, bool process_and_summarize, bool process_and_summarize_user):
279 0 : refreshed_(true), name_(name), tags_(tags), ints_(ints), process_and_store_(process_and_store),
280 0 : forward_(forward), process_before_forward_(process_before_forward), process_and_summarize_(process_and_summarize),
281 0 : process_and_summarize_user_(process_and_summarize_user) {
282 0 : }
283 : bool operator==(const MessageConfig &rhs) {
284 : return rhs.IsMe(name_) && rhs.tags().size() == tags_.size() &&
285 : std::equal(rhs.tags().begin(), rhs.tags().end(), tags_.begin()) &&
286 : rhs.ints().size() == ints_.size() &&
287 : std::equal(rhs.ints().begin(), rhs.ints().end(), ints_.begin()) &&
288 : rhs.forward() == forward_ &&
289 : rhs.process_and_store() == process_and_store_ &&
290 : rhs.process_before_forward() == process_before_forward_ &&
291 : rhs.process_and_summarize() == process_and_summarize_ &&
292 : rhs.process_and_summarize_user() == process_and_summarize_user_;
293 : }
294 :
295 0 : const std::string name() { return name_; }
296 0 : const std::vector< std::string > tags() const { return tags_; }
297 0 : const std::vector< std::string > ints() const { return ints_; }
298 0 : const bool process_and_store() const { return process_and_store_; }
299 0 : const bool process_and_summarize() const { return process_and_summarize_; }
300 0 : const bool process_and_summarize_user() const { return process_and_summarize_user_; }
301 0 : const bool forward() const { return forward_; }
302 0 : const bool process_before_forward() const { return process_before_forward_; }
303 : bool IsMe(const std::string &name) const { return name == name_; }
304 0 : void Refresh(const std::string &name, const std::vector< std::string > &tags,
305 : const std::vector< std::string > &ints, bool process_and_store,
306 : bool forward, bool process_before_forward, bool process_and_summarize,
307 : bool process_and_summarize_user) {
308 0 : if (tags.size() != tags_.size()) {
309 0 : tags_ = tags;
310 0 : } else if (!(std::equal(tags.begin(), tags.end(), tags_.begin()))) {
311 0 : tags_ = tags;
312 : }
313 0 : if (ints.size() != ints_.size()) {
314 0 : ints_ = ints;
315 0 : } else if (!(std::equal(ints.begin(), ints.end(), ints_.begin()))) {
316 0 : ints_ = ints;
317 : }
318 0 : if (process_and_store_ != process_and_store)
319 0 : process_and_store_ = process_and_store;
320 0 : if (process_and_summarize_ != process_and_summarize)
321 0 : process_and_summarize_ = process_and_summarize;
322 0 : if (process_and_summarize_user_ != process_and_summarize_user)
323 0 : process_and_summarize_user_ = process_and_summarize_user;
324 0 : if (forward_ != forward)
325 0 : forward_ = forward;
326 0 : if (process_before_forward_ != process_before_forward)
327 0 : process_before_forward_ = process_before_forward;
328 :
329 0 : refreshed_ = true;
330 0 : }
331 : bool GetandClearRefreshed() { bool r = refreshed_; refreshed_ = false; return r;}
332 : private:
333 : bool refreshed_;
334 : std::string name_;
335 : std::vector< std::string > tags_;
336 : std::vector< std::string > ints_;
337 : bool process_and_store_;
338 : bool forward_;
339 : bool process_before_forward_;
340 : bool process_and_summarize_;
341 : bool process_and_summarize_user_;
342 : };
343 :
344 : class SlaProfileRecord {
345 : public:
346 0 : explicit SlaProfileRecord(const std::string &name, const std::string &sla_params):
347 0 : refreshed_(true), name_(name), sla_params_(sla_params) {
348 0 : }
349 : bool operator==(const SlaProfileRecord &rhs) {
350 : return rhs.IsMe(name_) && rhs.sla_params() == sla_params_;
351 : }
352 :
353 0 : const std::string name() { return name_; }
354 0 : const std::string sla_params() const { return sla_params_; }
355 :
356 : bool IsMe(const std::string &name) const { return name == name_; }
357 0 : void Refresh(const std::string &name, const std::string &sla_params) {
358 0 : if (sla_params_ != sla_params)
359 0 : sla_params_ = sla_params;
360 :
361 0 : refreshed_ = true;
362 0 : }
363 : bool GetandClearRefreshed() { bool r = refreshed_; refreshed_ = false; return r;}
364 : private:
365 : bool refreshed_;
366 : std::string name_;
367 : std::string sla_params_;
368 : };
369 :
370 : typedef std::map<std::string, boost::shared_ptr<HostnameRecord> > Chr_t;
371 : typedef std::map<std::string, boost::shared_ptr<TenantRecord> > Ctr_t;
372 : typedef std::map<std::string, boost::shared_ptr<ApplicationRecord> > Car_t;
373 : typedef std::map<std::string, boost::shared_ptr<TenantApplicationRecord> > Ctar_t;
374 : typedef std::map<std::string, boost::shared_ptr<MessageConfig> > Cmc_t;
375 : typedef std::map<std::string, boost::shared_ptr<SlaProfileRecord> > Csr_t;
376 : typedef std::vector<IPNetwork> IPNetworks;
377 : typedef std::map<std::string, IPNetworks> IPNetworks_map;
378 : typedef std::map<std::string, std::map<std::string, uint64_t> > SyslogSessionConfig;
379 :
380 : class StructuredSyslogConfig {
381 : public:
382 : StructuredSyslogConfig(ConfigClientCollector *config_client, uint64_t structured_syslog_active_session_map_limit);
383 : ~StructuredSyslogConfig();
384 : uint32_t IPToUInt(std::string ip);
385 : int get_ip_version (const std::string ip);
386 : bool AddSyslogSessionCounter(const std::string session_unique_key,
387 : std::map<std::string, uint64_t> session_traffic_counters);
388 : int RemoveSyslogSessionCounter(const std::string &session_unique_key);
389 : bool FetchSyslogSessionCounters(const std::string &session_unique_key,
390 : std::map<std::string, uint64_t> &session_traffic_counters);
391 : std::vector<std::string> split_into_vector(std::string str, char delimiter) ;
392 : bool AddNetwork(const std::string& key, const std::string& network, const std::string& mask, const std::string& location);
393 : bool RefreshNetworksMap(const std::string location);
394 : std::string FindNetwork(std::string ip, std::string key, std::string src_location);
395 : void AddHostnameRecord(const std::string &name, const std::string &hostaddr,
396 : const std::string &tenant, const std::string &location,
397 : const std::string &device, const std::string &tags,
398 : const std::map< std::string, std::string > &linkmap);
399 : void AddTenantRecord(const std::string &name, const std::string &tenantaddr,
400 : const std::string &tenant, const std::string &tags,
401 : const std::map< std::string, std::string > &dscpmap_ipv4,
402 : const std::map< std::string, std::string > &dscpmap_ipv6);
403 : void AddApplicationRecord(const std::string &name, const std::string &app_category,
404 : const std::string &app_subcategory, const std::string &app_groups,
405 : const std::string &app_risk, const std::string &app_service_tags);
406 : void AddTenantApplicationRecord(const std::string &name, const std::string &tenant_app_category,
407 : const std::string &tenant_app_subcategory, const std::string &tenant_app_groups,
408 : const std::string &tenant_app_risk, const std::string &tenant_app_service_tags);
409 : void AddMessageConfig(const std::string &name, const std::vector< std::string > &tags,
410 : const std::vector< std::string > &ints, bool process_and_store,
411 : const std::string &forward, bool process_and_summarize, bool process_and_summarize_user);
412 : void AddSlaProfileRecord(const std::string &name, const std::string &sla_params);
413 : boost::shared_ptr<HostnameRecord> GetHostnameRecord(const std::string &name);
414 : boost::shared_ptr<TenantRecord> GetTenantRecord(const std::string &name);
415 : boost::shared_ptr<ApplicationRecord> GetApplicationRecord(const std::string &name);
416 : boost::shared_ptr<TenantApplicationRecord> GetTenantApplicationRecord(const std::string &name);
417 : boost::shared_ptr<MessageConfig> GetMessageConfig(const std::string &name);
418 : boost::shared_ptr<SlaProfileRecord> GetSlaProfileRecord(const std::string &name);
419 : void ReceiveConfig(const contrail_rapidjson::Document &jdoc, bool add_change);
420 : private:
421 : void HostnameRecordsHandler(const contrail_rapidjson::Document &jdoc,
422 : bool add_change);
423 : void TenantRecordsHandler(const contrail_rapidjson::Document &jdoc,
424 : bool add_change);
425 : void ApplicationRecordsHandler(const contrail_rapidjson::Document &jdoc,
426 : bool add_change);
427 : void MessageConfigsHandler(const contrail_rapidjson::Document &jdoc,
428 : bool add_change);
429 : void SlaProfileRecordsHandler(const contrail_rapidjson::Document &jdoc,
430 : bool add_change);
431 : Chr_t hostname_records_;
432 : Ctr_t tenant_records_;
433 : Car_t application_records_;
434 : Ctar_t tenant_application_records_;
435 : Cmc_t message_configs_;
436 : Csr_t sla_profile_records_;
437 :
438 : //networks_map_refresh_mutex should be used only to refresh networks map
439 : std::mutex networks_map_refresh_mutex;
440 : IPNetworks_map networks_map_;
441 : SyslogSessionConfig session_config_map_;
442 : uint64_t activeSessionConfigMapLIMIT;
443 : };
444 :
445 :
446 : #endif
|