blob: 556b4331297ce179b1f1ebbe8933b4ab0e096812 [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 Kobayashic0bc3192013-03-27 23:12:03 +000019controllers=["onosdevb1", "onosdevb2", "onosdevb3", "onosdevb4"]
20#controllers=["onosgui1", "onosgui2", "onosgui3", "onosgui4", "onosgui5", "onosgui6", "onosgui7", "onosgui8"]
21#core_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"]
22core_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"]
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +000023
24nr_flow=0
Ubuntu82b8a832013-02-06 22:00:11 +000025
26DEBUG=1
27pp = pprint.PrettyPrinter(indent=4)
28
29app = Flask(__name__)
30
31## Worker Functions ##
32def log_error(txt):
33 print '%s' % (txt)
34
35def debug(txt):
36 if DEBUG:
37 print '%s' % (txt)
38
39## Rest APIs ##
40### File Fetch ###
41@app.route('/ui/img/<filename>', methods=['GET'])
42@app.route('/img/<filename>', methods=['GET'])
43@app.route('/css/<filename>', methods=['GET'])
44@app.route('/js/models/<filename>', methods=['GET'])
45@app.route('/js/views/<filename>', methods=['GET'])
46@app.route('/js/<filename>', methods=['GET'])
47@app.route('/lib/<filename>', methods=['GET'])
48@app.route('/', methods=['GET'])
49@app.route('/<filename>', methods=['GET'])
50@app.route('/tpl/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000051@app.route('/ons-demo/<filename>', methods=['GET'])
52@app.route('/ons-demo/js/<filename>', methods=['GET'])
53@app.route('/ons-demo/css/<filename>', methods=['GET'])
54@app.route('/ons-demo/assets/<filename>', methods=['GET'])
55@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000056def return_file(filename="index.html"):
57 if request.path == "/":
58 fullpath = "./index.html"
59 else:
60 fullpath = str(request.path)[1:]
61
62 response = make_response(open(fullpath).read())
63 suffix = fullpath.split(".")[-1]
64
65 if suffix == "html" or suffix == "htm":
66 response.headers["Content-type"] = "text/html"
67 elif suffix == "js":
68 response.headers["Content-type"] = "application/javascript"
69 elif suffix == "css":
70 response.headers["Content-type"] = "text/css"
71 elif suffix == "png":
72 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -070073 elif suffix == "svg":
74 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000075
76 return response
77
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000078## PROXY API (allows development where the webui is served from someplace other than the controller)##
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +000079ONOS_GUI3_HOST="http://devb-gui.onlab.us:8080"
80ONOS_GUI3_CONTROL_HOST="http://devb-gui.onlab.us:8080"
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000081ONOS_LOCAL_HOST="http://localhost:8080" ;# for Amazon EC2
82
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070083@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
84def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
85 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -070086 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 -070087 print command
88 result = os.popen(command).read()
89 except:
90 print "REST IF has issue"
91 exit
92
93 resp = Response(result, status=200, mimetype='application/json')
94 return resp
95
Paul Greyson8d1c6362013-03-27 13:05:24 -070096@app.route("/proxy/gui/switch/<cmd>/<dpid>")
97def proxy_switch_status_change(cmd, dpid):
98 try:
99 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
100 print command
101 result = os.popen(command).read()
102 except:
103 print "REST IF has issue"
104 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700105
Paul Greyson8d1c6362013-03-27 13:05:24 -0700106 resp = Response(result, status=200, mimetype='application/json')
107 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700108
Paul Greyson2913af82013-03-27 14:53:17 -0700109@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
110def proxy_controller_status_change(cmd, controller_name):
111 try:
112 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
113 print command
114 result = os.popen(command).read()
115 except:
116 print "REST IF has issue"
117 exit
118
119 resp = Response(result, status=200, mimetype='application/json')
120 return resp
121
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000122@app.route("/wm/core/topology/switches/all/json")
123def switches():
124 if request.args.get('proxy') == None:
125 host = ONOS_LOCAL_HOST
126 else:
127 host = ONOS_GUI3_HOST
128
129 try:
130 command = "curl -s %s/wm/core/topology/switches/all/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700131# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000132 result = os.popen(command).read()
133 except:
134 print "REST IF has issue"
135 exit
136
137 resp = Response(result, status=200, mimetype='application/json')
138 return resp
139
140@app.route("/wm/core/topology/links/json")
141def links():
142 if request.args.get('proxy') == None:
143 host = ONOS_LOCAL_HOST
144 else:
145 host = ONOS_GUI3_HOST
146
147 try:
148 command = "curl -s %s/wm/core/topology/links/json" % (host)
Paul Greyson8d1c6362013-03-27 13:05:24 -0700149 print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000150 result = os.popen(command).read()
151 except:
152 print "REST IF has issue"
153 exit
154
155 resp = Response(result, status=200, mimetype='application/json')
156 return resp
157
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000158@app.route("/wm/flow/getsummary/0/0/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000159def flows():
160 if request.args.get('proxy') == None:
161 host = ONOS_LOCAL_HOST
162 else:
163 host = ONOS_GUI3_HOST
164
165 try:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000166 command = "curl -s %s/wm/flow/getsummary/0/0/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700167# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000168 result = os.popen(command).read()
169 except:
170 print "REST IF has issue"
171 exit
172
173 resp = Response(result, status=200, mimetype='application/json')
174 return resp
175
176@app.route("/wm/registry/controllers/json")
177def registry_controllers():
178 if request.args.get('proxy') == None:
179 host = ONOS_LOCAL_HOST
180 else:
181 host = ONOS_GUI3_HOST
182
183 try:
184 command = "curl -s %s/wm/registry/controllers/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700185# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000186 result = os.popen(command).read()
187 except:
188 print "REST IF has issue"
189 exit
190
191 resp = Response(result, status=200, mimetype='application/json')
192 return resp
193
194@app.route("/wm/registry/switches/json")
195def registry_switches():
196 if request.args.get('proxy') == None:
197 host = ONOS_LOCAL_HOST
198 else:
199 host = ONOS_GUI3_HOST
200
201 try:
202 command = "curl -s %s/wm/registry/switches/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700203# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000204 result = os.popen(command).read()
205 except:
206 print "REST IF has issue"
207 exit
208
209 resp = Response(result, status=200, mimetype='application/json')
210 return resp
211
Ubuntu82b8a832013-02-06 22:00:11 +0000212
213def node_id(switch_array, dpid):
214 id = -1
215 for i, val in enumerate(switch_array):
216 if val['name'] == dpid:
217 id = i
218 break
219
220 return id
221
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000222## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000223@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000224def topology_for_gui():
225 try:
226 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
227 result = os.popen(command).read()
228 parsedResult = json.loads(result)
229 except:
230 log_error("REST IF has issue: %s" % command)
231 log_error("%s" % result)
232 sys.exit(0)
233
234 topo = {}
235 switches = []
236 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000237 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000238
239 for v in parsedResult:
240 if v.has_key('dpid'):
241# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
242 dpid = str(v['dpid'])
243 state = str(v['state'])
244 sw = {}
245 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000246 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000247
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000248 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000249 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000250 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000251
Ubuntu37ebda62013-03-01 00:35:31 +0000252## Comment in if we need devies
253# sw_index = len(switches) - 1
254# for p in v['ports']:
255# for d in p['devices']:
256# device = {}
257# device['attached_switch']=dpid
258# device['name']=d['mac']
259# if d['state'] == "ACTIVE":
260# device['group']=1000
261# else:
262# device['group']=1001
263#
264# switches.append(device)
265# device_index = len (switches) -1
266# link = {}
267# link['source'] = device_index
268# link['target'] = sw_index
269# link['type'] = -1
270# links.append(link)
271# link = {}
272# link['source'] = sw_index
273# link['target'] = device_index
274# link['type'] = -1
275# links.append(link)
276
Ubuntu5b2b24a2013-02-27 09:51:13 +0000277# try:
278# command = "curl -s \'http://%s:%s/wm/registry/controllers/json\'" % (RestIP, RestPort)
279# result = os.popen(command).read()
280# controllers = json.loads(result)
281# except:
282# log_error("xx REST IF has issue: %s" % command)
283# log_error("%s" % result)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000284
285 try:
286 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
287 result = os.popen(command).read()
288 parsedResult = json.loads(result)
289 except:
290 log_error("REST IF has issue: %s" % command)
291 log_error("%s" % result)
292
293 for key in parsedResult:
294 dpid = key
295 ctrl = parsedResult[dpid][0]['controllerId']
296 sw_id = node_id(switches, dpid)
297 if sw_id != -1:
298 if switches[sw_id]['group'] != 0:
299 switches[sw_id]['group'] = controllers.index(ctrl) + 1
300
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000301 try:
302 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000303# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000304 p1=1
305 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000306# v2 = "00:00:00:00:00:0d:00:d3"
307 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000308 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
309 result = os.popen(command).read()
310 parsedResult = json.loads(result)
311 except:
312 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000313 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000314
Ubuntu765deff2013-02-28 18:39:13 +0000315 path = []
316 if parsedResult.has_key('flowEntries'):
317 flowEntries= parsedResult['flowEntries']
318 for i, v in enumerate(flowEntries):
319 if i < len(flowEntries) - 1:
320 sdpid= flowEntries[i]['dpid']['value']
321 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700322 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000323
Ubuntu82b8a832013-02-06 22:00:11 +0000324 try:
325 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
326 result = os.popen(command).read()
327 parsedResult = json.loads(result)
328 except:
329 log_error("REST IF has issue: %s" % command)
330 log_error("%s" % result)
331 sys.exit(0)
332
333 for v in parsedResult:
334 link = {}
335 if v.has_key('dst-switch'):
336 dst_dpid = str(v['dst-switch'])
337 dst_id = node_id(switches, dst_dpid)
338 if v.has_key('src-switch'):
339 src_dpid = str(v['src-switch'])
340 src_id = node_id(switches, src_dpid)
341 link['source'] = src_id
342 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000343
344 onpath = 0
345 for (s,d) in path:
346 if s == v['src-switch'] and d == v['dst-switch']:
347 onpath = 1
348 break
349 link['type'] = onpath
350
Ubuntu82b8a832013-02-06 22:00:11 +0000351 links.append(link)
352
353 topo['nodes'] = switches
354 topo['links'] = links
355
Ubuntu37ebda62013-03-01 00:35:31 +0000356 pp.pprint(topo)
Ubuntu82b8a832013-02-06 22:00:11 +0000357 js = json.dumps(topo)
358 resp = Response(js, status=200, mimetype='application/json')
359 return resp
360
Ubuntuaea2a682013-02-08 08:30:10 +0000361#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
362#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
363@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
364def shortest_path(v1, p1, v2, p2):
365 try:
366 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
367 result = os.popen(command).read()
368 parsedResult = json.loads(result)
369 except:
370 log_error("REST IF has issue: %s" % command)
371 log_error("%s" % result)
372 sys.exit(0)
373
374 topo = {}
375 switches = []
376 links = []
377
378 for v in parsedResult:
379 if v.has_key('dpid'):
380 dpid = str(v['dpid'])
381 state = str(v['state'])
382 sw = {}
383 sw['name']=dpid
384 if str(v['state']) == "ACTIVE":
385 if dpid[-2:-1] == "a":
386 sw['group']=1
387 if dpid[-2:-1] == "b":
388 sw['group']=2
389 if dpid[-2:-1] == "c":
390 sw['group']=3
391 if str(v['state']) == "INACTIVE":
392 sw['group']=0
393
394 switches.append(sw)
395
396 try:
397 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
398 result = os.popen(command).read()
399 parsedResult = json.loads(result)
400 except:
401 log_error("No route")
402 parsedResult = []
403# exit(1)
404
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700405 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000406 for i, v in enumerate(parsedResult):
407 if i < len(parsedResult) - 1:
408 sdpid= parsedResult[i]['switch']
409 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700410 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000411
412 try:
413 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
414 result = os.popen(command).read()
415 parsedResult = json.loads(result)
416 except:
417 log_error("REST IF has issue: %s" % command)
418 log_error("%s" % result)
419 sys.exit(0)
420
421 for v in parsedResult:
422 link = {}
423 if v.has_key('dst-switch'):
424 dst_dpid = str(v['dst-switch'])
425 dst_id = node_id(switches, dst_dpid)
426 if v.has_key('src-switch'):
427 src_dpid = str(v['src-switch'])
428 src_id = node_id(switches, src_dpid)
429 link['source'] = src_id
430 link['target'] = dst_id
431 onpath = 0
432 for (s,d) in path:
433 if s == v['src-switch'] and d == v['dst-switch']:
434 onpath = 1
435 break
436
437 link['type'] = onpath
438 links.append(link)
439
440 topo['nodes'] = switches
441 topo['links'] = links
442
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000443# pp.pprint(topo)
Ubuntuaea2a682013-02-08 08:30:10 +0000444 js = json.dumps(topo)
445 resp = Response(js, status=200, mimetype='application/json')
446 return resp
447
Ubuntu82b8a832013-02-06 22:00:11 +0000448@app.route("/wm/core/controller/switches/json")
449def query_switch():
450 try:
451 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
452# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000453 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000454 result = os.popen(command).read()
455 parsedResult = json.loads(result)
456 except:
457 log_error("REST IF has issue: %s" % command)
458 log_error("%s" % result)
459 sys.exit(0)
460
461# print command
462# print result
463 switches_ = []
464 for v in parsedResult:
465 if v.has_key('dpid'):
466 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
467 dpid = str(v['dpid'])
468 state = str(v['state'])
469 sw = {}
470 sw['dpid']=dpid
471 sw['active']=state
472 switches_.append(sw)
473
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000474# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000475 js = json.dumps(switches_)
476 resp = Response(js, status=200, mimetype='application/json')
477 return resp
478
479@app.route("/wm/device/")
480def devices():
481 try:
482 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
483 result = os.popen(command).read()
484 parsedResult = json.loads(result)['results']
485 except:
486 log_error("REST IF has issue: %s" % command)
487 log_error("%s" % result)
488 sys.exit(0)
489
490 devices = []
491 for v in parsedResult:
492 dl_addr = v['dl_addr']
493 nw_addr = v['nw_addr']
494 vertex = v['_id']
495 mac = []
496 mac.append(dl_addr)
497 ip = []
498 ip.append(nw_addr)
499 device = {}
500 device['entryClass']="DefaultEntryClass"
501 device['mac']=mac
502 device['ipv4']=ip
503 device['vlan']=[]
504 device['lastSeen']=0
505 attachpoints =[]
506
507 port, dpid = deviceV_to_attachpoint(vertex)
508 attachpoint = {}
509 attachpoint['port']=port
510 attachpoint['switchDPID']=dpid
511 attachpoints.append(attachpoint)
512 device['attachmentPoint']=attachpoints
513 devices.append(device)
514
515 print devices
516 js = json.dumps(devices)
517 resp = Response(js, status=200, mimetype='application/json')
518 return resp
519
520#{"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}
521
Ubuntu82b8a832013-02-06 22:00:11 +0000522## return fake stat for now
523@app.route("/wm/core/switch/<switchId>/<statType>/json")
524def switch_stat(switchId, statType):
525 if statType == "desc":
526 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
527 ret = {}
528 ret[switchId]=desc
529 elif statType == "aggregate":
530 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
531 ret = {}
532 ret[switchId]=aggr
533 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700534 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000535
536 js = json.dumps(ret)
537 resp = Response(js, status=200, mimetype='application/json')
538 return resp
539
540
541@app.route("/wm/topology/links/json")
542def query_links():
543 try:
544 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000545 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000546 result = os.popen(command).read()
547 parsedResult = json.loads(result)['results']
548 except:
549 log_error("REST IF has issue: %s" % command)
550 log_error("%s" % result)
551 sys.exit(0)
552
553 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000554# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000555 sport = []
556 links = []
557 for v in parsedResult:
558 srcport = v['_id']
559 try:
560 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
561 print command
562 result = os.popen(command).read()
563 linkResults = json.loads(result)['results']
564 except:
565 log_error("REST IF has issue: %s" % command)
566 log_error("%s" % result)
567 sys.exit(0)
568
569 for p in linkResults:
570 if p.has_key('type') and p['type'] == "port":
571 dstport = p['_id']
572 (sport, sdpid) = portV_to_port_dpid(srcport)
573 (dport, ddpid) = portV_to_port_dpid(dstport)
574 link = {}
575 link["src-switch"]=sdpid
576 link["src-port"]=sport
577 link["src-port-state"]=0
578 link["dst-switch"]=ddpid
579 link["dst-port"]=dport
580 link["dst-port-state"]=0
581 link["type"]="internal"
582 links.append(link)
583
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000584# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000585 js = json.dumps(links)
586 resp = Response(js, status=200, mimetype='application/json')
587 return resp
588
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700589topo_less = {
590 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000591 {"name" : "00:a0", "group" : 1},
592 {"name" : "00:a1", "group" : 1},
593 {"name" : "00:a2", "group" : 1},
594 ],
595 "links" : [
596 {"source" :0, "target": 1},
597 {"source" :1, "target": 0},
598 {"source" :0, "target": 2},
599 {"source" :2, "target": 0},
600 {"source" :1, "target": 2},
601 {"source" :2, "target": 1},
602 ]
603}
604
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700605topo_more = {
606 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000607 {"name" : "00:a3", "group" : 2},
608 {"name" : "00:a0", "group" : 1},
609 {"name" : "00:a1", "group" : 1},
610 {"name" : "00:a2", "group" : 1},
611 ],
612 "links" : [
613 {"source" :1, "target": 2},
614 {"source" :2, "target": 1},
615 {"source" :1, "target": 3},
616 {"source" :3, "target": 1},
617 {"source" :2, "target": 3},
618 {"source" :3, "target": 2},
619 {"source" :0, "target": 2},
620 ]
621}
622
623@app.route("/topology_more")
624def topology_more():
625 topo = topo_more
626 js = json.dumps(topo)
627 resp = Response(js, status=200, mimetype='application/json')
628 return resp
629
630@app.route("/topology_less")
631def topology_less():
632 topo = topo_less
633 js = json.dumps(topo)
634 resp = Response(js, status=200, mimetype='application/json')
635 return resp
636
637cont_status1 = [
638 {"name":"onos9vpc", "onos": 1, "cassandra": 1},
639 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
640 {"name":"onos11vpc", "onos": 1, "cassandra": 0},
641 {"name":"onos12vpc", "onos": 1, "cassandra": 0}]
642
643cont_status2 = [
644 {"name":"onos9vpc", "onos": 0, "cassandra": 1},
645 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
646 {"name":"onos11vpc", "onos": 0, "cassandra": 1},
647 {"name":"onos12vpc", "onos": 0, "cassandra": 1}]
648
649@app.route("/controller_status1")
650def controller_status1():
651 status = cont_status1
652 js = json.dumps(status)
653 resp = Response(js, status=200, mimetype='application/json')
654 pp.pprint(resp)
655 return resp
656
657@app.route("/controller_status2")
658def controller_status2():
659 status = cont_status2
660 js = json.dumps(status)
661 resp = Response(js, status=200, mimetype='application/json')
662 pp.pprint(resp)
663 return resp
664
Ubuntuc016ba12013-02-27 21:53:41 +0000665@app.route("/controller_status")
666def controller_status():
667 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
668 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
669
670 cont_status=[]
671 for i in controllers:
672 status={}
673 onos=os.popen(onos_check % i).read()[:-1]
674 status["name"]=i
675 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000676 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000677 cont_status.append(status)
678
679 js = json.dumps(cont_status)
680 resp = Response(js, status=200, mimetype='application/json')
681 pp.pprint(js)
682 return resp
683
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000684@app.route("/gui/controller/<cmd>/<controller_name>")
685def controller_status_change(cmd, controller_name):
686 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
687 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
688
689 if cmd == "up":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000690 print start_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000691 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000692 ret = "controller %s is up" % (controller_name)
693 elif cmd == "down":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000694 print stop_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000695 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000696 ret = "controller %s is down" % (controller_name)
697
698 return ret
699
700@app.route("/gui/switch/<cmd>/<dpid>")
701def switch_status_change(cmd, dpid):
702 r = re.compile(':')
703 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000704 host=controllers[0]
705 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
706 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000707 print "cmd_string"
708
709 if cmd =="up" or cmd=="down":
710 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000711 os.popen(cmd_string)
712 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000713
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000714 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000715
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000716#* Link Up/Down
717#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
718#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000719
720@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
721def link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
722 if src_dpid in core_switches:
723 host = controllers[0]
724 else:
725 hostid=int(src_dpid.split(':')[-2])
726 host = controllers[hostid-1]
727
728 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
729 print cmd_string
730
731 if cmd =="up" or cmd=="down":
732 result=os.popen(cmd_string).read()
733
734 return result
735
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000736#* Create Flow
737#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000738#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
739@app.route("/gui/addfow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
740def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
741 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
742 print command
743 ret = os.popen(command).read()
744 if ret == "":
745 flow_nr=0
746 else:
747 flow_nr=int(ret)
748
749 flow_nr += 1
750 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)
751 print command
752 errcode = os.popen(command).read()
753 return errcode
754
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000755#* Delete Flow
756#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000757@app.route("/gui/delflow/<flow_id>")
758def del_flow(flow_id):
759 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
760 print command
761 errcode = os.popen(command).read()
762 return errcode
763
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000764#* Start Iperf Througput
765#http://localhost:9000/gui/iperf/start/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000766@app.route("/gui/iperf/start/<flow_id>")
767def iperf_start(flow_id):
768 command = "iperf -xCMSV -t30 -i1 -u -c 127.0.0.1 > iperf_%s.out &" % (flow_id)
769 print command
770 errcode = os.popen(command).read()
771 return errcode
772
773
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000774#* Get Iperf Throughput
775#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000776@app.route("/gui/iperf/rate/<flow_id>")
777def iperf_rate(flow_id):
778 try:
779 command = "curl -s http://%s:%s/iperf_%s.out" % (RestIP, 9000, flow_id)
780 print command
781 result = os.popen(command).read()
782 except:
783 print "REST IF has issue"
784 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000785
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000786 resp = Response(result, status=200, mimetype='text/html')
787 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000788
789
Ubuntu82b8a832013-02-06 22:00:11 +0000790if __name__ == "__main__":
791 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000792# 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")
793# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
794# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
795# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
796# link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1)
797
798
Ubuntu82b8a832013-02-06 22:00:11 +0000799 print "-- query all switches --"
800 query_switch()
801 print "-- query topo --"
802 topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000803# link_change(1,2,3,4)
804 print "-- query all links --"
805 query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000806# print "-- query all devices --"
807# devices()
808 else:
809 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000810 app.run(threaded=True, host="0.0.0.0", port=9000)