blob: ec6f89be49e3122b53ff5dee163160a5609745a0 [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):
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000660 if (TESTBED == "hw"):
661 start_onos="cd; onos start %s" % (controller_name[-1:])
662 stop_onos="cd; onos stop %s" % (controller_name[-1:])
663 else:
664 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
665 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000666
667 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000668 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000669 ret = "controller %s is up" % (controller_name)
670 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000671 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000672 ret = "controller %s is down" % (controller_name)
673
674 return ret
675
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000676@app.route("/gui/switchctrl/<cmd>")
677def switch_controller_setting(cmd):
678 if cmd =="local":
679 print "All aggr switches connects to local controller only"
680 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700681 if (TESTBED == "sw"):
682 for i in range(0, len(controllers)):
683 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-local.sh'" % (controllers[i])
684 result += os.popen(cmd_string).read()
685 else:
686 cmd_string="cd; switch local"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000687 result += os.popen(cmd_string).read()
688 elif cmd =="all":
689 print "All aggr switches connects to all controllers except for core controller"
690 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700691 if (TESTBED == "sw"):
692 for i in range(0, len(controllers)):
693 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i])
694 result += os.popen(cmd_string).read()
695 else:
696 cmd_string="cd; switch all"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000697 result += os.popen(cmd_string).read()
698
699 return result
700
701
702
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000703@app.route("/gui/switch/<cmd>/<dpid>")
704def switch_status_change(cmd, dpid):
Tim Lindberg201ade22013-04-05 11:52:08 -0700705 result = ""
706 if (TESTBED == "hw"):
707 return result
708
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000709 r = re.compile(':')
710 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000711 host=controllers[0]
712 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
713 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000714 print "cmd_string"
715
716 if cmd =="up" or cmd=="down":
717 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000718 os.popen(cmd_string)
719 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000720
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000721 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000722
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000723#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000724#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000725@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
726def link_up(src_dpid, src_port, dst_dpid, dst_port):
Tim Lindberg201ade22013-04-05 11:52:08 -0700727 result = ""
728
729 if (TESTBED == "sw"):
730 result = link_up_sw(src_dpid, src_port, dst_dpid, dst_port)
731 else:
732 result = link_up_hw(src_dpid, src_port, dst_dpid, dst_port)
733 return result
734
735# Link up on software testbed
736def link_up_sw(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000737
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000738 cmd = 'up'
739 result=""
740
Paul Greyson472da4c2013-03-28 11:43:17 -0700741 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000742 if dpid in core_switches:
743 host = controllers[0]
744 src_ports = [1, 2, 3, 4, 5]
745 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000746 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000747 host = controllers[hostid-1]
748 if hostid == 2 :
749 src_ports = [51]
750 else :
751 src_ports = [26]
752
753 for port in src_ports :
754 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
755 print cmd_string
756 res=os.popen(cmd_string).read()
757 result = result + ' ' + res
758
759 return result
760
Tim Lindberg201ade22013-04-05 11:52:08 -0700761# Link up on hardware testbed
762def link_up_hw(src_dpid, src_port, dst_dpid, dst_port):
763
764 port1 = src_port
765 port2 = dst_port
766 if src_dpid == "00:00:00:00:ba:5e:ba:11":
767 if dst_dpid == "00:00:00:08:a2:08:f9:01":
768 port1 = 24
769 port2 = 24
770 elif dst_dpid == "00:01:00:16:97:08:9a:46":
771 port1 = 23
772 port2 = 23
773 elif src_dpid == "00:00:00:00:ba:5e:ba:13":
774 if dst_dpid == "00:00:20:4e:7f:51:8a:35":
775 port1 = 22
776 port2 = 22
777 elif dst_dpid == "00:00:00:00:00:00:ba:12":
778 port1 = 23
779 port2 = 23
780 elif src_dpid == "00:00:00:00:00:00:ba:12":
781 if dst_dpid == "00:00:00:00:ba:5e:ba:13":
782 port1 = 23
783 port2 = 23
784 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
785 port1 = 22
786 port2 = 22
787 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
788 port1 = 24
789 port2 = 21
790 elif src_dpid == "00:01:00:16:97:08:9a:46":
791 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
792 port1 = 23
793 port2 = 23
794 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
795 port1 = 24
796 port2 = 24
797 elif src_dpid == "00:00:00:08:a2:08:f9:01":
798 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
799 port1 = 24
800 port2 = 24
801 elif dst_dpid == "00:00:00:00:00:00:ba:12":
802 port1 = 22
803 port2 = 22
804 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
805 port1 = 23
806 port2 = 23
807 elif src_dpid == "00:00:20:4e:7f:51:8a:35":
808 if dst_dpid == "00:00:00:00:00:00:ba:12":
809 port1 = 21
810 port2 = 24
811 elif dst_dpid == "00:00:00:00:ba:5e:ba:13":
812 port1 = 22
813 port2 = 22
814 elif dst_dpid == "00:01:00:16:97:08:9a:46":
815 port1 = 24
816 port2 = 24
817 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
818 port1 = 23
819 port2 = 23
820
821 cmd = 'up'
822 result=""
823 host = controllers[0]
824 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % (src_dpid, port1, cmd)
825 print cmd_string
826 res=os.popen(cmd_string).read()
827 result = result + ' ' + res
828 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % (dst_dpid, port2, cmd)
829 print cmd_string
830 res=os.popen(cmd_string).read()
831 result = result + ' ' + res
832
833
834 return result
835
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000836
837#* Link Down
838#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000839@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000840def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
841
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000842 if src_dpid in core_switches:
843 host = controllers[0]
844 else:
845 hostid=int(src_dpid.split(':')[-2])
846 host = controllers[hostid-1]
847
Tim Lindberg201ade22013-04-05 11:52:08 -0700848 if (TESTBED == "sw"):
849 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
850 else:
Tim Lindberga6c04172013-04-05 17:34:05 -0700851 if ( src_dpid == "00:00:00:08:a2:08:f9:01" ):
852 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % ( dst_dpid, dst_port, cmd)
853 else:
854 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % ( src_dpid, src_port, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000855 print cmd_string
856
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000857 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000858
859 return result
860
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000861#* Create Flow
862#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000863#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 +0000864@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000865def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
866 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
867 print command
868 ret = os.popen(command).read()
869 if ret == "":
870 flow_nr=0
871 else:
872 flow_nr=int(ret)
873
874 flow_nr += 1
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000875 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)
876 flow_nr += 1
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000877 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 +0000878 print command
879 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000880 errcode1 = os.popen(command1).read()
881 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000882
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000883#* Delete Flow
884#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000885@app.route("/gui/delflow/<flow_id>")
886def del_flow(flow_id):
887 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
888 print command
889 errcode = os.popen(command).read()
890 return errcode
891
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000892#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700893#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000894@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
895def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000896 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000897 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000898 print command
899 result = os.popen(command).read()
900 if len(result) == 0:
901 print "No Flow found"
902 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000903 except:
904 print "REST IF has issue"
905 exit
906
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000907 parsedResult = json.loads(result)
908
909 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000910 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
911 src_port = parsedResult['dataPath']['srcPort']['port']['value']
912 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
913 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000914# 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 +0000915
916 if src_dpid in core_switches:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700917 src_host = controllers[0]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000918 else:
919 hostid=int(src_dpid.split(':')[-2])
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700920 src_host = controllers[hostid-1]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000921
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700922 if dst_dpid in core_switches:
923 dst_host = controllers[0]
924 else:
925 hostid=int(dst_dpid.split(':')[-2])
926 dst_host = controllers[hostid-1]
927
928# /runiperf.sh <flowid> <src_dpid> <dst_dpid> svr|client <proto>/<duration>/<interval>/<samples>
929 protocol="udp"
930 interval=0.1
931 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)
932 print cmd_string
933 os.popen(cmd_string)
934
935 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 +0000936 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000937 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000938
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000939 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000940
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000941#* Get Iperf Throughput
942#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000943@app.route("/gui/iperf/rate/<flow_id>")
944def iperf_rate(flow_id):
Tim Lindberga6c04172013-04-05 17:34:05 -0700945 if (TESTBED == "hw"):
946 return "{}"
947
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000948 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000949 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
950 print command
951 result = os.popen(command).read()
952 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000953 resp = Response(result, status=400, mimetype='text/html')
954 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000955 except:
956 print "REST IF has issue"
957 exit
958
959 parsedResult = json.loads(result)
960
961 flowId = int(parsedResult['flowId']['value'], 16)
962 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
963 src_port = parsedResult['dataPath']['srcPort']['port']['value']
964 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
965 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
966
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700967 if dst_dpid in core_switches:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000968 host = controllers[0]
969 else:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700970 hostid=int(dst_dpid.split(':')[-2])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000971 host = controllers[hostid-1]
972
973 try:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700974 command = "curl -s http://%s:%s/log/iperfsvr_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000975 print command
976 result = os.popen(command).read()
977 except:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000978 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000979
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +0000980 if len(result) == 0:
981 resp = Response(result, status=400, mimetype='text/html')
982 return "no iperf file found (flowid %s)" % flow_id;
983 else:
984 resp = Response(result, status=200, mimetype='application/json')
985 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000986
987
Ubuntu82b8a832013-02-06 22:00:11 +0000988if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000989 random.seed()
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +0000990 read_config()
Ubuntu82b8a832013-02-06 22:00:11 +0000991 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000992# 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")
993# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
994# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
995# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
996# 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 +0000997# print "-- query all switches --"
998# query_switch()
999# print "-- query topo --"
1000# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001001# link_change(1,2,3,4)
1002 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001003# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +00001004# print "-- query all devices --"
1005# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001006# iperf_start(1,10,15)
1007# iperf_rate(1)
1008 switches()
Ubuntu82b8a832013-02-06 22:00:11 +00001009 else:
1010 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +00001011 app.run(threaded=True, host="0.0.0.0", port=9000)