blob: ccf9a53b7df6564992756d8c3fec35fd1d53e416 [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
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000143@app.route("/wm/core/topology/switches/all/json")
144def switches():
145 if request.args.get('proxy') == None:
146 host = ONOS_LOCAL_HOST
147 else:
148 host = ONOS_GUI3_HOST
149
150 try:
151 command = "curl -s %s/wm/core/topology/switches/all/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700152# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000153 result = os.popen(command).read()
154 except:
155 print "REST IF has issue"
156 exit
157
158 resp = Response(result, status=200, mimetype='application/json')
159 return resp
160
161@app.route("/wm/core/topology/links/json")
162def links():
163 if request.args.get('proxy') == None:
164 host = ONOS_LOCAL_HOST
165 else:
166 host = ONOS_GUI3_HOST
167
168 try:
169 command = "curl -s %s/wm/core/topology/links/json" % (host)
Paul Greyson8d1c6362013-03-27 13:05:24 -0700170 print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000171 result = os.popen(command).read()
172 except:
173 print "REST IF has issue"
174 exit
175
176 resp = Response(result, status=200, mimetype='application/json')
177 return resp
178
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000179@app.route("/wm/flow/getsummary/0/0/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000180def flows():
181 if request.args.get('proxy') == None:
182 host = ONOS_LOCAL_HOST
183 else:
184 host = ONOS_GUI3_HOST
185
186 try:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000187 command = "curl -s %s/wm/flow/getsummary/0/0/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700188# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000189 result = os.popen(command).read()
190 except:
191 print "REST IF has issue"
192 exit
193
194 resp = Response(result, status=200, mimetype='application/json')
195 return resp
196
197@app.route("/wm/registry/controllers/json")
198def registry_controllers():
199 if request.args.get('proxy') == None:
200 host = ONOS_LOCAL_HOST
201 else:
202 host = ONOS_GUI3_HOST
203
204 try:
205 command = "curl -s %s/wm/registry/controllers/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700206# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000207 result = os.popen(command).read()
208 except:
209 print "REST IF has issue"
210 exit
211
212 resp = Response(result, status=200, mimetype='application/json')
213 return resp
214
215@app.route("/wm/registry/switches/json")
216def registry_switches():
217 if request.args.get('proxy') == None:
218 host = ONOS_LOCAL_HOST
219 else:
220 host = ONOS_GUI3_HOST
221
222 try:
223 command = "curl -s %s/wm/registry/switches/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700224# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000225 result = os.popen(command).read()
226 except:
227 print "REST IF has issue"
228 exit
229
230 resp = Response(result, status=200, mimetype='application/json')
231 return resp
232
Ubuntu82b8a832013-02-06 22:00:11 +0000233
234def node_id(switch_array, dpid):
235 id = -1
236 for i, val in enumerate(switch_array):
237 if val['name'] == dpid:
238 id = i
239 break
240
241 return id
242
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000243## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000244@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000245def topology_for_gui():
246 try:
247 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
248 result = os.popen(command).read()
249 parsedResult = json.loads(result)
250 except:
251 log_error("REST IF has issue: %s" % command)
252 log_error("%s" % result)
253 sys.exit(0)
254
255 topo = {}
256 switches = []
257 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000258 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000259
260 for v in parsedResult:
261 if v.has_key('dpid'):
262# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
263 dpid = str(v['dpid'])
264 state = str(v['state'])
265 sw = {}
266 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000267 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000268
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000269 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000270 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000271 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000272
Ubuntu37ebda62013-03-01 00:35:31 +0000273## Comment in if we need devies
274# sw_index = len(switches) - 1
275# for p in v['ports']:
276# for d in p['devices']:
277# device = {}
278# device['attached_switch']=dpid
279# device['name']=d['mac']
280# if d['state'] == "ACTIVE":
281# device['group']=1000
282# else:
283# device['group']=1001
284#
285# switches.append(device)
286# device_index = len (switches) -1
287# link = {}
288# link['source'] = device_index
289# link['target'] = sw_index
290# link['type'] = -1
291# links.append(link)
292# link = {}
293# link['source'] = sw_index
294# link['target'] = device_index
295# link['type'] = -1
296# links.append(link)
297
Ubuntu5b2b24a2013-02-27 09:51:13 +0000298# try:
299# command = "curl -s \'http://%s:%s/wm/registry/controllers/json\'" % (RestIP, RestPort)
300# result = os.popen(command).read()
301# controllers = json.loads(result)
302# except:
303# log_error("xx REST IF has issue: %s" % command)
304# log_error("%s" % result)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000305
306 try:
307 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
308 result = os.popen(command).read()
309 parsedResult = json.loads(result)
310 except:
311 log_error("REST IF has issue: %s" % command)
312 log_error("%s" % result)
313
314 for key in parsedResult:
315 dpid = key
316 ctrl = parsedResult[dpid][0]['controllerId']
317 sw_id = node_id(switches, dpid)
318 if sw_id != -1:
319 if switches[sw_id]['group'] != 0:
320 switches[sw_id]['group'] = controllers.index(ctrl) + 1
321
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000322 try:
323 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000324# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000325 p1=1
326 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000327# v2 = "00:00:00:00:00:0d:00:d3"
328 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000329 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
330 result = os.popen(command).read()
331 parsedResult = json.loads(result)
332 except:
333 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000334 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000335
Ubuntu765deff2013-02-28 18:39:13 +0000336 path = []
337 if parsedResult.has_key('flowEntries'):
338 flowEntries= parsedResult['flowEntries']
339 for i, v in enumerate(flowEntries):
340 if i < len(flowEntries) - 1:
341 sdpid= flowEntries[i]['dpid']['value']
342 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700343 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000344
Ubuntu82b8a832013-02-06 22:00:11 +0000345 try:
346 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
347 result = os.popen(command).read()
348 parsedResult = json.loads(result)
349 except:
350 log_error("REST IF has issue: %s" % command)
351 log_error("%s" % result)
352 sys.exit(0)
353
354 for v in parsedResult:
355 link = {}
356 if v.has_key('dst-switch'):
357 dst_dpid = str(v['dst-switch'])
358 dst_id = node_id(switches, dst_dpid)
359 if v.has_key('src-switch'):
360 src_dpid = str(v['src-switch'])
361 src_id = node_id(switches, src_dpid)
362 link['source'] = src_id
363 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000364
365 onpath = 0
366 for (s,d) in path:
367 if s == v['src-switch'] and d == v['dst-switch']:
368 onpath = 1
369 break
370 link['type'] = onpath
371
Ubuntu82b8a832013-02-06 22:00:11 +0000372 links.append(link)
373
374 topo['nodes'] = switches
375 topo['links'] = links
376
Ubuntu37ebda62013-03-01 00:35:31 +0000377 pp.pprint(topo)
Ubuntu82b8a832013-02-06 22:00:11 +0000378 js = json.dumps(topo)
379 resp = Response(js, status=200, mimetype='application/json')
380 return resp
381
Ubuntuaea2a682013-02-08 08:30:10 +0000382#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
383#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
384@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
385def shortest_path(v1, p1, v2, p2):
386 try:
387 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
388 result = os.popen(command).read()
389 parsedResult = json.loads(result)
390 except:
391 log_error("REST IF has issue: %s" % command)
392 log_error("%s" % result)
393 sys.exit(0)
394
395 topo = {}
396 switches = []
397 links = []
398
399 for v in parsedResult:
400 if v.has_key('dpid'):
401 dpid = str(v['dpid'])
402 state = str(v['state'])
403 sw = {}
404 sw['name']=dpid
405 if str(v['state']) == "ACTIVE":
406 if dpid[-2:-1] == "a":
407 sw['group']=1
408 if dpid[-2:-1] == "b":
409 sw['group']=2
410 if dpid[-2:-1] == "c":
411 sw['group']=3
412 if str(v['state']) == "INACTIVE":
413 sw['group']=0
414
415 switches.append(sw)
416
417 try:
418 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
419 result = os.popen(command).read()
420 parsedResult = json.loads(result)
421 except:
422 log_error("No route")
423 parsedResult = []
424# exit(1)
425
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700426 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000427 for i, v in enumerate(parsedResult):
428 if i < len(parsedResult) - 1:
429 sdpid= parsedResult[i]['switch']
430 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700431 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000432
433 try:
434 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
435 result = os.popen(command).read()
436 parsedResult = json.loads(result)
437 except:
438 log_error("REST IF has issue: %s" % command)
439 log_error("%s" % result)
440 sys.exit(0)
441
442 for v in parsedResult:
443 link = {}
444 if v.has_key('dst-switch'):
445 dst_dpid = str(v['dst-switch'])
446 dst_id = node_id(switches, dst_dpid)
447 if v.has_key('src-switch'):
448 src_dpid = str(v['src-switch'])
449 src_id = node_id(switches, src_dpid)
450 link['source'] = src_id
451 link['target'] = dst_id
452 onpath = 0
453 for (s,d) in path:
454 if s == v['src-switch'] and d == v['dst-switch']:
455 onpath = 1
456 break
457
458 link['type'] = onpath
459 links.append(link)
460
461 topo['nodes'] = switches
462 topo['links'] = links
463
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000464# pp.pprint(topo)
Ubuntuaea2a682013-02-08 08:30:10 +0000465 js = json.dumps(topo)
466 resp = Response(js, status=200, mimetype='application/json')
467 return resp
468
Ubuntu82b8a832013-02-06 22:00:11 +0000469@app.route("/wm/core/controller/switches/json")
470def query_switch():
471 try:
472 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
473# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000474 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000475 result = os.popen(command).read()
476 parsedResult = json.loads(result)
477 except:
478 log_error("REST IF has issue: %s" % command)
479 log_error("%s" % result)
480 sys.exit(0)
481
482# print command
483# print result
484 switches_ = []
485 for v in parsedResult:
486 if v.has_key('dpid'):
487 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
488 dpid = str(v['dpid'])
489 state = str(v['state'])
490 sw = {}
491 sw['dpid']=dpid
492 sw['active']=state
493 switches_.append(sw)
494
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000495# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000496 js = json.dumps(switches_)
497 resp = Response(js, status=200, mimetype='application/json')
498 return resp
499
500@app.route("/wm/device/")
501def devices():
502 try:
503 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
504 result = os.popen(command).read()
505 parsedResult = json.loads(result)['results']
506 except:
507 log_error("REST IF has issue: %s" % command)
508 log_error("%s" % result)
509 sys.exit(0)
510
511 devices = []
512 for v in parsedResult:
513 dl_addr = v['dl_addr']
514 nw_addr = v['nw_addr']
515 vertex = v['_id']
516 mac = []
517 mac.append(dl_addr)
518 ip = []
519 ip.append(nw_addr)
520 device = {}
521 device['entryClass']="DefaultEntryClass"
522 device['mac']=mac
523 device['ipv4']=ip
524 device['vlan']=[]
525 device['lastSeen']=0
526 attachpoints =[]
527
528 port, dpid = deviceV_to_attachpoint(vertex)
529 attachpoint = {}
530 attachpoint['port']=port
531 attachpoint['switchDPID']=dpid
532 attachpoints.append(attachpoint)
533 device['attachmentPoint']=attachpoints
534 devices.append(device)
535
536 print devices
537 js = json.dumps(devices)
538 resp = Response(js, status=200, mimetype='application/json')
539 return resp
540
541#{"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}
542
Ubuntu82b8a832013-02-06 22:00:11 +0000543## return fake stat for now
544@app.route("/wm/core/switch/<switchId>/<statType>/json")
545def switch_stat(switchId, statType):
546 if statType == "desc":
547 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
548 ret = {}
549 ret[switchId]=desc
550 elif statType == "aggregate":
551 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
552 ret = {}
553 ret[switchId]=aggr
554 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700555 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000556
557 js = json.dumps(ret)
558 resp = Response(js, status=200, mimetype='application/json')
559 return resp
560
561
562@app.route("/wm/topology/links/json")
563def query_links():
564 try:
565 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000566 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000567 result = os.popen(command).read()
568 parsedResult = json.loads(result)['results']
569 except:
570 log_error("REST IF has issue: %s" % command)
571 log_error("%s" % result)
572 sys.exit(0)
573
574 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000575# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000576 sport = []
577 links = []
578 for v in parsedResult:
579 srcport = v['_id']
580 try:
581 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
582 print command
583 result = os.popen(command).read()
584 linkResults = json.loads(result)['results']
585 except:
586 log_error("REST IF has issue: %s" % command)
587 log_error("%s" % result)
588 sys.exit(0)
589
590 for p in linkResults:
591 if p.has_key('type') and p['type'] == "port":
592 dstport = p['_id']
593 (sport, sdpid) = portV_to_port_dpid(srcport)
594 (dport, ddpid) = portV_to_port_dpid(dstport)
595 link = {}
596 link["src-switch"]=sdpid
597 link["src-port"]=sport
598 link["src-port-state"]=0
599 link["dst-switch"]=ddpid
600 link["dst-port"]=dport
601 link["dst-port-state"]=0
602 link["type"]="internal"
603 links.append(link)
604
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000605# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000606 js = json.dumps(links)
607 resp = Response(js, status=200, mimetype='application/json')
608 return resp
609
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700610topo_less = {
611 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000612 {"name" : "00:a0", "group" : 1},
613 {"name" : "00:a1", "group" : 1},
614 {"name" : "00:a2", "group" : 1},
615 ],
616 "links" : [
617 {"source" :0, "target": 1},
618 {"source" :1, "target": 0},
619 {"source" :0, "target": 2},
620 {"source" :2, "target": 0},
621 {"source" :1, "target": 2},
622 {"source" :2, "target": 1},
623 ]
624}
625
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700626topo_more = {
627 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000628 {"name" : "00:a3", "group" : 2},
629 {"name" : "00:a0", "group" : 1},
630 {"name" : "00:a1", "group" : 1},
631 {"name" : "00:a2", "group" : 1},
632 ],
633 "links" : [
634 {"source" :1, "target": 2},
635 {"source" :2, "target": 1},
636 {"source" :1, "target": 3},
637 {"source" :3, "target": 1},
638 {"source" :2, "target": 3},
639 {"source" :3, "target": 2},
640 {"source" :0, "target": 2},
641 ]
642}
643
644@app.route("/topology_more")
645def topology_more():
646 topo = topo_more
647 js = json.dumps(topo)
648 resp = Response(js, status=200, mimetype='application/json')
649 return resp
650
651@app.route("/topology_less")
652def topology_less():
653 topo = topo_less
654 js = json.dumps(topo)
655 resp = Response(js, status=200, mimetype='application/json')
656 return resp
657
658cont_status1 = [
659 {"name":"onos9vpc", "onos": 1, "cassandra": 1},
660 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
661 {"name":"onos11vpc", "onos": 1, "cassandra": 0},
662 {"name":"onos12vpc", "onos": 1, "cassandra": 0}]
663
664cont_status2 = [
665 {"name":"onos9vpc", "onos": 0, "cassandra": 1},
666 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
667 {"name":"onos11vpc", "onos": 0, "cassandra": 1},
668 {"name":"onos12vpc", "onos": 0, "cassandra": 1}]
669
670@app.route("/controller_status1")
671def controller_status1():
672 status = cont_status1
673 js = json.dumps(status)
674 resp = Response(js, status=200, mimetype='application/json')
675 pp.pprint(resp)
676 return resp
677
678@app.route("/controller_status2")
679def controller_status2():
680 status = cont_status2
681 js = json.dumps(status)
682 resp = Response(js, status=200, mimetype='application/json')
683 pp.pprint(resp)
684 return resp
685
Ubuntuc016ba12013-02-27 21:53:41 +0000686@app.route("/controller_status")
687def controller_status():
688 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
689 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
690
691 cont_status=[]
692 for i in controllers:
693 status={}
694 onos=os.popen(onos_check % i).read()[:-1]
695 status["name"]=i
696 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000697 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000698 cont_status.append(status)
699
700 js = json.dumps(cont_status)
701 resp = Response(js, status=200, mimetype='application/json')
702 pp.pprint(js)
703 return resp
704
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000705@app.route("/gui/controller/<cmd>/<controller_name>")
706def controller_status_change(cmd, controller_name):
707 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
708 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
709
710 if cmd == "up":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000711 print start_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000712 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000713 ret = "controller %s is up" % (controller_name)
714 elif cmd == "down":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000715 print stop_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000716 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000717 ret = "controller %s is down" % (controller_name)
718
719 return ret
720
721@app.route("/gui/switch/<cmd>/<dpid>")
722def switch_status_change(cmd, dpid):
723 r = re.compile(':')
724 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000725 host=controllers[0]
726 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
727 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000728 print "cmd_string"
729
730 if cmd =="up" or cmd=="down":
731 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000732 os.popen(cmd_string)
733 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000734
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000735 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000736
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000737#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000738#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000739@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
740def link_up(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000741
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000742 cmd = 'up'
743 result=""
744
Paul Greyson472da4c2013-03-28 11:43:17 -0700745 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000746 if dpid in core_switches:
747 host = controllers[0]
748 src_ports = [1, 2, 3, 4, 5]
749 else:
750 hostid=int(src_dpid.split(':')[-2])
751 host = controllers[hostid-1]
752 if hostid == 2 :
753 src_ports = [51]
754 else :
755 src_ports = [26]
756
757 for port in src_ports :
758 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
759 print cmd_string
760 res=os.popen(cmd_string).read()
761 result = result + ' ' + res
762
763 return result
764
765
766#* Link Down
767#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000768@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000769def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
770
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000771 if src_dpid in core_switches:
772 host = controllers[0]
773 else:
774 hostid=int(src_dpid.split(':')[-2])
775 host = controllers[hostid-1]
776
777 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
778 print cmd_string
779
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000780 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000781
782 return result
783
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000784#* Create Flow
785#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000786#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
787@app.route("/gui/addfow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
788def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
789 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
790 print command
791 ret = os.popen(command).read()
792 if ret == "":
793 flow_nr=0
794 else:
795 flow_nr=int(ret)
796
797 flow_nr += 1
798 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)
799 print command
800 errcode = os.popen(command).read()
801 return errcode
802
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000803#* Delete Flow
804#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000805@app.route("/gui/delflow/<flow_id>")
806def del_flow(flow_id):
807 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
808 print command
809 errcode = os.popen(command).read()
810 return errcode
811
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000812#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700813#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
814@app.route("/gui/iperf/start/<flow_id>/<duration>")
815def iperf_start(flow_id,duration):
816 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 +0000817 print command
818 errcode = os.popen(command).read()
819 return errcode
820
821
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000822#* Get Iperf Throughput
823#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000824@app.route("/gui/iperf/rate/<flow_id>")
825def iperf_rate(flow_id):
826 try:
827 command = "curl -s http://%s:%s/iperf_%s.out" % (RestIP, 9000, flow_id)
828 print command
829 result = os.popen(command).read()
830 except:
831 print "REST IF has issue"
832 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000833
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000834 resp = Response(result, status=200, mimetype='text/html')
835 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000836
837
Ubuntu82b8a832013-02-06 22:00:11 +0000838if __name__ == "__main__":
839 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000840# 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")
841# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
842# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
843# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
844# link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1)
845
846
Ubuntu82b8a832013-02-06 22:00:11 +0000847 print "-- query all switches --"
848 query_switch()
849 print "-- query topo --"
850 topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000851# link_change(1,2,3,4)
852 print "-- query all links --"
853 query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000854# print "-- query all devices --"
855# devices()
856 else:
857 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000858 app.run(threaded=True, host="0.0.0.0", port=9000)