blob: 3d0a6d3173882d727166949ca428f39e13fa4dee [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"
72
73 return response
74
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000075## PROXY API (allows development where the webui is served from someplace other than the controller)##
76ONOS_GUI3_HOST="http://gui3.onlab.us:8080"
Paul Greyson8d1c6362013-03-27 13:05:24 -070077ONOS_GUI3_CONTROL_HOST="http://gui3.onlab.us:8081"
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000078ONOS_LOCAL_HOST="http://localhost:8080" ;# for Amazon EC2
79
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070080@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
81def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
82 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -070083 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 -070084 print command
85 result = os.popen(command).read()
86 except:
87 print "REST IF has issue"
88 exit
89
90 resp = Response(result, status=200, mimetype='application/json')
91 return resp
92
Paul Greyson8d1c6362013-03-27 13:05:24 -070093@app.route("/proxy/gui/switch/<cmd>/<dpid>")
94def proxy_switch_status_change(cmd, dpid):
95 try:
96 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
97 print command
98 result = os.popen(command).read()
99 except:
100 print "REST IF has issue"
101 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700102
Paul Greyson8d1c6362013-03-27 13:05:24 -0700103 resp = Response(result, status=200, mimetype='application/json')
104 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700105
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000106@app.route("/wm/core/topology/switches/all/json")
107def switches():
108 if request.args.get('proxy') == None:
109 host = ONOS_LOCAL_HOST
110 else:
111 host = ONOS_GUI3_HOST
112
113 try:
114 command = "curl -s %s/wm/core/topology/switches/all/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700115# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000116 result = os.popen(command).read()
117 except:
118 print "REST IF has issue"
119 exit
120
121 resp = Response(result, status=200, mimetype='application/json')
122 return resp
123
124@app.route("/wm/core/topology/links/json")
125def links():
126 if request.args.get('proxy') == None:
127 host = ONOS_LOCAL_HOST
128 else:
129 host = ONOS_GUI3_HOST
130
131 try:
132 command = "curl -s %s/wm/core/topology/links/json" % (host)
Paul Greyson8d1c6362013-03-27 13:05:24 -0700133 print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000134 result = os.popen(command).read()
135 except:
136 print "REST IF has issue"
137 exit
138
139 resp = Response(result, status=200, mimetype='application/json')
140 return resp
141
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000142@app.route("/wm/flow/getsummary/0/0/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000143def flows():
144 if request.args.get('proxy') == None:
145 host = ONOS_LOCAL_HOST
146 else:
147 host = ONOS_GUI3_HOST
148
149 try:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000150 command = "curl -s %s/wm/flow/getsummary/0/0/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700151# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000152 result = os.popen(command).read()
153 except:
154 print "REST IF has issue"
155 exit
156
157 resp = Response(result, status=200, mimetype='application/json')
158 return resp
159
160@app.route("/wm/registry/controllers/json")
161def registry_controllers():
162 if request.args.get('proxy') == None:
163 host = ONOS_LOCAL_HOST
164 else:
165 host = ONOS_GUI3_HOST
166
167 try:
168 command = "curl -s %s/wm/registry/controllers/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700169# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000170 result = os.popen(command).read()
171 except:
172 print "REST IF has issue"
173 exit
174
175 resp = Response(result, status=200, mimetype='application/json')
176 return resp
177
178@app.route("/wm/registry/switches/json")
179def registry_switches():
180 if request.args.get('proxy') == None:
181 host = ONOS_LOCAL_HOST
182 else:
183 host = ONOS_GUI3_HOST
184
185 try:
186 command = "curl -s %s/wm/registry/switches/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700187# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000188 result = os.popen(command).read()
189 except:
190 print "REST IF has issue"
191 exit
192
193 resp = Response(result, status=200, mimetype='application/json')
194 return resp
195
Ubuntu82b8a832013-02-06 22:00:11 +0000196
197def node_id(switch_array, dpid):
198 id = -1
199 for i, val in enumerate(switch_array):
200 if val['name'] == dpid:
201 id = i
202 break
203
204 return id
205
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000206## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000207@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000208def topology_for_gui():
209 try:
210 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
211 result = os.popen(command).read()
212 parsedResult = json.loads(result)
213 except:
214 log_error("REST IF has issue: %s" % command)
215 log_error("%s" % result)
216 sys.exit(0)
217
218 topo = {}
219 switches = []
220 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000221 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000222
223 for v in parsedResult:
224 if v.has_key('dpid'):
225# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
226 dpid = str(v['dpid'])
227 state = str(v['state'])
228 sw = {}
229 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000230 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000231
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000232 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000233 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000234 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000235
Ubuntu37ebda62013-03-01 00:35:31 +0000236## Comment in if we need devies
237# sw_index = len(switches) - 1
238# for p in v['ports']:
239# for d in p['devices']:
240# device = {}
241# device['attached_switch']=dpid
242# device['name']=d['mac']
243# if d['state'] == "ACTIVE":
244# device['group']=1000
245# else:
246# device['group']=1001
247#
248# switches.append(device)
249# device_index = len (switches) -1
250# link = {}
251# link['source'] = device_index
252# link['target'] = sw_index
253# link['type'] = -1
254# links.append(link)
255# link = {}
256# link['source'] = sw_index
257# link['target'] = device_index
258# link['type'] = -1
259# links.append(link)
260
Ubuntu5b2b24a2013-02-27 09:51:13 +0000261# try:
262# command = "curl -s \'http://%s:%s/wm/registry/controllers/json\'" % (RestIP, RestPort)
263# result = os.popen(command).read()
264# controllers = json.loads(result)
265# except:
266# log_error("xx REST IF has issue: %s" % command)
267# log_error("%s" % result)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000268
269 try:
270 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
271 result = os.popen(command).read()
272 parsedResult = json.loads(result)
273 except:
274 log_error("REST IF has issue: %s" % command)
275 log_error("%s" % result)
276
277 for key in parsedResult:
278 dpid = key
279 ctrl = parsedResult[dpid][0]['controllerId']
280 sw_id = node_id(switches, dpid)
281 if sw_id != -1:
282 if switches[sw_id]['group'] != 0:
283 switches[sw_id]['group'] = controllers.index(ctrl) + 1
284
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000285 try:
286 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000287# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000288 p1=1
289 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000290# v2 = "00:00:00:00:00:0d:00:d3"
291 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000292 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
293 result = os.popen(command).read()
294 parsedResult = json.loads(result)
295 except:
296 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000297 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000298
Ubuntu765deff2013-02-28 18:39:13 +0000299 path = []
300 if parsedResult.has_key('flowEntries'):
301 flowEntries= parsedResult['flowEntries']
302 for i, v in enumerate(flowEntries):
303 if i < len(flowEntries) - 1:
304 sdpid= flowEntries[i]['dpid']['value']
305 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700306 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000307
Ubuntu82b8a832013-02-06 22:00:11 +0000308 try:
309 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
310 result = os.popen(command).read()
311 parsedResult = json.loads(result)
312 except:
313 log_error("REST IF has issue: %s" % command)
314 log_error("%s" % result)
315 sys.exit(0)
316
317 for v in parsedResult:
318 link = {}
319 if v.has_key('dst-switch'):
320 dst_dpid = str(v['dst-switch'])
321 dst_id = node_id(switches, dst_dpid)
322 if v.has_key('src-switch'):
323 src_dpid = str(v['src-switch'])
324 src_id = node_id(switches, src_dpid)
325 link['source'] = src_id
326 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000327
328 onpath = 0
329 for (s,d) in path:
330 if s == v['src-switch'] and d == v['dst-switch']:
331 onpath = 1
332 break
333 link['type'] = onpath
334
Ubuntu82b8a832013-02-06 22:00:11 +0000335 links.append(link)
336
337 topo['nodes'] = switches
338 topo['links'] = links
339
Ubuntu37ebda62013-03-01 00:35:31 +0000340 pp.pprint(topo)
Ubuntu82b8a832013-02-06 22:00:11 +0000341 js = json.dumps(topo)
342 resp = Response(js, status=200, mimetype='application/json')
343 return resp
344
Ubuntuaea2a682013-02-08 08:30:10 +0000345#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
346#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
347@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
348def shortest_path(v1, p1, v2, p2):
349 try:
350 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
351 result = os.popen(command).read()
352 parsedResult = json.loads(result)
353 except:
354 log_error("REST IF has issue: %s" % command)
355 log_error("%s" % result)
356 sys.exit(0)
357
358 topo = {}
359 switches = []
360 links = []
361
362 for v in parsedResult:
363 if v.has_key('dpid'):
364 dpid = str(v['dpid'])
365 state = str(v['state'])
366 sw = {}
367 sw['name']=dpid
368 if str(v['state']) == "ACTIVE":
369 if dpid[-2:-1] == "a":
370 sw['group']=1
371 if dpid[-2:-1] == "b":
372 sw['group']=2
373 if dpid[-2:-1] == "c":
374 sw['group']=3
375 if str(v['state']) == "INACTIVE":
376 sw['group']=0
377
378 switches.append(sw)
379
380 try:
381 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
382 result = os.popen(command).read()
383 parsedResult = json.loads(result)
384 except:
385 log_error("No route")
386 parsedResult = []
387# exit(1)
388
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700389 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000390 for i, v in enumerate(parsedResult):
391 if i < len(parsedResult) - 1:
392 sdpid= parsedResult[i]['switch']
393 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700394 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000395
396 try:
397 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
398 result = os.popen(command).read()
399 parsedResult = json.loads(result)
400 except:
401 log_error("REST IF has issue: %s" % command)
402 log_error("%s" % result)
403 sys.exit(0)
404
405 for v in parsedResult:
406 link = {}
407 if v.has_key('dst-switch'):
408 dst_dpid = str(v['dst-switch'])
409 dst_id = node_id(switches, dst_dpid)
410 if v.has_key('src-switch'):
411 src_dpid = str(v['src-switch'])
412 src_id = node_id(switches, src_dpid)
413 link['source'] = src_id
414 link['target'] = dst_id
415 onpath = 0
416 for (s,d) in path:
417 if s == v['src-switch'] and d == v['dst-switch']:
418 onpath = 1
419 break
420
421 link['type'] = onpath
422 links.append(link)
423
424 topo['nodes'] = switches
425 topo['links'] = links
426
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000427# pp.pprint(topo)
Ubuntuaea2a682013-02-08 08:30:10 +0000428 js = json.dumps(topo)
429 resp = Response(js, status=200, mimetype='application/json')
430 return resp
431
Ubuntu82b8a832013-02-06 22:00:11 +0000432@app.route("/wm/core/controller/switches/json")
433def query_switch():
434 try:
435 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
436# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000437 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000438 result = os.popen(command).read()
439 parsedResult = json.loads(result)
440 except:
441 log_error("REST IF has issue: %s" % command)
442 log_error("%s" % result)
443 sys.exit(0)
444
445# print command
446# print result
447 switches_ = []
448 for v in parsedResult:
449 if v.has_key('dpid'):
450 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
451 dpid = str(v['dpid'])
452 state = str(v['state'])
453 sw = {}
454 sw['dpid']=dpid
455 sw['active']=state
456 switches_.append(sw)
457
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000458# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000459 js = json.dumps(switches_)
460 resp = Response(js, status=200, mimetype='application/json')
461 return resp
462
463@app.route("/wm/device/")
464def devices():
465 try:
466 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
467 result = os.popen(command).read()
468 parsedResult = json.loads(result)['results']
469 except:
470 log_error("REST IF has issue: %s" % command)
471 log_error("%s" % result)
472 sys.exit(0)
473
474 devices = []
475 for v in parsedResult:
476 dl_addr = v['dl_addr']
477 nw_addr = v['nw_addr']
478 vertex = v['_id']
479 mac = []
480 mac.append(dl_addr)
481 ip = []
482 ip.append(nw_addr)
483 device = {}
484 device['entryClass']="DefaultEntryClass"
485 device['mac']=mac
486 device['ipv4']=ip
487 device['vlan']=[]
488 device['lastSeen']=0
489 attachpoints =[]
490
491 port, dpid = deviceV_to_attachpoint(vertex)
492 attachpoint = {}
493 attachpoint['port']=port
494 attachpoint['switchDPID']=dpid
495 attachpoints.append(attachpoint)
496 device['attachmentPoint']=attachpoints
497 devices.append(device)
498
499 print devices
500 js = json.dumps(devices)
501 resp = Response(js, status=200, mimetype='application/json')
502 return resp
503
504#{"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}
505
Ubuntu82b8a832013-02-06 22:00:11 +0000506## return fake stat for now
507@app.route("/wm/core/switch/<switchId>/<statType>/json")
508def switch_stat(switchId, statType):
509 if statType == "desc":
510 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
511 ret = {}
512 ret[switchId]=desc
513 elif statType == "aggregate":
514 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
515 ret = {}
516 ret[switchId]=aggr
517 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700518 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000519
520 js = json.dumps(ret)
521 resp = Response(js, status=200, mimetype='application/json')
522 return resp
523
524
525@app.route("/wm/topology/links/json")
526def query_links():
527 try:
528 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000529 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000530 result = os.popen(command).read()
531 parsedResult = json.loads(result)['results']
532 except:
533 log_error("REST IF has issue: %s" % command)
534 log_error("%s" % result)
535 sys.exit(0)
536
537 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000538# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000539 sport = []
540 links = []
541 for v in parsedResult:
542 srcport = v['_id']
543 try:
544 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
545 print command
546 result = os.popen(command).read()
547 linkResults = 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 for p in linkResults:
554 if p.has_key('type') and p['type'] == "port":
555 dstport = p['_id']
556 (sport, sdpid) = portV_to_port_dpid(srcport)
557 (dport, ddpid) = portV_to_port_dpid(dstport)
558 link = {}
559 link["src-switch"]=sdpid
560 link["src-port"]=sport
561 link["src-port-state"]=0
562 link["dst-switch"]=ddpid
563 link["dst-port"]=dport
564 link["dst-port-state"]=0
565 link["type"]="internal"
566 links.append(link)
567
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000568# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000569 js = json.dumps(links)
570 resp = Response(js, status=200, mimetype='application/json')
571 return resp
572
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700573topo_less = {
574 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000575 {"name" : "00:a0", "group" : 1},
576 {"name" : "00:a1", "group" : 1},
577 {"name" : "00:a2", "group" : 1},
578 ],
579 "links" : [
580 {"source" :0, "target": 1},
581 {"source" :1, "target": 0},
582 {"source" :0, "target": 2},
583 {"source" :2, "target": 0},
584 {"source" :1, "target": 2},
585 {"source" :2, "target": 1},
586 ]
587}
588
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700589topo_more = {
590 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000591 {"name" : "00:a3", "group" : 2},
592 {"name" : "00:a0", "group" : 1},
593 {"name" : "00:a1", "group" : 1},
594 {"name" : "00:a2", "group" : 1},
595 ],
596 "links" : [
597 {"source" :1, "target": 2},
598 {"source" :2, "target": 1},
599 {"source" :1, "target": 3},
600 {"source" :3, "target": 1},
601 {"source" :2, "target": 3},
602 {"source" :3, "target": 2},
603 {"source" :0, "target": 2},
604 ]
605}
606
607@app.route("/topology_more")
608def topology_more():
609 topo = topo_more
610 js = json.dumps(topo)
611 resp = Response(js, status=200, mimetype='application/json')
612 return resp
613
614@app.route("/topology_less")
615def topology_less():
616 topo = topo_less
617 js = json.dumps(topo)
618 resp = Response(js, status=200, mimetype='application/json')
619 return resp
620
621cont_status1 = [
622 {"name":"onos9vpc", "onos": 1, "cassandra": 1},
623 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
624 {"name":"onos11vpc", "onos": 1, "cassandra": 0},
625 {"name":"onos12vpc", "onos": 1, "cassandra": 0}]
626
627cont_status2 = [
628 {"name":"onos9vpc", "onos": 0, "cassandra": 1},
629 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
630 {"name":"onos11vpc", "onos": 0, "cassandra": 1},
631 {"name":"onos12vpc", "onos": 0, "cassandra": 1}]
632
633@app.route("/controller_status1")
634def controller_status1():
635 status = cont_status1
636 js = json.dumps(status)
637 resp = Response(js, status=200, mimetype='application/json')
638 pp.pprint(resp)
639 return resp
640
641@app.route("/controller_status2")
642def controller_status2():
643 status = cont_status2
644 js = json.dumps(status)
645 resp = Response(js, status=200, mimetype='application/json')
646 pp.pprint(resp)
647 return resp
648
Ubuntuc016ba12013-02-27 21:53:41 +0000649@app.route("/controller_status")
650def controller_status():
651 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
652 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
653
654 cont_status=[]
655 for i in controllers:
656 status={}
657 onos=os.popen(onos_check % i).read()[:-1]
658 status["name"]=i
659 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000660 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000661 cont_status.append(status)
662
663 js = json.dumps(cont_status)
664 resp = Response(js, status=200, mimetype='application/json')
665 pp.pprint(js)
666 return resp
667
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000668@app.route("/gui/controller/<cmd>/<controller_name>")
669def controller_status_change(cmd, controller_name):
670 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
671 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
672
673 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000674 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000675 ret = "controller %s is up" % (controller_name)
676 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000677 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000678 ret = "controller %s is down" % (controller_name)
679
680 return ret
681
682@app.route("/gui/switch/<cmd>/<dpid>")
683def switch_status_change(cmd, dpid):
684 r = re.compile(':')
685 dpid = re.sub(r, '', dpid)
686 cmd_string="ssh -i ~/.ssh/onlabkey.pem onosgui1 'cd ONOS/scripts; ./switch.sh %s %s'" % (dpid, cmd)
687 get_status="ssh -i ~/.ssh/onlabkey.pem onosgui1 'cd ONOS/scripts; ./switch.sh %s'" % (dpid)
688 print "cmd_string"
689
690 if cmd =="up" or cmd=="down":
691 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000692 os.popen(cmd_string)
693 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000694
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000695 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000696
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000697#* Link Up/Down
698#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
699#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000700
701@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
702def link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
703 if src_dpid in core_switches:
704 host = controllers[0]
705 else:
706 hostid=int(src_dpid.split(':')[-2])
707 host = controllers[hostid-1]
708
709 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
710 print cmd_string
711
712 if cmd =="up" or cmd=="down":
713 result=os.popen(cmd_string).read()
714
715 return result
716
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000717#* Create Flow
718#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000719#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
720@app.route("/gui/addfow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
721def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
722 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
723 print command
724 ret = os.popen(command).read()
725 if ret == "":
726 flow_nr=0
727 else:
728 flow_nr=int(ret)
729
730 flow_nr += 1
731 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)
732 print command
733 errcode = os.popen(command).read()
734 return errcode
735
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000736#* Delete Flow
737#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000738@app.route("/gui/delflow/<flow_id>")
739def del_flow(flow_id):
740 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
741 print command
742 errcode = os.popen(command).read()
743 return errcode
744
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000745#* Start Iperf Througput
746#http://localhost:9000/gui/iperf/start/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000747@app.route("/gui/iperf/start/<flow_id>")
748def iperf_start(flow_id):
749 command = "iperf -xCMSV -t30 -i1 -u -c 127.0.0.1 > iperf_%s.out &" % (flow_id)
750 print command
751 errcode = os.popen(command).read()
752 return errcode
753
754
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000755#* Get Iperf Throughput
756#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000757@app.route("/gui/iperf/rate/<flow_id>")
758def iperf_rate(flow_id):
759 try:
760 command = "curl -s http://%s:%s/iperf_%s.out" % (RestIP, 9000, flow_id)
761 print command
762 result = os.popen(command).read()
763 except:
764 print "REST IF has issue"
765 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000766
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000767 resp = Response(result, status=200, mimetype='text/html')
768 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000769
770
Ubuntu82b8a832013-02-06 22:00:11 +0000771if __name__ == "__main__":
772 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000773# 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")
774# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
775# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
776# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
777# link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1)
778
779
Ubuntu82b8a832013-02-06 22:00:11 +0000780 print "-- query all switches --"
781 query_switch()
782 print "-- query topo --"
783 topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000784# link_change(1,2,3,4)
785 print "-- query all links --"
786 query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000787# print "-- query all devices --"
788# devices()
789 else:
790 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000791 app.run(threaded=True, host="0.0.0.0", port=9000)