Testplan and suite for 1 iBGP peer performance tests
[integration/test.git] / csit / libraries / Sxp.py
1 import json
2 from ipaddr import IPAddress
3 from string import Template
4
5
6 def mod(num, base):
7     return int(num) % int(base)
8
9
10 def get_ip_from_number(n):
11     ip = IPAddress(2130706432 + n)
12     return str(ip)
13
14
15 def lower_version(ver1, ver2):
16     v1 = int(ver1[-1:])
17     v2 = int(ver2[-1:])
18     if v1 <= v2:
19         return ver1
20     else:
21         return ver2
22
23
24 def parse_connections(input):
25     data = json.loads(input)
26     connections = data['output']['connections']
27     output = []
28     for list in connections.values():
29         output = output + list
30     return output
31
32
33 def find_connection(input, version, mode, ip, port, state):
34     for connection in parse_connections(input):
35         if (connection['peer-address'] == ip and connection['tcp-port'] == int(port) and connection['mode'] == mode and
36                 connection['version'] == version):
37             if state == 'none':
38                 return True
39             elif connection['state'] == state:
40                 return True
41     return False
42
43
44 def parse_prefix_groups(input, source_):
45     data = json.loads(input)
46     bindings = data['sxp-node:master-database']
47     output = []
48     for binding in bindings.values():
49         for binding_source in binding:
50             if binding_source['binding-source'] == source_:
51                 for prefix_group in binding_source['prefix-group']:
52                     output.append(prefix_group)
53     return output
54
55
56 def find_binding(input, sgt, prefix, source_, action):
57     found = False
58     for prefixgroup in parse_prefix_groups(input, source_):
59         if prefixgroup['sgt'] == int(sgt):
60             for binding in prefixgroup['binding']:
61                 if binding['ip-prefix'] == prefix and binding['action'] == action:
62                     found = True
63     return found
64
65
66 def find_binding_with_peer_sequence(input, sgt, prefix, source_, action, node_id, peer_seq):
67     correct_sequence = False
68     found_source = False
69     for prefixgroup in parse_prefix_groups(input, source_):
70         if prefixgroup['sgt'] == int(sgt):
71             for binding in prefixgroup['binding']:
72                 if binding['ip-prefix'] == prefix and binding['action'] == action:
73                     for peer in binding['peer-sequence']['peer']:
74                         if peer['seq'] == int(peer_seq) and peer['node-id'] == node_id:
75                             correct_sequence = True
76                     for peer_source in binding['sources']['source']:
77                         if peer_source == node_id:
78                             found_source = True
79     return found_source and correct_sequence
80
81
82 def add_entry_xml(sgt, prefix, ip):
83     templ = Template('''<input>
84   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
85   <sgt xmlns="urn:opendaylight:sxp:controller">$sgt</sgt>
86   <ip-prefix xmlns="urn:opendaylight:sxp:controller">$prefix</ip-prefix>
87 </input>''')
88     data = templ.substitute({'sgt': sgt, 'prefix': prefix, 'ip': ip})
89     return data
90
91
92 def add_connection_xml(version, mode, ip, port, ip_, password_):
93     templ = Template('''<input>
94    <requested-node xmlns="urn:opendaylight:sxp:controller">$ip_</requested-node>
95    <connections xmlns="urn:opendaylight:sxp:controller">
96       <connection>
97          <peer-address>$ip</peer-address>
98          <tcp-port>$port</tcp-port>
99          <password>$password_</password>
100          <mode>$mode</mode>
101          <version>$version</version>
102          <description>Connection to ISR-G2</description>
103          <connection-timers>
104             <hold-time-min-acceptable>45</hold-time-min-acceptable>
105             <keep-alive-time>30</keep-alive-time>
106             <reconciliation-time>120</reconciliation-time>
107          </connection-timers>
108       </connection>
109    </connections>
110 </input>
111 ''')
112     data = templ.substitute(
113         {'ip': ip, 'port': port, 'mode': mode, 'version': version, 'ip_': ip_, 'password_': password_})
114     return data
115
116
117 def delete_connections_xml(address, port, node):
118     templ = Template('''<input>
119    <requested-node xmlns="urn:opendaylight:sxp:controller">$node</requested-node>
120    <peer-address xmlns="urn:opendaylight:sxp:controller">$address</peer-address>
121    <tcp-port xmlns="urn:opendaylight:sxp:controller">$port</tcp-port>
122 </input>''')
123     data = templ.substitute({'address': address, 'port': port, 'node': node})
124     return data
125
126
127 def update_binding_xml(sgt0, prefix0, sgt1, prefix1, ip):
128     templ = Template('''<input>
129   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
130   <original-binding xmlns="urn:opendaylight:sxp:controller">
131     <sgt>$sgt0</sgt>
132     <ip-prefix>$prefix0</ip-prefix>
133   </original-binding>
134   <new-binding xmlns="urn:opendaylight:sxp:controller">
135     <sgt>$sgt1</sgt>
136     <ip-prefix>$prefix1</ip-prefix>
137   </new-binding>
138 </input>''')
139     data = templ.substitute(
140         {'sgt0': sgt0, 'sgt1': sgt1, 'prefix0': prefix0, 'prefix1': prefix1, 'ip': ip})
141     return data
142
143
144 def delete_binding_xml(sgt, prefix, ip):
145     templ = Template('''<input>
146   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
147   <sgt xmlns="urn:opendaylight:sxp:controller">$sgt</sgt>
148   <ip-prefix xmlns="urn:opendaylight:sxp:controller">$prefix</ip-prefix>
149 </input>''')
150     data = templ.substitute({'sgt': sgt, 'prefix': prefix, 'ip': ip})
151     return data
152
153
154 def get_connections_from_node_xml(ip):
155     templ = Template('''<input>
156    <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
157 </input>''')
158     data = templ.substitute({'ip': ip})
159     return data
160
161
162 def get_bindings_from_node_xml(ip):
163     templ = Template('''<input>
164   <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
165 </input>''')
166     data = templ.substitute({'ip': ip})
167     return data