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