blob: bd3af98414887abd1628435db2a23831f672b2ea [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
Paul Greyson472da4c2013-03-28 11:43:17 -0700130@app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
131def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
132 try:
133 command = "curl -s %s/gui/addflow/%s/%s/%s/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC)
134 print command
135 result = os.popen(command).read()
136 except:
137 print "REST IF has issue"
138 exit
139
140 resp = Response(result, status=200, mimetype='application/json')
141 return resp
142
Paul Greyson6f918402013-03-28 12:18:30 -0700143@app.route("/proxy/gui/delflow/<flow_id>")
144def proxy_del_flow(flow_id):
145 try:
146 command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
147 print command
148 result = os.popen(command).read()
149 except:
150 print "REST IF has issue"
151 exit
152
153 resp = Response(result, status=200, mimetype='application/json')
154 return resp
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000155@app.route("/wm/core/topology/switches/all/json")
156def switches():
157 if request.args.get('proxy') == None:
158 host = ONOS_LOCAL_HOST
159 else:
160 host = ONOS_GUI3_HOST
161
162 try:
163 command = "curl -s %s/wm/core/topology/switches/all/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700164# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000165 result = os.popen(command).read()
166 except:
167 print "REST IF has issue"
168 exit
169
170 resp = Response(result, status=200, mimetype='application/json')
171 return resp
172
173@app.route("/wm/core/topology/links/json")
174def links():
175 if request.args.get('proxy') == None:
176 host = ONOS_LOCAL_HOST
177 else:
178 host = ONOS_GUI3_HOST
179
180 try:
181 command = "curl -s %s/wm/core/topology/links/json" % (host)
Paul Greyson8d1c6362013-03-27 13:05:24 -0700182 print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000183 result = os.popen(command).read()
184 except:
185 print "REST IF has issue"
186 exit
187
188 resp = Response(result, status=200, mimetype='application/json')
189 return resp
190
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000191@app.route("/wm/flow/getsummary/0/0/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000192def flows():
193 if request.args.get('proxy') == None:
194 host = ONOS_LOCAL_HOST
195 else:
196 host = ONOS_GUI3_HOST
197
198 try:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000199 command = "curl -s %s/wm/flow/getsummary/0/0/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700200# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000201 result = os.popen(command).read()
202 except:
203 print "REST IF has issue"
204 exit
205
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000206
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000207 resp = Response(result, status=200, mimetype='application/json')
208 return resp
209
210@app.route("/wm/registry/controllers/json")
211def registry_controllers():
212 if request.args.get('proxy') == None:
213 host = ONOS_LOCAL_HOST
214 else:
215 host = ONOS_GUI3_HOST
216
217 try:
218 command = "curl -s %s/wm/registry/controllers/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700219# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000220 result = os.popen(command).read()
221 except:
222 print "REST IF has issue"
223 exit
224
225 resp = Response(result, status=200, mimetype='application/json')
226 return resp
227
228@app.route("/wm/registry/switches/json")
229def registry_switches():
230 if request.args.get('proxy') == None:
231 host = ONOS_LOCAL_HOST
232 else:
233 host = ONOS_GUI3_HOST
234
235 try:
236 command = "curl -s %s/wm/registry/switches/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700237# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000238 result = os.popen(command).read()
239 except:
240 print "REST IF has issue"
241 exit
242
243 resp = Response(result, status=200, mimetype='application/json')
244 return resp
245
Ubuntu82b8a832013-02-06 22:00:11 +0000246
247def node_id(switch_array, dpid):
248 id = -1
249 for i, val in enumerate(switch_array):
250 if val['name'] == dpid:
251 id = i
252 break
253
254 return id
255
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000256## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000257@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000258def topology_for_gui():
259 try:
260 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
261 result = os.popen(command).read()
262 parsedResult = json.loads(result)
263 except:
264 log_error("REST IF has issue: %s" % command)
265 log_error("%s" % result)
266 sys.exit(0)
267
268 topo = {}
269 switches = []
270 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000271 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000272
273 for v in parsedResult:
274 if v.has_key('dpid'):
275# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
276 dpid = str(v['dpid'])
277 state = str(v['state'])
278 sw = {}
279 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000280 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000281
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000282 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000283 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000284 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000285
Ubuntu37ebda62013-03-01 00:35:31 +0000286## Comment in if we need devies
287# sw_index = len(switches) - 1
288# for p in v['ports']:
289# for d in p['devices']:
290# device = {}
291# device['attached_switch']=dpid
292# device['name']=d['mac']
293# if d['state'] == "ACTIVE":
294# device['group']=1000
295# else:
296# device['group']=1001
297#
298# switches.append(device)
299# device_index = len (switches) -1
300# link = {}
301# link['source'] = device_index
302# link['target'] = sw_index
303# link['type'] = -1
304# links.append(link)
305# link = {}
306# link['source'] = sw_index
307# link['target'] = device_index
308# link['type'] = -1
309# links.append(link)
310
Ubuntu5b2b24a2013-02-27 09:51:13 +0000311# try:
312# command = "curl -s \'http://%s:%s/wm/registry/controllers/json\'" % (RestIP, RestPort)
313# result = os.popen(command).read()
314# controllers = json.loads(result)
315# except:
316# log_error("xx REST IF has issue: %s" % command)
317# log_error("%s" % result)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000318
319 try:
320 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
321 result = os.popen(command).read()
322 parsedResult = json.loads(result)
323 except:
324 log_error("REST IF has issue: %s" % command)
325 log_error("%s" % result)
326
327 for key in parsedResult:
328 dpid = key
329 ctrl = parsedResult[dpid][0]['controllerId']
330 sw_id = node_id(switches, dpid)
331 if sw_id != -1:
332 if switches[sw_id]['group'] != 0:
333 switches[sw_id]['group'] = controllers.index(ctrl) + 1
334
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000335 try:
336 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000337# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000338 p1=1
339 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000340# v2 = "00:00:00:00:00:0d:00:d3"
341 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000342 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
343 result = os.popen(command).read()
344 parsedResult = json.loads(result)
345 except:
346 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000347 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000348
Ubuntu765deff2013-02-28 18:39:13 +0000349 path = []
350 if parsedResult.has_key('flowEntries'):
351 flowEntries= parsedResult['flowEntries']
352 for i, v in enumerate(flowEntries):
353 if i < len(flowEntries) - 1:
354 sdpid= flowEntries[i]['dpid']['value']
355 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700356 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000357
Ubuntu82b8a832013-02-06 22:00:11 +0000358 try:
359 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
360 result = os.popen(command).read()
361 parsedResult = json.loads(result)
362 except:
363 log_error("REST IF has issue: %s" % command)
364 log_error("%s" % result)
365 sys.exit(0)
366
367 for v in parsedResult:
368 link = {}
369 if v.has_key('dst-switch'):
370 dst_dpid = str(v['dst-switch'])
371 dst_id = node_id(switches, dst_dpid)
372 if v.has_key('src-switch'):
373 src_dpid = str(v['src-switch'])
374 src_id = node_id(switches, src_dpid)
375 link['source'] = src_id
376 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000377
378 onpath = 0
379 for (s,d) in path:
380 if s == v['src-switch'] and d == v['dst-switch']:
381 onpath = 1
382 break
383 link['type'] = onpath
384
Ubuntu82b8a832013-02-06 22:00:11 +0000385 links.append(link)
386
387 topo['nodes'] = switches
388 topo['links'] = links
389
Ubuntu37ebda62013-03-01 00:35:31 +0000390 pp.pprint(topo)
Ubuntu82b8a832013-02-06 22:00:11 +0000391 js = json.dumps(topo)
392 resp = Response(js, status=200, mimetype='application/json')
393 return resp
394
Ubuntuaea2a682013-02-08 08:30:10 +0000395#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
396#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
397@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
398def shortest_path(v1, p1, v2, p2):
399 try:
400 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
401 result = os.popen(command).read()
402 parsedResult = json.loads(result)
403 except:
404 log_error("REST IF has issue: %s" % command)
405 log_error("%s" % result)
406 sys.exit(0)
407
408 topo = {}
409 switches = []
410 links = []
411
412 for v in parsedResult:
413 if v.has_key('dpid'):
414 dpid = str(v['dpid'])
415 state = str(v['state'])
416 sw = {}
417 sw['name']=dpid
418 if str(v['state']) == "ACTIVE":
419 if dpid[-2:-1] == "a":
420 sw['group']=1
421 if dpid[-2:-1] == "b":
422 sw['group']=2
423 if dpid[-2:-1] == "c":
424 sw['group']=3
425 if str(v['state']) == "INACTIVE":
426 sw['group']=0
427
428 switches.append(sw)
429
430 try:
431 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
432 result = os.popen(command).read()
433 parsedResult = json.loads(result)
434 except:
435 log_error("No route")
436 parsedResult = []
437# exit(1)
438
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700439 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000440 for i, v in enumerate(parsedResult):
441 if i < len(parsedResult) - 1:
442 sdpid= parsedResult[i]['switch']
443 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700444 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000445
446 try:
447 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
448 result = os.popen(command).read()
449 parsedResult = json.loads(result)
450 except:
451 log_error("REST IF has issue: %s" % command)
452 log_error("%s" % result)
453 sys.exit(0)
454
455 for v in parsedResult:
456 link = {}
457 if v.has_key('dst-switch'):
458 dst_dpid = str(v['dst-switch'])
459 dst_id = node_id(switches, dst_dpid)
460 if v.has_key('src-switch'):
461 src_dpid = str(v['src-switch'])
462 src_id = node_id(switches, src_dpid)
463 link['source'] = src_id
464 link['target'] = dst_id
465 onpath = 0
466 for (s,d) in path:
467 if s == v['src-switch'] and d == v['dst-switch']:
468 onpath = 1
469 break
470
471 link['type'] = onpath
472 links.append(link)
473
474 topo['nodes'] = switches
475 topo['links'] = links
476
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000477# pp.pprint(topo)
Ubuntuaea2a682013-02-08 08:30:10 +0000478 js = json.dumps(topo)
479 resp = Response(js, status=200, mimetype='application/json')
480 return resp
481
Ubuntu82b8a832013-02-06 22:00:11 +0000482@app.route("/wm/core/controller/switches/json")
483def query_switch():
484 try:
485 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
486# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000487 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000488 result = os.popen(command).read()
489 parsedResult = json.loads(result)
490 except:
491 log_error("REST IF has issue: %s" % command)
492 log_error("%s" % result)
493 sys.exit(0)
494
495# print command
496# print result
497 switches_ = []
498 for v in parsedResult:
499 if v.has_key('dpid'):
500 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
501 dpid = str(v['dpid'])
502 state = str(v['state'])
503 sw = {}
504 sw['dpid']=dpid
505 sw['active']=state
506 switches_.append(sw)
507
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000508# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000509 js = json.dumps(switches_)
510 resp = Response(js, status=200, mimetype='application/json')
511 return resp
512
513@app.route("/wm/device/")
514def devices():
515 try:
516 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
517 result = os.popen(command).read()
518 parsedResult = json.loads(result)['results']
519 except:
520 log_error("REST IF has issue: %s" % command)
521 log_error("%s" % result)
522 sys.exit(0)
523
524 devices = []
525 for v in parsedResult:
526 dl_addr = v['dl_addr']
527 nw_addr = v['nw_addr']
528 vertex = v['_id']
529 mac = []
530 mac.append(dl_addr)
531 ip = []
532 ip.append(nw_addr)
533 device = {}
534 device['entryClass']="DefaultEntryClass"
535 device['mac']=mac
536 device['ipv4']=ip
537 device['vlan']=[]
538 device['lastSeen']=0
539 attachpoints =[]
540
541 port, dpid = deviceV_to_attachpoint(vertex)
542 attachpoint = {}
543 attachpoint['port']=port
544 attachpoint['switchDPID']=dpid
545 attachpoints.append(attachpoint)
546 device['attachmentPoint']=attachpoints
547 devices.append(device)
548
549 print devices
550 js = json.dumps(devices)
551 resp = Response(js, status=200, mimetype='application/json')
552 return resp
553
554#{"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}
555
Ubuntu82b8a832013-02-06 22:00:11 +0000556## return fake stat for now
557@app.route("/wm/core/switch/<switchId>/<statType>/json")
558def switch_stat(switchId, statType):
559 if statType == "desc":
560 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
561 ret = {}
562 ret[switchId]=desc
563 elif statType == "aggregate":
564 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
565 ret = {}
566 ret[switchId]=aggr
567 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700568 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000569
570 js = json.dumps(ret)
571 resp = Response(js, status=200, mimetype='application/json')
572 return resp
573
574
575@app.route("/wm/topology/links/json")
576def query_links():
577 try:
578 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000579 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000580 result = os.popen(command).read()
581 parsedResult = json.loads(result)['results']
582 except:
583 log_error("REST IF has issue: %s" % command)
584 log_error("%s" % result)
585 sys.exit(0)
586
587 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000588# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000589 sport = []
590 links = []
591 for v in parsedResult:
592 srcport = v['_id']
593 try:
594 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
595 print command
596 result = os.popen(command).read()
597 linkResults = json.loads(result)['results']
598 except:
599 log_error("REST IF has issue: %s" % command)
600 log_error("%s" % result)
601 sys.exit(0)
602
603 for p in linkResults:
604 if p.has_key('type') and p['type'] == "port":
605 dstport = p['_id']
606 (sport, sdpid) = portV_to_port_dpid(srcport)
607 (dport, ddpid) = portV_to_port_dpid(dstport)
608 link = {}
609 link["src-switch"]=sdpid
610 link["src-port"]=sport
611 link["src-port-state"]=0
612 link["dst-switch"]=ddpid
613 link["dst-port"]=dport
614 link["dst-port-state"]=0
615 link["type"]="internal"
616 links.append(link)
617
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000618# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000619 js = json.dumps(links)
620 resp = Response(js, status=200, mimetype='application/json')
621 return resp
622
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700623topo_less = {
624 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000625 {"name" : "00:a0", "group" : 1},
626 {"name" : "00:a1", "group" : 1},
627 {"name" : "00:a2", "group" : 1},
628 ],
629 "links" : [
630 {"source" :0, "target": 1},
631 {"source" :1, "target": 0},
632 {"source" :0, "target": 2},
633 {"source" :2, "target": 0},
634 {"source" :1, "target": 2},
635 {"source" :2, "target": 1},
636 ]
637}
638
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700639topo_more = {
640 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000641 {"name" : "00:a3", "group" : 2},
642 {"name" : "00:a0", "group" : 1},
643 {"name" : "00:a1", "group" : 1},
644 {"name" : "00:a2", "group" : 1},
645 ],
646 "links" : [
647 {"source" :1, "target": 2},
648 {"source" :2, "target": 1},
649 {"source" :1, "target": 3},
650 {"source" :3, "target": 1},
651 {"source" :2, "target": 3},
652 {"source" :3, "target": 2},
653 {"source" :0, "target": 2},
654 ]
655}
656
657@app.route("/topology_more")
658def topology_more():
659 topo = topo_more
660 js = json.dumps(topo)
661 resp = Response(js, status=200, mimetype='application/json')
662 return resp
663
664@app.route("/topology_less")
665def topology_less():
666 topo = topo_less
667 js = json.dumps(topo)
668 resp = Response(js, status=200, mimetype='application/json')
669 return resp
670
671cont_status1 = [
672 {"name":"onos9vpc", "onos": 1, "cassandra": 1},
673 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
674 {"name":"onos11vpc", "onos": 1, "cassandra": 0},
675 {"name":"onos12vpc", "onos": 1, "cassandra": 0}]
676
677cont_status2 = [
678 {"name":"onos9vpc", "onos": 0, "cassandra": 1},
679 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
680 {"name":"onos11vpc", "onos": 0, "cassandra": 1},
681 {"name":"onos12vpc", "onos": 0, "cassandra": 1}]
682
683@app.route("/controller_status1")
684def controller_status1():
685 status = cont_status1
686 js = json.dumps(status)
687 resp = Response(js, status=200, mimetype='application/json')
688 pp.pprint(resp)
689 return resp
690
691@app.route("/controller_status2")
692def controller_status2():
693 status = cont_status2
694 js = json.dumps(status)
695 resp = Response(js, status=200, mimetype='application/json')
696 pp.pprint(resp)
697 return resp
698
Ubuntuc016ba12013-02-27 21:53:41 +0000699@app.route("/controller_status")
700def controller_status():
701 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
702 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
703
704 cont_status=[]
705 for i in controllers:
706 status={}
707 onos=os.popen(onos_check % i).read()[:-1]
708 status["name"]=i
709 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000710 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000711 cont_status.append(status)
712
713 js = json.dumps(cont_status)
714 resp = Response(js, status=200, mimetype='application/json')
715 pp.pprint(js)
716 return resp
717
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000718@app.route("/gui/controller/<cmd>/<controller_name>")
719def controller_status_change(cmd, controller_name):
720 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
721 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
722
723 if cmd == "up":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000724 print start_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000725 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000726 ret = "controller %s is up" % (controller_name)
727 elif cmd == "down":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000728 print stop_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000729 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000730 ret = "controller %s is down" % (controller_name)
731
732 return ret
733
734@app.route("/gui/switch/<cmd>/<dpid>")
735def switch_status_change(cmd, dpid):
736 r = re.compile(':')
737 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000738 host=controllers[0]
739 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
740 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000741 print "cmd_string"
742
743 if cmd =="up" or cmd=="down":
744 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000745 os.popen(cmd_string)
746 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000747
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000748 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000749
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000750#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000751#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000752@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
753def link_up(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000754
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000755 cmd = 'up'
756 result=""
757
Paul Greyson472da4c2013-03-28 11:43:17 -0700758 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000759 if dpid in core_switches:
760 host = controllers[0]
761 src_ports = [1, 2, 3, 4, 5]
762 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000763 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000764 host = controllers[hostid-1]
765 if hostid == 2 :
766 src_ports = [51]
767 else :
768 src_ports = [26]
769
770 for port in src_ports :
771 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
772 print cmd_string
773 res=os.popen(cmd_string).read()
774 result = result + ' ' + res
775
776 return result
777
778
779#* Link Down
780#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000781@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000782def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
783
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000784 if src_dpid in core_switches:
785 host = controllers[0]
786 else:
787 hostid=int(src_dpid.split(':')[-2])
788 host = controllers[hostid-1]
789
790 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
791 print cmd_string
792
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000793 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000794
795 return result
796
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000797#* Create Flow
798#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000799#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
Masayoshi Kobayashi81f65652013-03-28 21:06:39 +0000800@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000801def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
802 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
803 print command
804 ret = os.popen(command).read()
805 if ret == "":
806 flow_nr=0
807 else:
808 flow_nr=int(ret)
809
810 flow_nr += 1
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000811 command = "/home/ubuntu/ONOS/web/add_flow.py -m %d %s %s %s %s %s matchSrcMac %s matchDstMac %s > /dev/null 2>&1 & " % (flow_nr, "dummy", src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000812 print command
813 errcode = os.popen(command).read()
814 return errcode
815
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000816#* Delete Flow
817#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000818@app.route("/gui/delflow/<flow_id>")
819def del_flow(flow_id):
820 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
821 print command
822 errcode = os.popen(command).read()
823 return errcode
824
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000825#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700826#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
827@app.route("/gui/iperf/start/<flow_id>/<duration>")
828def iperf_start(flow_id,duration):
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000829 command = "iperf -t%d -i0.1 -yJ -o iperf_%s.out -c 127.0.0.1 &" % (duration, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000830 print command
831 errcode = os.popen(command).read()
832 return errcode
833
834
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000835#* Get Iperf Throughput
836#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000837@app.route("/gui/iperf/rate/<flow_id>")
838def iperf_rate(flow_id):
839 try:
840 command = "curl -s http://%s:%s/iperf_%s.out" % (RestIP, 9000, flow_id)
841 print command
842 result = os.popen(command).read()
843 except:
844 print "REST IF has issue"
845 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000846
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000847 resp = Response(result, status=200, mimetype='text/html')
848 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000849
850
Ubuntu82b8a832013-02-06 22:00:11 +0000851if __name__ == "__main__":
852 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000853# 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")
854# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
855# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
856# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
857# link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1)
858
859
Ubuntu82b8a832013-02-06 22:00:11 +0000860 print "-- query all switches --"
861 query_switch()
862 print "-- query topo --"
863 topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000864# link_change(1,2,3,4)
865 print "-- query all links --"
866 query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000867# print "-- query all devices --"
868# devices()
869 else:
870 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000871 app.run(threaded=True, host="0.0.0.0", port=9000)