2 from netaddr import IPAddress
3 from string import Template
6 def get_active_controller_from_json(resp, service):
7 """Gets index of active controller running specified service
9 :param resp: JSON formatted response from EOS
11 :param service: EOS Service to look for
13 :return: Index of controller
15 entities = json.loads(resp)['entity-owners']['entity-type']
16 for entity in entities:
17 if entity['type'] == "org.opendaylight.mdsal.ServiceEntityType":
18 for instance in entity['entity']:
19 if service in instance['id']:
20 return int(instance['owner'][-1:])
25 """Gets modulo of number
27 :param num: Number to be used
29 :param base: Base used
31 :returns: Int representing modulo of specified numbers.
34 return int(num) % int(base)
37 def get_average_of_items(items):
38 """Gets average of items in provided list
40 :param items: To be proceed
41 :return: Average value
44 return sum(items) / len(items)
47 def get_opposing_mode(mode):
48 """Generate string representing opposing SXP peer mode
50 :param mode: SXP peer mode
52 :returns: String with opposing SXP peer mode.
57 elif 'listener' == mode:
62 def get_ip_from_number(n, base=2130706432):
63 """Generate string representing Ipv4 from specified number plus base value
65 :param n: Number to be converted
67 :param base: Starting index
69 :returns: String containing Ipv4.
72 ip = IPAddress(int(base) + n)
76 def get_ip_from_number_and_ip(n, ip_address):
77 """Generate string representing Ipv4 from specified number and IPAddress
79 :param n: Number to be converted
81 :param ip_address: Base address
83 :returns: String containing Ipv4.
86 ip = IPAddress(int(IPAddress(ip_address)) + n)
90 def lower_version(ver1, ver2):
91 """Generate xml containing SGT mach data
93 :param ver1: Version of SXP protocol for compare
95 :param ver2: Version of SXP protocol for compare
97 :returns: String containing lower from those two specified versions.
108 def get_filter_entry(seq, entry_type, sgt="", esgt="", acl="", eacl="", pl="", epl="", ps=""):
109 """Generate xml containing FilterEntry data
111 :param seq: Sequence of entry
113 :param entry_type: Type of entry (permit/deny)
114 :type entry_type: str
115 :param sgt: SGT matches to be added to entry
117 :param esgt: SGT ranges match to be added to entry
119 :param acl: ACL matches to be added to entry
121 :param eacl: EACL matches to be added to entry
123 :param pl: PrefixList matches to be added to entry
125 :param epl: ExtendedPrefixList matches to be added to entry
127 :param ps: PeerSequence matches to be added to entry
129 :returns: String containing xml data for request
133 # Generate XML request containing combination of Matches of different types
135 args = sgt.split(',')
136 entries += add_sgt_matches_xml(args)
138 args = esgt.split(',')
139 entries += add_sgt_range_xml(args[0], args[1])
141 entries += add_pl_entry_xml(pl)
143 args = epl.split(',')
144 entries += add_epl_entry_xml(args[0], args[1], args[2])
146 args = acl.split(',')
147 entries += add_acl_entry_xml(args[0], args[1])
149 args = eacl.split(',')
150 entries += add_eacl_entry_xml(args[0], args[1], args[2], args[3])
153 entries += add_ps_entry_xml(args[0], args[1])
154 # Wrap entries in ACL/PrefixList according to specified values
156 return add_pl_entry_default_xml(seq, entry_type, entries)
158 return add_ps_entry_default_xml(seq, entry_type, entries)
159 return add_acl_entry_default_xml(seq, entry_type, entries)
162 def add_peers(*args):
163 """Generate xml containing Peer mach data
165 :param args: Peers data
167 :returns: String containing xml data for request
172 <peer-address>$ip</peer-address>
175 for count, value in enumerate(args):
176 peers += templ.substitute({'ip': value})
180 def add_domains(*args):
181 """Generate xml containing Domain mach data
183 :param args: Domain data
185 :returns: String containing xml data for request
193 for count, value in enumerate(args):
194 peers += templ.substitute({'name': value})
198 def add_sgt_matches_xml(sgt_entries):
199 """Generate xml containing SGT mach data
201 :param sgt_entries: SGT matches
202 :type sgt_entries: str
203 :returns: String containing xml data for request
207 <matches>$sgt</matches>''')
209 for sgt in sgt_entries:
210 matches += templ.substitute({'sgt': sgt})
214 def add_sgt_range_xml(start, end):
215 """Generate xml containing SGT RangeMach data
217 :param start: Start range of SGT
219 :param end: End range of SGT
221 :returns: String containing xml data for request
225 <sgt-start>$start</sgt-start>
226 <sgt-end>$end</sgt-end>''')
227 match = templ.substitute({'start': start, 'end': end})
231 def add_acl_entry_default_xml(seq, entry_type, acl_entries):
232 """Generate xml containing AccessList data
234 :param seq: Sequence of PrefixList entry
236 :param entry_type: Entry type (permit/deny)
237 :type entry_type: str
238 :param acl_entries: XML data containing AccessList entries
239 :type acl_entries: str
240 :returns: String containing xml data for request
245 <entry-type>$entry_type</entry-type>
246 <entry-seq>$seq</entry-seq>$acl_entries
248 matches = templ.substitute(
249 {'seq': seq, 'entry_type': entry_type, 'acl_entries': acl_entries})
253 def add_acl_entry_xml(ip, mask):
254 """Generate xml containing AccessList data
256 :param ip: Ipv4/6 address
258 :param mask: Ipv4/6 wildcard mask
260 :returns: String containing xml data for request
265 <ip-address>$ip</ip-address>
266 <wildcard-mask>$mask</wildcard-mask>
268 return templ.substitute({'ip': ip, 'mask': mask})
271 def add_eacl_entry_xml(ip, mask, amask, wmask):
272 """Generate xml containing ExtendedAccessList data
274 :param ip: Ipv4/6 address
276 :param mask: Ipv4/6 wildcard mask
278 :param amask: Ipv4/6 address mask
280 :param wmask: Ipv4/6 address wildcard mask
282 :returns: String containing xml data for request
287 <ip-address>$ip</ip-address>
288 <wildcard-mask>$mask</wildcard-mask>
290 <address-mask>$amask</address-mask>
291 <wildcard-mask>$wmask</wildcard-mask>
294 return templ.substitute({'ip': ip, 'mask': mask, 'amask': amask, 'wmask': wmask})
297 def add_ps_entry_default_xml(seq, entry_type, ps_entries):
298 """Generate xml containing PeerSequence data
300 :param seq: Sequence of PrefixList entry
302 :param entry_type: Entry type (permit/deny)
303 :type entry_type: str
304 :param ps_entries: XML data containing PeerSequence entries
305 :type ps_entries: str
306 :returns: String containing xml data for request
310 <peer-sequence-entry xmlns="urn:opendaylight:sxp:controller">
311 <entry-type>$entry_type</entry-type>
312 <entry-seq>$seq</entry-seq>$ps_entries
313 </peer-sequence-entry>''')
314 return templ.substitute({'seq': seq, 'entry_type': entry_type, 'ps_entries': ps_entries})
317 def add_pl_entry_default_xml(seq, entry_type, pl_entries):
318 """Generate xml containing PrefixList data
320 :param seq: Sequence of PrefixList entry
322 :param entry_type: Entry type (permit/deny)
323 :type entry_type: str
324 :param pl_entries: XML data containing PrefixList entries
325 :type pl_entries: str
326 :returns: String containing xml data for request
330 <prefix-list-entry xmlns="urn:opendaylight:sxp:controller">
331 <entry-type>$entry_type</entry-type>
332 <entry-seq>$seq</entry-seq>$pl_entries
333 </prefix-list-entry>''')
334 return templ.substitute({'seq': seq, 'entry_type': entry_type, 'pl_entries': pl_entries})
337 def add_pl_entry_xml(prefix):
338 """Generate xml containing PrefixList data
340 :param prefix: Ipv4/6 prefix
342 :returns: String containing xml data for request
347 <ip-prefix>$prefix</ip-prefix>
348 </prefix-list-match>''')
349 return templ.substitute({'prefix': prefix})
352 def add_epl_entry_xml(prefix, op, mask):
353 """Generate xml containing Extended PrefixList data
355 :param prefix: Ipv4/6 prefix
357 :param op: PrefixList option (ge/le/eq)
359 :param mask: Ipv4/6 Mask
361 :returns: String containing xml data for request
366 <ip-prefix>$prefix</ip-prefix>
368 <mask-range>$op</mask-range>
369 <mask-value>$mask</mask-value>
371 </prefix-list-match>''')
372 return templ.substitute({'prefix': prefix, 'mask': mask, 'op': op})
375 def add_ps_entry_xml(op, length):
376 """Generate xml containing Extended PrefixList data
378 :param op: PrefixList option (ge/le/eq)
380 :param length: PeerSequence length
382 :returns: String containing xml data for request
386 <peer-sequence-length>$length</peer-sequence-length>
387 <peer-sequence-range>$op</peer-sequence-range>
389 return templ.substitute({'length': length, 'op': op})
392 def parse_peer_groups(groups_json):
393 """Parse JSON string into Array of PeerGroups
395 :param groups_json: JSON containing PeerGroups
396 :type groups_json: str
397 :returns: Array containing PeerGroups.
400 data = json.loads(groups_json)
401 groups = data['output']
403 for group in groups.values():
408 def parse_connections(connections_json):
409 """Parse JSON string into Array of Connections
411 :param connections_json: JSON containing Connections
412 :type connections_json: str
413 :returns: Array containing Connections.
416 data = json.loads(connections_json)
417 connections = data['output']['connections']
419 for connection in connections.values():
424 def find_connection(connections_json, version, mode, ip, port, state):
425 """Test if Connection with specified values is contained in JSON
427 :param connections_json: JSON containing Connections
428 :type connections_json: str
429 :param version: Version of SXP protocol (version1/2/3/4)
431 :param mode: Mode of SXP peer (speaker/listener/both)
433 :param ip: Ipv4/6 address of remote peer
435 :param port: Port on with remote peer listens
437 :param state: State of connection (on/off/pendingOn/deleteHoldDown)
439 :returns: True if Connection with specified params was found, otherwise False.
442 for connection in parse_connections(connections_json):
443 if (connection['peer-address'] == ip and connection['tcp-port'] == int(port) and (
444 mode.strip() == 'any' or connection['mode'] == mode) and connection['version'] == version):
447 elif connection['state'] == state:
452 def parse_bindings(bindings_json):
453 """Parse JSON string into Array of Bindings
455 :param bindings_json: JSON containing Bindings
456 :type bindings_json: str
457 :returns: Array containing Bindings.
460 data = json.loads(bindings_json)
462 for bindings_json in data['output'].values():
463 for binding in bindings_json:
464 output.append(binding)
468 def find_binding(bindings, sgt, prefix):
469 """Test if Binding with specified values is contained in JSON
471 :param bindings: JSON containing Bindings
473 :param sgt: Source Group Tag
475 :param prefix: Ipv4/6 prefix
477 :returns: True if Binding with specified params was found, otherwise False.
480 for binding in parse_bindings(bindings):
481 if binding['sgt'] == int(sgt):
482 for ip_prefix in binding['ip-prefix']:
483 if ip_prefix == prefix:
488 def parse_prefix_groups(prefix_groups_json, source_):
489 """Parse JSON string into Array of PrefixGroups
491 :param prefix_groups_json: JSON containing PrefixGroups
492 :type prefix_groups_json: str
493 :param source_: Source of PrefixGroups (sxp/local)
495 :returns: Array containing PrefixGroups.
498 data = json.loads(prefix_groups_json)
499 bindings = data['sxp-node:master-database']
501 for binding in bindings.values():
502 for binding_source in binding:
503 if source_ == "any" or binding_source['binding-source'] == source_:
504 for prefix_group in binding_source['prefix-group']:
505 output.append(prefix_group)
509 def find_binding_legacy(prefix_groups_json, sgt, prefix, source_, action):
510 """Test if Binding with specified values is contained in JSON
512 :param prefix_groups_json: JSON containing Bindings and PrefixGroups
513 :type prefix_groups_json: str
514 :param sgt: Source Group Tag
516 :param prefix: Ipv4/6 prefix
518 :param source_: Source of binding (local/sxp)
520 :param action: Action for binding (add/delete)
522 :returns: True if Binding with specified params was found, otherwise False.
526 for prefixgroup in parse_prefix_groups(prefix_groups_json, source_):
527 if prefixgroup['sgt'] == int(sgt):
528 for binding in prefixgroup['binding']:
529 if binding['ip-prefix'] == prefix and binding['action'] == action:
534 def add_connection_xml(version, mode, ip, port, node, password_, domain_name, bindings_timeout=0, security_mode=''):
535 """Generate xml for Add Connection request
537 :param version: Version of SXP protocol (version1/2/3/4)
539 :param mode: Mode of SXP peer (speaker/listener/both)
541 :param ip: Ipv4/6 address of remote peer
543 :param port: Port on with remote peer listens
545 :param node: Ipv4 address of node
547 :param password_: Password type (none/default)
549 :param domain_name: Name of Domain
550 :type domain_name: str
551 :param security_mode: Default/TSL security
552 :type security_mode: str
553 :param bindings_timeout: Specifies DHD and Reconciliation timers
554 :type bindings_timeout: int
555 :returns: String containing xml data for request
558 templ = Template('''<input>
559 <requested-node xmlns="urn:opendaylight:sxp:controller">$node</requested-node>
561 <connections xmlns="urn:opendaylight:sxp:controller">
563 <peer-address>$ip</peer-address>
564 <tcp-port>$port</tcp-port>
565 <password>$password_</password>
567 <version>$version</version>
568 <description>Connection to ISR-G2</description>
571 <hold-time-min-acceptable>45</hold-time-min-acceptable>
572 <keep-alive-time>30</keep-alive-time>
573 <reconciliation-time>$timeout</reconciliation-time>
574 <delete-hold-down-time>$timeout</delete-hold-down-time>
575 <hold-time>90</hold-time>
576 <hold-time-max>180</hold-time-max>
577 <hold-time-min>90</hold-time-min>
583 data = templ.substitute(
584 {'ip': ip, 'port': port, 'mode': mode, 'version': version, 'node': node,
585 'password_': password_, 'domain': get_domain_name(domain_name), 'timeout': bindings_timeout,
586 'security_type': '<security-type>' + security_mode + '</security-type>' if security_mode else ''})
590 def delete_connections_xml(address, port, node, domain_name):
591 """Generate xml for Delete Connection request
593 :param address: Ipv4/6 address of remote peer
595 :param port: Port on with remote peer listens
597 :param node: Ipv4 address of node
599 :param domain_name: Name of Domain
600 :type domain_name: str
601 :returns: String containing xml data for request
604 templ = Template('''<input>
605 <requested-node xmlns="urn:opendaylight:sxp:controller">$node</requested-node>
607 <peer-address xmlns="urn:opendaylight:sxp:controller">$address</peer-address>
608 <tcp-port xmlns="urn:opendaylight:sxp:controller">$port</tcp-port>
610 data = templ.substitute({'address': address, 'port': port, 'node': node, 'domain': get_domain_name(domain_name)})
614 def add_peer_group_xml(name, peers, ip):
615 """Generate xml for Add PeerGroups request
617 :param name: Name of PeerGroup
619 :param peers: XML formatted peers that will be added to group
621 :param ip: Ipv4 address of node
623 :returns: String containing xml data for request
626 templ = Template('''<input>
627 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
628 <sxp-peer-group xmlns="urn:opendaylight:sxp:controller">
629 <name xmlns="urn:opendaylight:sxp:controller">$name</name>
630 <sxp-peers xmlns="urn:opendaylight:sxp:controller">$peers</sxp-peers>
633 data = templ.substitute({'name': name, 'peers': peers, 'ip': ip})
637 def delete_peer_group_xml(name, ip):
638 """Generate xml for Delete PeerGroup request
640 :param name: Name of PeerGroup
642 :param ip: Ipv4 address of node
644 :returns: String containing xml data for request
647 templ = Template('''<input>
648 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
649 <peer-group-name xmlns="urn:opendaylight:sxp:controller">$name</peer-group-name>
651 data = templ.substitute({'name': name, 'ip': ip})
655 def get_peer_groups_from_node_xml(ip):
656 """Generate xml for Get PeerGroups request
658 :param ip: Ipv4 address of node
660 :returns: String containing xml data for request
663 templ = Template('''<input>
664 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
666 data = templ.substitute({'ip': ip})
670 def add_filter_xml(group, filter_type, entries, ip, policy=None):
671 """Generate xml for Add Filter request
673 :param group: Name of group containing filter
675 :param filter_type: Type of filter
676 :type filter_type: str
677 :param entries: XML formatted entries that will be added in filter
679 :param ip: Ipv4 address of node
681 :param policy: Policy of filter update mechanism
683 :returns: String containing xml data for request
687 policy = "<filter-policy>" + policy + "</filter-policy>"
690 templ = Template('''<input>
691 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
692 <peer-group-name xmlns="urn:opendaylight:sxp:controller">$group</peer-group-name>
693 <sxp-filter xmlns="urn:opendaylight:sxp:controller">
695 <filter-type>$filter_type</filter-type>$entries
698 data = templ.substitute(
699 {'group': group, 'filter_type': filter_type, 'ip': ip, 'entries': entries, 'filter_policy': policy})
703 def add_domain_filter_xml(domain, domains, entries, ip, filter_name=None):
704 """Generate xml for Add Domain Filter request
706 :param domain: Name of Domain containing filter
708 :param domains: Domains on which filter will be applied
710 :param entries: XML formatted entries that will be added in filter
712 :param ip: Ipv4 address of node
714 :param filter_name: Name of filter
715 :type filter_name: str
716 :returns: String containing xml data for request
720 filter_name = "<filter-name>" + filter_name + "</filter-name>"
721 templ = Template('''<input>
722 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
723 <domain-name xmlns="urn:opendaylight:sxp:controller">$domain</domain-name>
724 <sxp-domain-filter xmlns="urn:opendaylight:sxp:controller">
726 <domains>$domains</domains>
730 data = templ.substitute(
731 {'domain': domain, 'domains': domains, 'ip': ip, 'entries': entries, 'filter_name': filter_name})
735 def delete_filter_xml(group, filter_type, ip):
736 """Generate xml for Delete Filter request
738 :param group: Name of group containing filter
740 :param filter_type: Type of filter
741 :type filter_type: str
742 :param ip: Ipv4 address of node
744 :returns: String containing xml data for request
747 templ = Template('''<input>
748 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
749 <peer-group-name xmlns="urn:opendaylight:sxp:controller">$group</peer-group-name>
750 <filter-type xmlns="urn:opendaylight:sxp:controller">$filter_type</filter-type>
752 data = templ.substitute(
753 {'group': group, 'filter_type': filter_type, 'ip': ip})
757 def delete_domain_filter_xml(domain, ip, filter_name=None):
758 """Generate xml for Delete Filter request
760 :param domain: Name of Domain containing filter
762 :param ip: Ipv4 address of node
764 :param filter_name: Name of filter
765 :type filter_name: str
766 :returns: String containing xml data for request
770 filter_name = '<filter-name xmlns="urn:opendaylight:sxp:controller">' + filter_name + "</filter-name>"
771 templ = Template('''<input>
772 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
773 <domain-name xmlns="urn:opendaylight:sxp:controller">$domain</domain-name>
776 data = templ.substitute(
777 {'domain': domain, 'ip': ip, 'filter_name': filter_name})
781 def get_connections_from_node_xml(ip, domain_name):
782 """Generate xml for Get Connections request
784 :param ip: Ipv4 address of node
786 :param domain_name: Name of Domain
787 :type domain_name: str
788 :returns: String containing xml data for request
791 templ = Template('''<input>
792 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
795 data = templ.substitute({'ip': ip, 'domain': get_domain_name(domain_name)})
799 def get_bindings_from_node_xml(ip, binding_range, domain_name):
800 """Generate xml for Get Bindings request
802 :param binding_range: All or only Local bindings
803 :type binding_range: str
804 :param ip: Ipv4 address of node
806 :param domain_name: Name of Domain
807 :type domain_name: str
808 :returns: String containing xml data for request
811 templ = Template('''<input>
812 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
813 <bindings-range xmlns="urn:opendaylight:sxp:controller">$range</bindings-range>
816 data = templ.substitute({'ip': ip, 'range': binding_range, 'domain': get_domain_name(domain_name)})
820 def add_node_xml(node_id, port, password, version, node_ip=None, expansion=0, bindings_timeout=0, keystores=None,
822 """Generate xml for Add Node request
824 :param node_id: Ipv4 address formatted node id
826 :param node_ip: Ipv4 address of node
828 :param port: Node port number
830 :param password: TCP-MD5 password
832 :param version: Sxp device version
834 :param expansion: Bindings expansion
836 :param bindings_timeout: Specifies DHD and Reconciliation timers
837 :type bindings_timeout: int
838 :param keystores: SSL keystore and truststore specification
839 :type keystores: dict
840 :returns: String containing xml data for request
848 <location>$keystore</location>
850 <path-type>PATH</path-type>
851 <password>$passwd</password>
854 <location>$truststore</location>
856 <path-type>PATH</path-type>
857 <password>$passwd</password>
859 <certificate-password>$passwd</certificate-password>
862 {'keystore': keystores['keystore'], 'truststore': keystores['truststore'], 'passwd': keystores['password']})
864 templ = Template('''<input xmlns="urn:opendaylight:sxp:controller">
865 <node-id>$id</node-id>
867 <retry-open-time>$retry_open_timer</retry-open-time>
868 <hold-time-min-acceptable>120</hold-time-min-acceptable>
869 <delete-hold-down-time>$timeout</delete-hold-down-time>
870 <hold-time-min>90</hold-time-min>
871 <reconciliation-time>$timeout</reconciliation-time>
872 <hold-time>90</hold-time>
873 <hold-time-max>180</hold-time-max>
874 <keep-alive-time>30</keep-alive-time>
876 <mapping-expanded>$expansion</mapping-expanded>
879 <password>$password</password>
881 <tcp-port>$port</tcp-port>
882 <version>$version</version>
883 <description>ODL SXP Controller</description>
884 <source-ip>$ip</source-ip>
886 data = templ.substitute(
887 {'ip': node_ip or node_id, 'id': node_id, 'port': port, 'password': password,
888 'version': version, 'expansion': expansion, 'timeout': bindings_timeout, 'tls': tls,
889 'retry_open_timer': retry_open_timer})
893 def delete_node_xml(node_id):
894 """Generate xml for Delete node request
896 :param node_id: Ipv4 address formatted node id
898 :returns: String containing xml data for request
901 templ = Template('''<input xmlns="urn:opendaylight:sxp:controller">
902 <node-id>$id</node-id>
904 data = templ.substitute({'id': node_id})
908 def add_domain_xml_fluorine(node_id, name, sgt, prefixes, origin):
909 """Generate xml for Add Domain request (Fluorine version: bindings with origin)
911 :param node_id: Id of node
913 :param name: Name of Domain
915 :param sgt: Security group
917 :param prefixes: List of ip-prefixes
919 :param origin: Origin of added bindings
921 :returns: String containing xml data for request
925 if prefixes != 'None':
927 for prefix in prefixes.split(','):
928 xml_prefixes += '\n' + '<ip-prefix>' + prefix + '</ip-prefix>'
930 master_database += '''<master-database>
935 </master-database>'''
936 master_database = Template(master_database).substitute(({'sgt': sgt, 'xml_prefixes': xml_prefixes}))
938 templ = Template('''<input xmlns="urn:opendaylight:sxp:controller">
939 <node-id>$id</node-id>
940 <domain-name>$name</domain-name>
941 <origin>$origin</origin>
945 data = templ.substitute({'name': name, 'id': node_id, 'origin': origin, 'master_database': master_database})
949 def add_domain_xml_oxygen(node_id, name, sgt, prefixes):
950 """Generate xml for Add Domain request (Oxygen version: bindings without origin)
952 :param node_id: Id of node
954 :param name: Name of Domain
956 :param sgt: Security group
958 :param prefixes: List of ip-prefixes
960 :returns: String containing xml data for request
964 if prefixes != 'None':
966 for prefix in prefixes.split(','):
967 xml_prefixes += '\n' + '<ip-prefix>' + prefix + '</ip-prefix>'
969 master_database += '''<master-database>
974 </master-database>'''
975 master_database = Template(master_database).substitute(({'sgt': sgt, 'xml_prefixes': xml_prefixes}))
977 templ = Template('''<input xmlns="urn:opendaylight:sxp:controller">
978 <node-id>$id</node-id>
979 <domain-name>$name</domain-name>
983 data = templ.substitute({'name': name, 'id': node_id, 'master_database': master_database})
987 def delete_domain_xml(node_id, name):
988 """Generate xml for Remove Domain request
990 :param node_id: Id of node
992 :param name: Name of Domain
994 :returns: String containing xml data for request
997 return add_domain_xml(node_id, name)
1000 def get_domain_name(domain_name):
1001 """Generate xml for Get Bindings request
1003 :param domain_name: Name of Domain
1004 :type domain_name: str
1005 :returns: String containing xml data for request
1008 if domain_name == 'global':
1011 return '<domain-name xmlns="urn:opendaylight:sxp:controller">' + domain_name + '</domain-name>'
1014 def add_bindings_xml_fluorine(node_id, domain, sgt, prefixes, origin):
1015 """Generate xml for Add Bindings request (Fluorine version with origin type)
1017 :param node_id: Id of node
1019 :param domain: Name of Domain
1021 :param sgt: Security group
1023 :param prefixes: List of ip-prefixes
1025 :param origin: Origin of added bindings
1027 :returns: String containing xml data for request
1031 for prefix in prefixes.split(','):
1032 xml_prefixes += '\n' + '<ip-prefix>' + prefix + '</ip-prefix>'
1033 templ = Template('''<input xmlns="urn:opendaylight:sxp:controller">
1034 <node-id>$id</node-id>
1035 <domain-name>$name</domain-name>
1036 <origin>$origin</origin>
1044 data = templ.substitute({'name': domain, 'id': node_id, 'sgt': sgt, 'xml_prefixes': xml_prefixes, 'origin': origin})
1048 def add_bindings_xml_oxygen(node_id, domain, sgt, prefixes):
1049 """Generate xml for Add Bindings request (Oxygen version without origin type)
1051 :param node_id: Id of node
1053 :param domain: Name of Domain
1055 :param sgt: Security group
1057 :param prefixes: List of ip-prefixes
1059 :returns: String containing xml data for request
1063 for prefix in prefixes.split(','):
1064 xml_prefixes += '\n' + '<ip-prefix>' + prefix + '</ip-prefix>'
1065 templ = Template('''<input xmlns="urn:opendaylight:sxp:controller">
1066 <node-id>$id</node-id>
1067 <domain-name>$name</domain-name>
1073 data = templ.substitute({'name': domain, 'id': node_id, 'sgt': sgt, 'xml_prefixes': xml_prefixes})
1077 def delete_bindings_xml(node_id, domain, sgt, prefixes):
1078 """Generate xml for Remove Bindings request
1080 :param node_id: Id of node
1082 :param domain: Name of Domain
1084 :param sgt: Security group
1086 :param prefixes: Comma separated list of ip-prefixes
1088 :returns: String containing xml data for request
1092 for prefix in prefixes.split(','):
1093 xml_prefixes += '\n' + '<ip-prefix>' + prefix + '</ip-prefix>'
1094 templ = Template('''<input xmlns="urn:opendaylight:sxp:controller">
1095 <node-id>$id</node-id>
1096 <domain-name>$name</domain-name>
1102 data = templ.substitute({'name': domain, 'id': node_id, 'sgt': sgt, 'xml_prefixes': xml_prefixes})
1106 def prefix_range(start, end):
1107 """Generate and concatenate ip-prefixes
1109 :param start: Start index
1111 :param end: End index
1113 :returns: String containing concatenated ip-prefixes
1121 prefixes += get_ip_from_number(index + start) + '/32'
1128 def route_definition_xml(virtual_ip, net_mask, interface):
1129 """Generate xml for Add Bindings request
1131 :param interface: Network interface name
1132 :type interface: str
1133 :param net_mask: NetMask of virtual ip
1135 :param virtual_ip: Virtual ip
1136 :type virtual_ip: str
1137 :returns: String containing xml data for request
1140 templ = Template('''
1141 <routing-definition>
1142 <ip-address>$vip</ip-address>
1143 <interface>$interface</interface>
1144 <netmask>$mask</netmask>
1145 </routing-definition>
1147 data = templ.substitute({'mask': net_mask, 'vip': virtual_ip, 'interface': interface})
1151 def route_definitions_xml(routes, old_routes=None):
1152 """Generate xml for Add Bindings request
1154 :param routes: XML formatted data containing RouteDefinitions
1156 :param old_routes: Routes add to request that needs to persist
1157 :type old_routes: str
1158 :returns: String containing xml data for request
1161 if old_routes and "</sxp-cluster-route>" in old_routes:
1162 templ = Template(old_routes.replace("</sxp-cluster-route>", "$routes</sxp-cluster-route>"))
1164 templ = Template('''<sxp-cluster-route xmlns="urn:opendaylight:sxp:cluster:route">
1166 </sxp-cluster-route>
1168 data = templ.substitute({'routes': routes})
1172 def add_binding_origin_xml(origin, priority):
1173 """Generate xml for Add Binding Origin request
1175 :param origin: Origin type
1177 :param priority: Origin priority
1179 :returns: String containing xml data for request
1182 templ = Template('''<input xmlns="urn:opendaylight:sxp:config:controller">
1183 <origin>$origin</origin>
1184 <priority>$priority</priority>
1186 data = templ.substitute({'origin': origin, 'priority': priority})
1190 def update_binding_origin_xml(origin, priority):
1191 """Generate xml for Update Binding Origin request
1193 :param origin: Origin type
1195 :param priority: Origin priority
1197 :returns: String containing xml data for request
1200 templ = Template('''<input xmlns="urn:opendaylight:sxp:config:controller">
1201 <origin>$origin</origin>
1202 <priority>$priority</priority>
1204 data = templ.substitute({'origin': origin, 'priority': priority})
1208 def delete_binding_origin_xml(origin):
1209 """Generate xml for Delete Binding Origin request
1211 :param origin: Origin type
1213 :returns: String containing xml data for request
1216 templ = Template('''<input xmlns="urn:opendaylight:sxp:config:controller">
1217 <origin>$origin</origin>
1219 data = templ.substitute({'origin': origin})
1223 def find_binding_origin(origins_json, origin):
1224 """Test if Binding origin of specified value is contained in JSON
1226 :param origins_json: JSON containing Binding origins
1227 :type origins_json: str
1228 :param origin: Origin to be found
1230 :returns: True if Binding origin of specified origin type was found, otherwise False.
1233 for json_origin in parse_binding_origins(origins_json):
1234 if json_origin['origin'] == origin:
1239 def find_binding_origin_with_priority(origins_json, origin, priority):
1240 """Test if Binding origin of specified value and priority is contained in JSON
1242 :param origins_json: JSON containing Binding origins
1243 :type origins_json: str
1244 :param origin: Origin to be found
1246 :param priority: desired priority of origin
1248 :returns: True if Binding origin of specified origin type with desired priority was found, otherwise False.
1251 for json_origin in parse_binding_origins(origins_json):
1252 if json_origin['origin'] == origin:
1253 if json_origin['priority'] == int(priority):
1258 def parse_binding_origins(origins_json):
1259 """Parse JSON string into Array of Binding origins
1261 :param origins_json: JSON containing Binding origins
1262 :type origins_json: str
1263 :returns: Array containing Binding origins.
1267 for origins in origins_json['binding-origins'].values():
1268 for origin in origins:
1269 output.append(origin)