blob: 8a94ef9e16870fef8db7255bca69dbd720620865 [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 Kobayashi13e2ebe2013-03-26 18:38:41 +000019#controllers=["onosgui1", "onosgui2", "onosgui3", "onosgui4"]
20controllers=["onosgui1", "onosgui2", "onosgui3", "onosgui4", "onosgui5", "onosgui6", "onosgui7", "onosgui8"]
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +000021core_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"]
22
23nr_flow=0
Ubuntu82b8a832013-02-06 22:00:11 +000024
25DEBUG=1
26pp = pprint.PrettyPrinter(indent=4)
27
28app = Flask(__name__)
29
30## Worker Functions ##
31def log_error(txt):
32 print '%s' % (txt)
33
34def debug(txt):
35 if DEBUG:
36 print '%s' % (txt)
37
38## Rest APIs ##
39### File Fetch ###
40@app.route('/ui/img/<filename>', methods=['GET'])
41@app.route('/img/<filename>', methods=['GET'])
42@app.route('/css/<filename>', methods=['GET'])
43@app.route('/js/models/<filename>', methods=['GET'])
44@app.route('/js/views/<filename>', methods=['GET'])
45@app.route('/js/<filename>', methods=['GET'])
46@app.route('/lib/<filename>', methods=['GET'])
47@app.route('/', methods=['GET'])
48@app.route('/<filename>', methods=['GET'])
49@app.route('/tpl/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000050@app.route('/ons-demo/<filename>', methods=['GET'])
51@app.route('/ons-demo/js/<filename>', methods=['GET'])
52@app.route('/ons-demo/css/<filename>', methods=['GET'])
53@app.route('/ons-demo/assets/<filename>', methods=['GET'])
54@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000055def return_file(filename="index.html"):
56 if request.path == "/":
57 fullpath = "./index.html"
58 else:
59 fullpath = str(request.path)[1:]
60
61 response = make_response(open(fullpath).read())
62 suffix = fullpath.split(".")[-1]
63
64 if suffix == "html" or suffix == "htm":
65 response.headers["Content-type"] = "text/html"
66 elif suffix == "js":
67 response.headers["Content-type"] = "application/javascript"
68 elif suffix == "css":
69 response.headers["Content-type"] = "text/css"
70 elif suffix == "png":
71 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -070072 elif suffix == "svg":
73 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000074
75 return response
76
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000077## PROXY API (allows development where the webui is served from someplace other than the controller)##
78ONOS_GUI3_HOST="http://gui3.onlab.us:8080"
Paul Greyson8d1c6362013-03-27 13:05:24 -070079ONOS_GUI3_CONTROL_HOST="http://gui3.onlab.us:8081"
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000080ONOS_LOCAL_HOST="http://localhost:8080" ;# for Amazon EC2
81
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070082@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
83def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
84 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -070085 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 -070086 print command
87 result = os.popen(command).read()
88 except:
89 print "REST IF has issue"
90 exit
91
92 resp = Response(result, status=200, mimetype='application/json')
93 return resp
94
Paul Greyson8d1c6362013-03-27 13:05:24 -070095@app.route("/proxy/gui/switch/<cmd>/<dpid>")
96def proxy_switch_status_change(cmd, dpid):
97 try:
98 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
99 print command
100 result = os.popen(command).read()
101 except:
102 print "REST IF has issue"
103 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700104
Paul Greyson8d1c6362013-03-27 13:05:24 -0700105 resp = Response(result, status=200, mimetype='application/json')
106 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700107
Paul Greyson2913af82013-03-27 14:53:17 -0700108@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
109def proxy_controller_status_change(cmd, controller_name):
110 try:
111 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
112 print command
113 result = os.popen(command).read()
114 except:
115 print "REST IF has issue"
116 exit
117
118 resp = Response(result, status=200, mimetype='application/json')
119 return resp
120
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000121@app.route("/wm/core/topology/switches/all/json")
122def switches():
123 if request.args.get('proxy') == None:
124 host = ONOS_LOCAL_HOST
125 else:
126 host = ONOS_GUI3_HOST
127
128 try:
129 command = "curl -s %s/wm/core/topology/switches/all/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700130# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000131 result = os.popen(command).read()
132 except:
133 print "REST IF has issue"
134 exit
135
136 resp = Response(result, status=200, mimetype='application/json')
137 return resp
138
139@app.route("/wm/core/topology/links/json")
140def links():
141 if request.args.get('proxy') == None:
142 host = ONOS_LOCAL_HOST
143 else:
144 host = ONOS_GUI3_HOST
145
146 try:
147 command = "curl -s %s/wm/core/topology/links/json" % (host)
Paul Greyson8d1c6362013-03-27 13:05:24 -0700148 print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000149 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
156
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000157@app.route("/wm/flow/getsummary/0/0/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000158def flows():
159 if request.args.get('proxy') == None:
160 host = ONOS_LOCAL_HOST
161 else:
162 host = ONOS_GUI3_HOST
163
164 try:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000165 command = "curl -s %s/wm/flow/getsummary/0/0/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700166# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000167 result = os.popen(command).read()
168 except:
169 print "REST IF has issue"
170 exit
171
172 resp = Response(result, status=200, mimetype='application/json')
173 return resp
174
175@app.route("/wm/registry/controllers/json")
176def registry_controllers():
177 if request.args.get('proxy') == None:
178 host = ONOS_LOCAL_HOST
179 else:
180 host = ONOS_GUI3_HOST
181
182 try:
183 command = "curl -s %s/wm/registry/controllers/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700184# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000185 result = os.popen(command).read()
186 except:
187 print "REST IF has issue"
188 exit
189
190 resp = Response(result, status=200, mimetype='application/json')
191 return resp
192
193@app.route("/wm/registry/switches/json")
194def registry_switches():
195 if request.args.get('proxy') == None:
196 host = ONOS_LOCAL_HOST
197 else:
198 host = ONOS_GUI3_HOST
199
200 try:
201 command = "curl -s %s/wm/registry/switches/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700202# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000203 result = os.popen(command).read()
204 except:
205 print "REST IF has issue"
206 exit
207
208 resp = Response(result, status=200, mimetype='application/json')
209 return resp
210
Ubuntu82b8a832013-02-06 22:00:11 +0000211
212def node_id(switch_array, dpid):
213 id = -1
214 for i, val in enumerate(switch_array):
215 if val['name'] == dpid:
216 id = i
217 break
218
219 return id
220
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000221## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000222@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000223def topology_for_gui():
224 try:
225 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
226 result = os.popen(command).read()
227 parsedResult = json.loads(result)
228 except:
229 log_error("REST IF has issue: %s" % command)
230 log_error("%s" % result)
231 sys.exit(0)
232
233 topo = {}
234 switches = []
235 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000236 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000237
238 for v in parsedResult:
239 if v.has_key('dpid'):
240# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
241 dpid = str(v['dpid'])
242 state = str(v['state'])
243 sw = {}
244 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000245 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000246
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000247 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000248 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000249 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000250
Ubuntu37ebda62013-03-01 00:35:31 +0000251## Comment in if we need devies
252# sw_index = len(switches) - 1
253# for p in v['ports']:
254# for d in p['devices']:
255# device = {}
256# device['attached_switch']=dpid
257# device['name']=d['mac']
258# if d['state'] == "ACTIVE":
259# device['group']=1000
260# else:
261# device['group']=1001
262#
263# switches.append(device)
264# device_index = len (switches) -1
265# link = {}
266# link['source'] = device_index
267# link['target'] = sw_index
268# link['type'] = -1
269# links.append(link)
270# link = {}
271# link['source'] = sw_index
272# link['target'] = device_index
273# link['type'] = -1
274# links.append(link)
275
Ubuntu5b2b24a2013-02-27 09:51:13 +0000276# try:
277# command = "curl -s \'http://%s:%s/wm/registry/controllers/json\'" % (RestIP, RestPort)
278# result = os.popen(command).read()
279# controllers = json.loads(result)
280# except:
281# log_error("xx REST IF has issue: %s" % command)
282# log_error("%s" % result)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000283
284 try:
285 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
286 result = os.popen(command).read()
287 parsedResult = json.loads(result)
288 except:
289 log_error("REST IF has issue: %s" % command)
290 log_error("%s" % result)
291
292 for key in parsedResult:
293 dpid = key
294 ctrl = parsedResult[dpid][0]['controllerId']
295 sw_id = node_id(switches, dpid)
296 if sw_id != -1:
297 if switches[sw_id]['group'] != 0:
298 switches[sw_id]['group'] = controllers.index(ctrl) + 1
299
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000300 try:
301 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000302# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000303 p1=1
304 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000305# v2 = "00:00:00:00:00:0d:00:d3"
306 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000307 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
308 result = os.popen(command).read()
309 parsedResult = json.loads(result)
310 except:
311 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000312 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000313
Ubuntu765deff2013-02-28 18:39:13 +0000314 path = []
315 if parsedResult.has_key('flowEntries'):
316 flowEntries= parsedResult['flowEntries']
317 for i, v in enumerate(flowEntries):
318 if i < len(flowEntries) - 1:
319 sdpid= flowEntries[i]['dpid']['value']
320 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700321 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000322
Ubuntu82b8a832013-02-06 22:00:11 +0000323 try:
324 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
325 result = os.popen(command).read()
326 parsedResult = json.loads(result)
327 except:
328 log_error("REST IF has issue: %s" % command)
329 log_error("%s" % result)
330 sys.exit(0)
331
332 for v in parsedResult:
333 link = {}
334 if v.has_key('dst-switch'):
335 dst_dpid = str(v['dst-switch'])
336 dst_id = node_id(switches, dst_dpid)
337 if v.has_key('src-switch'):
338 src_dpid = str(v['src-switch'])
339 src_id = node_id(switches, src_dpid)
340 link['source'] = src_id
341 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000342
343 onpath = 0
344 for (s,d) in path:
345 if s == v['src-switch'] and d == v['dst-switch']:
346 onpath = 1
347 break
348 link['type'] = onpath
349
Ubuntu82b8a832013-02-06 22:00:11 +0000350 links.append(link)
351
352 topo['nodes'] = switches
353 topo['links'] = links
354
Ubuntu37ebda62013-03-01 00:35:31 +0000355 pp.pprint(topo)
Ubuntu82b8a832013-02-06 22:00:11 +0000356 js = json.dumps(topo)
357 resp = Response(js, status=200, mimetype='application/json')
358 return resp
359
Ubuntuaea2a682013-02-08 08:30:10 +0000360#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
361#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
362@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
363def shortest_path(v1, p1, v2, p2):
364 try:
365 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
366 result = os.popen(command).read()
367 parsedResult = json.loads(result)
368 except:
369 log_error("REST IF has issue: %s" % command)
370 log_error("%s" % result)
371 sys.exit(0)
372
373 topo = {}
374 switches = []
375 links = []
376
377 for v in parsedResult:
378 if v.has_key('dpid'):
379 dpid = str(v['dpid'])
380 state = str(v['state'])
381 sw = {}
382 sw['name']=dpid
383 if str(v['state']) == "ACTIVE":
384 if dpid[-2:-1] == "a":
385 sw['group']=1
386 if dpid[-2:-1] == "b":
387 sw['group']=2
388 if dpid[-2:-1] == "c":
389 sw['group']=3
390 if str(v['state']) == "INACTIVE":
391 sw['group']=0
392
393 switches.append(sw)
394
395 try:
396 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
397 result = os.popen(command).read()
398 parsedResult = json.loads(result)
399 except:
400 log_error("No route")
401 parsedResult = []
402# exit(1)
403
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700404 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000405 for i, v in enumerate(parsedResult):
406 if i < len(parsedResult) - 1:
407 sdpid= parsedResult[i]['switch']
408 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700409 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000410
411 try:
412 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
413 result = os.popen(command).read()
414 parsedResult = json.loads(result)
415 except:
416 log_error("REST IF has issue: %s" % command)
417 log_error("%s" % result)
418 sys.exit(0)
419
420 for v in parsedResult:
421 link = {}
422 if v.has_key('dst-switch'):
423 dst_dpid = str(v['dst-switch'])
424 dst_id = node_id(switches, dst_dpid)
425 if v.has_key('src-switch'):
426 src_dpid = str(v['src-switch'])
427 src_id = node_id(switches, src_dpid)
428 link['source'] = src_id
429 link['target'] = dst_id
430 onpath = 0
431 for (s,d) in path:
432 if s == v['src-switch'] and d == v['dst-switch']:
433 onpath = 1
434 break
435
436 link['type'] = onpath
437 links.append(link)
438
439 topo['nodes'] = switches
440 topo['links'] = links
441
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000442# pp.pprint(topo)
Ubuntuaea2a682013-02-08 08:30:10 +0000443 js = json.dumps(topo)
444 resp = Response(js, status=200, mimetype='application/json')
445 return resp
446
Ubuntu82b8a832013-02-06 22:00:11 +0000447@app.route("/wm/core/controller/switches/json")
448def query_switch():
449 try:
450 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
451# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000452 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000453 result = os.popen(command).read()
454 parsedResult = json.loads(result)
455 except:
456 log_error("REST IF has issue: %s" % command)
457 log_error("%s" % result)
458 sys.exit(0)
459
460# print command
461# print result
462 switches_ = []
463 for v in parsedResult:
464 if v.has_key('dpid'):
465 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
466 dpid = str(v['dpid'])
467 state = str(v['state'])
468 sw = {}
469 sw['dpid']=dpid
470 sw['active']=state
471 switches_.append(sw)
472
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000473# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000474 js = json.dumps(switches_)
475 resp = Response(js, status=200, mimetype='application/json')
476 return resp
477
478@app.route("/wm/device/")
479def devices():
480 try:
481 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
482 result = os.popen(command).read()
483 parsedResult = json.loads(result)['results']
484 except:
485 log_error("REST IF has issue: %s" % command)
486 log_error("%s" % result)
487 sys.exit(0)
488
489 devices = []
490 for v in parsedResult:
491 dl_addr = v['dl_addr']
492 nw_addr = v['nw_addr']
493 vertex = v['_id']
494 mac = []
495 mac.append(dl_addr)
496 ip = []
497 ip.append(nw_addr)
498 device = {}
499 device['entryClass']="DefaultEntryClass"
500 device['mac']=mac
501 device['ipv4']=ip
502 device['vlan']=[]
503 device['lastSeen']=0
504 attachpoints =[]
505
506 port, dpid = deviceV_to_attachpoint(vertex)
507 attachpoint = {}
508 attachpoint['port']=port
509 attachpoint['switchDPID']=dpid
510 attachpoints.append(attachpoint)
511 device['attachmentPoint']=attachpoints
512 devices.append(device)
513
514 print devices
515 js = json.dumps(devices)
516 resp = Response(js, status=200, mimetype='application/json')
517 return resp
518
519#{"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}
520
Ubuntu82b8a832013-02-06 22:00:11 +0000521## return fake stat for now
522@app.route("/wm/core/switch/<switchId>/<statType>/json")
523def switch_stat(switchId, statType):
524 if statType == "desc":
525 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
526 ret = {}
527 ret[switchId]=desc
528 elif statType == "aggregate":
529 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
530 ret = {}
531 ret[switchId]=aggr
532 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700533 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000534
535 js = json.dumps(ret)
536 resp = Response(js, status=200, mimetype='application/json')
537 return resp
538
539
540@app.route("/wm/topology/links/json")
541def query_links():
542 try:
543 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000544 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000545 result = os.popen(command).read()
546 parsedResult = json.loads(result)['results']
547 except:
548 log_error("REST IF has issue: %s" % command)
549 log_error("%s" % result)
550 sys.exit(0)
551
552 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000553# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000554 sport = []
555 links = []
556 for v in parsedResult:
557 srcport = v['_id']
558 try:
559 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
560 print command
561 result = os.popen(command).read()
562 linkResults = json.loads(result)['results']
563 except:
564 log_error("REST IF has issue: %s" % command)
565 log_error("%s" % result)
566 sys.exit(0)
567
568 for p in linkResults:
569 if p.has_key('type') and p['type'] == "port":
570 dstport = p['_id']
571 (sport, sdpid) = portV_to_port_dpid(srcport)
572 (dport, ddpid) = portV_to_port_dpid(dstport)
573 link = {}
574 link["src-switch"]=sdpid
575 link["src-port"]=sport
576 link["src-port-state"]=0
577 link["dst-switch"]=ddpid
578 link["dst-port"]=dport
579 link["dst-port-state"]=0
580 link["type"]="internal"
581 links.append(link)
582
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000583# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000584 js = json.dumps(links)
585 resp = Response(js, status=200, mimetype='application/json')
586 return resp
587
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700588topo_less = {
589 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000590 {"name" : "00:a0", "group" : 1},
591 {"name" : "00:a1", "group" : 1},
592 {"name" : "00:a2", "group" : 1},
593 ],
594 "links" : [
595 {"source" :0, "target": 1},
596 {"source" :1, "target": 0},
597 {"source" :0, "target": 2},
598 {"source" :2, "target": 0},
599 {"source" :1, "target": 2},
600 {"source" :2, "target": 1},
601 ]
602}
603
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700604topo_more = {
605 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000606 {"name" : "00:a3", "group" : 2},
607 {"name" : "00:a0", "group" : 1},
608 {"name" : "00:a1", "group" : 1},
609 {"name" : "00:a2", "group" : 1},
610 ],
611 "links" : [
612 {"source" :1, "target": 2},
613 {"source" :2, "target": 1},
614 {"source" :1, "target": 3},
615 {"source" :3, "target": 1},
616 {"source" :2, "target": 3},
617 {"source" :3, "target": 2},
618 {"source" :0, "target": 2},
619 ]
620}
621
622@app.route("/topology_more")
623def topology_more():
624 topo = topo_more
625 js = json.dumps(topo)
626 resp = Response(js, status=200, mimetype='application/json')
627 return resp
628
629@app.route("/topology_less")
630def topology_less():
631 topo = topo_less
632 js = json.dumps(topo)
633 resp = Response(js, status=200, mimetype='application/json')
634 return resp
635
636cont_status1 = [
637 {"name":"onos9vpc", "onos": 1, "cassandra": 1},
638 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
639 {"name":"onos11vpc", "onos": 1, "cassandra": 0},
640 {"name":"onos12vpc", "onos": 1, "cassandra": 0}]
641
642cont_status2 = [
643 {"name":"onos9vpc", "onos": 0, "cassandra": 1},
644 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
645 {"name":"onos11vpc", "onos": 0, "cassandra": 1},
646 {"name":"onos12vpc", "onos": 0, "cassandra": 1}]
647
648@app.route("/controller_status1")
649def controller_status1():
650 status = cont_status1
651 js = json.dumps(status)
652 resp = Response(js, status=200, mimetype='application/json')
653 pp.pprint(resp)
654 return resp
655
656@app.route("/controller_status2")
657def controller_status2():
658 status = cont_status2
659 js = json.dumps(status)
660 resp = Response(js, status=200, mimetype='application/json')
661 pp.pprint(resp)
662 return resp
663
Ubuntuc016ba12013-02-27 21:53:41 +0000664@app.route("/controller_status")
665def controller_status():
666 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
667 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
668
669 cont_status=[]
670 for i in controllers:
671 status={}
672 onos=os.popen(onos_check % i).read()[:-1]
673 status["name"]=i
674 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000675 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000676 cont_status.append(status)
677
678 js = json.dumps(cont_status)
679 resp = Response(js, status=200, mimetype='application/json')
680 pp.pprint(js)
681 return resp
682
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000683@app.route("/gui/controller/<cmd>/<controller_name>")
684def controller_status_change(cmd, controller_name):
685 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
686 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
687
688 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000689 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000690 ret = "controller %s is up" % (controller_name)
691 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000692 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000693 ret = "controller %s is down" % (controller_name)
694
695 return ret
696
697@app.route("/gui/switch/<cmd>/<dpid>")
698def switch_status_change(cmd, dpid):
699 r = re.compile(':')
700 dpid = re.sub(r, '', dpid)
701 cmd_string="ssh -i ~/.ssh/onlabkey.pem onosgui1 'cd ONOS/scripts; ./switch.sh %s %s'" % (dpid, cmd)
702 get_status="ssh -i ~/.ssh/onlabkey.pem onosgui1 'cd ONOS/scripts; ./switch.sh %s'" % (dpid)
703 print "cmd_string"
704
705 if cmd =="up" or cmd=="down":
706 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000707 os.popen(cmd_string)
708 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000709
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000710 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000711
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000712#* Link Up/Down
713#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
714#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000715
716@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
717def link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
718 if src_dpid in core_switches:
719 host = controllers[0]
720 else:
721 hostid=int(src_dpid.split(':')[-2])
722 host = controllers[hostid-1]
723
724 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
725 print cmd_string
726
727 if cmd =="up" or cmd=="down":
728 result=os.popen(cmd_string).read()
729
730 return result
731
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000732#* Create Flow
733#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000734#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
735@app.route("/gui/addfow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
736def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
737 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
738 print command
739 ret = os.popen(command).read()
740 if ret == "":
741 flow_nr=0
742 else:
743 flow_nr=int(ret)
744
745 flow_nr += 1
746 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)
747 print command
748 errcode = os.popen(command).read()
749 return errcode
750
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000751#* Delete Flow
752#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000753@app.route("/gui/delflow/<flow_id>")
754def del_flow(flow_id):
755 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
756 print command
757 errcode = os.popen(command).read()
758 return errcode
759
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000760#* Start Iperf Througput
761#http://localhost:9000/gui/iperf/start/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000762@app.route("/gui/iperf/start/<flow_id>")
763def iperf_start(flow_id):
764 command = "iperf -xCMSV -t30 -i1 -u -c 127.0.0.1 > iperf_%s.out &" % (flow_id)
765 print command
766 errcode = os.popen(command).read()
767 return errcode
768
769
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000770#* Get Iperf Throughput
771#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000772@app.route("/gui/iperf/rate/<flow_id>")
773def iperf_rate(flow_id):
774 try:
775 command = "curl -s http://%s:%s/iperf_%s.out" % (RestIP, 9000, flow_id)
776 print command
777 result = os.popen(command).read()
778 except:
779 print "REST IF has issue"
780 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000781
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000782 resp = Response(result, status=200, mimetype='text/html')
783 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000784
785
Ubuntu82b8a832013-02-06 22:00:11 +0000786if __name__ == "__main__":
787 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000788# 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")
789# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
790# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
791# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
792# link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1)
793
794
Ubuntu82b8a832013-02-06 22:00:11 +0000795 print "-- query all switches --"
796 query_switch()
797 print "-- query topo --"
798 topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000799# link_change(1,2,3,4)
800 print "-- query all links --"
801 query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000802# print "-- query all devices --"
803# devices()
804 else:
805 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000806 app.run(threaded=True, host="0.0.0.0", port=9000)