LCOV - code coverage report
Current view: top level - bgp - bgp_config_parser.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 246 543 45.3 %
Date: 2026-11-16 02:06:09 Functions: 16 31 51.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_parser.h"
       7             : 
       8             : #include <boost/uuid/name_generator.hpp>
       9             : #include <pugixml/pugixml.hpp>
      10             : 
      11             : #include <map>
      12             : #include <set>
      13             : #include <sstream>
      14             : #include <utility>
      15             : #include <vector>
      16             : 
      17             : #include "bgp/bgp_config.h"
      18             : #include "bgp/bgp_log.h"
      19             : #include "bgp/rtarget/rtarget_address.h"
      20             : #include "ifmap/ifmap_server_table.h"
      21             : 
      22             : #include "schema/bgp_schema_types.h"
      23             : #include "schema/vnc_cfg_types.h"
      24             : 
      25             : using pugi::xml_attribute;
      26             : using pugi::xml_document;
      27             : using pugi::xml_node;
      28             : using pugi::xml_parse_result;
      29             : using std::unique_ptr;
      30             : using std::istringstream;
      31             : using std::list;
      32             : using std::ostringstream;
      33             : using std::make_pair;
      34             : using std::map;
      35             : using std::multimap;
      36             : using std::pair;
      37             : using std::set;
      38             : using std::string;
      39             : using std::vector;
      40             : 
      41             : typedef multimap<
      42             :     pair<string, string>,
      43             :     pair<autogen::BgpSessionAttributes, string> > SessionMap;
      44             : 
      45             : namespace {
      46             : 
      47         130 : static void MapObjectSetProperty(const string &ltype, const string &lname,
      48             :                                  const string &propname,
      49             :                                  AutogenProperty *property,
      50             :                                  BgpConfigParser::RequestList *requests) {
      51         130 :     DBRequest *request = new DBRequest;
      52         130 :     request->oper = DBRequest::DB_ENTRY_ADD_CHANGE;
      53         130 :     IFMapTable::RequestKey *key = new IFMapTable::RequestKey();
      54         130 :     request->key.reset(key);
      55         130 :     key->id_type = ltype;
      56         130 :     key->id_name = lname;
      57         130 :     IFMapServerTable::RequestData *data = new IFMapServerTable::RequestData();
      58         130 :     request->data.reset(data);
      59         130 :     data->metadata = propname;
      60         130 :     data->content.reset(property);
      61         130 :     data->origin.set_origin(IFMapOrigin::MAP_SERVER);
      62         130 :     requests->push_back(request);
      63         130 : }
      64             : 
      65           0 : static void MapObjectClearProperty(const string &ltype, const string &lname,
      66             :                                    const string &propname,
      67             :                                    BgpConfigParser::RequestList *requests) {
      68           0 :     DBRequest *request = new DBRequest;
      69           0 :     request->oper = DBRequest::DB_ENTRY_DELETE;
      70           0 :     IFMapTable::RequestKey *key = new IFMapTable::RequestKey();
      71           0 :     request->key.reset(key);
      72           0 :     key->id_type = ltype;
      73           0 :     key->id_name = lname;
      74           0 :     IFMapServerTable::RequestData *data = new IFMapServerTable::RequestData();
      75           0 :     request->data.reset(data);
      76           0 :     data->metadata = propname;
      77           0 :     data->origin.set_origin(IFMapOrigin::MAP_SERVER);
      78           0 :     requests->push_back(request);
      79           0 : }
      80             : 
      81         100 : static void MapObjectLink(const string &ltype, const string &lname,
      82             :                          const string &rtype, const string &rname,
      83             :                          const string &linkname,
      84             :                          BgpConfigParser::RequestList *requests) {
      85         100 :     DBRequest *request = new DBRequest;
      86         100 :     request->oper = DBRequest::DB_ENTRY_ADD_CHANGE;
      87         100 :     IFMapTable::RequestKey *key = new IFMapTable::RequestKey();
      88         100 :     request->key.reset(key);
      89         100 :     key->id_type = ltype;
      90         100 :     key->id_name = lname;
      91         100 :     IFMapServerTable::RequestData *data = new IFMapServerTable::RequestData();
      92         100 :     request->data.reset(data);
      93         100 :     data->metadata = linkname;
      94         100 :     data->id_type = rtype;
      95         100 :     data->id_name = rname;
      96         100 :     data->origin.set_origin(IFMapOrigin::MAP_SERVER);
      97         100 :     requests->push_back(request);
      98         100 : }
      99             : 
     100           0 : static void MapObjectUnlink(const string &ltype, const string &lname,
     101             :                             const string &rtype, const string &rname,
     102             :                             const string &linkname,
     103             :                             BgpConfigParser::RequestList *requests) {
     104           0 :     DBRequest *request = new DBRequest;
     105           0 :     request->oper = DBRequest::DB_ENTRY_DELETE;
     106           0 :     IFMapTable::RequestKey *key = new IFMapTable::RequestKey();
     107           0 :     request->key.reset(key);
     108           0 :     key->id_type = ltype;
     109           0 :     key->id_name = lname;
     110           0 :     IFMapServerTable::RequestData *data = new IFMapServerTable::RequestData();
     111           0 :     request->data.reset(data);
     112           0 :     data->metadata = linkname;
     113           0 :     data->id_type = rtype;
     114           0 :     data->id_name = rname;
     115           0 :     data->origin.set_origin(IFMapOrigin::MAP_SERVER);
     116           0 :     requests->push_back(request);
     117           0 : }
     118             : 
     119         400 : static void MapObjectLinkAttr(const string &ltype, const string &lname,
     120             :                              const string &rtype, const string &rname,
     121             :                              const string &linkname, AutogenProperty *attr,
     122             :                              BgpConfigParser::RequestList *requests) {
     123         400 :     DBRequest *request = new DBRequest;
     124         400 :     request->oper = DBRequest::DB_ENTRY_ADD_CHANGE;
     125         400 :     IFMapTable::RequestKey *key = new IFMapTable::RequestKey();
     126         400 :     request->key.reset(key);
     127         400 :     key->id_type = ltype;
     128         400 :     key->id_name = lname;
     129         400 :     IFMapServerTable::RequestData *data = new IFMapServerTable::RequestData();
     130         400 :     request->data.reset(data);
     131         400 :     data->metadata = linkname;
     132         400 :     data->id_type = rtype;
     133         400 :     data->id_name = rname;
     134         400 :     data->content.reset(attr);
     135         400 :     data->origin.set_origin(IFMapOrigin::MAP_SERVER);
     136         400 :     requests->push_back(request);
     137         400 : }
     138             : 
     139         160 : static autogen::BgpSessionAttributes *GetPeeringSessionAttribute(
     140             :         const pair<string, string> &key, autogen::BgpPeeringAttributes *peer,
     141             :         int session_id, const string config_uuid) {
     142         160 :     string uuid;
     143         160 :     if (!config_uuid.empty()) {
     144           0 :         uuid = config_uuid;
     145             :     } else {
     146         160 :         uuid = BgpConfigParser::session_uuid(key.first, key.second, session_id);
     147             :     }
     148             : 
     149         160 :     autogen::BgpSession *session = NULL;
     150         160 :     for (vector<autogen::BgpSession>::iterator iter =
     151         160 :             peer->session.begin();
     152         160 :          iter != peer->session.end(); ++iter) {
     153          80 :         if (iter->uuid == uuid) {
     154          80 :             session = iter.operator->();
     155          80 :             break;
     156             :         }
     157             :     }
     158         160 :     if (session == NULL) {
     159          80 :         peer->session.push_back(autogen::BgpSession());
     160          80 :         session = &peer->session.back();
     161          80 :         session->uuid = uuid;
     162             :     }
     163         160 :     session->attributes.push_back(autogen::BgpSessionAttributes());
     164         160 :     autogen::BgpSessionAttributes *sattr = &session->attributes.back();
     165         160 :     return sattr;
     166         160 : }
     167             : 
     168          80 : static void MaybeMergeBidirectionalSessionParams(
     169             :         autogen::BgpPeeringAttributes *peer) {
     170          80 : }
     171             : 
     172         110 : static void BuildPeeringLinks(const string &instance,
     173             :                               const SessionMap &sessions,
     174             :                               BgpConfigParser::RequestList *requests) {
     175             :     typedef map<pair<string, string>, autogen::BgpPeeringAttributes *>
     176             :         PeeringMap;
     177         110 :     PeeringMap peerings;
     178             : 
     179         110 :     pair<string, string> sprev;
     180         110 :     int session_id = 0;
     181         110 :     for (SessionMap::const_iterator iter = sessions.begin();
     182         270 :          iter != sessions.end(); ++iter) {
     183         160 :         const string &left = iter->first.first;
     184         160 :         const string &right = iter->first.second;
     185         160 :         pair<string, string> key;
     186         160 :         if (left <= right) {
     187          80 :             key = make_pair(left, right);
     188             :         } else {
     189          80 :             key = make_pair(right, left);
     190             :         }
     191             : 
     192         160 :         string config_uuid = iter->second.second;
     193         160 :         if ((sprev.first == left) && (sprev.second == right)) {
     194           0 :             session_id++;
     195             :         } else {
     196         160 :             session_id = 1;
     197         160 :             sprev = make_pair(left, right);
     198             :         }
     199             : 
     200         160 :         autogen::BgpPeeringAttributes *peer = NULL;
     201         160 :         PeeringMap::iterator loc = peerings.find(key);
     202         160 :         if (loc == peerings.end()) {
     203          80 :             peer = new autogen::BgpPeeringAttributes();
     204          80 :             peerings.insert(make_pair(key, peer));
     205             :         } else {
     206          80 :             peer = loc->second;
     207             :         }
     208             :         // add uni-directional attributes for this session.
     209             :         autogen::BgpSessionAttributes *attrp =
     210         160 :             GetPeeringSessionAttribute(key, peer, session_id, config_uuid);
     211         160 :         attrp->Copy(iter->second.first);
     212         160 :         attrp->bgp_router = left;
     213         160 :     }
     214             : 
     215             :     // generate the links.
     216             :     // merging uni-directional attributes into a common attribute when they
     217             :     // are the same.
     218         190 :     for (PeeringMap::iterator iter = peerings.begin(); iter != peerings.end();
     219          80 :          ++iter) {
     220          80 :         autogen::BgpPeeringAttributes *peer = iter->second;
     221          80 :         MaybeMergeBidirectionalSessionParams(peer);
     222          80 :         string left(instance + ':'), right(instance  + ':');
     223          80 :         left.append(iter->first.first);
     224          80 :         right.append(iter->first.second);
     225          80 :         MapObjectLinkAttr("bgp-router", left, "bgp-router", right,
     226             :                           "bgp-peering", peer, requests);
     227          80 :     }
     228         110 : }
     229             : 
     230           0 : static void RemovePeeringLinks(const string &instance,
     231             :                                const SessionMap &sessions,
     232             :                                BgpConfigParser::RequestList *requests) {
     233           0 :     set<pair<string, string> > key_set;
     234             : 
     235           0 :     for (SessionMap::const_iterator iter = sessions.begin();
     236           0 :          iter != sessions.end(); ++iter) {
     237           0 :         const string &left = iter->first.first;
     238           0 :         const string &right = iter->first.second;
     239           0 :         pair<string, string> key;
     240           0 :         if (left <= right) {
     241           0 :             key = make_pair(left, right);
     242             :         } else {
     243           0 :             key = make_pair(right, left);
     244             :         }
     245           0 :         if (key_set.count(key) > 0) {
     246           0 :             continue;
     247             :         }
     248           0 :         key_set.insert(key);
     249           0 :         string id_left(instance + ':'), id_right(instance  + ':');
     250           0 :         id_left.append(key.first);
     251           0 :         id_right.append(key.second);
     252             : 
     253           0 :         MapObjectUnlink("bgp-router", id_left, "bgp-router", id_right,
     254             :                         "bgp-peering", requests);
     255           0 :     }
     256           0 : }
     257             : 
     258         160 : static bool ParseSession(const string &identifier, const xml_node &node,
     259             :                          SessionMap *sessions) {
     260         160 :     autogen::BgpSessionAttributes attr;
     261         160 :     attr.Clear();
     262         160 :     xml_attribute to = node.attribute("to");
     263         160 :     assert(to);
     264         160 :     assert(attr.XmlParse(node));
     265             : 
     266         160 :     string to_value = to.value();
     267         160 :     string to_name, uuid;
     268         160 :     size_t pos = to_value.find(':');
     269         160 :     if (pos == string::npos) {
     270         160 :         to_name = to_value;
     271             :     } else {
     272           0 :         to_name = to_value.substr(0, pos);
     273           0 :         uuid = string(to_value, pos + 1);
     274             :     }
     275             : 
     276         160 :     sessions->insert(
     277         320 :         make_pair(make_pair(identifier, to_name), make_pair(attr, uuid)));
     278         160 :     return true;
     279         160 : }
     280             : 
     281           0 : static bool ParseServiceChain(const string &instance, const xml_node &node,
     282             :                               bool add_change, const string &sc_info,
     283             :                               BgpConfigParser::RequestList *requests) {
     284             :     unique_ptr<autogen::ServiceChainInfo> property(
     285           0 :         new autogen::ServiceChainInfo());
     286           0 :     property->sc_head = true;
     287           0 :     assert(property->XmlParse(node));
     288             : 
     289           0 :     if (add_change) {
     290           0 :         MapObjectSetProperty("routing-instance", instance,
     291           0 :             sc_info, property.release(), requests);
     292             :     } else {
     293           0 :         MapObjectClearProperty("routing-instance", instance,
     294             :             sc_info, requests);
     295             :     }
     296             : 
     297           0 :     return true;
     298           0 : }
     299             : 
     300           0 : static bool ParseInstanceRouteAggregate(const string &instance,
     301             :     const xml_node &node, bool add_change,
     302             :     BgpConfigParser::RequestList *requests) {
     303             : 
     304           0 :     xml_attribute to = node.attribute("to");
     305           0 :     assert(to);
     306           0 :     string aggregate_name = to.value();
     307           0 :     if (add_change) {
     308           0 :         MapObjectLinkAttr("routing-instance", instance,
     309             :             "route-aggregate", aggregate_name,
     310             :             "route-aggregate-routing-instance", NULL, requests);
     311             :     } else {
     312           0 :         MapObjectUnlink("routing-instance", instance,
     313             :             "route-aggregate", aggregate_name,
     314             :             "route-aggregate-routing-instance", requests);
     315             :     }
     316             : 
     317           0 :     return true;
     318           0 : }
     319             : 
     320           0 : static bool ParseInstanceRoutingPolicy(const string &instance,
     321             :     const xml_node &node, bool add_change,
     322             :     BgpConfigParser::RequestList *requests) {
     323             : 
     324           0 :     xml_attribute to = node.attribute("to");
     325           0 :     assert(to);
     326           0 :     string policy_name = to.value();
     327             :     unique_ptr<autogen::RoutingPolicyType> attr(
     328           0 :         new autogen::RoutingPolicyType());
     329           0 :     assert(attr->XmlParse(node));
     330           0 :     if (add_change) {
     331           0 :         MapObjectLinkAttr("routing-instance", instance,
     332             :             "routing-policy", policy_name,
     333           0 :             "routing-policy-routing-instance", attr.release(), requests);
     334             :     } else {
     335           0 :         MapObjectUnlink("routing-instance", instance,
     336             :             "routing-policy", policy_name,
     337             :             "routing-policy-routing-instance", requests);
     338             :     }
     339             : 
     340           0 :     return true;
     341           0 : }
     342             : 
     343           0 : static bool ParseStaticRoute(const string &instance, const xml_node &node,
     344             :                               bool add_change,
     345             :                               BgpConfigParser::RequestList *requests) {
     346             :     unique_ptr<autogen::StaticRouteEntriesType> property(
     347           0 :         new autogen::StaticRouteEntriesType());
     348           0 :     assert(property->XmlParse(node));
     349             : 
     350           0 :     if (add_change) {
     351           0 :         MapObjectSetProperty("routing-instance", instance,
     352           0 :             "static-route-entries", property.release(), requests);
     353             :     } else {
     354           0 :         MapObjectClearProperty("routing-instance", instance,
     355             :             "static-route-entries", requests);
     356             :     }
     357             : 
     358           0 :     return true;
     359           0 : }
     360             : 
     361           0 : static bool ParseInstanceHasPnf(const string &instance, const xml_node &node,
     362             :     bool add_change, BgpConfigParser::RequestList *requests) {
     363             :     unique_ptr<autogen::RoutingInstance::OolProperty> property(
     364           0 :         new autogen::RoutingInstance::OolProperty);
     365           0 :     property->data = (string(node.child_value()) == "true");
     366           0 :     if (add_change) {
     367           0 :         MapObjectSetProperty("routing-instance", instance,
     368           0 :             "routing-instance-has-pnf", property.release(), requests);
     369             :     } else {
     370           0 :         MapObjectClearProperty("routing-instance", instance,
     371             :             "routing-instance-has-pnf", requests);
     372             :     }
     373             : 
     374           0 :     return true;
     375           0 : }
     376             : 
     377         100 : static bool ParseBgpRouter(const string &instance, const xml_node &node,
     378             :                            bool add_change, string *nodename,
     379             :                            SessionMap *sessions,
     380             :                            BgpConfigParser::RequestList *requests) {
     381             :     unique_ptr<autogen::BgpRouterParams> property(
     382         100 :         new autogen::BgpRouterParams());
     383         100 :     xml_attribute name = node.attribute("name");
     384         100 :     assert(name);
     385         100 :     string identifier = name.value();
     386         100 :     assert(property->XmlParse(node));
     387             : 
     388         100 :     if (property->autonomous_system == 0) {
     389           0 :         property->autonomous_system =
     390           0 :             BgpConfigManager::kDefaultAutonomousSystem;
     391             :     }
     392         100 :     if (property->identifier.empty()) {
     393           0 :         property->identifier = property->address;
     394             :     }
     395             : 
     396         100 :     bool has_sessions = false;
     397         260 :     for (xml_node xsession = node.child("session"); xsession;
     398         160 :          xsession = xsession.next_sibling("session")) {
     399         160 :         has_sessions = true;
     400         160 :         ParseSession(identifier, xsession, sessions);
     401             :     }
     402             : 
     403         100 :     string fqname(instance + ":" + identifier);
     404         100 :     if (!has_sessions) {
     405           0 :         *nodename = fqname;
     406             :     }
     407             : 
     408         100 :     string subcluster_name;
     409         100 :     if (node.child("sub-cluster")) {
     410           0 :         xml_attribute sc = node.child("sub-cluster").attribute("name");
     411           0 :         subcluster_name = sc.value();
     412           0 :         assert(!subcluster_name.empty());
     413             :     }
     414             : 
     415         100 :     if (add_change) {
     416         100 :         MapObjectLink("routing-instance", instance,
     417             :             "bgp-router", fqname, "instance-bgp-router", requests);
     418         100 :         MapObjectSetProperty("bgp-router", fqname,
     419         100 :             "bgp-router-parameters", property.release(), requests);
     420         100 :         if (!subcluster_name.empty()) {
     421           0 :             MapObjectLink("bgp-router", fqname, "sub-cluster", subcluster_name,
     422             :                           "bgp-router-sub-cluster", requests);
     423             :         }
     424             :     } else {
     425           0 :         MapObjectClearProperty("bgp-router", fqname,
     426             :             "bgp-router-parameters", requests);
     427           0 :         MapObjectUnlink("routing-instance", instance,
     428             :             "bgp-router", fqname, "instance-bgp-router", requests);
     429           0 :         if (!subcluster_name.empty()) {
     430           0 :             MapObjectUnlink("bgp-router", fqname, "sub-cluster",
     431             :                     subcluster_name, "bgp-router-sub-cluster", requests);
     432             :         }
     433             :     }
     434             : 
     435         100 :     return true;
     436         100 : }
     437             : 
     438             : // Creates a full-mesh of links between all the routers that have been
     439             : // defined.
     440         110 : static void AddNeighborMesh(const list<string> &routers,
     441             :                             BgpConfigParser::RequestList *requests) {
     442         110 :     for (list<string>::const_iterator iter = routers.begin();
     443         110 :          iter != routers.end(); ) {
     444           0 :         const string &left = *iter;
     445           0 :         ++iter;
     446           0 :         for (list<string>::const_iterator tgt = iter;
     447           0 :              tgt != routers.end(); ++tgt) {
     448           0 :             MapObjectLink("bgp-router", left, "bgp-router", *tgt, "bgp-peering",
     449             :                          requests);
     450             :         }
     451             :     }
     452         110 : }
     453             : 
     454           0 : static void DeleteNeighborMesh(const list<string> &routers,
     455             :                                BgpConfigParser::RequestList *requests) {
     456           0 :     for (list<string>::const_iterator iter = routers.begin();
     457           0 :          iter != routers.end(); ) {
     458           0 :         const string &left = *iter;
     459           0 :         ++iter;
     460           0 :         for (list<string>::const_iterator tgt = iter;
     461           0 :              tgt != routers.end(); ++tgt) {
     462           0 :             MapObjectUnlink("bgp-router", left, "bgp-router", *tgt,
     463             :                 "bgp-peering", requests);
     464             :         }
     465             :     }
     466           0 : }
     467             : 
     468         320 : static bool ParseInstanceTarget(const string &instance, const xml_node &node,
     469             :                                 bool add_change,
     470             :                                 BgpConfigParser::RequestList *requests) {
     471         320 :     string rtarget(node.child_value());
     472         320 :     boost::trim(rtarget);
     473         320 :     boost::system::error_code parse_err;
     474         320 :     RouteTarget::FromString(rtarget, &parse_err);
     475         320 :     assert(!parse_err);
     476             : 
     477             :     unique_ptr<autogen::InstanceTargetType> params(
     478         320 :         new autogen::InstanceTargetType());
     479         320 :     assert(params->XmlParse(node));
     480             : 
     481         320 :     if (add_change) {
     482         320 :         MapObjectLinkAttr("routing-instance", instance, "route-target", rtarget,
     483         320 :             "instance-target", params.release(), requests);
     484             :     } else {
     485           0 :         MapObjectUnlink("routing-instance", instance, "route-target", rtarget,
     486             :             "instance-target", requests);
     487             :     }
     488             : 
     489         320 :     return true;
     490         320 : }
     491             : 
     492           0 : static bool ParseInstanceVirtualNetwork(const string &instance,
     493             :     const xml_node &node, bool add_change,
     494             :     BgpConfigParser::RequestList *requests) {
     495           0 :     string vn_name = node.child_value();
     496           0 :     if (add_change) {
     497           0 :         MapObjectLink("routing-instance", instance,
     498             :             "virtual-network", vn_name,
     499             :             "virtual-network-routing-instance", requests);
     500             :     } else {
     501           0 :         MapObjectUnlink("routing-instance", instance,
     502             :             "virtual-network", vn_name,
     503             :             "virtual-network-routing-instance", requests);
     504             :     }
     505             : 
     506           0 :     return true;
     507           0 : }
     508             : 
     509             : }  // namespace
     510             : 
     511          30 : BgpConfigParser::BgpConfigParser(DB *db)
     512          30 :         : db_(db) {
     513          30 : }
     514             : 
     515          80 : bool BgpConfigParser::ParseRoutingInstance(const xml_node &parent,
     516             :                                            bool add_change,
     517             :                                            RequestList *requests) const {
     518          80 :     string instance(parent.attribute("name").value());
     519          80 :     assert(!instance.empty());
     520             : 
     521          80 :     SessionMap sessions;
     522          80 :     list<string> routers;
     523             : 
     524         400 :     for (xml_node node = parent.first_child(); node;
     525         320 :          node = node.next_sibling()) {
     526         320 :         if (strcmp(node.name(), "bgp-router") == 0) {
     527           0 :             string router_name;
     528           0 :             ParseBgpRouter(instance, node, add_change, &router_name,
     529             :                            &sessions, requests);
     530           0 :             if (!router_name.empty()) {
     531           0 :                 routers.push_back(router_name);
     532             :             }
     533         320 :         } else if (strcmp(node.name(), "vrf-target") == 0) {
     534         320 :             ParseInstanceTarget(instance, node, add_change, requests);
     535           0 :         } else if (strcmp(node.name(), "virtual-network") == 0) {
     536           0 :             ParseInstanceVirtualNetwork(instance, node, add_change, requests);
     537           0 :         } else if (strcmp(node.name(), "service-chain-info") == 0) {
     538           0 :             ParseServiceChain(instance, node, add_change,
     539             :                               "service-chain-information", requests);
     540           0 :         } else if (strcmp(node.name(), "ipv6-service-chain-info") == 0) {
     541           0 :             ParseServiceChain(instance, node, add_change,
     542             :                               "ipv6-service-chain-information", requests);
     543           0 :         } else if (strcmp(node.name(), "evpn-service-chain-info") == 0) {
     544           0 :             ParseServiceChain(instance, node, add_change,
     545             :                               "evpn-service-chain-information", requests);
     546           0 :         } else if (strcmp(node.name(), "evpn-ipv6-service-chain-info") == 0) {
     547           0 :             ParseServiceChain(instance, node, add_change,
     548             :                               "evpn-ipv6-service-chain-information", requests);
     549           0 :         } else if (strcmp(node.name(), "route-aggregate") == 0) {
     550           0 :             ParseInstanceRouteAggregate(instance, node, add_change, requests);
     551           0 :         } else if (strcmp(node.name(), "routing-policy") == 0) {
     552           0 :             ParseInstanceRoutingPolicy(instance, node, add_change, requests);
     553           0 :         } else if (strcmp(node.name(), "static-route-entries") == 0) {
     554           0 :             ParseStaticRoute(instance, node, add_change, requests);
     555           0 :         } else if (strcmp(node.name(), "routing-instance-has-pnf") == 0) {
     556           0 :             ParseInstanceHasPnf(instance, node, add_change, requests);
     557             :         }
     558             :     }
     559             : 
     560          80 :     if (add_change) {
     561          80 :         BuildPeeringLinks(instance, sessions, requests);
     562             :         // Generate a full mesh of peering sessions for neighbors that do not
     563             :         // specify session attributes.
     564          80 :         AddNeighborMesh(routers, requests);
     565             :     } else {
     566           0 :         RemovePeeringLinks(instance, sessions, requests);
     567           0 :         DeleteNeighborMesh(routers, requests);
     568             :     }
     569          80 :     return true;
     570          80 : }
     571             : 
     572           0 : bool BgpConfigParser::ParseVirtualNetwork(const xml_node &node,
     573             :                                           bool add_change,
     574             :                                           RequestList *requests) const {
     575             :     // vn name
     576           0 :     string vn_name(node.attribute("name").value());
     577           0 :     assert(!vn_name.empty());
     578             : 
     579             :     unique_ptr<autogen::VirtualNetwork::OolProperty> pbb_property(
     580           0 :         new autogen::VirtualNetwork::OolProperty);
     581           0 :     pbb_property->data = false;
     582             : 
     583           0 :     if (node.attribute("pbb-evpn-enable")) {
     584           0 :         pbb_property->data =
     585           0 :             (string(node.attribute("pbb-evpn-enable").value()) == "true");
     586             :     }
     587             : 
     588             :     unique_ptr<autogen::VirtualNetworkType> property(
     589           0 :         new autogen::VirtualNetworkType());
     590           0 :     assert(property->XmlParse(node));
     591             : 
     592           0 :     if (add_change) {
     593           0 :         MapObjectSetProperty("virtual-network", vn_name,
     594           0 :             "virtual-network-properties", property.release(), requests);
     595           0 :         MapObjectSetProperty("virtual-network", vn_name,
     596           0 :             "pbb-evpn-enable", pbb_property.release(), requests);
     597             :     } else {
     598           0 :         MapObjectClearProperty("virtual-network", vn_name,
     599             :             "virtual-network-properties", requests);
     600           0 :         MapObjectClearProperty("virtual-network", vn_name,
     601             :             "pbb-evpn-enable", requests);
     602             :     }
     603             : 
     604           0 :     return true;
     605           0 : }
     606             : 
     607           0 : bool BgpConfigParser::ParseSubCluster(const xml_node &node,
     608             :                                       bool add_change,
     609             :                                       RequestList *requests) const {
     610           0 :     string subcluster_name(node.attribute("name").value());
     611           0 :     assert(!subcluster_name.empty());
     612             : 
     613             :     unique_ptr<autogen::SubCluster::StringProperty> subcluster_property(
     614           0 :         new autogen::SubCluster::StringProperty);
     615             : 
     616           0 :     if (node.child("sub-cluster-asn"))
     617           0 :         subcluster_property->data = string(node.child_value("sub-cluster-asn"));
     618             : 
     619           0 :     if (add_change) {
     620           0 :         MapObjectSetProperty("sub-cluster", subcluster_name,
     621           0 :             "sub-cluster-asn", subcluster_property.release(), requests);
     622             :     } else {
     623           0 :         MapObjectClearProperty("sub-cluster", subcluster_name,
     624             :                                "sub-cluster-asn", requests);
     625             :     }
     626             : 
     627             :     unique_ptr<autogen::SubCluster::NtProperty> id_property(
     628           0 :         new autogen::SubCluster::NtProperty);
     629           0 :     if (node.child("sub-cluster-id")) {
     630             :         std::stringstream sub_cluster_id(
     631           0 :                 string(node.child_value("sub-cluster-id")));
     632           0 :         sub_cluster_id >> id_property->data;
     633           0 :     }
     634           0 :     if (add_change) {
     635           0 :         MapObjectSetProperty("sub-cluster", subcluster_name,
     636           0 :             "sub-cluster-id", id_property.release(), requests);
     637             :     } else {
     638           0 :         MapObjectClearProperty("sub-cluster", subcluster_name,
     639             :                                "sub-cluster-id", requests);
     640             :     }
     641           0 :     return true;
     642           0 : }
     643             : 
     644           0 : bool BgpConfigParser::ParseRouteAggregate(const xml_node &node,
     645             :                                           bool add_change,
     646             :                                           RequestList *requests) const {
     647             :     // policy name
     648           0 :     string aggregate_name(node.attribute("name").value());
     649           0 :     assert(!aggregate_name.empty());
     650             : 
     651             : 
     652           0 :     for (xml_node child = node.first_child(); child;
     653           0 :          child = child.next_sibling()) {
     654           0 :         if (strcmp(child.name(), "aggregate-route-entries") == 0) {
     655           0 :             if (add_change) {
     656             :                 unique_ptr<autogen::RouteListType>
     657           0 :                     aggregate_routes(new autogen::RouteListType());
     658           0 :                 assert(aggregate_routes->XmlParse(child));
     659           0 :                 MapObjectSetProperty("route-aggregate", aggregate_name,
     660             :                                      "aggregate-route-entries",
     661           0 :                                      aggregate_routes.release(), requests);
     662           0 :             } else {
     663           0 :                 MapObjectClearProperty("route-aggregate", aggregate_name,
     664             :                                        "aggregate-route-entries", requests);
     665             :             }
     666           0 :         } else if (strcmp(child.name(), "nexthop") == 0) {
     667           0 :             if (add_change) {
     668           0 :                 string nexthop = child.child_value();
     669             : 
     670             :                 autogen::RouteAggregate::StringProperty *nexthop_property =
     671           0 :                     new autogen::RouteAggregate::StringProperty();
     672           0 :                 nexthop_property->data = nexthop;
     673           0 :                 MapObjectSetProperty("route-aggregate", aggregate_name,
     674             :                                      "aggregate-route-nexthop",
     675             :                                      nexthop_property, requests);
     676           0 :             } else {
     677           0 :                 MapObjectClearProperty("route-aggregate", aggregate_name,
     678             :                                        "aggregate-route-nexthop", requests);
     679             :             }
     680             :         }
     681             :     }
     682             : 
     683           0 :     return true;
     684           0 : }
     685             : 
     686           0 : bool BgpConfigParser::ParseRoutingPolicy(const xml_node &node,
     687             :                                           bool add_change,
     688             :                                           RequestList *requests) const {
     689             :     // policy name
     690           0 :     string policy_name(node.attribute("name").value());
     691           0 :     assert(!policy_name.empty());
     692             : 
     693             :     unique_ptr<autogen::PolicyStatementType> policy_statement(
     694           0 :         new autogen::PolicyStatementType());
     695           0 :     assert(policy_statement->XmlParse(node));
     696             : 
     697           0 :     if (add_change) {
     698           0 :         MapObjectSetProperty("routing-policy", policy_name,
     699           0 :             "routing-policy-entries", policy_statement.release(), requests);
     700             :     } else {
     701           0 :         MapObjectClearProperty("routing-policy", policy_name,
     702             :             "routing-policy-entries", requests);
     703             :     }
     704             : 
     705           0 :     return true;
     706           0 : }
     707             : 
     708          30 : bool BgpConfigParser::ParseGlobalSystemConfig(const xml_node &node,
     709             :                                               bool add_change,
     710             :                                               RequestList *requests) const {
     711          60 :     for (xml_node child = node.first_child(); child;
     712          30 :             child = child.next_sibling()) {
     713          30 :         if (strcmp(child.name(), "graceful-restart-parameters") == 0) {
     714             :             unique_ptr<autogen::GracefulRestartParametersType> gr_config(
     715          30 :                     new autogen::GracefulRestartParametersType());
     716          30 :             assert(gr_config->XmlParse(child));
     717             : 
     718          30 :             if (add_change) {
     719          30 :                 MapObjectSetProperty("global-system-config", "",
     720             :                     "graceful-restart-parameters",
     721          30 :                     gr_config.release(), requests);
     722             :             } else {
     723           0 :                 MapObjectClearProperty("global-system-config", "",
     724             :                                        "graceful-restart-parameters", requests);
     725             :             }
     726          30 :         }
     727          30 :         if (strcmp(child.name(), "bgpaas-parameters") == 0) {
     728             :             unique_ptr<autogen::BGPaaServiceParametersType> bgpaas_config(
     729           0 :                     new autogen::BGPaaServiceParametersType());
     730           0 :             assert(bgpaas_config->XmlParse(child));
     731             : 
     732           0 :             if (add_change) {
     733           0 :                 MapObjectSetProperty("global-system-config", "",
     734           0 :                     "bgpaas-parameters", bgpaas_config.release(), requests);
     735             :             } else {
     736           0 :                 MapObjectClearProperty("global-system-config", "",
     737             :                                        "bgpaas-parameters", requests);
     738             :             }
     739           0 :         }
     740          30 :         if (strcmp(child.name(), "bgp-always-compare-med") == 0) {
     741             :             unique_ptr<autogen::GlobalSystemConfig::OolProperty> property(
     742           0 :                 new autogen::GlobalSystemConfig::OolProperty);
     743           0 :             property->data = (string(child.child_value()) == "true");
     744           0 :             if (add_change) {
     745           0 :                 MapObjectSetProperty("global-system-config", "",
     746           0 :                     "bgp-always-compare-med", property.release(), requests);
     747             :             } else {
     748           0 :                 MapObjectClearProperty("global-system-config", "",
     749             :                     "bgp-always-compare-med", requests);
     750             :             }
     751           0 :         }
     752          30 :         if (strcmp(child.name(), "bgp-all-tags-are-global") == 0) {
     753             :             unique_ptr<autogen::GlobalSystemConfig::OolProperty> property(
     754           0 :                 new autogen::GlobalSystemConfig::OolProperty);
     755           0 :             property->data = (string(child.child_value()) == "true");
     756           0 :             if (add_change) {
     757           0 :                 MapObjectSetProperty("global-system-config", "",
     758           0 :                     "bgp-all-tags-are-global", property.release(), requests);
     759             :             } else {
     760           0 :                 MapObjectClearProperty("global-system-config", "",
     761             :                     "bgp-all-tags-are-global", requests);
     762             :             }
     763           0 :         }
     764          30 :         if (strcmp(child.name(), "enable-4byte-as") == 0) {
     765             :             unique_ptr<autogen::GlobalSystemConfig::OolProperty> property(
     766           0 :                 new autogen::GlobalSystemConfig::OolProperty);
     767           0 :             property->data = (string(child.child_value()) == "true");
     768           0 :             if (add_change) {
     769           0 :                 MapObjectSetProperty("global-system-config", "",
     770           0 :                     "enable-4byte-as", property.release(), requests);
     771             :             } else {
     772           0 :                 MapObjectClearProperty("global-system-config", "",
     773             :                     "enable-4byte-as", requests);
     774             :             }
     775           0 :         }
     776          30 :         if (strcmp(child.name(), "fast-convergence-parameters") == 0) {
     777             :             unique_ptr<autogen::FastConvergenceParametersType> property(
     778           0 :                     new autogen::FastConvergenceParametersType());
     779           0 :             assert(property->XmlParse(child));
     780           0 :             if (add_change) {
     781           0 :                 MapObjectSetProperty("global-system-config", "",
     782           0 :                     "fast-convergence-parameters", property.release(), requests);
     783             :             } else {
     784           0 :                 MapObjectClearProperty("global-system-config", "",
     785             :                     "fast-convergence-parameters", requests);
     786             :             }
     787           0 :         }
     788          30 :         if (strcmp(child.name(), "rd-cluster-seed") == 0) {
     789             :             unique_ptr<autogen::GlobalSystemConfig::NtProperty> property(
     790           0 :                 new autogen::GlobalSystemConfig::NtProperty);
     791           0 :             property->data = atoi(child.child_value());
     792           0 :             if (add_change) {
     793           0 :                 MapObjectSetProperty("global-system-config", "",
     794           0 :                     "rd-cluster-seed", property.release(), requests);
     795             :             } else {
     796           0 :                 MapObjectClearProperty("global-system-config", "",
     797             :                     "rd-cluster-seed", requests);
     798             :             }
     799           0 :         }
     800             :     }
     801          30 :     return true;
     802             : }
     803             : 
     804           0 : bool BgpConfigParser::ParseGlobalQosConfig(const xml_node &node,
     805             :                                            bool add_change,
     806             :                                            RequestList *requests) const {
     807           0 :     for (xml_node child = node.first_child(); child;
     808           0 :             child = child.next_sibling()) {
     809           0 :         if (strcmp(child.name(), "control-traffic-dscp") == 0) {
     810             :             unique_ptr<autogen::ControlTrafficDscpType> cfg(
     811           0 :                     new autogen::ControlTrafficDscpType());
     812           0 :             assert(cfg->XmlParse(child));
     813             : 
     814           0 :             if (add_change) {
     815           0 :                 MapObjectSetProperty("global-qos-config", "",
     816           0 :                     "control-traffic-dscp", cfg.release(), requests);
     817             :             } else {
     818           0 :                 MapObjectClearProperty("global-qos-config", "",
     819             :                                        "control-traffic-dscp", requests);
     820             :             }
     821           0 :         }
     822             :     }
     823           0 :     return true;
     824             : }
     825             : 
     826          30 : bool BgpConfigParser::ParseConfig(const xml_node &root, bool add_change,
     827             :                                   RequestList *requests) const {
     828          30 :     SessionMap sessions;
     829          30 :     list<string> routers;
     830             : 
     831         240 :     for (xml_node node = root.first_child(); node; node = node.next_sibling()) {
     832         210 :         if (strcmp(node.name(), "bgp-router") == 0) {
     833         100 :             string router_name;
     834         100 :             ParseBgpRouter(BgpConfigManager::kMasterInstance, node, add_change,
     835             :                            &router_name, &sessions, requests);
     836         100 :             if (!router_name.empty()) {
     837           0 :                 routers.push_back(router_name);
     838             :             }
     839         100 :         }
     840         210 :         if (strcmp(node.name(), "routing-instance") == 0) {
     841          80 :             ParseRoutingInstance(node, add_change, requests);
     842             :         }
     843         210 :         if (strcmp(node.name(), "virtual-network") == 0) {
     844           0 :             ParseVirtualNetwork(node, add_change, requests);
     845             :         }
     846         210 :         if (strcmp(node.name(), "sub-cluster") == 0) {
     847           0 :             ParseSubCluster(node, add_change, requests);
     848             :         }
     849         210 :         if (strcmp(node.name(), "route-aggregate") == 0) {
     850           0 :             ParseRouteAggregate(node, add_change, requests);
     851             :         }
     852         210 :         if (strcmp(node.name(), "routing-policy") == 0) {
     853           0 :             ParseRoutingPolicy(node, add_change, requests);
     854             :         }
     855         210 :         if (strcmp(node.name(), "global-system-config") == 0) {
     856          30 :             ParseGlobalSystemConfig(node, add_change, requests);
     857             :         }
     858         210 :         if (strcmp(node.name(), "global-qos-config") == 0) {
     859           0 :             ParseGlobalQosConfig(node, add_change, requests);
     860             :         }
     861             :     }
     862             : 
     863          30 :     if (add_change) {
     864          30 :         BuildPeeringLinks(BgpConfigManager::kMasterInstance, sessions,
     865             :                           requests);
     866             :         // Generate a full mesh of peering sessions for neighbors that do not
     867             :         // specify session attributes.
     868          30 :         AddNeighborMesh(routers, requests);
     869             :     } else {
     870           0 :         RemovePeeringLinks(BgpConfigManager::kMasterInstance, sessions,
     871             :                            requests);
     872           0 :         DeleteNeighborMesh(routers, requests);
     873             :     }
     874             : 
     875          30 :     return true;
     876          30 : }
     877             : 
     878          30 : bool BgpConfigParser::Parse(const string &content)  {
     879          30 :     istringstream sstream(content);
     880          30 :     xml_document xdoc;
     881          30 :     xml_parse_result result = xdoc.load(sstream);
     882          30 :     if (result.status == pugi::status_no_document_element) {
     883           0 :         return true;
     884             :     }
     885             : 
     886          30 :     if (result.status == pugi::status_ok) {
     887          30 :         RequestList requests;
     888          60 :         for (xml_node node = xdoc.first_child(); node; node = node.next_sibling()) {
     889          30 :             const char *oper = node.name();
     890          30 :             assert((strcmp(oper, "config") == 0) || (strcmp(oper, "delete") == 0));
     891          30 :             bool add_change = (strcmp(oper, "config") == 0);
     892          30 :             assert(ParseConfig(node, add_change, &requests));
     893             :         }
     894             : 
     895         660 :         while (!requests.empty()) {
     896         630 :             unique_ptr<DBRequest> req(requests.front());
     897         630 :             requests.pop_front();
     898             : 
     899             :             IFMapTable::RequestKey *key =
     900         630 :                     static_cast<IFMapTable::RequestKey *>(req->key.get());
     901             : 
     902         630 :             IFMapTable *table = IFMapTable::FindTable(db_, key->id_type);
     903         630 :             assert(table);
     904         630 :             table->Enqueue(req.get());
     905         630 :         }
     906             : 
     907          30 :         return true;
     908          30 :     }
     909           0 :     return false;
     910          30 : }
     911             : 
     912         192 : string BgpConfigParser::session_uuid(const string &left, const string &right,
     913             :                                      int index) {
     914             :     boost::uuids::nil_generator nil;
     915         192 :     boost::uuids::name_generator gen(nil());
     916             :     boost::uuids::uuid uuid;
     917         192 :     ostringstream oss;
     918         192 :     oss << left << ":" << right << ":" << index;
     919         192 :     uuid = gen(oss.str());
     920         384 :     return boost::uuids::to_string(uuid);
     921         192 : }

Generated by: LCOV version 1.14