blob: f0380464e7a52f8326dfd6e7323a3e03e8766228 [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"
77ONOS_LOCAL_HOST="http://localhost:8080" ;# for Amazon EC2
78
79@app.route("/wm/core/topology/switches/all/json")
80def switches():
81 if request.args.get('proxy') == None:
82 host = ONOS_LOCAL_HOST
83 else:
84 host = ONOS_GUI3_HOST
85
86 try:
87 command = "curl -s %s/wm/core/topology/switches/all/json" % (host)
88 print command
89 result = os.popen(command).read()
90 except:
91 print "REST IF has issue"
92 exit
93
94 resp = Response(result, status=200, mimetype='application/json')
95 return resp
96
97@app.route("/wm/core/topology/links/json")
98def links():
99 if request.args.get('proxy') == None:
100 host = ONOS_LOCAL_HOST
101 else:
102 host = ONOS_GUI3_HOST
103
104 try:
105 command = "curl -s %s/wm/core/topology/links/json" % (host)
106 print command
107 result = os.popen(command).read()
108 except:
109 print "REST IF has issue"
110 exit
111
112 resp = Response(result, status=200, mimetype='application/json')
113 return resp
114
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000115@app.route("/wm/flow/getsummary/0/0/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000116def flows():
117 if request.args.get('proxy') == None:
118 host = ONOS_LOCAL_HOST
119 else:
120 host = ONOS_GUI3_HOST
121
122 try:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000123 command = "curl -s %s/wm/flow/getsummary/0/0/json" % (host)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000124 print command
125 result = os.popen(command).read()
126 except:
127 print "REST IF has issue"
128 exit
129
130 resp = Response(result, status=200, mimetype='application/json')
131 return resp
132
133@app.route("/wm/registry/controllers/json")
134def registry_controllers():
135 if request.args.get('proxy') == None:
136 host = ONOS_LOCAL_HOST
137 else:
138 host = ONOS_GUI3_HOST
139
140 try:
141 command = "curl -s %s/wm/registry/controllers/json" % (host)
142 print command
143 result = os.popen(command).read()
144 except:
145 print "REST IF has issue"
146 exit
147
148 resp = Response(result, status=200, mimetype='application/json')
149 return resp
150
151@app.route("/wm/registry/switches/json")
152def registry_switches():
153 if request.args.get('proxy') == None:
154 host = ONOS_LOCAL_HOST
155 else:
156 host = ONOS_GUI3_HOST
157
158 try:
159 command = "curl -s %s/wm/registry/switches/json" % (host)
160 print command
161 result = os.popen(command).read()
162 except:
163 print "REST IF has issue"
164 exit
165
166 resp = Response(result, status=200, mimetype='application/json')
167 return resp
168
Ubuntu82b8a832013-02-06 22:00:11 +0000169
170def node_id(switch_array, dpid):
171 id = -1
172 for i, val in enumerate(switch_array):
173 if val['name'] == dpid:
174 id = i
175 break
176
177 return id
178
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000179## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000180@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000181def topology_for_gui():
182 try:
183 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
184 result = os.popen(command).read()
185 parsedResult = json.loads(result)
186 except:
187 log_error("REST IF has issue: %s" % command)
188 log_error("%s" % result)
189 sys.exit(0)
190
191 topo = {}
192 switches = []
193 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000194 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000195
196 for v in parsedResult:
197 if v.has_key('dpid'):
198# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
199 dpid = str(v['dpid'])
200 state = str(v['state'])
201 sw = {}
202 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000203 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000204
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000205 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000206 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000207 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000208
Ubuntu37ebda62013-03-01 00:35:31 +0000209## Comment in if we need devies
210# sw_index = len(switches) - 1
211# for p in v['ports']:
212# for d in p['devices']:
213# device = {}
214# device['attached_switch']=dpid
215# device['name']=d['mac']
216# if d['state'] == "ACTIVE":
217# device['group']=1000
218# else:
219# device['group']=1001
220#
221# switches.append(device)
222# device_index = len (switches) -1
223# link = {}
224# link['source'] = device_index
225# link['target'] = sw_index
226# link['type'] = -1
227# links.append(link)
228# link = {}
229# link['source'] = sw_index
230# link['target'] = device_index
231# link['type'] = -1
232# links.append(link)
233
Ubuntu5b2b24a2013-02-27 09:51:13 +0000234# try:
235# command = "curl -s \'http://%s:%s/wm/registry/controllers/json\'" % (RestIP, RestPort)
236# result = os.popen(command).read()
237# controllers = json.loads(result)
238# except:
239# log_error("xx REST IF has issue: %s" % command)
240# log_error("%s" % result)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000241
242 try:
243 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
244 result = os.popen(command).read()
245 parsedResult = json.loads(result)
246 except:
247 log_error("REST IF has issue: %s" % command)
248 log_error("%s" % result)
249
250 for key in parsedResult:
251 dpid = key
252 ctrl = parsedResult[dpid][0]['controllerId']
253 sw_id = node_id(switches, dpid)
254 if sw_id != -1:
255 if switches[sw_id]['group'] != 0:
256 switches[sw_id]['group'] = controllers.index(ctrl) + 1
257
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000258 try:
259 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000260# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000261 p1=1
262 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000263# v2 = "00:00:00:00:00:0d:00:d3"
264 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000265 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
266 result = os.popen(command).read()
267 parsedResult = json.loads(result)
268 except:
269 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000270 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000271
Ubuntu765deff2013-02-28 18:39:13 +0000272 path = []
273 if parsedResult.has_key('flowEntries'):
274 flowEntries= parsedResult['flowEntries']
275 for i, v in enumerate(flowEntries):
276 if i < len(flowEntries) - 1:
277 sdpid= flowEntries[i]['dpid']['value']
278 ddpid = flowEntries[i+1]['dpid']['value']
279 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000280
Ubuntu82b8a832013-02-06 22:00:11 +0000281 try:
282 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
283 result = os.popen(command).read()
284 parsedResult = json.loads(result)
285 except:
286 log_error("REST IF has issue: %s" % command)
287 log_error("%s" % result)
288 sys.exit(0)
289
290 for v in parsedResult:
291 link = {}
292 if v.has_key('dst-switch'):
293 dst_dpid = str(v['dst-switch'])
294 dst_id = node_id(switches, dst_dpid)
295 if v.has_key('src-switch'):
296 src_dpid = str(v['src-switch'])
297 src_id = node_id(switches, src_dpid)
298 link['source'] = src_id
299 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000300
301 onpath = 0
302 for (s,d) in path:
303 if s == v['src-switch'] and d == v['dst-switch']:
304 onpath = 1
305 break
306 link['type'] = onpath
307
Ubuntu82b8a832013-02-06 22:00:11 +0000308 links.append(link)
309
310 topo['nodes'] = switches
311 topo['links'] = links
312
Ubuntu37ebda62013-03-01 00:35:31 +0000313 pp.pprint(topo)
Ubuntu82b8a832013-02-06 22:00:11 +0000314 js = json.dumps(topo)
315 resp = Response(js, status=200, mimetype='application/json')
316 return resp
317
Ubuntuaea2a682013-02-08 08:30:10 +0000318#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
319#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
320@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
321def shortest_path(v1, p1, v2, p2):
322 try:
323 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
324 result = os.popen(command).read()
325 parsedResult = json.loads(result)
326 except:
327 log_error("REST IF has issue: %s" % command)
328 log_error("%s" % result)
329 sys.exit(0)
330
331 topo = {}
332 switches = []
333 links = []
334
335 for v in parsedResult:
336 if v.has_key('dpid'):
337 dpid = str(v['dpid'])
338 state = str(v['state'])
339 sw = {}
340 sw['name']=dpid
341 if str(v['state']) == "ACTIVE":
342 if dpid[-2:-1] == "a":
343 sw['group']=1
344 if dpid[-2:-1] == "b":
345 sw['group']=2
346 if dpid[-2:-1] == "c":
347 sw['group']=3
348 if str(v['state']) == "INACTIVE":
349 sw['group']=0
350
351 switches.append(sw)
352
353 try:
354 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
355 result = os.popen(command).read()
356 parsedResult = json.loads(result)
357 except:
358 log_error("No route")
359 parsedResult = []
360# exit(1)
361
362 path = [];
363 for i, v in enumerate(parsedResult):
364 if i < len(parsedResult) - 1:
365 sdpid= parsedResult[i]['switch']
366 ddpid = parsedResult[i+1]['switch']
367 path.append( (sdpid, ddpid))
368
369 try:
370 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
371 result = os.popen(command).read()
372 parsedResult = json.loads(result)
373 except:
374 log_error("REST IF has issue: %s" % command)
375 log_error("%s" % result)
376 sys.exit(0)
377
378 for v in parsedResult:
379 link = {}
380 if v.has_key('dst-switch'):
381 dst_dpid = str(v['dst-switch'])
382 dst_id = node_id(switches, dst_dpid)
383 if v.has_key('src-switch'):
384 src_dpid = str(v['src-switch'])
385 src_id = node_id(switches, src_dpid)
386 link['source'] = src_id
387 link['target'] = dst_id
388 onpath = 0
389 for (s,d) in path:
390 if s == v['src-switch'] and d == v['dst-switch']:
391 onpath = 1
392 break
393
394 link['type'] = onpath
395 links.append(link)
396
397 topo['nodes'] = switches
398 topo['links'] = links
399
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000400# pp.pprint(topo)
Ubuntuaea2a682013-02-08 08:30:10 +0000401 js = json.dumps(topo)
402 resp = Response(js, status=200, mimetype='application/json')
403 return resp
404
Ubuntu82b8a832013-02-06 22:00:11 +0000405@app.route("/wm/core/controller/switches/json")
406def query_switch():
407 try:
408 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
409# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000410 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000411 result = os.popen(command).read()
412 parsedResult = json.loads(result)
413 except:
414 log_error("REST IF has issue: %s" % command)
415 log_error("%s" % result)
416 sys.exit(0)
417
418# print command
419# print result
420 switches_ = []
421 for v in parsedResult:
422 if v.has_key('dpid'):
423 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
424 dpid = str(v['dpid'])
425 state = str(v['state'])
426 sw = {}
427 sw['dpid']=dpid
428 sw['active']=state
429 switches_.append(sw)
430
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000431# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000432 js = json.dumps(switches_)
433 resp = Response(js, status=200, mimetype='application/json')
434 return resp
435
436@app.route("/wm/device/")
437def devices():
438 try:
439 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
440 result = os.popen(command).read()
441 parsedResult = json.loads(result)['results']
442 except:
443 log_error("REST IF has issue: %s" % command)
444 log_error("%s" % result)
445 sys.exit(0)
446
447 devices = []
448 for v in parsedResult:
449 dl_addr = v['dl_addr']
450 nw_addr = v['nw_addr']
451 vertex = v['_id']
452 mac = []
453 mac.append(dl_addr)
454 ip = []
455 ip.append(nw_addr)
456 device = {}
457 device['entryClass']="DefaultEntryClass"
458 device['mac']=mac
459 device['ipv4']=ip
460 device['vlan']=[]
461 device['lastSeen']=0
462 attachpoints =[]
463
464 port, dpid = deviceV_to_attachpoint(vertex)
465 attachpoint = {}
466 attachpoint['port']=port
467 attachpoint['switchDPID']=dpid
468 attachpoints.append(attachpoint)
469 device['attachmentPoint']=attachpoints
470 devices.append(device)
471
472 print devices
473 js = json.dumps(devices)
474 resp = Response(js, status=200, mimetype='application/json')
475 return resp
476
477#{"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}
478
Ubuntu82b8a832013-02-06 22:00:11 +0000479## return fake stat for now
480@app.route("/wm/core/switch/<switchId>/<statType>/json")
481def switch_stat(switchId, statType):
482 if statType == "desc":
483 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
484 ret = {}
485 ret[switchId]=desc
486 elif statType == "aggregate":
487 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
488 ret = {}
489 ret[switchId]=aggr
490 else:
491 ret = {}
492
493 js = json.dumps(ret)
494 resp = Response(js, status=200, mimetype='application/json')
495 return resp
496
497
498@app.route("/wm/topology/links/json")
499def query_links():
500 try:
501 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000502 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000503 result = os.popen(command).read()
504 parsedResult = json.loads(result)['results']
505 except:
506 log_error("REST IF has issue: %s" % command)
507 log_error("%s" % result)
508 sys.exit(0)
509
510 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000511# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000512 sport = []
513 links = []
514 for v in parsedResult:
515 srcport = v['_id']
516 try:
517 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
518 print command
519 result = os.popen(command).read()
520 linkResults = json.loads(result)['results']
521 except:
522 log_error("REST IF has issue: %s" % command)
523 log_error("%s" % result)
524 sys.exit(0)
525
526 for p in linkResults:
527 if p.has_key('type') and p['type'] == "port":
528 dstport = p['_id']
529 (sport, sdpid) = portV_to_port_dpid(srcport)
530 (dport, ddpid) = portV_to_port_dpid(dstport)
531 link = {}
532 link["src-switch"]=sdpid
533 link["src-port"]=sport
534 link["src-port-state"]=0
535 link["dst-switch"]=ddpid
536 link["dst-port"]=dport
537 link["dst-port-state"]=0
538 link["type"]="internal"
539 links.append(link)
540
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000541# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000542 js = json.dumps(links)
543 resp = Response(js, status=200, mimetype='application/json')
544 return resp
545
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000546topo_less = {
547 "nodes" : [
548 {"name" : "00:a0", "group" : 1},
549 {"name" : "00:a1", "group" : 1},
550 {"name" : "00:a2", "group" : 1},
551 ],
552 "links" : [
553 {"source" :0, "target": 1},
554 {"source" :1, "target": 0},
555 {"source" :0, "target": 2},
556 {"source" :2, "target": 0},
557 {"source" :1, "target": 2},
558 {"source" :2, "target": 1},
559 ]
560}
561
562topo_more = {
563 "nodes" : [
564 {"name" : "00:a3", "group" : 2},
565 {"name" : "00:a0", "group" : 1},
566 {"name" : "00:a1", "group" : 1},
567 {"name" : "00:a2", "group" : 1},
568 ],
569 "links" : [
570 {"source" :1, "target": 2},
571 {"source" :2, "target": 1},
572 {"source" :1, "target": 3},
573 {"source" :3, "target": 1},
574 {"source" :2, "target": 3},
575 {"source" :3, "target": 2},
576 {"source" :0, "target": 2},
577 ]
578}
579
580@app.route("/topology_more")
581def topology_more():
582 topo = topo_more
583 js = json.dumps(topo)
584 resp = Response(js, status=200, mimetype='application/json')
585 return resp
586
587@app.route("/topology_less")
588def topology_less():
589 topo = topo_less
590 js = json.dumps(topo)
591 resp = Response(js, status=200, mimetype='application/json')
592 return resp
593
594cont_status1 = [
595 {"name":"onos9vpc", "onos": 1, "cassandra": 1},
596 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
597 {"name":"onos11vpc", "onos": 1, "cassandra": 0},
598 {"name":"onos12vpc", "onos": 1, "cassandra": 0}]
599
600cont_status2 = [
601 {"name":"onos9vpc", "onos": 0, "cassandra": 1},
602 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
603 {"name":"onos11vpc", "onos": 0, "cassandra": 1},
604 {"name":"onos12vpc", "onos": 0, "cassandra": 1}]
605
606@app.route("/controller_status1")
607def controller_status1():
608 status = cont_status1
609 js = json.dumps(status)
610 resp = Response(js, status=200, mimetype='application/json')
611 pp.pprint(resp)
612 return resp
613
614@app.route("/controller_status2")
615def controller_status2():
616 status = cont_status2
617 js = json.dumps(status)
618 resp = Response(js, status=200, mimetype='application/json')
619 pp.pprint(resp)
620 return resp
621
Ubuntuc016ba12013-02-27 21:53:41 +0000622@app.route("/controller_status")
623def controller_status():
624 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
625 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
626
627 cont_status=[]
628 for i in controllers:
629 status={}
630 onos=os.popen(onos_check % i).read()[:-1]
631 status["name"]=i
632 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000633 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000634 cont_status.append(status)
635
636 js = json.dumps(cont_status)
637 resp = Response(js, status=200, mimetype='application/json')
638 pp.pprint(js)
639 return resp
640
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000641@app.route("/gui/controller/<cmd>/<controller_name>")
642def controller_status_change(cmd, controller_name):
643 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
644 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
645
646 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000647 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000648 ret = "controller %s is up" % (controller_name)
649 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000650 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000651 ret = "controller %s is down" % (controller_name)
652
653 return ret
654
655@app.route("/gui/switch/<cmd>/<dpid>")
656def switch_status_change(cmd, dpid):
657 r = re.compile(':')
658 dpid = re.sub(r, '', dpid)
659 cmd_string="ssh -i ~/.ssh/onlabkey.pem onosgui1 'cd ONOS/scripts; ./switch.sh %s %s'" % (dpid, cmd)
660 get_status="ssh -i ~/.ssh/onlabkey.pem onosgui1 'cd ONOS/scripts; ./switch.sh %s'" % (dpid)
661 print "cmd_string"
662
663 if cmd =="up" or cmd=="down":
664 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000665 os.popen(cmd_string)
666 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000667
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000668 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000669
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000670#* Link Up/Down
671#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
672#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000673
674@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
675def link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
676 if src_dpid in core_switches:
677 host = controllers[0]
678 else:
679 hostid=int(src_dpid.split(':')[-2])
680 host = controllers[hostid-1]
681
682 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
683 print cmd_string
684
685 if cmd =="up" or cmd=="down":
686 result=os.popen(cmd_string).read()
687
688 return result
689
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000690#* Create Flow
691#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000692#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
693@app.route("/gui/addfow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
694def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
695 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
696 print command
697 ret = os.popen(command).read()
698 if ret == "":
699 flow_nr=0
700 else:
701 flow_nr=int(ret)
702
703 flow_nr += 1
704 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)
705 print command
706 errcode = os.popen(command).read()
707 return errcode
708
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000709#* Delete Flow
710#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000711@app.route("/gui/delflow/<flow_id>")
712def del_flow(flow_id):
713 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
714 print command
715 errcode = os.popen(command).read()
716 return errcode
717
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000718#* Start Iperf Througput
719#http://localhost:9000/gui/iperf/start/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000720@app.route("/gui/iperf/start/<flow_id>")
721def iperf_start(flow_id):
722 command = "iperf -xCMSV -t30 -i1 -u -c 127.0.0.1 > iperf_%s.out &" % (flow_id)
723 print command
724 errcode = os.popen(command).read()
725 return errcode
726
727
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000728#* Get Iperf Throughput
729#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000730@app.route("/gui/iperf/rate/<flow_id>")
731def iperf_rate(flow_id):
732 try:
733 command = "curl -s http://%s:%s/iperf_%s.out" % (RestIP, 9000, flow_id)
734 print command
735 result = os.popen(command).read()
736 except:
737 print "REST IF has issue"
738 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000739
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000740 resp = Response(result, status=200, mimetype='text/html')
741 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000742
743
Ubuntu82b8a832013-02-06 22:00:11 +0000744if __name__ == "__main__":
745 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000746# 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")
747# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
748# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
749# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
750# link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1)
751
752
Ubuntu82b8a832013-02-06 22:00:11 +0000753 print "-- query all switches --"
754 query_switch()
755 print "-- query topo --"
756 topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000757# link_change(1,2,3,4)
758 print "-- query all links --"
759 query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000760# print "-- query all devices --"
761# devices()
762 else:
763 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000764 app.run(threaded=True, host="0.0.0.0", port=9000)