LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-analytics/contrail-collector - structured_syslog_config.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 27 478 5.6 %
Date: 2026-08-03 02:19:58 Functions: 3 29 10.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : 
       6             : #include <sstream>
       7             : #include <boost/make_shared.hpp>
       8             : #include <boost/asio.hpp>
       9             : #include <analytics/analytics_types.h>
      10             : #include "base/regex.h"
      11             : #include "analytics_types.h"
      12             : #include "structured_syslog_config.h"
      13             : #include "options.h"
      14             : #include <base/logging.h>
      15             : #include <boost/bind/bind.hpp>
      16             : #include "rapidjson/document.h"
      17             : #include "rapidjson/writer.h"
      18             : #include "rapidjson/stringbuffer.h"
      19             : 
      20             : using boost::regex_error;
      21             : using contrail::regex;
      22             : using contrail::regex_match;
      23             : using contrail::regex_search;
      24             : using namespace boost::placeholders;
      25             : 
      26          11 : StructuredSyslogConfig::StructuredSyslogConfig(ConfigClientCollector *config_client, uint64_t structured_syslog_active_session_map_limit) {
      27          11 :     activeSessionConfigMapLIMIT = structured_syslog_active_session_map_limit;
      28          11 :     LOG(INFO, "StructuredSyslogConfig:Num of active session LIMIT in session config map: " << activeSessionConfigMapLIMIT);
      29          11 :     if (config_client) {
      30           0 :         config_client->RegisterConfigReceive("structured-systemlog", boost::bind(
      31             :                                  &StructuredSyslogConfig::ReceiveConfig, this, _1, _2));
      32             :     }
      33          11 : }
      34             : 
      35          11 : StructuredSyslogConfig::~StructuredSyslogConfig() {
      36          11 :     hostname_records_.erase(hostname_records_.begin(),
      37          11 :                             hostname_records_.end());
      38          11 :     tenant_records_.erase(tenant_records_.begin(),
      39          11 :                             tenant_records_.end());
      40          11 :     application_records_.erase(application_records_.begin(),
      41          11 :                                application_records_.end());
      42          11 :     tenant_application_records_.erase(tenant_application_records_.begin(),
      43          11 :                                       tenant_application_records_.end());
      44          11 :     networks_map_.erase(networks_map_.begin(), networks_map_.end());
      45          11 :     session_config_map_.erase(session_config_map_.begin(),
      46          11 :                                 session_config_map_.end());
      47          11 : }
      48             : 
      49             : 
      50             : bool 
      51           0 : StructuredSyslogConfig::AddSyslogSessionCounter(const std::string session_unique_key, 
      52             :                                                 std::map<std::string, uint64_t> session_traffic_counters) {
      53             :     try {
      54           0 :         SyslogSessionConfig::iterator it = session_config_map_.find(session_unique_key);
      55           0 :         if (it != session_config_map_.end()) {
      56           0 :             LOG(DEBUG, "Session key found in session counter map.");
      57             :         }
      58             :         else {
      59           0 :             LOG(DEBUG, "Session key NOT found in session counter map.");
      60             :             //Put a Limit to num of session entries in the map.
      61           0 :             if(session_config_map_.size() >= activeSessionConfigMapLIMIT) {
      62           0 :                 LOG(ERROR, "active sessions Config Map LIMIT reached. Current active session count: "<< session_config_map_.size());
      63           0 :                 return false;
      64             :             }
      65             :         }
      66           0 :         LOG(DEBUG, "Adding/Replacing session traffic counters for session key " << session_unique_key);
      67           0 :         session_config_map_[session_unique_key] = session_traffic_counters;
      68           0 :         return true;
      69             :     }
      70           0 :     catch (std::exception &e) {
      71           0 :         LOG(ERROR, "Adding session traffic counters failed for session key: " << session_unique_key);
      72           0 :         return false;
      73           0 :     }
      74             :  }
      75             : 
      76             : int 
      77           0 : StructuredSyslogConfig::RemoveSyslogSessionCounter(const std::string &session_unique_key) {
      78             :     try {
      79           0 :         LOG(DEBUG, "Removing Syslog Session Counter for " << session_unique_key);
      80           0 :         return session_config_map_.erase(session_unique_key);
      81             :     }
      82           0 :     catch (std::exception &e) {
      83           0 :         LOG(ERROR, "Removing session traffic counters failed for session key: " << session_unique_key);
      84           0 :         return 0;
      85           0 :     }
      86             : }
      87             : 
      88             : bool
      89           0 : StructuredSyslogConfig::FetchSyslogSessionCounters(const std::string &session_unique_key,
      90             :                                                     std::map<std::string, uint64_t> &session_traffic_counters) {
      91             :     try {
      92           0 :         SyslogSessionConfig::iterator itr = session_config_map_.find(session_unique_key);
      93           0 :         if (itr != session_config_map_.end()) {
      94           0 :             session_traffic_counters = itr->second;
      95           0 :             return true;
      96             :         }
      97             :         else{
      98           0 :             LOG(DEBUG, "Session counters not found for session key: " << session_unique_key);
      99           0 :             return false;
     100             :         }
     101             :     }
     102           0 :     catch (std::exception &e) {
     103           0 :         LOG(ERROR, "ERROR in fetching Session traffic counters for session key: " << session_unique_key);
     104           0 :         return false;
     105           0 :     }
     106             : }
     107             : 
     108             : /*  Return int 4 when IP belongs to protocol IPv4 or
     109             :     Return int 6 when IP belongs to protocol IPv6, otherwise
     110             :     Return int -1 if IP is not valid */
     111             : int
     112           0 : StructuredSyslogConfig::get_ip_version (const std::string ip) {
     113           0 :     int version = -1;
     114             :     try {
     115           0 :         boost::asio::ip::address addr = boost::asio::ip::address::from_string(ip);
     116           0 :         if (addr.is_v4())
     117           0 :             version = 4;
     118           0 :         else if (addr.is_v6())
     119           0 :             version = 6;
     120             :     }
     121           0 :     catch (std::exception &e){
     122           0 :         LOG(ERROR, "IP : "<< ip <<" found while checking IP protocol is invalid. ERROR: " << e.what());
     123           0 :     }
     124           0 :     return version;
     125             : }
     126             : 
     127             : uint32_t
     128           0 : StructuredSyslogConfig::IPToUInt(std::string ip) {
     129             :     int a, b, c, d;
     130           0 :     uint32_t addr = 0;
     131             : 
     132           0 :     if (sscanf(ip.c_str(), "%d.%d.%d.%d", &a, &b, &c, &d) != 4)
     133           0 :         return 0;
     134             : 
     135           0 :     addr = a << 24;
     136           0 :     addr |= b << 16;
     137           0 :     addr |= c << 8;
     138           0 :     addr |= d;
     139           0 :     return addr;
     140             : }
     141             : 
     142             : std::vector<std::string>
     143           0 : StructuredSyslogConfig::split_into_vector(std::string  str, char delimiter) {
     144           0 :     std::vector<std::string> list;
     145           0 :     std::stringstream ss(str);
     146           0 :     std::string s;
     147           0 :     while(getline(ss, s, delimiter)){
     148           0 :         list.push_back(s);
     149             :     }
     150           0 :     return list;
     151           0 : }
     152             : 
     153             : 
     154             : 
     155             : bool
     156           0 : StructuredSyslogConfig::AddNetwork(const std::string& key, const std::string& network, const std::string& mask, const std::string& location)
     157             : {
     158           0 :     uint32_t network_addr = IPToUInt(network);
     159           0 :     uint32_t mask_addr = IPToUInt(mask);
     160             : 
     161           0 :     uint32_t net_lower = (network_addr & mask_addr);
     162           0 :     uint32_t net_upper = (net_lower | (~mask_addr));
     163             : 
     164           0 :     std::string id = location;
     165           0 :     IPNetwork net(net_lower, net_upper, id);
     166           0 :     std::scoped_lock lock(networks_map_refresh_mutex);
     167             : 
     168           0 :     IPNetworks_map::iterator it = networks_map_.find(key);
     169           0 :     if (it  != networks_map_.end()) {
     170           0 :         LOG(DEBUG, "VPN name found in Networks MAP while adding network. Appending to existing values of VPN key... ");
     171             :         //sorted insertion into vector
     172           0 :         it->second.insert(std::upper_bound(it->second.begin(), it->second.end(), net), net);
     173           0 :         LOG(DEBUG, "IPNetwork with destination address " << network_addr << " added in networks_map with VPN key " << key);
     174             :     } else {
     175           0 :         LOG(DEBUG, "VPN name NOT found in Networks MAP while adding network. Creating a new entry in Networks MAP ...");
     176           0 :         IPNetworks  new_network;
     177           0 :         new_network.push_back(net);
     178           0 :         networks_map_.insert(std::make_pair(key, new_network) );
     179           0 :         LOG(DEBUG, "IPNetwork with destination address " << network_addr << " added in networks_map with VPN key " << key);
     180           0 :     }
     181           0 :     return true;
     182           0 : }
     183             : 
     184             : 
     185             : bool
     186           0 : StructuredSyslogConfig::RefreshNetworksMap(const std::string location){
     187             : 
     188           0 :     std::scoped_lock lock(networks_map_refresh_mutex);
     189           0 :     for(IPNetworks_map::iterator it = networks_map_.begin(); it != networks_map_.end(); it++){
     190           0 :         std::vector<int> indexes_to_be_deleted;
     191           0 :         for(IPNetworks::iterator i = it->second.begin(); i != it->second.end(); i++) {
     192           0 :             if (location == i->id){
     193           0 :                 LOG(DEBUG, "Location " << i->id << " to be deleted from Networks MAP with VPN " << it->first );
     194           0 :                 indexes_to_be_deleted.push_back(i - it->second.begin());
     195             :             }
     196             :         }
     197           0 :         for(std::vector<int>::reverse_iterator v = indexes_to_be_deleted.rbegin(); v != indexes_to_be_deleted.rend(); ++v) {
     198           0 :             IPNetworks::iterator i = it->second.begin();
     199           0 :             it->second.erase(*v + i);
     200             :         }
     201           0 :     }
     202           0 :     LOG(INFO, "Networks MAP Refreshed!" );
     203           0 :     return true;
     204           0 : }
     205             : 
     206             : std::string
     207           0 : StructuredSyslogConfig::FindNetwork(std::string ip,  std::string key, std::string src_location)
     208             : {
     209           0 :     uint32_t network_addr = IPToUInt(ip);
     210           0 :     IPNetwork ip_network(network_addr, 0, ip);
     211           0 :     std::string unknown_location;
     212             : 
     213           0 :     IPNetworks_map::iterator it = networks_map_.find(key);
     214           0 :     if (it  != networks_map_.end()) {
     215           0 :         IPNetworks::iterator upper = std::upper_bound(it->second.begin(), it->second.end(), ip_network);
     216             : 
     217           0 :         uint32_t idx = upper - it->second.begin();
     218           0 :         if (idx <= it->second.size() && idx != 0){
     219           0 :             IPNetwork found_network_obj(0, 0, unknown_location);
     220           0 :             uint32_t min_range = UINT32_MAX;
     221             :             //check for last idx (idx-1) before upper_bound idx.
     222             :             //check for overlapping IP ranges in loop.
     223             :             //network with min. range should take priority.
     224           0 :             for (int i = idx - 1; i >= 0; i--) {
     225           0 :                 IPNetwork possible_network_obj = it->second[i];
     226           0 :                 LOG(DEBUG, "Checking for possible network-range match location " << possible_network_obj.id);
     227           0 :                 uint32_t possible_network_range = possible_network_obj.address_end - possible_network_obj.address_begin;
     228           0 :                 if ((network_addr >= possible_network_obj.address_begin)
     229           0 :                     && (network_addr <= possible_network_obj.address_end)) {
     230           0 :                     if (possible_network_range < min_range) {
     231           0 :                         if (possible_network_obj.id != src_location) {
     232           0 :                             found_network_obj = possible_network_obj;
     233           0 :                             min_range = possible_network_range;
     234           0 :                             LOG(DEBUG, "Possible Network found for " << ip << " from Tenant::VPN " <<  key << 
     235             :                                 " in Site : " << found_network_obj.id  << " with range : " << possible_network_range);
     236             :                         }
     237             :                     }
     238             :                 }
     239             : 
     240           0 :             }
     241           0 :             if (!(found_network_obj.id.empty())) {
     242           0 :                 LOG(DEBUG, "Network found for " << ip << " from Tenant::VPN " <<  key << 
     243             :                     " in Site : " << found_network_obj.id );
     244           0 :                 return found_network_obj.id;
     245             :             }
     246             :             else {
     247           0 :                 LOG(DEBUG,"Network address "<< ip <<" doesnt not belong to the found range " 
     248             :                     << found_network_obj.address_begin << " - " << found_network_obj.address_end << " in Tenant::VPN " << key);
     249             :             }
     250           0 :         }
     251             :         else{
     252           0 :             LOG(DEBUG,"Network range not found for " << ip << " in Tenant::VPN " << key );
     253             :         }
     254             :     }
     255             :     else {
     256           0 :         LOG(DEBUG, "Tenant::VPN "<< key << " NOT found in Network MAP!");
     257             :     }
     258           0 :     return unknown_location;
     259           0 :  }
     260             : 
     261             : void
     262           0 : StructuredSyslogConfig::HostnameRecordsHandler(const contrail_rapidjson::Document &jdoc,
     263             :                                                bool add_update) {
     264           0 :     if (jdoc.IsObject() && jdoc.HasMember("structured_syslog_hostname_record")) {
     265           0 :         const contrail_rapidjson::Value& hr = jdoc["structured_syslog_hostname_record"];
     266           0 :         std::string name, hostaddr, tenant, location, device, tags;
     267           0 :         std::map< std::string, std::string > linkmap;
     268           0 :         bool location_exists = false;
     269             : 
     270           0 :         if (hr.HasMember("fq_name")) {
     271           0 :             const contrail_rapidjson::Value& fq_name = hr["fq_name"];
     272           0 :             contrail_rapidjson::SizeType sz = fq_name.Size();
     273           0 :             name = fq_name[sz-1].GetString();
     274           0 :             LOG(DEBUG, "NAME got from fq_name: " << name);
     275             :         }
     276           0 :         if (hr.HasMember("structured_syslog_hostaddr")) {
     277           0 :             hostaddr = hr["structured_syslog_hostaddr"].GetString();
     278             :         }
     279           0 :         if (hr.HasMember("structured_syslog_tenant")) {
     280           0 :             tenant = hr["structured_syslog_tenant"].GetString();
     281             :         }
     282           0 :         if (hr.HasMember("structured_syslog_location")) {
     283           0 :             location = hr["structured_syslog_location"].GetString();
     284             :         }
     285           0 :         if (hr.HasMember("structured_syslog_device")) {
     286           0 :             device = hr["structured_syslog_device"].GetString();
     287             :         }
     288           0 :         if (hr.HasMember("structured_syslog_hostname_tags")) {
     289           0 :             tags = hr["structured_syslog_hostname_tags"].GetString();
     290             :         }
     291           0 :         if (hr.HasMember("structured_syslog_linkmap")) {
     292           0 :             const contrail_rapidjson::Value& linkmap_fields = hr["structured_syslog_linkmap"];
     293           0 :             const contrail_rapidjson::Value& links_array = linkmap_fields["links"];
     294           0 :             assert(links_array.IsArray());
     295           0 :             for (contrail_rapidjson::SizeType i = 0; i < links_array.Size(); i++) {
     296           0 :                 std::string underlay = links_array[i]["underlay"].GetString();
     297           0 :                 std::string link_type = links_array[i]["link_type"].GetString() ;
     298           0 :                 std::string traffic_destination = links_array[i]["traffic_destination"].GetString() ; 
     299           0 :                 std::string link_metadata = links_array[i]["metadata"].GetString() ;
     300           0 :                 std::string overlay_link_data = underlay + "@" + link_type + "@" + traffic_destination + "@" + link_metadata ;
     301           0 :                 linkmap.insert(std::make_pair(links_array[i]["overlay"].GetString(),
     302             :                 overlay_link_data));
     303           0 :                 LOG(DEBUG, "Adding HostnameRecord: " << name << " linkmap: "
     304             :                 << links_array[i]["overlay"].GetString() << " : "
     305             :                 << overlay_link_data);
     306           0 :             }
     307             :         }
     308           0 :         if (hr.HasMember("structured_syslog_lan_segment_list")) {
     309           0 :             const contrail_rapidjson::Value& LANSegmentList_fields = hr["structured_syslog_lan_segment_list"];
     310           0 :             const contrail_rapidjson::Value& LANSegmentList_array = LANSegmentList_fields["LANSegmentList"];
     311           0 :             assert(LANSegmentList_array.IsArray());
     312           0 :             for (Chr_t::iterator it = hostname_records_.begin();it != hostname_records_.end(); it++){
     313           0 :                     if (location == (it->second->location())) {
     314           0 :                         LOG(DEBUG,"location already exists in hostname_records !!");
     315           0 :                         location_exists = true;
     316           0 :                         break;
     317             :                     }
     318             :             }
     319           0 :             if (location_exists) {
     320           0 :                 LOG(DEBUG, "Refresh LAN MAP for location : "<<location);
     321           0 :                 RefreshNetworksMap(location);
     322             :             }
     323           0 :             for (contrail_rapidjson::SizeType i = 0; i < LANSegmentList_array.Size(); i++) {
     324           0 :                 std::string vpn = LANSegmentList_array[i]["vpn"].GetString();
     325           0 :                 std::string network_ranges = LANSegmentList_array[i]["network_ranges"].GetString();
     326           0 :                 LOG(DEBUG, "Adding networks map with VPN: " << LANSegmentList_array[i]["vpn"].GetString()
     327             :                  << " LANSegmentList: " << LANSegmentList_array[i]["network_ranges"].GetString());
     328             : 
     329           0 :                 std::vector<std::string> network_range_list = split_into_vector(network_ranges,',');
     330           0 :                 for (std::vector<std::string>::iterator iter = network_range_list.begin();
     331           0 :                     iter != network_range_list.end(); iter++){
     332           0 :                     std::vector<std::string> ip_and_subnet = split_into_vector(*iter,'/');
     333           0 :                     std::string network_key = tenant + "::" + vpn;
     334           0 :                     AddNetwork (network_key, ip_and_subnet[0], ip_and_subnet[1], location);
     335           0 :                 }
     336           0 :             }
     337             :         }
     338           0 :         if (add_update) {
     339           0 :             LOG(DEBUG, "Adding HostnameRecord: " << name);
     340           0 :             AddHostnameRecord(name, hostaddr, tenant,
     341             :                                  location, device, tags, linkmap);
     342             :         } else {
     343           0 :             Chr_t::iterator cit = hostname_records_.find(name);
     344           0 :             if (cit != hostname_records_.end()) {
     345           0 :                 LOG(DEBUG, "Erasing LAN MAP for location : "<< cit->second->location());
     346           0 :                 if (!cit->second->location().empty()){
     347           0 :                    RefreshNetworksMap(location);
     348             :                 }
     349           0 :                 LOG(DEBUG, "Erasing HostnameRecord: " << cit->second->name());
     350           0 :                 hostname_records_.erase(cit);
     351             :             }
     352             :         }
     353           0 :         return;
     354           0 :     }
     355             : }
     356             : 
     357             : void
     358           0 : StructuredSyslogConfig::TenantRecordsHandler(const contrail_rapidjson::Document &jdoc,
     359             :                                                bool add_update) {
     360           0 :     if (jdoc.IsObject() && jdoc.HasMember("structured_syslog_tenant_record")) {
     361           0 :         const contrail_rapidjson::Value& hr = jdoc["structured_syslog_tenant_record"];
     362           0 :         std::string name, tenantaddr, tenant, tags;
     363           0 :         std::map< std::string, std::string > dscpmap_ipv4;
     364           0 :         std::map< std::string, std::string > dscpmap_ipv6;
     365             : 
     366           0 :         if (hr.HasMember("fq_name")) {
     367           0 :             const contrail_rapidjson::Value& fq_name = hr["fq_name"];
     368           0 :             contrail_rapidjson::SizeType sz = fq_name.Size();
     369           0 :             name = fq_name[sz-1].GetString();
     370           0 :             LOG(DEBUG, "NAME got from fq_name: " << name);
     371             :         }
     372           0 :         if (hr.HasMember("structured_syslog_tenantaddr")) {
     373           0 :             tenantaddr = hr["structured_syslog_tenantaddr"].GetString();
     374             :         }
     375           0 :         if (hr.HasMember("structured_syslog_tenant")) {
     376           0 :             tenant = hr["structured_syslog_tenant"].GetString();
     377             :         }
     378           0 :         if (hr.HasMember("structured_syslog_tenant_tags")) {
     379           0 :             tags = hr["structured_syslog_tenant_tags"].GetString();
     380             :         }
     381           0 :         if (hr.HasMember("structured_syslog_dscpmap")) {
     382           0 :             const contrail_rapidjson::Value& dscpmap_fields = hr["structured_syslog_dscpmap"];
     383           0 :             const contrail_rapidjson::Value& dscpmapipv4_array = dscpmap_fields["dscpListIPv4"];
     384           0 :             const contrail_rapidjson::Value& dscpmapipv6_array = dscpmap_fields["dscpListIPv6"];
     385           0 :             assert(dscpmapipv6_array.IsArray());
     386           0 :             assert(dscpmapipv4_array.IsArray());
     387           0 :             for (contrail_rapidjson::SizeType i = 0; i < dscpmapipv4_array.Size(); i++) {
     388           0 :                 dscpmap_ipv4.insert(std::make_pair<std::string,
     389           0 :                 std::string >(dscpmapipv4_array[i]["dscp_value"].GetString(),
     390           0 :                 dscpmapipv4_array[i]["alias_code"].GetString()));
     391           0 :                 LOG(DEBUG, "Adding TenantRecord: " << name << " dscpmap ipv4: "
     392             :                 << dscpmapipv4_array[i]["dscp_value"].GetString() << " : "
     393             :                 << dscpmapipv4_array[i]["alias_code"].GetString());
     394             :             }
     395           0 :             for (contrail_rapidjson::SizeType i = 0; i < dscpmapipv6_array.Size(); i++) {
     396           0 :                 dscpmap_ipv6.insert(std::make_pair<std::string,
     397           0 :                 std::string >(dscpmapipv6_array[i]["dscp_value"].GetString(),
     398           0 :                 dscpmapipv6_array[i]["alias_code"].GetString()));
     399           0 :                 LOG(DEBUG, "Adding TenantRecord: " << name << " dscpmap ipv6: "
     400             :                 << dscpmapipv6_array[i]["dscp_value"].GetString() << " : "
     401             :                 << dscpmapipv6_array[i]["alias_code"].GetString());
     402             :             }
     403             :         }
     404           0 :         if (add_update) {
     405           0 :             LOG(DEBUG, "Adding TenantRecord: " << name);
     406           0 :             AddTenantRecord(name, tenantaddr, tenant,
     407             :                                 tags, dscpmap_ipv4, dscpmap_ipv6);
     408             :         } else {
     409           0 :             Ctr_t::iterator cit = tenant_records_.find(name);
     410           0 :             if (cit != tenant_records_.end()) {
     411           0 :                 LOG(DEBUG, "Erasing TenantRecord: " << cit->second->name());
     412           0 :                 tenant_records_.erase(cit);
     413             :             }
     414             :         }
     415           0 :         return;
     416           0 :     }
     417             : }
     418             : 
     419             : 
     420             : void
     421           0 : StructuredSyslogConfig::ApplicationRecordsHandler(const contrail_rapidjson::Document &jdoc,
     422             :                                                   bool add_update) {
     423           0 :     if (jdoc.IsObject() && jdoc.HasMember("structured_syslog_application_record")) {
     424           0 :         const contrail_rapidjson::Value& ar = jdoc["structured_syslog_application_record"];
     425           0 :         std::string name, app_category, app_subcategory,
     426           0 :                     app_groups, app_risk, app_service_tags;
     427             : 
     428           0 :         if (ar.HasMember("fq_name")) {
     429           0 :             const contrail_rapidjson::Value& fq_name = ar["fq_name"];
     430           0 :             contrail_rapidjson::SizeType sz = fq_name.Size();
     431           0 :             name = fq_name[sz-1].GetString();
     432           0 :             LOG(DEBUG, "NAME got from fq_name: " << name);
     433             :         }
     434             : 
     435           0 :         if (ar.HasMember("structured_syslog_app_category")) {
     436           0 :             app_category = ar["structured_syslog_app_category"].GetString();
     437             :         }
     438           0 :         if (ar.HasMember("structured_syslog_app_subcategory")) {
     439           0 :             app_subcategory = ar["structured_syslog_app_subcategory"].GetString();
     440             :         }
     441           0 :         if (ar.HasMember("structured_syslog_app_groups")) {
     442           0 :             app_groups = ar["structured_syslog_app_groups"].GetString();
     443             :         }
     444           0 :         if (ar.HasMember("structured_syslog_app_risk")) {
     445           0 :             app_risk = ar["structured_syslog_app_risk"].GetString();
     446             :         }
     447           0 :         if (ar.HasMember("structured_syslog_app_service_tags")) {
     448           0 :             app_service_tags = ar["structured_syslog_app_service_tags"].GetString();
     449             :         }
     450             : 
     451           0 :         const contrail_rapidjson::Value& fq_name = ar["fq_name"];
     452           0 :         std::string tenant_name = fq_name[1].GetString();
     453           0 :         if (tenant_name.compare("default-global-analytics-config") == 0) {
     454           0 :             if (add_update) {
     455           0 :                 LOG(DEBUG, "Adding ApplicationRecord: " << name);
     456           0 :                 AddApplicationRecord(name, app_category, app_subcategory,
     457             :                                         app_groups, app_risk, app_service_tags);
     458             :             } else {
     459           0 :                 Car_t::iterator cit = application_records_.find(name);
     460           0 :                 if (cit != application_records_.end()) {
     461           0 :                     LOG(DEBUG, "Erasing ApplicationRecord: " << cit->second->name());
     462           0 :                     application_records_.erase(cit);
     463             :                 }
     464             :            }
     465             :         }
     466             :         else {
     467           0 :             std::string apprec_name;
     468           0 :             apprec_name =  tenant_name + '/' + name;
     469           0 :             if (add_update) {
     470           0 :                 LOG(DEBUG, "Adding TenantApplicationRecord: " << apprec_name);
     471           0 :                 AddTenantApplicationRecord(apprec_name, app_category, app_subcategory,
     472             :                                         app_groups, app_risk, app_service_tags);
     473             :             } else {
     474           0 :                 Ctar_t::iterator ctit = tenant_application_records_.find(apprec_name);
     475           0 :                 if (ctit != tenant_application_records_.end()) {
     476           0 :                     LOG(DEBUG, "Erasing TenantApplicationRecord: " << ctit->second->name());
     477           0 :                     tenant_application_records_.erase(ctit);
     478             :                 }
     479             :             }
     480           0 :         }
     481             : 
     482           0 :         return;
     483           0 :     }
     484             : }
     485             : 
     486             : void
     487           0 : StructuredSyslogConfig::MessageConfigsHandler(const contrail_rapidjson::Document &jdoc,
     488             :                                               bool add_update) {
     489           0 :     if (jdoc.IsObject() && jdoc.HasMember("structured_syslog_message")) {
     490           0 :         const contrail_rapidjson::Value& hr = jdoc["structured_syslog_message"];
     491           0 :         std::vector< std::string > ints;
     492           0 :         std::vector< std::string > tags;
     493           0 :         std::string name, forward;
     494           0 :         bool process_and_store = false;
     495           0 :         bool process_and_summarize = false;
     496           0 :         bool process_and_summarize_user = false;
     497             : 
     498           0 :         if (hr.HasMember("fq_name")) {
     499           0 :             const contrail_rapidjson::Value& fq_name = hr["fq_name"];
     500           0 :             contrail_rapidjson::SizeType sz = fq_name.Size();
     501           0 :             name = fq_name[sz-1].GetString();
     502           0 :             LOG(DEBUG, "NAME got from fq_name: " << name);
     503             :         }
     504             : 
     505           0 :         if (hr.HasMember("structured_syslog_message_tagged_fields")) {
     506           0 :             const contrail_rapidjson::Value& tagged_fields = hr["structured_syslog_message_tagged_fields"];
     507           0 :             const contrail_rapidjson::Value& tag_array = tagged_fields["field_names"];
     508           0 :             assert(tag_array.IsArray());
     509           0 :             for (contrail_rapidjson::SizeType i = 0; i < tag_array.Size(); i++)
     510           0 :                 tags.push_back(tag_array[i].GetString());
     511             :         }
     512           0 :         if (hr.HasMember("structured_syslog_message_integer_fields")) {
     513           0 :             const contrail_rapidjson::Value& integer_fields = hr["structured_syslog_message_integer_fields"];
     514           0 :             const contrail_rapidjson::Value& int_array = integer_fields["field_names"];
     515           0 :             assert(int_array.IsArray());
     516           0 :             for (contrail_rapidjson::SizeType i = 0; i < int_array.Size(); i++)
     517           0 :                 ints.push_back(int_array[i].GetString());
     518             :         }
     519           0 :         if (hr.HasMember("structured_syslog_message_forward")) {
     520           0 :             forward = hr["structured_syslog_message_forward"].GetString();
     521             :         }
     522           0 :         if (hr.HasMember("structured_syslog_message_process_and_store")) {
     523           0 :             process_and_store = hr["structured_syslog_message_process_and_store"].GetBool();
     524             :         }
     525           0 :         if (hr.HasMember("structured_syslog_message_process_and_summarize")) {
     526           0 :             process_and_summarize = hr["structured_syslog_message_process_and_summarize"].GetBool();
     527             :         }
     528           0 :         if (hr.HasMember("structured_syslog_message_process_and_summarize_user")) {
     529           0 :             process_and_summarize_user = hr["structured_syslog_message_process_and_summarize_user"].GetBool();
     530             :         }
     531           0 :         if (add_update) {
     532           0 :             LOG(DEBUG, "Adding MessageConfig: " << name);
     533           0 :             AddMessageConfig(name, tags, ints, process_and_store, forward, process_and_summarize, process_and_summarize_user);
     534             :         } else {
     535           0 :             Cmc_t::iterator cit = message_configs_.find(name);
     536           0 :             if (cit != message_configs_.end()) {
     537           0 :                 LOG(DEBUG, "Erasing MessageConfig: " << cit->second->name());
     538           0 :                 message_configs_.erase(cit);
     539             :             }
     540             :         }
     541           0 :         return;
     542           0 :     }
     543             : 
     544             : }
     545             : 
     546             : void
     547           0 : StructuredSyslogConfig::SlaProfileRecordsHandler(const contrail_rapidjson::Document &jdoc,
     548             :                                                  bool add_update) {
     549           0 :     if (jdoc.IsObject() && jdoc.HasMember("structured_syslog_sla_profile")) {
     550           0 :         const contrail_rapidjson::Value& slar = jdoc["structured_syslog_sla_profile"];
     551           0 :         std::string name, sla_params, tenant_name, slarec_name;
     552             : 
     553           0 :         if (slar.HasMember("fq_name")) {
     554           0 :             const contrail_rapidjson::Value& fq_name = slar["fq_name"];
     555           0 :             contrail_rapidjson::SizeType sz = fq_name.Size();
     556           0 :             name = fq_name[sz-1].GetString();
     557           0 :             tenant_name = fq_name[1].GetString();
     558           0 :             slarec_name =  tenant_name + '/' + name;
     559           0 :             LOG(DEBUG, "NAME got from fq_name: " << name);
     560             :         }
     561           0 :         if (slar.HasMember("structured_syslog_sla_params")) {
     562           0 :             sla_params = slar["structured_syslog_sla_params"].GetString();
     563             :         }
     564           0 :         if (add_update) {
     565           0 :             LOG(DEBUG, "Adding SlaProfileRecord: " << slarec_name);
     566           0 :             AddSlaProfileRecord(slarec_name, sla_params);
     567             :         } else {
     568           0 :             Csr_t::iterator cit = sla_profile_records_.find(slarec_name);
     569           0 :             if (cit != sla_profile_records_.end()) {
     570           0 :                 LOG(DEBUG, "Erasing SlaProfileRecord: " << cit->second->name());
     571           0 :                 sla_profile_records_.erase(cit);
     572             :             }
     573             :         }
     574           0 :         return;
     575           0 :     }
     576             : }
     577             : 
     578             : void
     579           0 : StructuredSyslogConfig::ReceiveConfig(const contrail_rapidjson::Document &jdoc, bool add_change) {
     580           0 :     HostnameRecordsHandler(jdoc, add_change);
     581           0 :     TenantRecordsHandler(jdoc, add_change);
     582           0 :     ApplicationRecordsHandler(jdoc, add_change);
     583           0 :     MessageConfigsHandler(jdoc, add_change);
     584           0 :     SlaProfileRecordsHandler(jdoc, add_change);
     585           0 : }
     586             : 
     587             : boost::shared_ptr<HostnameRecord>
     588           0 : StructuredSyslogConfig::GetHostnameRecord(const std::string &name) {
     589           0 :     Chr_t::iterator it = hostname_records_.find(name);
     590           0 :     if (it  != hostname_records_.end()) {
     591           0 :         return it->second;
     592             :     }
     593           0 :     return boost::shared_ptr<HostnameRecord>();
     594             : }
     595             : 
     596             : void
     597           0 : StructuredSyslogConfig::AddHostnameRecord(const std::string &name,
     598             :         const std::string &hostaddr, const std::string &tenant,
     599             :         const std::string &location, const std::string &device,
     600             :         const std::string &tags, const std::map< std::string, std::string > &linkmap) {
     601           0 :     Chr_t::iterator it = hostname_records_.find(name);
     602           0 :     if (it  != hostname_records_.end()) {
     603           0 :         it->second->Refresh(name, hostaddr, tenant, location,
     604             :                            device, tags, linkmap);
     605             :     } else {
     606             :         boost::shared_ptr<HostnameRecord> c(new HostnameRecord(
     607           0 :                     name, hostaddr, tenant, location, device, tags, linkmap));
     608           0 :         hostname_records_.insert(std::make_pair(name, c));
     609           0 :     }
     610           0 : }
     611             : 
     612             : boost::shared_ptr<TenantRecord>
     613           0 : StructuredSyslogConfig::GetTenantRecord(const std::string &name) {
     614           0 :     Ctr_t::iterator it = tenant_records_.find(name);
     615           0 :     if (it  != tenant_records_.end()) {
     616           0 :         return it->second;
     617             :     }
     618           0 :     return boost::shared_ptr<TenantRecord>();
     619             : }
     620             : 
     621             : void
     622           0 : StructuredSyslogConfig::AddTenantRecord(const std::string &name,
     623             :         const std::string &tenantaddr, const std::string &tenant,
     624             :         const std::string &tags, const std::map< std::string, std::string > &dscpmap_ipv4,
     625             :         const std::map< std::string, std::string > &dscpmap_ipv6) {
     626           0 :     Ctr_t::iterator it = tenant_records_.find(name);
     627           0 :     if (it  != tenant_records_.end()) {
     628           0 :         it->second->Refresh(name, tenantaddr, tenant, tags, dscpmap_ipv4, dscpmap_ipv6);
     629             :     } else {
     630             :         boost::shared_ptr<TenantRecord> c(new TenantRecord(
     631           0 :                     name, tenantaddr, tenant, tags, dscpmap_ipv4, dscpmap_ipv6));
     632           0 :         tenant_records_.insert(std::make_pair(name, c));
     633           0 :     }
     634           0 : }
     635             : 
     636             : boost::shared_ptr<ApplicationRecord>
     637           0 : StructuredSyslogConfig::GetApplicationRecord(const std::string &name) {
     638           0 :     Car_t::iterator it = application_records_.find(name);
     639           0 :     if (it  != application_records_.end()) {
     640           0 :         return it->second;
     641             :     }
     642           0 :     return boost::shared_ptr<ApplicationRecord>();
     643             : }
     644             : 
     645             : void
     646           0 : StructuredSyslogConfig::AddApplicationRecord(const std::string &name,
     647             :         const std::string &app_category, const std::string &app_subcategory,
     648             :         const std::string &app_groups, const std::string &app_risk,
     649             :         const std::string &app_service_tags) {
     650           0 :     Car_t::iterator it = application_records_.find(name);
     651           0 :     if (it  != application_records_.end()) {
     652           0 :         it->second->Refresh(name, app_category, app_subcategory, app_groups,
     653             :                            app_risk, app_service_tags);
     654             :     } else {
     655             :         boost::shared_ptr<ApplicationRecord> c(new ApplicationRecord(
     656             :                     name, app_category, app_subcategory, app_groups,
     657           0 :                     app_risk, app_service_tags));
     658           0 :         application_records_.insert(std::make_pair(name, c));
     659           0 :     }
     660           0 : }
     661             : 
     662             : boost::shared_ptr<TenantApplicationRecord>
     663           0 : StructuredSyslogConfig::GetTenantApplicationRecord(const std::string &name) {
     664           0 :     Ctar_t::iterator it = tenant_application_records_.find(name);
     665           0 :     if (it  != tenant_application_records_.end()) {
     666           0 :         return it->second;
     667             :     }
     668           0 :     return boost::shared_ptr<TenantApplicationRecord>();
     669             : }
     670             : 
     671             : void
     672           0 : StructuredSyslogConfig::AddTenantApplicationRecord(const std::string &name,
     673             :         const std::string &tenant_app_category, const std::string &tenant_app_subcategory,
     674             :         const std::string &tenant_app_groups, const std::string &tenant_app_risk,
     675             :         const std::string &tenant_app_service_tags) {
     676           0 :     Ctar_t::iterator it = tenant_application_records_.find(name);
     677           0 :     if (it  != tenant_application_records_.end()) {
     678           0 :         it->second->Refresh(name, tenant_app_category, tenant_app_subcategory,
     679             :                         tenant_app_groups, tenant_app_risk, tenant_app_service_tags);
     680             :     } else {
     681             :         boost::shared_ptr<TenantApplicationRecord> c(new TenantApplicationRecord(
     682             :                     name, tenant_app_category, tenant_app_subcategory,
     683           0 :                     tenant_app_groups, tenant_app_risk, tenant_app_service_tags));
     684           0 :         tenant_application_records_.insert(std::make_pair(name, c));
     685           0 :     }
     686           0 : }
     687             : 
     688             : boost::shared_ptr<SlaProfileRecord>
     689           0 : StructuredSyslogConfig::GetSlaProfileRecord(const std::string &name) {
     690           0 :     Csr_t::iterator it = sla_profile_records_.find(name);
     691           0 :     if (it  != sla_profile_records_.end()) {
     692           0 :         return it->second;
     693             :     }
     694           0 :     return boost::shared_ptr<SlaProfileRecord>();
     695             : }
     696             : 
     697             : void
     698           0 : StructuredSyslogConfig::AddSlaProfileRecord(const std::string &name,
     699             :         const std::string &sla_params) {
     700           0 :     Csr_t::iterator it = sla_profile_records_.find(name);
     701           0 :     if (it  != sla_profile_records_.end()) {
     702           0 :         it->second->Refresh(name, sla_params);
     703             :     } else {
     704             :         boost::shared_ptr<SlaProfileRecord> c(new SlaProfileRecord(
     705           0 :                     name, sla_params));
     706           0 :         sla_profile_records_.insert(std::make_pair(name, c));
     707           0 :     }
     708           0 : }
     709             : 
     710             : boost::shared_ptr<MessageConfig>
     711          30 : StructuredSyslogConfig::GetMessageConfig(const std::string &name) {
     712          30 :     Cmc_t::iterator it = message_configs_.find(name);
     713          30 :     if (it  != message_configs_.end()) {
     714             :         /* exact match */
     715           0 :         return it->second;
     716             :     }
     717             :     /* no exact match, look for match based on regex */
     718          30 :     Cmc_t::iterator cit = message_configs_.begin();
     719          30 :     Cmc_t::iterator end = message_configs_.end();
     720          30 :     Cmc_t::iterator match = end;
     721          30 :     while (cit != end) {
     722           0 :         regex pattern;
     723           0 :         Cmc_t::iterator dit = cit++;
     724           0 :         boost::match_results<std::string::const_iterator> what;
     725           0 :         boost::match_flag_type flags = boost::match_default;
     726           0 :         std::string::const_iterator name_start = name.begin(), name_end = name.end();
     727             :         try {
     728           0 :             pattern = regex(dit->second->name());
     729             :         }
     730           0 :         catch (regex_error &e) {
     731           0 :             LOG(DEBUG, "skipping invalid regex pattern: " << dit->second->name());
     732           0 :             continue;
     733           0 :         }
     734           0 :         if(regex_search(name_start, name_end, what, pattern, flags)) {
     735           0 :             if ((match == end) || (match->second->name().length() < dit->second->name().length())) {
     736           0 :                 match = dit;
     737             :             }
     738             :         }
     739           0 :     }
     740          30 :     if (match != end) {
     741           0 :         return match->second;
     742             :     }
     743             :     /* no match */
     744          30 :     return boost::shared_ptr<MessageConfig>();
     745             : }
     746             : 
     747             : void
     748           0 : StructuredSyslogConfig::AddMessageConfig(const std::string &name,
     749             :         const std::vector< std::string > &tags, const std::vector< std::string > &ints,
     750             :         bool process_and_store, const std::string &forward_action, bool process_and_summarize, 
     751             :         bool process_and_summarize_user) {
     752           0 :     bool forward = false, process_before_forward = false;
     753           0 :     if (forward_action == "forward-unprocessed") {
     754           0 :         forward = true;
     755             :     }
     756           0 :     if (forward_action == "forward-processed") {
     757           0 :         forward = true;
     758           0 :         process_before_forward = true;
     759             :     }
     760           0 :     Cmc_t::iterator it = message_configs_.find(name);
     761           0 :     if (it  != message_configs_.end()) {
     762           0 :         it->second->Refresh(name, tags, ints, process_and_store, forward, process_before_forward, 
     763             :                             process_and_summarize, process_and_summarize_user);
     764             :     } else {
     765             :         boost::shared_ptr<MessageConfig> c(new MessageConfig(
     766             :                     name, tags, ints, process_and_store, forward, process_before_forward, 
     767           0 :                     process_and_summarize, process_and_summarize_user));
     768           0 :         message_configs_.insert(std::make_pair(name, c));
     769           0 :     }
     770           0 : }

Generated by: LCOV version 1.14