Fix typo in Sxp library
[integration/test.git] / csit / libraries / Sxp.py
1 import json
2 from netaddr import IPAddress
3 from string import Template
4
5
6 def get_active_controller_from_json(resp, service):
7     """Gets index of active controller running specified service
8
9     :param resp: JSON formatted response from EOS
10     :type resp: string
11     :param service: EOS Service to look for
12     :type service: string
13     :return: Index of controller
14     """
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:])
21     return 0
22
23
24 def mod(num, base):
25     """Gets modulo of number
26
27     :param num: Number to be used
28     :type num: string
29     :param base: Base used
30     :type base: string
31     :returns: Int representing modulo of specified numbers.
32
33     """
34     return int(num) % int(base)
35
36
37 def get_average_of_items(items):
38     """Gets average of items in provided list
39
40     :param items: To be proceed
41     :return: Average value
42
43     """
44     return sum(items) / len(items)
45
46
47 def get_opposing_mode(mode):
48     """Generate string representing opposing SXP peer mode
49
50         :param mode: SXP peer mode
51         :type mode: str
52         :returns: String with opposing SXP peer mode.
53
54         """
55     if 'speaker' == mode:
56         return 'listener'
57     elif 'listener' == mode:
58         return 'speaker'
59     return 'both'
60
61
62 def get_ip_from_number(n, base=2130706432):
63     """Generate string representing Ipv4 from specified number plus base value
64
65     :param n: Number to be converted
66     :type n: int
67     :param base: Starting index
68     :type base: int
69     :returns: String containing Ipv4.
70
71     """
72     ip = IPAddress(int(base) + n)
73     return str(ip)
74
75
76 def get_ip_from_number_and_ip(n, ip_address):
77     """Generate string representing Ipv4 from specified number and IPAddress
78
79     :param n: Number to be converted
80     :type n: int
81     :param ip_address: Base address
82     :type ip_address: string
83     :returns: String containing Ipv4.
84
85     """
86     ip = IPAddress(int(IPAddress(ip_address)) + n)
87     return str(ip)
88
89
90 def lower_version(ver1, ver2):
91     """Generate xml containing SGT mach data
92
93     :param ver1: Version of SXP protocol for compare
94     :type ver1: string
95     :param ver2: Version of SXP protocol for compare
96     :type ver2: string
97     :returns: String containing lower from those two specified versions.
98
99     """
100     v1 = int(ver1[-1:])
101     v2 = int(ver2[-1:])
102     if v1 <= v2:
103         return ver1
104     else:
105         return ver2
106
107
108 def get_filter_entry(seq, entry_type, sgt="", esgt="", acl="", eacl="", pl="", epl="", ps=""):
109     """Generate xml containing FilterEntry data
110
111     :param seq: Sequence of entry
112     :type seq: string
113     :param entry_type: Type of entry (permit/deny)
114     :type entry_type: string
115     :param sgt: SGT matches to be added to entry
116     :type sgt: string
117     :param esgt: SGT ranges match to be added to entry
118     :type esgt: string
119     :param acl: ACL matches to be added to entry
120     :type acl: string
121     :param eacl: EACL matches to be added to entry
122     :type eacl: string
123     :param pl: PrefixList matches to be added to entry
124     :type pl: string
125     :param epl: ExtendedPrefixList matches to be added to entry
126     :type epl: string
127     :param ps: PeerSequence matches to be added to entry
128     :type ps: string
129     :returns: String containing xml data for request
130
131     """
132     entries = ""
133     # Generate XML request containing combination of Matches of different types
134     if sgt:
135         args = sgt.split(',')
136         entries += add_sgt_matches_xml(args)
137     elif esgt:
138         args = esgt.split(',')
139         entries += add_sgt_range_xml(args[0], args[1])
140     if pl:
141         entries += add_pl_entry_xml(pl)
142     elif epl:
143         args = epl.split(',')
144         entries += add_epl_entry_xml(args[0], args[1], args[2])
145     if acl:
146         args = acl.split(',')
147         entries += add_acl_entry_xml(args[0], args[1])
148     elif eacl:
149         args = eacl.split(',')
150         entries += add_eacl_entry_xml(args[0], args[1], args[2], args[3])
151     if ps:
152         args = ps.split(',')
153         entries += add_ps_entry_xml(args[0], args[1])
154     # Wrap entries in ACL/PrefixList according to specified values
155     if pl or epl:
156         return add_pl_entry_default_xml(seq, entry_type, entries)
157     elif ps:
158         return add_ps_entry_default_xml(seq, entry_type, entries)
159     return add_acl_entry_default_xml(seq, entry_type, entries)
160
161
162 def add_peers(*args):
163     """Generate xml containing Peer mach data
164
165     :param args: Peers data
166     :type args: dict
167     :returns: String containing xml data for request
168
169     """
170     templ = Template('''
171         <sxp-peer>
172             <peer-address>$ip</peer-address>
173         </sxp-peer>''')
174     peers = ""
175     for count, value in enumerate(args):
176         peers += templ.substitute({'ip': value})
177     return peers
178
179
180 def add_domains(*args):
181     """Generate xml containing Domain mach data
182
183     :param args: Domain data
184     :type args: dict
185     :returns: String containing xml data for request
186
187     """
188     templ = Template('''
189         <domain>
190             <name>$name</name>
191         </domain>''')
192     peers = ""
193     for count, value in enumerate(args):
194         peers += templ.substitute({'name': value})
195     return peers
196
197
198 def add_sgt_matches_xml(sgt_entries):
199     """Generate xml containing SGT mach data
200
201     :param sgt_entries: SGT matches
202     :type sgt_entries: string
203     :returns: String containing xml data for request
204
205     """
206     templ = Template('''
207         <matches>$sgt</matches>''')
208     matches = ""
209     for sgt in sgt_entries:
210         matches += templ.substitute({'sgt': sgt})
211     return matches
212
213
214 def add_sgt_range_xml(start, end):
215     """Generate xml containing SGT RangeMach data
216
217     :param start: Start range of SGT
218     :type start: string
219     :param end: End range of SGT
220     :type end: string
221     :returns: String containing xml data for request
222
223     """
224     templ = Template('''
225         <sgt-start>$start</sgt-start>
226         <sgt-end>$end</sgt-end>''')
227     match = templ.substitute({'start': start, 'end': end})
228     return match
229
230
231 def add_acl_entry_default_xml(seq, entry_type, acl_entries):
232     """Generate xml containing AccessList data
233
234     :param seq: Sequence of PrefixList entry
235     :type seq: string
236     :param entry_type: Entry type (permit/deny)
237     :type entry_type: string
238     :param acl_entries: XML data containing AccessList entries
239     :type acl_entries: string
240     :returns: String containing xml data for request
241
242     """
243     templ = Template('''
244         <acl-entry>
245             <entry-type>$entry_type</entry-type>
246             <entry-seq>$seq</entry-seq>$acl_entries
247         </acl-entry>''')
248     matches = templ.substitute(
249         {'seq': seq, 'entry_type': entry_type, 'acl_entries': acl_entries})
250     return matches
251
252
253 def add_acl_entry_xml(ip, mask):
254     """Generate xml containing AccessList data
255
256     :param ip: Ipv4/6 address
257     :type ip: string
258     :param mask: Ipv4/6 wildcard mask
259     :type mask: string
260     :returns: String containing xml data for request
261
262     """
263     templ = Template('''
264         <acl-match>
265             <ip-address>$ip</ip-address>
266             <wildcard-mask>$mask</wildcard-mask>
267         </acl-match>''')
268     return templ.substitute({'ip': ip, 'mask': mask})
269
270
271 def add_eacl_entry_xml(ip, mask, amask, wmask):
272     """Generate xml containing ExtendedAccessList data
273
274     :param ip: Ipv4/6 address
275     :type ip: string
276     :param mask: Ipv4/6 wildcard mask
277     :type mask: string
278     :param amask: Ipv4/6 address mask
279     :type amask: string
280     :param wmask: Ipv4/6 address wildcard mask
281     :type wmask: string
282     :returns: String containing xml data for request
283
284     """
285     templ = Template('''
286         <acl-match>
287             <ip-address>$ip</ip-address>
288             <wildcard-mask>$mask</wildcard-mask>
289             <mask>
290               <address-mask>$amask</address-mask>
291               <wildcard-mask>$wmask</wildcard-mask>
292             </mask>
293         </acl-match>''')
294     return templ.substitute({'ip': ip, 'mask': mask, 'amask': amask, 'wmask': wmask})
295
296
297 def add_ps_entry_default_xml(seq, entry_type, ps_entries):
298     """Generate xml containing PeerSequence data
299
300     :param seq: Sequence of PrefixList entry
301     :type seq: string
302     :param entry_type: Entry type (permit/deny)
303     :type entry_type: string
304     :param ps_entries: XML data containing PeerSequence entries
305     :type ps_entries: string
306     :returns: String containing xml data for request
307
308     """
309     templ = Template('''
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})
315
316
317 def add_pl_entry_default_xml(seq, entry_type, pl_entries):
318     """Generate xml containing PrefixList data
319
320     :param seq: Sequence of PrefixList entry
321     :type seq: string
322     :param entry_type: Entry type (permit/deny)
323     :type entry_type: string
324     :param pl_entries: XML data containing PrefixList entries
325     :type pl_entries: string
326     :returns: String containing xml data for request
327
328     """
329     templ = Template('''
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})
335
336
337 def add_pl_entry_xml(prefix):
338     """Generate xml containing PrefixList data
339
340     :param prefix: Ipv4/6 prefix
341     :type prefix: string
342     :returns: String containing xml data for request
343
344     """
345     templ = Template('''
346         <prefix-list-match>
347             <ip-prefix>$prefix</ip-prefix>
348         </prefix-list-match>''')
349     return templ.substitute({'prefix': prefix})
350
351
352 def add_epl_entry_xml(prefix, op, mask):
353     """Generate xml containing Extended PrefixList data
354
355     :param prefix: Ipv4/6 prefix
356     :type prefix: string
357     :param op: PrefixList option (ge/le/eq)
358     :type op: string
359     :param mask: Ipv4/6 Mask
360     :type mask: string
361     :returns: String containing xml data for request
362
363     """
364     templ = Template('''
365         <prefix-list-match>
366             <ip-prefix>$prefix</ip-prefix>
367             <mask>
368                 <mask-range>$op</mask-range>
369                 <mask-value>$mask</mask-value>
370             </mask>
371         </prefix-list-match>''')
372     return templ.substitute({'prefix': prefix, 'mask': mask, 'op': op})
373
374
375 def add_ps_entry_xml(op, length):
376     """Generate xml containing Extended PrefixList data
377
378     :param op: PrefixList option (ge/le/eq)
379     :type op: string
380     :param length: PeerSequence length
381     :type length: string
382     :returns: String containing xml data for request
383
384     """
385     templ = Template('''
386         <peer-sequence-length>$length</peer-sequence-length>
387         <peer-sequence-range>$op</peer-sequence-range>
388         ''')
389     return templ.substitute({'length': length, 'op': op})
390
391
392 def parse_peer_groups(groups_json):
393     """Parse JSON string into Array of PeerGroups
394
395     :param groups_json: JSON containing PeerGroups
396     :type groups_json: string
397     :returns: Array containing PeerGroups.
398
399     """
400     data = json.loads(groups_json)
401     groups = data['output']
402     output = []
403     for group in groups.values():
404         output += group
405     return output
406
407
408 def parse_connections(connections_json):
409     """Parse JSON string into Array of Connections
410
411     :param connections_json: JSON containing Connections
412     :type connections_json: string
413     :returns: Array containing Connections.
414
415     """
416     data = json.loads(connections_json)
417     connections = data['output']['connections']
418     output = []
419     for connection in connections.values():
420         output += connection
421     return output
422
423
424 def find_connection(connections_json, version, mode, ip, port, state):
425     """Test if Connection with specified values is contained in JSON
426
427     :param connections_json: JSON containing Connections
428     :type connections_json: string
429     :param version: Version of SXP protocol (version1/2/3/4)
430     :type version: string
431     :param mode: Mode of SXP peer (speaker/listener/both)
432     :type mode: string
433     :param ip: Ipv4/6 address of remote peer
434     :type ip: string
435     :param port: Port on with remote peer listens
436     :type port: string
437     :param state: State of connection (on/off/pendingOn/deleteHoldDown)
438     :type state: string
439     :returns: True if Connection with specified params was found, otherwise False.
440
441     """
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):
445             if state == 'none':
446                 return True
447             elif connection['state'] == state:
448                 return True
449     return False
450
451
452 def parse_bindings(bindings_json):
453     """Parse JSON string into Array of Bindings
454
455     :param bindings_json: JSON containing Bindings
456     :type bindings_json: string
457     :returns: Array containing Bindings.
458
459     """
460     data = json.loads(bindings_json)
461     output = []
462     for bindings_json in data['output'].values():
463         for binding in bindings_json:
464             output.append(binding)
465     return output
466
467
468 def find_binding(bindings, sgt, prefix):
469     """Test if Binding with specified values is contained in JSON
470
471     :param bindings: JSON containing Bindings
472     :type bindings: string
473     :param sgt: Source Group Tag
474     :type sgt: string
475     :param prefix: Ipv4/6 prefix
476     :type prefix: string
477     :returns: True if Binding with specified params was found, otherwise False.
478
479     """
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:
484                     return True
485     return False
486
487
488 def parse_prefix_groups(prefix_groups_json, source_):
489     """Parse JSON string into Array of PrefixGroups
490
491     :param prefix_groups_json: JSON containing PrefixGroups
492     :type prefix_groups_json: string
493     :param source_: Source of PrefixGroups (sxp/local)
494     :type source_: string
495     :returns: Array containing PrefixGroups.
496
497     """
498     data = json.loads(prefix_groups_json)
499     bindings = data['sxp-node:master-database']
500     output = []
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)
506     return output
507
508
509 def find_binding_legacy(prefix_groups_json, sgt, prefix, source_, action):
510     """Test if Binding with specified values is contained in JSON
511
512     :param prefix_groups_json: JSON containing Bindings and PrefixGroups
513     :type prefix_groups_json: string
514     :param sgt: Source Group Tag
515     :type sgt: string
516     :param prefix: Ipv4/6 prefix
517     :type prefix: string
518     :param source_: Source of binding (local/sxp)
519     :type source_: string
520     :param action: Action for binding (add/delete)
521     :type action: string
522     :returns: True if Binding with specified params was found, otherwise False.
523
524     """
525     found = 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:
530                     found = True
531     return found
532
533
534 def add_entry_xml(sgt, prefix, ip, domain_name):
535     """Generate xml for Add Bindings request
536
537     :param sgt: Source Group Tag
538     :type sgt: string
539     :param prefix: Ipv4/6 prefix
540     :type prefix: string
541     :param ip: Ipv4 address of node
542     :type ip: string
543     :param domain_name: Name of Domain
544     :type domain_name: string
545     :returns: String containing xml data for request
546
547     """
548     templ = Template('''<input>
549   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
550   $domain
551   <sgt xmlns="urn:opendaylight:sxp:controller">$sgt</sgt>
552   <ip-prefix xmlns="urn:opendaylight:sxp:controller">$prefix</ip-prefix>
553 </input>''')
554     data = templ.substitute({'sgt': sgt, 'prefix': prefix, 'ip': ip, 'domain': get_domain_name(domain_name)})
555     return data
556
557
558 def add_connection_xml(version, mode, ip, port, node, password_, domain_name, bindings_timeout=0):
559     """Generate xml for Add Connection request
560
561     :param version: Version of SXP protocol (version1/2/3/4)
562     :type version: string
563     :param mode: Mode of SXP peer (speaker/listener/both)
564     :type mode: string
565     :param ip: Ipv4/6 address of remote peer
566     :type ip: string
567     :param port: Port on with remote peer listens
568     :type port: string
569     :param node: Ipv4 address of node
570     :type node: string
571     :param password_: Password type (none/default)
572     :type password_: string
573     :param domain_name: Name of Domain
574     :type domain_name: string
575     :param bindings_timeout: Specifies DHD and Reconciliation timers
576     :type bindings_timeout: int
577     :returns: String containing xml data for request
578
579     """
580     templ = Template('''<input>
581    <requested-node xmlns="urn:opendaylight:sxp:controller">$node</requested-node>
582    $domain
583    <connections xmlns="urn:opendaylight:sxp:controller">
584       <connection>
585          <peer-address>$ip</peer-address>
586          <tcp-port>$port</tcp-port>
587          <password>$password_</password>
588          <mode>$mode</mode>
589          <version>$version</version>
590          <description>Connection to ISR-G2</description>
591          <connection-timers>
592             <hold-time-min-acceptable>45</hold-time-min-acceptable>
593             <keep-alive-time>30</keep-alive-time>
594             <reconciliation-time>$timeout</reconciliation-time>
595             <delete-hold-down-time>$timeout</delete-hold-down-time>
596          </connection-timers>
597       </connection>
598    </connections>
599 </input>
600 ''')
601     data = templ.substitute(
602         {'ip': ip, 'port': port, 'mode': mode, 'version': version, 'node': node,
603          'password_': password_, 'domain': get_domain_name(domain_name), 'timeout': bindings_timeout})
604     return data
605
606
607 def delete_connections_xml(address, port, node, domain_name):
608     """Generate xml for Delete Connection request
609
610     :param address: Ipv4/6 address of remote peer
611     :type address: string
612     :param port: Port on with remote peer listens
613     :type port: string
614     :param node: Ipv4 address of node
615     :type node: string
616     :param domain_name: Name of Domain
617     :type domain_name: string
618     :returns: String containing xml data for request
619
620     """
621     templ = Template('''<input>
622    <requested-node xmlns="urn:opendaylight:sxp:controller">$node</requested-node>
623    $domain
624    <peer-address xmlns="urn:opendaylight:sxp:controller">$address</peer-address>
625    <tcp-port xmlns="urn:opendaylight:sxp:controller">$port</tcp-port>
626 </input>''')
627     data = templ.substitute({'address': address, 'port': port, 'node': node, 'domain': get_domain_name(domain_name)})
628     return data
629
630
631 def update_binding_xml(sgt0, prefix0, sgt1, prefix1, ip, domain_name):
632     """Generate xml for Update Binding request
633
634     :param sgt0: Original Source Group Tag
635     :type sgt0: string
636     :param prefix0: Original Ipv4/6 prefix
637     :type prefix0: string
638     :param sgt1: New Source Group Tag
639     :type sgt1: string
640     :param prefix1: New Ipv4/6 prefix
641     :type prefix1: string
642     :param ip: Ipv4 address of node
643     :type ip: string
644     :param domain_name: Name of Domain
645     :type domain_name: string
646     :returns: String containing xml data for request
647
648     """
649     templ = Template('''<input>
650   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
651   $domain
652   <original-binding xmlns="urn:opendaylight:sxp:controller">
653     <sgt>$sgt0</sgt>
654     <ip-prefix>$prefix0</ip-prefix>
655   </original-binding>
656   <new-binding xmlns="urn:opendaylight:sxp:controller">
657     <sgt>$sgt1</sgt>
658     <ip-prefix>$prefix1</ip-prefix>
659   </new-binding>
660 </input>''')
661     data = templ.substitute(
662         {'sgt0': sgt0, 'sgt1': sgt1, 'prefix0': prefix0, 'prefix1': prefix1, 'ip': ip,
663          'domain': get_domain_name(domain_name)})
664     return data
665
666
667 def delete_binding_xml(sgt, prefix, ip, domain_name):
668     """Generate xml for Delete Binding request
669
670     :param sgt: Source Group Tag
671     :type sgt: string
672     :param prefix: Ipv4/6 prefix
673     :type prefix: string
674     :param ip: Ipv4 address of node
675     :type ip: string
676     :param domain_name: Name of Domain
677     :type domain_name: string
678     :returns: String containing xml data for request
679
680     """
681     templ = Template('''<input>
682   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
683   <sgt xmlns="urn:opendaylight:sxp:controller">$sgt</sgt>
684   <ip-prefix xmlns="urn:opendaylight:sxp:controller">$prefix</ip-prefix>
685   $domain
686 </input>''')
687     data = templ.substitute({'sgt': sgt, 'prefix': prefix, 'ip': ip, 'domain': get_domain_name(domain_name)})
688     return data
689
690
691 def add_peer_group_xml(name, peers, ip):
692     """Generate xml for Add PeerGroups request
693
694     :param name: Name of PeerGroup
695     :type name: string
696     :param peers: XML formatted peers that will be added to group
697     :type peers: string
698     :param ip: Ipv4 address of node
699     :type ip: string
700     :returns: String containing xml data for request
701
702     """
703     templ = Template('''<input>
704   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
705   <sxp-peer-group xmlns="urn:opendaylight:sxp:controller">
706     <name xmlns="urn:opendaylight:sxp:controller">$name</name>
707     <sxp-peers xmlns="urn:opendaylight:sxp:controller">$peers</sxp-peers>
708     </sxp-peer-group>
709 </input>''')
710     data = templ.substitute({'name': name, 'peers': peers, 'ip': ip})
711     return data
712
713
714 def delete_peer_group_xml(name, ip):
715     """Generate xml for Delete PeerGroup request
716
717     :param name: Name of PeerGroup
718     :type name: string
719     :param ip: Ipv4 address of node
720     :type ip: string
721     :returns: String containing xml data for request
722
723     """
724     templ = Template('''<input>
725   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
726   <peer-group-name xmlns="urn:opendaylight:sxp:controller">$name</peer-group-name>
727 </input>''')
728     data = templ.substitute({'name': name, 'ip': ip})
729     return data
730
731
732 def get_peer_groups_from_node_xml(ip):
733     """Generate xml for Get PeerGroups request
734
735     :param ip: Ipv4 address of node
736     :type ip: string
737     :returns: String containing xml data for request
738
739     """
740     templ = Template('''<input>
741    <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
742 </input>''')
743     data = templ.substitute({'ip': ip})
744     return data
745
746
747 def add_filter_xml(group, filter_type, entries, ip):
748     """Generate xml for Add Filter request
749
750     :param group: Name of group containing filter
751     :type group: string
752     :param filter_type: Type of filter
753     :type filter_type: string
754     :param entries: XML formatted entries that will be added in filter
755     :type entries: string
756     :param ip: Ipv4 address of node
757     :type ip: string
758     :returns: String containing xml data for request
759
760     """
761     templ = Template('''<input>
762   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
763   <peer-group-name xmlns="urn:opendaylight:sxp:controller">$group</peer-group-name>
764   <sxp-filter xmlns="urn:opendaylight:sxp:controller">
765     <filter-type>$filter_type</filter-type>$entries
766   </sxp-filter>
767 </input>''')
768     data = templ.substitute(
769         {'group': group, 'filter_type': filter_type, 'ip': ip, 'entries': entries})
770     return data
771
772
773 def add_domain_filter_xml(domain, domains, entries, ip, filter_name=None):
774     """Generate xml for Add Domain Filter request
775
776     :param domain: Name of Domain containing filter
777     :type domain: string
778     :param domains: Domains on which filter will be applied
779     :type domains: string
780     :param entries: XML formatted entries that will be added in filter
781     :type entries: string
782     :param ip: Ipv4 address of node
783     :type ip: string
784     :param filter_name: Name of filter
785     :type filter_name: string
786     :returns: String containing xml data for request
787
788     """
789     if filter_name:
790         filter_name = "<filter-name>" + filter_name + "</filter-name>"
791     templ = Template('''<input>
792   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
793   <domain-name xmlns="urn:opendaylight:sxp:controller">$domain</domain-name>
794   <sxp-domain-filter xmlns="urn:opendaylight:sxp:controller">
795     $filter_name
796     <domains>$domains</domains>
797     $entries
798   </sxp-domain-filter>
799 </input>''')
800     data = templ.substitute(
801         {'domain': domain, 'domains': domains, 'ip': ip, 'entries': entries, 'filter_name': filter_name})
802     return data
803
804
805 def delete_filter_xml(group, filter_type, ip):
806     """Generate xml for Delete Filter request
807
808     :param group: Name of group containing filter
809     :type group: string
810     :param filter_type: Type of filter
811     :type filter_type: string
812     :param ip: Ipv4 address of node
813     :type ip: string
814     :returns: String containing xml data for request
815
816     """
817     templ = Template('''<input>
818   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
819   <peer-group-name xmlns="urn:opendaylight:sxp:controller">$group</peer-group-name>
820   <filter-type xmlns="urn:opendaylight:sxp:controller">$filter_type</filter-type>
821 </input>''')
822     data = templ.substitute(
823         {'group': group, 'filter_type': filter_type, 'ip': ip})
824     return data
825
826
827 def delete_domain_filter_xml(domain, ip, filter_name=None):
828     """Generate xml for Delete Filter request
829
830     :param domain: Name of Domain containing filter
831     :type domain: string
832     :param ip: Ipv4 address of node
833     :type ip: string
834     :param filter_name: Name of filter
835     :type filter_name: string
836     :returns: String containing xml data for request
837
838     """
839     if filter_name:
840         filter_name = '<filter-name xmlns="urn:opendaylight:sxp:controller">' + filter_name + "</filter-name>"
841     templ = Template('''<input>
842   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
843   <domain-name xmlns="urn:opendaylight:sxp:controller">$domain</domain-name>
844   $filter_name
845 </input>''')
846     data = templ.substitute(
847         {'domain': domain, 'ip': ip, 'filter_name': filter_name})
848     return data
849
850
851 def get_connections_from_node_xml(ip, domain_name):
852     """Generate xml for Get Connections request
853
854     :param ip: Ipv4 address of node
855     :type ip: string
856     :param domain_name: Name of Domain
857     :type domain_name: string
858     :returns: String containing xml data for request
859
860     """
861     templ = Template('''<input>
862    <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
863    $domain
864 </input>''')
865     data = templ.substitute({'ip': ip, 'domain': get_domain_name(domain_name)})
866     return data
867
868
869 def get_bindings_from_node_xml(ip, binding_range, domain_name):
870     """Generate xml for Get Bindings request
871
872     :param binding_range: All or only Local bindings
873     :type binding_range: string
874     :param ip: Ipv4 address of node
875     :type ip: string
876     :param domain_name: Name of Domain
877     :type domain_name: string
878     :returns: String containing xml data for request
879
880     """
881     templ = Template('''<input>
882   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
883   <bindings-range xmlns="urn:opendaylight:sxp:controller">$range</bindings-range>
884   $domain
885 </input>''')
886     data = templ.substitute({'ip': ip, 'range': binding_range, 'domain': get_domain_name(domain_name)})
887     return data
888
889
890 def add_node_xml(node_id, port, password, version, node_ip=None, expansion=0, bindings_timeout=0):
891     """Generate xml for Add Node request
892
893     :param node_id: Ipv4 address formatted node id
894     :type node_id: string
895     :param node_ip: Ipv4 address of node
896     :type node_ip: string
897     :param port: Node port number
898     :type port: int
899     :param password: TCP-MD5 password
900     :type password: string
901     :param version: Sxp device version
902     :type version: string
903     :param expansion: Bindings expansion
904     :type expansion: int
905     :param bindings_timeout: Specifies DHD and Reconciliation timers
906     :type bindings_timeout: int
907     :returns: String containing xml data for request
908
909     """
910     templ = Template('''<input xmlns="urn:opendaylight:sxp:controller">
911     <node-id>$id</node-id>
912     <timers>
913         <retry-open-time>1</retry-open-time>
914         <hold-time-min-acceptable>120</hold-time-min-acceptable>
915         <delete-hold-down-time>$timeout</delete-hold-down-time>
916         <hold-time-min>90</hold-time-min>
917         <reconciliation-time>$timeout</reconciliation-time>
918         <hold-time>90</hold-time>
919         <hold-time-max>180</hold-time-max>
920         <keep-alive-time>30</keep-alive-time>
921     </timers>
922     <mapping-expanded>$expansion</mapping-expanded>
923     <security>
924         <password>$password</password>
925     </security>
926     <tcp-port>$port</tcp-port>
927     <version>$version</version>
928     <description>ODL SXP Controller</description>
929     <source-ip>$ip</source-ip>
930     <master-database></master-database>
931 </input>''')
932     data = templ.substitute(
933         {'ip': node_id if not node_ip else node_ip, 'id': node_id, 'port': port, 'password': password,
934          'version': version, 'expansion': expansion, 'timeout': bindings_timeout})
935     return data
936
937
938 def delete_node_xml(node_id):
939     """Generate xml for Delete node request
940
941     :param node_id: Ipv4 address formatted node id
942     :type node_id: string
943     :returns: String containing xml data for request
944
945     """
946     templ = Template('''<input xmlns="urn:opendaylight:sxp:controller">
947   <node-id>$id</node-id>
948 </input>''')
949     data = templ.substitute({'id': node_id})
950     return data
951
952
953 def add_domain_xml(node_id, name):
954     """Generate xml for Add Domain request
955
956     :param node_id: Id of node
957     :type node_id: string
958     :param name: Name of Domain
959     :type name: string
960     :returns: String containing xml data for request
961
962     """
963     templ = Template('''<input>
964   <node-id xmlns="urn:opendaylight:sxp:controller">$id</node-id>
965   <domain-name xmlns="urn:opendaylight:sxp:controller">$name</domain-name>
966 </input>''')
967     data = templ.substitute({'name': name, 'id': node_id})
968     return data
969
970
971 def delete_domain_xml(node_id, name):
972     """Generate xml for Remove Domain request
973
974     :param node_id: Id of node
975     :type node_id: string
976     :param name: Name of Domain
977     :type name: string
978     :returns: String containing xml data for request
979
980     """
981     return add_domain_xml(node_id, name)
982
983
984 def get_domain_name(domain_name):
985     """Generate xml for Get Bindings request
986
987     :param domain_name: Name of Domain
988     :type domain_name: string
989     :returns: String containing xml data for request
990
991     """
992     if domain_name == 'global':
993         return ''
994     else:
995         return '<domain-name xmlns="urn:opendaylight:sxp:controller">' + domain_name + '</domain-name>'
996
997
998 def add_bindings_xml(node_id, domain, sgt, prefixes):
999     """Generate xml for Add Bindings request
1000
1001     :param node_id: Id of node
1002     :type node_id: string
1003     :param domain: Name of Domain
1004     :type domain: string
1005     :param sgt: Security group
1006     :type sgt: int
1007     :param prefixes: List of ip-prefixes
1008     :type prefixes: string
1009     :returns: String containing xml data for request
1010
1011     """
1012     bindings = ''
1013     for prefix in prefixes.split(','):
1014         bindings += '\n' + '<ip-prefix>' + prefix + '</ip-prefix>'
1015     templ = Template('''<input>
1016   <node-id xmlns="urn:opendaylight:sxp:controller">$id</node-id>
1017   <domain-name xmlns="urn:opendaylight:sxp:controller">$name</domain-name>
1018   <binding xmlns="urn:opendaylight:sxp:controller">
1019       <sgt>$sgt</sgt>
1020       $bindings
1021   </binding>
1022 </input>''')
1023     data = templ.substitute({'name': domain, 'id': node_id, 'sgt': sgt, 'bindings': bindings})
1024     return data
1025
1026
1027 def delete_bindings_xml(node_id, domain, sgt, prefixes):
1028     """Generate xml for Remove Bindings request
1029
1030     :param node_id: Id of node
1031     :type node_id: string
1032     :param domain: Name of Domain
1033     :type domain: string
1034     :param sgt: Security group
1035     :type sgt: int
1036     :param prefixes: List of ip-prefixes
1037     :type prefixes: string
1038     :returns: String containing xml data for request
1039
1040     """
1041     return add_bindings_xml(node_id, domain, sgt, prefixes)
1042
1043
1044 def prefix_range(start, end):
1045     """Generate and concatenate ip-prefixes
1046
1047     :param start: Start index
1048     :type start: string
1049     :param end: End index
1050     :type end: string
1051     :returns: String containing concatenated ip-prefixes
1052
1053     """
1054     start = int(start)
1055     end = int(end)
1056     index = 0
1057     prefixes = ''
1058     while index < end:
1059         prefixes += get_ip_from_number(index + start) + '/32'
1060         index += 1
1061         if index < end:
1062             prefixes += ','
1063     return prefixes