blob: 44d7ab7eb70c175277b8b50f7d57e79b8195a843 [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():
Jonathan Hartb2554482013-04-08 13:40:31 -070026 global LB, TESTBED, controllers, core_switches, ONOS_GUI3_HOST, ONOS_GUI3_CONTROL_HOST
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000027 f = open(CONFIG_FILE)
28 conf = json.load(f)
29 LB = conf['LB']
Jonathan Hartb2554482013-04-08 13:40:31 -070030 TESTBED = conf['TESTBED']
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000031 controllers = conf['controllers']
Jonathan Hartb2554482013-04-08 13:40:31 -070032 core_switches=conf['core_switches']
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000033 ONOS_GUI3_HOST=conf['ONOS_GUI3_HOST']
34 ONOS_GUI3_CONTROL_HOST=conf['ONOS_GUI3_CONTROL_HOST']
35 f.close()
Tim Lindberg201ade22013-04-05 11:52:08 -070036
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000037pp = pprint.PrettyPrinter(indent=4)
Ubuntu82b8a832013-02-06 22:00:11 +000038app = Flask(__name__)
39
40## Worker Functions ##
41def log_error(txt):
42 print '%s' % (txt)
43
44def debug(txt):
45 if DEBUG:
46 print '%s' % (txt)
47
Ubuntu82b8a832013-02-06 22:00:11 +000048### File Fetch ###
49@app.route('/ui/img/<filename>', methods=['GET'])
50@app.route('/img/<filename>', methods=['GET'])
51@app.route('/css/<filename>', methods=['GET'])
52@app.route('/js/models/<filename>', methods=['GET'])
53@app.route('/js/views/<filename>', methods=['GET'])
54@app.route('/js/<filename>', methods=['GET'])
55@app.route('/lib/<filename>', methods=['GET'])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +000056@app.route('/log/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000057@app.route('/', methods=['GET'])
58@app.route('/<filename>', methods=['GET'])
59@app.route('/tpl/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000060@app.route('/ons-demo/<filename>', methods=['GET'])
61@app.route('/ons-demo/js/<filename>', methods=['GET'])
62@app.route('/ons-demo/css/<filename>', methods=['GET'])
63@app.route('/ons-demo/assets/<filename>', methods=['GET'])
64@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000065def return_file(filename="index.html"):
66 if request.path == "/":
67 fullpath = "./index.html"
68 else:
69 fullpath = str(request.path)[1:]
70
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +000071 try:
72 open(fullpath)
73 except:
74 response = make_response("Cannot find a file: %s" % (fullpath), 500)
75 response.headers["Content-type"] = "text/html"
76 return response
77
Ubuntu82b8a832013-02-06 22:00:11 +000078 response = make_response(open(fullpath).read())
79 suffix = fullpath.split(".")[-1]
80
81 if suffix == "html" or suffix == "htm":
82 response.headers["Content-type"] = "text/html"
83 elif suffix == "js":
84 response.headers["Content-type"] = "application/javascript"
85 elif suffix == "css":
86 response.headers["Content-type"] = "text/css"
87 elif suffix == "png":
88 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -070089 elif suffix == "svg":
90 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000091
92 return response
93
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000094## Proxy ##
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070095@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
96def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
97 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -070098 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 -070099 print command
100 result = os.popen(command).read()
101 except:
102 print "REST IF has issue"
103 exit
104
105 resp = Response(result, status=200, mimetype='application/json')
106 return resp
107
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000108@app.route("/proxy/gui/switchctrl/<cmd>")
109def proxy_switch_controller_setting(cmd):
110 try:
111 command = "curl -s %s/gui/switchctrl/%s" % (ONOS_GUI3_CONTROL_HOST, cmd)
112 print command
113 result = os.popen(command).read()
114 except:
115 print "REST IF has issue"
116 exit
117
118 resp = Response(result, status=200, mimetype='application/json')
119 return resp
120
Paul Greyson8d1c6362013-03-27 13:05:24 -0700121@app.route("/proxy/gui/switch/<cmd>/<dpid>")
122def proxy_switch_status_change(cmd, dpid):
123 try:
124 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
125 print command
126 result = os.popen(command).read()
127 except:
128 print "REST IF has issue"
129 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700130
Paul Greyson8d1c6362013-03-27 13:05:24 -0700131 resp = Response(result, status=200, mimetype='application/json')
132 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700133
Paul Greyson2913af82013-03-27 14:53:17 -0700134@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
135def proxy_controller_status_change(cmd, controller_name):
136 try:
137 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
138 print command
139 result = os.popen(command).read()
140 except:
141 print "REST IF has issue"
142 exit
143
144 resp = Response(result, status=200, mimetype='application/json')
145 return resp
146
Paul Greyson472da4c2013-03-28 11:43:17 -0700147@app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
148def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
149 try:
150 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)
151 print command
152 result = os.popen(command).read()
153 except:
154 print "REST IF has issue"
155 exit
156
157 resp = Response(result, status=200, mimetype='application/json')
158 return resp
159
Paul Greyson6f918402013-03-28 12:18:30 -0700160@app.route("/proxy/gui/delflow/<flow_id>")
161def proxy_del_flow(flow_id):
162 try:
163 command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
164 print command
165 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
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000172
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700173@app.route("/proxy/gui/iperf/start/<flow_id>/<duration>/<samples>")
174def proxy_iperf_start(flow_id,duration,samples):
175 try:
176 command = "curl -s %s/gui/iperf/start/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id, duration, samples)
177 print command
178 result = os.popen(command).read()
179 except:
180 print "REST IF has issue"
181 exit
182
183 resp = Response(result, status=200, mimetype='application/json')
184 return resp
185
186@app.route("/proxy/gui/iperf/rate/<flow_id>")
187def proxy_iperf_rate(flow_id):
188 try:
189 command = "curl -s %s/gui/iperf/rate/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
190 print command
191 result = os.popen(command).read()
192 except:
193 print "REST IF has issue"
194 exit
195
196 resp = Response(result, status=200, mimetype='application/json')
197 return resp
198
199
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000200###### ONOS RESET API ##############################
201## Worker Func ###
202def get_json(url):
203 code = 200
204 try:
205 command = "curl -s %s" % (url)
206 result = os.popen(command).read()
207 parsedResult = json.loads(result)
Masayoshi Kobayashi8ac33362013-04-05 03:17:26 +0000208 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
209 print "REST %s returned code %s" % (command, parsedResult['code'])
210 code=500
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000211 except:
212 print "REST IF %s has issue" % command
213 result = ""
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000214 code = 500
215
216 return (code, result)
217
218def pick_host():
219 if LB == True:
220 nr_host=len(controllers)
221 r=random.randint(0, nr_host - 1)
222 host=controllers[r]
223 else:
224 host=ONOS_DEFAULT_HOST
225
226 return "http://" + host + ":8080"
227
228## Switch ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000229@app.route("/wm/core/topology/switches/all/json")
230def switches():
231 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000232 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000233 else:
234 host = ONOS_GUI3_HOST
235
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000236 url ="%s/wm/core/topology/switches/all/json" % (host)
237 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000238
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000239 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000240 return resp
241
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000242## Link ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000243@app.route("/wm/core/topology/links/json")
244def links():
245 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000246 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000247 else:
248 host = ONOS_GUI3_HOST
249
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000250 url ="%s/wm/core/topology/links/json" % (host)
251 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000252
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000253 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000254 return resp
255
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000256## FlowSummary ##
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000257@app.route("/wm/flow/getsummary/<start>/<range>/json")
258def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000259 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000260 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000261 else:
262 host = ONOS_GUI3_HOST
263
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000264 url ="%s/wm/flow/getsummary/%s/%s/json" % (host, start, range)
265 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000266
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000267 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000268 return resp
269
270@app.route("/wm/registry/controllers/json")
271def registry_controllers():
272 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000273 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000274 else:
275 host = ONOS_GUI3_HOST
276
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000277 url= "%s/wm/registry/controllers/json" % (host)
278 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000279
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000280 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000281 return resp
282
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000283
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000284@app.route("/wm/registry/switches/json")
285def registry_switches():
286 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000287 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000288 else:
289 host = ONOS_GUI3_HOST
290
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000291 url="%s/wm/registry/switches/json" % (host)
292 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000293
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000294 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000295 return resp
296
Ubuntu82b8a832013-02-06 22:00:11 +0000297def node_id(switch_array, dpid):
298 id = -1
299 for i, val in enumerate(switch_array):
300 if val['name'] == dpid:
301 id = i
302 break
303
304 return id
305
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000306## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000307@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000308def topology_for_gui():
309 try:
310 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
311 result = os.popen(command).read()
312 parsedResult = json.loads(result)
313 except:
314 log_error("REST IF has issue: %s" % command)
315 log_error("%s" % result)
316 sys.exit(0)
317
318 topo = {}
319 switches = []
320 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000321 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000322
323 for v in parsedResult:
324 if v.has_key('dpid'):
325# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
326 dpid = str(v['dpid'])
327 state = str(v['state'])
328 sw = {}
329 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000330 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000331
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000332 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000333 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000334 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000335
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000336 try:
337 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
338 result = os.popen(command).read()
339 parsedResult = json.loads(result)
340 except:
341 log_error("REST IF has issue: %s" % command)
342 log_error("%s" % result)
343
344 for key in parsedResult:
345 dpid = key
346 ctrl = parsedResult[dpid][0]['controllerId']
347 sw_id = node_id(switches, dpid)
348 if sw_id != -1:
349 if switches[sw_id]['group'] != 0:
350 switches[sw_id]['group'] = controllers.index(ctrl) + 1
351
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000352 try:
353 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000354# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000355 p1=1
356 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000357# v2 = "00:00:00:00:00:0d:00:d3"
358 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000359 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
360 result = os.popen(command).read()
361 parsedResult = json.loads(result)
362 except:
363 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000364 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000365
Ubuntu765deff2013-02-28 18:39:13 +0000366 path = []
367 if parsedResult.has_key('flowEntries'):
368 flowEntries= parsedResult['flowEntries']
369 for i, v in enumerate(flowEntries):
370 if i < len(flowEntries) - 1:
371 sdpid= flowEntries[i]['dpid']['value']
372 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700373 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000374
Ubuntu82b8a832013-02-06 22:00:11 +0000375 try:
376 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
377 result = os.popen(command).read()
378 parsedResult = json.loads(result)
379 except:
380 log_error("REST IF has issue: %s" % command)
381 log_error("%s" % result)
382 sys.exit(0)
383
384 for v in parsedResult:
385 link = {}
386 if v.has_key('dst-switch'):
387 dst_dpid = str(v['dst-switch'])
388 dst_id = node_id(switches, dst_dpid)
389 if v.has_key('src-switch'):
390 src_dpid = str(v['src-switch'])
391 src_id = node_id(switches, src_dpid)
392 link['source'] = src_id
393 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000394
395 onpath = 0
396 for (s,d) in path:
397 if s == v['src-switch'] and d == v['dst-switch']:
398 onpath = 1
399 break
400 link['type'] = onpath
401
Ubuntu82b8a832013-02-06 22:00:11 +0000402 links.append(link)
403
404 topo['nodes'] = switches
405 topo['links'] = links
406
Ubuntu82b8a832013-02-06 22:00:11 +0000407 js = json.dumps(topo)
408 resp = Response(js, status=200, mimetype='application/json')
409 return resp
410
Ubuntuaea2a682013-02-08 08:30:10 +0000411#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
412#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
413@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
414def shortest_path(v1, p1, v2, p2):
415 try:
416 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
417 result = os.popen(command).read()
418 parsedResult = json.loads(result)
419 except:
420 log_error("REST IF has issue: %s" % command)
421 log_error("%s" % result)
422 sys.exit(0)
423
424 topo = {}
425 switches = []
426 links = []
427
428 for v in parsedResult:
429 if v.has_key('dpid'):
430 dpid = str(v['dpid'])
431 state = str(v['state'])
432 sw = {}
433 sw['name']=dpid
434 if str(v['state']) == "ACTIVE":
435 if dpid[-2:-1] == "a":
436 sw['group']=1
437 if dpid[-2:-1] == "b":
438 sw['group']=2
439 if dpid[-2:-1] == "c":
440 sw['group']=3
441 if str(v['state']) == "INACTIVE":
442 sw['group']=0
443
444 switches.append(sw)
445
446 try:
447 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
448 result = os.popen(command).read()
449 parsedResult = json.loads(result)
450 except:
451 log_error("No route")
452 parsedResult = []
453# exit(1)
454
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700455 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000456 for i, v in enumerate(parsedResult):
457 if i < len(parsedResult) - 1:
458 sdpid= parsedResult[i]['switch']
459 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700460 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000461
462 try:
463 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
464 result = os.popen(command).read()
465 parsedResult = json.loads(result)
466 except:
467 log_error("REST IF has issue: %s" % command)
468 log_error("%s" % result)
469 sys.exit(0)
470
471 for v in parsedResult:
472 link = {}
473 if v.has_key('dst-switch'):
474 dst_dpid = str(v['dst-switch'])
475 dst_id = node_id(switches, dst_dpid)
476 if v.has_key('src-switch'):
477 src_dpid = str(v['src-switch'])
478 src_id = node_id(switches, src_dpid)
479 link['source'] = src_id
480 link['target'] = dst_id
481 onpath = 0
482 for (s,d) in path:
483 if s == v['src-switch'] and d == v['dst-switch']:
484 onpath = 1
485 break
486
487 link['type'] = onpath
488 links.append(link)
489
490 topo['nodes'] = switches
491 topo['links'] = links
492
Ubuntuaea2a682013-02-08 08:30:10 +0000493 js = json.dumps(topo)
494 resp = Response(js, status=200, mimetype='application/json')
495 return resp
496
Ubuntu82b8a832013-02-06 22:00:11 +0000497@app.route("/wm/core/controller/switches/json")
498def query_switch():
499 try:
500 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
501# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000502 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000503 result = os.popen(command).read()
504 parsedResult = json.loads(result)
505 except:
506 log_error("REST IF has issue: %s" % command)
507 log_error("%s" % result)
508 sys.exit(0)
509
510# print command
511# print result
512 switches_ = []
513 for v in parsedResult:
514 if v.has_key('dpid'):
515 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
516 dpid = str(v['dpid'])
517 state = str(v['state'])
518 sw = {}
519 sw['dpid']=dpid
520 sw['active']=state
521 switches_.append(sw)
522
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000523# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000524 js = json.dumps(switches_)
525 resp = Response(js, status=200, mimetype='application/json')
526 return resp
527
528@app.route("/wm/device/")
529def devices():
530 try:
531 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
532 result = os.popen(command).read()
533 parsedResult = json.loads(result)['results']
534 except:
535 log_error("REST IF has issue: %s" % command)
536 log_error("%s" % result)
537 sys.exit(0)
538
539 devices = []
540 for v in parsedResult:
541 dl_addr = v['dl_addr']
542 nw_addr = v['nw_addr']
543 vertex = v['_id']
544 mac = []
545 mac.append(dl_addr)
546 ip = []
547 ip.append(nw_addr)
548 device = {}
549 device['entryClass']="DefaultEntryClass"
550 device['mac']=mac
551 device['ipv4']=ip
552 device['vlan']=[]
553 device['lastSeen']=0
554 attachpoints =[]
555
556 port, dpid = deviceV_to_attachpoint(vertex)
557 attachpoint = {}
558 attachpoint['port']=port
559 attachpoint['switchDPID']=dpid
560 attachpoints.append(attachpoint)
561 device['attachmentPoint']=attachpoints
562 devices.append(device)
563
Ubuntu82b8a832013-02-06 22:00:11 +0000564 js = json.dumps(devices)
565 resp = Response(js, status=200, mimetype='application/json')
566 return resp
567
568#{"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}
569
Ubuntu82b8a832013-02-06 22:00:11 +0000570## return fake stat for now
571@app.route("/wm/core/switch/<switchId>/<statType>/json")
572def switch_stat(switchId, statType):
573 if statType == "desc":
574 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
575 ret = {}
576 ret[switchId]=desc
577 elif statType == "aggregate":
578 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
579 ret = {}
580 ret[switchId]=aggr
581 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700582 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000583
584 js = json.dumps(ret)
585 resp = Response(js, status=200, mimetype='application/json')
586 return resp
587
588
589@app.route("/wm/topology/links/json")
590def query_links():
591 try:
592 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000593 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000594 result = os.popen(command).read()
595 parsedResult = json.loads(result)['results']
596 except:
597 log_error("REST IF has issue: %s" % command)
598 log_error("%s" % result)
599 sys.exit(0)
600
601 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000602# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000603 sport = []
604 links = []
605 for v in parsedResult:
606 srcport = v['_id']
607 try:
608 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
609 print command
610 result = os.popen(command).read()
611 linkResults = json.loads(result)['results']
612 except:
613 log_error("REST IF has issue: %s" % command)
614 log_error("%s" % result)
615 sys.exit(0)
616
617 for p in linkResults:
618 if p.has_key('type') and p['type'] == "port":
619 dstport = p['_id']
620 (sport, sdpid) = portV_to_port_dpid(srcport)
621 (dport, ddpid) = portV_to_port_dpid(dstport)
622 link = {}
623 link["src-switch"]=sdpid
624 link["src-port"]=sport
625 link["src-port-state"]=0
626 link["dst-switch"]=ddpid
627 link["dst-port"]=dport
628 link["dst-port-state"]=0
629 link["type"]="internal"
630 links.append(link)
631
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000632# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000633 js = json.dumps(links)
634 resp = Response(js, status=200, mimetype='application/json')
635 return resp
636
Ubuntuc016ba12013-02-27 21:53:41 +0000637@app.route("/controller_status")
638def controller_status():
Tim Lindberg201ade22013-04-05 11:52:08 -0700639# onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
640 onos_check="cd; onos status %s | grep %s | awk '{print $2}'"
Ubuntuc016ba12013-02-27 21:53:41 +0000641 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
642
643 cont_status=[]
644 for i in controllers:
645 status={}
646 onos=os.popen(onos_check % i).read()[:-1]
Tim Lindberg201ade22013-04-05 11:52:08 -0700647 onos=os.popen(onos_check % (i, i.lower())).read()[:-1]
Ubuntuc016ba12013-02-27 21:53:41 +0000648 status["name"]=i
649 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000650 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000651 cont_status.append(status)
652
653 js = json.dumps(cont_status)
654 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000655 return resp
656
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000657### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000658@app.route("/gui/controller/<cmd>/<controller_name>")
659def controller_status_change(cmd, controller_name):
Tim Lindberg201ade22013-04-05 11:52:08 -0700660# start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
661# stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
662 start_onos="cd; onos start %s" % (controller_name[-1:])
663 stop_onos="cd; onos stop %s" % (controller_name[-1:])
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000664
665 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000666 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000667 ret = "controller %s is up" % (controller_name)
668 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000669 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000670 ret = "controller %s is down" % (controller_name)
671
672 return ret
673
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000674@app.route("/gui/switchctrl/<cmd>")
675def switch_controller_setting(cmd):
676 if cmd =="local":
677 print "All aggr switches connects to local controller only"
678 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700679 if (TESTBED == "sw"):
680 for i in range(0, len(controllers)):
681 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-local.sh'" % (controllers[i])
682 result += os.popen(cmd_string).read()
683 else:
684 cmd_string="cd; switch local"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000685 result += os.popen(cmd_string).read()
686 elif cmd =="all":
687 print "All aggr switches connects to all controllers except for core controller"
688 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700689 if (TESTBED == "sw"):
690 for i in range(0, len(controllers)):
691 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i])
692 result += os.popen(cmd_string).read()
693 else:
694 cmd_string="cd; switch all"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000695 result += os.popen(cmd_string).read()
696
697 return result
698
699
700
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000701@app.route("/gui/switch/<cmd>/<dpid>")
702def switch_status_change(cmd, dpid):
Tim Lindberg201ade22013-04-05 11:52:08 -0700703 result = ""
704 if (TESTBED == "hw"):
705 return result
706
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000707 r = re.compile(':')
708 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000709 host=controllers[0]
710 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
711 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000712 print "cmd_string"
713
714 if cmd =="up" or cmd=="down":
715 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000716 os.popen(cmd_string)
717 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000718
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000719 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000720
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000721#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000722#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000723@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
724def link_up(src_dpid, src_port, dst_dpid, dst_port):
Tim Lindberg201ade22013-04-05 11:52:08 -0700725 result = ""
726
727 if (TESTBED == "sw"):
728 result = link_up_sw(src_dpid, src_port, dst_dpid, dst_port)
729 else:
730 result = link_up_hw(src_dpid, src_port, dst_dpid, dst_port)
731 return result
732
733# Link up on software testbed
734def link_up_sw(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000735
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000736 cmd = 'up'
737 result=""
738
Paul Greyson472da4c2013-03-28 11:43:17 -0700739 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000740 if dpid in core_switches:
741 host = controllers[0]
742 src_ports = [1, 2, 3, 4, 5]
743 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000744 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000745 host = controllers[hostid-1]
746 if hostid == 2 :
747 src_ports = [51]
748 else :
749 src_ports = [26]
750
751 for port in src_ports :
752 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
753 print cmd_string
754 res=os.popen(cmd_string).read()
755 result = result + ' ' + res
756
757 return result
758
Tim Lindberg201ade22013-04-05 11:52:08 -0700759# Link up on hardware testbed
760def link_up_hw(src_dpid, src_port, dst_dpid, dst_port):
761
762 port1 = src_port
763 port2 = dst_port
764 if src_dpid == "00:00:00:00:ba:5e:ba:11":
765 if dst_dpid == "00:00:00:08:a2:08:f9:01":
766 port1 = 24
767 port2 = 24
768 elif dst_dpid == "00:01:00:16:97:08:9a:46":
769 port1 = 23
770 port2 = 23
771 elif src_dpid == "00:00:00:00:ba:5e:ba:13":
772 if dst_dpid == "00:00:20:4e:7f:51:8a:35":
773 port1 = 22
774 port2 = 22
775 elif dst_dpid == "00:00:00:00:00:00:ba:12":
776 port1 = 23
777 port2 = 23
778 elif src_dpid == "00:00:00:00:00:00:ba:12":
779 if dst_dpid == "00:00:00:00:ba:5e:ba:13":
780 port1 = 23
781 port2 = 23
782 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
783 port1 = 22
784 port2 = 22
785 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
786 port1 = 24
787 port2 = 21
788 elif src_dpid == "00:01:00:16:97:08:9a:46":
789 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
790 port1 = 23
791 port2 = 23
792 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
793 port1 = 24
794 port2 = 24
795 elif src_dpid == "00:00:00:08:a2:08:f9:01":
796 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
797 port1 = 24
798 port2 = 24
799 elif dst_dpid == "00:00:00:00:00:00:ba:12":
800 port1 = 22
801 port2 = 22
802 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
803 port1 = 23
804 port2 = 23
805 elif src_dpid == "00:00:20:4e:7f:51:8a:35":
806 if dst_dpid == "00:00:00:00:00:00:ba:12":
807 port1 = 21
808 port2 = 24
809 elif dst_dpid == "00:00:00:00:ba:5e:ba:13":
810 port1 = 22
811 port2 = 22
812 elif dst_dpid == "00:01:00:16:97:08:9a:46":
813 port1 = 24
814 port2 = 24
815 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
816 port1 = 23
817 port2 = 23
818
819 cmd = 'up'
820 result=""
821 host = controllers[0]
822 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % (src_dpid, port1, cmd)
823 print cmd_string
824 res=os.popen(cmd_string).read()
825 result = result + ' ' + res
826 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % (dst_dpid, port2, cmd)
827 print cmd_string
828 res=os.popen(cmd_string).read()
829 result = result + ' ' + res
830
831
832 return result
833
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000834
835#* Link Down
836#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000837@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000838def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
839
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000840 if src_dpid in core_switches:
841 host = controllers[0]
842 else:
843 hostid=int(src_dpid.split(':')[-2])
844 host = controllers[hostid-1]
845
Tim Lindberg201ade22013-04-05 11:52:08 -0700846 if (TESTBED == "sw"):
847 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
848 else:
Tim Lindberga6c04172013-04-05 17:34:05 -0700849 if ( src_dpid == "00:00:00:08:a2:08:f9:01" ):
850 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % ( dst_dpid, dst_port, cmd)
851 else:
852 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % ( src_dpid, src_port, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000853 print cmd_string
854
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000855 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000856
857 return result
858
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000859#* Create Flow
860#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000861#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 +0000862@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000863def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
864 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
865 print command
866 ret = os.popen(command).read()
867 if ret == "":
868 flow_nr=0
869 else:
870 flow_nr=int(ret)
871
872 flow_nr += 1
Umesh Krishnaswamyf22d3ed2013-04-03 11:57:54 -0700873 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 +0000874 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 +0000875 print command
876 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000877 errcode1 = os.popen(command1).read()
878 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000879
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000880#* Delete Flow
881#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000882@app.route("/gui/delflow/<flow_id>")
883def del_flow(flow_id):
884 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
885 print command
886 errcode = os.popen(command).read()
887 return errcode
888
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000889#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700890#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000891@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
892def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000893 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000894 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000895 print command
896 result = os.popen(command).read()
897 if len(result) == 0:
898 print "No Flow found"
899 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000900 except:
901 print "REST IF has issue"
902 exit
903
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000904 parsedResult = json.loads(result)
905
906 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000907 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
908 src_port = parsedResult['dataPath']['srcPort']['port']['value']
909 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
910 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000911# 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 +0000912
913 if src_dpid in core_switches:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700914 src_host = controllers[0]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000915 else:
916 hostid=int(src_dpid.split(':')[-2])
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700917 src_host = controllers[hostid-1]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000918
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700919 if dst_dpid in core_switches:
920 dst_host = controllers[0]
921 else:
922 hostid=int(dst_dpid.split(':')[-2])
923 dst_host = controllers[hostid-1]
924
925# /runiperf.sh <flowid> <src_dpid> <dst_dpid> svr|client <proto>/<duration>/<interval>/<samples>
926 protocol="udp"
927 interval=0.1
928 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)
929 print cmd_string
930 os.popen(cmd_string)
931
932 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 +0000933 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000934 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000935
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000936 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000937
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000938#* Get Iperf Throughput
939#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000940@app.route("/gui/iperf/rate/<flow_id>")
941def iperf_rate(flow_id):
Tim Lindberga6c04172013-04-05 17:34:05 -0700942 if (TESTBED == "hw"):
943 return "{}"
944
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000945 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000946 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
947 print command
948 result = os.popen(command).read()
949 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000950 resp = Response(result, status=400, mimetype='text/html')
951 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000952 except:
953 print "REST IF has issue"
954 exit
955
956 parsedResult = json.loads(result)
957
958 flowId = int(parsedResult['flowId']['value'], 16)
959 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
960 src_port = parsedResult['dataPath']['srcPort']['port']['value']
961 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
962 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
963
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700964 if dst_dpid in core_switches:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000965 host = controllers[0]
966 else:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700967 hostid=int(dst_dpid.split(':')[-2])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000968 host = controllers[hostid-1]
969
970 try:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700971 command = "curl -s http://%s:%s/log/iperfsvr_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000972 print command
973 result = os.popen(command).read()
974 except:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000975 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000976
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +0000977 if len(result) == 0:
978 resp = Response(result, status=400, mimetype='text/html')
979 return "no iperf file found (flowid %s)" % flow_id;
980 else:
981 resp = Response(result, status=200, mimetype='application/json')
982 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000983
984
Ubuntu82b8a832013-02-06 22:00:11 +0000985if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000986 random.seed()
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +0000987 read_config()
Ubuntu82b8a832013-02-06 22:00:11 +0000988 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000989# 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")
990# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
991# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
992# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
993# 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 +0000994# print "-- query all switches --"
995# query_switch()
996# print "-- query topo --"
997# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000998# link_change(1,2,3,4)
999 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001000# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +00001001# print "-- query all devices --"
1002# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001003# iperf_start(1,10,15)
1004# iperf_rate(1)
1005 switches()
Ubuntu82b8a832013-02-06 22:00:11 +00001006 else:
1007 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +00001008 app.run(threaded=True, host="0.0.0.0", port=9000)