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_entry_xml(sgt, prefix, ip, domain_name):
535 """Generate xml for Add Bindings request
537 :param sgt: Source Group Tag
539 :param prefix: Ipv4/6 prefix
541 :param ip: Ipv4 address of node
543 :param domain_name: Name of Domain
544 :type domain_name: str
545 :returns: String containing xml data for request
548 templ = Template('''<input>
549 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
551 <sgt xmlns="urn:opendaylight:sxp:controller">$sgt</sgt>
552 <ip-prefix xmlns="urn:opendaylight:sxp:controller">$prefix</ip-prefix>
554 data = templ.substitute({'sgt': sgt, 'prefix': prefix, 'ip': ip, 'domain': get_domain_name(domain_name)})
558 def add_connection_xml(version, mode, ip, port, node, password_, domain_name, bindings_timeout=0, security_mode=''):
559 """Generate xml for Add Connection request
561 :param version: Version of SXP protocol (version1/2/3/4)
563 :param mode: Mode of SXP peer (speaker/listener/both)
565 :param ip: Ipv4/6 address of remote peer
567 :param port: Port on with remote peer listens
569 :param node: Ipv4 address of node
571 :param password_: Password type (none/default)
573 :param domain_name: Name of Domain
574 :type domain_name: str
575 :param security_mode: Default/TSL security
576 :type security_mode: str
577 :param bindings_timeout: Specifies DHD and Reconciliation timers
578 :type bindings_timeout: int
579 :returns: String containing xml data for request
582 templ = Template('''<input>
583 <requested-node xmlns="urn:opendaylight:sxp:controller">$node</requested-node>
585 <connections xmlns="urn:opendaylight:sxp:controller">
587 <peer-address>$ip</peer-address>
588 <tcp-port>$port</tcp-port>
589 <password>$password_</password>
591 <version>$version</version>
592 <description>Connection to ISR-G2</description>
595 <hold-time-min-acceptable>45</hold-time-min-acceptable>
596 <keep-alive-time>30</keep-alive-time>
597 <reconciliation-time>$timeout</reconciliation-time>
598 <delete-hold-down-time>$timeout</delete-hold-down-time>
604 data = templ.substitute(
605 {'ip': ip, 'port': port, 'mode': mode, 'version': version, 'node': node,
606 'password_': password_, 'domain': get_domain_name(domain_name), 'timeout': bindings_timeout,
607 'security_type': '<security-type>' + security_mode + '</security-type>' if security_mode else ''})
611 def delete_connections_xml(address, port, node, domain_name):
612 """Generate xml for Delete Connection request
614 :param address: Ipv4/6 address of remote peer
616 :param port: Port on with remote peer listens
618 :param node: Ipv4 address of node
620 :param domain_name: Name of Domain
621 :type domain_name: str
622 :returns: String containing xml data for request
625 templ = Template('''<input>
626 <requested-node xmlns="urn:opendaylight:sxp:controller">$node</requested-node>
628 <peer-address xmlns="urn:opendaylight:sxp:controller">$address</peer-address>
629 <tcp-port xmlns="urn:opendaylight:sxp:controller">$port</tcp-port>
631 data = templ.substitute({'address': address, 'port': port, 'node': node, 'domain': get_domain_name(domain_name)})
635 def update_binding_xml(sgt0, prefix0, sgt1, prefix1, ip, domain_name):
636 """Generate xml for Update Binding request
638 :param sgt0: Original Source Group Tag
640 :param prefix0: Original Ipv4/6 prefix
642 :param sgt1: New Source Group Tag
644 :param prefix1: New Ipv4/6 prefix
646 :param ip: Ipv4 address of node
648 :param domain_name: Name of Domain
649 :type domain_name: str
650 :returns: String containing xml data for request
653 templ = Template('''<input>
654 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
656 <original-binding xmlns="urn:opendaylight:sxp:controller">
658 <ip-prefix>$prefix0</ip-prefix>
660 <new-binding xmlns="urn:opendaylight:sxp:controller">
662 <ip-prefix>$prefix1</ip-prefix>
665 data = templ.substitute(
666 {'sgt0': sgt0, 'sgt1': sgt1, 'prefix0': prefix0, 'prefix1': prefix1, 'ip': ip,
667 'domain': get_domain_name(domain_name)})
671 def delete_binding_xml(sgt, prefix, ip, domain_name):
672 """Generate xml for Delete Binding request
674 :param sgt: Source Group Tag
676 :param prefix: Ipv4/6 prefix
678 :param ip: Ipv4 address of node
680 :param domain_name: Name of Domain
681 :type domain_name: str
682 :returns: String containing xml data for request
685 templ = Template('''<input>
686 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
687 <sgt xmlns="urn:opendaylight:sxp:controller">$sgt</sgt>
688 <ip-prefix xmlns="urn:opendaylight:sxp:controller">$prefix</ip-prefix>
691 data = templ.substitute({'sgt': sgt, 'prefix': prefix, 'ip': ip, 'domain': get_domain_name(domain_name)})
695 def add_peer_group_xml(name, peers, ip):
696 """Generate xml for Add PeerGroups request
698 :param name: Name of PeerGroup
700 :param peers: XML formatted peers that will be added to group
702 :param ip: Ipv4 address of node
704 :returns: String containing xml data for request
707 templ = Template('''<input>
708 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
709 <sxp-peer-group xmlns="urn:opendaylight:sxp:controller">
710 <name xmlns="urn:opendaylight:sxp:controller">$name</name>
711 <sxp-peers xmlns="urn:opendaylight:sxp:controller">$peers</sxp-peers>
714 data = templ.substitute({'name': name, 'peers': peers, 'ip': ip})
718 def delete_peer_group_xml(name, ip):
719 """Generate xml for Delete PeerGroup request
721 :param name: Name of PeerGroup
723 :param ip: Ipv4 address of node
725 :returns: String containing xml data for request
728 templ = Template('''<input>
729 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
730 <peer-group-name xmlns="urn:opendaylight:sxp:controller">$name</peer-group-name>
732 data = templ.substitute({'name': name, 'ip': ip})
736 def get_peer_groups_from_node_xml(ip):
737 """Generate xml for Get PeerGroups request
739 :param ip: Ipv4 address of node
741 :returns: String containing xml data for request
744 templ = Template('''<input>
745 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
747 data = templ.substitute({'ip': ip})
751 def add_filter_xml(group, filter_type, entries, ip, policy=None):
752 """Generate xml for Add Filter request
754 :param group: Name of group containing filter
756 :param filter_type: Type of filter
757 :type filter_type: str
758 :param entries: XML formatted entries that will be added in filter
760 :param ip: Ipv4 address of node
762 :param policy: Policy of filter update mechanism
764 :returns: String containing xml data for request
768 policy = "<filter-policy>" + policy + "</filter-policy>"
771 templ = Template('''<input>
772 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
773 <peer-group-name xmlns="urn:opendaylight:sxp:controller">$group</peer-group-name>
774 <sxp-filter xmlns="urn:opendaylight:sxp:controller">
776 <filter-type>$filter_type</filter-type>$entries
779 data = templ.substitute(
780 {'group': group, 'filter_type': filter_type, 'ip': ip, 'entries': entries, 'filter_policy': policy})
784 def add_domain_filter_xml(domain, domains, entries, ip, filter_name=None):
785 """Generate xml for Add Domain Filter request
787 :param domain: Name of Domain containing filter
789 :param domains: Domains on which filter will be applied
791 :param entries: XML formatted entries that will be added in filter
793 :param ip: Ipv4 address of node
795 :param filter_name: Name of filter
796 :type filter_name: str
797 :returns: String containing xml data for request
801 filter_name = "<filter-name>" + filter_name + "</filter-name>"
802 templ = Template('''<input>
803 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
804 <domain-name xmlns="urn:opendaylight:sxp:controller">$domain</domain-name>
805 <sxp-domain-filter xmlns="urn:opendaylight:sxp:controller">
807 <domains>$domains</domains>
811 data = templ.substitute(
812 {'domain': domain, 'domains': domains, 'ip': ip, 'entries': entries, 'filter_name': filter_name})
816 def delete_filter_xml(group, filter_type, ip):
817 """Generate xml for Delete Filter request
819 :param group: Name of group containing filter
821 :param filter_type: Type of filter
822 :type filter_type: str
823 :param ip: Ipv4 address of node
825 :returns: String containing xml data for request
828 templ = Template('''<input>
829 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
830 <peer-group-name xmlns="urn:opendaylight:sxp:controller">$group</peer-group-name>
831 <filter-type xmlns="urn:opendaylight:sxp:controller">$filter_type</filter-type>
833 data = templ.substitute(
834 {'group': group, 'filter_type': filter_type, 'ip': ip})
838 def delete_domain_filter_xml(domain, ip, filter_name=None):
839 """Generate xml for Delete Filter request
841 :param domain: Name of Domain containing filter
843 :param ip: Ipv4 address of node
845 :param filter_name: Name of filter
846 :type filter_name: str
847 :returns: String containing xml data for request
851 filter_name = '<filter-name xmlns="urn:opendaylight:sxp:controller">' + filter_name + "</filter-name>"
852 templ = Template('''<input>
853 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
854 <domain-name xmlns="urn:opendaylight:sxp:controller">$domain</domain-name>
857 data = templ.substitute(
858 {'domain': domain, 'ip': ip, 'filter_name': filter_name})
862 def get_connections_from_node_xml(ip, domain_name):
863 """Generate xml for Get Connections request
865 :param ip: Ipv4 address of node
867 :param domain_name: Name of Domain
868 :type domain_name: str
869 :returns: String containing xml data for request
872 templ = Template('''<input>
873 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
876 data = templ.substitute({'ip': ip, 'domain': get_domain_name(domain_name)})
880 def get_bindings_from_node_xml(ip, binding_range, domain_name):
881 """Generate xml for Get Bindings request
883 :param binding_range: All or only Local bindings
884 :type binding_range: str
885 :param ip: Ipv4 address of node
887 :param domain_name: Name of Domain
888 :type domain_name: str
889 :returns: String containing xml data for request
892 templ = Template('''<input>
893 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
894 <bindings-range xmlns="urn:opendaylight:sxp:controller">$range</bindings-range>
897 data = templ.substitute({'ip': ip, 'range': binding_range, 'domain': get_domain_name(domain_name)})
901 def add_node_xml(node_id, port, password, version, node_ip=None, expansion=0, bindings_timeout=0, keystores=None,
903 """Generate xml for Add Node request
905 :param node_id: Ipv4 address formatted node id
907 :param node_ip: Ipv4 address of node
909 :param port: Node port number
911 :param password: TCP-MD5 password
913 :param version: Sxp device version
915 :param expansion: Bindings expansion
917 :param bindings_timeout: Specifies DHD and Reconciliation timers
918 :type bindings_timeout: int
919 :param keystores: SSL keystore and truststore specification
920 :type keystores: dict
921 :returns: String containing xml data for request
929 <location>$keystore</location>
931 <path-type>PATH</path-type>
932 <password>$passwd</password>
935 <location>$truststore</location>
937 <path-type>PATH</path-type>
938 <password>$passwd</password>
940 <certificate-password>$passwd</certificate-password>
943 {'keystore': keystores['keystore'], 'truststore': keystores['truststore'], 'passwd': keystores['password']})
945 templ = Template('''<input xmlns="urn:opendaylight:sxp:controller">
946 <node-id>$id</node-id>
948 <retry-open-time>$retry_open_timer</retry-open-time>
949 <hold-time-min-acceptable>120</hold-time-min-acceptable>
950 <delete-hold-down-time>$timeout</delete-hold-down-time>
951 <hold-time-min>90</hold-time-min>
952 <reconciliation-time>$timeout</reconciliation-time>
953 <hold-time>90</hold-time>
954 <hold-time-max>180</hold-time-max>
955 <keep-alive-time>30</keep-alive-time>
957 <mapping-expanded>$expansion</mapping-expanded>
960 <password>$password</password>
962 <tcp-port>$port</tcp-port>
963 <version>$version</version>
964 <description>ODL SXP Controller</description>
965 <source-ip>$ip</source-ip>
966 <master-database></master-database>
968 data = templ.substitute(
969 {'ip': node_ip or node_id, 'id': node_id, 'port': port, 'password': password,
970 'version': version, 'expansion': expansion, 'timeout': bindings_timeout, 'tls': tls,
971 'retry_open_timer': retry_open_timer})
975 def delete_node_xml(node_id):
976 """Generate xml for Delete node request
978 :param node_id: Ipv4 address formatted node id
980 :returns: String containing xml data for request
983 templ = Template('''<input xmlns="urn:opendaylight:sxp:controller">
984 <node-id>$id</node-id>
986 data = templ.substitute({'id': node_id})
990 def add_domain_xml(node_id, name):
991 """Generate xml for Add Domain request
993 :param node_id: Id of node
995 :param name: Name of Domain
997 :returns: String containing xml data for request
1000 templ = Template('''<input>
1001 <node-id xmlns="urn:opendaylight:sxp:controller">$id</node-id>
1002 <domain-name xmlns="urn:opendaylight:sxp:controller">$name</domain-name>
1004 data = templ.substitute({'name': name, 'id': node_id})
1008 def delete_domain_xml(node_id, name):
1009 """Generate xml for Remove Domain request
1011 :param node_id: Id of node
1013 :param name: Name of Domain
1015 :returns: String containing xml data for request
1018 return add_domain_xml(node_id, name)
1021 def get_domain_name(domain_name):
1022 """Generate xml for Get Bindings request
1024 :param domain_name: Name of Domain
1025 :type domain_name: str
1026 :returns: String containing xml data for request
1029 if domain_name == 'global':
1032 return '<domain-name xmlns="urn:opendaylight:sxp:controller">' + domain_name + '</domain-name>'
1035 def add_bindings_xml(node_id, domain, sgt, prefixes):
1036 """Generate xml for Add Bindings request
1038 :param node_id: Id of node
1040 :param domain: Name of Domain
1042 :param sgt: Security group
1044 :param prefixes: List of ip-prefixes
1046 :returns: String containing xml data for request
1050 for prefix in prefixes.split(','):
1051 bindings += '\n' + '<ip-prefix>' + prefix + '</ip-prefix>'
1052 templ = Template('''<input>
1053 <node-id xmlns="urn:opendaylight:sxp:controller">$id</node-id>
1054 <domain-name xmlns="urn:opendaylight:sxp:controller">$name</domain-name>
1055 <binding xmlns="urn:opendaylight:sxp:controller">
1060 data = templ.substitute({'name': domain, 'id': node_id, 'sgt': sgt, 'bindings': bindings})
1064 def delete_bindings_xml(node_id, domain, sgt, prefixes):
1065 """Generate xml for Remove Bindings request
1067 :param node_id: Id of node
1069 :param domain: Name of Domain
1071 :param sgt: Security group
1073 :param prefixes: List of ip-prefixes
1075 :returns: String containing xml data for request
1078 return add_bindings_xml(node_id, domain, sgt, prefixes)
1081 def prefix_range(start, end):
1082 """Generate and concatenate ip-prefixes
1084 :param start: Start index
1086 :param end: End index
1088 :returns: String containing concatenated ip-prefixes
1096 prefixes += get_ip_from_number(index + start) + '/32'
1103 def route_definition_xml(virtual_ip, net_mask, interface):
1104 """Generate xml for Add Bindings request
1106 :param interface: Network interface name
1107 :type interface: str
1108 :param net_mask: NetMask of virtual ip
1110 :param virtual_ip: Virtual ip
1111 :type virtual_ip: str
1112 :returns: String containing xml data for request
1115 templ = Template('''
1116 <routing-definition>
1117 <ip-address>$vip</ip-address>
1118 <interface>$interface</interface>
1119 <netmask>$mask</netmask>
1120 </routing-definition>
1122 data = templ.substitute({'mask': net_mask, 'vip': virtual_ip, 'interface': interface})
1126 def route_definitions_xml(routes, old_routes=None):
1127 """Generate xml for Add Bindings request
1129 :param routes: XML formatted data containing RouteDefinitions
1131 :param old_routes: Routes add to request that needs to persist
1132 :type old_routes: str
1133 :returns: String containing xml data for request
1136 if old_routes and "</sxp-cluster-route>" in old_routes:
1137 templ = Template(old_routes.replace("</sxp-cluster-route>", "$routes</sxp-cluster-route>"))
1139 templ = Template('''<sxp-cluster-route xmlns="urn:opendaylight:sxp:cluster:route">
1141 </sxp-cluster-route>
1143 data = templ.substitute({'routes': routes})