blob: 4490cf60962369906c17db4989778ab82b2ef285 [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
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000016## Global Var for ON.Lab local REST ##
Ubuntuf6ce96c2013-02-07 01:45:07 +000017RestIP="localhost"
Ubuntu82b8a832013-02-06 22:00:11 +000018RestPort=8080
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000019
Tim Lindberga6c04172013-04-05 17:34:05 -070020LB=False #; True or False
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000021ONOS_DEFAULT_HOST="localhost" ;# Has to set if LB=False
Tim Lindberga6c04172013-04-05 17:34:05 -070022TESTBED="hw"
Ubuntu82b8a832013-02-06 22:00:11 +000023DEBUG=1
Ubuntu82b8a832013-02-06 22:00:11 +000024
Tim Lindberg201ade22013-04-05 11:52:08 -070025if (TESTBED == "hw"):
26 # Settings for running on hardware testbed
27 controllers=["ONOS1", "ONOS2", "ONOS3", "ONOS4", "ONOS5", "ONOS6", "ONOS7", "ONOS8"]
28 core_switches=["00:00:00:00:ba:5e:ba:11", "00:00:00:00:00:00:ba:12", "00:00:20:4e:7f:51:8a:35", "00:00:00:00:ba:5e:ba:13", "00:00:00:08:a2:08:f9:01", "00:01:00:16:97:08:9a:46"]
29 ONOS_GUI3_HOST="http://10.128.4.11:9000"
30 ONOS_GUI3_CONTROL_HOST="http://10.128.4.11:9000"
31else:
32 # Settings for running on software testbed
33 controllers=["onosdevt1", "onosdevt2", "onosdevt3", "onosdevt4", "onosdevt5", "onosdevt6", "onosdevt7", "onosdevt8"]
34# core_switches=["00:00:00:00:ba:5e:ba:11", "00:00:00:00:00:00:ba:12", "00:00:20:4e:7f:51:8a:35", "00:00:00:00:ba:5e:ba:13", "00:00:00:08:a2:08:f9:01", "00:00:00:16:97:08:9a:46"]
35 core_switches=["00:00:00:00:00:00:01:01", "00:00:00:00:00:00:01:02", "00:00:00:00:00:00:01:03", "00:00:00:00:00:00:01:04", "00:00:00:00:00:00:01:05", "00:00:00:00:00:00:01:06"]
36 ONOS_GUI3_HOST="http://devt-gui.onlab.us:8080"
37 ONOS_GUI3_CONTROL_HOST="http://devt-gui.onlab.us:8080"
38
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000039pp = pprint.PrettyPrinter(indent=4)
Ubuntu82b8a832013-02-06 22:00:11 +000040app = Flask(__name__)
41
42## Worker Functions ##
43def log_error(txt):
44 print '%s' % (txt)
45
46def debug(txt):
47 if DEBUG:
48 print '%s' % (txt)
49
Ubuntu82b8a832013-02-06 22:00:11 +000050### File Fetch ###
51@app.route('/ui/img/<filename>', methods=['GET'])
52@app.route('/img/<filename>', methods=['GET'])
53@app.route('/css/<filename>', methods=['GET'])
54@app.route('/js/models/<filename>', methods=['GET'])
55@app.route('/js/views/<filename>', methods=['GET'])
56@app.route('/js/<filename>', methods=['GET'])
57@app.route('/lib/<filename>', methods=['GET'])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +000058@app.route('/log/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000059@app.route('/', methods=['GET'])
60@app.route('/<filename>', methods=['GET'])
61@app.route('/tpl/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000062@app.route('/ons-demo/<filename>', methods=['GET'])
63@app.route('/ons-demo/js/<filename>', methods=['GET'])
64@app.route('/ons-demo/css/<filename>', methods=['GET'])
65@app.route('/ons-demo/assets/<filename>', methods=['GET'])
66@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000067def return_file(filename="index.html"):
68 if request.path == "/":
69 fullpath = "./index.html"
70 else:
71 fullpath = str(request.path)[1:]
72
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +000073 try:
74 open(fullpath)
75 except:
76 response = make_response("Cannot find a file: %s" % (fullpath), 500)
77 response.headers["Content-type"] = "text/html"
78 return response
79
Ubuntu82b8a832013-02-06 22:00:11 +000080 response = make_response(open(fullpath).read())
81 suffix = fullpath.split(".")[-1]
82
83 if suffix == "html" or suffix == "htm":
84 response.headers["Content-type"] = "text/html"
85 elif suffix == "js":
86 response.headers["Content-type"] = "application/javascript"
87 elif suffix == "css":
88 response.headers["Content-type"] = "text/css"
89 elif suffix == "png":
90 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -070091 elif suffix == "svg":
92 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000093
94 return response
95
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000096## Proxy ##
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070097@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
98def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
99 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -0700100 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 -0700101 print command
102 result = os.popen(command).read()
103 except:
104 print "REST IF has issue"
105 exit
106
107 resp = Response(result, status=200, mimetype='application/json')
108 return resp
109
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000110@app.route("/proxy/gui/switchctrl/<cmd>")
111def proxy_switch_controller_setting(cmd):
112 try:
113 command = "curl -s %s/gui/switchctrl/%s" % (ONOS_GUI3_CONTROL_HOST, cmd)
114 print command
115 result = os.popen(command).read()
116 except:
117 print "REST IF has issue"
118 exit
119
120 resp = Response(result, status=200, mimetype='application/json')
121 return resp
122
Paul Greyson8d1c6362013-03-27 13:05:24 -0700123@app.route("/proxy/gui/switch/<cmd>/<dpid>")
124def proxy_switch_status_change(cmd, dpid):
125 try:
126 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
127 print command
128 result = os.popen(command).read()
129 except:
130 print "REST IF has issue"
131 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700132
Paul Greyson8d1c6362013-03-27 13:05:24 -0700133 resp = Response(result, status=200, mimetype='application/json')
134 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700135
Paul Greyson2913af82013-03-27 14:53:17 -0700136@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
137def proxy_controller_status_change(cmd, controller_name):
138 try:
139 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
140 print command
141 result = os.popen(command).read()
142 except:
143 print "REST IF has issue"
144 exit
145
146 resp = Response(result, status=200, mimetype='application/json')
147 return resp
148
Paul Greyson472da4c2013-03-28 11:43:17 -0700149@app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
150def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
151 try:
152 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)
153 print command
154 result = os.popen(command).read()
155 except:
156 print "REST IF has issue"
157 exit
158
159 resp = Response(result, status=200, mimetype='application/json')
160 return resp
161
Paul Greyson6f918402013-03-28 12:18:30 -0700162@app.route("/proxy/gui/delflow/<flow_id>")
163def proxy_del_flow(flow_id):
164 try:
165 command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
166 print command
167 result = os.popen(command).read()
168 except:
169 print "REST IF has issue"
170 exit
171
172 resp = Response(result, status=200, mimetype='application/json')
173 return resp
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000174
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700175@app.route("/proxy/gui/iperf/start/<flow_id>/<duration>/<samples>")
176def proxy_iperf_start(flow_id,duration,samples):
177 try:
178 command = "curl -s %s/gui/iperf/start/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id, duration, samples)
179 print command
180 result = os.popen(command).read()
181 except:
182 print "REST IF has issue"
183 exit
184
185 resp = Response(result, status=200, mimetype='application/json')
186 return resp
187
188@app.route("/proxy/gui/iperf/rate/<flow_id>")
189def proxy_iperf_rate(flow_id):
190 try:
191 command = "curl -s %s/gui/iperf/rate/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
192 print command
193 result = os.popen(command).read()
194 except:
195 print "REST IF has issue"
196 exit
197
198 resp = Response(result, status=200, mimetype='application/json')
199 return resp
200
201
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000202###### ONOS RESET API ##############################
203## Worker Func ###
204def get_json(url):
205 code = 200
206 try:
207 command = "curl -s %s" % (url)
208 result = os.popen(command).read()
209 parsedResult = json.loads(result)
Masayoshi Kobayashi8ac33362013-04-05 03:17:26 +0000210 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
211 print "REST %s returned code %s" % (command, parsedResult['code'])
212 code=500
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000213 except:
214 print "REST IF %s has issue" % command
215 result = ""
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000216 code = 500
217
218 return (code, result)
219
220def pick_host():
221 if LB == True:
222 nr_host=len(controllers)
223 r=random.randint(0, nr_host - 1)
224 host=controllers[r]
225 else:
226 host=ONOS_DEFAULT_HOST
227
228 return "http://" + host + ":8080"
229
230## Switch ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000231@app.route("/wm/core/topology/switches/all/json")
232def switches():
233 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000234 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000235 else:
236 host = ONOS_GUI3_HOST
237
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000238 url ="%s/wm/core/topology/switches/all/json" % (host)
239 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000240
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000241 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000242 return resp
243
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000244## Link ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000245@app.route("/wm/core/topology/links/json")
246def links():
247 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000248 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000249 else:
250 host = ONOS_GUI3_HOST
251
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000252 url ="%s/wm/core/topology/links/json" % (host)
253 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000254
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000255 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000256 return resp
257
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000258## FlowSummary ##
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000259@app.route("/wm/flow/getsummary/<start>/<range>/json")
260def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000261 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000262 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000263 else:
264 host = ONOS_GUI3_HOST
265
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000266 url ="%s/wm/flow/getsummary/%s/%s/json" % (host, start, range)
267 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000268
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000269 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000270 return resp
271
272@app.route("/wm/registry/controllers/json")
273def registry_controllers():
274 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000275 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000276 else:
277 host = ONOS_GUI3_HOST
278
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000279 url= "%s/wm/registry/controllers/json" % (host)
280 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000281
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000282 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000283 return resp
284
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000285
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000286@app.route("/wm/registry/switches/json")
287def registry_switches():
288 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000289 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000290 else:
291 host = ONOS_GUI3_HOST
292
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000293 url="%s/wm/registry/switches/json" % (host)
294 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000295
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000296 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000297 return resp
298
Ubuntu82b8a832013-02-06 22:00:11 +0000299def node_id(switch_array, dpid):
300 id = -1
301 for i, val in enumerate(switch_array):
302 if val['name'] == dpid:
303 id = i
304 break
305
306 return id
307
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000308## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000309@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000310def topology_for_gui():
311 try:
312 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
313 result = os.popen(command).read()
314 parsedResult = json.loads(result)
315 except:
316 log_error("REST IF has issue: %s" % command)
317 log_error("%s" % result)
318 sys.exit(0)
319
320 topo = {}
321 switches = []
322 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000323 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000324
325 for v in parsedResult:
326 if v.has_key('dpid'):
327# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
328 dpid = str(v['dpid'])
329 state = str(v['state'])
330 sw = {}
331 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000332 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000333
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000334 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000335 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000336 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000337
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000338 try:
339 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
340 result = os.popen(command).read()
341 parsedResult = json.loads(result)
342 except:
343 log_error("REST IF has issue: %s" % command)
344 log_error("%s" % result)
345
346 for key in parsedResult:
347 dpid = key
348 ctrl = parsedResult[dpid][0]['controllerId']
349 sw_id = node_id(switches, dpid)
350 if sw_id != -1:
351 if switches[sw_id]['group'] != 0:
352 switches[sw_id]['group'] = controllers.index(ctrl) + 1
353
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000354 try:
355 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000356# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000357 p1=1
358 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000359# v2 = "00:00:00:00:00:0d:00:d3"
360 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000361 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
362 result = os.popen(command).read()
363 parsedResult = json.loads(result)
364 except:
365 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000366 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000367
Ubuntu765deff2013-02-28 18:39:13 +0000368 path = []
369 if parsedResult.has_key('flowEntries'):
370 flowEntries= parsedResult['flowEntries']
371 for i, v in enumerate(flowEntries):
372 if i < len(flowEntries) - 1:
373 sdpid= flowEntries[i]['dpid']['value']
374 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700375 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000376
Ubuntu82b8a832013-02-06 22:00:11 +0000377 try:
378 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
379 result = os.popen(command).read()
380 parsedResult = json.loads(result)
381 except:
382 log_error("REST IF has issue: %s" % command)
383 log_error("%s" % result)
384 sys.exit(0)
385
386 for v in parsedResult:
387 link = {}
388 if v.has_key('dst-switch'):
389 dst_dpid = str(v['dst-switch'])
390 dst_id = node_id(switches, dst_dpid)
391 if v.has_key('src-switch'):
392 src_dpid = str(v['src-switch'])
393 src_id = node_id(switches, src_dpid)
394 link['source'] = src_id
395 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000396
397 onpath = 0
398 for (s,d) in path:
399 if s == v['src-switch'] and d == v['dst-switch']:
400 onpath = 1
401 break
402 link['type'] = onpath
403
Ubuntu82b8a832013-02-06 22:00:11 +0000404 links.append(link)
405
406 topo['nodes'] = switches
407 topo['links'] = links
408
Ubuntu82b8a832013-02-06 22:00:11 +0000409 js = json.dumps(topo)
410 resp = Response(js, status=200, mimetype='application/json')
411 return resp
412
Ubuntuaea2a682013-02-08 08:30:10 +0000413#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
414#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
415@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
416def shortest_path(v1, p1, v2, p2):
417 try:
418 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
419 result = os.popen(command).read()
420 parsedResult = json.loads(result)
421 except:
422 log_error("REST IF has issue: %s" % command)
423 log_error("%s" % result)
424 sys.exit(0)
425
426 topo = {}
427 switches = []
428 links = []
429
430 for v in parsedResult:
431 if v.has_key('dpid'):
432 dpid = str(v['dpid'])
433 state = str(v['state'])
434 sw = {}
435 sw['name']=dpid
436 if str(v['state']) == "ACTIVE":
437 if dpid[-2:-1] == "a":
438 sw['group']=1
439 if dpid[-2:-1] == "b":
440 sw['group']=2
441 if dpid[-2:-1] == "c":
442 sw['group']=3
443 if str(v['state']) == "INACTIVE":
444 sw['group']=0
445
446 switches.append(sw)
447
448 try:
449 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
450 result = os.popen(command).read()
451 parsedResult = json.loads(result)
452 except:
453 log_error("No route")
454 parsedResult = []
455# exit(1)
456
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700457 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000458 for i, v in enumerate(parsedResult):
459 if i < len(parsedResult) - 1:
460 sdpid= parsedResult[i]['switch']
461 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700462 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000463
464 try:
465 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
466 result = os.popen(command).read()
467 parsedResult = json.loads(result)
468 except:
469 log_error("REST IF has issue: %s" % command)
470 log_error("%s" % result)
471 sys.exit(0)
472
473 for v in parsedResult:
474 link = {}
475 if v.has_key('dst-switch'):
476 dst_dpid = str(v['dst-switch'])
477 dst_id = node_id(switches, dst_dpid)
478 if v.has_key('src-switch'):
479 src_dpid = str(v['src-switch'])
480 src_id = node_id(switches, src_dpid)
481 link['source'] = src_id
482 link['target'] = dst_id
483 onpath = 0
484 for (s,d) in path:
485 if s == v['src-switch'] and d == v['dst-switch']:
486 onpath = 1
487 break
488
489 link['type'] = onpath
490 links.append(link)
491
492 topo['nodes'] = switches
493 topo['links'] = links
494
Ubuntuaea2a682013-02-08 08:30:10 +0000495 js = json.dumps(topo)
496 resp = Response(js, status=200, mimetype='application/json')
497 return resp
498
Ubuntu82b8a832013-02-06 22:00:11 +0000499@app.route("/wm/core/controller/switches/json")
500def query_switch():
501 try:
502 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
503# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000504 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000505 result = os.popen(command).read()
506 parsedResult = json.loads(result)
507 except:
508 log_error("REST IF has issue: %s" % command)
509 log_error("%s" % result)
510 sys.exit(0)
511
512# print command
513# print result
514 switches_ = []
515 for v in parsedResult:
516 if v.has_key('dpid'):
517 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
518 dpid = str(v['dpid'])
519 state = str(v['state'])
520 sw = {}
521 sw['dpid']=dpid
522 sw['active']=state
523 switches_.append(sw)
524
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000525# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000526 js = json.dumps(switches_)
527 resp = Response(js, status=200, mimetype='application/json')
528 return resp
529
530@app.route("/wm/device/")
531def devices():
532 try:
533 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
534 result = os.popen(command).read()
535 parsedResult = json.loads(result)['results']
536 except:
537 log_error("REST IF has issue: %s" % command)
538 log_error("%s" % result)
539 sys.exit(0)
540
541 devices = []
542 for v in parsedResult:
543 dl_addr = v['dl_addr']
544 nw_addr = v['nw_addr']
545 vertex = v['_id']
546 mac = []
547 mac.append(dl_addr)
548 ip = []
549 ip.append(nw_addr)
550 device = {}
551 device['entryClass']="DefaultEntryClass"
552 device['mac']=mac
553 device['ipv4']=ip
554 device['vlan']=[]
555 device['lastSeen']=0
556 attachpoints =[]
557
558 port, dpid = deviceV_to_attachpoint(vertex)
559 attachpoint = {}
560 attachpoint['port']=port
561 attachpoint['switchDPID']=dpid
562 attachpoints.append(attachpoint)
563 device['attachmentPoint']=attachpoints
564 devices.append(device)
565
Ubuntu82b8a832013-02-06 22:00:11 +0000566 js = json.dumps(devices)
567 resp = Response(js, status=200, mimetype='application/json')
568 return resp
569
570#{"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}
571
Ubuntu82b8a832013-02-06 22:00:11 +0000572## return fake stat for now
573@app.route("/wm/core/switch/<switchId>/<statType>/json")
574def switch_stat(switchId, statType):
575 if statType == "desc":
576 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
577 ret = {}
578 ret[switchId]=desc
579 elif statType == "aggregate":
580 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
581 ret = {}
582 ret[switchId]=aggr
583 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700584 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000585
586 js = json.dumps(ret)
587 resp = Response(js, status=200, mimetype='application/json')
588 return resp
589
590
591@app.route("/wm/topology/links/json")
592def query_links():
593 try:
594 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000595 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000596 result = os.popen(command).read()
597 parsedResult = json.loads(result)['results']
598 except:
599 log_error("REST IF has issue: %s" % command)
600 log_error("%s" % result)
601 sys.exit(0)
602
603 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000604# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000605 sport = []
606 links = []
607 for v in parsedResult:
608 srcport = v['_id']
609 try:
610 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
611 print command
612 result = os.popen(command).read()
613 linkResults = json.loads(result)['results']
614 except:
615 log_error("REST IF has issue: %s" % command)
616 log_error("%s" % result)
617 sys.exit(0)
618
619 for p in linkResults:
620 if p.has_key('type') and p['type'] == "port":
621 dstport = p['_id']
622 (sport, sdpid) = portV_to_port_dpid(srcport)
623 (dport, ddpid) = portV_to_port_dpid(dstport)
624 link = {}
625 link["src-switch"]=sdpid
626 link["src-port"]=sport
627 link["src-port-state"]=0
628 link["dst-switch"]=ddpid
629 link["dst-port"]=dport
630 link["dst-port-state"]=0
631 link["type"]="internal"
632 links.append(link)
633
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000634# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000635 js = json.dumps(links)
636 resp = Response(js, status=200, mimetype='application/json')
637 return resp
638
Ubuntuc016ba12013-02-27 21:53:41 +0000639@app.route("/controller_status")
640def controller_status():
Tim Lindberg201ade22013-04-05 11:52:08 -0700641# onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
642 onos_check="cd; onos status %s | grep %s | awk '{print $2}'"
Ubuntuc016ba12013-02-27 21:53:41 +0000643 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
644
645 cont_status=[]
646 for i in controllers:
647 status={}
648 onos=os.popen(onos_check % i).read()[:-1]
Tim Lindberg201ade22013-04-05 11:52:08 -0700649 onos=os.popen(onos_check % (i, i.lower())).read()[:-1]
Ubuntuc016ba12013-02-27 21:53:41 +0000650 status["name"]=i
651 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000652 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000653 cont_status.append(status)
654
655 js = json.dumps(cont_status)
656 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000657 return resp
658
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000659### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000660@app.route("/gui/controller/<cmd>/<controller_name>")
661def controller_status_change(cmd, controller_name):
Tim Lindberg201ade22013-04-05 11:52:08 -0700662# start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
663# stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
664 start_onos="cd; onos start %s" % (controller_name[-1:])
665 stop_onos="cd; onos stop %s" % (controller_name[-1:])
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
Umesh Krishnaswamyf22d3ed2013-04-03 11:57:54 -0700875 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 +0000876 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 +0000877 print command
878 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000879 errcode1 = os.popen(command1).read()
880 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000881
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000882#* Delete Flow
883#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000884@app.route("/gui/delflow/<flow_id>")
885def del_flow(flow_id):
886 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
887 print command
888 errcode = os.popen(command).read()
889 return errcode
890
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000891#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700892#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000893@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
894def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000895 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000896 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000897 print command
898 result = os.popen(command).read()
899 if len(result) == 0:
900 print "No Flow found"
901 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000902 except:
903 print "REST IF has issue"
904 exit
905
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000906 parsedResult = json.loads(result)
907
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000908# flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000909 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:
917 host = controllers[0]
918 else:
919 hostid=int(src_dpid.split(':')[-2])
920 host = controllers[hostid-1]
921
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000922# ./runiperf.sh 2 00:00:00:00:00:00:02:02 1 00:00:00:00:00:00:03:02 1 100 15
923 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./runiperf.sh %d %s %s %s %s %s %s'" % (host, flowId, src_dpid, src_port, dst_dpid, dst_port, duration, samples)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000924 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000925 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000926
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000927 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000928
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000929#* Get Iperf Throughput
930#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000931@app.route("/gui/iperf/rate/<flow_id>")
932def iperf_rate(flow_id):
Tim Lindberga6c04172013-04-05 17:34:05 -0700933 if (TESTBED == "hw"):
934 return "{}"
935
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000936 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000937 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
938 print command
939 result = os.popen(command).read()
940 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000941 resp = Response(result, status=400, mimetype='text/html')
942 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000943 except:
944 print "REST IF has issue"
945 exit
946
947 parsedResult = json.loads(result)
948
949 flowId = int(parsedResult['flowId']['value'], 16)
950 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
951 src_port = parsedResult['dataPath']['srcPort']['port']['value']
952 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
953 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
954
955 if src_dpid in core_switches:
956 host = controllers[0]
957 else:
958 hostid=int(src_dpid.split(':')[-2])
959 host = controllers[hostid-1]
960
961 try:
962 command = "curl -s http://%s:%s/log/iperf_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000963 print command
964 result = os.popen(command).read()
965 except:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000966 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000967
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +0000968 if len(result) == 0:
969 resp = Response(result, status=400, mimetype='text/html')
970 return "no iperf file found (flowid %s)" % flow_id;
971 else:
972 resp = Response(result, status=200, mimetype='application/json')
973 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000974
975
Ubuntu82b8a832013-02-06 22:00:11 +0000976if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000977 random.seed()
Ubuntu82b8a832013-02-06 22:00:11 +0000978 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000979# 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")
980# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
981# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
982# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
983# 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 +0000984# print "-- query all switches --"
985# query_switch()
986# print "-- query topo --"
987# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000988# link_change(1,2,3,4)
989 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000990# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000991# print "-- query all devices --"
992# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000993# iperf_start(1,10,15)
994# iperf_rate(1)
995 switches()
Ubuntu82b8a832013-02-06 22:00:11 +0000996 else:
997 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000998 app.run(threaded=True, host="0.0.0.0", port=9000)