/
corsa_adaptor.py
409 lines (309 loc) · 14.8 KB
/
corsa_adaptor.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# Copyright Sean Donovan, Joaquin Chung
# Corsa-at-GENI project
import logging
import json
from threading import Timer, Lock, Thread
from datetime import datetime, timedelta
from connection_info import ConnectionInfo
from switch import Switch
from neighbor import Neighbor
from bridge import Bridge
from tunnel import Tunnel
from flask import Flask, jsonify, request, make_response
class CorsaAdaptor(object):
''' The CorsaAdaptor is used to run the adaptation work.
'''
def __init__(self, options):
# Setup logging
self._setup_logger(options.logfile, 'adaptor')
# Setup local storage
self.switches = {}
# Should we send anything to switches?
self.no_switch = options.no_switch
# Parse Config File. The following will be set:
# - self.base_url
self._parse_config_file(options.config)
# Setup Flask configuration stuff
self.host = options.host
self.port = options.port
pass
def _setup_logger(self, filename, logname):
''' Internal function for setting up the logger formats. '''
# reused from https://github.com/sdonovan1985/netassay-ryu/blob/master/base/mcm.py
formatter = logging.Formatter('%(asctime)s %(name)-12s: %(levelname)-8s %(message)s')
console = logging.StreamHandler()
console.setLevel(logging.WARNING)
console.setFormatter(formatter)
logfile = logging.FileHandler(filename)
logfile.setLevel(logging.DEBUG)
logfile.setFormatter(formatter)
self.logger = logging.getLogger(logname)
self.logger.setLevel(logging.DEBUG)
self.logger.addHandler(console)
self.logger.addHandler(logfile)
def _parse_config_file(self, config_file):
''' Config file has general configuration, such as href prefix.
Config file has information for Switches
- Name
- Connection info
- List of reserved bridge numbers
- Neighbors
'''
with open(config_file) as data_file:
data = json.load(data_file)
self.base_url = data['adaptor-href-base']
for switch in data['switches']:
switch_name = switch['name']
connection_info = switch['connection-info']
reserved_bridges = switch['reserved-bridges']
neighbors = switch['neighbors']
# Determine the correct href for the switch we're creating
switch_href = self.base_url + "switches/" + switch_name
# Create Switch object
switch = Switch(switch_name, switch_href, self.no_switch)
# Create the ConnectionInfo object and add it to the switch
cxn_info_object = ConnectionInfo(connection_info['address'],
connection_info['rest_key'])
switch.set_connection_info(cxn_info_object)
# Add list of reserved bridges
switch.set_reserved_bridges(reserved_bridges)
# Create all the neighbors and add them to the switch object
for neighbor in neighbors:
neighbor_name = neighbor['name']
neighbor_type = neighbor['type']
neighbor_physport = neighbor['port']
neighbor_vlans = neighbor['vlans']
neighbor_href = switch_href + "/neighbors/" + neighbor_name
neighbor_object = Neighbor(neighbor_name, neighbor_href,
neighbor_vlans, neighbor_physport,
neighbor_type)
switch.add_neighbor(neighbor_object)
# Save off switch
self.switches[switch_name] = switch
def get_switch(self, switch_name):
return self.switches[switch_name]
def get_neighbor(self, switch_name, neighbor_name):
neighbors = self.switches[switch_name].get_neighbors()
for n in neighbors:
if n.get_name() == neighbor_name:
return n
return None
def get_bridge(self, switch_name, bridge_name):
bridges = self.get_switch(switch_name).get_bridges()
for b in bridges:
if b.get_name() == bridge_name:
return b
return None
def get_bridge_by_urn(self, switch_name, bridge_urn):
bridges = self.get_switch(switch_name).get_bridges()
for b in bridges:
if b.get_urn() == bridge_urn:
return b
return None
def get_tunnel(self, switch_name, bridge_name, tunnel_name):
bridge = self.get_bridge(switch_name, bridge_name)
for c in bridge.get_tunnels():
if c.get_name() == tunnel_name:
return c
return None
def main_loop(self):
app = Flask(__name__, static_url_path='', static_folder='')
@app.route('/', strict_slashes=False, methods=['GET'])
def flask_index():
return "Hello World!"
@app.route('/api/v1/switches', strict_slashes=False, methods=['GET'])
def flask_switches_get():
print "flask_switches_get"
sw_dict = {}
for key in self.switches.keys():
sw_dict[key] = self.switches[key].to_json()
print sw_dict
return jsonify(sw_dict)
@app.route('/api/v1/switches/<switch>',
strict_slashes=False, methods=['GET'])
def flask_specific_switch_get(switch):
print "flask_specific_switch_get"
sw = self.get_switch(switch)
return jsonify(sw.to_json())
@app.route('/api/v1/switches/<switch>/neighbors',
strict_slashes=False, methods=['GET'])
def flask_neighbors_get(switch):
print "flask_neighbors_get"
sw = self.get_switch(switch)
n_dict = {}
for n in sw.get_neighbors():
n_dict[n.get_name()] = n.to_json()
return jsonify(n_dict)
# No POST function for Neighbors, only in
@app.route('/api/v1/switches/<switch>/neighbors/<neighbor>',
strict_slashes=False, methods=['GET'])
def flask_specific_neighbor_get(switch, neighbor):
print "flask_specific_neighbor_get"
n = self.get_neighbor(switch, neighbor)
return jsonify(n.to_json())
@app.route('/api/v1/switches/<switch>/bridges',
strict_slashes=False, methods=['GET'])
def flask_bridges_get(switch):
print "flask_bridges_get"
sw = self.get_switch(switch)
b_dict = {}
for b in sw.get_bridges():
b_dict[b.get_name()] = b.to_json()
return jsonify(b_dict)
#curl -i -H "Content-Type: application/json" -X POST -d '{"urn":"qwertyuiopasdfhkgjdflkj", "controller_addr":"1.2.3.4", "controller_port":6633, "dpid":1234}' http://localhost:5000/api/v1/switches/sox-switch/bridges/
@app.route('/api/v1/switches/<switch>/bridges',
strict_slashes=False, methods=['POST'])
def flask_bridges_post(switch):
print "flask_bridges_post"
if (not request.json or not 'urn' in request.json):
return make_response(jsonify({'error':'improper format, requires name %s' % request.json}),
400)
sw = self.get_switch(switch)
br = sw.create_bridge(**request.json)
return jsonify({br.get_name():br.to_json()})
@app.route('/api/v1/switches/<switch>/bridges/urn/<bridge>',
strict_slashes=False, methods=['GET'])
def flask_specific_bridge_by_urn_get(switch, bridge):
print "flask_specific_bridge_by_urn_get"
b = self.get_bridge_by_urn(switch, bridge)
return jsonify(b.to_json())
@app.route('/api/v1/switches/<switch>/bridges/<bridge>',
strict_slashes=False, methods=['GET'])
def flask_specific_bridge_get(switch, bridge):
print "flask_specific_bridge_get"
b = self.get_bridge(switch, bridge)
return jsonify(b.to_json())
#curl -i -H "Content-Type: application/json" -X DELETE http://localhost:5000/api/v1/switches/sox-switch/bridges/br52
@app.route('/api/v1/switches/<switch>/bridges/<bridge>',
strict_slashes=False, methods=['DELETE'])
def flask_specific_bridge_delete(switch, bridge):
print "flask_specific_bridge_delete"
sw = self.get_switch(switch)
sw.remove_bridge(bridge)
return make_response(jsonify({'success':'deleted %s:%s' %
(switch, bridge)}), 204)
@app.route('/api/v1/switches/<switch>/bridges/<bridge>/tunnels',
strict_slashes=False, methods=['GET'])
def flask_tunnels_get(switch, bridge):
print "flask_tunnels_get"
b = self.get_bridge(switch, bridge)
t_dict = {}
for t in b.get_tunnels():
t_dict[t.get_name()] = t.to_json()
return jsonify(t_dict)
#curl -i -H "Content-Type: application/json" -X POST -d '{"dstname":"oxfam", "physport":1, "dstvlan":2000}' http://localhost:5000/api/v1/switches/sox-switch/bridges/br52/tunnels
@app.route('/api/v1/switches/<switch>/bridges/<bridge>/tunnels',
strict_slashes=False, methods=['POST'])
def flask_tunnels_post(switch, bridge):
if (not request.json or
not 'dstname' in request.json or
not 'physport' in request.json or
not 'dstvlan' in request.json):
return make_response(jsonify({'error':'improper format, requires dstname, physport, and dstvlan %s' % request.json}),
400)
br = self.get_bridge(switch, bridge)
t = br.add_tunnel(**request.json)
return jsonify({request.json['dstname']:t.to_json()})
@app.route('/api/v1/switches/<switch>/bridges/<bridge>/tunnels/<tunnel>',
strict_slashes=False, methods=['GET'])
def flask_specific_tunnel_get(switch, bridge, tunnel):
t = self.get_tunnel(switch, bridge, tunnel)
return jsonify(t.to_json())
#curl -i -H "Content-Type: application/json" -X DELETE http://localhost:5000/api/v1/switches/sox-switch/bridges/br52/tunnels/oxfam
@app.route('/api/v1/switches/<switch>/bridges/<bridge>/tunnels/<tunnel>',
strict_slashes=False, methods=['DELETE'])
def flask_specific_tunnel_delete(switch, bridge, tunnel):
br = self.get_bridge(switch, bridge)
br.remove_tunnel(tunnel)
return make_response(jsonify({'success':'deleted %s:%s:%s' %
(switch, bridge, tunnel)}), 204)
#curl http://localhost:5000/corsa/switch/sox-switch/bigredbutton
@app.route('/corsa/switch/<switch>/bigredbutton',
strict_slashes=False, methods=['GET'])
def flask_big_red_button(switch):
print "RECEIVED BIG RED BUTTON CALL FOR %s" % switch
sw = self.get_switch(switch)
sw.big_red_button()
return make_response(jsonify({'success':"All switches and tunnels deleted."}), 200)
#curl http://localhost:5000/corsa/switch/sox-switch/api/v1/bridges/br52/tunnels
@app.route('/corsa/switch/<switch>/<path:path>',
strict_slashes=False, methods=['GET'])
def flask_get_corsa_stuff(switch, path):
print "Corsa API request on switch %s: \"%s\"" % (switch,path)
sw = self.get_switch(switch)
retval = sw.corsa_api_get(path)
return make_response(jsonify(retval), 200)
print "STARTING APP.RUN"
app.run(host=self.host, port=self.port)
# FLASK Endpoints
def testing(adaptor):
# Testing code.
print adaptor.switches
sw = adaptor.get_switch("sox-switch")
print "switch is:\n%s" % sw
print "\n\n\n\n"
print json.dumps(sw.get_bridge_list(), indent=2)
print "\n\n\n\n"
# Create bridge
br = sw.create_bridge("123456789", "10.2.3.4", 6633, "100000")
print "CREATED NEW BRIDGE:\n%s" % sw
print "\n\n\n\n"
print json.dumps(sw.get_bridge_list(), indent=2)
print "\n\n\n\n"
# Create tunnel
print br
br.add_tunnel("temp1", 1, 2000)
br.add_tunnel("temp2", 1, 2001)
print br
# Delete tunnel
br.remove_tunnel("temp1")
print br
# Cleanup!
sw.remove_bridge("123456789")
print sw
print "\n\n\n\n"
print json.dumps(sw.get_bridge_list(), indent=2)
print "\n\n\n\n"
def REST_setup_for_testing(adaptor):
# Add a few bridges and tunnels for REST testing
sw = adaptor.get_switch("sox-switch")
br1 = sw.create_bridge("123456789", "10.2.3.4", 6633, "100000")
br2 = sw.create_bridge("qwertyiu", "10.2.3.4", 6634, "100001")
br1.add_tunnel("temp1", 1, 2000)
br1.add_tunnel("temp2", 1, 2001)
br2.add_tunnel("temp3", 1, 2002)
br2.add_tunnel("temp4", 1, 2003)
br2.add_tunnel("temp5", 1, 2004)
br2.add_tunnel("temp6", 1, 2005)
print "br1 tunnels: %s" % br1.get_tunnels()
print "br2 tunnels: %s" % br2.get_tunnels()
print "bridge_ht: %s" % sw._get_bridge_ht()
print "SETUP COMPLETE"
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-c", "--config-file", dest="config", type=str,
action="store", help="Specifies the configuration file")
parser.add_argument("-l", "--log-file", dest="logfile", type=str,
action="store", help="Specifies the logfile name",
default="corsa_adaptor.log")
parser.add_argument("-H", "--host", dest="host", type=str,
action="store", help="Specifes the host address to use",
default="0.0.0.0")
parser.add_argument("-p", "--port", dest="port", type=int,
action="store", help="Specifes the port to use",
default=5000)
parser.add_argument("-n", "--no-switch", dest="no_switch", default=False,
action="store_true",
help="Run without attached Corsa Switch")
options = parser.parse_args()
print options
if not options.config:
parser.print_help()
exit()
adaptor = CorsaAdaptor(options)
# For manual testing
#REST_setup_for_testing(adaptor)
adaptor.main_loop()
# For semi-automated testing.
#testing(adaptor)