8 #include <boost/uuid/name_generator.hpp>
9 #include <pugixml/pugixml.hpp>
22 #include "schema/bgp_schema_types.h"
23 #include "schema/vnc_cfg_types.h"
25 using pugi::xml_attribute;
26 using pugi::xml_document;
28 using pugi::xml_parse_result;
29 using std::unique_ptr;
30 using std::istringstream;
32 using std::ostringstream;
43 pair<autogen::BgpSessionAttributes, string> >
SessionMap;
47 static void MapObjectSetProperty(
const string <ype,
const string &lname,
48 const string &propname,
54 request->
key.reset(key);
58 request->
data.reset(data);
62 requests->push_back(request);
65 static void MapObjectClearProperty(
const string <ype,
const string &lname,
66 const string &propname,
71 request->
key.reset(key);
75 request->
data.reset(data);
78 requests->push_back(request);
81 static void MapObjectLink(
const string <ype,
const string &lname,
82 const string &rtype,
const string &rname,
83 const string &linkname,
88 request->
key.reset(key);
92 request->
data.reset(data);
97 requests->push_back(request);
100 static void MapObjectUnlink(
const string <ype,
const string &lname,
101 const string &rtype,
const string &rname,
102 const string &linkname,
107 request->
key.reset(key);
111 request->
data.reset(data);
116 requests->push_back(request);
119 static void MapObjectLinkAttr(
const string <ype,
const string &lname,
120 const string &rtype,
const string &rname,
126 request->
key.reset(key);
130 request->
data.reset(data);
136 requests->push_back(request);
139 static autogen::BgpSessionAttributes *GetPeeringSessionAttribute(
140 const pair<string, string> &key, autogen::BgpPeeringAttributes *peer,
141 int session_id,
const string config_uuid) {
143 if (!config_uuid.empty()) {
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->();
158 if (session == NULL) {
159 peer->session.push_back(autogen::BgpSession());
160 session = &peer->session.back();
161 session->uuid =
uuid;
163 session->attributes.push_back(autogen::BgpSessionAttributes());
164 autogen::BgpSessionAttributes *sattr = &session->attributes.back();
168 static void MaybeMergeBidirectionalSessionParams(
169 autogen::BgpPeeringAttributes *peer) {
172 static void BuildPeeringLinks(
const string &instance,
175 typedef map<pair<string, string>, autogen::BgpPeeringAttributes *>
179 pair<string, string> sprev;
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;
187 key = make_pair(left, right);
189 key = make_pair(right, left);
192 string config_uuid = iter->second.second;
193 if ((sprev.first == left) && (sprev.second == right)) {
197 sprev = make_pair(left, right);
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));
209 autogen::BgpSessionAttributes *attrp =
210 GetPeeringSessionAttribute(key, peer, session_id, config_uuid);
211 attrp->Copy(iter->second.first);
212 attrp->bgp_router = left;
218 for (PeeringMap::iterator iter = peerings.begin(); iter != peerings.end();
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);
230 static void RemovePeeringLinks(
const string &instance,
233 set<pair<string, string> > key_set;
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;
241 key = make_pair(left, right);
243 key = make_pair(right, left);
245 if (key_set.count(key) > 0) {
249 string id_left(instance +
':'), id_right(instance +
':');
250 id_left.append(key.first);
251 id_right.append(key.second);
253 MapObjectUnlink(
"bgp-router", id_left,
"bgp-router", id_right,
254 "bgp-peering", requests);
258 static bool ParseSession(
const string &identifier,
const xml_node &node,
260 autogen::BgpSessionAttributes attr;
262 xml_attribute to = node.attribute(
"to");
264 assert(attr.XmlParse(node));
266 string to_value = to.value();
267 string to_name,
uuid;
268 size_t pos = to_value.find(
':');
269 if (pos == string::npos) {
272 to_name = to_value.substr(0, pos);
273 uuid = string(to_value, pos + 1);
277 make_pair(make_pair(identifier, to_name), make_pair(attr,
uuid)));
281 static bool ParseServiceChain(
const string &instance,
const xml_node &node,
282 bool add_change,
const string &sc_info,
284 unique_ptr<autogen::ServiceChainInfo> property(
285 new autogen::ServiceChainInfo());
286 property->sc_head =
true;
287 assert(property->XmlParse(node));
290 MapObjectSetProperty(
"routing-instance", instance,
291 sc_info, property.release(), requests);
293 MapObjectClearProperty(
"routing-instance", instance,
300 static bool ParseInstanceRouteAggregate(
const string &instance,
301 const xml_node &node,
bool add_change,
304 xml_attribute to = node.attribute(
"to");
306 string aggregate_name = to.value();
308 MapObjectLinkAttr(
"routing-instance", instance,
309 "route-aggregate", aggregate_name,
310 "route-aggregate-routing-instance", NULL, requests);
312 MapObjectUnlink(
"routing-instance", instance,
313 "route-aggregate", aggregate_name,
314 "route-aggregate-routing-instance", requests);
320 static bool ParseInstanceRoutingPolicy(
const string &instance,
321 const xml_node &node,
bool add_change,
324 xml_attribute to = node.attribute(
"to");
326 string policy_name = to.value();
327 unique_ptr<autogen::RoutingPolicyType> attr(
328 new autogen::RoutingPolicyType());
329 assert(attr->XmlParse(node));
331 MapObjectLinkAttr(
"routing-instance", instance,
332 "routing-policy", policy_name,
333 "routing-policy-routing-instance", attr.release(), requests);
335 MapObjectUnlink(
"routing-instance", instance,
336 "routing-policy", policy_name,
337 "routing-policy-routing-instance", requests);
343 static bool ParseStaticRoute(
const string &instance,
const xml_node &node,
346 unique_ptr<autogen::StaticRouteEntriesType> property(
347 new autogen::StaticRouteEntriesType());
348 assert(property->XmlParse(node));
351 MapObjectSetProperty(
"routing-instance", instance,
352 "static-route-entries", property.release(), requests);
354 MapObjectClearProperty(
"routing-instance", instance,
355 "static-route-entries", requests);
361 static bool ParseInstanceHasPnf(
const string &instance,
const xml_node &node,
363 unique_ptr<autogen::RoutingInstance::OolProperty> property(
364 new autogen::RoutingInstance::OolProperty);
365 property->data = (string(node.child_value()) ==
"true");
367 MapObjectSetProperty(
"routing-instance", instance,
368 "routing-instance-has-pnf", property.release(), requests);
370 MapObjectClearProperty(
"routing-instance", instance,
371 "routing-instance-has-pnf", requests);
377 static bool ParseBgpRouter(
const string &instance,
const xml_node &node,
378 bool add_change,
string *nodename,
381 unique_ptr<autogen::BgpRouterParams> property(
382 new autogen::BgpRouterParams());
383 xml_attribute name = node.attribute(
"name");
385 string identifier = name.value();
386 assert(property->XmlParse(node));
388 if (property->autonomous_system == 0) {
389 property->autonomous_system =
392 if (property->identifier.empty()) {
393 property->identifier =
property->address;
396 bool has_sessions =
false;
397 for (xml_node xsession = node.child(
"session"); xsession;
398 xsession = xsession.next_sibling(
"session")) {
400 ParseSession(identifier, xsession, sessions);
403 string fqname(instance +
":" + identifier);
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());
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);
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);
440 static void AddNeighborMesh(
const list<string> &routers,
442 for (list<string>::const_iterator iter = routers.begin();
443 iter != routers.end(); ) {
444 const string &left = *iter;
446 for (list<string>::const_iterator tgt = iter;
447 tgt != routers.end(); ++tgt) {
448 MapObjectLink(
"bgp-router", left,
"bgp-router", *tgt,
"bgp-peering",
454 static void DeleteNeighborMesh(
const list<string> &routers,
456 for (list<string>::const_iterator iter = routers.begin();
457 iter != routers.end(); ) {
458 const string &left = *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);
468 static bool ParseInstanceTarget(
const string &instance,
const xml_node &node,
471 string rtarget(node.child_value());
472 boost::trim(rtarget);
473 boost::system::error_code parse_err;
477 unique_ptr<autogen::InstanceTargetType> params(
478 new autogen::InstanceTargetType());
479 assert(params->XmlParse(node));
482 MapObjectLinkAttr(
"routing-instance", instance,
"route-target", rtarget,
483 "instance-target", params.release(), requests);
485 MapObjectUnlink(
"routing-instance", instance,
"route-target", rtarget,
486 "instance-target", requests);
492 static bool ParseInstanceVirtualNetwork(
const string &instance,
493 const xml_node &node,
bool add_change,
495 string vn_name = node.child_value();
497 MapObjectLink(
"routing-instance", instance,
498 "virtual-network", vn_name,
499 "virtual-network-routing-instance", requests);
501 MapObjectUnlink(
"routing-instance", instance,
502 "virtual-network", vn_name,
503 "virtual-network-routing-instance", requests);
518 string instance(parent.attribute(
"name").value());
519 assert(!instance.empty());
522 list<string> routers;
524 for (xml_node node = parent.first_child(); node;
525 node = node.next_sibling()) {
526 if (strcmp(node.name(),
"bgp-router") == 0) {
528 ParseBgpRouter(instance, node, add_change, &router_name,
529 &sessions, requests);
530 if (!router_name.empty()) {
531 routers.push_back(router_name);
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);
561 BuildPeeringLinks(instance, sessions, requests);
564 AddNeighborMesh(routers, requests);
566 RemovePeeringLinks(instance, sessions, requests);
567 DeleteNeighborMesh(routers, requests);
576 string vn_name(node.attribute(
"name").value());
577 assert(!vn_name.empty());
579 unique_ptr<autogen::VirtualNetwork::OolProperty> pbb_property(
580 new autogen::VirtualNetwork::OolProperty);
581 pbb_property->data =
false;
583 if (node.attribute(
"pbb-evpn-enable")) {
585 (string(node.attribute(
"pbb-evpn-enable").value()) ==
"true");
588 unique_ptr<autogen::VirtualNetworkType> property(
589 new autogen::VirtualNetworkType());
590 assert(property->XmlParse(node));
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);
598 MapObjectClearProperty(
"virtual-network", vn_name,
599 "virtual-network-properties", requests);
600 MapObjectClearProperty(
"virtual-network", vn_name,
601 "pbb-evpn-enable", requests);
610 string subcluster_name(node.attribute(
"name").value());
611 assert(!subcluster_name.empty());
613 unique_ptr<autogen::SubCluster::StringProperty> subcluster_property(
614 new autogen::SubCluster::StringProperty);
616 if (node.child(
"sub-cluster-asn"))
617 subcluster_property->data = string(node.child_value(
"sub-cluster-asn"));
620 MapObjectSetProperty(
"sub-cluster", subcluster_name,
621 "sub-cluster-asn", subcluster_property.release(), requests);
623 MapObjectClearProperty(
"sub-cluster", subcluster_name,
624 "sub-cluster-asn", requests);
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;
635 MapObjectSetProperty(
"sub-cluster", subcluster_name,
636 "sub-cluster-id", id_property.release(), requests);
638 MapObjectClearProperty(
"sub-cluster", subcluster_name,
639 "sub-cluster-id", requests);
648 string aggregate_name(node.attribute(
"name").value());
649 assert(!aggregate_name.empty());
652 for (xml_node child = node.first_child(); child;
653 child = child.next_sibling()) {
654 if (strcmp(child.name(),
"aggregate-route-entries") == 0) {
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);
663 MapObjectClearProperty(
"route-aggregate", aggregate_name,
664 "aggregate-route-entries", requests);
666 }
else if (strcmp(child.name(),
"nexthop") == 0) {
668 string nexthop = child.child_value();
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);
677 MapObjectClearProperty(
"route-aggregate", aggregate_name,
678 "aggregate-route-nexthop", requests);
690 string policy_name(node.attribute(
"name").value());
691 assert(!policy_name.empty());
693 unique_ptr<autogen::PolicyStatementType> policy_statement(
694 new autogen::PolicyStatementType());
695 assert(policy_statement->XmlParse(node));
698 MapObjectSetProperty(
"routing-policy", policy_name,
699 "routing-policy-entries", policy_statement.release(), requests);
701 MapObjectClearProperty(
"routing-policy", policy_name,
702 "routing-policy-entries", requests);
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));
719 MapObjectSetProperty(
"global-system-config",
"",
720 "graceful-restart-parameters",
721 gr_config.release(), requests);
723 MapObjectClearProperty(
"global-system-config",
"",
724 "graceful-restart-parameters", requests);
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));
733 MapObjectSetProperty(
"global-system-config",
"",
734 "bgpaas-parameters", bgpaas_config.release(), requests);
736 MapObjectClearProperty(
"global-system-config",
"",
737 "bgpaas-parameters", requests);
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");
745 MapObjectSetProperty(
"global-system-config",
"",
746 "bgp-always-compare-med", property.release(), requests);
748 MapObjectClearProperty(
"global-system-config",
"",
749 "bgp-always-compare-med", requests);
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");
757 MapObjectSetProperty(
"global-system-config",
"",
758 "bgp-all-tags-are-global", property.release(), requests);
760 MapObjectClearProperty(
"global-system-config",
"",
761 "bgp-all-tags-are-global", requests);
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");
769 MapObjectSetProperty(
"global-system-config",
"",
770 "enable-4byte-as", property.release(), requests);
772 MapObjectClearProperty(
"global-system-config",
"",
773 "enable-4byte-as", requests);
776 if (strcmp(child.name(),
"fast-convergence-parameters") == 0) {
777 unique_ptr<autogen::FastConvergenceParametersType> property(
778 new autogen::FastConvergenceParametersType());
779 assert(property->XmlParse(child));
781 MapObjectSetProperty(
"global-system-config",
"",
782 "fast-convergence-parameters", property.release(), requests);
784 MapObjectClearProperty(
"global-system-config",
"",
785 "fast-convergence-parameters", requests);
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());
793 MapObjectSetProperty(
"global-system-config",
"",
794 "rd-cluster-seed", property.release(), requests);
796 MapObjectClearProperty(
"global-system-config",
"",
797 "rd-cluster-seed", requests);
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));
815 MapObjectSetProperty(
"global-qos-config",
"",
816 "control-traffic-dscp", cfg.release(), requests);
818 MapObjectClearProperty(
"global-qos-config",
"",
819 "control-traffic-dscp", requests);
829 list<string> routers;
831 for (xml_node node = root.first_child(); node; node = node.next_sibling()) {
832 if (strcmp(node.name(),
"bgp-router") == 0) {
835 &router_name, &sessions, requests);
836 if (!router_name.empty()) {
837 routers.push_back(router_name);
840 if (strcmp(node.name(),
"routing-instance") == 0) {
843 if (strcmp(node.name(),
"virtual-network") == 0) {
846 if (strcmp(node.name(),
"sub-cluster") == 0) {
849 if (strcmp(node.name(),
"route-aggregate") == 0) {
852 if (strcmp(node.name(),
"routing-policy") == 0) {
855 if (strcmp(node.name(),
"global-system-config") == 0) {
858 if (strcmp(node.name(),
"global-qos-config") == 0) {
868 AddNeighborMesh(routers, requests);
872 DeleteNeighborMesh(routers, requests);
879 istringstream sstream(content);
881 xml_parse_result result = xdoc.load(sstream);
882 if (result.status == pugi::status_no_document_element) {
886 if (result.status == pugi::status_ok) {
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);
895 while (!requests.empty()) {
896 unique_ptr<DBRequest> req(requests.front());
897 requests.pop_front();
914 boost::uuids::nil_generator nil;
915 boost::uuids::name_generator gen(nil());
918 oss << left <<
":" << right <<
":" << index;
919 uuid = gen(oss.str());
920 return boost::uuids::to_string(
uuid);
multimap< pair< string, string >, pair< autogen::BgpSessionAttributes, string > > SessionMap
static const uint32_t kDefaultAutonomousSystem
static const char * kMasterInstance
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)
static IFMapTable * FindTable(DB *db, const std::string &element_type)
static RouteTarget FromString(const std::string &str, boost::system::error_code *error=NULL)
std::unique_ptr< DBRequestKey > key
std::unique_ptr< DBRequestData > data
void set_origin(Origin in_origin)
std::unique_ptr< AutogenProperty > content