-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpce_server.py
271 lines (250 loc) · 9.57 KB
/
pce_server.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
import flask
from flask import request, jsonify, abort, render_template, flash, redirect, url_for
import odl_bgp_ls
import argparse, sys
import logging
import logging.config
from logging.handlers import RotatingFileHandler
from logging import Formatter
logging.basicConfig(filename='logs/odl_bgp.log', level=logging.INFO)
app = flask.Flask(__name__)
app.secret_key = "super secret key"
app.config["DEBUG"] = True
tunnel_vars = {}
path_list = []
pcc = ''
reload(sys)
sys.setdefaultencoding("utf-8")
@app.route('/pce/')
def home():
maininputs = {}
topology = odl_bgp_ls.topology_rep()
if type(topology) == str:
maininputs['nodes'] = []
maininputs['links'] = []
else:
maininputs['nodes'] = topology['nodes']
maininputs['links'] = topology['links']
maininputs['BGP_Instance'] = ''
maininputs['localas'] = ''
maininputs['router_id'] = ''
bgp_instances = odl_bgp_ls.get_bgp_instances()
if bgp_instances and isinstance(bgp_instances, list):
maininputs['BGP_Instance'] = str(bgp_instances[0]['instance_name'])
maininputs['localas'] = str(bgp_instances[0]['AS'])
maininputs['router_id'] = str(bgp_instances[0]['router_id'])
neighbors = ''
if maininputs['BGP_Instance']:
neighbors = odl_bgp_ls.get_bgpls_peers(maininputs['BGP_Instance'])
if isinstance(neighbors, list):
maininputs['neighbors'] = neighbors
pccs = odl_bgp_ls.get_pcc_list()
if isinstance(pccs, list):
maininputs['pccs'] = pccs
return render_template('home1.html', **maininputs)
@app.route('/pce/possible_lsps/', methods=['POST'])
def lsps_with_constraints():
global tunnel_vars, path_list
tunnel_vars = {
"pcc" : request.form['pcc'].strip(),
"lsp_name" : request.form['lsp_name'].strip(),
"src_ip" : request.form['src_ip'].strip(),
"dest_ip" : request.form['dest_ip'].strip(),
"setup_priority" : request.form['setup_priority'].strip(),
"hold_priority" : request.form['hold_priority'].strip(),
"bandwidth" : request.form['bandwidth'].strip(),
"color_list" : request.form['color_list'].split(",")
}
path_list = odl_bgp_ls.possible_lsps(**tunnel_vars)
if type(path_list) == str:
flash(path_list)
return redirect(url_for('home'))
else:
logging.info("Possible LSPS calculated successfully")
for path in path_list:
path['next_hop_list'] = ",".join(path['next_hop_list'])
inputs={'path_list':path_list}
return render_template('lsp.html', **inputs)
@app.route('/pce/igp_paths/', methods=['POST'])
def igp_paths():
if not request.json:
abort(400)
vars = request.json
path_list = odl_bgp_ls.shortest_igp_paths(**vars)
if type(path_list) == str:
logging.error(path_list)
return jsonify(Error = path_list), 400
else:
logging.info("IGP path calculated successfully")
return jsonify(shortest_paths = path_list)
@app.route('/pce/push_lsp/', methods=['POST'])
def push_lsp():
path_id = request.form['path_id']
variables = tunnel_vars
variables['ero_prefix'] = path_list[int(path_id)-1]['next_hop_list'].split(",")[:]
response = odl_bgp_ls.push_lsp(**variables)
flash(response)
return redirect(url_for('home'))
@app.route('/pce/remove_lsp/', methods=['POST'])
def remove_lsp():
variables = {
"pcc" : pcc,
"lsp_name" : request.form['lsp_name']
}
response = odl_bgp_ls.remove_lsp(**variables)
flash(response)
return redirect(url_for('home'))
@app.route('/pce/pcc_list/', methods=['GET'])
def get_pcc_list():
pcc_list = odl_bgp_ls.get_pcc_list()
if type(pcc_list) == str:
flash(pcc_list)
return redirect(url_for('home'))
else:
logging.info("Successfully got PCC list")
return jsonify(pcc_list = pcc_list)
@app.route('/pce/bgp_instances/', methods=['GET'])
def get_bgp_instances():
bgp_instances = odl_bgp_ls.get_bgp_instances()
if type(bgp_instances) == str:
logging.error(bgp_instances)
return jsonify(Error = bgp_instances), 400
elif not bgp_instances:
logging.error("No BGP-LS instance configured")
return jsonify(Error = "There is no BGP-LS instance configured. BGP-LS instance with atleast one peer is required"), 400
else:
logging.info("BGP Instance information fetched successfully")
return jsonify(bgp_instances = bgp_instances)
@app.route('/pce/bgp_instances/', methods=['POST'])
def add_bgp_instances():
instances = odl_bgp_ls.get_bgp_instances()
bgp_instance = ''
if type(instances) == str and instances == "No BGP Instance configured in ODL":
vars = {}
vars["instance_name"] = str(request.form["instance_name"].strip())
vars["router_id"] = str(request.form["router_id"].strip())
vars["local_as"] = int(request.form["local_as"].strip())
response = odl_bgp_ls.add_bgp_instance(**vars)
flash(response)
return redirect(url_for('home'))
if type(instances) == str:
flash("Unable to get configuration from ODL")
return redirect(url_for('home'))
if instances:
flash("Already one BGP Instance exists. Please delete the existing BGP instance first")
return redirect(url_for('home'))
@app.route('/pce/bgp_instances/<instance_name>', methods=['POST'])
def del_bgp_instances(instance_name):
response = odl_bgp_ls.del_bgp_instance(instance_name)
flash(response)
return redirect(url_for('home'))
@app.route('/pce/established_lsps/', methods=['POST'])
def established_lsps():
try:
global pcc
pcc = request.form["pcclient"].strip()
except KeyError as e:
logging.error(e)
flash("Select a PCC first")
return redirect(url_for('home'))
established_lsps = odl_bgp_ls.get_reported_lsp(pcc)
if type(established_lsps) == str:
flash(established_lsps)
return redirect(url_for('home'))
else:
lsps = {'established_lsps' : established_lsps}
return render_template('estblsps.html', **lsps)
@app.route('/pce/get-ls-peers/', methods=['GET'])
def get_bgpls_peer():
instances = odl_bgp_ls.get_bgp_instances()
bgp_instance = ''
if type(instances) == str:
return jsonify(Error = instances), 400
if not instances:
return jsonify(Error = "Create BGP Instance first"), 400
for inst in instances:
if inst['instance_name'] != 'example-bgp-rib':
bgp_instance = inst['instance_name']
break
if not bgp_instance:
return jsonify(Error = "Create BGP Instance first"), 400
peer_list = odl_bgp_ls.get_bgpls_peers(bgp_instance)
if type(peer_list) == str:
return jsonify(Error = peer_list), 400
else:
return jsonify(neighbors = peer_list)
@app.route('/pce/add-ls-peer/', methods=['POST'])
def add_bgpls_peer():
bgp_instance = ''
instances = odl_bgp_ls.get_bgp_instances()
if type(instances) == str:
flash(instances)
return redirect(url_for('home'))
if not instances:
flash("Create BGP Instance first")
return redirect(url_for('home'))
for inst in instances:
if inst['instance_name'] != 'example-bgp-rib':
bgp_instance = inst['instance_name']
break
if not bgp_instance:
flash("Create BGP Instance first")
return redirect(url_for('home'))
vars={}
vars['bgp_instance'] = bgp_instance
vars['peer_ip'] = request.form['peer_ip'].strip()
vars['hold_time'] = "90"
vars['connectretry_timer'] = "10"
vars['remote_port'] = "179"
vars['passive_mode'] = "false"
response = odl_bgp_ls.add_bgpls_neigh(**vars)
flash(response)
return redirect(url_for('home'))
@app.route('/pce/del-ls-peer/', methods=['POST'])
def del_bgpls_peer():
instances = odl_bgp_ls.get_bgp_instances()
bgp_instance = ''
if type(instances) == str:
flash(instances)
return redirect(url_for('home'))
if not instances:
flash("Create BGP Instance first")
return redirect(url_for('home'))
for inst in instances:
if inst['instance_name'] != 'example-bgp-rib':
bgp_instance = inst['instance_name']
break
if not bgp_instance:
flash("Create BGP Instance first")
return redirect(url_for('home'))
try:
vars = {
"bgp_instance" : bgp_instance,
"peer_ip" : request.form['peer_ip']
}
except KeyError as e:
logging.error(e)
flash("Select a peer to be deleted")
return redirect(url_for('home'))
response = odl_bgp_ls.del_bgpls_neigh(**vars)
flash(response)
return redirect(url_for('home'))
@app.route('/pce/topology_reset/', methods=['POST'])
def Topology_reset():
instances = odl_bgp_ls.get_bgp_instances()
bgp_instance = ''
for inst in instances:
if inst['instance_name'] != 'example-bgp-rib':
bgp_instance = inst['instance_name']
break
if not bgp_instance:
flash("Create BGP Instance first")
return redirect(url_for('home'))
vars = {
"instance_name" : bgp_instance
}
response = odl_bgp_ls.topology_reset(**vars)
flash(response)
return redirect(url_for('home'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9521)