-
Notifications
You must be signed in to change notification settings - Fork 0
/
epe-controller-base-prefixes.py
288 lines (267 loc) · 9.39 KB
/
epe-controller-base-prefixes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python
import socket
from sys import stdout
from sys import stdin
from time import sleep
from jnpr.junos import Device
from lxml import etree
import re
import os
import copy
import re
import subprocess
import signal
import os
import yaml
labelmap = {}
ConfiguredPeerList = {}
PeerToASBRMap = {}
serviceroutes = dict()
serviceroutesold = dict()
def InitialPeerCheck():
loadlabels()
loadPeerToASBRMap()
loadserviceroutes()
ConfiguredPeerList = ReturnPeerList()
for i in range(0,len(ConfiguredPeerList.keys())):
if ConfiguredPeerList['peer_address'+ str(i)] in labelmap.keys():
return i
else:
while ConfiguredPeerList['peer_address'+ str(i)] not in labelmap.keys():
loadlabels()
loadPeerToASBRMap()
loadserviceroutes()
ConfiguredPeerList = ReturnPeerList()
if ConfiguredPeerList['peer_address'+ str(i)] not in labelmap.keys() and i == int(len(ConfiguredPeerList) - 1):
stdout.write('\n=========================================================================\n\n'"Man you ain't got nuthing going on no-how..."'\n'"Lets just keep rolling until you sort this out......")
stdout.write('\nStart a Global EPE Peer, Bro.....''\n''Or configure another in the Global Configured Peer List File ........''\nIn "ConfiguredEPEPeerList"\n\n\n=========================================================================\n\n\n')
stdout.flush()
sleep(2)
i = 0
continue
elif ConfiguredPeerList['peer_address'+ str(i)] not in labelmap.keys() and i < int(len(ConfiguredPeerList.keys()) - 1):
i += 1
continue
elif i == None:
i += 1
continue
else:
return i
def check_and_add_route():
global serviceroutes
global serviceroutesold
#Load the labels, the service routes, peers and ASBR Mappings
loadlabels()
loadPeerToASBRMap()
loadserviceroutes()
ConfiguredPeerList = {}
ConfiguredPeerList = ReturnPeerList()
i = InitialPeerCheck()
CurrentPeer = 0
while ConfiguredPeerList['peer_address'+ str(i)] in labelmap.keys():
for i in range(0,len(ConfiguredPeerList.keys())):
if ConfiguredPeerList['peer_address'+ str(i)] in labelmap.keys() and labelmap[ConfiguredPeerList['peer_address'+ str(i)]] is not '0' or None or 0:
print('\n====================================================================================================\n\n''Here is the pertinent run-time information\n')
print(labelmap)
print(serviceroutes)
print(ConfiguredPeerList)
print('\n====================================================================================================\n\n')
print ('\n=================================================================================\n\n' 'Using this EPE Peer Currently ' + str(ConfiguredPeerList['peer_address'+ str(i)]) + ' address!!! --->> LABEL:' +str(labelmap[ConfiguredPeerList['peer_address'+ str(i)]])+ '\n')
servicerouteslist = [y for x in serviceroutes.values() for y in x]
servicerouteslistold = [y for x in serviceroutesold.values() for y in x]
if len(servicerouteslistold) == len(servicerouteslist) and cmp(serviceroutes, serviceroutesold) == 0 and CurrentPeer == ConfiguredPeerList['peer_address'+ str(i)]:
print("No Change in the Route Table, still using Peer " + str(ConfiguredPeerList['peer_address'+ str(i)]) + "\nOn Egress ASBR "+ str(PeerToASBRMap[str(ConfiguredPeerList['peer_address'+ str(i)])][0])+' with label [' + str(labelmap[ConfiguredPeerList['peer_address'+ str(i)]]) + ']')
pass
elif CurrentPeer != ConfiguredPeerList['peer_address'+ str(i)]:
v = 0
print("Advertising all current routes "'\n')
print("Current Prefixes Are: ")
for route in servicerouteslist:
print(str(route) + ' ')
print('\n')
for route in servicerouteslist:
stdout.write('announce route ' + str(route) +' next-hop '+ str(PeerToASBRMap[str(ConfiguredPeerList['peer_address'+ str(i)])][0])+' label [' + str(labelmap[ConfiguredPeerList['peer_address'+ str(i)]]) + ']''\n')
stdout.flush()
v += 1
elif len(servicerouteslistold) == 0 and len(servicerouteslist) >= 0:
v = 0
print("Advertising the following newly learned routes from Egress ASBR's: ")
for route in servicerouteslist:
if route not in servicerouteslistold:
print(str(route) +' ')
print('\n')
if route not in servicerouteslistold:
stdout.write('announce route ' + str(route) +' next-hop '+ str(PeerToASBRMap[str(ConfiguredPeerList['peer_address'+ str(i)])][0])+' label [' + str(labelmap[ConfiguredPeerList['peer_address'+ str(i)]]) + ']''\n')
stdout.flush()
v += 1
elif len(servicerouteslistold) >= len(servicerouteslist):
v = 0
print("Removing the following routes no longer Advertised by Egress ASBR's: ")
for route in servicerouteslistold:
if route not in servicerouteslist:
print(str(route) +' ')
print('\n')
if route not in servicerouteslist:
stdout.write('withdraw route ' + str(route) +' next-hop '+ str(PeerToASBRMap[str(ConfiguredPeerList['peer_address'+ str(i)])][0])+' label [800000]''\n')
stdout.flush()
v += 1
elif len(servicerouteslistold) <= len(servicerouteslist):
v = 0
print("Advertising the following newly learned routes from Egress ASBR's: ")
for route in servicerouteslist:
if route not in servicerouteslistold:
print(str(route) +' ')
print('\n')
if route not in servicerouteslistold:
stdout.write('announce route ' + str(route) +' next-hop '+ str(PeerToASBRMap[str(ConfiguredPeerList['peer_address'+ str(i)])][0])+' label [' + str(labelmap[ConfiguredPeerList['peer_address'+ str(i)]]) + ']''\n')
stdout.flush()
v += 1
print('\n\n=================================================================================\n\n\n')
stdout.flush()
CurrentPeer = ConfiguredPeerList['peer_address'+ str(i)]
loadlabels()
loadPeerToASBRMap()
loadserviceroutes()
sleep(5)
break
elif ConfiguredPeerList['peer_address'+ str(i)] in labelmap.keys() and labelmap[ConfiguredPeerList['peer_address'+ str(i)]] == '0' and i < int(len(ConfiguredPeerList.keys()) - 1):
i += 1
elif i < int(len(ConfiguredPeerList.keys()) - 1):
i += 1
else:
servicerouteslist = [y for x in serviceroutes.values() for y in x]
stdout.write('\n========================================================================================\n\n All defined EPE Peers Are Idle.\n Lets just remove the EPE Routes\n\n')
v = 0
for route in servicerouteslist:
stdout.write('withdraw route ' + str(route) +' next-hop '+ str(PeerToASBRMap[str(ConfiguredPeerList['peer_address'+ str(i)])][0])+' label [800000]''\n')
v += 1
stdout.write('\n\n========================================================================================\n\n\n')
stdout.flush()
i = 0
sleep(5)
CurrentPeer = 0
check_and_add_route()
def ReturnPeerList():
script_dir = os.path.dirname(__file__)
rel_path = "RuntimeVariables.yaml"
abs_file_path = os.path.join(script_dir, rel_path)
f = open(abs_file_path,'r')
RuntimeVariables = yaml.load(f.read())
ConfiguredPeerList = copy.deepcopy(RuntimeVariables['ConfiguredPeerList'])
f.close()
return ConfiguredPeerList
def loadPeerToASBRMap():
global PeerToASBRMap
script_dir = os.path.dirname(__file__)
rel_path = "PeerToASBRMapping"
abs_file_path = os.path.join(script_dir, rel_path)
g=open(abs_file_path, "r")
if PeerToASBRMap == {}:
g.close()
sleep(2)
g=open(abs_file_path, "r")
for line in g:
x = line.split(":")
a = x[0]
d = len(a)-3
a = a[0:d]
b = x[1]
c = len(b)-1
b = b[0:c]
try:
PeerToASBRMap[a].append(b)
except KeyError:
PeerToASBRMap[a] = [b]
g.close()
check_and_add_route()
else:
PeerToASBRMap = {}
for line in g:
x = line.split(":")
a = x[0]
d = len(a)-3
a = a[0:d]
b = x[1]
c = len(b)-1
b = b[0:c]
try:
PeerToASBRMap[a].append(b)
except KeyError:
PeerToASBRMap[a] = [b]
g.close()
def loadlabels():
script_dir = os.path.dirname(__file__)
rel_path = "PeerToLabelMapping"
abs_file_path = os.path.join(script_dir, rel_path)
f=open(abs_file_path, "r")
if labelmap == {}:
f.close()
sleep(2)
f=open(abs_file_path, "r")
for line in f:
x = line.split(":")
a = x[0]
d = len(a)-3
a = a[0:d]
b = x[1]
c = len(b)-2
b = b[1:c]
labelmap[a] = b
f.close()
check_and_add_route()
else:
f=open(abs_file_path, "r")
for line in f:
x = line.split(":")
a = x[0]
d = len(a)-3
a = a[0:d]
b = x[1]
c = len(b)-2
b = b[1:c]
labelmap[a] = b
f.close()
def loadserviceroutes():
global serviceroutesold
global serviceroutes
script_dir = os.path.dirname(__file__)
rel_path = "ServicePrefixes"
abs_file_path = os.path.join(script_dir, rel_path)
g=open(abs_file_path, "r")
if serviceroutes == {}:
g.close()
sleep(2)
g=open(abs_file_path, "r")
for line in g:
x = line.split(":")
a = x[0]
b = x[1]
c = len(b)-1
b = b[0:c]
try:
serviceroutes[a].append(b)
except KeyError:
serviceroutes[a] = [b]
g.close()
return serviceroutes
check_and_add_route()
else:
serviceroutesold = copy.deepcopy(serviceroutes)
serviceroutes = {}
for line in g:
x = line.split(":")
a = x[0]
b = x[1]
c = len(b)-1
b = b[0:c]
try:
serviceroutes[a].append(b)
except KeyError:
serviceroutes[a] = [b]
g.close()
return serviceroutes
return serviceroutesold
def main():
check_and_add_route()
if __name__ == "__main__":
main()