2 from ipaddr import IPAddress
3 from string import Template
7 return int(num) % int(base)
10 def get_ip_from_number(n):
11 ip = IPAddress(2130706432 + n)
15 def lower_version(ver1, ver2):
24 def parse_connections(input):
25 data = json.loads(input)
26 connections = data['output']['connections']
28 for list in connections.values():
29 output = output + list
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):
39 elif connection['state'] == state:
44 def parse_prefix_groups(input, source_):
45 data = json.loads(input)
46 bindings = data['sxp-node:master-database']
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)
56 def find_binding(input, sgt, prefix, source_, action):
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:
66 def find_binding_with_peer_sequence(input, sgt, prefix, source_, action, node_id, peer_seq):
67 correct_sequence = 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:
79 return found_source and correct_sequence
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>
88 data = templ.substitute({'sgt': sgt, 'prefix': prefix, 'ip': ip})
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">
97 <peer-address>$ip</peer-address>
98 <tcp-port>$port</tcp-port>
99 <password>$password_</password>
101 <version>$version</version>
102 <description>Connection to ISR-G2</description>
104 <hold-time-min-acceptable>45</hold-time-min-acceptable>
105 <keep-alive-time>30</keep-alive-time>
106 <reconciliation-time>120</reconciliation-time>
112 data = templ.substitute(
113 {'ip': ip, 'port': port, 'mode': mode, 'version': version, 'ip_': ip_, 'password_': password_})
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>
123 data = templ.substitute({'address': address, 'port': port, 'node': node})
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">
132 <ip-prefix>$prefix0</ip-prefix>
134 <new-binding xmlns="urn:opendaylight:sxp:controller">
136 <ip-prefix>$prefix1</ip-prefix>
139 data = templ.substitute(
140 {'sgt0': sgt0, 'sgt1': sgt1, 'prefix0': prefix0, 'prefix1': prefix1, 'ip': ip})
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>
150 data = templ.substitute({'sgt': sgt, 'prefix': prefix, 'ip': ip})
154 def get_connections_from_node_xml(ip):
155 templ = Template('''<input>
156 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
158 data = templ.substitute({'ip': ip})
162 def get_bindings_from_node_xml(ip):
163 templ = Template('''<input>
164 <requested-node xmlns="urn:opendaylight:sxp:controller">$ip</requested-node>
166 data = templ.substitute({'ip': ip})