blob: b1ce2ca9d161e1e60cb98c51f9889df3157e93db [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
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000010import random
Ubuntu82b8a832013-02-06 22:00:11 +000011
Masayoshi Kobayashi51011522013-03-27 00:18:12 +000012import re
13
Ubuntu82b8a832013-02-06 22:00:11 +000014from flask import Flask, json, Response, render_template, make_response, request
15
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000016
17CONFIG_FILE=os.getenv("HOME") + "/ONOS/web/config.json"
18
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000019## Global Var for ON.Lab local REST ##
Ubuntuf6ce96c2013-02-07 01:45:07 +000020RestIP="localhost"
Ubuntu82b8a832013-02-06 22:00:11 +000021RestPort=8080
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000022ONOS_DEFAULT_HOST="localhost" ;# Has to set if LB=False
Ubuntu82b8a832013-02-06 22:00:11 +000023DEBUG=1
Ubuntu82b8a832013-02-06 22:00:11 +000024
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000025def read_config():
26 global LB, controllers, core_switches, ONOS_GUI3_HOST, ONOS_GUI3_CONTROL_HOST
27 f = open(CONFIG_FILE)
28 conf = json.load(f)
29 LB = conf['LB']
30 controllers = conf['controllers']
31 core_switcehs=conf['core_switches']
32 ONOS_GUI3_HOST=conf['ONOS_GUI3_HOST']
33 ONOS_GUI3_CONTROL_HOST=conf['ONOS_GUI3_CONTROL_HOST']
34 f.close()
Tim Lindberg201ade22013-04-05 11:52:08 -070035
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000036pp = pprint.PrettyPrinter(indent=4)
Ubuntu82b8a832013-02-06 22:00:11 +000037app = Flask(__name__)
38
39## Worker Functions ##
40def log_error(txt):
41 print '%s' % (txt)
42
43def debug(txt):
44 if DEBUG:
45 print '%s' % (txt)
46
Ubuntu82b8a832013-02-06 22:00:11 +000047### File Fetch ###
48@app.route('/ui/img/<filename>', methods=['GET'])
49@app.route('/img/<filename>', methods=['GET'])
50@app.route('/css/<filename>', methods=['GET'])
51@app.route('/js/models/<filename>', methods=['GET'])
52@app.route('/js/views/<filename>', methods=['GET'])
53@app.route('/js/<filename>', methods=['GET'])
54@app.route('/lib/<filename>', methods=['GET'])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +000055@app.route('/log/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000056@app.route('/', methods=['GET'])
57@app.route('/<filename>', methods=['GET'])
58@app.route('/tpl/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000059@app.route('/ons-demo/<filename>', methods=['GET'])
60@app.route('/ons-demo/js/<filename>', methods=['GET'])
61@app.route('/ons-demo/css/<filename>', methods=['GET'])
62@app.route('/ons-demo/assets/<filename>', methods=['GET'])
63@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000064def return_file(filename="index.html"):
65 if request.path == "/":
66 fullpath = "./index.html"
67 else:
68 fullpath = str(request.path)[1:]
69
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +000070 try:
71 open(fullpath)
72 except:
73 response = make_response("Cannot find a file: %s" % (fullpath), 500)
74 response.headers["Content-type"] = "text/html"
75 return response
76
Ubuntu82b8a832013-02-06 22:00:11 +000077 response = make_response(open(fullpath).read())
78 suffix = fullpath.split(".")[-1]
79
80 if suffix == "html" or suffix == "htm":
81 response.headers["Content-type"] = "text/html"
82 elif suffix == "js":
83 response.headers["Content-type"] = "application/javascript"
84 elif suffix == "css":
85 response.headers["Content-type"] = "text/css"
86 elif suffix == "png":
87 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -070088 elif suffix == "svg":
89 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000090
91 return response
92
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000093## Proxy ##
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070094@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
95def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
96 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -070097 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 -070098 print command
99 result = os.popen(command).read()
100 except:
101 print "REST IF has issue"
102 exit
103
104 resp = Response(result, status=200, mimetype='application/json')
105 return resp
106
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000107@app.route("/proxy/gui/switchctrl/<cmd>")
108def proxy_switch_controller_setting(cmd):
109 try:
110 command = "curl -s %s/gui/switchctrl/%s" % (ONOS_GUI3_CONTROL_HOST, cmd)
111 print command
112 result = os.popen(command).read()
113 except:
114 print "REST IF has issue"
115 exit
116
117 resp = Response(result, status=200, mimetype='application/json')
118 return resp
119
Paul Greyson8d1c6362013-03-27 13:05:24 -0700120@app.route("/proxy/gui/switch/<cmd>/<dpid>")
121def proxy_switch_status_change(cmd, dpid):
122 try:
123 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
124 print command
125 result = os.popen(command).read()
126 except:
127 print "REST IF has issue"
128 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700129
Paul Greyson8d1c6362013-03-27 13:05:24 -0700130 resp = Response(result, status=200, mimetype='application/json')
131 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700132
Paul Greyson2913af82013-03-27 14:53:17 -0700133@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
134def proxy_controller_status_change(cmd, controller_name):
135 try:
136 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
137 print command
138 result = os.popen(command).read()
139 except:
140 print "REST IF has issue"
141 exit
142
143 resp = Response(result, status=200, mimetype='application/json')
144 return resp
145
Paul Greyson472da4c2013-03-28 11:43:17 -0700146@app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
147def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
148 try:
149 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)
150 print command
151 result = os.popen(command).read()
152 except:
153 print "REST IF has issue"
154 exit
155
156 resp = Response(result, status=200, mimetype='application/json')
157 return resp
158
Paul Greyson6f918402013-03-28 12:18:30 -0700159@app.route("/proxy/gui/delflow/<flow_id>")
160def proxy_del_flow(flow_id):
161 try:
162 command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
163 print command
164 result = os.popen(command).read()
165 except:
166 print "REST IF has issue"
167 exit
168
169 resp = Response(result, status=200, mimetype='application/json')
170 return resp
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000171
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700172@app.route("/proxy/gui/iperf/start/<flow_id>/<duration>/<samples>")
173def proxy_iperf_start(flow_id,duration,samples):
174 try:
175 command = "curl -s %s/gui/iperf/start/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id, duration, samples)
176 print command
177 result = os.popen(command).read()
178 except:
179 print "REST IF has issue"
180 exit
181
182 resp = Response(result, status=200, mimetype='application/json')
183 return resp
184
185@app.route("/proxy/gui/iperf/rate/<flow_id>")
186def proxy_iperf_rate(flow_id):
187 try:
188 command = "curl -s %s/gui/iperf/rate/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
189 print command
190 result = os.popen(command).read()
191 except:
192 print "REST IF has issue"
193 exit
194
195 resp = Response(result, status=200, mimetype='application/json')
196 return resp
197
198
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000199###### ONOS RESET API ##############################
200## Worker Func ###
201def get_json(url):
202 code = 200
203 try:
204 command = "curl -s %s" % (url)
205 result = os.popen(command).read()
206 parsedResult = json.loads(result)
Masayoshi Kobayashi8ac33362013-04-05 03:17:26 +0000207 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
208 print "REST %s returned code %s" % (command, parsedResult['code'])
209 code=500
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000210 except:
211 print "REST IF %s has issue" % command
212 result = ""
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000213 code = 500
214
215 return (code, result)
216
217def pick_host():
218 if LB == True:
219 nr_host=len(controllers)
220 r=random.randint(0, nr_host - 1)
221 host=controllers[r]
222 else:
223 host=ONOS_DEFAULT_HOST
224
225 return "http://" + host + ":8080"
226
227## Switch ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000228@app.route("/wm/core/topology/switches/all/json")
229def switches():
230 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000231 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000232 else:
233 host = ONOS_GUI3_HOST
234
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000235 url ="%s/wm/core/topology/switches/all/json" % (host)
236 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000237
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000238 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000239 return resp
240
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000241## Link ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000242@app.route("/wm/core/topology/links/json")
243def links():
244 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000245 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000246 else:
247 host = ONOS_GUI3_HOST
248
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000249 url ="%s/wm/core/topology/links/json" % (host)
250 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000251
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000252 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000253 return resp
254
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000255## FlowSummary ##
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000256@app.route("/wm/flow/getsummary/<start>/<range>/json")
257def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000258 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000259 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000260 else:
261 host = ONOS_GUI3_HOST
262
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000263 url ="%s/wm/flow/getsummary/%s/%s/json" % (host, start, range)
264 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000265
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000266 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000267 return resp
268
269@app.route("/wm/registry/controllers/json")
270def registry_controllers():
271 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000272 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000273 else:
274 host = ONOS_GUI3_HOST
275
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000276 url= "%s/wm/registry/controllers/json" % (host)
277 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000278
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000279 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000280 return resp
281
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000282
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000283@app.route("/wm/registry/switches/json")
284def registry_switches():
285 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000286 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000287 else:
288 host = ONOS_GUI3_HOST
289
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000290 url="%s/wm/registry/switches/json" % (host)
291 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000292
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000293 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000294 return resp
295
Ubuntu82b8a832013-02-06 22:00:11 +0000296def node_id(switch_array, dpid):
297 id = -1
298 for i, val in enumerate(switch_array):
299 if val['name'] == dpid:
300 id = i
301 break
302
303 return id
304
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000305## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000306@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000307def topology_for_gui():
308 try:
309 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
310 result = os.popen(command).read()
311 parsedResult = json.loads(result)
312 except:
313 log_error("REST IF has issue: %s" % command)
314 log_error("%s" % result)
315 sys.exit(0)
316
317 topo = {}
318 switches = []
319 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000320 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000321
322 for v in parsedResult:
323 if v.has_key('dpid'):
324# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
325 dpid = str(v['dpid'])
326 state = str(v['state'])
327 sw = {}
328 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000329 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000330
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000331 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000332 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000333 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000334
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000335 try:
336 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
337 result = os.popen(command).read()
338 parsedResult = json.loads(result)
339 except:
340 log_error("REST IF has issue: %s" % command)
341 log_error("%s" % result)
342
343 for key in parsedResult:
344 dpid = key
345 ctrl = parsedResult[dpid][0]['controllerId']
346 sw_id = node_id(switches, dpid)
347 if sw_id != -1:
348 if switches[sw_id]['group'] != 0:
349 switches[sw_id]['group'] = controllers.index(ctrl) + 1
350
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000351 try:
352 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000353# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000354 p1=1
355 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000356# v2 = "00:00:00:00:00:0d:00:d3"
357 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000358 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
359 result = os.popen(command).read()
360 parsedResult = json.loads(result)
361 except:
362 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000363 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000364
Ubuntu765deff2013-02-28 18:39:13 +0000365 path = []
366 if parsedResult.has_key('flowEntries'):
367 flowEntries= parsedResult['flowEntries']
368 for i, v in enumerate(flowEntries):
369 if i < len(flowEntries) - 1:
370 sdpid= flowEntries[i]['dpid']['value']
371 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700372 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000373
Ubuntu82b8a832013-02-06 22:00:11 +0000374 try:
375 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
376 result = os.popen(command).read()
377 parsedResult = json.loads(result)
378 except:
379 log_error("REST IF has issue: %s" % command)
380 log_error("%s" % result)
381 sys.exit(0)
382
383 for v in parsedResult:
384 link = {}
385 if v.has_key('dst-switch'):
386 dst_dpid = str(v['dst-switch'])
387 dst_id = node_id(switches, dst_dpid)
388 if v.has_key('src-switch'):
389 src_dpid = str(v['src-switch'])
390 src_id = node_id(switches, src_dpid)
391 link['source'] = src_id
392 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000393
394 onpath = 0
395 for (s,d) in path:
396 if s == v['src-switch'] and d == v['dst-switch']:
397 onpath = 1
398 break
399 link['type'] = onpath
400
Ubuntu82b8a832013-02-06 22:00:11 +0000401 links.append(link)
402
403 topo['nodes'] = switches
404 topo['links'] = links
405
Ubuntu82b8a832013-02-06 22:00:11 +0000406 js = json.dumps(topo)
407 resp = Response(js, status=200, mimetype='application/json')
408 return resp
409
Ubuntuaea2a682013-02-08 08:30:10 +0000410#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
411#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
412@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
413def shortest_path(v1, p1, v2, p2):
414 try:
415 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
416 result = os.popen(command).read()
417 parsedResult = json.loads(result)
418 except:
419 log_error("REST IF has issue: %s" % command)
420 log_error("%s" % result)
421 sys.exit(0)
422
423 topo = {}
424 switches = []
425 links = []
426
427 for v in parsedResult:
428 if v.has_key('dpid'):
429 dpid = str(v['dpid'])
430 state = str(v['state'])
431 sw = {}
432 sw['name']=dpid
433 if str(v['state']) == "ACTIVE":
434 if dpid[-2:-1] == "a":
435 sw['group']=1
436 if dpid[-2:-1] == "b":
437 sw['group']=2
438 if dpid[-2:-1] == "c":
439 sw['group']=3
440 if str(v['state']) == "INACTIVE":
441 sw['group']=0
442
443 switches.append(sw)
444
445 try:
446 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
447 result = os.popen(command).read()
448 parsedResult = json.loads(result)
449 except:
450 log_error("No route")
451 parsedResult = []
452# exit(1)
453
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700454 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000455 for i, v in enumerate(parsedResult):
456 if i < len(parsedResult) - 1:
457 sdpid= parsedResult[i]['switch']
458 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700459 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000460
461 try:
462 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
463 result = os.popen(command).read()
464 parsedResult = json.loads(result)
465 except:
466 log_error("REST IF has issue: %s" % command)
467 log_error("%s" % result)
468 sys.exit(0)
469
470 for v in parsedResult:
471 link = {}
472 if v.has_key('dst-switch'):
473 dst_dpid = str(v['dst-switch'])
474 dst_id = node_id(switches, dst_dpid)
475 if v.has_key('src-switch'):
476 src_dpid = str(v['src-switch'])
477 src_id = node_id(switches, src_dpid)
478 link['source'] = src_id
479 link['target'] = dst_id
480 onpath = 0
481 for (s,d) in path:
482 if s == v['src-switch'] and d == v['dst-switch']:
483 onpath = 1
484 break
485
486 link['type'] = onpath
487 links.append(link)
488
489 topo['nodes'] = switches
490 topo['links'] = links
491
Ubuntuaea2a682013-02-08 08:30:10 +0000492 js = json.dumps(topo)
493 resp = Response(js, status=200, mimetype='application/json')
494 return resp
495
Ubuntu82b8a832013-02-06 22:00:11 +0000496@app.route("/wm/core/controller/switches/json")
497def query_switch():
498 try:
499 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
500# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000501 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000502 result = os.popen(command).read()
503 parsedResult = json.loads(result)
504 except:
505 log_error("REST IF has issue: %s" % command)
506 log_error("%s" % result)
507 sys.exit(0)
508
509# print command
510# print result
511 switches_ = []
512 for v in parsedResult:
513 if v.has_key('dpid'):
514 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
515 dpid = str(v['dpid'])
516 state = str(v['state'])
517 sw = {}
518 sw['dpid']=dpid
519 sw['active']=state
520 switches_.append(sw)
521
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000522# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000523 js = json.dumps(switches_)
524 resp = Response(js, status=200, mimetype='application/json')
525 return resp
526
527@app.route("/wm/device/")
528def devices():
529 try:
530 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
531 result = os.popen(command).read()
532 parsedResult = json.loads(result)['results']
533 except:
534 log_error("REST IF has issue: %s" % command)
535 log_error("%s" % result)
536 sys.exit(0)
537
538 devices = []
539 for v in parsedResult:
540 dl_addr = v['dl_addr']
541 nw_addr = v['nw_addr']
542 vertex = v['_id']
543 mac = []
544 mac.append(dl_addr)
545 ip = []
546 ip.append(nw_addr)
547 device = {}
548 device['entryClass']="DefaultEntryClass"
549 device['mac']=mac
550 device['ipv4']=ip
551 device['vlan']=[]
552 device['lastSeen']=0
553 attachpoints =[]
554
555 port, dpid = deviceV_to_attachpoint(vertex)
556 attachpoint = {}
557 attachpoint['port']=port
558 attachpoint['switchDPID']=dpid
559 attachpoints.append(attachpoint)
560 device['attachmentPoint']=attachpoints
561 devices.append(device)
562
Ubuntu82b8a832013-02-06 22:00:11 +0000563 js = json.dumps(devices)
564 resp = Response(js, status=200, mimetype='application/json')
565 return resp
566
567#{"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}
568
Ubuntu82b8a832013-02-06 22:00:11 +0000569## return fake stat for now
570@app.route("/wm/core/switch/<switchId>/<statType>/json")
571def switch_stat(switchId, statType):
572 if statType == "desc":
573 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
574 ret = {}
575 ret[switchId]=desc
576 elif statType == "aggregate":
577 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
578 ret = {}
579 ret[switchId]=aggr
580 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700581 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000582
583 js = json.dumps(ret)
584 resp = Response(js, status=200, mimetype='application/json')
585 return resp
586
587
588@app.route("/wm/topology/links/json")
589def query_links():
590 try:
591 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000592 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000593 result = os.popen(command).read()
594 parsedResult = json.loads(result)['results']
595 except:
596 log_error("REST IF has issue: %s" % command)
597 log_error("%s" % result)
598 sys.exit(0)
599
600 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000601# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000602 sport = []
603 links = []
604 for v in parsedResult:
605 srcport = v['_id']
606 try:
607 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
608 print command
609 result = os.popen(command).read()
610 linkResults = 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 for p in linkResults:
617 if p.has_key('type') and p['type'] == "port":
618 dstport = p['_id']
619 (sport, sdpid) = portV_to_port_dpid(srcport)
620 (dport, ddpid) = portV_to_port_dpid(dstport)
621 link = {}
622 link["src-switch"]=sdpid
623 link["src-port"]=sport
624 link["src-port-state"]=0
625 link["dst-switch"]=ddpid
626 link["dst-port"]=dport
627 link["dst-port-state"]=0
628 link["type"]="internal"
629 links.append(link)
630
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000631# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000632 js = json.dumps(links)
633 resp = Response(js, status=200, mimetype='application/json')
634 return resp
635
Ubuntuc016ba12013-02-27 21:53:41 +0000636@app.route("/controller_status")
637def controller_status():
Tim Lindberg201ade22013-04-05 11:52:08 -0700638# onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
639 onos_check="cd; onos status %s | grep %s | awk '{print $2}'"
Ubuntuc016ba12013-02-27 21:53:41 +0000640 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
641
642 cont_status=[]
643 for i in controllers:
644 status={}
645 onos=os.popen(onos_check % i).read()[:-1]
Tim Lindberg201ade22013-04-05 11:52:08 -0700646 onos=os.popen(onos_check % (i, i.lower())).read()[:-1]
Ubuntuc016ba12013-02-27 21:53:41 +0000647 status["name"]=i
648 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000649 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000650 cont_status.append(status)
651
652 js = json.dumps(cont_status)
653 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000654 return resp
655
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000656### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000657@app.route("/gui/controller/<cmd>/<controller_name>")
658def controller_status_change(cmd, controller_name):
Tim Lindberg201ade22013-04-05 11:52:08 -0700659# start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
660# stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
661 start_onos="cd; onos start %s" % (controller_name[-1:])
662 stop_onos="cd; onos stop %s" % (controller_name[-1:])
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000663
664 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000665 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000666 ret = "controller %s is up" % (controller_name)
667 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000668 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000669 ret = "controller %s is down" % (controller_name)
670
671 return ret
672
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000673@app.route("/gui/switchctrl/<cmd>")
674def switch_controller_setting(cmd):
675 if cmd =="local":
676 print "All aggr switches connects to local controller only"
677 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700678 if (TESTBED == "sw"):
679 for i in range(0, len(controllers)):
680 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-local.sh'" % (controllers[i])
681 result += os.popen(cmd_string).read()
682 else:
683 cmd_string="cd; switch local"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000684 result += os.popen(cmd_string).read()
685 elif cmd =="all":
686 print "All aggr switches connects to all controllers except for core controller"
687 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700688 if (TESTBED == "sw"):
689 for i in range(0, len(controllers)):
690 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i])
691 result += os.popen(cmd_string).read()
692 else:
693 cmd_string="cd; switch all"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000694 result += os.popen(cmd_string).read()
695
696 return result
697
698
699
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000700@app.route("/gui/switch/<cmd>/<dpid>")
701def switch_status_change(cmd, dpid):
Tim Lindberg201ade22013-04-05 11:52:08 -0700702 result = ""
703 if (TESTBED == "hw"):
704 return result
705
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000706 r = re.compile(':')
707 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000708 host=controllers[0]
709 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
710 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000711 print "cmd_string"
712
713 if cmd =="up" or cmd=="down":
714 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000715 os.popen(cmd_string)
716 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000717
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000718 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000719
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000720#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000721#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000722@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
723def link_up(src_dpid, src_port, dst_dpid, dst_port):
Tim Lindberg201ade22013-04-05 11:52:08 -0700724 result = ""
725
726 if (TESTBED == "sw"):
727 result = link_up_sw(src_dpid, src_port, dst_dpid, dst_port)
728 else:
729 result = link_up_hw(src_dpid, src_port, dst_dpid, dst_port)
730 return result
731
732# Link up on software testbed
733def link_up_sw(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000734
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000735 cmd = 'up'
736 result=""
737
Paul Greyson472da4c2013-03-28 11:43:17 -0700738 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000739 if dpid in core_switches:
740 host = controllers[0]
741 src_ports = [1, 2, 3, 4, 5]
742 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000743 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000744 host = controllers[hostid-1]
745 if hostid == 2 :
746 src_ports = [51]
747 else :
748 src_ports = [26]
749
750 for port in src_ports :
751 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
752 print cmd_string
753 res=os.popen(cmd_string).read()
754 result = result + ' ' + res
755
756 return result
757
Tim Lindberg201ade22013-04-05 11:52:08 -0700758# Link up on hardware testbed
759def link_up_hw(src_dpid, src_port, dst_dpid, dst_port):
760
761 port1 = src_port
762 port2 = dst_port
763 if src_dpid == "00:00:00:00:ba:5e:ba:11":
764 if dst_dpid == "00:00:00:08:a2:08:f9:01":
765 port1 = 24
766 port2 = 24
767 elif dst_dpid == "00:01:00:16:97:08:9a:46":
768 port1 = 23
769 port2 = 23
770 elif src_dpid == "00:00:00:00:ba:5e:ba:13":
771 if dst_dpid == "00:00:20:4e:7f:51:8a:35":
772 port1 = 22
773 port2 = 22
774 elif dst_dpid == "00:00:00:00:00:00:ba:12":
775 port1 = 23
776 port2 = 23
777 elif src_dpid == "00:00:00:00:00:00:ba:12":
778 if dst_dpid == "00:00:00:00:ba:5e:ba:13":
779 port1 = 23
780 port2 = 23
781 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
782 port1 = 22
783 port2 = 22
784 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
785 port1 = 24
786 port2 = 21
787 elif src_dpid == "00:01:00:16:97:08:9a:46":
788 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
789 port1 = 23
790 port2 = 23
791 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
792 port1 = 24
793 port2 = 24
794 elif src_dpid == "00:00:00:08:a2:08:f9:01":
795 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
796 port1 = 24
797 port2 = 24
798 elif dst_dpid == "00:00:00:00:00:00:ba:12":
799 port1 = 22
800 port2 = 22
801 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
802 port1 = 23
803 port2 = 23
804 elif src_dpid == "00:00:20:4e:7f:51:8a:35":
805 if dst_dpid == "00:00:00:00:00:00:ba:12":
806 port1 = 21
807 port2 = 24
808 elif dst_dpid == "00:00:00:00:ba:5e:ba:13":
809 port1 = 22
810 port2 = 22
811 elif dst_dpid == "00:01:00:16:97:08:9a:46":
812 port1 = 24
813 port2 = 24
814 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
815 port1 = 23
816 port2 = 23
817
818 cmd = 'up'
819 result=""
820 host = controllers[0]
821 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % (src_dpid, port1, cmd)
822 print cmd_string
823 res=os.popen(cmd_string).read()
824 result = result + ' ' + res
825 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % (dst_dpid, port2, cmd)
826 print cmd_string
827 res=os.popen(cmd_string).read()
828 result = result + ' ' + res
829
830
831 return result
832
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000833
834#* Link Down
835#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000836@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000837def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
838
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000839 if src_dpid in core_switches:
840 host = controllers[0]
841 else:
842 hostid=int(src_dpid.split(':')[-2])
843 host = controllers[hostid-1]
844
Tim Lindberg201ade22013-04-05 11:52:08 -0700845 if (TESTBED == "sw"):
846 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
847 else:
Tim Lindberga6c04172013-04-05 17:34:05 -0700848 if ( src_dpid == "00:00:00:08:a2:08:f9:01" ):
849 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % ( dst_dpid, dst_port, cmd)
850 else:
851 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % ( src_dpid, src_port, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000852 print cmd_string
853
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000854 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000855
856 return result
857
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000858#* Create Flow
859#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000860#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 +0000861@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000862def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
863 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
864 print command
865 ret = os.popen(command).read()
866 if ret == "":
867 flow_nr=0
868 else:
869 flow_nr=int(ret)
870
871 flow_nr += 1
Umesh Krishnaswamyf22d3ed2013-04-03 11:57:54 -0700872 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 Kobayashicdb652f2013-04-04 18:24:29 +0000873 command1 = "/home/ubuntu/ONOS/web/add_flow.py -m onos %d %s %s %s %s %s matchSrcMac %s matchDstMac %s" % (flow_nr, "dummy", dst_dpid, dst_port, src_dpid, src_port, dstMAC, srcMAC)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000874 print command
875 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000876 errcode1 = os.popen(command1).read()
877 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000878
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000879#* Delete Flow
880#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000881@app.route("/gui/delflow/<flow_id>")
882def del_flow(flow_id):
883 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
884 print command
885 errcode = os.popen(command).read()
886 return errcode
887
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000888#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700889#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000890@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
891def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000892 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000893 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000894 print command
895 result = os.popen(command).read()
896 if len(result) == 0:
897 print "No Flow found"
898 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000899 except:
900 print "REST IF has issue"
901 exit
902
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000903 parsedResult = json.loads(result)
904
905 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000906 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
907 src_port = parsedResult['dataPath']['srcPort']['port']['value']
908 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
909 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000910# 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 +0000911
912 if src_dpid in core_switches:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700913 src_host = controllers[0]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000914 else:
915 hostid=int(src_dpid.split(':')[-2])
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700916 src_host = controllers[hostid-1]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000917
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700918 if dst_dpid in core_switches:
919 dst_host = controllers[0]
920 else:
921 hostid=int(dst_dpid.split(':')[-2])
922 dst_host = controllers[hostid-1]
923
924# /runiperf.sh <flowid> <src_dpid> <dst_dpid> svr|client <proto>/<duration>/<interval>/<samples>
925 protocol="udp"
926 interval=0.1
927 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./runiperf.sh %d %s %s %s %s/%s/%s/%s'" % (dst_host, flowId, src_dpid, dst_dpid, "svr", protocol, duration, interval, samples)
928 print cmd_string
929 os.popen(cmd_string)
930
931 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./runiperf.sh %d %s %s %s %s/%s/%s/%s'" % (src_host, flowId, src_dpid, dst_dpid, "client", protocol, duration, interval, samples)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000932 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000933 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000934
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000935 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000936
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000937#* Get Iperf Throughput
938#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000939@app.route("/gui/iperf/rate/<flow_id>")
940def iperf_rate(flow_id):
Tim Lindberga6c04172013-04-05 17:34:05 -0700941 if (TESTBED == "hw"):
942 return "{}"
943
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000944 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000945 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
946 print command
947 result = os.popen(command).read()
948 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000949 resp = Response(result, status=400, mimetype='text/html')
950 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000951 except:
952 print "REST IF has issue"
953 exit
954
955 parsedResult = json.loads(result)
956
957 flowId = int(parsedResult['flowId']['value'], 16)
958 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
959 src_port = parsedResult['dataPath']['srcPort']['port']['value']
960 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
961 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
962
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700963 if dst_dpid in core_switches:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000964 host = controllers[0]
965 else:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700966 hostid=int(dst_dpid.split(':')[-2])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000967 host = controllers[hostid-1]
968
969 try:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700970 command = "curl -s http://%s:%s/log/iperfsvr_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000971 print command
972 result = os.popen(command).read()
973 except:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000974 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000975
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +0000976 if len(result) == 0:
977 resp = Response(result, status=400, mimetype='text/html')
978 return "no iperf file found (flowid %s)" % flow_id;
979 else:
980 resp = Response(result, status=200, mimetype='application/json')
981 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000982
983
Ubuntu82b8a832013-02-06 22:00:11 +0000984if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000985 random.seed()
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +0000986 read_config()
Ubuntu82b8a832013-02-06 22:00:11 +0000987 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000988# 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")
989# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
990# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
991# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
992# 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 +0000993# print "-- query all switches --"
994# query_switch()
995# print "-- query topo --"
996# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000997# link_change(1,2,3,4)
998 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000999# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +00001000# print "-- query all devices --"
1001# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001002# iperf_start(1,10,15)
1003# iperf_rate(1)
1004 switches()
Ubuntu82b8a832013-02-06 22:00:11 +00001005 else:
1006 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +00001007 app.run(threaded=True, host="0.0.0.0", port=9000)