blob: 0306de9b0ef72340c39d43fcf1db3b53c542966f [file] [log] [blame]
Ubuntu82b8a832013-02-06 22:00:11 +00001#! /usr/bin/env python
2import pprint
3import os
4import sys
5import subprocess
6import json
7import argparse
8import io
9import time
10
Masayoshi Kobayashi51011522013-03-27 00:18:12 +000011import re
12
Ubuntu82b8a832013-02-06 22:00:11 +000013from flask import Flask, json, Response, render_template, make_response, request
14
15## Global Var ##
Ubuntuf6ce96c2013-02-07 01:45:07 +000016RestIP="localhost"
Ubuntu82b8a832013-02-06 22:00:11 +000017RestPort=8080
18#DBName="onos-network-map"
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000019
20## Uncomment the desired block based on your testbed environment
21
22# Settings for running on production
23controllers=["onosgui1", "onosgui2", "onosgui3", "onosgui4", "onosgui5", "onosgui6", "onosgui7", "onosgui8"]
24core_switches=["00:00:00:00:ba:5e:ba:11", "00:00:00:00:00:00:ba:12", "00:00:20:4e:7f:51:8a:35", "00:00:00:00:ba:5e:ba:13", "00:00:00:08:a2:08:f9:01", "00:00:00:16:97:08:9a:46"]
25ONOS_GUI3_HOST="http://gui3.onlab.us:8080"
26ONOS_GUI3_CONTROL_HOST="http://gui3.onlab.us:8081"
27
28# Settings for running on dev testbed. Replace dev
29#controllers=["onosdevb1", "onosdevb2", "onosdevb3", "onosdevb4"]
30#core_switches=["00:00:00:00:00:00:01:01", "00:00:00:00:00:00:01:02", "00:00:00:00:00:00:01:03", "00:00:00:00:00:00:01:04", "00:00:00:00:00:00:01:05", "00:00:00:00:00:00:01:06"]
31#ONOS_GUI3_HOST="http://devb-gui.onlab.us:8080"
32#ONOS_GUI3_CONTROL_HOST="http://devb-gui.onlab.us:8080"
33
34ONOS_LOCAL_HOST="http://localhost:8080" ;# for Amazon EC2
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +000035
36nr_flow=0
Ubuntu82b8a832013-02-06 22:00:11 +000037
38DEBUG=1
39pp = pprint.PrettyPrinter(indent=4)
40
41app = Flask(__name__)
42
43## Worker Functions ##
44def log_error(txt):
45 print '%s' % (txt)
46
47def debug(txt):
48 if DEBUG:
49 print '%s' % (txt)
50
51## Rest APIs ##
52### File Fetch ###
53@app.route('/ui/img/<filename>', methods=['GET'])
54@app.route('/img/<filename>', methods=['GET'])
55@app.route('/css/<filename>', methods=['GET'])
56@app.route('/js/models/<filename>', methods=['GET'])
57@app.route('/js/views/<filename>', methods=['GET'])
58@app.route('/js/<filename>', methods=['GET'])
59@app.route('/lib/<filename>', methods=['GET'])
60@app.route('/', methods=['GET'])
61@app.route('/<filename>', methods=['GET'])
62@app.route('/tpl/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000063@app.route('/ons-demo/<filename>', methods=['GET'])
64@app.route('/ons-demo/js/<filename>', methods=['GET'])
65@app.route('/ons-demo/css/<filename>', methods=['GET'])
66@app.route('/ons-demo/assets/<filename>', methods=['GET'])
67@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000068def return_file(filename="index.html"):
69 if request.path == "/":
70 fullpath = "./index.html"
71 else:
72 fullpath = str(request.path)[1:]
73
74 response = make_response(open(fullpath).read())
75 suffix = fullpath.split(".")[-1]
76
77 if suffix == "html" or suffix == "htm":
78 response.headers["Content-type"] = "text/html"
79 elif suffix == "js":
80 response.headers["Content-type"] = "application/javascript"
81 elif suffix == "css":
82 response.headers["Content-type"] = "text/css"
83 elif suffix == "png":
84 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -070085 elif suffix == "svg":
86 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000087
88 return response
89
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000090
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070091@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
92def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
93 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -070094 command = "curl -s %s/gui/link/%s/%s/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, src_dpid, src_port, dst_dpid, dst_port)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070095 print command
96 result = os.popen(command).read()
97 except:
98 print "REST IF has issue"
99 exit
100
101 resp = Response(result, status=200, mimetype='application/json')
102 return resp
103
Paul Greyson8d1c6362013-03-27 13:05:24 -0700104@app.route("/proxy/gui/switch/<cmd>/<dpid>")
105def proxy_switch_status_change(cmd, dpid):
106 try:
107 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
108 print command
109 result = os.popen(command).read()
110 except:
111 print "REST IF has issue"
112 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700113
Paul Greyson8d1c6362013-03-27 13:05:24 -0700114 resp = Response(result, status=200, mimetype='application/json')
115 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700116
Paul Greyson2913af82013-03-27 14:53:17 -0700117@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
118def proxy_controller_status_change(cmd, controller_name):
119 try:
120 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
121 print command
122 result = os.popen(command).read()
123 except:
124 print "REST IF has issue"
125 exit
126
127 resp = Response(result, status=200, mimetype='application/json')
128 return resp
129
Paul Greyson472da4c2013-03-28 11:43:17 -0700130@app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
131def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
132 try:
133 command = "curl -s %s/gui/addflow/%s/%s/%s/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC)
134 print command
135 result = os.popen(command).read()
136 except:
137 print "REST IF has issue"
138 exit
139
140 resp = Response(result, status=200, mimetype='application/json')
141 return resp
142
Paul Greyson6f918402013-03-28 12:18:30 -0700143@app.route("/proxy/gui/delflow/<flow_id>")
144def proxy_del_flow(flow_id):
145 try:
146 command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
147 print command
148 result = os.popen(command).read()
149 except:
150 print "REST IF has issue"
151 exit
152
153 resp = Response(result, status=200, mimetype='application/json')
154 return resp
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000155@app.route("/wm/core/topology/switches/all/json")
156def switches():
157 if request.args.get('proxy') == None:
158 host = ONOS_LOCAL_HOST
159 else:
160 host = ONOS_GUI3_HOST
161
162 try:
163 command = "curl -s %s/wm/core/topology/switches/all/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700164# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000165 result = os.popen(command).read()
166 except:
167 print "REST IF has issue"
168 exit
169
170 resp = Response(result, status=200, mimetype='application/json')
171 return resp
172
173@app.route("/wm/core/topology/links/json")
174def links():
175 if request.args.get('proxy') == None:
176 host = ONOS_LOCAL_HOST
177 else:
178 host = ONOS_GUI3_HOST
179
180 try:
181 command = "curl -s %s/wm/core/topology/links/json" % (host)
Paul Greyson8d1c6362013-03-27 13:05:24 -0700182 print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000183 result = os.popen(command).read()
184 except:
185 print "REST IF has issue"
186 exit
187
188 resp = Response(result, status=200, mimetype='application/json')
189 return resp
190
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000191@app.route("/wm/flow/getsummary/0/0/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000192def flows():
193 if request.args.get('proxy') == None:
194 host = ONOS_LOCAL_HOST
195 else:
196 host = ONOS_GUI3_HOST
197
198 try:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000199 command = "curl -s %s/wm/flow/getsummary/0/0/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700200# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000201 result = os.popen(command).read()
202 except:
203 print "REST IF has issue"
204 exit
205
206 resp = Response(result, status=200, mimetype='application/json')
207 return resp
208
209@app.route("/wm/registry/controllers/json")
210def registry_controllers():
211 if request.args.get('proxy') == None:
212 host = ONOS_LOCAL_HOST
213 else:
214 host = ONOS_GUI3_HOST
215
216 try:
217 command = "curl -s %s/wm/registry/controllers/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700218# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000219 result = os.popen(command).read()
220 except:
221 print "REST IF has issue"
222 exit
223
224 resp = Response(result, status=200, mimetype='application/json')
225 return resp
226
227@app.route("/wm/registry/switches/json")
228def registry_switches():
229 if request.args.get('proxy') == None:
230 host = ONOS_LOCAL_HOST
231 else:
232 host = ONOS_GUI3_HOST
233
234 try:
235 command = "curl -s %s/wm/registry/switches/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700236# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000237 result = os.popen(command).read()
238 except:
239 print "REST IF has issue"
240 exit
241
242 resp = Response(result, status=200, mimetype='application/json')
243 return resp
244
Ubuntu82b8a832013-02-06 22:00:11 +0000245
246def node_id(switch_array, dpid):
247 id = -1
248 for i, val in enumerate(switch_array):
249 if val['name'] == dpid:
250 id = i
251 break
252
253 return id
254
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000255## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000256@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000257def topology_for_gui():
258 try:
259 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
260 result = os.popen(command).read()
261 parsedResult = json.loads(result)
262 except:
263 log_error("REST IF has issue: %s" % command)
264 log_error("%s" % result)
265 sys.exit(0)
266
267 topo = {}
268 switches = []
269 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000270 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000271
272 for v in parsedResult:
273 if v.has_key('dpid'):
274# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
275 dpid = str(v['dpid'])
276 state = str(v['state'])
277 sw = {}
278 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000279 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000280
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000281 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000282 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000283 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000284
Ubuntu37ebda62013-03-01 00:35:31 +0000285## Comment in if we need devies
286# sw_index = len(switches) - 1
287# for p in v['ports']:
288# for d in p['devices']:
289# device = {}
290# device['attached_switch']=dpid
291# device['name']=d['mac']
292# if d['state'] == "ACTIVE":
293# device['group']=1000
294# else:
295# device['group']=1001
296#
297# switches.append(device)
298# device_index = len (switches) -1
299# link = {}
300# link['source'] = device_index
301# link['target'] = sw_index
302# link['type'] = -1
303# links.append(link)
304# link = {}
305# link['source'] = sw_index
306# link['target'] = device_index
307# link['type'] = -1
308# links.append(link)
309
Ubuntu5b2b24a2013-02-27 09:51:13 +0000310# try:
311# command = "curl -s \'http://%s:%s/wm/registry/controllers/json\'" % (RestIP, RestPort)
312# result = os.popen(command).read()
313# controllers = json.loads(result)
314# except:
315# log_error("xx REST IF has issue: %s" % command)
316# log_error("%s" % result)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000317
318 try:
319 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
320 result = os.popen(command).read()
321 parsedResult = json.loads(result)
322 except:
323 log_error("REST IF has issue: %s" % command)
324 log_error("%s" % result)
325
326 for key in parsedResult:
327 dpid = key
328 ctrl = parsedResult[dpid][0]['controllerId']
329 sw_id = node_id(switches, dpid)
330 if sw_id != -1:
331 if switches[sw_id]['group'] != 0:
332 switches[sw_id]['group'] = controllers.index(ctrl) + 1
333
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000334 try:
335 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000336# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000337 p1=1
338 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000339# v2 = "00:00:00:00:00:0d:00:d3"
340 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000341 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
342 result = os.popen(command).read()
343 parsedResult = json.loads(result)
344 except:
345 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000346 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000347
Ubuntu765deff2013-02-28 18:39:13 +0000348 path = []
349 if parsedResult.has_key('flowEntries'):
350 flowEntries= parsedResult['flowEntries']
351 for i, v in enumerate(flowEntries):
352 if i < len(flowEntries) - 1:
353 sdpid= flowEntries[i]['dpid']['value']
354 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700355 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000356
Ubuntu82b8a832013-02-06 22:00:11 +0000357 try:
358 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
359 result = os.popen(command).read()
360 parsedResult = json.loads(result)
361 except:
362 log_error("REST IF has issue: %s" % command)
363 log_error("%s" % result)
364 sys.exit(0)
365
366 for v in parsedResult:
367 link = {}
368 if v.has_key('dst-switch'):
369 dst_dpid = str(v['dst-switch'])
370 dst_id = node_id(switches, dst_dpid)
371 if v.has_key('src-switch'):
372 src_dpid = str(v['src-switch'])
373 src_id = node_id(switches, src_dpid)
374 link['source'] = src_id
375 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000376
377 onpath = 0
378 for (s,d) in path:
379 if s == v['src-switch'] and d == v['dst-switch']:
380 onpath = 1
381 break
382 link['type'] = onpath
383
Ubuntu82b8a832013-02-06 22:00:11 +0000384 links.append(link)
385
386 topo['nodes'] = switches
387 topo['links'] = links
388
Ubuntu37ebda62013-03-01 00:35:31 +0000389 pp.pprint(topo)
Ubuntu82b8a832013-02-06 22:00:11 +0000390 js = json.dumps(topo)
391 resp = Response(js, status=200, mimetype='application/json')
392 return resp
393
Ubuntuaea2a682013-02-08 08:30:10 +0000394#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
395#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
396@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
397def shortest_path(v1, p1, v2, p2):
398 try:
399 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
400 result = os.popen(command).read()
401 parsedResult = json.loads(result)
402 except:
403 log_error("REST IF has issue: %s" % command)
404 log_error("%s" % result)
405 sys.exit(0)
406
407 topo = {}
408 switches = []
409 links = []
410
411 for v in parsedResult:
412 if v.has_key('dpid'):
413 dpid = str(v['dpid'])
414 state = str(v['state'])
415 sw = {}
416 sw['name']=dpid
417 if str(v['state']) == "ACTIVE":
418 if dpid[-2:-1] == "a":
419 sw['group']=1
420 if dpid[-2:-1] == "b":
421 sw['group']=2
422 if dpid[-2:-1] == "c":
423 sw['group']=3
424 if str(v['state']) == "INACTIVE":
425 sw['group']=0
426
427 switches.append(sw)
428
429 try:
430 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
431 result = os.popen(command).read()
432 parsedResult = json.loads(result)
433 except:
434 log_error("No route")
435 parsedResult = []
436# exit(1)
437
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700438 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000439 for i, v in enumerate(parsedResult):
440 if i < len(parsedResult) - 1:
441 sdpid= parsedResult[i]['switch']
442 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700443 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000444
445 try:
446 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
447 result = os.popen(command).read()
448 parsedResult = json.loads(result)
449 except:
450 log_error("REST IF has issue: %s" % command)
451 log_error("%s" % result)
452 sys.exit(0)
453
454 for v in parsedResult:
455 link = {}
456 if v.has_key('dst-switch'):
457 dst_dpid = str(v['dst-switch'])
458 dst_id = node_id(switches, dst_dpid)
459 if v.has_key('src-switch'):
460 src_dpid = str(v['src-switch'])
461 src_id = node_id(switches, src_dpid)
462 link['source'] = src_id
463 link['target'] = dst_id
464 onpath = 0
465 for (s,d) in path:
466 if s == v['src-switch'] and d == v['dst-switch']:
467 onpath = 1
468 break
469
470 link['type'] = onpath
471 links.append(link)
472
473 topo['nodes'] = switches
474 topo['links'] = links
475
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000476# pp.pprint(topo)
Ubuntuaea2a682013-02-08 08:30:10 +0000477 js = json.dumps(topo)
478 resp = Response(js, status=200, mimetype='application/json')
479 return resp
480
Ubuntu82b8a832013-02-06 22:00:11 +0000481@app.route("/wm/core/controller/switches/json")
482def query_switch():
483 try:
484 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
485# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000486 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000487 result = os.popen(command).read()
488 parsedResult = json.loads(result)
489 except:
490 log_error("REST IF has issue: %s" % command)
491 log_error("%s" % result)
492 sys.exit(0)
493
494# print command
495# print result
496 switches_ = []
497 for v in parsedResult:
498 if v.has_key('dpid'):
499 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
500 dpid = str(v['dpid'])
501 state = str(v['state'])
502 sw = {}
503 sw['dpid']=dpid
504 sw['active']=state
505 switches_.append(sw)
506
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000507# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000508 js = json.dumps(switches_)
509 resp = Response(js, status=200, mimetype='application/json')
510 return resp
511
512@app.route("/wm/device/")
513def devices():
514 try:
515 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
516 result = os.popen(command).read()
517 parsedResult = json.loads(result)['results']
518 except:
519 log_error("REST IF has issue: %s" % command)
520 log_error("%s" % result)
521 sys.exit(0)
522
523 devices = []
524 for v in parsedResult:
525 dl_addr = v['dl_addr']
526 nw_addr = v['nw_addr']
527 vertex = v['_id']
528 mac = []
529 mac.append(dl_addr)
530 ip = []
531 ip.append(nw_addr)
532 device = {}
533 device['entryClass']="DefaultEntryClass"
534 device['mac']=mac
535 device['ipv4']=ip
536 device['vlan']=[]
537 device['lastSeen']=0
538 attachpoints =[]
539
540 port, dpid = deviceV_to_attachpoint(vertex)
541 attachpoint = {}
542 attachpoint['port']=port
543 attachpoint['switchDPID']=dpid
544 attachpoints.append(attachpoint)
545 device['attachmentPoint']=attachpoints
546 devices.append(device)
547
548 print devices
549 js = json.dumps(devices)
550 resp = Response(js, status=200, mimetype='application/json')
551 return resp
552
553#{"entityClass":"DefaultEntityClass","mac":["7c:d1:c3:e0:8c:a3"],"ipv4":["192.168.2.102","10.1.10.35"],"vlan":[],"attachmentPoint":[{"port":13,"switchDPID":"00:01:00:12:e2:78:32:44","errorStatus":null}],"lastSeen":1357333593496}
554
Ubuntu82b8a832013-02-06 22:00:11 +0000555## return fake stat for now
556@app.route("/wm/core/switch/<switchId>/<statType>/json")
557def switch_stat(switchId, statType):
558 if statType == "desc":
559 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
560 ret = {}
561 ret[switchId]=desc
562 elif statType == "aggregate":
563 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
564 ret = {}
565 ret[switchId]=aggr
566 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700567 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000568
569 js = json.dumps(ret)
570 resp = Response(js, status=200, mimetype='application/json')
571 return resp
572
573
574@app.route("/wm/topology/links/json")
575def query_links():
576 try:
577 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000578 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000579 result = os.popen(command).read()
580 parsedResult = json.loads(result)['results']
581 except:
582 log_error("REST IF has issue: %s" % command)
583 log_error("%s" % result)
584 sys.exit(0)
585
586 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000587# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000588 sport = []
589 links = []
590 for v in parsedResult:
591 srcport = v['_id']
592 try:
593 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
594 print command
595 result = os.popen(command).read()
596 linkResults = json.loads(result)['results']
597 except:
598 log_error("REST IF has issue: %s" % command)
599 log_error("%s" % result)
600 sys.exit(0)
601
602 for p in linkResults:
603 if p.has_key('type') and p['type'] == "port":
604 dstport = p['_id']
605 (sport, sdpid) = portV_to_port_dpid(srcport)
606 (dport, ddpid) = portV_to_port_dpid(dstport)
607 link = {}
608 link["src-switch"]=sdpid
609 link["src-port"]=sport
610 link["src-port-state"]=0
611 link["dst-switch"]=ddpid
612 link["dst-port"]=dport
613 link["dst-port-state"]=0
614 link["type"]="internal"
615 links.append(link)
616
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000617# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000618 js = json.dumps(links)
619 resp = Response(js, status=200, mimetype='application/json')
620 return resp
621
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700622topo_less = {
623 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000624 {"name" : "00:a0", "group" : 1},
625 {"name" : "00:a1", "group" : 1},
626 {"name" : "00:a2", "group" : 1},
627 ],
628 "links" : [
629 {"source" :0, "target": 1},
630 {"source" :1, "target": 0},
631 {"source" :0, "target": 2},
632 {"source" :2, "target": 0},
633 {"source" :1, "target": 2},
634 {"source" :2, "target": 1},
635 ]
636}
637
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700638topo_more = {
639 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000640 {"name" : "00:a3", "group" : 2},
641 {"name" : "00:a0", "group" : 1},
642 {"name" : "00:a1", "group" : 1},
643 {"name" : "00:a2", "group" : 1},
644 ],
645 "links" : [
646 {"source" :1, "target": 2},
647 {"source" :2, "target": 1},
648 {"source" :1, "target": 3},
649 {"source" :3, "target": 1},
650 {"source" :2, "target": 3},
651 {"source" :3, "target": 2},
652 {"source" :0, "target": 2},
653 ]
654}
655
656@app.route("/topology_more")
657def topology_more():
658 topo = topo_more
659 js = json.dumps(topo)
660 resp = Response(js, status=200, mimetype='application/json')
661 return resp
662
663@app.route("/topology_less")
664def topology_less():
665 topo = topo_less
666 js = json.dumps(topo)
667 resp = Response(js, status=200, mimetype='application/json')
668 return resp
669
670cont_status1 = [
671 {"name":"onos9vpc", "onos": 1, "cassandra": 1},
672 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
673 {"name":"onos11vpc", "onos": 1, "cassandra": 0},
674 {"name":"onos12vpc", "onos": 1, "cassandra": 0}]
675
676cont_status2 = [
677 {"name":"onos9vpc", "onos": 0, "cassandra": 1},
678 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
679 {"name":"onos11vpc", "onos": 0, "cassandra": 1},
680 {"name":"onos12vpc", "onos": 0, "cassandra": 1}]
681
682@app.route("/controller_status1")
683def controller_status1():
684 status = cont_status1
685 js = json.dumps(status)
686 resp = Response(js, status=200, mimetype='application/json')
687 pp.pprint(resp)
688 return resp
689
690@app.route("/controller_status2")
691def controller_status2():
692 status = cont_status2
693 js = json.dumps(status)
694 resp = Response(js, status=200, mimetype='application/json')
695 pp.pprint(resp)
696 return resp
697
Ubuntuc016ba12013-02-27 21:53:41 +0000698@app.route("/controller_status")
699def controller_status():
700 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
701 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
702
703 cont_status=[]
704 for i in controllers:
705 status={}
706 onos=os.popen(onos_check % i).read()[:-1]
707 status["name"]=i
708 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000709 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000710 cont_status.append(status)
711
712 js = json.dumps(cont_status)
713 resp = Response(js, status=200, mimetype='application/json')
714 pp.pprint(js)
715 return resp
716
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000717@app.route("/gui/controller/<cmd>/<controller_name>")
718def controller_status_change(cmd, controller_name):
719 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
720 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
721
722 if cmd == "up":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000723 print start_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000724 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000725 ret = "controller %s is up" % (controller_name)
726 elif cmd == "down":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000727 print stop_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000728 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000729 ret = "controller %s is down" % (controller_name)
730
731 return ret
732
733@app.route("/gui/switch/<cmd>/<dpid>")
734def switch_status_change(cmd, dpid):
735 r = re.compile(':')
736 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000737 host=controllers[0]
738 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
739 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000740 print "cmd_string"
741
742 if cmd =="up" or cmd=="down":
743 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000744 os.popen(cmd_string)
745 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000746
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000747 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000748
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000749#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000750#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000751@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
752def link_up(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000753
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000754 cmd = 'up'
755 result=""
756
Paul Greyson472da4c2013-03-28 11:43:17 -0700757 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000758 if dpid in core_switches:
759 host = controllers[0]
760 src_ports = [1, 2, 3, 4, 5]
761 else:
762 hostid=int(src_dpid.split(':')[-2])
763 host = controllers[hostid-1]
764 if hostid == 2 :
765 src_ports = [51]
766 else :
767 src_ports = [26]
768
769 for port in src_ports :
770 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
771 print cmd_string
772 res=os.popen(cmd_string).read()
773 result = result + ' ' + res
774
775 return result
776
777
778#* Link Down
779#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000780@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000781def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
782
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000783 if src_dpid in core_switches:
784 host = controllers[0]
785 else:
786 hostid=int(src_dpid.split(':')[-2])
787 host = controllers[hostid-1]
788
789 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
790 print cmd_string
791
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000792 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000793
794 return result
795
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000796#* Create Flow
797#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000798#1 FOOBAR 00:00:00:00:00:00:01:01 1 00:00:00:00:00:00:01:0b 1 matchSrcMac 00:00:00:00:00:00 matchDstMac 00:01:00:00:00:00
Masayoshi Kobayashi81f65652013-03-28 21:06:39 +0000799@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000800def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
801 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
802 print command
803 ret = os.popen(command).read()
804 if ret == "":
805 flow_nr=0
806 else:
807 flow_nr=int(ret)
808
809 flow_nr += 1
810 command = "/home/ubuntu/ONOS/web/add_flow.py %d %s %s %s %s %s matchSrcMac %s matchDstMac %s" % (flow_nr, "dummy", src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC)
811 print command
812 errcode = os.popen(command).read()
813 return errcode
814
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000815#* Delete Flow
816#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000817@app.route("/gui/delflow/<flow_id>")
818def del_flow(flow_id):
819 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
820 print command
821 errcode = os.popen(command).read()
822 return errcode
823
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000824#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700825#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
826@app.route("/gui/iperf/start/<flow_id>/<duration>")
827def iperf_start(flow_id,duration):
828 command = "iperf -xCMSV -t%d -i 0.5 -y c -u -c 127.0.0.1 > iperf_%s.out 2>/dev/null &" % (duration, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000829 print command
830 errcode = os.popen(command).read()
831 return errcode
832
833
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000834#* Get Iperf Throughput
835#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000836@app.route("/gui/iperf/rate/<flow_id>")
837def iperf_rate(flow_id):
838 try:
839 command = "curl -s http://%s:%s/iperf_%s.out" % (RestIP, 9000, flow_id)
840 print command
841 result = os.popen(command).read()
842 except:
843 print "REST IF has issue"
844 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000845
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000846 resp = Response(result, status=200, mimetype='text/html')
847 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000848
849
Ubuntu82b8a832013-02-06 22:00:11 +0000850if __name__ == "__main__":
851 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000852# add_flow("00:00:00:00:00:00:02:02", 1, "00:00:00:00:00:00:03:02", 1, "00:00:00:00:02:02", "00:00:00:00:03:0c")
853# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
854# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
855# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
856# link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1)
857
858
Ubuntu82b8a832013-02-06 22:00:11 +0000859 print "-- query all switches --"
860 query_switch()
861 print "-- query topo --"
862 topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000863# link_change(1,2,3,4)
864 print "-- query all links --"
865 query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000866# print "-- query all devices --"
867# devices()
868 else:
869 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000870 app.run(threaded=True, host="0.0.0.0", port=9000)