blob: e4a2684498844ffcd187736d9d393057ef060061 [file] [log] [blame]
Ubuntu82b8a832013-02-06 22:00:11 +00001#! /usr/bin/env python
2import pprint
3import os
4import sys
5import subprocess
6import json
7import argparse
8import io
9import time
10
Masayoshi Kobayashi51011522013-03-27 00:18:12 +000011import re
12
Ubuntu82b8a832013-02-06 22:00:11 +000013from flask import Flask, json, Response, render_template, make_response, request
14
15## Global Var ##
Ubuntuf6ce96c2013-02-07 01:45:07 +000016RestIP="localhost"
Ubuntu82b8a832013-02-06 22:00:11 +000017RestPort=8080
18#DBName="onos-network-map"
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000019
20## Uncomment the desired block based on your testbed environment
21
22# Settings for running on production
23controllers=["onosgui1", "onosgui2", "onosgui3", "onosgui4", "onosgui5", "onosgui6", "onosgui7", "onosgui8"]
24core_switches=["00:00:00:00:ba:5e:ba:11", "00:00:00:00:00:00:ba:12", "00:00:20:4e:7f:51:8a:35", "00:00:00:00:ba:5e:ba:13", "00:00:00:08:a2:08:f9:01", "00:00:00:16:97:08:9a:46"]
25ONOS_GUI3_HOST="http://gui3.onlab.us:8080"
26ONOS_GUI3_CONTROL_HOST="http://gui3.onlab.us:8081"
27
28# Settings for running on dev testbed. Replace dev
29#controllers=["onosdevb1", "onosdevb2", "onosdevb3", "onosdevb4"]
30#core_switches=["00:00:00:00:00:00:01:01", "00:00:00:00:00:00:01:02", "00:00:00:00:00:00:01:03", "00:00:00:00:00:00:01:04", "00:00:00:00:00:00:01:05", "00:00:00:00:00:00:01:06"]
31#ONOS_GUI3_HOST="http://devb-gui.onlab.us:8080"
32#ONOS_GUI3_CONTROL_HOST="http://devb-gui.onlab.us:8080"
33
34ONOS_LOCAL_HOST="http://localhost:8080" ;# for Amazon EC2
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +000035
36nr_flow=0
Ubuntu82b8a832013-02-06 22:00:11 +000037
38DEBUG=1
39pp = pprint.PrettyPrinter(indent=4)
40
41app = Flask(__name__)
42
43## Worker Functions ##
44def log_error(txt):
45 print '%s' % (txt)
46
47def debug(txt):
48 if DEBUG:
49 print '%s' % (txt)
50
51## Rest APIs ##
52### File Fetch ###
53@app.route('/ui/img/<filename>', methods=['GET'])
54@app.route('/img/<filename>', methods=['GET'])
55@app.route('/css/<filename>', methods=['GET'])
56@app.route('/js/models/<filename>', methods=['GET'])
57@app.route('/js/views/<filename>', methods=['GET'])
58@app.route('/js/<filename>', methods=['GET'])
59@app.route('/lib/<filename>', methods=['GET'])
60@app.route('/', methods=['GET'])
61@app.route('/<filename>', methods=['GET'])
62@app.route('/tpl/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000063@app.route('/ons-demo/<filename>', methods=['GET'])
64@app.route('/ons-demo/js/<filename>', methods=['GET'])
65@app.route('/ons-demo/css/<filename>', methods=['GET'])
66@app.route('/ons-demo/assets/<filename>', methods=['GET'])
67@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000068def return_file(filename="index.html"):
69 if request.path == "/":
70 fullpath = "./index.html"
71 else:
72 fullpath = str(request.path)[1:]
73
74 response = make_response(open(fullpath).read())
75 suffix = fullpath.split(".")[-1]
76
77 if suffix == "html" or suffix == "htm":
78 response.headers["Content-type"] = "text/html"
79 elif suffix == "js":
80 response.headers["Content-type"] = "application/javascript"
81 elif suffix == "css":
82 response.headers["Content-type"] = "text/css"
83 elif suffix == "png":
84 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -070085 elif suffix == "svg":
86 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000087
88 return response
89
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000090
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070091@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
92def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
93 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -070094 command = "curl -s %s/gui/link/%s/%s/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, src_dpid, src_port, dst_dpid, dst_port)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070095 print command
96 result = os.popen(command).read()
97 except:
98 print "REST IF has issue"
99 exit
100
101 resp = Response(result, status=200, mimetype='application/json')
102 return resp
103
Paul Greyson8d1c6362013-03-27 13:05:24 -0700104@app.route("/proxy/gui/switch/<cmd>/<dpid>")
105def proxy_switch_status_change(cmd, dpid):
106 try:
107 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
108 print command
109 result = os.popen(command).read()
110 except:
111 print "REST IF has issue"
112 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700113
Paul Greyson8d1c6362013-03-27 13:05:24 -0700114 resp = Response(result, status=200, mimetype='application/json')
115 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700116
Paul Greyson2913af82013-03-27 14:53:17 -0700117@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
118def proxy_controller_status_change(cmd, controller_name):
119 try:
120 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
121 print command
122 result = os.popen(command).read()
123 except:
124 print "REST IF has issue"
125 exit
126
127 resp = Response(result, status=200, mimetype='application/json')
128 return resp
129
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000130@app.route("/wm/core/topology/switches/all/json")
131def switches():
132 if request.args.get('proxy') == None:
133 host = ONOS_LOCAL_HOST
134 else:
135 host = ONOS_GUI3_HOST
136
137 try:
138 command = "curl -s %s/wm/core/topology/switches/all/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700139# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000140 result = os.popen(command).read()
141 except:
142 print "REST IF has issue"
143 exit
144
145 resp = Response(result, status=200, mimetype='application/json')
146 return resp
147
148@app.route("/wm/core/topology/links/json")
149def links():
150 if request.args.get('proxy') == None:
151 host = ONOS_LOCAL_HOST
152 else:
153 host = ONOS_GUI3_HOST
154
155 try:
156 command = "curl -s %s/wm/core/topology/links/json" % (host)
Paul Greyson8d1c6362013-03-27 13:05:24 -0700157 print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000158 result = os.popen(command).read()
159 except:
160 print "REST IF has issue"
161 exit
162
163 resp = Response(result, status=200, mimetype='application/json')
164 return resp
165
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000166@app.route("/wm/flow/getsummary/0/0/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000167def flows():
168 if request.args.get('proxy') == None:
169 host = ONOS_LOCAL_HOST
170 else:
171 host = ONOS_GUI3_HOST
172
173 try:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000174 command = "curl -s %s/wm/flow/getsummary/0/0/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700175# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000176 result = os.popen(command).read()
177 except:
178 print "REST IF has issue"
179 exit
180
181 resp = Response(result, status=200, mimetype='application/json')
182 return resp
183
184@app.route("/wm/registry/controllers/json")
185def registry_controllers():
186 if request.args.get('proxy') == None:
187 host = ONOS_LOCAL_HOST
188 else:
189 host = ONOS_GUI3_HOST
190
191 try:
192 command = "curl -s %s/wm/registry/controllers/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700193# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000194 result = os.popen(command).read()
195 except:
196 print "REST IF has issue"
197 exit
198
199 resp = Response(result, status=200, mimetype='application/json')
200 return resp
201
202@app.route("/wm/registry/switches/json")
203def registry_switches():
204 if request.args.get('proxy') == None:
205 host = ONOS_LOCAL_HOST
206 else:
207 host = ONOS_GUI3_HOST
208
209 try:
210 command = "curl -s %s/wm/registry/switches/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700211# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000212 result = os.popen(command).read()
213 except:
214 print "REST IF has issue"
215 exit
216
217 resp = Response(result, status=200, mimetype='application/json')
218 return resp
219
Ubuntu82b8a832013-02-06 22:00:11 +0000220
221def node_id(switch_array, dpid):
222 id = -1
223 for i, val in enumerate(switch_array):
224 if val['name'] == dpid:
225 id = i
226 break
227
228 return id
229
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000230## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000231@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000232def topology_for_gui():
233 try:
234 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
235 result = os.popen(command).read()
236 parsedResult = json.loads(result)
237 except:
238 log_error("REST IF has issue: %s" % command)
239 log_error("%s" % result)
240 sys.exit(0)
241
242 topo = {}
243 switches = []
244 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000245 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000246
247 for v in parsedResult:
248 if v.has_key('dpid'):
249# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
250 dpid = str(v['dpid'])
251 state = str(v['state'])
252 sw = {}
253 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000254 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000255
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000256 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000257 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000258 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000259
Ubuntu37ebda62013-03-01 00:35:31 +0000260## Comment in if we need devies
261# sw_index = len(switches) - 1
262# for p in v['ports']:
263# for d in p['devices']:
264# device = {}
265# device['attached_switch']=dpid
266# device['name']=d['mac']
267# if d['state'] == "ACTIVE":
268# device['group']=1000
269# else:
270# device['group']=1001
271#
272# switches.append(device)
273# device_index = len (switches) -1
274# link = {}
275# link['source'] = device_index
276# link['target'] = sw_index
277# link['type'] = -1
278# links.append(link)
279# link = {}
280# link['source'] = sw_index
281# link['target'] = device_index
282# link['type'] = -1
283# links.append(link)
284
Ubuntu5b2b24a2013-02-27 09:51:13 +0000285# try:
286# command = "curl -s \'http://%s:%s/wm/registry/controllers/json\'" % (RestIP, RestPort)
287# result = os.popen(command).read()
288# controllers = json.loads(result)
289# except:
290# log_error("xx REST IF has issue: %s" % command)
291# log_error("%s" % result)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000292
293 try:
294 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
295 result = os.popen(command).read()
296 parsedResult = json.loads(result)
297 except:
298 log_error("REST IF has issue: %s" % command)
299 log_error("%s" % result)
300
301 for key in parsedResult:
302 dpid = key
303 ctrl = parsedResult[dpid][0]['controllerId']
304 sw_id = node_id(switches, dpid)
305 if sw_id != -1:
306 if switches[sw_id]['group'] != 0:
307 switches[sw_id]['group'] = controllers.index(ctrl) + 1
308
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000309 try:
310 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000311# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000312 p1=1
313 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000314# v2 = "00:00:00:00:00:0d:00:d3"
315 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000316 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
317 result = os.popen(command).read()
318 parsedResult = json.loads(result)
319 except:
320 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000321 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000322
Ubuntu765deff2013-02-28 18:39:13 +0000323 path = []
324 if parsedResult.has_key('flowEntries'):
325 flowEntries= parsedResult['flowEntries']
326 for i, v in enumerate(flowEntries):
327 if i < len(flowEntries) - 1:
328 sdpid= flowEntries[i]['dpid']['value']
329 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700330 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000331
Ubuntu82b8a832013-02-06 22:00:11 +0000332 try:
333 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
334 result = os.popen(command).read()
335 parsedResult = json.loads(result)
336 except:
337 log_error("REST IF has issue: %s" % command)
338 log_error("%s" % result)
339 sys.exit(0)
340
341 for v in parsedResult:
342 link = {}
343 if v.has_key('dst-switch'):
344 dst_dpid = str(v['dst-switch'])
345 dst_id = node_id(switches, dst_dpid)
346 if v.has_key('src-switch'):
347 src_dpid = str(v['src-switch'])
348 src_id = node_id(switches, src_dpid)
349 link['source'] = src_id
350 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000351
352 onpath = 0
353 for (s,d) in path:
354 if s == v['src-switch'] and d == v['dst-switch']:
355 onpath = 1
356 break
357 link['type'] = onpath
358
Ubuntu82b8a832013-02-06 22:00:11 +0000359 links.append(link)
360
361 topo['nodes'] = switches
362 topo['links'] = links
363
Ubuntu37ebda62013-03-01 00:35:31 +0000364 pp.pprint(topo)
Ubuntu82b8a832013-02-06 22:00:11 +0000365 js = json.dumps(topo)
366 resp = Response(js, status=200, mimetype='application/json')
367 return resp
368
Ubuntuaea2a682013-02-08 08:30:10 +0000369#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
370#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
371@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
372def shortest_path(v1, p1, v2, p2):
373 try:
374 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
375 result = os.popen(command).read()
376 parsedResult = json.loads(result)
377 except:
378 log_error("REST IF has issue: %s" % command)
379 log_error("%s" % result)
380 sys.exit(0)
381
382 topo = {}
383 switches = []
384 links = []
385
386 for v in parsedResult:
387 if v.has_key('dpid'):
388 dpid = str(v['dpid'])
389 state = str(v['state'])
390 sw = {}
391 sw['name']=dpid
392 if str(v['state']) == "ACTIVE":
393 if dpid[-2:-1] == "a":
394 sw['group']=1
395 if dpid[-2:-1] == "b":
396 sw['group']=2
397 if dpid[-2:-1] == "c":
398 sw['group']=3
399 if str(v['state']) == "INACTIVE":
400 sw['group']=0
401
402 switches.append(sw)
403
404 try:
405 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
406 result = os.popen(command).read()
407 parsedResult = json.loads(result)
408 except:
409 log_error("No route")
410 parsedResult = []
411# exit(1)
412
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700413 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000414 for i, v in enumerate(parsedResult):
415 if i < len(parsedResult) - 1:
416 sdpid= parsedResult[i]['switch']
417 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700418 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000419
420 try:
421 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
422 result = os.popen(command).read()
423 parsedResult = json.loads(result)
424 except:
425 log_error("REST IF has issue: %s" % command)
426 log_error("%s" % result)
427 sys.exit(0)
428
429 for v in parsedResult:
430 link = {}
431 if v.has_key('dst-switch'):
432 dst_dpid = str(v['dst-switch'])
433 dst_id = node_id(switches, dst_dpid)
434 if v.has_key('src-switch'):
435 src_dpid = str(v['src-switch'])
436 src_id = node_id(switches, src_dpid)
437 link['source'] = src_id
438 link['target'] = dst_id
439 onpath = 0
440 for (s,d) in path:
441 if s == v['src-switch'] and d == v['dst-switch']:
442 onpath = 1
443 break
444
445 link['type'] = onpath
446 links.append(link)
447
448 topo['nodes'] = switches
449 topo['links'] = links
450
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000451# pp.pprint(topo)
Ubuntuaea2a682013-02-08 08:30:10 +0000452 js = json.dumps(topo)
453 resp = Response(js, status=200, mimetype='application/json')
454 return resp
455
Ubuntu82b8a832013-02-06 22:00:11 +0000456@app.route("/wm/core/controller/switches/json")
457def query_switch():
458 try:
459 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
460# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000461 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000462 result = os.popen(command).read()
463 parsedResult = json.loads(result)
464 except:
465 log_error("REST IF has issue: %s" % command)
466 log_error("%s" % result)
467 sys.exit(0)
468
469# print command
470# print result
471 switches_ = []
472 for v in parsedResult:
473 if v.has_key('dpid'):
474 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
475 dpid = str(v['dpid'])
476 state = str(v['state'])
477 sw = {}
478 sw['dpid']=dpid
479 sw['active']=state
480 switches_.append(sw)
481
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000482# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000483 js = json.dumps(switches_)
484 resp = Response(js, status=200, mimetype='application/json')
485 return resp
486
487@app.route("/wm/device/")
488def devices():
489 try:
490 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
491 result = os.popen(command).read()
492 parsedResult = json.loads(result)['results']
493 except:
494 log_error("REST IF has issue: %s" % command)
495 log_error("%s" % result)
496 sys.exit(0)
497
498 devices = []
499 for v in parsedResult:
500 dl_addr = v['dl_addr']
501 nw_addr = v['nw_addr']
502 vertex = v['_id']
503 mac = []
504 mac.append(dl_addr)
505 ip = []
506 ip.append(nw_addr)
507 device = {}
508 device['entryClass']="DefaultEntryClass"
509 device['mac']=mac
510 device['ipv4']=ip
511 device['vlan']=[]
512 device['lastSeen']=0
513 attachpoints =[]
514
515 port, dpid = deviceV_to_attachpoint(vertex)
516 attachpoint = {}
517 attachpoint['port']=port
518 attachpoint['switchDPID']=dpid
519 attachpoints.append(attachpoint)
520 device['attachmentPoint']=attachpoints
521 devices.append(device)
522
523 print devices
524 js = json.dumps(devices)
525 resp = Response(js, status=200, mimetype='application/json')
526 return resp
527
528#{"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}
529
Ubuntu82b8a832013-02-06 22:00:11 +0000530## return fake stat for now
531@app.route("/wm/core/switch/<switchId>/<statType>/json")
532def switch_stat(switchId, statType):
533 if statType == "desc":
534 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
535 ret = {}
536 ret[switchId]=desc
537 elif statType == "aggregate":
538 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
539 ret = {}
540 ret[switchId]=aggr
541 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700542 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000543
544 js = json.dumps(ret)
545 resp = Response(js, status=200, mimetype='application/json')
546 return resp
547
548
549@app.route("/wm/topology/links/json")
550def query_links():
551 try:
552 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000553 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000554 result = os.popen(command).read()
555 parsedResult = json.loads(result)['results']
556 except:
557 log_error("REST IF has issue: %s" % command)
558 log_error("%s" % result)
559 sys.exit(0)
560
561 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000562# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000563 sport = []
564 links = []
565 for v in parsedResult:
566 srcport = v['_id']
567 try:
568 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
569 print command
570 result = os.popen(command).read()
571 linkResults = json.loads(result)['results']
572 except:
573 log_error("REST IF has issue: %s" % command)
574 log_error("%s" % result)
575 sys.exit(0)
576
577 for p in linkResults:
578 if p.has_key('type') and p['type'] == "port":
579 dstport = p['_id']
580 (sport, sdpid) = portV_to_port_dpid(srcport)
581 (dport, ddpid) = portV_to_port_dpid(dstport)
582 link = {}
583 link["src-switch"]=sdpid
584 link["src-port"]=sport
585 link["src-port-state"]=0
586 link["dst-switch"]=ddpid
587 link["dst-port"]=dport
588 link["dst-port-state"]=0
589 link["type"]="internal"
590 links.append(link)
591
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000592# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000593 js = json.dumps(links)
594 resp = Response(js, status=200, mimetype='application/json')
595 return resp
596
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700597topo_less = {
598 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000599 {"name" : "00:a0", "group" : 1},
600 {"name" : "00:a1", "group" : 1},
601 {"name" : "00:a2", "group" : 1},
602 ],
603 "links" : [
604 {"source" :0, "target": 1},
605 {"source" :1, "target": 0},
606 {"source" :0, "target": 2},
607 {"source" :2, "target": 0},
608 {"source" :1, "target": 2},
609 {"source" :2, "target": 1},
610 ]
611}
612
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700613topo_more = {
614 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000615 {"name" : "00:a3", "group" : 2},
616 {"name" : "00:a0", "group" : 1},
617 {"name" : "00:a1", "group" : 1},
618 {"name" : "00:a2", "group" : 1},
619 ],
620 "links" : [
621 {"source" :1, "target": 2},
622 {"source" :2, "target": 1},
623 {"source" :1, "target": 3},
624 {"source" :3, "target": 1},
625 {"source" :2, "target": 3},
626 {"source" :3, "target": 2},
627 {"source" :0, "target": 2},
628 ]
629}
630
631@app.route("/topology_more")
632def topology_more():
633 topo = topo_more
634 js = json.dumps(topo)
635 resp = Response(js, status=200, mimetype='application/json')
636 return resp
637
638@app.route("/topology_less")
639def topology_less():
640 topo = topo_less
641 js = json.dumps(topo)
642 resp = Response(js, status=200, mimetype='application/json')
643 return resp
644
645cont_status1 = [
646 {"name":"onos9vpc", "onos": 1, "cassandra": 1},
647 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
648 {"name":"onos11vpc", "onos": 1, "cassandra": 0},
649 {"name":"onos12vpc", "onos": 1, "cassandra": 0}]
650
651cont_status2 = [
652 {"name":"onos9vpc", "onos": 0, "cassandra": 1},
653 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
654 {"name":"onos11vpc", "onos": 0, "cassandra": 1},
655 {"name":"onos12vpc", "onos": 0, "cassandra": 1}]
656
657@app.route("/controller_status1")
658def controller_status1():
659 status = cont_status1
660 js = json.dumps(status)
661 resp = Response(js, status=200, mimetype='application/json')
662 pp.pprint(resp)
663 return resp
664
665@app.route("/controller_status2")
666def controller_status2():
667 status = cont_status2
668 js = json.dumps(status)
669 resp = Response(js, status=200, mimetype='application/json')
670 pp.pprint(resp)
671 return resp
672
Ubuntuc016ba12013-02-27 21:53:41 +0000673@app.route("/controller_status")
674def controller_status():
675 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
676 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
677
678 cont_status=[]
679 for i in controllers:
680 status={}
681 onos=os.popen(onos_check % i).read()[:-1]
682 status["name"]=i
683 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000684 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000685 cont_status.append(status)
686
687 js = json.dumps(cont_status)
688 resp = Response(js, status=200, mimetype='application/json')
689 pp.pprint(js)
690 return resp
691
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000692@app.route("/gui/controller/<cmd>/<controller_name>")
693def controller_status_change(cmd, controller_name):
694 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
695 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
696
697 if cmd == "up":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000698 print start_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000699 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000700 ret = "controller %s is up" % (controller_name)
701 elif cmd == "down":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000702 print stop_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000703 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000704 ret = "controller %s is down" % (controller_name)
705
706 return ret
707
708@app.route("/gui/switch/<cmd>/<dpid>")
709def switch_status_change(cmd, dpid):
710 r = re.compile(':')
711 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000712 host=controllers[0]
713 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
714 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000715 print "cmd_string"
716
717 if cmd =="up" or cmd=="down":
718 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000719 os.popen(cmd_string)
720 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000721
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000722 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000723
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000724#* Link Up/Down
725#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
726#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000727
728@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
729def link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
730 if src_dpid in core_switches:
731 host = controllers[0]
732 else:
733 hostid=int(src_dpid.split(':')[-2])
734 host = controllers[hostid-1]
735
736 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
737 print cmd_string
738
739 if cmd =="up" or cmd=="down":
740 result=os.popen(cmd_string).read()
741
742 return result
743
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000744#* Create Flow
745#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000746#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
747@app.route("/gui/addfow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
748def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
749 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
750 print command
751 ret = os.popen(command).read()
752 if ret == "":
753 flow_nr=0
754 else:
755 flow_nr=int(ret)
756
757 flow_nr += 1
758 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)
759 print command
760 errcode = os.popen(command).read()
761 return errcode
762
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000763#* Delete Flow
764#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000765@app.route("/gui/delflow/<flow_id>")
766def del_flow(flow_id):
767 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
768 print command
769 errcode = os.popen(command).read()
770 return errcode
771
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000772#* Start Iperf Througput
773#http://localhost:9000/gui/iperf/start/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000774@app.route("/gui/iperf/start/<flow_id>")
775def iperf_start(flow_id):
776 command = "iperf -xCMSV -t30 -i1 -u -c 127.0.0.1 > iperf_%s.out &" % (flow_id)
777 print command
778 errcode = os.popen(command).read()
779 return errcode
780
781
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000782#* Get Iperf Throughput
783#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000784@app.route("/gui/iperf/rate/<flow_id>")
785def iperf_rate(flow_id):
786 try:
787 command = "curl -s http://%s:%s/iperf_%s.out" % (RestIP, 9000, flow_id)
788 print command
789 result = os.popen(command).read()
790 except:
791 print "REST IF has issue"
792 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000793
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000794 resp = Response(result, status=200, mimetype='text/html')
795 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000796
797
Ubuntu82b8a832013-02-06 22:00:11 +0000798if __name__ == "__main__":
799 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000800# 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")
801# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
802# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
803# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
804# link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1)
805
806
Ubuntu82b8a832013-02-06 22:00:11 +0000807 print "-- query all switches --"
808 query_switch()
809 print "-- query topo --"
810 topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000811# link_change(1,2,3,4)
812 print "-- query all links --"
813 query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000814# print "-- query all devices --"
815# devices()
816 else:
817 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000818 app.run(threaded=True, host="0.0.0.0", port=9000)