LCOV - code coverage report
Current view: top level - bgp - bgp_config_ifmap.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 644 1455 44.3 %
Date: 2026-11-16 02:06:09 Functions: 61 131 46.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "bgp/bgp_config_ifmap.h"
       7             : 
       8             : #include <boost/foreach.hpp>
       9             : 
      10             : #include <algorithm>
      11             : 
      12             : #include "base/string_util.h"
      13             : #include "base/task_annotations.h"
      14             : #include "bgp/bgp_common.h"
      15             : #include "bgp/bgp_config_listener.h"
      16             : #include "bgp/bgp_log.h"
      17             : #include "bgp/routing-instance/iservice_chain_mgr.h"
      18             : #include "ifmap/ifmap_node.h"
      19             : #include "ifmap/ifmap_table.h"
      20             : 
      21             : #include "schema/bgp_schema_types.h"
      22             : #include "schema/vnc_cfg_types.h"
      23             : 
      24             : using std::unique_ptr;
      25             : using std::find;
      26             : using std::make_pair;
      27             : using std::pair;
      28             : using std::set;
      29             : using std::sort;
      30             : using std::string;
      31             : using std::vector;
      32             : using boost::iequals;
      33             : 
      34             : const int BgpIfmapConfigManager::kConfigTaskInstanceId = 0;
      35             : 
      36             : static BgpNeighborConfig::AddressFamilyList default_addr_family_list;
      37             : 
      38           1 : void DefaultAddressFamilyInit() {
      39           1 :     default_addr_family_list.push_back("inet");
      40           1 :     default_addr_family_list.push_back("inet-vpn");
      41           1 : }
      42             : 
      43             : MODULE_INITIALIZER(DefaultAddressFamilyInit);
      44             : 
      45         440 : static string IdentifierParent(const string &identifier) {
      46         440 :     string parent;
      47             :     size_t last;
      48         440 :     last = identifier.rfind(':');
      49         440 :     if (last == 0 || last == string::npos) {
      50           0 :         return parent;
      51             :     }
      52         440 :     parent = identifier.substr(0, last);
      53         440 :     return parent;
      54           0 : }
      55             : 
      56         148 : static uint32_t IpAddressToBgpIdentifier(const IpAddress &address) {
      57         148 :     return htonl(address.to_v4().to_ulong());
      58             : }
      59             : 
      60          64 : static string BgpIdentifierToString(uint32_t identifier) {
      61          64 :     Ip4Address addr(ntohl(identifier));
      62         128 :     return addr.to_string();
      63             : }
      64             : 
      65          32 : BgpIfmapPeeringConfig::BgpIfmapPeeringConfig(BgpIfmapInstanceConfig *instance)
      66          32 :     : instance_(instance) {
      67          32 : }
      68             : 
      69          32 : BgpIfmapPeeringConfig::~BgpIfmapPeeringConfig() {
      70          32 :     STLDeleteElements(&neighbors_);
      71          32 : }
      72             : 
      73          32 : void BgpIfmapPeeringConfig::SetNodeProxy(IFMapNodeProxy *proxy) {
      74          32 :     if (proxy != NULL) {
      75          32 :         node_proxy_.Swap(proxy);
      76          32 :         name_ = node_proxy_.node()->name();
      77             :     }
      78          32 : }
      79             : 
      80           0 : BgpIfmapRoutingPolicyLinkConfig::BgpIfmapRoutingPolicyLinkConfig(
      81           0 :     BgpIfmapInstanceConfig *rti, BgpIfmapRoutingPolicyConfig *rtp) :
      82           0 :      instance_(rti), policy_(rtp) {
      83           0 : }
      84             : 
      85           0 : BgpIfmapRoutingPolicyLinkConfig::~BgpIfmapRoutingPolicyLinkConfig() {
      86           0 : }
      87             : 
      88           0 : void BgpIfmapRoutingPolicyLinkConfig::SetNodeProxy(IFMapNodeProxy *proxy) {
      89           0 :     if (proxy != NULL) {
      90           0 :         node_proxy_.Swap(proxy);
      91           0 :         name_ = node_proxy_.node()->name();
      92             :     }
      93           0 : }
      94             : 
      95           0 : bool BgpIfmapRoutingPolicyLinkConfig::GetInstancePolicyPair(
      96             :     DBGraph *graph, IFMapNode *node, pair<IFMapNode *, IFMapNode *> *pair) {
      97           0 :     IFMapNode *routing_instance = NULL;
      98           0 :     IFMapNode *routing_policy = NULL;
      99             : 
     100           0 :     for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
     101           0 :          iter != node->end(graph); ++iter) {
     102           0 :         IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
     103           0 :         if (strcmp(adj->table()->Typename(), "routing-instance") == 0)
     104           0 :             routing_instance = adj;
     105           0 :         if (strcmp(adj->table()->Typename(), "routing-policy") == 0)
     106           0 :             routing_policy = adj;
     107             :     }
     108           0 :     if (routing_policy == NULL || routing_instance == NULL) {
     109           0 :         return false;
     110             :     }
     111             : 
     112           0 :     pair->first = routing_instance;
     113           0 :     pair->second = routing_policy;
     114           0 :     return true;
     115             : }
     116             : 
     117           0 : void BgpIfmapRoutingPolicyLinkConfig::Update(BgpIfmapConfigManager *manager,
     118             :     const autogen::RoutingPolicyRoutingInstance *ri_rp) {
     119           0 :     ri_rp_link_.reset(ri_rp);
     120           0 : }
     121             : 
     122           0 : void BgpIfmapRoutingPolicyLinkConfig::Delete(BgpIfmapConfigManager *manager) {
     123           0 :     ri_rp_link_.reset();
     124           0 : }
     125             : 
     126         128 : static AuthenticationData::KeyType KeyChainType(const string &value) {
     127             :     // Case-insensitive comparison
     128         128 :     if (boost::iequals(value, "md5")) {
     129           0 :         return AuthenticationData::MD5;
     130             :     }
     131         128 :     return AuthenticationData::NIL;
     132             : }
     133             : 
     134         128 : static void BuildKeyChain(BgpNeighborConfig *neighbor,
     135             :     const autogen::AuthenticationData &values) {
     136         128 :     AuthenticationData keydata;
     137         128 :     keydata.set_key_type(KeyChainType(values.key_type));
     138             : 
     139         128 :     AuthenticationKey key;
     140         128 :     for (vector<autogen::AuthenticationKeyItem>::const_iterator iter =
     141         256 :             values.key_items.begin(); iter != values.key_items.end(); ++iter) {
     142           0 :         key.id = iter->key_id;
     143           0 :         key.value = iter->key;
     144           0 :         key.start_time = 0;
     145           0 :         keydata.AddKeyToKeyChain(key);
     146             :     }
     147         128 :     neighbor->set_keydata(keydata);
     148         128 : }
     149             : 
     150             : //
     151             : // Check if the family is allowed to be configured for BgpNeighborConfig.
     152             : // Only families inet and inet6 are allowed on non-master instances.
     153             : //
     154           0 : static bool AddressFamilyIsValid(BgpNeighborConfig *neighbor,
     155             :     const string &family) {
     156           0 :     if (neighbor->instance_name() == BgpConfigManager::kMasterInstance)
     157           0 :         return true;
     158           0 :     return (family == "inet" || family == "inet6");
     159             : }
     160             : 
     161             : //
     162             : // Build list of BgpFamilyAttributesConfig elements from the list of address
     163             : // families. This is provided for backward compatibility with configurations
     164             : // that represent each family with a simple string.
     165             : //
     166           0 : static void BuildFamilyAttributesList(BgpNeighborConfig *neighbor,
     167             :     const BgpNeighborConfig::AddressFamilyList &family_list,
     168             :     const vector<string> &remote_family_list) {
     169           0 :     BgpNeighborConfig::FamilyAttributesList family_attributes_list;
     170           0 :     BOOST_FOREACH(const string &family, family_list) {
     171             :         // Skip families that are not valid/supported for the neighbor.
     172           0 :         if (!AddressFamilyIsValid(neighbor, family))
     173           0 :             continue;
     174             : 
     175             :         // Skip families that are not configured on remote bgp-router.
     176           0 :         if (!remote_family_list.empty()) {
     177           0 :             vector<string>::const_iterator it = find(
     178             :                 remote_family_list.begin(), remote_family_list.end(), family);
     179           0 :             if (it == remote_family_list.end())
     180           0 :                 continue;
     181             :         }
     182             : 
     183           0 :         BgpFamilyAttributesConfig family_attributes(family);
     184           0 :         family_attributes_list.push_back(family_attributes);
     185           0 :     }
     186             : 
     187           0 :     neighbor->set_family_attributes_list(family_attributes_list);
     188           0 : }
     189             : 
     190             : //
     191             : // Build list of BgpFamilyAttributesConfig elements from BgpFamilyAttributes
     192             : // list in BgpSessionAttributes.
     193             : //
     194             : // Implement backward compatibility by also adding BgpFamilyAttributesConfig
     195             : // elements for families that are not in BgpFamilyAttributes list but are in
     196             : // the address_families list.
     197             : //
     198          64 : static void BuildFamilyAttributesList(BgpNeighborConfig *neighbor,
     199             :     const autogen::BgpSessionAttributes *attributes) {
     200          64 :     set<string> family_set;
     201          64 :     BgpNeighborConfig::FamilyAttributesList family_attributes_list;
     202          64 :     BOOST_FOREACH(const autogen::BgpFamilyAttributes &family_config,
     203             :         attributes->family_attributes) {
     204           0 :         if (!AddressFamilyIsValid(neighbor, family_config.address_family))
     205           0 :             continue;
     206             :         BgpFamilyAttributesConfig family_attributes(
     207           0 :             family_config.address_family);
     208           0 :         family_attributes.loop_count = family_config.loop_count;
     209           0 :         family_attributes.prefix_limit = family_config.prefix_limit.maximum;
     210           0 :         family_attributes.idle_timeout =
     211           0 :             family_config.prefix_limit.idle_timeout;
     212             :         family_attributes.default_tunnel_encap_list =
     213           0 :             family_config.default_tunnel_encap;
     214           0 :         family_attributes_list.push_back(family_attributes);
     215           0 :         family_set.insert(family_config.address_family);
     216           0 :     }
     217             : 
     218         704 :     BOOST_FOREACH(const string &family, attributes->address_families.family) {
     219         320 :         if (family_set.find(family) != family_set.end())
     220           0 :             continue;
     221         320 :         BgpFamilyAttributesConfig family_attributes(family);
     222         320 :         family_attributes_list.push_back(family_attributes);
     223         320 :     }
     224             : 
     225          64 :     neighbor->set_family_attributes_list(family_attributes_list);
     226          64 : }
     227             : 
     228             : //
     229             : // Set the autogen::BgpSessionAttributes for this BgpNeighborConfig.
     230             : //
     231             : // The autogen::BgpSession will have up to 3 session attributes - one that
     232             : // applies to the local router, one that applies the remote router and one
     233             : // that applies to both.
     234             : //
     235          64 : static void NeighborSetSessionAttributes(
     236             :     BgpNeighborConfig *neighbor, const string &localname,
     237             :     const autogen::BgpSession *session) {
     238             :     typedef vector<autogen::BgpSessionAttributes> AttributeVec;
     239          64 :     const autogen::BgpSessionAttributes *common = NULL;
     240          64 :     const autogen::BgpSessionAttributes *local = NULL;
     241          64 :     for (AttributeVec::const_iterator iter = session->attributes.begin();
     242         192 :          iter != session->attributes.end(); ++iter) {
     243         128 :         const autogen::BgpSessionAttributes *attr = iter.operator->();
     244         128 :         if (attr->bgp_router.empty()) {
     245           0 :             common = attr;
     246         256 :         } else if (neighbor->router_type() != "bgpaas-client" &&
     247         128 :             attr->bgp_router == localname) {
     248          64 :             local = attr;
     249          64 :         } else if (neighbor->router_type() == "bgpaas-client" &&
     250           0 :             attr->bgp_router == "bgpaas-server") {
     251           0 :             local = attr;
     252             :         }
     253             :     }
     254             : 
     255             :     // TODO(nsheth): local should override rather than replace common.
     256          64 :     const autogen::BgpSessionAttributes *attributes = NULL;
     257          64 :     if (common != NULL) {
     258           0 :         attributes = common;
     259          64 :     } else if (local != NULL) {
     260          64 :         attributes = local;
     261             :     }
     262          64 :     if (attributes != NULL) {
     263          64 :         neighbor->set_passive(attributes->passive);
     264          64 :         neighbor->set_as_override(attributes->as_override);
     265          64 :         neighbor->set_private_as_action(attributes->private_as_action);
     266          64 :         neighbor->set_loop_count(attributes->loop_count);
     267          64 :         if (attributes->admin_down) {
     268           0 :             neighbor->set_admin_down(true);
     269             :         }
     270          64 :         if (attributes->local_autonomous_system) {
     271           0 :             neighbor->set_local_as(attributes->local_autonomous_system);
     272             :         }
     273          64 :         if (attributes->hold_time) {
     274           0 :             neighbor->set_hold_time(attributes->hold_time);
     275             :         }
     276          64 :         BuildFamilyAttributesList(neighbor, attributes);
     277          64 :         BuildKeyChain(neighbor, attributes->auth_data);
     278          64 :         const autogen::RouteOriginOverride &origin_override =
     279             :                 attributes->route_origin_override;
     280          64 :         neighbor->SetOriginOverride(origin_override.origin_override,
     281          64 :                                     origin_override.origin);
     282             :     }
     283          64 : }
     284             : 
     285          64 : static BgpNeighborConfig *MakeBgpNeighborConfig(
     286             :     const BgpIfmapInstanceConfig *instance,
     287             :     const BgpIfmapInstanceConfig *master_instance,
     288             :     const string &local_name,
     289             :     const string &remote_name,
     290             :     const autogen::BgpRouter *local_router,
     291             :     const autogen::BgpRouter *remote_router,
     292             :     const autogen::BgpSession *session) {
     293          64 :     BgpNeighborConfig *neighbor = new BgpNeighborConfig();
     294             : 
     295          64 :     neighbor->set_instance_name(instance->name());
     296             : 
     297             :     // If the autogen::BgpSession has a uuid, we append it to the remote
     298             :     // bgp-router's name to make the BgpNeighborConfig's name unique.
     299          64 :     if (session && !session->uuid.empty()) {
     300          64 :         neighbor->set_uuid(session->uuid);
     301          64 :         neighbor->set_name(remote_name + ":" + session->uuid);
     302             :     } else {
     303           0 :         neighbor->set_name(remote_name);
     304             :     }
     305             : 
     306             :     // Store a copy of the remote bgp-router's autogen::BgpRouterParams and
     307             :     // derive the autogen::BgpSessionAttributes for the session.
     308          64 :     const autogen::BgpRouterParams &params = remote_router->parameters();
     309          64 :     neighbor->set_router_type(params.router_type);
     310          64 :     if (params.admin_down) {
     311           0 :         neighbor->set_admin_down(true);
     312             :     }
     313          64 :     if (params.local_autonomous_system) {
     314           0 :         neighbor->set_peer_as(params.local_autonomous_system);
     315             :     } else {
     316          64 :         neighbor->set_peer_as(params.autonomous_system);
     317             :     }
     318          64 :     boost::system::error_code err;
     319          64 :     neighbor->set_peer_address(
     320          64 :         IpAddress::from_string(params.address, err));
     321          64 :     if (err) {
     322           0 :         BGP_LOG_WARNING_STR(BgpConfig, BGP_LOG_FLAG_ALL,
     323             :                             "Invalid peer address " << params.address <<
     324             :                             " for neighbor " << neighbor->name());
     325             :     }
     326             : 
     327          64 :     Ip4Address identifier = Ip4Address::from_string(params.identifier, err);
     328          64 :     if (err) {
     329           0 :         BGP_LOG_WARNING_STR(BgpConfig, BGP_LOG_FLAG_ALL,
     330             :                             "Invalid peer identifier " << params.identifier <<
     331             :                             " for neighbor " << neighbor->name());
     332             :     }
     333          64 :     neighbor->set_peer_identifier(IpAddressToBgpIdentifier(identifier));
     334             : 
     335          64 :     if (params.router_type == "bgpaas-client") {
     336             :         IpAddress inet_gw_address =
     337           0 :             Ip4Address::from_string(params.gateway_address, err);
     338           0 :         if (!params.gateway_address.empty() && err) {
     339           0 :             BGP_LOG_WARNING_STR(BgpConfig, BGP_LOG_FLAG_ALL,
     340             :                                 "Invalid gateway address " <<
     341             :                                 params.gateway_address <<
     342             :                                 " for neighbor " << neighbor->name());
     343             :         } else {
     344           0 :             neighbor->set_gateway_address(Address::INET, inet_gw_address);
     345             :         }
     346             : 
     347             :         IpAddress inet6_gw_address =
     348           0 :             Ip6Address::from_string(params.ipv6_gateway_address, err);
     349           0 :         if (!params.ipv6_gateway_address.empty() && err) {
     350           0 :             BGP_LOG_WARNING_STR(BgpConfig, BGP_LOG_FLAG_ALL,
     351             :                 "Invalid ipv6 gateway address " <<
     352             :                 params.ipv6_gateway_address <<
     353             :                 " for neighbor " << neighbor->name());
     354             :         } else {
     355           0 :             neighbor->set_gateway_address(Address::INET6, inet6_gw_address);
     356             :         }
     357             :     }
     358             : 
     359          64 :     neighbor->set_port(params.port);
     360          64 :     neighbor->set_source_port(params.source_port);
     361          64 :     neighbor->set_hold_time(params.hold_time);
     362             : 
     363          64 :     if (session != NULL) {
     364          64 :         NeighborSetSessionAttributes(neighbor, local_name, session);
     365             :     }
     366             : 
     367             :     // Get the local identifier and local as from the master protocol config.
     368             :     const BgpIfmapProtocolConfig *master_protocol =
     369          64 :         master_instance->protocol_config();
     370          64 :     if (master_protocol && master_protocol->bgp_router()) {
     371             :         const autogen::BgpRouterParams &master_params =
     372          64 :             master_protocol->router_params();
     373          64 :         if (master_params.admin_down) {
     374           0 :             neighbor->set_admin_down(true);
     375             :         }
     376             :         Ip4Address localid =
     377          64 :             Ip4Address::from_string(master_params.identifier, err);
     378          64 :         if (!err) {
     379          64 :             neighbor->set_local_identifier(IpAddressToBgpIdentifier(localid));
     380             :         }
     381          64 :         if (!neighbor->local_as()) {
     382          64 :             if (master_params.local_autonomous_system) {
     383           0 :                 neighbor->set_local_as(master_params.local_autonomous_system);
     384             :             } else {
     385          64 :                 neighbor->set_local_as(master_params.autonomous_system);
     386             :             }
     387             :         }
     388          64 :         if (instance != master_instance) {
     389           0 :             neighbor->set_passive(true);
     390             :         }
     391             :     }
     392             : 
     393             :     // Get other parameters from the instance protocol config.
     394             :     // Note that there's no instance protocol config for non-master instances.
     395          64 :     const BgpIfmapProtocolConfig *protocol = instance->protocol_config();
     396          64 :     if (protocol && protocol->bgp_router()) {
     397             :         const autogen::BgpRouterParams &protocol_params =
     398          64 :             protocol->router_params();
     399          64 :         if (neighbor->family_attributes_list().empty()) {
     400           0 :             BuildFamilyAttributesList(neighbor,
     401           0 :                 protocol_params.address_families.family,
     402           0 :                 params.address_families.family);
     403             :         }
     404          64 :         if (neighbor->auth_data().Empty()) {
     405             :             const autogen::BgpRouterParams &local_params =
     406          64 :                 local_router->parameters();
     407          64 :             BuildKeyChain(neighbor, local_params.auth_data);
     408             :         }
     409             :     }
     410             : 
     411          64 :     if (neighbor->family_attributes_list().empty()) {
     412           0 :         BuildFamilyAttributesList(neighbor, default_addr_family_list,
     413           0 :             params.address_families.family);
     414             :     }
     415             : 
     416          64 :     return neighbor;
     417             : }
     418             : 
     419             : //
     420             : // Build map of BgpNeighborConfigs based on the data in autogen::BgpPeering.
     421             : //
     422          64 : void BgpIfmapPeeringConfig::BuildNeighbors(BgpIfmapConfigManager *manager,
     423             :     const autogen::BgpRouter *local_rt_config,
     424             :     const string &peername, const autogen::BgpRouter *remote_rt_config,
     425             :     const autogen::BgpPeering *peering, NeighborMap *map) {
     426             :     const BgpIfmapInstanceConfig *master_instance =
     427          64 :         manager->config()->FindInstance(BgpConfigManager::kMasterInstance);
     428             : 
     429             :     // If there are one or more autogen::BgpSessions for the peering, use
     430             :     // those to create the BgpNeighborConfigs.
     431          64 :     const autogen::BgpPeeringAttributes &attr = peering->data();
     432          64 :     for (autogen::BgpPeeringAttributes::const_iterator iter = attr.begin();
     433         128 :          iter != attr.end(); ++iter) {
     434          64 :         BgpNeighborConfig *neighbor = MakeBgpNeighborConfig(
     435          64 :             instance_, master_instance, manager->localname(), peername,
     436          64 :             local_rt_config, remote_rt_config, iter.operator->());
     437          64 :         map->insert(make_pair(neighbor->name(), neighbor));
     438             :     }
     439             : 
     440             :     // When no sessions are present, create a single BgpNeighborConfig with
     441             :     // no per-session configuration.
     442          64 :     if (map->empty()) {
     443           0 :         BgpNeighborConfig *neighbor = MakeBgpNeighborConfig(
     444           0 :             instance_, master_instance, manager->localname(), peername,
     445           0 :             local_rt_config, remote_rt_config, NULL);
     446           0 :         map->insert(make_pair(neighbor->name(), neighbor));
     447             :     }
     448          64 : }
     449             : 
     450             : //
     451             : // Update BgpIfmapPeeringConfig based on updated autogen::BgpPeering.
     452             : //
     453             : // This mainly involves building future BgpNeighborConfigs and doing a diff of
     454             : // the current and future BgpNeighborConfigs.  Note that BgpIfmapInstanceConfig
     455             : // also has references to BgpNeighborConfigs, so it also needs to be updated as
     456             : // part of the process.
     457             : //
     458          64 : void BgpIfmapPeeringConfig::Update(BgpIfmapConfigManager *manager,
     459             :     const autogen::BgpPeering *peering) {
     460          64 :     IFMapNode *node = node_proxy_.node();
     461          64 :     assert(node != NULL);
     462          64 :     bgp_peering_.reset(peering);
     463             : 
     464             :     // Build the future NeighborMap.  The future map should be empty if the
     465             :     // bgp-peering is deleted or if the parameters for the remote bgp-router
     466             :     // are not available.
     467          64 :     NeighborMap future;
     468          64 :     pair<IFMapNode *, IFMapNode *> routers;
     469         128 :     if (!node->IsDeleted() &&
     470          64 :         GetRouterPair(manager->graph(), manager->localname(), node, &routers)) {
     471             :         const autogen::BgpRouter *local_rt_config =
     472             :                 static_cast<const autogen::BgpRouter *>(
     473          64 :                     routers.first->GetObject());
     474             :         const autogen::BgpRouter *remote_rt_config =
     475             :                 static_cast<const autogen::BgpRouter *>(
     476          64 :                     routers.second->GetObject());
     477         128 :         if (local_rt_config &&
     478          64 :             local_rt_config->IsPropertySet(autogen::BgpRouter::PARAMETERS) &&
     479         128 :             remote_rt_config &&
     480          64 :             remote_rt_config->IsPropertySet(autogen::BgpRouter::PARAMETERS)) {
     481          64 :             BuildNeighbors(manager, local_rt_config, routers.second->name(),
     482             :                            remote_rt_config, peering, &future);
     483             :         }
     484             :     }
     485             : 
     486             :     // Swap out the NeighborMap in preparation for doing a diff.
     487          64 :     NeighborMap current;
     488          64 :     current.swap(neighbors_);
     489             : 
     490             :     // Do a diff on the current and future BgpNeighborConfigs, making sure
     491             :     // that the BgpIfmapInstanceConfig is updated accordingly.  We add any new
     492             :     // BgpNeighborConfigs to our NeighborMap.
     493          64 :     NeighborMap::iterator it1 = current.begin();
     494          64 :     NeighborMap::iterator it2 = future.begin();
     495          96 :     while (it1 != current.end() && it2 != future.end()) {
     496          32 :         if (it1->first < it2->first) {
     497           0 :             BgpNeighborConfig *prev = it1->second;
     498           0 :             instance_->DeleteNeighbor(manager, prev);
     499           0 :             ++it1;
     500          32 :         } else if (it1->first > it2->first) {
     501           0 :             BgpNeighborConfig *neighbor = it2->second;
     502           0 :             instance_->AddNeighbor(manager, neighbor);
     503           0 :             neighbors_.insert(*it2);
     504           0 :             it2->second = NULL;
     505           0 :             ++it2;
     506             :         } else {
     507          32 :             BgpNeighborConfig *neighbor = it1->second;
     508          32 :             BgpNeighborConfig *update = it2->second;
     509          32 :             if (*neighbor != *update) {
     510           0 :                 instance_->ChangeNeighbor(manager, update);
     511           0 :                 neighbors_.insert(*it2);
     512           0 :                 it2->second = NULL;
     513             :             } else {
     514          32 :                 neighbors_.insert(*it1);
     515          32 :                 it1->second = NULL;
     516             :             }
     517          32 :             ++it1;
     518          32 :             ++it2;
     519             :         }
     520             :     }
     521          64 :     for (; it1 != current.end(); ++it1) {
     522           0 :         BgpNeighborConfig *prev = it1->second;
     523           0 :         instance_->DeleteNeighbor(manager, prev);
     524             :     }
     525          96 :     for (; it2 != future.end(); ++it2) {
     526          32 :         BgpNeighborConfig *neighbor = it2->second;
     527          32 :         instance_->AddNeighbor(manager, neighbor);
     528          32 :         neighbors_.insert(*it2);
     529          32 :         it2->second = NULL;
     530             :     }
     531             : 
     532             :     // Get rid of the current and future NeighborMaps and destroy any mapped
     533             :     // BgpNeighborConfigs. Note that we have carefully reset mapped values to
     534             :     // NULL above when we don't want a BgpNeighborConfig to get destroyed.
     535          64 :     STLDeleteElements(&current);
     536          64 :     STLDeleteElements(&future);
     537          64 : }
     538             : 
     539             : //
     540             : // Delete all state for the given BgpPeeringConfig.
     541             : //
     542             : // This mainly involves getting rid of BgpNeighborConfigs in the NeighborMap.
     543             : //
     544           0 : void BgpIfmapPeeringConfig::Delete(BgpIfmapConfigManager *manager) {
     545           0 :     NeighborMap current;
     546           0 :     current.swap(neighbors_);
     547           0 :     for (NeighborMap::iterator iter = current.begin();
     548           0 :          iter != current.end(); ++iter) {
     549           0 :         instance_->DeleteNeighbor(manager, iter->second);
     550             :     }
     551           0 :     STLDeleteElements(&current);
     552           0 :     bgp_peering_.reset();
     553           0 : }
     554             : 
     555             : //
     556             : // Find the IFMapNodes for a bgp-peering.
     557             : //
     558             : // The "node" is the IFMapNode for the bgp-peering link.  The bgp-peering is
     559             : // interesting only if one of the bgp-routers is the local node.
     560             : //
     561             : // Return true if both bgp-routers for the bgp-peering exist and one of them
     562             : // is the local one. Also fill in the local and remote IFMapNode pointers if
     563             : // we return true.
     564             : //
     565         144 : bool BgpIfmapPeeringConfig::GetRouterPair(DBGraph *db_graph,
     566             :     const string  &localname, IFMapNode *node,
     567             :     pair<IFMapNode *, IFMapNode *> *pair) {
     568         144 :     IFMapNode *local = NULL;
     569         144 :     IFMapNode *remote = NULL;
     570         144 :     string local_router_type;
     571         144 :     string remote_router_type;
     572         144 :     string local_instance;
     573         144 :     string remote_instance;
     574             : 
     575         144 :     for (DBGraphVertex::adjacency_iterator iter = node->begin(db_graph);
     576         432 :          iter != node->end(db_graph); ++iter) {
     577         288 :         IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
     578         288 :         if (strcmp(adj->table()->Typename(), "bgp-router") != 0)
     579           0 :             continue;
     580             :         autogen::BgpRouter *router =
     581         288 :             static_cast<autogen::BgpRouter *>(adj->GetObject());
     582         288 :         if (!router)
     583           0 :             continue;
     584         288 :         const autogen::BgpRouterParams &params = router->parameters();
     585         288 :         string instance_name(IdentifierParent(adj->name()));
     586         288 :         string name = adj->name().substr(instance_name.size() + 1);
     587         288 :         if (name == localname && params.router_type != "bgpaas-client") {
     588          96 :             local = adj;
     589          96 :             local_router_type = params.router_type;
     590          96 :             local_instance = instance_name;
     591         192 :         } else if (instance_name != BgpConfigManager::kMasterInstance &&
     592           0 :             params.router_type == "bgpaas-server") {
     593           0 :             local = adj;
     594           0 :             local_router_type = params.router_type;
     595           0 :             local_instance = instance_name;
     596             :         } else {
     597         192 :             remote = adj;
     598         192 :             remote_router_type = params.router_type;
     599         192 :             remote_instance = instance_name;
     600             :         }
     601         288 :     }
     602             : 
     603          96 :     if ((local == NULL || remote == NULL) || (local_router_type ==
     604          96 :          "bgpaas-server" && remote_router_type != "bgpaas-client") ||
     605         192 :         (local_router_type != "bgpaas-server" && remote_router_type ==
     606         240 :          "bgpaas-client") || (local_instance != remote_instance)) {
     607          48 :         BGP_LOG_STR(BgpConfig, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_SYSLOG,
     608             :                  "localname: " << localname <<
     609             :                  ((local == NULL) ? " Local node not present" :
     610             :                                   " Local node present") <<
     611             :                  ((remote == NULL) ? " Remote node not present" :
     612             :                                   " Remote node present") <<
     613             :                  " local_router_type: " << local_router_type <<
     614             :                  " remote_router_type: " << remote_router_type <<
     615             :                  " local instance: " << local_instance <<
     616             :                  " remote instance: " << remote_instance);
     617          48 :         return false;
     618             :     }
     619             : 
     620          96 :     pair->first = local;
     621          96 :     pair->second = remote;
     622          96 :     return true;
     623         144 : }
     624             : 
     625             : //
     626             : // Constructor for BgpIfmapProtocolConfig.
     627             : //
     628          20 : BgpIfmapProtocolConfig::BgpIfmapProtocolConfig(BgpIfmapInstanceConfig *instance)
     629          20 :     : instance_(instance),
     630          20 :       data_(instance->name()) {
     631          20 : }
     632             : 
     633             : //
     634             : // Destructor for BgpIfmapProtocolConfig.
     635             : //
     636          20 : BgpIfmapProtocolConfig::~BgpIfmapProtocolConfig() {
     637          20 : }
     638             : 
     639         568 : const autogen::BgpRouterParams &BgpIfmapProtocolConfig::router_params() const {
     640         568 :     return bgp_router_->parameters();
     641             : }
     642             : 
     643             : //
     644             : // Set the IFMapNodeProxy for the BgpIfmapProtocolConfig.
     645             : //
     646          20 : void BgpIfmapProtocolConfig::SetNodeProxy(IFMapNodeProxy *proxy) {
     647          20 :     if (proxy != NULL) {
     648          20 :         node_proxy_.Swap(proxy);
     649             :     }
     650          20 : }
     651             : 
     652             : //
     653             : // Update autogen::BgpRouter object for this BgpIfmapProtocolConfig.
     654             : //
     655          40 : void BgpIfmapProtocolConfig::Update(BgpIfmapConfigManager *manager,
     656             :     const autogen::BgpRouter *router) {
     657          40 :     bgp_router_.reset(router);
     658          40 :     const autogen::BgpRouterParams &params = router->parameters();
     659          40 :     data_.set_admin_down(params.admin_down);
     660          40 :     data_.set_cluster_id(params.cluster_id);
     661          40 :     data_.set_autonomous_system(params.autonomous_system);
     662          40 :     data_.set_local_autonomous_system(params.local_autonomous_system);
     663          40 :     data_.set_port(params.port ?: BgpConfigManager::kDefaultPort);
     664          40 :     boost::system::error_code err;
     665          40 :     IpAddress identifier = IpAddress::from_string(params.identifier, err);
     666          40 :     if (!err) {
     667          20 :         data_.set_identifier(IpAddressToBgpIdentifier(identifier));
     668             :     }
     669          40 :     data_.set_hold_time(params.hold_time);
     670             : 
     671             :     // reset subcluster name and id,
     672             :     // will be set again if mapping is found in schema
     673          40 :     data_.reset_subcluster_name();
     674          40 :     data_.reset_subcluster_id();
     675             :     // get subcluster name this router is associated with
     676          40 :     IFMapNode *node = node_proxy_.node();
     677          40 :     if (!node)
     678          20 :         return;
     679          20 :     DBGraph *graph = manager->graph();
     680          20 :     for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
     681          68 :          iter != node->end(graph); ++iter) {
     682          48 :         IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
     683          48 :         if (strcmp(adj->table()->Typename(), "sub-cluster") == 0) {
     684           0 :             data_.set_subcluster_name(adj->name());
     685             :             const autogen::SubCluster *sc =
     686           0 :                 static_cast<autogen::SubCluster *>(adj->GetObject());
     687           0 :             if (sc) {
     688           0 :                 data_.set_subcluster_id(sc->id());
     689             :             }
     690           0 :             break;
     691             :         }
     692             :     }
     693             : }
     694             : 
     695             : //
     696             : // Delete autogen::BgpRouter object for this BgpIfmapProtocolConfig.
     697             : //
     698           0 : void BgpIfmapProtocolConfig::Delete(BgpIfmapConfigManager *manager) {
     699           0 :     manager->Notify(&data_, BgpConfigManager::CFG_DELETE);
     700           0 :     bgp_router_.reset();
     701           0 : }
     702             : 
     703           0 : const string &BgpIfmapProtocolConfig::InstanceName() const {
     704           0 :     return instance_->name();
     705             : }
     706             : 
     707             : //
     708             : // Constructor for BgpIfmapInstanceConfig.
     709             : //
     710         100 : BgpIfmapInstanceConfig::BgpIfmapInstanceConfig(const string &name)
     711         100 :     : name_(name),
     712         100 :       data_(name),
     713         100 :       index_(-1),
     714         200 :       protocol_(NULL) {
     715         100 : }
     716             : 
     717             : //
     718             : // Destructor for BgpIfmapInstanceConfig.
     719             : //
     720         100 : BgpIfmapInstanceConfig::~BgpIfmapInstanceConfig() {
     721         100 : }
     722             : 
     723             : //
     724             : // Set the IFMapNodeProxy for the BgpIfmapInstanceConfig.
     725             : //
     726         100 : void BgpIfmapInstanceConfig::SetNodeProxy(IFMapNodeProxy *proxy) {
     727         100 :     if (proxy != NULL) {
     728         100 :         node_proxy_.Swap(proxy);
     729             :     }
     730         100 : }
     731             : 
     732             : //
     733             : // Get the BgpIfmapProtocolConfig for this BgpIfmapInstanceConfig, create
     734             : // it if needed.
     735             : //
     736          20 : BgpIfmapProtocolConfig *BgpIfmapInstanceConfig::LocateProtocol() {
     737          20 :     if (protocol_.get() == NULL) {
     738          20 :         protocol_.reset(new BgpIfmapProtocolConfig(this));
     739             :     }
     740          20 :     return protocol_.get();
     741             : }
     742             : 
     743             : //
     744             : // Delete the BgpIfmapProtocolConfig for this BgpIfmapInstanceConfig.
     745             : //
     746           0 : void BgpIfmapInstanceConfig::ResetProtocol() {
     747           0 :     protocol_.reset();
     748           0 : }
     749             : 
     750             : //
     751             : // Get the route-target for an instance-target. The input IFMapNode is the
     752             : // midnode that represents the instance-target link. We traverse the graph
     753             : // the graph edges till we find the adjacency to the route-target.
     754             : //
     755             : // Return true and fill in the target string if we find the route-target.
     756             : //
     757         638 : static bool GetInstanceTargetRouteTarget(DBGraph *graph, IFMapNode *node,
     758             :     string *target) {
     759         638 :     for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
     760        1276 :          iter != node->end(graph); ++iter) {
     761        1276 :         IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
     762        1276 :         if (strcmp(adj->table()->Typename(), "route-target") == 0) {
     763         638 :             *target = adj->name();
     764         638 :             return true;
     765             :         }
     766             :     }
     767           0 :     return false;
     768             : }
     769             : 
     770             : //
     771             : // Fill in all the export route targets for a routing-instance.  The input
     772             : // IFMapNode represents the routing-instance.  We traverse the graph edges
     773             : // and look for instance-target adjacencies. If the instance-target is an
     774             : // export and import target, add it to the vector.
     775             : //
     776             : // Note that we purposely skip adding export only targets to the vector.
     777             : // Reasoning is that export only targets are manually configured by users
     778             : // and hence should not be imported based on policy.
     779             : //
     780           0 : static void GetRoutingInstanceExportTargets(DBGraph *graph, IFMapNode *node,
     781             :     vector<string> *target_list) {
     782           0 :     for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
     783           0 :          iter != node->end(graph); ++iter) {
     784           0 :         IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
     785           0 :         string target;
     786           0 :         if ((strcmp(adj->table()->Typename(), "instance-target") == 0) &&
     787           0 :             (GetInstanceTargetRouteTarget(graph, adj, &target))) {
     788             :             const autogen::InstanceTarget *itarget =
     789           0 :                     dynamic_cast<autogen::InstanceTarget *>(adj->GetObject());
     790           0 :             if (!itarget)
     791           0 :                 continue;
     792           0 :             const autogen::InstanceTargetType &itt = itarget->data();
     793           0 :             if (itt.import_export.empty())
     794           0 :                 target_list->push_back(target);
     795             :         }
     796           0 :     }
     797           0 : }
     798             : 
     799             : //
     800             : // Fill in all the export route targets of the routing-instance at the other
     801             : // end of a connection.  The src_node is the routing-instance from which we
     802             : // reached the connection node. The name of the source routing-instance is
     803             : // src_instance.
     804             : //
     805             : // If the connection is unidirectional and the destination-instance for the
     806             : // connection is not the routing-instance from which we started, we should
     807             : // not get any targets from this connection.  This is what a unidirectional
     808             : // connection means.
     809             : //
     810           0 : static void GetConnectionExportTargets(DBGraph *graph, IFMapNode *src_node,
     811             :     const string &src_instance, IFMapNode *node,
     812             :     vector<string> *target_list) {
     813             :     const autogen::Connection *conn =
     814           0 :         dynamic_cast<autogen::Connection *>(node->GetObject());
     815           0 :     if (!conn)
     816           0 :         return;
     817           0 :     const autogen::ConnectionType &conn_type = conn->data();
     818           0 :     if (!conn_type.destination_instance.empty() &&
     819           0 :          conn_type.destination_instance != src_instance)
     820           0 :         return;
     821           0 :     for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
     822           0 :          iter != node->end(graph); ++iter) {
     823           0 :         IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
     824           0 :         if (adj == src_node)
     825           0 :             continue;
     826           0 :         if (strcmp(adj->table()->Typename(), "routing-instance") != 0)
     827           0 :             continue;
     828           0 :         GetRoutingInstanceExportTargets(graph, adj, target_list);
     829             :     }
     830             : }
     831             : 
     832             : //
     833             : // Fill in all the routing-policies for a routing-instance.  The input
     834             : // IFMapNode represents the routing-policy-routing-instance.  We traverse to
     835             : // graph edges and look for routing-policy adjacency
     836             : //
     837           0 : static bool GetRoutingInstanceRoutingPolicy(DBGraph *graph, IFMapNode *node,
     838             :     RoutingPolicyAttachInfo *ri_rp_link) {
     839           0 :     string sequence;
     840             :     const autogen::RoutingPolicyRoutingInstance *policy =
     841           0 :         static_cast<autogen::RoutingPolicyRoutingInstance *>(node->GetObject());
     842           0 :     const autogen::RoutingPolicyType &attach_info = policy->data();
     843             : 
     844           0 :     ri_rp_link->sequence_ = attach_info.sequence;
     845             : 
     846           0 :     for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
     847           0 :          iter != node->end(graph); ++iter) {
     848           0 :         IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
     849           0 :         if (strcmp(adj->table()->Typename(), "routing-policy") == 0) {
     850           0 :             ri_rp_link->routing_policy_ = adj->name();
     851           0 :             return true;
     852             :         }
     853             :     }
     854           0 :     return false;
     855           0 : }
     856             : 
     857             : //
     858             : // Fill in all the route-aggregation for a routing-instance.
     859             : //
     860           0 : static bool GetRouteAggregateConfig(DBGraph *graph, IFMapNode *node,
     861             :     BgpInstanceConfig::AggregateRouteList *inet_list,
     862             :     BgpInstanceConfig::AggregateRouteList *inet6_list) {
     863             :     const autogen::RouteAggregate *ra =
     864           0 :         static_cast<autogen::RouteAggregate *>(node->GetObject());
     865           0 :     if (ra == NULL) return false;
     866             : 
     867           0 :     boost::system::error_code ec;
     868             :     IpAddress nexthop =
     869           0 :         IpAddress::from_string(ra->aggregate_route_nexthop(), ec);
     870           0 :     if (ec.failed()) return false;
     871             : 
     872           0 :     BOOST_FOREACH(const string &route, ra->aggregate_route_entries()) {
     873           0 :         AggregateRouteConfig aggregate;
     874           0 :         aggregate.nexthop = nexthop;
     875             : 
     876           0 :         Ip4Address address;
     877           0 :         ec = Ip4SubnetParse(route, &address, &aggregate.prefix_length);
     878           0 :         if (!ec) {
     879           0 :             if (!nexthop.is_v4()) continue;
     880           0 :             aggregate.aggregate = address;
     881           0 :             inet_list->push_back(aggregate);
     882             :         } else {
     883           0 :             if (!nexthop.is_v6()) continue;
     884           0 :             Ip6Address address;
     885           0 :             ec = Inet6SubnetParse(route, &address, &aggregate.prefix_length);
     886           0 :             if (ec.failed()) continue;
     887           0 :             aggregate.aggregate = address;
     888           0 :             inet6_list->push_back(aggregate);
     889             :         }
     890             :     }
     891             : 
     892           0 :     return true;
     893             : }
     894             : 
     895             : //
     896             : // Get the network id for a virtual-network.  The input IFMapNode represents
     897             : // the virtual-network.
     898             : //
     899           0 : static int GetVirtualNetworkIndex(DBGraph *graph, IFMapNode *node) {
     900             :     const autogen::VirtualNetwork *vn =
     901           0 :         static_cast<autogen::VirtualNetwork *>(node->GetObject());
     902           0 :     if (vn && vn->IsPropertySet(autogen::VirtualNetwork::NETWORK_ID))
     903           0 :         return vn->network_id();
     904           0 :     if (vn && vn->IsPropertySet(autogen::VirtualNetwork::PROPERTIES))
     905           0 :         return vn->properties().network_id;
     906           0 :     return 0;
     907             : }
     908             : 
     909             : //
     910             : // Check if a virtual-network allows transit. The input IFMapNode represents
     911             : // the virtual-network.
     912             : //
     913           0 : static bool GetVirtualNetworkAllowTransit(DBGraph *graph, IFMapNode *node) {
     914             :     const autogen::VirtualNetwork *vn =
     915           0 :         static_cast<autogen::VirtualNetwork *>(node->GetObject());
     916           0 :     if (vn && vn->IsPropertySet(autogen::VirtualNetwork::PROPERTIES))
     917           0 :         return vn->properties().allow_transit;
     918           0 :     return false;
     919             : }
     920             : 
     921             : 
     922             : //
     923             : // Check if a virtual-network has pbb-evpn enabled.
     924             : // The input IFMapNode represents the virtual-network.
     925             : //
     926           0 : static bool GetVirtualNetworkPbbEvpnEnable(DBGraph *graph, IFMapNode *node) {
     927             :     const autogen::VirtualNetwork *vn =
     928           0 :         static_cast<autogen::VirtualNetwork *>(node->GetObject());
     929           0 :     if (vn && vn->IsPropertySet(autogen::VirtualNetwork::PBB_EVPN_ENABLE))
     930           0 :         return vn->pbb_evpn_enable();
     931           0 :     return false;
     932             : }
     933             : 
     934             : 
     935             : //
     936             : // Get the vxlan id for a virtual-network.  The input IFMapNode represents
     937             : // the virtual-network.
     938             : //
     939             : // The vxlan_network_identifier is 0 when automatic mode is in use. In that
     940             : // case, the network_id is used as vxlan id.
     941             : //
     942           0 : static int GetVirtualNetworkVxlanId(DBGraph *graph, IFMapNode *node) {
     943             :     const autogen::VirtualNetwork *vn =
     944           0 :         static_cast<autogen::VirtualNetwork *>(node->GetObject());
     945           0 :     if (vn && vn->IsPropertySet(autogen::VirtualNetwork::PROPERTIES)) {
     946           0 :         if (vn->properties().vxlan_network_identifier) {
     947           0 :             return vn->properties().vxlan_network_identifier;
     948             :         } else {
     949           0 :             return vn->properties().network_id;
     950             :         }
     951             :     }
     952           0 :     return 0;
     953             : }
     954             : 
     955           0 : static void SetStaticRouteConfig(BgpInstanceConfig *rti,
     956             :     const autogen::RoutingInstance *config) {
     957           0 :     BgpInstanceConfig::StaticRouteList inet_list;
     958           0 :     BgpInstanceConfig::StaticRouteList inet6_list;
     959           0 :     BOOST_FOREACH(const autogen::StaticRouteType &route,
     960             :                   config->static_route_entries()) {
     961           0 :         boost::system::error_code ec;
     962           0 :         StaticRouteConfig item;
     963           0 :         item.nexthop = IpAddress::from_string(route.next_hop, ec);
     964           0 :         if (ec.failed())
     965           0 :             continue;
     966             : 
     967           0 :         item.route_targets = route.route_target;
     968           0 :         item.communities = route.community;
     969           0 :         if (item.nexthop.is_v4()) {
     970           0 :             Ip4Address address;
     971           0 :             ec = Ip4SubnetParse(route.prefix, &address, &item.prefix_length);
     972           0 :             if (ec.failed())
     973           0 :                 continue;
     974           0 :             item.address = address;
     975             :             std::pair<BgpInstanceConfig::StaticRouteList::iterator, bool> ret =
     976           0 :                 inet_list.insert(item);
     977           0 :             if (!ret.second) {
     978           0 :                 BGP_LOG_WARNING_STR(BgpConfig, BGP_LOG_FLAG_ALL,
     979             :                     "Duplicate static route prefix " << route.prefix <<
     980             :                     " with nexthop " << route.next_hop <<
     981             :                     " for routing instance " << rti->name());
     982             :             }
     983             :         } else {
     984           0 :             Ip6Address address;
     985           0 :             ec = Inet6SubnetParse(route.prefix, &address, &item.prefix_length);
     986           0 :             if (ec.failed())
     987           0 :                 continue;
     988           0 :             item.address = address;
     989             :             std::pair<BgpInstanceConfig::StaticRouteList::iterator, bool> ret =
     990           0 :                 inet6_list.insert(item);
     991           0 :             if (!ret.second) {
     992           0 :                 BGP_LOG_WARNING_STR(BgpConfig, BGP_LOG_FLAG_ALL,
     993             :                     "Duplicate static route prefix " << route.prefix <<
     994             :                     " with nexthop " << route.next_hop <<
     995             :                     " for routing instance " << rti->name());
     996             :             }
     997             :         }
     998           0 :     }
     999           0 :     rti->swap_static_routes(Address::INET, &inet_list);
    1000           0 :     rti->swap_static_routes(Address::INET6, &inet6_list);
    1001           0 : }
    1002             : 
    1003           0 : static void SetServiceChainConfig(BgpInstanceConfig *rti,
    1004             :     const autogen::RoutingInstance *config) {
    1005           0 :     BgpInstanceConfig::ServiceChainList list;
    1006             :     const autogen::ServiceChainInfo &inet_chain =
    1007           0 :         config->service_chain_information();
    1008           0 :     if (config->IsPropertySet(
    1009             :         autogen::RoutingInstance::SERVICE_CHAIN_INFORMATION)) {
    1010           0 :         ServiceChainConfig item = {
    1011             :             SCAddress::INET,
    1012           0 :             inet_chain.routing_instance,
    1013           0 :             inet_chain.prefix,
    1014           0 :             inet_chain.service_chain_address,
    1015           0 :             inet_chain.service_instance,
    1016           0 :             inet_chain.source_routing_instance,
    1017           0 :             inet_chain.service_chain_id,
    1018           0 :             inet_chain.sc_head,
    1019           0 :             inet_chain.retain_as_path,
    1020           0 :         };
    1021           0 :         list.push_back(item);
    1022           0 :     }
    1023             : 
    1024             :     const autogen::ServiceChainInfo &inet6_chain =
    1025           0 :         config->ipv6_service_chain_information();
    1026           0 :     if (config->IsPropertySet(
    1027             :         autogen::RoutingInstance::IPV6_SERVICE_CHAIN_INFORMATION)) {
    1028           0 :         ServiceChainConfig item = {
    1029             :             SCAddress::INET6,
    1030           0 :             inet6_chain.routing_instance,
    1031           0 :             inet6_chain.prefix,
    1032           0 :             inet6_chain.service_chain_address,
    1033           0 :             inet6_chain.service_instance,
    1034           0 :             inet6_chain.source_routing_instance,
    1035           0 :             inet6_chain.service_chain_id,
    1036           0 :             inet6_chain.sc_head,
    1037           0 :             inet6_chain.retain_as_path,
    1038           0 :         };
    1039           0 :         list.push_back(item);
    1040           0 :     }
    1041             : 
    1042             :     const autogen::ServiceChainInfo &evpn_chain =
    1043           0 :         config->evpn_service_chain_information();
    1044           0 :     if (config->IsPropertySet(
    1045             :         autogen::RoutingInstance::EVPN_SERVICE_CHAIN_INFORMATION)) {
    1046           0 :         ServiceChainConfig item = {
    1047             :             SCAddress::EVPN,
    1048           0 :             evpn_chain.routing_instance,
    1049           0 :             evpn_chain.prefix,
    1050           0 :             evpn_chain.service_chain_address,
    1051           0 :             evpn_chain.service_instance,
    1052           0 :             evpn_chain.source_routing_instance,
    1053           0 :             evpn_chain.service_chain_id,
    1054           0 :             evpn_chain.sc_head,
    1055           0 :             evpn_chain.retain_as_path,
    1056           0 :         };
    1057           0 :         list.push_back(item);
    1058           0 :     }
    1059             : 
    1060             :     const autogen::ServiceChainInfo &evpnv6_chain =
    1061           0 :         config->evpn_ipv6_service_chain_information();
    1062           0 :     if (config->IsPropertySet(
    1063             :         autogen::RoutingInstance::EVPN_IPV6_SERVICE_CHAIN_INFORMATION)) {
    1064           0 :         ServiceChainConfig item = {
    1065             :             SCAddress::EVPN6,
    1066           0 :             evpnv6_chain.routing_instance,
    1067           0 :             evpnv6_chain.prefix,
    1068           0 :             evpnv6_chain.service_chain_address,
    1069           0 :             evpnv6_chain.service_instance,
    1070           0 :             evpnv6_chain.source_routing_instance,
    1071           0 :             evpnv6_chain.service_chain_id,
    1072           0 :             evpnv6_chain.sc_head,
    1073           0 :             evpnv6_chain.retain_as_path,
    1074           0 :         };
    1075           0 :         list.push_back(item);
    1076           0 :     }
    1077             : 
    1078           0 :     rti->swap_service_chain_list(&list);
    1079           0 : }
    1080             : 
    1081             : // Get name of vxlan RI for bridge RI
    1082             : 
    1083           0 : static string GetConnectedVxlanRiVn(DBGraph *graph, IFMapNode *node) {
    1084             :     const autogen::VirtualNetwork *vn_vxlan =
    1085           0 :         static_cast<autogen::VirtualNetwork *>(node->GetObject());
    1086           0 :     if (!vn_vxlan)
    1087           0 :         return "";
    1088           0 :     for (DBGraphVertex::adjacency_iterator iter_ri = node->begin(graph);
    1089           0 :         iter_ri != node->end(graph); ++iter_ri) {
    1090           0 :         IFMapNode *adj_ri = static_cast<IFMapNode *>(iter_ri.operator->());
    1091           0 :         if (strcmp(adj_ri->table()->Typename(), "routing-instance") == 0)  {
    1092             :             const autogen::RoutingInstance *iri =
    1093           0 :                     dynamic_cast<autogen::RoutingInstance *>(adj_ri->GetObject());
    1094           0 :             if (!iri)
    1095           0 :                 continue;
    1096           0 :             return adj_ri->name();
    1097             :         }
    1098             :     }
    1099           0 :     return "";
    1100             : }
    1101             : 
    1102           0 : static string GetConnectedVxlanRiLr(DBGraph *graph, IFMapNode *node) {
    1103           0 :     std::string ri_name = "";
    1104           0 :     for (DBGraphVertex::adjacency_iterator iter_lr = node->begin(graph);
    1105           0 :         iter_lr != node->end(graph); ++iter_lr) {
    1106           0 :         IFMapNode *adj_lr = static_cast<IFMapNode *>(iter_lr.operator->());
    1107           0 :         if (strcmp(adj_lr->table()->Typename(),
    1108           0 :                    "logical-router-virtual-network") == 0)  {
    1109           0 :             for (DBGraphVertex::adjacency_iterator iter_vn = adj_lr->begin(graph);
    1110           0 :                 iter_vn != adj_lr->end(graph); ++iter_vn) {
    1111           0 :                 IFMapNode *adj_vn = static_cast<IFMapNode *>(iter_vn.operator->());
    1112           0 :                 if (strcmp(adj_vn->table()->Typename(), "virtual-network") == 0)  {
    1113           0 :                     ri_name = GetConnectedVxlanRiVn(graph, adj_vn);
    1114           0 :                     if (ri_name != "") {
    1115           0 :                         return ri_name;
    1116             :                     }
    1117             :                 }
    1118             :             }
    1119             :         }
    1120             :     }
    1121           0 :     return "";
    1122           0 : }
    1123             : 
    1124             : 
    1125           0 : static string GetConnectedVxlanRi(DBGraph *graph, IFMapNode *node) {
    1126           0 :     std::string ri_name = "";
    1127             :     const autogen::VirtualNetwork *vn =
    1128           0 :         static_cast<autogen::VirtualNetwork *>(node->GetObject());
    1129           0 :     if (!vn) {
    1130           0 :         return "";
    1131             :     }
    1132           0 :     for (DBGraphVertex::adjacency_iterator iter_vmi = node->begin(graph);
    1133           0 :          iter_vmi != node->end(graph); ++iter_vmi) {
    1134           0 :         IFMapNode *adj_vmi = static_cast<IFMapNode *>(iter_vmi.operator->());
    1135           0 :         if (strcmp(adj_vmi->table()->Typename(),
    1136           0 :                    "virtual-machine-interface") == 0) {
    1137           0 :             for (DBGraphVertex::adjacency_iterator iter_lr = adj_vmi->begin(graph);
    1138           0 :                 iter_lr != adj_vmi->end(graph); ++iter_lr) {
    1139           0 :                 IFMapNode *adj_lr = static_cast<IFMapNode *>(iter_lr.operator->());
    1140           0 :                 if (strcmp(adj_lr->table()->Typename(), "logical-router") == 0)  {
    1141           0 :                     ri_name = GetConnectedVxlanRiLr(graph, adj_lr);
    1142           0 :                     if (ri_name != "") {
    1143           0 :                         return ri_name;
    1144             :                     }
    1145             :                 }
    1146             :             }
    1147             :         }
    1148             :     }
    1149           0 :     return "";
    1150           0 : }
    1151             : 
    1152             : 
    1153           0 : void BgpIfmapConfigManager::ProcessRoutingPolicyLink(
    1154             :     const BgpConfigDelta &delta) {
    1155           0 :     CHECK_CONCURRENCY("bgp::Config");
    1156             : 
    1157             :     BgpIfmapRoutingPolicyLinkConfig *ri_rp_link
    1158           0 :         = config_->FindRoutingPolicyLink(delta.id_name);
    1159           0 :     if (ri_rp_link == NULL) {
    1160           0 :         IFMapNodeProxy *proxy = delta.node.get();
    1161           0 :         if (proxy == NULL) {
    1162           0 :             return;
    1163             :         }
    1164           0 :         IFMapNode *node = proxy->node();
    1165           0 :         if (node == NULL || delta.obj.get() == NULL) {
    1166           0 :             return;
    1167             :         }
    1168             : 
    1169           0 :         pair<IFMapNode *, IFMapNode *> ri_rp_pair;
    1170           0 :         if (!BgpIfmapRoutingPolicyLinkConfig::GetInstancePolicyPair(db_graph_,
    1171             :             node, &ri_rp_pair)) {
    1172           0 :             return;
    1173             :         }
    1174             : 
    1175           0 :         string instance_name = ri_rp_pair.first->name();
    1176           0 :         string policy_name = ri_rp_pair.second->name();
    1177             : 
    1178             :         BgpIfmapInstanceConfig *rti  =
    1179           0 :             config_->FindInstance(instance_name);
    1180             :         BgpIfmapRoutingPolicyConfig *rtp  =
    1181           0 :             config_->FindRoutingPolicy(policy_name);
    1182           0 :         if (!rti || !rtp) {
    1183           0 :             return;
    1184             :         }
    1185             : 
    1186           0 :         ri_rp_link = config_->CreateRoutingPolicyLink(rti, rtp, proxy);
    1187           0 :     } else {
    1188           0 :         const IFMapNode *node = ri_rp_link->node();
    1189           0 :         assert(node != NULL);
    1190           0 :         if (delta.obj.get() == NULL) {
    1191           0 :             BgpIfmapRoutingPolicyConfig *rtp = ri_rp_link->policy();
    1192           0 :             BgpIfmapInstanceConfig *rti = ri_rp_link->instance();
    1193           0 :             config_->DeleteRoutingPolicyLink(ri_rp_link);
    1194           0 :             if (rtp->DeleteIfEmpty(this)) {
    1195           0 :                 config_->DeleteRoutingPolicy(rtp);
    1196             :             }
    1197           0 :             if (rti->DeleteIfEmpty(this)) {
    1198           0 :                 BGP_CONFIG_LOG_INSTANCE(Delete, server(), rti,
    1199             :                     SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL);
    1200           0 :                 config_->DeleteInstance(rti);
    1201             :             }
    1202           0 :             return;
    1203             :         }
    1204             :     }
    1205             : 
    1206             :     autogen::RoutingPolicyRoutingInstance *ri_rp_link_cfg =
    1207           0 :         static_cast<autogen::RoutingPolicyRoutingInstance *>(delta.obj.get());
    1208             : 
    1209           0 :     ri_rp_link->Update(this, ri_rp_link_cfg);
    1210             : }
    1211             : 
    1212           0 : static bool CompareRoutingPolicyOrder(const RoutingPolicyAttachInfo &lhs,
    1213             :                                       const RoutingPolicyAttachInfo &rhs) {
    1214           0 :     return (lhs.sequence_ < rhs.sequence_);
    1215             : }
    1216             : 
    1217          99 : void BgpIfmapInstanceConfig::ProcessIdentifierUpdate(uint32_t new_id,
    1218             :                                                      uint32_t old_id) {
    1219          99 :     BgpInstanceConfig::RouteTargetList import_list = data_.import_list();
    1220          99 :     if (old_id > 0) {
    1221           0 :         string old_vit = "target:" + GetVitFromId(ntohl(old_id));
    1222           0 :         import_list.erase(old_vit);
    1223           0 :     }
    1224          99 :     if (new_id > 0) {
    1225          99 :         string new_vit = "target:" + GetVitFromId(ntohl(new_id));
    1226          99 :         import_list.insert(new_vit);
    1227          99 :     }
    1228          99 :     data_.set_import_list(import_list);
    1229          99 : }
    1230             : 
    1231          99 : void BgpIfmapInstanceConfig::ProcessASUpdate(uint32_t new_as, uint32_t old_as) {
    1232          99 :     BgpInstanceConfig::RouteTargetList import_list = data_.import_list();
    1233          99 :     if (old_as > 0) {
    1234          99 :         string old_es_rtarget = "target:" + GetESRouteTarget(old_as);
    1235          99 :         import_list.erase(old_es_rtarget);
    1236          99 :     }
    1237          99 :     if (new_as > 0) {
    1238          99 :         string new_es_rtarget = "target:" + GetESRouteTarget(new_as);
    1239          99 :         import_list.insert(new_es_rtarget);
    1240          99 :     }
    1241          99 :     data_.set_import_list(import_list);
    1242          99 : }
    1243             : 
    1244         181 : string BgpIfmapInstanceConfig::GetVitFromId(uint32_t identifier) const {
    1245         181 :     if (identifier == 0)
    1246           0 :         return "";
    1247         362 :     return Ip4Address(identifier).to_string() + ":" + integerToString(index());
    1248             : }
    1249             : 
    1250         379 : string BgpIfmapInstanceConfig::GetESRouteTarget(uint32_t as) const {
    1251         379 :     if (as == 0)
    1252           0 :         return "";
    1253         379 :     if (as > 0xffFF)
    1254           0 :         return integerToString(as) + ":" +
    1255           0 :             integerToString(EVPN_ES_IMPORT_ROUTE_TARGET_AS4);
    1256         758 :     return integerToString(as) + ":" +
    1257        1137 :         integerToString(EVPN_ES_IMPORT_ROUTE_TARGET_AS2);
    1258             : }
    1259             : 
    1260             : // Populate vrf import route-target (used for MVPN) and ES route target in
    1261             : // import list.
    1262         181 : void BgpIfmapInstanceConfig::InsertVitAndESRTargetInImportList(
    1263             :                          BgpIfmapConfigManager *mgr,
    1264             :                          BgpInstanceConfig::RouteTargetList& import_list) {
    1265             :     const BgpIfmapInstanceConfig *master_instance =
    1266         181 :         mgr->config()->FindInstance(BgpConfigManager::kMasterInstance);
    1267         181 :     uint32_t bgp_identifier = 0;
    1268         181 :     uint32_t as = 0;
    1269         181 :     if (master_instance) {
    1270             :         const BgpIfmapProtocolConfig *master_protocol =
    1271         181 :             master_instance->protocol_config();
    1272         181 :         if (master_protocol) {
    1273         181 :             if (master_protocol->protocol_config()) {
    1274             :                 bgp_identifier =
    1275         181 :                     master_protocol->protocol_config()->identifier();
    1276         181 :                 as = master_protocol->protocol_config()->autonomous_system();
    1277             :             }
    1278             :         }
    1279             :     }
    1280         181 :     if (bgp_identifier > 0)
    1281          82 :         import_list.insert("target:" + GetVitFromId(ntohl(bgp_identifier)));
    1282         181 :     if (as > 0)
    1283         181 :         import_list.insert("target:" + GetESRouteTarget(as));
    1284         181 : }
    1285             : 
    1286             : //
    1287             : // Update BgpIfmapInstanceConfig based on a new autogen::RoutingInstance object.
    1288             : //
    1289             : // Rebuild the import and export route target lists and update the virtual
    1290             : // network information.
    1291             : //
    1292             : // Targets that are configured on this routing-instance (which corresponds
    1293             : // to all adjacencies to instance-target) are added to the import list or
    1294             : // export list or both depending on the import_export attribute.
    1295             : //
    1296             : // Export targets for all other routing-instances that we are connected to
    1297             : // are added to our import list.
    1298             : //
    1299         181 : void BgpIfmapInstanceConfig::Update(BgpIfmapConfigManager *manager,
    1300             :     const autogen::RoutingInstance *config) {
    1301         181 :     BgpInstanceConfig::RouteTargetList import_list, export_list;
    1302         181 :     BgpInstanceConfig::AggregateRouteList inet6_aggregate_list;
    1303         181 :     BgpInstanceConfig::AggregateRouteList inet_aggregate_list;
    1304         181 :     RoutingPolicyConfigList policy_list;
    1305         181 :     data_.Clear();
    1306             : 
    1307         181 :     DBGraph *graph = manager->graph();
    1308         181 :     IFMapNode *node = node_proxy_.node();
    1309         181 :     for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
    1310         919 :          iter != node->end(graph); ++iter) {
    1311         738 :         IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
    1312         738 :         if (strcmp(adj->table()->Typename(), "instance-target") == 0) {
    1313         638 :             string target;
    1314         638 :             if (GetInstanceTargetRouteTarget(graph, adj, &target)) {
    1315             :                 const autogen::InstanceTarget *itarget =
    1316         638 :                     dynamic_cast<autogen::InstanceTarget *>(adj->GetObject());
    1317         638 :                 if (!itarget)
    1318           0 :                     continue;
    1319         638 :                 const autogen::InstanceTargetType &itt = itarget->data();
    1320         638 :                 if (itt.import_export == "import") {
    1321           0 :                     import_list.insert(target);
    1322         638 :                 } else if (itt.import_export == "export") {
    1323           0 :                     export_list.insert(target);
    1324             :                 } else {
    1325         638 :                     import_list.insert(target);
    1326         638 :                     export_list.insert(target);
    1327             :                 }
    1328             :             }
    1329         738 :         } else if (strcmp(adj->table()->Typename(),
    1330         100 :                           "routing-policy-routing-instance") == 0) {
    1331           0 :             RoutingPolicyAttachInfo policy_info;
    1332           0 :             if (GetRoutingInstanceRoutingPolicy(graph, adj, &policy_info)) {
    1333           0 :                 policy_list.push_back(policy_info);
    1334             :             }
    1335         100 :         } else if (strcmp(adj->table()->Typename(), "route-aggregate") == 0) {
    1336           0 :             GetRouteAggregateConfig(graph, adj, &inet_aggregate_list,
    1337             :                                     &inet6_aggregate_list);
    1338         100 :         } else if (strcmp(adj->table()->Typename(), "connection") == 0) {
    1339           0 :             vector<string> target_list;
    1340           0 :             GetConnectionExportTargets(graph, node, name_, adj, &target_list);
    1341           0 :             BOOST_FOREACH(string target, target_list) {
    1342           0 :                 import_list.insert(target);
    1343           0 :             }
    1344         100 :         } else if (strcmp(adj->table()->Typename(), "virtual-network") == 0) {
    1345           0 :             data_.set_virtual_network(adj->name());
    1346           0 :             data_.set_virtual_network_index(GetVirtualNetworkIndex(graph, adj));
    1347           0 :             data_.set_virtual_network_allow_transit(
    1348           0 :                 GetVirtualNetworkAllowTransit(graph, adj));
    1349           0 :             data_.set_vxlan_id(GetVirtualNetworkVxlanId(graph, adj));
    1350           0 :             data_.set_virtual_network_pbb_evpn_enable(
    1351           0 :                 GetVirtualNetworkPbbEvpnEnable(graph, adj));
    1352           0 :             data_.set_routing_instance_vxlan(GetConnectedVxlanRi(graph, adj));
    1353             :         }
    1354             :     }
    1355             : 
    1356         181 :     InsertVitAndESRTargetInImportList(manager, import_list);
    1357         181 :     data_.set_import_list(import_list);
    1358         181 :     data_.set_export_list(export_list);
    1359             : 
    1360         181 :     sort(policy_list.begin(), policy_list.end(), CompareRoutingPolicyOrder);
    1361         181 :     data_.swap_routing_policy_list(&policy_list);
    1362         181 :     data_.swap_aggregate_routes(Address::INET, &inet_aggregate_list);
    1363         181 :     data_.swap_aggregate_routes(Address::INET6, &inet6_aggregate_list);
    1364             : 
    1365         181 :     if (config) {
    1366           0 :         data_.set_has_pnf(config->has_pnf());
    1367           0 :         SetStaticRouteConfig(&data_, config);
    1368           0 :         SetServiceChainConfig(&data_, config);
    1369             :     }
    1370         181 : }
    1371             : 
    1372             : //
    1373             : // Reset IFMap related state in the BgpIfmapInstanceConfig.
    1374             : //
    1375           0 : void BgpIfmapInstanceConfig::ResetConfig() {
    1376           0 :     node_proxy_.Clear();
    1377           0 : }
    1378             : 
    1379             : //
    1380             : // Return true if the BgpIfmapInstanceConfig is ready to be deleted.  The
    1381             : // caller is responsible for actually deleting it.
    1382             : //
    1383           0 : bool BgpIfmapInstanceConfig::DeleteIfEmpty(BgpConfigManager *manager) {
    1384           0 :     if (name_ == BgpConfigManager::kMasterInstance) {
    1385           0 :         return false;
    1386             :     }
    1387           0 :     if (node() != NULL || protocol_.get() != NULL) {
    1388           0 :         return false;
    1389             :     }
    1390           0 :     if (!neighbors_.empty() || !peerings_.empty() ||
    1391           0 :         !routing_policies_.empty()) {
    1392           0 :         return false;
    1393             :     }
    1394             : 
    1395           0 :     manager->Notify(&data_, BgpConfigManager::CFG_DELETE);
    1396           0 :     return true;
    1397             : }
    1398             : 
    1399             : //
    1400             : // Add a BgpNeighborConfig to this BgpIfmapInstanceConfig.
    1401             : //
    1402             : // The BgpNeighborConfig is added to the NeighborMap and the BgpConfigManager
    1403             : // is notified.
    1404             : //
    1405          32 : void BgpIfmapInstanceConfig::AddNeighbor(BgpConfigManager *manager,
    1406             :     BgpNeighborConfig *neighbor) {
    1407          32 :     BGP_CONFIG_LOG_NEIGHBOR(
    1408             :         Create, manager->server(), neighbor, SandeshLevel::SYS_DEBUG,
    1409             :         BGP_LOG_FLAG_ALL,
    1410             :         neighbor->admin_down(),
    1411             :         neighbor->passive(),
    1412             :         BgpIdentifierToString(neighbor->local_identifier()),
    1413             :         neighbor->local_as(),
    1414             :         neighbor->peer_address().to_string(), neighbor->peer_as(),
    1415             :         neighbor->GetAddressFamilies(), neighbor->AuthKeyTypeToString(),
    1416             :         neighbor->AuthKeysToString());
    1417          32 :     neighbors_.insert(make_pair(neighbor->name(), neighbor));
    1418          32 :     data_.add_neighbor(neighbor->name());
    1419          32 :     manager->Notify(neighbor, BgpConfigManager::CFG_ADD);
    1420          32 : }
    1421             : 
    1422             : //
    1423             : // Change a BgpNeighborConfig that's already in this BgpIfmapInstanceConfig.
    1424             : //
    1425           0 : void BgpIfmapInstanceConfig::ChangeNeighbor(BgpConfigManager *manager,
    1426             :     BgpNeighborConfig *neighbor) {
    1427           0 :     NeighborMap::iterator loc = neighbors_.find(neighbor->name());
    1428           0 :     assert(loc != neighbors_.end());
    1429           0 :     loc->second = neighbor;
    1430             : 
    1431           0 :     BGP_CONFIG_LOG_NEIGHBOR(
    1432             :         Update, manager->server(), neighbor,
    1433             :         SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
    1434             :         neighbor->admin_down(),
    1435             :         neighbor->passive(),
    1436             :         BgpIdentifierToString(neighbor->local_identifier()),
    1437             :         neighbor->local_as(),
    1438             :         neighbor->peer_address().to_string(), neighbor->peer_as(),
    1439             :         neighbor->GetAddressFamilies(), neighbor->AuthKeyTypeToString(),
    1440             :         neighbor->AuthKeysToString());
    1441           0 :     manager->Notify(neighbor, BgpConfigManager::CFG_CHANGE);
    1442           0 : }
    1443             : 
    1444             : //
    1445             : // Delete a BgpNeighborConfig from this BgpIfmapInstanceConfig.
    1446             : //
    1447             : // The BgpConfigManager is notified and BgpNeighborConfig is removed from the
    1448             : // NeighborMap. Note that the caller is responsible for actually deleting the
    1449             : // BgpNeighborConfig object.
    1450             : //
    1451           0 : void BgpIfmapInstanceConfig::DeleteNeighbor(BgpConfigManager *manager,
    1452             :     BgpNeighborConfig *neighbor) {
    1453           0 :     BGP_CONFIG_LOG_NEIGHBOR(Delete, manager->server(), neighbor,
    1454             :         SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL);
    1455           0 :     manager->Notify(neighbor, BgpConfigManager::CFG_DELETE);
    1456           0 :     neighbors_.erase(neighbor->name());
    1457           0 :     data_.delete_neighbor(neighbor->name());
    1458           0 : }
    1459             : 
    1460             : //
    1461             : // Find the BgpNeighborConfig by name in this BgpIfmapInstanceConfig.
    1462             : //
    1463           0 : const BgpNeighborConfig *BgpIfmapInstanceConfig::FindNeighbor(
    1464             :     const string &name) const {
    1465           0 :     NeighborMap::const_iterator loc;
    1466             : 
    1467           0 :     if (name.find(name_) == string::npos) {
    1468           0 :         string fqn(name_);
    1469           0 :         fqn += ":";
    1470           0 :         fqn += name;
    1471           0 :         loc = neighbors_.find(fqn);
    1472           0 :     } else {
    1473           0 :         loc = neighbors_.find(name);
    1474             :     }
    1475             : 
    1476           0 :     return loc != neighbors_.end() ? loc->second : NULL;
    1477             : }
    1478             : 
    1479             : //
    1480             : // Add a BgpPeeringConfig to this BgpIfmapInstanceConfig.
    1481             : //
    1482          32 : void BgpIfmapInstanceConfig::AddPeering(BgpIfmapPeeringConfig *peering) {
    1483          32 :     peerings_.insert(make_pair(peering->name(), peering));
    1484          32 : }
    1485             : 
    1486             : //
    1487             : // Delete a BgpPeeringConfig from this BgpIfmapInstanceConfig.
    1488             : //
    1489           0 : void BgpIfmapInstanceConfig::DeletePeering(BgpIfmapPeeringConfig *peering) {
    1490           0 :     peerings_.erase(peering->name());
    1491           0 : }
    1492             : 
    1493             : //
    1494             : // Add a BgpIfmapRoutingPolicyConfig to this BgpIfmapInstanceConfig.
    1495             : //
    1496           0 : void BgpIfmapInstanceConfig::AddRoutingPolicy(
    1497             :     BgpIfmapRoutingPolicyConfig *rtp) {
    1498           0 :     routing_policies_.insert(make_pair(rtp->name(), rtp));
    1499           0 : }
    1500             : 
    1501             : //
    1502             : // Delete a BgpIfmapRoutingPolicyConfig from this BgpIfmapInstanceConfig.
    1503             : //
    1504           0 : void BgpIfmapInstanceConfig::DeleteRoutingPolicy(
    1505             :     BgpIfmapRoutingPolicyConfig *rtp) {
    1506           0 :     routing_policies_.erase(rtp->name());
    1507           0 : }
    1508             : 
    1509          20 : void BgpIfmapConfigData::ProcessIdentifierAndASUpdate(
    1510             :                             BgpIfmapConfigManager* manager,
    1511             :                             uint32_t new_id, uint32_t old_id,
    1512             :                             uint32_t new_as, uint32_t old_as) {
    1513          20 :     assert(new_id != old_id || new_as != old_as);
    1514         119 :     for (unsigned int i = 0; i < instances_.size(); i++) {
    1515          99 :         BgpIfmapInstanceConfig * ifmap_config = instances_.At(i);
    1516          99 :         if (!ifmap_config)
    1517           0 :             continue;
    1518          99 :         if (new_id != old_id)
    1519          99 :             ifmap_config->ProcessIdentifierUpdate(new_id, old_id);
    1520          99 :         if (new_as != old_as)
    1521          99 :             ifmap_config->ProcessASUpdate(new_as, old_as);
    1522          99 :         manager->UpdateInstanceConfig(ifmap_config,
    1523             :                                       BgpConfigManager::CFG_CHANGE);
    1524             :     }
    1525          20 : }
    1526             : 
    1527             : //
    1528             : // Constructor for BgpIfmapConfigData.
    1529             : //
    1530          20 : BgpIfmapConfigData::BgpIfmapConfigData() {
    1531             :     // Reserve bit 0 for master instance
    1532          20 :     instances_.ReserveBit(0);
    1533          20 : }
    1534             : 
    1535             : //
    1536             : // Destructor for BgpConfigData.
    1537             : //
    1538          20 : BgpIfmapConfigData::~BgpIfmapConfigData() {
    1539          20 :     instances_.clear();
    1540          20 :     STLDeleteElements(&peerings_);
    1541          20 :     STLDeleteElements(&routing_policies_);
    1542          20 :     STLDeleteElements(&ri_rp_links_);
    1543          20 : }
    1544             : 
    1545             : //
    1546             : // Locate the BgpIfmapInstanceConfig by name, create it if not found.  The newly
    1547             : // created BgpIfmapInstanceConfig gets added to the IfmapInstanceMap.
    1548             : //
    1549             : // Note that we do not have the IFMapNode representing the routing-instance
    1550             : // at this point.
    1551             : //
    1552         100 : BgpIfmapInstanceConfig *BgpIfmapConfigData::LocateInstance(const string &name) {
    1553         100 :     BgpIfmapInstanceConfig *rti = FindInstance(name);
    1554         100 :     if (rti != NULL) {
    1555           0 :         return rti;
    1556             :     }
    1557         100 :     rti = new BgpIfmapInstanceConfig(name);
    1558         100 :     int index = -1;
    1559         100 :     if (name == BgpConfigManager::kMasterInstance) {
    1560          20 :         index = 0;
    1561             :     }
    1562         100 :     index = instances_.Insert(name, rti, index);
    1563         100 :     rti->set_index(index);
    1564             :     pair<BgpInstanceMap::iterator, bool> result2 =
    1565         100 :             instance_config_map_.insert(
    1566         200 :                 make_pair(name, rti->instance_config()));
    1567         100 :     assert(result2.second);
    1568         100 :     return rti;
    1569             : }
    1570             : 
    1571             : //
    1572             : // Remove the given BgpIfmapInstanceConfig from the IfmapInstanceMap
    1573             : // and delete it.
    1574             : //
    1575           0 : void BgpIfmapConfigData::DeleteInstance(BgpIfmapInstanceConfig *rti) {
    1576           0 :     BgpInstanceMap::iterator loc2 = instance_config_map_.find(rti->name());
    1577           0 :     assert(loc2 != instance_config_map_.end());
    1578           0 :     instance_config_map_.erase(loc2);
    1579           0 :     instances_.Remove(rti->name(), rti->index(), false);
    1580           0 : }
    1581             : 
    1582             : //
    1583             : // Find the BgpIfmapInstanceConfig by name.
    1584             : //
    1585        1676 : BgpIfmapInstanceConfig *BgpIfmapConfigData::FindInstance(const string &name) {
    1586        1676 :     return instances_.Find(name);
    1587             : }
    1588             : 
    1589             : //
    1590             : // Find the BgpIfmapInstanceConfig by name.
    1591             : // Const version.
    1592             : //
    1593           0 : const BgpIfmapInstanceConfig *BgpIfmapConfigData::FindInstance(
    1594             :     const string &name) const {
    1595           0 :     return instances_.Find(name);
    1596             : }
    1597             : 
    1598             : BgpConfigManager::NeighborMapRange
    1599           0 : BgpIfmapInstanceConfig::NeighborMapItems() const {
    1600           0 :     return make_pair(neighbors_.begin(), neighbors_.end());
    1601             : }
    1602             : 
    1603             : //
    1604             : // Create a new BgpIfmapRoutingPolicyLinkConfig.
    1605             : //
    1606             : // The IFMapNodeProxy is a proxy for the IFMapNode which is the midnode that
    1607             : // represents the routing-policy-routing-instance. The newly created
    1608             : // BgpIfmapRoutingPolicyLinkConfig gets added to the IfmapRoutingPolicyLinkMap.
    1609             : //
    1610           0 : BgpIfmapRoutingPolicyLinkConfig *BgpIfmapConfigData::CreateRoutingPolicyLink(
    1611             :     BgpIfmapInstanceConfig *rti, BgpIfmapRoutingPolicyConfig *rtp,
    1612             :     IFMapNodeProxy *proxy) {
    1613             :     BgpIfmapRoutingPolicyLinkConfig *ri_rp_link =
    1614           0 :         new BgpIfmapRoutingPolicyLinkConfig(rti, rtp);
    1615           0 :     ri_rp_link->SetNodeProxy(proxy);
    1616             :     pair<IfmapRoutingPolicyLinkMap::iterator, bool> result =
    1617           0 :         ri_rp_links_.insert(make_pair(ri_rp_link->node()->name(), ri_rp_link));
    1618           0 :     assert(result.second);
    1619           0 :     ri_rp_link->instance()->AddRoutingPolicy(rtp);
    1620           0 :     ri_rp_link->policy()->AddInstance(rti);
    1621           0 :     return ri_rp_link;
    1622             : }
    1623             : 
    1624             : //
    1625             : // Delete a BgpIfmapRoutingPolicyLinkConfig.
    1626             : //
    1627             : // The BgpIfmapRoutingPolicyLinkConfig is erased from IfmapRoutingPolicyLinkMap
    1628             : // and then deleted.
    1629             : // Note that the reference to the IFMapNode for routing-policy-routing-instance
    1630             : // gets released via the destructor when the IFMapNodeProxy is destroyed.
    1631             : //
    1632           0 : void BgpIfmapConfigData::DeleteRoutingPolicyLink(
    1633             :     BgpIfmapRoutingPolicyLinkConfig *ri_rp_link) {
    1634           0 :     ri_rp_links_.erase(ri_rp_link->node()->name());
    1635           0 :     ri_rp_link->instance()->DeleteRoutingPolicy(ri_rp_link->policy());
    1636           0 :     ri_rp_link->policy()->RemoveInstance(ri_rp_link->instance());
    1637           0 :     delete ri_rp_link;
    1638           0 : }
    1639             : 
    1640             : //
    1641             : // Find the BgpIfmapRoutingPolicyLinkConfig by name.
    1642             : //
    1643           0 : BgpIfmapRoutingPolicyLinkConfig *BgpIfmapConfigData::FindRoutingPolicyLink(
    1644             :     const string &name) {
    1645           0 :     IfmapRoutingPolicyLinkMap::iterator loc = ri_rp_links_.find(name);
    1646           0 :     if (loc != ri_rp_links_.end()) {
    1647           0 :         return loc->second;
    1648             :     }
    1649           0 :     return NULL;
    1650             : }
    1651             : 
    1652             : //
    1653             : // Find the BgpIfmapRoutingPolicyLinkConfig by name.
    1654             : // Const version.
    1655             : //
    1656             : const BgpIfmapRoutingPolicyLinkConfig *
    1657           0 : BgpIfmapConfigData::FindRoutingPolicyLink(const string &name) const {
    1658           0 :     IfmapRoutingPolicyLinkMap::const_iterator loc = ri_rp_links_.find(name);
    1659           0 :     if (loc != ri_rp_links_.end()) {
    1660           0 :         return loc->second;
    1661             :     }
    1662           0 :     return NULL;
    1663             : }
    1664             : 
    1665             : //
    1666             : // Create a new BgpIfmapPeeringConfig.
    1667             : //
    1668             : // The IFMapNodeProxy is a proxy for the IFMapNode which is the
    1669             : // midnode that represents the bgp-peering. The newly created
    1670             : // BgpIfmapPeeringConfig gets added to the IfmapPeeringMap.
    1671             : //
    1672          32 : BgpIfmapPeeringConfig *BgpIfmapConfigData::CreatePeering(
    1673             :     BgpIfmapInstanceConfig *rti, IFMapNodeProxy *proxy) {
    1674          32 :     BgpIfmapPeeringConfig *peering = new BgpIfmapPeeringConfig(rti);
    1675          32 :     peering->SetNodeProxy(proxy);
    1676             :     pair<IfmapPeeringMap::iterator, bool> result =
    1677          32 :             peerings_.insert(make_pair(peering->node()->name(), peering));
    1678          32 :     assert(result.second);
    1679          32 :     peering->instance()->AddPeering(peering);
    1680          32 :     BGP_LOG_STR(BgpConfig, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_SYSLOG,
    1681             :          "Creating BgpIfmapPeering " << peering->node()->name());
    1682          32 :     return peering;
    1683             : }
    1684             : 
    1685             : //
    1686             : // Delete a BgpPeeringConfig.
    1687             : //
    1688             : // The BgpPeeringConfig is removed from the IfmapPeeringMap and then deleted.
    1689             : // Note that the reference to the IFMapNode for the bgp-peering gets released
    1690             : // via the destructor when the IFMapNodeProxy is destroyed.
    1691             : //
    1692           0 : void BgpIfmapConfigData::DeletePeering(BgpIfmapPeeringConfig *peering) {
    1693           0 :     BGP_LOG_STR(BgpConfig, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_SYSLOG,
    1694             :          "Deleting BgpIfmapPeering " << peering->node()->name());
    1695           0 :     peering->instance()->DeletePeering(peering);
    1696           0 :     peerings_.erase(peering->node()->name());
    1697           0 :     delete peering;
    1698           0 : }
    1699             : 
    1700             : //
    1701             : // Find the BgpPeeringConfig by name.
    1702             : //
    1703         160 : BgpIfmapPeeringConfig *BgpIfmapConfigData::FindPeering(const string &name) {
    1704         160 :     IfmapPeeringMap::iterator loc = peerings_.find(name);
    1705         160 :     if (loc != peerings_.end()) {
    1706          32 :         return loc->second;
    1707             :     }
    1708         128 :     return NULL;
    1709             : }
    1710             : 
    1711             : //
    1712             : // Find the BgpPeeringConfig by name.
    1713             : // Const version.
    1714             : //
    1715           0 : const BgpIfmapPeeringConfig *BgpIfmapConfigData::FindPeering(
    1716             :     const string &name) const {
    1717           0 :     IfmapPeeringMap::const_iterator loc = peerings_.find(name);
    1718           0 :     if (loc != peerings_.end()) {
    1719           0 :         return loc->second;
    1720             :     }
    1721           0 :     return NULL;
    1722             : }
    1723             : 
    1724             : BgpConfigManager::InstanceMapRange
    1725           0 : BgpIfmapConfigData::InstanceMapItems(const string &start_name) const {
    1726           0 :     return make_pair(instance_config_map_.lower_bound(start_name),
    1727           0 :         instance_config_map_.end());
    1728             : }
    1729             : 
    1730             : BgpConfigManager::RoutingPolicyMapRange
    1731           0 : BgpIfmapConfigData::RoutingPolicyMapItems(const string &start_name) const {
    1732           0 :     return make_pair(routing_policy_config_map_.lower_bound(start_name),
    1733           0 :         routing_policy_config_map_.end());
    1734             : }
    1735             : 
    1736             : //
    1737             : // Locate the BgpIfmapRoutingPolicyConfig by name, create it if not found.
    1738             : // The newly created BgpIfmapRoutingPolicyConfig gets added to the
    1739             : // IfmapRoutingPolicyMap.
    1740             : //
    1741           0 : BgpIfmapRoutingPolicyConfig *BgpIfmapConfigData::LocateRoutingPolicy(
    1742             :     const string &name) {
    1743           0 :     BgpIfmapRoutingPolicyConfig *rtp = FindRoutingPolicy(name);
    1744           0 :     if (rtp != NULL) {
    1745           0 :         return rtp;
    1746             :     }
    1747           0 :     rtp = new BgpIfmapRoutingPolicyConfig(name);
    1748             :     pair<IfmapRoutingPolicyMap::iterator, bool> result =
    1749           0 :             routing_policies_.insert(make_pair(name, rtp));
    1750           0 :     assert(result.second);
    1751             :     pair<BgpRoutingPolicyMap::iterator, bool> result2 =
    1752           0 :             routing_policy_config_map_.insert(
    1753           0 :                 make_pair(name, rtp->routing_policy_config()));
    1754           0 :     assert(result2.second);
    1755           0 :     return rtp;
    1756             : }
    1757             : 
    1758             : //
    1759             : // Remove the given BgpIfmapRoutingPolicyConfig from the IfmapRoutingPolicyMap
    1760             : // and delete it.
    1761             : //
    1762           0 : void BgpIfmapConfigData::DeleteRoutingPolicy(BgpIfmapRoutingPolicyConfig *rtp) {
    1763           0 :     IfmapRoutingPolicyMap::iterator loc = routing_policies_.find(rtp->name());
    1764           0 :     assert(loc != routing_policies_.end());
    1765           0 :     routing_policies_.erase(loc);
    1766             :     BgpRoutingPolicyMap::iterator loc2 =
    1767           0 :         routing_policy_config_map_.find(rtp->name());
    1768           0 :     assert(loc2 != routing_policy_config_map_.end());
    1769           0 :     routing_policy_config_map_.erase(loc2);
    1770           0 :     delete rtp;
    1771           0 : }
    1772             : 
    1773           0 : BgpIfmapRoutingPolicyConfig *BgpIfmapConfigData::FindRoutingPolicy(
    1774             :     const string &name) {
    1775           0 :     IfmapRoutingPolicyMap::iterator loc = routing_policies_.find(name);
    1776           0 :     if (loc != routing_policies_.end()) {
    1777           0 :         return loc->second;
    1778             :     }
    1779           0 :     return NULL;
    1780             : }
    1781             : 
    1782           0 : const BgpIfmapRoutingPolicyConfig *BgpIfmapConfigData::FindRoutingPolicy(
    1783             :     const string &name) const {
    1784           0 :     IfmapRoutingPolicyMap::const_iterator loc = routing_policies_.find(name);
    1785           0 :     if (loc != routing_policies_.end()) {
    1786           0 :         return loc->second;
    1787             :     }
    1788           0 :     return NULL;
    1789             : }
    1790             : 
    1791             : //
    1792             : // Constructor for BgpIfmapConfigManager.
    1793             : //
    1794          20 : BgpIfmapConfigManager::BgpIfmapConfigManager(BgpServer *server)
    1795             :     : BgpConfigManager(server),
    1796          20 :       db_(NULL),
    1797          20 :       db_graph_(NULL),
    1798          20 :       trigger_(boost::bind(&BgpIfmapConfigManager::ConfigHandler, this),
    1799             :           TaskScheduler::GetInstance()->GetTaskId("bgp::Config"),
    1800             :           kConfigTaskInstanceId),
    1801          20 :       listener_(new BgpConfigListener(this)),
    1802          40 :       config_(new BgpIfmapConfigData()) {
    1803          20 :     IdentifierMapInit();
    1804          20 : }
    1805             : 
    1806             : //
    1807             : // Destructor for BgpIfmapConfigManager.
    1808             : //
    1809          40 : BgpIfmapConfigManager::~BgpIfmapConfigManager() {
    1810          40 : }
    1811             : 
    1812             : //
    1813             : // Initialize the BgpConfigManager.
    1814             : //
    1815          20 : void BgpIfmapConfigManager::Initialize(DB *db, DBGraph *db_graph,
    1816             :                                        const string &localname) {
    1817          20 :     db_ = db;
    1818          20 :     db_graph_ = db_graph;
    1819          20 :     localname_ = localname;
    1820          20 :     listener_->Initialize();
    1821          20 :     DefaultConfig();
    1822          20 : }
    1823             : 
    1824             : //
    1825             : // Used to trigger a build and subsequent evaluation of the ChangeList.
    1826             : //
    1827         890 : void BgpIfmapConfigManager::OnChange() {
    1828         890 :     CHECK_CONCURRENCY("db::IFMapTable");
    1829         890 :     trigger_.Set();
    1830         890 : }
    1831             : 
    1832             : BgpConfigManager::InstanceMapRange
    1833           0 : BgpIfmapConfigManager::InstanceMapItems(const string &start_name) const {
    1834           0 :     return config_->InstanceMapItems(start_name);
    1835             : }
    1836             : 
    1837             : BgpConfigManager::RoutingPolicyMapRange
    1838           0 : BgpIfmapConfigManager::RoutingPolicyMapItems(const string &start_name) const {
    1839           0 :     return config_->RoutingPolicyMapItems(start_name);
    1840             : }
    1841             : 
    1842             : BgpConfigManager::NeighborMapRange
    1843           0 : BgpIfmapConfigManager::NeighborMapItems(
    1844             :     const string &instance_name) const {
    1845           0 :     static BgpConfigManager::NeighborMap nilMap;
    1846           0 :     BgpIfmapInstanceConfig *rti = config_->FindInstance(instance_name);
    1847           0 :     if (rti == NULL) {
    1848           0 :         return make_pair(nilMap.begin(), nilMap.end());
    1849             :     }
    1850           0 :     return rti->NeighborMapItems();
    1851             : }
    1852             : 
    1853         100 : void BgpIfmapConfigManager::ResetRoutingInstanceIndexBit(int index) {
    1854         100 :     config()->instances().ResetBit(index);
    1855         100 : }
    1856             : 
    1857           0 : int BgpIfmapConfigManager::NeighborCount(
    1858             :     const string &instance_name) const {
    1859           0 :     BgpIfmapInstanceConfig *rti = config_->FindInstance(instance_name);
    1860           0 :     if (rti == NULL) {
    1861           0 :         return 0;
    1862             :     }
    1863           0 :     return rti->neighbors().size();
    1864             : }
    1865             : 
    1866             : //
    1867             : // Constructor for BgpIfmapRoutingPolicyConfig.
    1868             : //
    1869           0 : BgpIfmapRoutingPolicyConfig::BgpIfmapRoutingPolicyConfig(
    1870           0 :     const string &name)
    1871           0 :     : name_(name),
    1872           0 :       data_(name) {
    1873           0 : }
    1874             : 
    1875             : //
    1876             : // Destructor for BgpIfmapRoutingPolicyConfig.
    1877             : //
    1878           0 : BgpIfmapRoutingPolicyConfig::~BgpIfmapRoutingPolicyConfig() {
    1879           0 : }
    1880             : 
    1881             : //
    1882             : // Set the IFMapNodeProxy for the BgpIfmapRoutingPolicyConfig.
    1883             : //
    1884           0 : void BgpIfmapRoutingPolicyConfig::SetNodeProxy(IFMapNodeProxy *proxy) {
    1885           0 :     if (proxy != NULL) {
    1886           0 :         node_proxy_.Swap(proxy);
    1887             :     }
    1888           0 : }
    1889             : 
    1890           0 : void BgpIfmapRoutingPolicyConfig::Delete(BgpConfigManager *manager) {
    1891           0 :     manager->Notify(&data_, BgpConfigManager::CFG_DELETE);
    1892           0 :     routing_policy_.reset();
    1893           0 : }
    1894             : 
    1895             : //
    1896             : // Return true if the BgpIfmapRoutingPolicyConfig is ready to be deleted.
    1897             : // The caller is responsible for actually deleting it.
    1898             : //
    1899           0 : bool BgpIfmapRoutingPolicyConfig::DeleteIfEmpty(BgpConfigManager *manager) {
    1900           0 :     if (node() != NULL) {
    1901           0 :         return false;
    1902             :     }
    1903           0 :     if (!instances_.empty()) {
    1904           0 :         return false;
    1905             :     }
    1906             : 
    1907           0 :     Delete(manager);
    1908           0 :     return true;
    1909             : }
    1910             : 
    1911             : //
    1912             : // Add a BgpIfmapInstanceConfig to BgpIfmapRoutingPolicyConfig.
    1913             : //
    1914           0 : void BgpIfmapRoutingPolicyConfig::AddInstance(BgpIfmapInstanceConfig *rti) {
    1915           0 :     instances_.insert(make_pair(rti->name(), rti));
    1916           0 : }
    1917             : 
    1918             : //
    1919             : // Remove a BgpIfmapInstanceConfig to BgpIfmapRoutingPolicyConfig.
    1920             : //
    1921           0 : void BgpIfmapRoutingPolicyConfig::RemoveInstance(BgpIfmapInstanceConfig *rti) {
    1922           0 :     instances_.erase(rti->name());
    1923           0 : }
    1924             : 
    1925             : 
    1926           0 : static void BuildPolicyTermConfig(autogen::PolicyTermType cfg_term,
    1927             :     RoutingPolicyTermConfig *term) {
    1928           0 :     term->match.protocols_match = cfg_term.term_match_condition.protocol;
    1929           0 :     BOOST_FOREACH(const autogen::PrefixMatchType &prefix_match,
    1930             :                   cfg_term.term_match_condition.prefix) {
    1931           0 :         string prefix_type(prefix_match.prefix_type);
    1932           0 :         PrefixMatchConfig match(prefix_match.prefix,
    1933           0 :             prefix_type.empty() ? "exact" : prefix_type);
    1934           0 :         term->match.prefixes_to_match.push_back(match);
    1935           0 :     }
    1936           0 :     term->match.community_match_all =
    1937           0 :         cfg_term.term_match_condition.community_match_all;
    1938           0 :     if (!cfg_term.term_match_condition.community_list.empty()) {
    1939             :         term->match.community_match =
    1940           0 :             cfg_term.term_match_condition.community_list;
    1941             :     }
    1942           0 :     if (!cfg_term.term_match_condition.community.empty()) {
    1943           0 :         term->match.community_match.push_back(
    1944           0 :             cfg_term.term_match_condition.community);
    1945             :     }
    1946           0 :     term->match.ext_community_match_all =
    1947           0 :         cfg_term.term_match_condition.extcommunity_match_all;
    1948           0 :     if (!cfg_term.term_match_condition.extcommunity_list.empty()) {
    1949             :         term->match.ext_community_match =
    1950           0 :             cfg_term.term_match_condition.extcommunity_list;
    1951             :     }
    1952             : 
    1953           0 :     BOOST_FOREACH(uint32_t asn,
    1954             :         cfg_term.term_action_list.update.as_path.expand.asn_list) {
    1955           0 :         term->action.update.aspath_expand.push_back(asn);
    1956             :     }
    1957           0 :     BOOST_FOREACH(const string community,
    1958             :                   cfg_term.term_action_list.update.community.add.community) {
    1959           0 :         term->action.update.community_add.push_back(community);
    1960           0 :     }
    1961           0 :     BOOST_FOREACH(const string community,
    1962             :                   cfg_term.term_action_list.update.community.remove.community) {
    1963           0 :         term->action.update.community_remove.push_back(community);
    1964           0 :     }
    1965           0 :     BOOST_FOREACH(const string community,
    1966             :                   cfg_term.term_action_list.update.community.set.community) {
    1967           0 :         term->action.update.community_set.push_back(community);
    1968           0 :     }
    1969           0 :     BOOST_FOREACH(const string community,
    1970             :                   cfg_term.term_action_list.update.extcommunity.add.community) {
    1971           0 :         term->action.update.ext_community_add.push_back(community);
    1972           0 :     }
    1973           0 :     BOOST_FOREACH(const string community,
    1974             :                   cfg_term.term_action_list.update.extcommunity.remove.community) {
    1975           0 :         term->action.update.ext_community_remove.push_back(community);
    1976           0 :     }
    1977           0 :     BOOST_FOREACH(const string community,
    1978             :                   cfg_term.term_action_list.update.extcommunity.set.community) {
    1979           0 :         term->action.update.ext_community_set.push_back(community);
    1980           0 :     }
    1981           0 :     term->action.update.local_pref =
    1982           0 :         cfg_term.term_action_list.update.local_pref;
    1983           0 :     term->action.update.med = cfg_term.term_action_list.update.med;
    1984             : 
    1985           0 :     term->action.action = RoutingPolicyActionConfig::NEXT_TERM;
    1986           0 :     if (strcmp(cfg_term.term_action_list.action.c_str(), "reject") == 0) {
    1987           0 :         term->action.action = RoutingPolicyActionConfig::REJECT;
    1988           0 :     } else if (
    1989           0 :         strcmp(cfg_term.term_action_list.action.c_str(), "accept") == 0) {
    1990           0 :         term->action.action = RoutingPolicyActionConfig::ACCEPT;
    1991             :     }
    1992           0 : }
    1993             : 
    1994           0 : static void BuildPolicyTermsConfig(BgpRoutingPolicyConfig *policy_cfg,
    1995             :     const autogen::RoutingPolicy *policy) {
    1996           0 :     vector<autogen::PolicyTermType> terms = policy->entries();
    1997           0 :     BOOST_FOREACH(autogen::PolicyTermType cfg_term, terms) {
    1998           0 :         RoutingPolicyTermConfig policy_term_cfg;
    1999           0 :         BuildPolicyTermConfig(cfg_term, &policy_term_cfg);
    2000           0 :         policy_cfg->add_term(policy_term_cfg);
    2001           0 :     }
    2002           0 : }
    2003             : 
    2004           0 : void BgpIfmapRoutingPolicyConfig::Update(BgpIfmapConfigManager *manager,
    2005             :     const autogen::RoutingPolicy *policy) {
    2006           0 :     routing_policy_.reset(policy);
    2007           0 :     data_.Clear();
    2008           0 :     if (policy) {
    2009           0 :         BuildPolicyTermsConfig(&data_, policy);
    2010             :     }
    2011           0 : }
    2012             : 
    2013             : //
    2014             : // Reset IFMap related state in the BgpIfmapRoutingPolicyConfig.
    2015             : //
    2016           0 : void BgpIfmapRoutingPolicyConfig::ResetConfig() {
    2017           0 :     node_proxy_.Clear();
    2018           0 : }
    2019             : 
    2020           0 : const BgpRoutingPolicyConfig *BgpIfmapConfigManager::FindRoutingPolicy(
    2021             :     const string &name) const {
    2022           0 :     BgpIfmapRoutingPolicyConfig *rtp = config_->FindRoutingPolicy(name);
    2023           0 :     if (rtp == NULL) {
    2024           0 :         return NULL;
    2025             :     }
    2026           0 :     return rtp->routing_policy_config();
    2027             : }
    2028             : 
    2029         141 : const BgpInstanceConfig *BgpIfmapConfigManager::FindInstance(
    2030             :     const string &name) const {
    2031         141 :     BgpIfmapInstanceConfig *rti = config_->FindInstance(name);
    2032         140 :     if (rti == NULL) {
    2033           0 :         return NULL;
    2034             :     }
    2035         140 :     return rti->instance_config();
    2036             : }
    2037             : 
    2038         956 : const BgpProtocolConfig *BgpIfmapConfigManager::GetProtocolConfig(
    2039             :     const string &instance_name) const {
    2040         956 :     BgpIfmapInstanceConfig *rti = config_->FindInstance(instance_name);
    2041         954 :     if (rti == NULL) {
    2042           0 :         return NULL;
    2043             :     }
    2044         954 :     const BgpIfmapProtocolConfig *proto = rti->protocol_config();
    2045         954 :     if (proto == NULL) {
    2046           0 :         return NULL;
    2047             :     }
    2048         954 :     return proto->protocol_config();
    2049             : }
    2050             : 
    2051           0 : const BgpNeighborConfig *BgpIfmapConfigManager::FindNeighbor(
    2052             :     const string &instance_name, const string &name) const {
    2053           0 :     BgpIfmapInstanceConfig *rti = config_->FindInstance(instance_name);
    2054           0 :     if (rti == NULL) {
    2055           0 :         return NULL;
    2056             :     }
    2057           0 :     return rti->FindNeighbor(name);
    2058             : }
    2059             : 
    2060             : //
    2061             : // Initialize autogen::BgpRouterParams with default values.
    2062             : //
    2063          20 : void BgpIfmapConfigManager::DefaultBgpRouterParams(
    2064             :     autogen::BgpRouterParams *param) {
    2065          20 :     param->Clear();
    2066          20 :     param->autonomous_system = BgpConfigManager::kDefaultAutonomousSystem;
    2067          20 :     param->port = BgpConfigManager::kDefaultPort;
    2068          20 : }
    2069             : 
    2070             : //
    2071             : // Create BgpInsatnceConfig for master routing-instance.  This
    2072             : // includes the BgpIfmapProtocolConfig for the local bgp-router in the
    2073             : // master routing-instance.
    2074             : //
    2075          20 : void BgpIfmapConfigManager::DefaultConfig() {
    2076          20 :     BgpIfmapInstanceConfig *rti = config_->LocateInstance(kMasterInstance);
    2077          20 :     unique_ptr<autogen::BgpRouter> router(new autogen::BgpRouter());
    2078          20 :     autogen::BgpRouterParams param;
    2079          20 :     DefaultBgpRouterParams(&param);
    2080          20 :     router->SetProperty("bgp-router-parameters", &param);
    2081          20 :     BgpIfmapProtocolConfig *protocol = rti->LocateProtocol();
    2082          20 :     protocol->Update(this, router.release());
    2083          20 :     Notify(rti->instance_config(), BgpConfigManager::CFG_ADD);
    2084             : 
    2085          20 :     vector<string> import_list;
    2086          20 :     vector<string> export_list;
    2087          20 :     BGP_CONFIG_LOG_INSTANCE(
    2088             :         Create, server(), rti, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
    2089             :         import_list, export_list,
    2090             :         rti->virtual_network(), rti->virtual_network_index());
    2091             : 
    2092          20 :     BGP_CONFIG_LOG_PROTOCOL(
    2093             :         Create, server(), protocol,
    2094             :         SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
    2095             :         protocol->router_params().admin_down,
    2096             :         protocol->router_params().autonomous_system,
    2097             :         protocol->router_params().identifier,
    2098             :         protocol->router_params().address,
    2099             :         protocol->router_params().hold_time,
    2100             :         vector<string>());
    2101          20 : }
    2102             : 
    2103          30 : bool BgpIfmapGlobalSystemConfig::Update(BgpIfmapConfigManager *manager,
    2104             :         const autogen::GlobalSystemConfig *system) {
    2105          30 :     bool changed = false;
    2106             : 
    2107          30 :     if (data_.gr_enable() != system->graceful_restart_parameters().enable) {
    2108          30 :         data_.set_gr_enable(system->graceful_restart_parameters().enable);
    2109          30 :         changed |= true;
    2110             :     }
    2111             : 
    2112          30 :     if (data_.gr_time() != system->graceful_restart_parameters().restart_time) {
    2113          30 :         data_.set_gr_time(system->graceful_restart_parameters().restart_time);
    2114          30 :         changed |= true;
    2115             :     }
    2116             : 
    2117          30 :     if (data_.llgr_time() != static_cast<uint32_t>(
    2118          30 :             system->graceful_restart_parameters().long_lived_restart_time)) {
    2119          60 :         data_.set_llgr_time(
    2120          30 :             system->graceful_restart_parameters().long_lived_restart_time);
    2121          30 :         changed |= true;
    2122             :     }
    2123             : 
    2124          30 :     if (data_.end_of_rib_timeout() !=
    2125          30 :             system->graceful_restart_parameters().end_of_rib_timeout) {
    2126          60 :         data_.set_end_of_rib_timeout(
    2127          30 :             system->graceful_restart_parameters().end_of_rib_timeout);
    2128          30 :         changed |= true;
    2129             :     }
    2130             : 
    2131          30 :     if (data_.gr_bgp_helper() !=
    2132          30 :             system->graceful_restart_parameters().bgp_helper_enable) {
    2133          60 :         data_.set_gr_bgp_helper(
    2134          30 :             system->graceful_restart_parameters().bgp_helper_enable);
    2135          30 :         changed |= true;
    2136             :     }
    2137             : 
    2138          30 :     if (data_.gr_xmpp_helper() !=
    2139          30 :             system->graceful_restart_parameters().xmpp_helper_enable) {
    2140          60 :         data_.set_gr_xmpp_helper(
    2141          30 :             system->graceful_restart_parameters().xmpp_helper_enable);
    2142          30 :         changed |= true;
    2143             :     }
    2144             : 
    2145          30 :     if (data_.enable_4byte_as() != system->enable_4byte_as()) {
    2146           0 :         data_.set_enable_4byte_as(system->enable_4byte_as());
    2147           0 :         changed |= true;
    2148             :     }
    2149             : 
    2150          30 :     if (data_.always_compare_med() != system->bgp_always_compare_med()) {
    2151           0 :         data_.set_always_compare_med(system->bgp_always_compare_med());
    2152           0 :         changed |= true;
    2153             :     }
    2154             : 
    2155          30 :     if (data_.rd_cluster_seed() != system->rd_cluster_seed()) {
    2156           0 :         data_.set_rd_cluster_seed(system->rd_cluster_seed());
    2157           0 :         changed |= true;
    2158             :     }
    2159             : 
    2160          30 :     if (data_.xmpp_hold_time() !=
    2161          30 :             system->fast_convergence_parameters().xmpp_hold_time) {
    2162           0 :         data_.set_xmpp_hold_time(
    2163           0 :             system->fast_convergence_parameters().xmpp_hold_time);
    2164           0 :         changed |= true;
    2165             :     }
    2166             : 
    2167          30 :     if (data_.bgpaas_port_start() != system->bgpaas_parameters().port_start) {
    2168          20 :         data_.set_bgpaas_port_start(system->bgpaas_parameters().port_start);
    2169          20 :         changed |= true;
    2170             :     }
    2171             : 
    2172          30 :     if (data_.bgpaas_port_end() != system->bgpaas_parameters().port_end) {
    2173          20 :         data_.set_bgpaas_port_end(system->bgpaas_parameters().port_end);
    2174          20 :         changed |= true;
    2175             :     }
    2176             : 
    2177          30 :     if (data_.fc_enabled() != system->fast_convergence_parameters().enable) {
    2178           0 :         data_.set_fc_enabled(system->fast_convergence_parameters().enable);
    2179           0 :         changed |= true;
    2180             :     }
    2181             : 
    2182          30 :     if (data_.nh_check_enabled() !=
    2183          30 :           system->fast_convergence_parameters().nh_reachability_check) {
    2184           0 :         data_.set_nh_check_enabled(
    2185           0 :             system->fast_convergence_parameters().nh_reachability_check);
    2186           0 :         changed |= true;
    2187             :     }
    2188             : 
    2189          30 :     if (data_.all_tags_are_global() != system->bgp_all_tags_are_global()) {
    2190           0 :         data_.set_all_tags_are_global(system->bgp_all_tags_are_global());
    2191           0 :         changed |= true;
    2192             :     }
    2193             : 
    2194          30 :     return changed;
    2195             : }
    2196             : 
    2197           0 : bool BgpIfmapGlobalQosConfig::Update(BgpIfmapConfigManager *manager,
    2198             :         const autogen::GlobalQosConfig *qos) {
    2199           0 :     bool changed = false;
    2200           0 :     const autogen::ControlTrafficDscpType &dscp = qos->control_traffic_dscp();
    2201             : 
    2202           0 :     if (data_.control_dscp() != dscp.control) {
    2203           0 :         data_.set_control_dscp(dscp.control);
    2204           0 :         Sandesh::SetDscpValue(dscp.control);
    2205           0 :         changed = true;
    2206             :     }
    2207           0 :     if (data_.analytics_dscp() != dscp.analytics) {
    2208           0 :         data_.set_analytics_dscp(dscp.analytics);
    2209           0 :         changed = true;
    2210             :     }
    2211           0 :     return changed;
    2212             : }
    2213             : 
    2214             : //
    2215             : // Initialize IdentifierMap with handlers for interesting identifier types.
    2216             : //
    2217             : // The IdentifierMap is used when processing BgpConfigDeltas generated by
    2218             : // the BgpConfigListener.
    2219             : //
    2220          20 : void BgpIfmapConfigManager::IdentifierMapInit() {
    2221          20 :     id_map_.insert(make_pair("routing-instance",
    2222          20 :             boost::bind(&BgpIfmapConfigManager::ProcessRoutingInstance,
    2223             :                         this, _1)));
    2224          20 :     id_map_.insert(make_pair("routing-policy",
    2225          20 :         boost::bind(&BgpIfmapConfigManager::ProcessRoutingPolicy, this, _1)));
    2226          20 :     id_map_.insert(make_pair("routing-policy-routing-instance",
    2227          20 :         boost::bind(&BgpIfmapConfigManager::ProcessRoutingPolicyLink, this,
    2228             :             _1)));
    2229          20 :     id_map_.insert(make_pair("bgp-router",
    2230          20 :         boost::bind(&BgpIfmapConfigManager::ProcessBgpRouter, this, _1)));
    2231          20 :     id_map_.insert(make_pair("bgp-peering",
    2232          20 :         boost::bind(&BgpIfmapConfigManager::ProcessBgpPeering, this, _1)));
    2233          20 :     id_map_.insert(make_pair("global-system-config",
    2234          20 :         boost::bind(&BgpIfmapConfigManager::ProcessGlobalSystemConfig, this,
    2235             :                     _1)));
    2236          20 :     id_map_.insert(make_pair("global-qos-config",
    2237          20 :         boost::bind(&BgpIfmapConfigManager::ProcessGlobalQosConfig, this,
    2238             :                     _1)));
    2239          20 : }
    2240             : 
    2241         280 : void BgpIfmapConfigManager::UpdateInstanceConfig(BgpIfmapInstanceConfig *rti,
    2242             :         BgpConfigManager::EventType event) {
    2243         280 :     if (!rti) {
    2244           0 :         return;
    2245             :     }
    2246             : 
    2247             :     // in case of id change, update import list and call subsequent code
    2248         280 :     Notify(rti->instance_config(), event);
    2249             : 
    2250         280 :     vector<string> import_rt(rti->import_list().begin(),
    2251         560 :                              rti->import_list().end());
    2252         280 :     vector<string> export_rt(rti->export_list().begin(),
    2253         560 :                              rti->export_list().end());
    2254         280 :     if (event == BgpConfigManager::CFG_ADD) {
    2255          80 :         BGP_CONFIG_LOG_INSTANCE(Create, server(), rti,
    2256             :             SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
    2257             :             import_rt, export_rt,
    2258             :             rti->virtual_network(), rti->virtual_network_index());
    2259             :     } else {
    2260         200 :         BGP_CONFIG_LOG_INSTANCE(Update, server(), rti,
    2261             :             SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
    2262             :             import_rt, export_rt,
    2263             :             rti->virtual_network(), rti->virtual_network_index());
    2264             :     }
    2265         280 : }
    2266             : 
    2267             : //
    2268             : // Handler for routing-instance objects.
    2269             : //
    2270             : // Note that BgpIfmapInstanceConfig object for the master instance is created
    2271             : // before we have received any configuration for it i.e. there's no IFMapNode
    2272             : // or autogen::RoutingInstance for it. However, we will eventually receive a
    2273             : // delta for the master.  The IFMapNodeProxy and autogen::RoutingInstance are
    2274             : // set at that time.
    2275             : //
    2276             : // For other routing-instances BgpIfmapConfigInstance can get created before we
    2277             : // see the IFMapNode for the routing-instance if we see the IFMapNode for the
    2278             : // local bgp-router in the routing-instance.  In this case, the IFMapNodeProxy
    2279             : // and autogen::RoutingInstance are set when we later see the IFMapNode for the
    2280             : // routing-instance.
    2281             : //
    2282             : // In all other cases, BgpIfmapConfigInstance is created when we see IFMapNode
    2283             : // for the routing-instance.  The IFMapNodeProxy and autogen::RoutingInstance
    2284             : // are set right away.
    2285             : //
    2286             : // References to the IFMapNode and the autogen::RoutingInstance are
    2287             : // released when the IFMapNode is marked deleted.  However the
    2288             : // BgpIfmapInstanceConfig does not get deleted till the NeighborMap is
    2289             : // empty and the BgpIfmapProtocolConfig is gone.
    2290             : //
    2291         181 : void BgpIfmapConfigManager::ProcessRoutingInstance(
    2292             :     const BgpConfigDelta &delta) {
    2293         181 :     CHECK_CONCURRENCY("bgp::Config");
    2294             : 
    2295         181 :     BgpConfigManager::EventType event = BgpConfigManager::CFG_CHANGE;
    2296         181 :     string instance_name = delta.id_name;
    2297         181 :     BgpIfmapInstanceConfig *rti = config_->FindInstance(instance_name);
    2298         181 :     if (rti == NULL) {
    2299          80 :         IFMapNodeProxy *proxy = delta.node.get();
    2300          80 :         if (proxy == NULL) {
    2301           0 :             return;
    2302             :         }
    2303          80 :         IFMapNode *node = proxy->node();
    2304          80 :         if (node == NULL || node->IsDeleted()) {
    2305           0 :             return;
    2306             :         }
    2307          80 :         event = BgpConfigManager::CFG_ADD;
    2308          80 :         rti = config_->LocateInstance(instance_name);
    2309          80 :         rti->SetNodeProxy(proxy);
    2310             :     } else {
    2311         101 :         IFMapNode *node = rti->node();
    2312         101 :         if (node == NULL) {
    2313          20 :             IFMapNodeProxy *proxy = delta.node.get();
    2314          20 :             if (proxy == NULL) {
    2315           0 :                 return;
    2316             :             }
    2317          20 :             rti->SetNodeProxy(proxy);
    2318          81 :         } else if (node->IsDeleted()) {
    2319           0 :             rti->ResetConfig();
    2320           0 :             if (rti->DeleteIfEmpty(this)) {
    2321           0 :                 BGP_CONFIG_LOG_INSTANCE(
    2322             :                     Delete, server(), rti,
    2323             :                     SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL);
    2324           0 :                 config_->DeleteInstance(rti);
    2325             :             }
    2326           0 :             return;
    2327             :         }
    2328             :     }
    2329             : 
    2330             :     autogen::RoutingInstance *rti_config =
    2331         181 :         static_cast<autogen::RoutingInstance *>(delta.obj.get());
    2332         181 :     if (rti->index() != -1)
    2333         181 :         rti->instance_config()->set_index(rti->index());
    2334         181 :     rti->Update(this, rti_config);
    2335         181 :     UpdateInstanceConfig(rti, event);
    2336         181 : }
    2337             : 
    2338             : //
    2339             : // Handler for bgp protocol config.
    2340             : //
    2341             : // Note that there's no underlying IFMap object for the bgp protocol config.
    2342             : // This is called by the handler for bgp-router objects. The BgpConfigDelta
    2343             : // is a delta for the bgp-router object.
    2344             : //
    2345          20 : void BgpIfmapConfigManager::ProcessBgpProtocol(const BgpConfigDelta &delta) {
    2346          20 :     CHECK_CONCURRENCY("bgp::Config");
    2347             : 
    2348          20 :     BgpConfigManager::EventType event = BgpConfigManager::CFG_CHANGE;
    2349          20 :     string instance_name(IdentifierParent(delta.id_name));
    2350          20 :     BgpIfmapInstanceConfig *rti = config_->FindInstance(instance_name);
    2351          20 :     BgpIfmapProtocolConfig *protocol = NULL;
    2352          20 :     if (rti != NULL) {
    2353          20 :         protocol = rti->protocol_config_mutable();
    2354             :     }
    2355             : 
    2356          20 :     if (protocol == NULL) {
    2357           0 :         IFMapNodeProxy *proxy = delta.node.get();
    2358           0 :         if (proxy == NULL) {
    2359           0 :             return;
    2360             :         }
    2361             :         // ignore identifier with no properties
    2362           0 :         if (delta.obj.get() == NULL) {
    2363           0 :             return;
    2364             :         }
    2365           0 :         IFMapNode *node = proxy->node();
    2366           0 :         if (node == NULL || node->IsDeleted()) {
    2367           0 :             return;
    2368             :         }
    2369           0 :         event = BgpConfigManager::CFG_ADD;
    2370           0 :         if (rti == NULL) {
    2371           0 :             rti = config_->LocateInstance(instance_name);
    2372           0 :             UpdateInstanceConfig(rti, event);
    2373             :         }
    2374           0 :         protocol = rti->LocateProtocol();
    2375           0 :         protocol->SetNodeProxy(proxy);
    2376             :     } else {
    2377          20 :         IFMapNode *node = protocol->node();
    2378          20 :         if (node == NULL) {
    2379             :             // The master instance creates a BgpRouter node internally. Ignore
    2380             :             // an update that doesn't specify any content.
    2381          20 :             if (delta.obj.get() == NULL) {
    2382           0 :                 return;
    2383             :             }
    2384          20 :             protocol->SetNodeProxy(delta.node.get());
    2385           0 :         } else if (delta.obj.get() == NULL) {
    2386           0 :             BGP_CONFIG_LOG_PROTOCOL(Delete, server(), protocol,
    2387             :                 SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL);
    2388           0 :             protocol->Delete(this);
    2389           0 :             rti->ResetProtocol();
    2390           0 :             if (rti->DeleteIfEmpty(this)) {
    2391           0 :                 BGP_CONFIG_LOG_INSTANCE(Delete, server(), rti,
    2392             :                     SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL);
    2393           0 :                 config_->DeleteInstance(rti);
    2394             :             }
    2395           0 :             return;
    2396             :         }
    2397             :     }
    2398             : 
    2399             :     autogen::BgpRouter *rt_config =
    2400          20 :         static_cast<autogen::BgpRouter *>(delta.obj.get());
    2401          20 :     uint32_t old_id = protocol->protocol_config()->identifier();
    2402          20 :     uint32_t old_as = protocol->protocol_config()->autonomous_system();
    2403          20 :     protocol->Update(this, rt_config);
    2404          20 :     uint32_t new_id = protocol->protocol_config()->identifier();
    2405          20 :     uint32_t new_as = protocol->protocol_config()->autonomous_system();
    2406          20 :     if (new_id != old_id || new_as != old_as) {
    2407          20 :         config_->ProcessIdentifierAndASUpdate(this, new_id, old_id, new_as,
    2408             :                                               old_as);
    2409             :     }
    2410          20 :     Notify(protocol->protocol_config(), event);
    2411             : 
    2412          20 :     if (!rt_config) {
    2413           0 :         return;
    2414             :     }
    2415             : 
    2416             :     vector<string> families(
    2417          20 :         protocol->router_params().address_families.begin(),
    2418          40 :         protocol->router_params().address_families.end());
    2419          20 :     if (event == BgpConfigManager::CFG_ADD) {
    2420           0 :         BGP_CONFIG_LOG_PROTOCOL(Create, server(), protocol,
    2421             :             SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
    2422             :             protocol->router_params().admin_down,
    2423             :             protocol->router_params().autonomous_system,
    2424             :             protocol->router_params().identifier,
    2425             :             protocol->router_params().address,
    2426             :             protocol->router_params().hold_time,
    2427             :             families);
    2428             :     } else {
    2429          20 :         BGP_CONFIG_LOG_PROTOCOL(Update, server(), protocol,
    2430             :             SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
    2431             :             protocol->router_params().admin_down,
    2432             :             protocol->router_params().autonomous_system,
    2433             :             protocol->router_params().identifier,
    2434             :             protocol->router_params().address,
    2435             :             protocol->router_params().hold_time,
    2436             :             families);
    2437             :     }
    2438          20 : }
    2439             : 
    2440             : //
    2441             : // Handler for routing policy objects.
    2442             : //
    2443             : // BgpConfigListener::DependencyTracker ensures associated routing instances
    2444             : // are present in the change list.
    2445             : //
    2446           0 : void BgpIfmapConfigManager::ProcessRoutingPolicy(const BgpConfigDelta &delta) {
    2447           0 :     CHECK_CONCURRENCY("bgp::Config");
    2448             : 
    2449           0 :     BgpConfigManager::EventType event = BgpConfigManager::CFG_CHANGE;
    2450           0 :     string policy_name = delta.id_name;
    2451           0 :     BgpIfmapRoutingPolicyConfig *rtp = config_->FindRoutingPolicy(policy_name);
    2452           0 :     if (rtp == NULL) {
    2453           0 :         IFMapNodeProxy *proxy = delta.node.get();
    2454           0 :         if (proxy == NULL) {
    2455           0 :             return;
    2456             :         }
    2457           0 :         IFMapNode *node = proxy->node();
    2458           0 :         if (node == NULL || node->IsDeleted()) {
    2459           0 :             return;
    2460             :         }
    2461           0 :         event = BgpConfigManager::CFG_ADD;
    2462           0 :         rtp = config_->LocateRoutingPolicy(policy_name);
    2463           0 :         rtp->SetNodeProxy(proxy);
    2464             :     } else {
    2465           0 :         IFMapNode *node = rtp->node();
    2466           0 :         if (node == NULL) {
    2467           0 :             IFMapNodeProxy *proxy = delta.node.get();
    2468           0 :             if (proxy == NULL) {
    2469           0 :                 return;
    2470             :             }
    2471           0 :             rtp->SetNodeProxy(proxy);
    2472           0 :         } else if (node->IsDeleted()) {
    2473           0 :             rtp->ResetConfig();
    2474           0 :             if (rtp->DeleteIfEmpty(this)) {
    2475           0 :                 config_->DeleteRoutingPolicy(rtp);
    2476             :             }
    2477           0 :             return;
    2478             :         }
    2479             :     }
    2480             : 
    2481             :     autogen::RoutingPolicy *rtp_config =
    2482           0 :         static_cast<autogen::RoutingPolicy *>(delta.obj.get());
    2483           0 :     rtp->Update(this, rtp_config);
    2484           0 :     Notify(rtp->routing_policy_config(), event);
    2485           0 : }
    2486             : 
    2487             : 
    2488             : //
    2489             : // Handler for bgp-router objects.
    2490             : //
    2491         100 : void BgpIfmapConfigManager::ProcessBgpRouter(const BgpConfigDelta &delta) {
    2492         100 :     CHECK_CONCURRENCY("bgp::Config");
    2493             : 
    2494         100 :     string instance_name(IdentifierParent(delta.id_name));
    2495         200 :     if (instance_name.empty() ||
    2496         100 :         instance_name != BgpConfigManager::kMasterInstance) {
    2497           0 :         return;
    2498             :     }
    2499             : 
    2500             :     // Ignore if this change is not for the local router.
    2501         100 :     string name = delta.id_name.substr(instance_name.size() + 1);
    2502         100 :     if (name != localname_) {
    2503          80 :         return;
    2504             :     }
    2505             : 
    2506          20 :     ProcessBgpProtocol(delta);
    2507             : 
    2508             :     // Update all peerings since we use local asn and identifier from master
    2509             :     // instance for all neighbors, including those in non-master instances.
    2510          20 :     BOOST_FOREACH(const BgpIfmapConfigData::IfmapPeeringMap::value_type &value,
    2511             :         config_->peerings()) {
    2512           0 :         BgpIfmapPeeringConfig *peering = value.second;
    2513           0 :         peering->Update(this, peering->bgp_peering());
    2514             :     }
    2515         180 : }
    2516             : 
    2517             : //
    2518             : // Handler for bgp-peering objects.
    2519             : //
    2520             : // We are only interested in this bgp-peering if it is adjacent to the local
    2521             : // router node.
    2522             : //
    2523             : // The BgpPeeringConfig is created the first time we see the bgp-peering.  It
    2524             : // is updated on subsequent changes and is deleted when the IFMapNode midnode
    2525             : // for the bgp-peering is deleted.
    2526             : //
    2527             : // Note that we are guaranteed that the IFMapNodes for the 2
    2528             : // bgp-routers for the bgp-peering already exist when we see the
    2529             : // bgp-peering. IFMap creates the nodes for the bgp-routers before
    2530             : // creating the midnode. This in turn guarantees that the
    2531             : // BgpIfmapInstanceConfig for the routing-instance also exists since we
    2532             : // create the BgpIfmapInstanceConfig before creating the
    2533             : // BgpIfmapProtocolConfig for a local bgp-router.
    2534             : //
    2535         160 : void BgpIfmapConfigManager::ProcessBgpPeering(const BgpConfigDelta &delta) {
    2536         160 :     CHECK_CONCURRENCY("bgp::Config");
    2537             : 
    2538         160 :     BgpConfigManager::EventType event = BgpConfigManager::CFG_CHANGE;
    2539         160 :     BgpIfmapPeeringConfig *peering = config_->FindPeering(delta.id_name);
    2540         160 :     if (peering == NULL) {
    2541         128 :         IFMapNodeProxy *proxy = delta.node.get();
    2542         128 :         if (proxy == NULL) {
    2543          48 :             BGP_LOG_STR(BgpConfig, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_SYSLOG,
    2544             :                  "ProcessBgpPeering failed. Cannot find proxy " <<
    2545             :                  delta.id_name);
    2546          96 :             return;
    2547             :         }
    2548          80 :         IFMapNode *node = proxy->node();
    2549          80 :         if (node == NULL || delta.obj.get() == NULL) {
    2550           0 :             BGP_LOG_STR(BgpConfig, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_SYSLOG,
    2551             :                  "ProcessBgpPeering failed. Cannot find node/obj " <<
    2552             :                   delta.id_name);
    2553           0 :             return;
    2554             :         }
    2555             : 
    2556          80 :         pair<IFMapNode *, IFMapNode *> routers;
    2557          80 :         if (!BgpIfmapPeeringConfig::GetRouterPair(db_graph_, localname_, node,
    2558             :                                                   &routers)) {
    2559          48 :             return;
    2560             :         }
    2561             : 
    2562          32 :         string instance_name(IdentifierParent(routers.first->name()));
    2563          32 :         BgpIfmapInstanceConfig *rti = config_->FindInstance(instance_name);
    2564          32 :         event = BgpConfigManager::CFG_ADD;
    2565             :         // Create rti if not present.
    2566          32 :         if (rti == NULL) {
    2567           0 :             rti = config_->LocateInstance(instance_name);
    2568           0 :             UpdateInstanceConfig(rti, event);
    2569             :         }
    2570          32 :         peering = config_->CreatePeering(rti, proxy);
    2571          32 :     } else {
    2572          32 :         const IFMapNode *node = peering->node();
    2573          32 :         assert(node != NULL);
    2574          32 :         if (delta.obj.get() == NULL) {
    2575           0 :             BGP_CONFIG_LOG_PEERING(Delete, server(), peering,
    2576             :                 SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL);
    2577           0 :             BgpIfmapInstanceConfig *rti = peering->instance();
    2578           0 :             peering->Delete(this);
    2579           0 :             config_->DeletePeering(peering);
    2580           0 :             if (rti->DeleteIfEmpty(this)) {
    2581           0 :                 BGP_CONFIG_LOG_INSTANCE(Delete, server(), rti,
    2582             :                     SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL);
    2583           0 :                 config_->DeleteInstance(rti);
    2584             :             }
    2585           0 :             return;
    2586             :         }
    2587             :     }
    2588             : 
    2589          64 :     if (event == BgpConfigManager::CFG_ADD) {
    2590          32 :         BGP_CONFIG_LOG_PEERING(Create, server(), peering,
    2591             :             SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL);
    2592             :     } else {
    2593          32 :         BGP_CONFIG_LOG_PEERING(Update, server(), peering,
    2594             :             SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL);
    2595             :     }
    2596             :     autogen::BgpPeering *peering_config =
    2597          64 :             static_cast<autogen::BgpPeering *>(delta.obj.get());
    2598          64 :     peering->Update(this, peering_config);
    2599             : }
    2600             : 
    2601          30 : void BgpIfmapConfigManager::ProcessGlobalSystemConfig(
    2602             :         const BgpConfigDelta &delta) {
    2603          30 :     IFMapNodeProxy *proxy = delta.node.get();
    2604          30 :     if (proxy == NULL)
    2605           0 :         return;
    2606             : 
    2607          30 :     IFMapNode *node = proxy->node();
    2608          30 :     autogen::GlobalSystemConfig *config, default_config;
    2609          30 :     if (node == NULL || node->IsDeleted() || delta.obj.get() == NULL) {
    2610           0 :         config = &default_config;
    2611             :     } else {
    2612          30 :         config = static_cast<autogen::GlobalSystemConfig *>(delta.obj.get());
    2613             :     }
    2614             : 
    2615          30 :     if (config_->global_config()->Update(this, config))
    2616          30 :         Notify(config_->global_config()->config(), BgpConfigManager::CFG_ADD);
    2617          30 : }
    2618             : 
    2619           0 : void BgpIfmapConfigManager::ProcessGlobalQosConfig(
    2620             :         const BgpConfigDelta &delta) {
    2621           0 :     IFMapNodeProxy *proxy = delta.node.get();
    2622           0 :     if (proxy == NULL)
    2623           0 :         return;
    2624             : 
    2625           0 :     IFMapNode *node = proxy->node();
    2626           0 :     autogen::GlobalQosConfig *config, default_config;
    2627           0 :     if (node == NULL || node->IsDeleted() || delta.obj.get() == NULL) {
    2628           0 :         config = &default_config;
    2629             :     } else {
    2630           0 :         config = static_cast<autogen::GlobalQosConfig *>(delta.obj.get());
    2631             :     }
    2632             : 
    2633           0 :     if (config_->global_qos()->Update(this, config))
    2634           0 :         Notify(config_->global_qos()->config(), BgpConfigManager::CFG_ADD);
    2635           0 : }
    2636             : 
    2637             : //
    2638             : // Process the BgpConfigDeltas on the change list. We simply call the handler
    2639             : // for each delta based on the object's identifier type.
    2640             : //
    2641          31 : void BgpIfmapConfigManager::ProcessChanges(const ChangeList &change_list) {
    2642          31 :     CHECK_CONCURRENCY("bgp::Config");
    2643             : 
    2644          31 :     for (ChangeList::const_iterator iter = change_list.begin();
    2645         502 :          iter != change_list.end(); ++iter) {
    2646         471 :         IdentifierMap::iterator loc = id_map_.find(iter->id_type);
    2647         471 :         if (loc != id_map_.end()) {
    2648         471 :             (loc->second)(*iter);
    2649             :         }
    2650             :     }
    2651          31 : }
    2652             : 
    2653             : //
    2654             : // Build and process the change list of BgpConfigDeltas.  The logic to build
    2655             : // the list is in BgpConfigListener and BgpConfigListener::DependencyTracker.
    2656             : //
    2657          31 : bool BgpIfmapConfigManager::ConfigHandler() {
    2658          31 :     CHECK_CONCURRENCY("bgp::Config");
    2659             : 
    2660          31 :     BgpConfigListener::ChangeList change_list;
    2661          31 :     listener_->GetChangeList(&change_list);
    2662          31 :     ProcessChanges(change_list);
    2663          31 :     return true;
    2664          31 : }
    2665             : 
    2666             : //
    2667             : // Terminate the BgpConfigManager.
    2668             : //
    2669          20 : void BgpIfmapConfigManager::Terminate() {
    2670          20 :     listener_->Terminate();
    2671          20 :     config_.reset();
    2672          20 : }

Generated by: LCOV version 1.14