blob: 4ed3ae50ee0679f6215015990831a094c3c5b15d [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'])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +000060@app.route('/log/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000061@app.route('/', methods=['GET'])
62@app.route('/<filename>', methods=['GET'])
63@app.route('/tpl/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000064@app.route('/ons-demo/<filename>', methods=['GET'])
65@app.route('/ons-demo/js/<filename>', methods=['GET'])
66@app.route('/ons-demo/css/<filename>', methods=['GET'])
67@app.route('/ons-demo/assets/<filename>', methods=['GET'])
68@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000069def return_file(filename="index.html"):
70 if request.path == "/":
71 fullpath = "./index.html"
72 else:
73 fullpath = str(request.path)[1:]
74
75 response = make_response(open(fullpath).read())
76 suffix = fullpath.split(".")[-1]
77
78 if suffix == "html" or suffix == "htm":
79 response.headers["Content-type"] = "text/html"
80 elif suffix == "js":
81 response.headers["Content-type"] = "application/javascript"
82 elif suffix == "css":
83 response.headers["Content-type"] = "text/css"
84 elif suffix == "png":
85 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -070086 elif suffix == "svg":
87 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000088
89 return response
90
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000091
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070092@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
93def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
94 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -070095 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 -070096 print command
97 result = os.popen(command).read()
98 except:
99 print "REST IF has issue"
100 exit
101
102 resp = Response(result, status=200, mimetype='application/json')
103 return resp
104
Paul Greyson8d1c6362013-03-27 13:05:24 -0700105@app.route("/proxy/gui/switch/<cmd>/<dpid>")
106def proxy_switch_status_change(cmd, dpid):
107 try:
108 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
109 print command
110 result = os.popen(command).read()
111 except:
112 print "REST IF has issue"
113 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700114
Paul Greyson8d1c6362013-03-27 13:05:24 -0700115 resp = Response(result, status=200, mimetype='application/json')
116 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700117
Paul Greyson2913af82013-03-27 14:53:17 -0700118@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
119def proxy_controller_status_change(cmd, controller_name):
120 try:
121 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
122 print command
123 result = os.popen(command).read()
124 except:
125 print "REST IF has issue"
126 exit
127
128 resp = Response(result, status=200, mimetype='application/json')
129 return resp
130
Paul Greyson472da4c2013-03-28 11:43:17 -0700131@app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
132def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
133 try:
134 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)
135 print command
136 result = os.popen(command).read()
137 except:
138 print "REST IF has issue"
139 exit
140
141 resp = Response(result, status=200, mimetype='application/json')
142 return resp
143
Paul Greyson6f918402013-03-28 12:18:30 -0700144@app.route("/proxy/gui/delflow/<flow_id>")
145def proxy_del_flow(flow_id):
146 try:
147 command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
148 print command
149 result = os.popen(command).read()
150 except:
151 print "REST IF has issue"
152 exit
153
154 resp = Response(result, status=200, mimetype='application/json')
155 return resp
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700156
157@app.route("/proxy/gui/iperf/start/<flow_id>/<duration>/<samples>")
158def proxy_iperf_start(flow_id,duration,samples):
159 try:
160 command = "curl -s %s/gui/iperf/start/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id, duration, samples)
161 print command
162 result = os.popen(command).read()
163 except:
164 print "REST IF has issue"
165 exit
166
167 resp = Response(result, status=200, mimetype='application/json')
168 return resp
169
170@app.route("/proxy/gui/iperf/rate/<flow_id>")
171def proxy_iperf_rate(flow_id):
172 try:
173 command = "curl -s %s/gui/iperf/rate/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
174 print command
175 result = os.popen(command).read()
176 except:
177 print "REST IF has issue"
178 exit
179
180 resp = Response(result, status=200, mimetype='application/json')
181 return resp
182
183
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000184@app.route("/wm/core/topology/switches/all/json")
185def switches():
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/core/topology/switches/all/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/core/topology/links/json")
203def links():
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/core/topology/links/json" % (host)
Paul Greyson8d1c6362013-03-27 13:05:24 -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
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000220@app.route("/wm/flow/getsummary/0/0/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000221def flows():
222 if request.args.get('proxy') == None:
223 host = ONOS_LOCAL_HOST
224 else:
225 host = ONOS_GUI3_HOST
226
227 try:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000228 command = "curl -s %s/wm/flow/getsummary/0/0/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700229# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000230 result = os.popen(command).read()
231 except:
232 print "REST IF has issue"
233 exit
234
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700235
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000236 resp = Response(result, status=200, mimetype='application/json')
237 return resp
238
239@app.route("/wm/registry/controllers/json")
240def registry_controllers():
241 if request.args.get('proxy') == None:
242 host = ONOS_LOCAL_HOST
243 else:
244 host = ONOS_GUI3_HOST
245
246 try:
247 command = "curl -s %s/wm/registry/controllers/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700248# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000249 result = os.popen(command).read()
250 except:
251 print "REST IF has issue"
252 exit
253
254 resp = Response(result, status=200, mimetype='application/json')
255 return resp
256
257@app.route("/wm/registry/switches/json")
258def registry_switches():
259 if request.args.get('proxy') == None:
260 host = ONOS_LOCAL_HOST
261 else:
262 host = ONOS_GUI3_HOST
263
264 try:
265 command = "curl -s %s/wm/registry/switches/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700266# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000267 result = os.popen(command).read()
268 except:
269 print "REST IF has issue"
270 exit
271
272 resp = Response(result, status=200, mimetype='application/json')
273 return resp
274
Ubuntu82b8a832013-02-06 22:00:11 +0000275
276def node_id(switch_array, dpid):
277 id = -1
278 for i, val in enumerate(switch_array):
279 if val['name'] == dpid:
280 id = i
281 break
282
283 return id
284
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000285## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000286@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000287def topology_for_gui():
288 try:
289 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
290 result = os.popen(command).read()
291 parsedResult = json.loads(result)
292 except:
293 log_error("REST IF has issue: %s" % command)
294 log_error("%s" % result)
295 sys.exit(0)
296
297 topo = {}
298 switches = []
299 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000300 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000301
302 for v in parsedResult:
303 if v.has_key('dpid'):
304# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
305 dpid = str(v['dpid'])
306 state = str(v['state'])
307 sw = {}
308 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000309 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000310
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000311 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000312 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000313 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000314
Ubuntu37ebda62013-03-01 00:35:31 +0000315## Comment in if we need devies
316# sw_index = len(switches) - 1
317# for p in v['ports']:
318# for d in p['devices']:
319# device = {}
320# device['attached_switch']=dpid
321# device['name']=d['mac']
322# if d['state'] == "ACTIVE":
323# device['group']=1000
324# else:
325# device['group']=1001
326#
327# switches.append(device)
328# device_index = len (switches) -1
329# link = {}
330# link['source'] = device_index
331# link['target'] = sw_index
332# link['type'] = -1
333# links.append(link)
334# link = {}
335# link['source'] = sw_index
336# link['target'] = device_index
337# link['type'] = -1
338# links.append(link)
339
Ubuntu5b2b24a2013-02-27 09:51:13 +0000340# try:
341# command = "curl -s \'http://%s:%s/wm/registry/controllers/json\'" % (RestIP, RestPort)
342# result = os.popen(command).read()
343# controllers = json.loads(result)
344# except:
345# log_error("xx REST IF has issue: %s" % command)
346# log_error("%s" % result)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000347
348 try:
349 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
350 result = os.popen(command).read()
351 parsedResult = json.loads(result)
352 except:
353 log_error("REST IF has issue: %s" % command)
354 log_error("%s" % result)
355
356 for key in parsedResult:
357 dpid = key
358 ctrl = parsedResult[dpid][0]['controllerId']
359 sw_id = node_id(switches, dpid)
360 if sw_id != -1:
361 if switches[sw_id]['group'] != 0:
362 switches[sw_id]['group'] = controllers.index(ctrl) + 1
363
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000364 try:
365 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000366# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000367 p1=1
368 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000369# v2 = "00:00:00:00:00:0d:00:d3"
370 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000371 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
372 result = os.popen(command).read()
373 parsedResult = json.loads(result)
374 except:
375 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000376 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000377
Ubuntu765deff2013-02-28 18:39:13 +0000378 path = []
379 if parsedResult.has_key('flowEntries'):
380 flowEntries= parsedResult['flowEntries']
381 for i, v in enumerate(flowEntries):
382 if i < len(flowEntries) - 1:
383 sdpid= flowEntries[i]['dpid']['value']
384 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700385 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000386
Ubuntu82b8a832013-02-06 22:00:11 +0000387 try:
388 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
389 result = os.popen(command).read()
390 parsedResult = json.loads(result)
391 except:
392 log_error("REST IF has issue: %s" % command)
393 log_error("%s" % result)
394 sys.exit(0)
395
396 for v in parsedResult:
397 link = {}
398 if v.has_key('dst-switch'):
399 dst_dpid = str(v['dst-switch'])
400 dst_id = node_id(switches, dst_dpid)
401 if v.has_key('src-switch'):
402 src_dpid = str(v['src-switch'])
403 src_id = node_id(switches, src_dpid)
404 link['source'] = src_id
405 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000406
407 onpath = 0
408 for (s,d) in path:
409 if s == v['src-switch'] and d == v['dst-switch']:
410 onpath = 1
411 break
412 link['type'] = onpath
413
Ubuntu82b8a832013-02-06 22:00:11 +0000414 links.append(link)
415
416 topo['nodes'] = switches
417 topo['links'] = links
418
Ubuntu37ebda62013-03-01 00:35:31 +0000419 pp.pprint(topo)
Ubuntu82b8a832013-02-06 22:00:11 +0000420 js = json.dumps(topo)
421 resp = Response(js, status=200, mimetype='application/json')
422 return resp
423
Ubuntuaea2a682013-02-08 08:30:10 +0000424#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
425#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
426@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
427def shortest_path(v1, p1, v2, p2):
428 try:
429 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
430 result = os.popen(command).read()
431 parsedResult = json.loads(result)
432 except:
433 log_error("REST IF has issue: %s" % command)
434 log_error("%s" % result)
435 sys.exit(0)
436
437 topo = {}
438 switches = []
439 links = []
440
441 for v in parsedResult:
442 if v.has_key('dpid'):
443 dpid = str(v['dpid'])
444 state = str(v['state'])
445 sw = {}
446 sw['name']=dpid
447 if str(v['state']) == "ACTIVE":
448 if dpid[-2:-1] == "a":
449 sw['group']=1
450 if dpid[-2:-1] == "b":
451 sw['group']=2
452 if dpid[-2:-1] == "c":
453 sw['group']=3
454 if str(v['state']) == "INACTIVE":
455 sw['group']=0
456
457 switches.append(sw)
458
459 try:
460 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
461 result = os.popen(command).read()
462 parsedResult = json.loads(result)
463 except:
464 log_error("No route")
465 parsedResult = []
466# exit(1)
467
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700468 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000469 for i, v in enumerate(parsedResult):
470 if i < len(parsedResult) - 1:
471 sdpid= parsedResult[i]['switch']
472 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700473 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000474
475 try:
476 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
477 result = os.popen(command).read()
478 parsedResult = json.loads(result)
479 except:
480 log_error("REST IF has issue: %s" % command)
481 log_error("%s" % result)
482 sys.exit(0)
483
484 for v in parsedResult:
485 link = {}
486 if v.has_key('dst-switch'):
487 dst_dpid = str(v['dst-switch'])
488 dst_id = node_id(switches, dst_dpid)
489 if v.has_key('src-switch'):
490 src_dpid = str(v['src-switch'])
491 src_id = node_id(switches, src_dpid)
492 link['source'] = src_id
493 link['target'] = dst_id
494 onpath = 0
495 for (s,d) in path:
496 if s == v['src-switch'] and d == v['dst-switch']:
497 onpath = 1
498 break
499
500 link['type'] = onpath
501 links.append(link)
502
503 topo['nodes'] = switches
504 topo['links'] = links
505
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000506# pp.pprint(topo)
Ubuntuaea2a682013-02-08 08:30:10 +0000507 js = json.dumps(topo)
508 resp = Response(js, status=200, mimetype='application/json')
509 return resp
510
Ubuntu82b8a832013-02-06 22:00:11 +0000511@app.route("/wm/core/controller/switches/json")
512def query_switch():
513 try:
514 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
515# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000516 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000517 result = os.popen(command).read()
518 parsedResult = json.loads(result)
519 except:
520 log_error("REST IF has issue: %s" % command)
521 log_error("%s" % result)
522 sys.exit(0)
523
524# print command
525# print result
526 switches_ = []
527 for v in parsedResult:
528 if v.has_key('dpid'):
529 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
530 dpid = str(v['dpid'])
531 state = str(v['state'])
532 sw = {}
533 sw['dpid']=dpid
534 sw['active']=state
535 switches_.append(sw)
536
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000537# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000538 js = json.dumps(switches_)
539 resp = Response(js, status=200, mimetype='application/json')
540 return resp
541
542@app.route("/wm/device/")
543def devices():
544 try:
545 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
546 result = os.popen(command).read()
547 parsedResult = 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 devices = []
554 for v in parsedResult:
555 dl_addr = v['dl_addr']
556 nw_addr = v['nw_addr']
557 vertex = v['_id']
558 mac = []
559 mac.append(dl_addr)
560 ip = []
561 ip.append(nw_addr)
562 device = {}
563 device['entryClass']="DefaultEntryClass"
564 device['mac']=mac
565 device['ipv4']=ip
566 device['vlan']=[]
567 device['lastSeen']=0
568 attachpoints =[]
569
570 port, dpid = deviceV_to_attachpoint(vertex)
571 attachpoint = {}
572 attachpoint['port']=port
573 attachpoint['switchDPID']=dpid
574 attachpoints.append(attachpoint)
575 device['attachmentPoint']=attachpoints
576 devices.append(device)
577
578 print devices
579 js = json.dumps(devices)
580 resp = Response(js, status=200, mimetype='application/json')
581 return resp
582
583#{"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}
584
Ubuntu82b8a832013-02-06 22:00:11 +0000585## return fake stat for now
586@app.route("/wm/core/switch/<switchId>/<statType>/json")
587def switch_stat(switchId, statType):
588 if statType == "desc":
589 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
590 ret = {}
591 ret[switchId]=desc
592 elif statType == "aggregate":
593 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
594 ret = {}
595 ret[switchId]=aggr
596 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700597 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000598
599 js = json.dumps(ret)
600 resp = Response(js, status=200, mimetype='application/json')
601 return resp
602
603
604@app.route("/wm/topology/links/json")
605def query_links():
606 try:
607 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000608 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000609 result = os.popen(command).read()
610 parsedResult = json.loads(result)['results']
611 except:
612 log_error("REST IF has issue: %s" % command)
613 log_error("%s" % result)
614 sys.exit(0)
615
616 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000617# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000618 sport = []
619 links = []
620 for v in parsedResult:
621 srcport = v['_id']
622 try:
623 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
624 print command
625 result = os.popen(command).read()
626 linkResults = json.loads(result)['results']
627 except:
628 log_error("REST IF has issue: %s" % command)
629 log_error("%s" % result)
630 sys.exit(0)
631
632 for p in linkResults:
633 if p.has_key('type') and p['type'] == "port":
634 dstport = p['_id']
635 (sport, sdpid) = portV_to_port_dpid(srcport)
636 (dport, ddpid) = portV_to_port_dpid(dstport)
637 link = {}
638 link["src-switch"]=sdpid
639 link["src-port"]=sport
640 link["src-port-state"]=0
641 link["dst-switch"]=ddpid
642 link["dst-port"]=dport
643 link["dst-port-state"]=0
644 link["type"]="internal"
645 links.append(link)
646
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000647# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000648 js = json.dumps(links)
649 resp = Response(js, status=200, mimetype='application/json')
650 return resp
651
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700652topo_less = {
653 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000654 {"name" : "00:a0", "group" : 1},
655 {"name" : "00:a1", "group" : 1},
656 {"name" : "00:a2", "group" : 1},
657 ],
658 "links" : [
659 {"source" :0, "target": 1},
660 {"source" :1, "target": 0},
661 {"source" :0, "target": 2},
662 {"source" :2, "target": 0},
663 {"source" :1, "target": 2},
664 {"source" :2, "target": 1},
665 ]
666}
667
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700668topo_more = {
669 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000670 {"name" : "00:a3", "group" : 2},
671 {"name" : "00:a0", "group" : 1},
672 {"name" : "00:a1", "group" : 1},
673 {"name" : "00:a2", "group" : 1},
674 ],
675 "links" : [
676 {"source" :1, "target": 2},
677 {"source" :2, "target": 1},
678 {"source" :1, "target": 3},
679 {"source" :3, "target": 1},
680 {"source" :2, "target": 3},
681 {"source" :3, "target": 2},
682 {"source" :0, "target": 2},
683 ]
684}
685
686@app.route("/topology_more")
687def topology_more():
688 topo = topo_more
689 js = json.dumps(topo)
690 resp = Response(js, status=200, mimetype='application/json')
691 return resp
692
693@app.route("/topology_less")
694def topology_less():
695 topo = topo_less
696 js = json.dumps(topo)
697 resp = Response(js, status=200, mimetype='application/json')
698 return resp
699
700cont_status1 = [
701 {"name":"onos9vpc", "onos": 1, "cassandra": 1},
702 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
703 {"name":"onos11vpc", "onos": 1, "cassandra": 0},
704 {"name":"onos12vpc", "onos": 1, "cassandra": 0}]
705
706cont_status2 = [
707 {"name":"onos9vpc", "onos": 0, "cassandra": 1},
708 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
709 {"name":"onos11vpc", "onos": 0, "cassandra": 1},
710 {"name":"onos12vpc", "onos": 0, "cassandra": 1}]
711
712@app.route("/controller_status1")
713def controller_status1():
714 status = cont_status1
715 js = json.dumps(status)
716 resp = Response(js, status=200, mimetype='application/json')
717 pp.pprint(resp)
718 return resp
719
720@app.route("/controller_status2")
721def controller_status2():
722 status = cont_status2
723 js = json.dumps(status)
724 resp = Response(js, status=200, mimetype='application/json')
725 pp.pprint(resp)
726 return resp
727
Ubuntuc016ba12013-02-27 21:53:41 +0000728@app.route("/controller_status")
729def controller_status():
730 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
731 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
732
733 cont_status=[]
734 for i in controllers:
735 status={}
736 onos=os.popen(onos_check % i).read()[:-1]
737 status["name"]=i
738 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000739 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000740 cont_status.append(status)
741
742 js = json.dumps(cont_status)
743 resp = Response(js, status=200, mimetype='application/json')
744 pp.pprint(js)
745 return resp
746
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000747@app.route("/gui/controller/<cmd>/<controller_name>")
748def controller_status_change(cmd, controller_name):
749 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
750 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
751
752 if cmd == "up":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000753 print start_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000754 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000755 ret = "controller %s is up" % (controller_name)
756 elif cmd == "down":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000757 print stop_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000758 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000759 ret = "controller %s is down" % (controller_name)
760
761 return ret
762
763@app.route("/gui/switch/<cmd>/<dpid>")
764def switch_status_change(cmd, dpid):
765 r = re.compile(':')
766 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000767 host=controllers[0]
768 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
769 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000770 print "cmd_string"
771
772 if cmd =="up" or cmd=="down":
773 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000774 os.popen(cmd_string)
775 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000776
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000777 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000778
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000779#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000780#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000781@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
782def link_up(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000783
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000784 cmd = 'up'
785 result=""
786
Paul Greyson472da4c2013-03-28 11:43:17 -0700787 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000788 if dpid in core_switches:
789 host = controllers[0]
790 src_ports = [1, 2, 3, 4, 5]
791 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000792 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000793 host = controllers[hostid-1]
794 if hostid == 2 :
795 src_ports = [51]
796 else :
797 src_ports = [26]
798
799 for port in src_ports :
800 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
801 print cmd_string
802 res=os.popen(cmd_string).read()
803 result = result + ' ' + res
804
805 return result
806
807
808#* Link Down
809#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000810@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000811def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
812
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000813 if src_dpid in core_switches:
814 host = controllers[0]
815 else:
816 hostid=int(src_dpid.split(':')[-2])
817 host = controllers[hostid-1]
818
819 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
820 print cmd_string
821
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000822 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000823
824 return result
825
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000826#* Create Flow
827#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000828#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 +0000829@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000830def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
831 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
832 print command
833 ret = os.popen(command).read()
834 if ret == "":
835 flow_nr=0
836 else:
837 flow_nr=int(ret)
838
839 flow_nr += 1
Umesh Krishnaswamyf22d3ed2013-04-03 11:57:54 -0700840 command = "/home/ubuntu/ONOS/web/add_flow.py -m onos %d %s %s %s %s %s matchSrcMac %s matchDstMac %s" % (flow_nr, "dummy", src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000841 print command
842 errcode = os.popen(command).read()
843 return errcode
844
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000845#* Delete Flow
846#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000847@app.route("/gui/delflow/<flow_id>")
848def del_flow(flow_id):
849 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
850 print command
851 errcode = os.popen(command).read()
852 return errcode
853
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000854#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700855#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000856@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
857def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000858 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000859 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000860 print command
861 result = os.popen(command).read()
862 if len(result) == 0:
863 print "No Flow found"
864 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000865 except:
866 print "REST IF has issue"
867 exit
868
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000869 parsedResult = json.loads(result)
870
871 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000872 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
873 src_port = parsedResult['dataPath']['srcPort']['port']['value']
874 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
875 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000876# print "FlowPath: (flowId = %s src = %s/%s dst = %s/%s" % (flowId, src_dpid, src_port, dst_dpid, dst_port)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000877
878 if src_dpid in core_switches:
879 host = controllers[0]
880 else:
881 hostid=int(src_dpid.split(':')[-2])
882 host = controllers[hostid-1]
883
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000884# ./runiperf.sh 2 00:00:00:00:00:00:02:02 1 00:00:00:00:00:00:03:02 1 100 15
885 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./runiperf.sh %d %s %s %s %s %s %s'" % (host, flowId, src_dpid, src_port, dst_dpid, dst_port, duration, samples)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000886 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000887 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000888
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700889 return
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000890
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000891#* Get Iperf Throughput
892#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000893@app.route("/gui/iperf/rate/<flow_id>")
894def iperf_rate(flow_id):
895 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000896 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
897 print command
898 result = os.popen(command).read()
899 if len(result) == 0:
900 print "No Flow found"
901 return;
902 except:
903 print "REST IF has issue"
904 exit
905
906 parsedResult = json.loads(result)
907
908 flowId = int(parsedResult['flowId']['value'], 16)
909 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
910 src_port = parsedResult['dataPath']['srcPort']['port']['value']
911 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
912 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
913
914 if src_dpid in core_switches:
915 host = controllers[0]
916 else:
917 hostid=int(src_dpid.split(':')[-2])
918 host = controllers[hostid-1]
919
920 try:
921 command = "curl -s http://%s:%s/log/iperf_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000922 print command
923 result = os.popen(command).read()
924 except:
925 print "REST IF has issue"
926 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000927
Masayoshi Kobayashi911f2632013-04-01 18:44:33 +0000928 resp = Response(result, status=200, mimetype='application/json')
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000929 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000930
931
Ubuntu82b8a832013-02-06 22:00:11 +0000932if __name__ == "__main__":
933 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000934# 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")
935# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
936# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
937# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
938# link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1)
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000939# print "-- query all switches --"
940# query_switch()
941# print "-- query topo --"
942# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000943# link_change(1,2,3,4)
944 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000945# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000946# print "-- query all devices --"
947# devices()
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000948 iperf_start(1,10,15)
949 iperf_rate(1)
Ubuntu82b8a832013-02-06 22:00:11 +0000950 else:
951 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000952 app.run(threaded=True, host="0.0.0.0", port=9000)