2 from ipaddr import IPAddress
3 from string import Template
7 """Gets modulo of number
9 :param num: Number to be used
11 :param base: Base used
13 :returns: Int representing modulo of specified numbers.
16 return int(num) % int(base)
19 def get_ip_from_number(n):
20 """Generate string representing Ipv4 from specified number that is added number 2130706432
22 :param n: Number to be converted
24 :returns: String containing Ipv4.
27 ip = IPAddress(2130706432 + n)
31 def lower_version(ver1, ver2):
32 """Generate xml containing SGT mach data
34 :param ver1: Version of SXP protocol for compare
36 :param ver2: Version of SXP protocol for compare
38 :returns: String containing lower from those two specified versions.
49 def get_filter_entry(seq, entry_type, sgt="", esgt="", acl="", eacl="", pl="", epl=""):
50 """Generate xml containing FilterEntry data
52 :param seq: Sequence of entry
54 :param entry_type: Type of entry (permit/deny)
55 :type entry_type: string
56 :param sgt: SGT matches to be added to entry
58 :param esgt: SGT ranges match to be added to entry
60 :param acl: ACL matches to be added to entry
62 :param eacl: EACL matches to be added to entry
64 :param pl: PrefixList matches to be added to entry
66 :param epl: ExtendedPrefixList matches to be added to entry
68 :returns: String containing xml data for request
72 # Generate XML request containing combination of Matches of different types
75 entries += add_sgt_matches_xml(args)
77 args = esgt.split(',')
78 entries += add_sgt_range_xml(args[0], args[1])
80 entries += add_pl_entry_xml(pl)
83 entries += add_epl_entry_xml(args[0], args[1], args[2])
86 entries += add_acl_entry_xml(args[0], args[1])
88 args = eacl.split(',')
89 entries += add_eacl_entry_xml(args[0], args[1], args[2], args[3])
90 # Wrap entries in ACL/PrefixList according to specified values
92 return add_pl_entry_default_xml(seq, entry_type, entries)
93 return add_acl_entry_default_xml(seq, entry_type, entries)
97 """Generate xml containing Peer mach data
99 :param args: Peers data
101 :returns: String containing xml data for request
106 <peer-address>$ip</peer-address>
109 for count, value in enumerate(args):
110 peers += templ.substitute({'ip': value})
114 def add_sgt_matches_xml(sgt_entries):
115 """Generate xml containing SGT mach data
117 :param sgt_entries: SGT matches
118 :type sgt_entries: string
119 :returns: String containing xml data for request
123 <matches>$sgt</matches>''')
125 for sgt in sgt_entries:
126 matches += templ.substitute({'sgt': sgt})
130 def add_sgt_range_xml(start, end):
131 """Generate xml containing SGT RangeMach data
133 :param start: Start range of SGT
135 :param end: End range of SGT
137 :returns: String containing xml data for request
141 <sgt-start>$start</sgt-start>
142 <sgt-end>$end</sgt-end>''')
143 match = templ.substitute({'start': start, 'end': end})
147 def add_acl_entry_default_xml(seq, entry_type, acl_entries):
148 """Generate xml containing AccessList data
150 :param seq: Sequence of PrefixList entry
152 :param entry_type: Entry type (permit/deny)
153 :type entry_type: string
154 :param acl_entries: XML data containing AccessList entries
155 :type acl_entries: string
156 :returns: String containing xml data for request
161 <entry-type>$entry_type</entry-type>
162 <entry-seq>$seq</entry-seq>$acl_entries
164 matches = templ.substitute(
165 {'seq': seq, 'entry_type': entry_type, 'acl_entries': acl_entries})
169 def add_acl_entry_xml(ip, mask):
170 """Generate xml containing AccessList data
172 :param ip: Ipv4/6 address
174 :param mask: Ipv4/6 wildcard mask
176 :returns: String containing xml data for request
181 <ip-address>$ip</ip-address>
182 <wildcard-mask>$mask</wildcard-mask>
184 return templ.substitute({'ip': ip, 'mask': mask})
187 def add_eacl_entry_xml(ip, mask, amask, wmask):
188 """Generate xml containing ExtendedAccessList data
190 :param ip: Ipv4/6 address
192 :param mask: Ipv4/6 wildcard mask
194 :param amask: Ipv4/6 address mask
196 :param wmask: Ipv4/6 address wildcard mask
198 :returns: String containing xml data for request
203 <ip-address>$ip</ip-address>
204 <wildcard-mask>$mask</wildcard-mask>
206 <address-mask>$amask</address-mask>
207 <wildcard-mask>$wmask</wildcard-mask>
210 return templ.substitute({'ip': ip, 'mask': mask, 'amask': amask, 'wmask': wmask})
213 def add_pl_entry_default_xml(seq, entry_type, pl_entries):
214 """Generate xml containing PrefixList data
216 :param seq: Sequence of PrefixList entry
218 :param entry_type: Entry type (permit/deny)
219 :type entry_type: string
220 :param pl_entries: XML data containing PrefixList entries
221 :type pl_entries: string
222 :returns: String containing xml data for request
226 <prefix-list-entry xmlns="urn:opendaylight:sxp:controller">
227 <entry-type>$entry_type</entry-type>
228 <entry-seq>$seq</entry-seq>$pl_entries
229 </prefix-list-entry>''')
230 return templ.substitute({'seq': seq, 'entry_type': entry_type, 'pl_entries': pl_entries})
233 def add_pl_entry_xml(prefix):
234 """Generate xml containing PrefixList data
236 :param prefix: Ipv4/6 prefix
238 :returns: String containing xml data for request
243 <ip-prefix>$prefix</ip-prefix>
244 </prefix-list-match>''')
245 return templ.substitute({'prefix': prefix})
248 def add_epl_entry_xml(prefix, op, mask):
249 """Generate xml containing Extended PrefixList data
251 :param prefix: Ipv4/6 prefix
253 :param op: PrefixList option (ge/le/eq)
255 :param mask: Ipv4/6 Mask
257 :returns: String containing xml data for request
262 <ip-prefix>$prefix</ip-prefix>
264 <mask-range>$op</mask-range>
265 <mask-value>$mask</mask-value>
267 </prefix-list-match>''')
268 return templ.substitute({'prefix': prefix, 'mask': mask, 'op': op})
271 def parse_peer_groups(groups_json):
272 """Parse JSON string into Array of PeerGroups
274 :param groups_json: JSON containing PeerGroups
275 :type groups_json: string
276 :returns: Array containing PeerGroups.
279 data = json.loads(groups_json)
280 groups = data['output']
282 for group in groups.values():
287 def parse_connections(connections_json):
288 """Parse JSON string into Array of Connections
290 :param connections_json: JSON containing Connections
291 :type connections_json: string
292 :returns: Array containing Connections.
295 data = json.loads(connections_json)
296 connections = data['output']['connections']
298 for connection in connections.values():
303 def find_connection(connections_json, version, mode, ip, port, state):
304 """Test if Connection with specified values is contained in JSON
306 :param connections_json: JSON containing Connections
307 :type connections_json: string
308 :param version: Version of SXP protocol (version1/2/3/4)
309 :type version: string
310 :param mode: Mode of SXP peer (speaker/listener/both)
312 :param ip: Ipv4/6 address of remote peer
314 :param port: Port on with remote peer listens
316 :param state: State of connection (on/off/pendingOn/deleteHoldDown)
318 :returns: True if Connection with specified params was found, otherwise False.
321 for connection in parse_connections(connections_json):
322 if (connection['peer-address'] == ip and connection['tcp-port'] == int(port) and connection['mode'] == mode and
323 connection['version'] == version):
326 elif connection['state'] == state:
331 def parse_bindings(bindings_json):
332 """Parse JSON string into Array of Bindings
334 :param bindings_json: JSON containing Bindings
335 :type bindings_json: string
336 :returns: Array containing Bindings.
339 data = json.loads(bindings_json)
341 for bindings_json in data['output'].values():
342 for binding in bindings_json:
343 output.append(binding)
347 def find_binding(bindings, sgt, prefix):
348 """Test if Binding with specified values is contained in JSON
350 :param bindings: JSON containing Bindings
351 :type bindings: string
352 :param sgt: Source Group Tag
354 :param prefix: Ipv4/6 prefix
356 :returns: True if Binding with specified params was found, otherwise False.
359 for binding in parse_bindings(bindings):
360 if binding['sgt'] == int(sgt):
361 for ip_prefix in binding['ip-prefix']:
362 if ip_prefix == prefix:
367 def parse_prefix_groups(prefix_groups_json, source_):
368 """Parse JSON string into Array of PrefixGroups
370 :param prefix_groups_json: JSON containing PrefixGroups
371 :type prefix_groups_json: string
372 :param source_: Source of PrefixGroups (sxp/local)
373 :type source_: string
374 :returns: Array containing PrefixGroups.
377 data = json.loads(prefix_groups_json)
378 bindings = data['sxp-node:master-database']
380 for binding in bindings.values():
381 for binding_source in binding:
382 if source_ == "any" or binding_source['binding-source'] == source_:
383 for prefix_group in binding_source['prefix-group']:
384 output.append(prefix_group)
388 def find_binding_legacy(prefix_groups_json, sgt, prefix, source_, action):
389 """Test if Binding with specified values is contained in JSON
391 :param prefix_groups_json: JSON containing Bindings and PrefixGroups
392 :type prefix_groups_json: string
393 :param sgt: Source Group Tag
395 :param prefix: Ipv4/6 prefix
397 :param source_: Source of binding (local/sxp)
398 :type source_: string
399 :param action: Action for binding (add/delete)
401 :returns: True if Binding with specified params was found, otherwise False.
405 for prefixgroup in parse_prefix_groups(prefix_groups_json, source_):
406 if prefixgroup['sgt'] == int(sgt):
407 for binding in prefixgroup['binding']:
408 if binding['ip-prefix'] == prefix and binding['action'] == action:
413 def add_entry_xml(sgt, prefix, ip):
414 """Generate xml for Add Bindings request
416 :param sgt: Source Group Tag
418 :param prefix: Ipv4/6 prefix
420 :param ip: Ipv4 address of node
422 :returns: String containing xml data for request
425 templ = Template('''<input>
426 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
427 <sgt xmlns="urn:opendaylight:sxp:controller">$sgt</sgt>
428 <ip-prefix xmlns="urn:opendaylight:sxp:controller">$prefix</ip-prefix>
430 data = templ.substitute({'sgt': sgt, 'prefix': prefix, 'ip': ip})
434 def add_connection_xml(version, mode, ip, port, node, password_):
435 """Generate xml for Add Connection request
437 :param version: Version of SXP protocol (version1/2/3/4)
438 :type version: string
439 :param mode: Mode of SXP peer (speaker/listener/both)
441 :param ip: Ipv4/6 address of remote peer
443 :param port: Port on with remote peer listens
445 :param node: Ipv4 address of node
447 :param password_: Password type (none/default)
448 :type password_: string
449 :returns: String containing xml data for request
452 templ = Template('''<input>
453 <requested-node xmlns="urn:opendaylight:sxp:controller">$node</requested-node>
454 <connections xmlns="urn:opendaylight:sxp:controller">
456 <peer-address>$ip</peer-address>
457 <tcp-port>$port</tcp-port>
458 <password>$password_</password>
460 <version>$version</version>
461 <description>Connection to ISR-G2</description>
463 <hold-time-min-acceptable>45</hold-time-min-acceptable>
464 <keep-alive-time>30</keep-alive-time>
465 <reconciliation-time>120</reconciliation-time>
471 data = templ.substitute(
472 {'ip': ip, 'port': port, 'mode': mode, 'version': version, 'node': node, 'password_': password_})
476 def delete_connections_xml(address, port, node):
477 """Generate xml for Delete Connection request
479 :param address: Ipv4/6 address of remote peer
480 :type address: string
481 :param port: Port on with remote peer listens
483 :param node: Ipv4 address of node
485 :returns: String containing xml data for request
488 templ = Template('''<input>
489 <requested-node xmlns="urn:opendaylight:sxp:controller">$node</requested-node>
490 <peer-address xmlns="urn:opendaylight:sxp:controller">$address</peer-address>
491 <tcp-port xmlns="urn:opendaylight:sxp:controller">$port</tcp-port>
493 data = templ.substitute({'address': address, 'port': port, 'node': node})
497 def update_binding_xml(sgt0, prefix0, sgt1, prefix1, ip):
498 """Generate xml for Update Binding request
500 :param sgt0: Original Source Group Tag
502 :param prefix0: Original Ipv4/6 prefix
503 :type prefix0: string
504 :param sgt1: New Source Group Tag
506 :param prefix1: New Ipv4/6 prefix
507 :type prefix1: string
508 :param ip: Ipv4 address of node
510 :returns: String containing xml data for request
513 templ = Template('''<input>
514 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
515 <original-binding xmlns="urn:opendaylight:sxp:controller">
517 <ip-prefix>$prefix0</ip-prefix>
519 <new-binding xmlns="urn:opendaylight:sxp:controller">
521 <ip-prefix>$prefix1</ip-prefix>
524 data = templ.substitute(
525 {'sgt0': sgt0, 'sgt1': sgt1, 'prefix0': prefix0, 'prefix1': prefix1, 'ip': ip})
529 def delete_binding_xml(sgt, prefix, ip):
530 """Generate xml for Delete Binding request
532 :param sgt: Source Group Tag
534 :param prefix: Ipv4/6 prefix
536 :param ip: Ipv4 address of node
538 :returns: String containing xml data for request
541 templ = Template('''<input>
542 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
543 <sgt xmlns="urn:opendaylight:sxp:controller">$sgt</sgt>
544 <ip-prefix xmlns="urn:opendaylight:sxp:controller">$prefix</ip-prefix>
546 data = templ.substitute({'sgt': sgt, 'prefix': prefix, 'ip': ip})
550 def add_peer_group_xml(name, peers, ip):
551 """Generate xml for Add PeerGroups request
553 :param name: Name of PeerGroup
555 :param peers: XML formatted peers that will be added to group
557 :param ip: Ipv4 address of node
559 :returns: String containing xml data for request
562 templ = Template('''<input>
563 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
564 <sxp-peer-group xmlns="urn:opendaylight:sxp:controller">
565 <name xmlns="urn:opendaylight:sxp:controller">$name</name>
566 <sxp-peers xmlns="urn:opendaylight:sxp:controller">$peers</sxp-peers>
569 data = templ.substitute({'name': name, 'peers': peers, 'ip': ip})
573 def delete_peer_group_xml(name, ip):
574 """Generate xml for Delete PeerGroup request
576 :param name: Name of PeerGroup
578 :param ip: Ipv4 address of node
580 :returns: String containing xml data for request
583 templ = Template('''<input>
584 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
585 <peer-group-name xmlns="urn:opendaylight:sxp:controller">$name</peer-group-name>
587 data = templ.substitute({'name': name, 'ip': ip})
591 def get_peer_groups_from_node_xml(ip):
592 """Generate xml for Get PeerGroups request
594 :param ip: Ipv4 address of node
596 :returns: String containing xml data for request
599 templ = Template('''<input>
600 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
602 data = templ.substitute({'ip': ip})
606 def add_filter_xml(group, filter_type, entries, ip):
607 """Generate xml for Add Filter request
609 :param group: Name of group containing filter
611 :param filter_type: Type of filter
612 :type filter_type: string
613 :param entries: XML formatted entries that will be added in filter
614 :type entries: string
615 :param ip: Ipv4 address of node
617 :returns: String containing xml data for request
621 templ = Template('''<input>
622 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
623 <peer-group-name xmlns="urn:opendaylight:sxp:controller">$group</peer-group-name>
624 <sxp-filter xmlns="urn:opendaylight:sxp:controller">
625 <filter-type>$filter_type</filter-type>$entries
628 data = templ.substitute(
629 {'group': group, 'filter_type': filter_type, 'ip': ip, 'entries': entries})
633 def delete_filter_xml(group, filter_type, ip):
634 """Generate xml for Delete Filter request
636 :param group: Name of group containing filter
638 :param filter_type: Type of filter
639 :type filter_type: string
640 :param ip: Ipv4 address of node
642 :returns: String containing xml data for request
645 templ = Template('''<input>
646 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
647 <peer-group-name xmlns="urn:opendaylight:sxp:controller">$group</peer-group-name>
648 <filter-type xmlns="urn:opendaylight:sxp:controller">$filter_type</filter-type>
650 data = templ.substitute(
651 {'group': group, 'filter_type': filter_type, 'ip': ip})
655 def get_connections_from_node_xml(ip):
656 """Generate xml for Get Connections 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 get_bindings_from_node_xml(ip, range):
671 """Generate xml for Get Bindings request
673 :param range: All or only Local bindings
675 :param ip: Ipv4 address of node
677 :returns: String containing xml data for request
680 templ = Template('''<input>
681 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
682 <bindings-range xmlns="urn:opendaylight:sxp:controller">$range</bindings-range>
684 data = templ.substitute({'ip': ip, 'range': range})