blob: e7e1d83b0f7f67aa1c8c65ed4eb3acc6be428e86 [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'])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +000060@app.route('/log/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000061@app.route('/', methods=['GET'])
62@app.route('/<filename>', methods=['GET'])
63@app.route('/tpl/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000064@app.route('/ons-demo/<filename>', methods=['GET'])
65@app.route('/ons-demo/js/<filename>', methods=['GET'])
66@app.route('/ons-demo/css/<filename>', methods=['GET'])
67@app.route('/ons-demo/assets/<filename>', methods=['GET'])
68@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000069def return_file(filename="index.html"):
70 if request.path == "/":
71 fullpath = "./index.html"
72 else:
73 fullpath = str(request.path)[1:]
74
75 response = make_response(open(fullpath).read())
76 suffix = fullpath.split(".")[-1]
77
78 if suffix == "html" or suffix == "htm":
79 response.headers["Content-type"] = "text/html"
80 elif suffix == "js":
81 response.headers["Content-type"] = "application/javascript"
82 elif suffix == "css":
83 response.headers["Content-type"] = "text/css"
84 elif suffix == "png":
85 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -070086 elif suffix == "svg":
87 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000088
89 return response
90
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000091
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070092@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
93def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
94 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -070095 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 -070096 print command
97 result = os.popen(command).read()
98 except:
99 print "REST IF has issue"
100 exit
101
102 resp = Response(result, status=200, mimetype='application/json')
103 return resp
104
Paul Greyson8d1c6362013-03-27 13:05:24 -0700105@app.route("/proxy/gui/switch/<cmd>/<dpid>")
106def proxy_switch_status_change(cmd, dpid):
107 try:
108 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
109 print command
110 result = os.popen(command).read()
111 except:
112 print "REST IF has issue"
113 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700114
Paul Greyson8d1c6362013-03-27 13:05:24 -0700115 resp = Response(result, status=200, mimetype='application/json')
116 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700117
Paul Greyson2913af82013-03-27 14:53:17 -0700118@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
119def proxy_controller_status_change(cmd, controller_name):
120 try:
121 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
122 print command
123 result = os.popen(command).read()
124 except:
125 print "REST IF has issue"
126 exit
127
128 resp = Response(result, status=200, mimetype='application/json')
129 return resp
130
Paul Greyson472da4c2013-03-28 11:43:17 -0700131@app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
132def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
133 try:
134 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)
135 print command
136 result = os.popen(command).read()
137 except:
138 print "REST IF has issue"
139 exit
140
141 resp = Response(result, status=200, mimetype='application/json')
142 return resp
143
Paul Greyson6f918402013-03-28 12:18:30 -0700144@app.route("/proxy/gui/delflow/<flow_id>")
145def proxy_del_flow(flow_id):
146 try:
147 command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
148 print command
149 result = os.popen(command).read()
150 except:
151 print "REST IF has issue"
152 exit
153
154 resp = Response(result, status=200, mimetype='application/json')
155 return resp
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000156@app.route("/wm/core/topology/switches/all/json")
157def switches():
158 if request.args.get('proxy') == None:
159 host = ONOS_LOCAL_HOST
160 else:
161 host = ONOS_GUI3_HOST
162
163 try:
164 command = "curl -s %s/wm/core/topology/switches/all/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700165# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000166 result = os.popen(command).read()
167 except:
168 print "REST IF has issue"
169 exit
170
171 resp = Response(result, status=200, mimetype='application/json')
172 return resp
173
174@app.route("/wm/core/topology/links/json")
175def links():
176 if request.args.get('proxy') == None:
177 host = ONOS_LOCAL_HOST
178 else:
179 host = ONOS_GUI3_HOST
180
181 try:
182 command = "curl -s %s/wm/core/topology/links/json" % (host)
Paul Greyson8d1c6362013-03-27 13:05:24 -0700183 print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000184 result = os.popen(command).read()
185 except:
186 print "REST IF has issue"
187 exit
188
189 resp = Response(result, status=200, mimetype='application/json')
190 return resp
191
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000192@app.route("/wm/flow/getsummary/0/0/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000193def flows():
194 if request.args.get('proxy') == None:
195 host = ONOS_LOCAL_HOST
196 else:
197 host = ONOS_GUI3_HOST
198
199 try:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000200 command = "curl -s %s/wm/flow/getsummary/0/0/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700201# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000202 result = os.popen(command).read()
203 except:
204 print "REST IF has issue"
205 exit
206
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000207
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000208 resp = Response(result, status=200, mimetype='application/json')
209 return resp
210
211@app.route("/wm/registry/controllers/json")
212def registry_controllers():
213 if request.args.get('proxy') == None:
214 host = ONOS_LOCAL_HOST
215 else:
216 host = ONOS_GUI3_HOST
217
218 try:
219 command = "curl -s %s/wm/registry/controllers/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700220# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000221 result = os.popen(command).read()
222 except:
223 print "REST IF has issue"
224 exit
225
226 resp = Response(result, status=200, mimetype='application/json')
227 return resp
228
229@app.route("/wm/registry/switches/json")
230def registry_switches():
231 if request.args.get('proxy') == None:
232 host = ONOS_LOCAL_HOST
233 else:
234 host = ONOS_GUI3_HOST
235
236 try:
237 command = "curl -s %s/wm/registry/switches/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700238# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000239 result = os.popen(command).read()
240 except:
241 print "REST IF has issue"
242 exit
243
244 resp = Response(result, status=200, mimetype='application/json')
245 return resp
246
Ubuntu82b8a832013-02-06 22:00:11 +0000247
248def node_id(switch_array, dpid):
249 id = -1
250 for i, val in enumerate(switch_array):
251 if val['name'] == dpid:
252 id = i
253 break
254
255 return id
256
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000257## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000258@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000259def topology_for_gui():
260 try:
261 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
262 result = os.popen(command).read()
263 parsedResult = json.loads(result)
264 except:
265 log_error("REST IF has issue: %s" % command)
266 log_error("%s" % result)
267 sys.exit(0)
268
269 topo = {}
270 switches = []
271 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000272 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000273
274 for v in parsedResult:
275 if v.has_key('dpid'):
276# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
277 dpid = str(v['dpid'])
278 state = str(v['state'])
279 sw = {}
280 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000281 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000282
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000283 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000284 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000285 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000286
Ubuntu37ebda62013-03-01 00:35:31 +0000287## Comment in if we need devies
288# sw_index = len(switches) - 1
289# for p in v['ports']:
290# for d in p['devices']:
291# device = {}
292# device['attached_switch']=dpid
293# device['name']=d['mac']
294# if d['state'] == "ACTIVE":
295# device['group']=1000
296# else:
297# device['group']=1001
298#
299# switches.append(device)
300# device_index = len (switches) -1
301# link = {}
302# link['source'] = device_index
303# link['target'] = sw_index
304# link['type'] = -1
305# links.append(link)
306# link = {}
307# link['source'] = sw_index
308# link['target'] = device_index
309# link['type'] = -1
310# links.append(link)
311
Ubuntu5b2b24a2013-02-27 09:51:13 +0000312# try:
313# command = "curl -s \'http://%s:%s/wm/registry/controllers/json\'" % (RestIP, RestPort)
314# result = os.popen(command).read()
315# controllers = json.loads(result)
316# except:
317# log_error("xx REST IF has issue: %s" % command)
318# log_error("%s" % result)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000319
320 try:
321 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
322 result = os.popen(command).read()
323 parsedResult = json.loads(result)
324 except:
325 log_error("REST IF has issue: %s" % command)
326 log_error("%s" % result)
327
328 for key in parsedResult:
329 dpid = key
330 ctrl = parsedResult[dpid][0]['controllerId']
331 sw_id = node_id(switches, dpid)
332 if sw_id != -1:
333 if switches[sw_id]['group'] != 0:
334 switches[sw_id]['group'] = controllers.index(ctrl) + 1
335
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000336 try:
337 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000338# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000339 p1=1
340 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000341# v2 = "00:00:00:00:00:0d:00:d3"
342 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000343 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
344 result = os.popen(command).read()
345 parsedResult = json.loads(result)
346 except:
347 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000348 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000349
Ubuntu765deff2013-02-28 18:39:13 +0000350 path = []
351 if parsedResult.has_key('flowEntries'):
352 flowEntries= parsedResult['flowEntries']
353 for i, v in enumerate(flowEntries):
354 if i < len(flowEntries) - 1:
355 sdpid= flowEntries[i]['dpid']['value']
356 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700357 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000358
Ubuntu82b8a832013-02-06 22:00:11 +0000359 try:
360 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
361 result = os.popen(command).read()
362 parsedResult = json.loads(result)
363 except:
364 log_error("REST IF has issue: %s" % command)
365 log_error("%s" % result)
366 sys.exit(0)
367
368 for v in parsedResult:
369 link = {}
370 if v.has_key('dst-switch'):
371 dst_dpid = str(v['dst-switch'])
372 dst_id = node_id(switches, dst_dpid)
373 if v.has_key('src-switch'):
374 src_dpid = str(v['src-switch'])
375 src_id = node_id(switches, src_dpid)
376 link['source'] = src_id
377 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000378
379 onpath = 0
380 for (s,d) in path:
381 if s == v['src-switch'] and d == v['dst-switch']:
382 onpath = 1
383 break
384 link['type'] = onpath
385
Ubuntu82b8a832013-02-06 22:00:11 +0000386 links.append(link)
387
388 topo['nodes'] = switches
389 topo['links'] = links
390
Ubuntu37ebda62013-03-01 00:35:31 +0000391 pp.pprint(topo)
Ubuntu82b8a832013-02-06 22:00:11 +0000392 js = json.dumps(topo)
393 resp = Response(js, status=200, mimetype='application/json')
394 return resp
395
Ubuntuaea2a682013-02-08 08:30:10 +0000396#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
397#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
398@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
399def shortest_path(v1, p1, v2, p2):
400 try:
401 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
402 result = os.popen(command).read()
403 parsedResult = json.loads(result)
404 except:
405 log_error("REST IF has issue: %s" % command)
406 log_error("%s" % result)
407 sys.exit(0)
408
409 topo = {}
410 switches = []
411 links = []
412
413 for v in parsedResult:
414 if v.has_key('dpid'):
415 dpid = str(v['dpid'])
416 state = str(v['state'])
417 sw = {}
418 sw['name']=dpid
419 if str(v['state']) == "ACTIVE":
420 if dpid[-2:-1] == "a":
421 sw['group']=1
422 if dpid[-2:-1] == "b":
423 sw['group']=2
424 if dpid[-2:-1] == "c":
425 sw['group']=3
426 if str(v['state']) == "INACTIVE":
427 sw['group']=0
428
429 switches.append(sw)
430
431 try:
432 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
433 result = os.popen(command).read()
434 parsedResult = json.loads(result)
435 except:
436 log_error("No route")
437 parsedResult = []
438# exit(1)
439
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700440 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000441 for i, v in enumerate(parsedResult):
442 if i < len(parsedResult) - 1:
443 sdpid= parsedResult[i]['switch']
444 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700445 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000446
447 try:
448 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
449 result = os.popen(command).read()
450 parsedResult = json.loads(result)
451 except:
452 log_error("REST IF has issue: %s" % command)
453 log_error("%s" % result)
454 sys.exit(0)
455
456 for v in parsedResult:
457 link = {}
458 if v.has_key('dst-switch'):
459 dst_dpid = str(v['dst-switch'])
460 dst_id = node_id(switches, dst_dpid)
461 if v.has_key('src-switch'):
462 src_dpid = str(v['src-switch'])
463 src_id = node_id(switches, src_dpid)
464 link['source'] = src_id
465 link['target'] = dst_id
466 onpath = 0
467 for (s,d) in path:
468 if s == v['src-switch'] and d == v['dst-switch']:
469 onpath = 1
470 break
471
472 link['type'] = onpath
473 links.append(link)
474
475 topo['nodes'] = switches
476 topo['links'] = links
477
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000478# pp.pprint(topo)
Ubuntuaea2a682013-02-08 08:30:10 +0000479 js = json.dumps(topo)
480 resp = Response(js, status=200, mimetype='application/json')
481 return resp
482
Ubuntu82b8a832013-02-06 22:00:11 +0000483@app.route("/wm/core/controller/switches/json")
484def query_switch():
485 try:
486 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
487# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000488 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000489 result = os.popen(command).read()
490 parsedResult = json.loads(result)
491 except:
492 log_error("REST IF has issue: %s" % command)
493 log_error("%s" % result)
494 sys.exit(0)
495
496# print command
497# print result
498 switches_ = []
499 for v in parsedResult:
500 if v.has_key('dpid'):
501 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
502 dpid = str(v['dpid'])
503 state = str(v['state'])
504 sw = {}
505 sw['dpid']=dpid
506 sw['active']=state
507 switches_.append(sw)
508
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000509# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000510 js = json.dumps(switches_)
511 resp = Response(js, status=200, mimetype='application/json')
512 return resp
513
514@app.route("/wm/device/")
515def devices():
516 try:
517 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
518 result = os.popen(command).read()
519 parsedResult = json.loads(result)['results']
520 except:
521 log_error("REST IF has issue: %s" % command)
522 log_error("%s" % result)
523 sys.exit(0)
524
525 devices = []
526 for v in parsedResult:
527 dl_addr = v['dl_addr']
528 nw_addr = v['nw_addr']
529 vertex = v['_id']
530 mac = []
531 mac.append(dl_addr)
532 ip = []
533 ip.append(nw_addr)
534 device = {}
535 device['entryClass']="DefaultEntryClass"
536 device['mac']=mac
537 device['ipv4']=ip
538 device['vlan']=[]
539 device['lastSeen']=0
540 attachpoints =[]
541
542 port, dpid = deviceV_to_attachpoint(vertex)
543 attachpoint = {}
544 attachpoint['port']=port
545 attachpoint['switchDPID']=dpid
546 attachpoints.append(attachpoint)
547 device['attachmentPoint']=attachpoints
548 devices.append(device)
549
550 print devices
551 js = json.dumps(devices)
552 resp = Response(js, status=200, mimetype='application/json')
553 return resp
554
555#{"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}
556
Ubuntu82b8a832013-02-06 22:00:11 +0000557## return fake stat for now
558@app.route("/wm/core/switch/<switchId>/<statType>/json")
559def switch_stat(switchId, statType):
560 if statType == "desc":
561 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
562 ret = {}
563 ret[switchId]=desc
564 elif statType == "aggregate":
565 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
566 ret = {}
567 ret[switchId]=aggr
568 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700569 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000570
571 js = json.dumps(ret)
572 resp = Response(js, status=200, mimetype='application/json')
573 return resp
574
575
576@app.route("/wm/topology/links/json")
577def query_links():
578 try:
579 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000580 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000581 result = os.popen(command).read()
582 parsedResult = json.loads(result)['results']
583 except:
584 log_error("REST IF has issue: %s" % command)
585 log_error("%s" % result)
586 sys.exit(0)
587
588 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000589# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000590 sport = []
591 links = []
592 for v in parsedResult:
593 srcport = v['_id']
594 try:
595 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
596 print command
597 result = os.popen(command).read()
598 linkResults = json.loads(result)['results']
599 except:
600 log_error("REST IF has issue: %s" % command)
601 log_error("%s" % result)
602 sys.exit(0)
603
604 for p in linkResults:
605 if p.has_key('type') and p['type'] == "port":
606 dstport = p['_id']
607 (sport, sdpid) = portV_to_port_dpid(srcport)
608 (dport, ddpid) = portV_to_port_dpid(dstport)
609 link = {}
610 link["src-switch"]=sdpid
611 link["src-port"]=sport
612 link["src-port-state"]=0
613 link["dst-switch"]=ddpid
614 link["dst-port"]=dport
615 link["dst-port-state"]=0
616 link["type"]="internal"
617 links.append(link)
618
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000619# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000620 js = json.dumps(links)
621 resp = Response(js, status=200, mimetype='application/json')
622 return resp
623
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700624topo_less = {
625 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000626 {"name" : "00:a0", "group" : 1},
627 {"name" : "00:a1", "group" : 1},
628 {"name" : "00:a2", "group" : 1},
629 ],
630 "links" : [
631 {"source" :0, "target": 1},
632 {"source" :1, "target": 0},
633 {"source" :0, "target": 2},
634 {"source" :2, "target": 0},
635 {"source" :1, "target": 2},
636 {"source" :2, "target": 1},
637 ]
638}
639
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700640topo_more = {
641 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000642 {"name" : "00:a3", "group" : 2},
643 {"name" : "00:a0", "group" : 1},
644 {"name" : "00:a1", "group" : 1},
645 {"name" : "00:a2", "group" : 1},
646 ],
647 "links" : [
648 {"source" :1, "target": 2},
649 {"source" :2, "target": 1},
650 {"source" :1, "target": 3},
651 {"source" :3, "target": 1},
652 {"source" :2, "target": 3},
653 {"source" :3, "target": 2},
654 {"source" :0, "target": 2},
655 ]
656}
657
658@app.route("/topology_more")
659def topology_more():
660 topo = topo_more
661 js = json.dumps(topo)
662 resp = Response(js, status=200, mimetype='application/json')
663 return resp
664
665@app.route("/topology_less")
666def topology_less():
667 topo = topo_less
668 js = json.dumps(topo)
669 resp = Response(js, status=200, mimetype='application/json')
670 return resp
671
672cont_status1 = [
673 {"name":"onos9vpc", "onos": 1, "cassandra": 1},
674 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
675 {"name":"onos11vpc", "onos": 1, "cassandra": 0},
676 {"name":"onos12vpc", "onos": 1, "cassandra": 0}]
677
678cont_status2 = [
679 {"name":"onos9vpc", "onos": 0, "cassandra": 1},
680 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
681 {"name":"onos11vpc", "onos": 0, "cassandra": 1},
682 {"name":"onos12vpc", "onos": 0, "cassandra": 1}]
683
684@app.route("/controller_status1")
685def controller_status1():
686 status = cont_status1
687 js = json.dumps(status)
688 resp = Response(js, status=200, mimetype='application/json')
689 pp.pprint(resp)
690 return resp
691
692@app.route("/controller_status2")
693def controller_status2():
694 status = cont_status2
695 js = json.dumps(status)
696 resp = Response(js, status=200, mimetype='application/json')
697 pp.pprint(resp)
698 return resp
699
Ubuntuc016ba12013-02-27 21:53:41 +0000700@app.route("/controller_status")
701def controller_status():
702 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
703 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
704
705 cont_status=[]
706 for i in controllers:
707 status={}
708 onos=os.popen(onos_check % i).read()[:-1]
709 status["name"]=i
710 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000711 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000712 cont_status.append(status)
713
714 js = json.dumps(cont_status)
715 resp = Response(js, status=200, mimetype='application/json')
716 pp.pprint(js)
717 return resp
718
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000719@app.route("/gui/controller/<cmd>/<controller_name>")
720def controller_status_change(cmd, controller_name):
721 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
722 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
723
724 if cmd == "up":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000725 print start_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000726 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000727 ret = "controller %s is up" % (controller_name)
728 elif cmd == "down":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000729 print stop_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000730 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000731 ret = "controller %s is down" % (controller_name)
732
733 return ret
734
735@app.route("/gui/switch/<cmd>/<dpid>")
736def switch_status_change(cmd, dpid):
737 r = re.compile(':')
738 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000739 host=controllers[0]
740 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
741 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000742 print "cmd_string"
743
744 if cmd =="up" or cmd=="down":
745 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000746 os.popen(cmd_string)
747 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000748
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000749 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000750
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000751#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000752#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000753@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
754def link_up(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000755
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000756 cmd = 'up'
757 result=""
758
Paul Greyson472da4c2013-03-28 11:43:17 -0700759 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000760 if dpid in core_switches:
761 host = controllers[0]
762 src_ports = [1, 2, 3, 4, 5]
763 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000764 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000765 host = controllers[hostid-1]
766 if hostid == 2 :
767 src_ports = [51]
768 else :
769 src_ports = [26]
770
771 for port in src_ports :
772 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
773 print cmd_string
774 res=os.popen(cmd_string).read()
775 result = result + ' ' + res
776
777 return result
778
779
780#* Link Down
781#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000782@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000783def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
784
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000785 if src_dpid in core_switches:
786 host = controllers[0]
787 else:
788 hostid=int(src_dpid.split(':')[-2])
789 host = controllers[hostid-1]
790
791 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
792 print cmd_string
793
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000794 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000795
796 return result
797
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000798#* Create Flow
799#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000800#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 +0000801@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000802def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
803 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
804 print command
805 ret = os.popen(command).read()
806 if ret == "":
807 flow_nr=0
808 else:
809 flow_nr=int(ret)
810
811 flow_nr += 1
Umesh Krishnaswamyf22d3ed2013-04-03 11:57:54 -0700812 command = "/home/ubuntu/ONOS/web/add_flow.py -m onos %d %s %s %s %s %s matchSrcMac %s matchDstMac %s" % (flow_nr, "dummy", src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000813 print command
814 errcode = os.popen(command).read()
815 return errcode
816
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000817#* Delete Flow
818#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000819@app.route("/gui/delflow/<flow_id>")
820def del_flow(flow_id):
821 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
822 print command
823 errcode = os.popen(command).read()
824 return errcode
825
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000826#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700827#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000828@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
829def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000830 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000831 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000832 print command
833 result = os.popen(command).read()
834 if len(result) == 0:
835 print "No Flow found"
836 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000837 except:
838 print "REST IF has issue"
839 exit
840
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000841 parsedResult = json.loads(result)
842
843 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000844 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
845 src_port = parsedResult['dataPath']['srcPort']['port']['value']
846 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
847 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000848# print "FlowPath: (flowId = %s src = %s/%s dst = %s/%s" % (flowId, src_dpid, src_port, dst_dpid, dst_port)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000849
850 if src_dpid in core_switches:
851 host = controllers[0]
852 else:
853 hostid=int(src_dpid.split(':')[-2])
854 host = controllers[hostid-1]
855
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000856# ./runiperf.sh 2 00:00:00:00:00:00:02:02 1 00:00:00:00:00:00:03:02 1 100 15
857 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./runiperf.sh %d %s %s %s %s %s %s'" % (host, flowId, src_dpid, src_port, dst_dpid, dst_port, duration, samples)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000858 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000859 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000860
861 return
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000862
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000863#* Get Iperf Throughput
864#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000865@app.route("/gui/iperf/rate/<flow_id>")
866def iperf_rate(flow_id):
867 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000868 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
869 print command
870 result = os.popen(command).read()
871 if len(result) == 0:
872 print "No Flow found"
873 return;
874 except:
875 print "REST IF has issue"
876 exit
877
878 parsedResult = json.loads(result)
879
880 flowId = int(parsedResult['flowId']['value'], 16)
881 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
882 src_port = parsedResult['dataPath']['srcPort']['port']['value']
883 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
884 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
885
886 if src_dpid in core_switches:
887 host = controllers[0]
888 else:
889 hostid=int(src_dpid.split(':')[-2])
890 host = controllers[hostid-1]
891
892 try:
893 command = "curl -s http://%s:%s/log/iperf_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000894 print command
895 result = os.popen(command).read()
896 except:
897 print "REST IF has issue"
898 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000899
Masayoshi Kobayashi911f2632013-04-01 18:44:33 +0000900 resp = Response(result, status=200, mimetype='application/json')
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000901 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000902
903
Ubuntu82b8a832013-02-06 22:00:11 +0000904if __name__ == "__main__":
905 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000906# 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")
907# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
908# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
909# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
910# link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1)
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000911# print "-- query all switches --"
912# query_switch()
913# print "-- query topo --"
914# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000915# link_change(1,2,3,4)
916 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000917# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000918# print "-- query all devices --"
919# devices()
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000920 iperf_start(1,10,15)
921 iperf_rate(1)
Ubuntu82b8a832013-02-06 22:00:11 +0000922 else:
923 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000924 app.run(threaded=True, host="0.0.0.0", port=9000)