blob: 06e47580b52a5c698b927b4580c716ff049a40e2 [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"
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +000018LINK_FILE=os.getenv("HOME") + "/ONOS/web/link.json"
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000019
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000020## Global Var for ON.Lab local REST ##
Ubuntuf6ce96c2013-02-07 01:45:07 +000021RestIP="localhost"
Ubuntu82b8a832013-02-06 22:00:11 +000022RestPort=8080
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000023ONOS_DEFAULT_HOST="localhost" ;# Has to set if LB=False
Ubuntu82b8a832013-02-06 22:00:11 +000024DEBUG=1
Ubuntu82b8a832013-02-06 22:00:11 +000025
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +000026pp = pprint.PrettyPrinter(indent=4)
27app = Flask(__name__)
28
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000029def read_config():
Jonathan Hartb2554482013-04-08 13:40:31 -070030 global LB, TESTBED, controllers, core_switches, ONOS_GUI3_HOST, ONOS_GUI3_CONTROL_HOST
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000031 f = open(CONFIG_FILE)
32 conf = json.load(f)
33 LB = conf['LB']
Jonathan Hartb2554482013-04-08 13:40:31 -070034 TESTBED = conf['TESTBED']
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000035 controllers = conf['controllers']
Jonathan Hartb2554482013-04-08 13:40:31 -070036 core_switches=conf['core_switches']
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000037 ONOS_GUI3_HOST=conf['ONOS_GUI3_HOST']
38 ONOS_GUI3_CONTROL_HOST=conf['ONOS_GUI3_CONTROL_HOST']
39 f.close()
Tim Lindberg201ade22013-04-05 11:52:08 -070040
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +000041def read_link_def():
42 global link_def
43 f=open(LINK_FILE)
44 try:
45 link_def=json.load(f)
46 f.close()
47 except:
48 print "Can't read link def file (link.json)"
49 sys.exit(1)
50
51def get_link_ports(src_dpid, dst_dpid):
52 ret = (-1, -1)
53 for link in link_def:
54 if link['src-switch'] == src_dpid and link['dst-switch'] == dst_dpid:
55 ret = (link['src-port'], link['dst-port'])
56 break
57 return ret
Ubuntu82b8a832013-02-06 22:00:11 +000058
59## Worker Functions ##
60def log_error(txt):
61 print '%s' % (txt)
62
63def debug(txt):
64 if DEBUG:
65 print '%s' % (txt)
66
Ubuntu82b8a832013-02-06 22:00:11 +000067### File Fetch ###
68@app.route('/ui/img/<filename>', methods=['GET'])
69@app.route('/img/<filename>', methods=['GET'])
70@app.route('/css/<filename>', methods=['GET'])
71@app.route('/js/models/<filename>', methods=['GET'])
72@app.route('/js/views/<filename>', methods=['GET'])
73@app.route('/js/<filename>', methods=['GET'])
74@app.route('/lib/<filename>', methods=['GET'])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +000075@app.route('/log/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000076@app.route('/', methods=['GET'])
77@app.route('/<filename>', methods=['GET'])
78@app.route('/tpl/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000079@app.route('/ons-demo/<filename>', methods=['GET'])
80@app.route('/ons-demo/js/<filename>', methods=['GET'])
81@app.route('/ons-demo/css/<filename>', methods=['GET'])
82@app.route('/ons-demo/assets/<filename>', methods=['GET'])
83@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000084def return_file(filename="index.html"):
85 if request.path == "/":
86 fullpath = "./index.html"
87 else:
88 fullpath = str(request.path)[1:]
89
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +000090 try:
91 open(fullpath)
92 except:
93 response = make_response("Cannot find a file: %s" % (fullpath), 500)
94 response.headers["Content-type"] = "text/html"
95 return response
96
Ubuntu82b8a832013-02-06 22:00:11 +000097 response = make_response(open(fullpath).read())
98 suffix = fullpath.split(".")[-1]
99
100 if suffix == "html" or suffix == "htm":
101 response.headers["Content-type"] = "text/html"
102 elif suffix == "js":
103 response.headers["Content-type"] = "application/javascript"
104 elif suffix == "css":
105 response.headers["Content-type"] = "text/css"
106 elif suffix == "png":
107 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -0700108 elif suffix == "svg":
109 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +0000110
111 return response
112
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000113## Proxy ##
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700114@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
115def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
116 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -0700117 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 -0700118 print command
119 result = os.popen(command).read()
120 except:
121 print "REST IF has issue"
122 exit
123
124 resp = Response(result, status=200, mimetype='application/json')
125 return resp
126
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000127@app.route("/proxy/gui/switchctrl/<cmd>")
128def proxy_switch_controller_setting(cmd):
129 try:
130 command = "curl -s %s/gui/switchctrl/%s" % (ONOS_GUI3_CONTROL_HOST, cmd)
131 print command
132 result = os.popen(command).read()
133 except:
134 print "REST IF has issue"
135 exit
136
137 resp = Response(result, status=200, mimetype='application/json')
138 return resp
139
Paul Greyson8d1c6362013-03-27 13:05:24 -0700140@app.route("/proxy/gui/switch/<cmd>/<dpid>")
141def proxy_switch_status_change(cmd, dpid):
142 try:
143 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
144 print command
145 result = os.popen(command).read()
146 except:
147 print "REST IF has issue"
148 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700149
Paul Greyson8d1c6362013-03-27 13:05:24 -0700150 resp = Response(result, status=200, mimetype='application/json')
151 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700152
Paul Greyson2913af82013-03-27 14:53:17 -0700153@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
154def proxy_controller_status_change(cmd, controller_name):
155 try:
156 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
157 print command
158 result = os.popen(command).read()
159 except:
160 print "REST IF has issue"
161 exit
162
163 resp = Response(result, status=200, mimetype='application/json')
164 return resp
165
Paul Greyson472da4c2013-03-28 11:43:17 -0700166@app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
167def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
168 try:
169 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)
170 print command
171 result = os.popen(command).read()
172 except:
173 print "REST IF has issue"
174 exit
175
176 resp = Response(result, status=200, mimetype='application/json')
177 return resp
178
Paul Greyson6f918402013-03-28 12:18:30 -0700179@app.route("/proxy/gui/delflow/<flow_id>")
180def proxy_del_flow(flow_id):
181 try:
182 command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
183 print command
184 result = os.popen(command).read()
185 except:
186 print "REST IF has issue"
187 exit
188
189 resp = Response(result, status=200, mimetype='application/json')
190 return resp
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000191
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700192@app.route("/proxy/gui/iperf/start/<flow_id>/<duration>/<samples>")
193def proxy_iperf_start(flow_id,duration,samples):
194 try:
195 command = "curl -s %s/gui/iperf/start/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id, duration, samples)
196 print command
197 result = os.popen(command).read()
198 except:
199 print "REST IF has issue"
200 exit
201
202 resp = Response(result, status=200, mimetype='application/json')
203 return resp
204
205@app.route("/proxy/gui/iperf/rate/<flow_id>")
206def proxy_iperf_rate(flow_id):
207 try:
208 command = "curl -s %s/gui/iperf/rate/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
209 print command
210 result = os.popen(command).read()
211 except:
212 print "REST IF has issue"
213 exit
214
215 resp = Response(result, status=200, mimetype='application/json')
216 return resp
217
218
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000219###### ONOS RESET API ##############################
220## Worker Func ###
221def get_json(url):
222 code = 200
223 try:
224 command = "curl -s %s" % (url)
225 result = os.popen(command).read()
226 parsedResult = json.loads(result)
Masayoshi Kobayashi8ac33362013-04-05 03:17:26 +0000227 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
228 print "REST %s returned code %s" % (command, parsedResult['code'])
229 code=500
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000230 except:
231 print "REST IF %s has issue" % command
232 result = ""
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000233 code = 500
234
235 return (code, result)
236
237def pick_host():
238 if LB == True:
239 nr_host=len(controllers)
240 r=random.randint(0, nr_host - 1)
241 host=controllers[r]
242 else:
243 host=ONOS_DEFAULT_HOST
244
245 return "http://" + host + ":8080"
246
247## Switch ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000248@app.route("/wm/core/topology/switches/all/json")
249def switches():
250 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000251 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000252 else:
253 host = ONOS_GUI3_HOST
254
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000255 url ="%s/wm/core/topology/switches/all/json" % (host)
256 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000257
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000258 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000259 return resp
260
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000261## Link ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000262@app.route("/wm/core/topology/links/json")
263def links():
264 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000265 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000266 else:
267 host = ONOS_GUI3_HOST
268
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000269 url ="%s/wm/core/topology/links/json" % (host)
270 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000271
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000272 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000273 return resp
274
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000275## FlowSummary ##
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000276@app.route("/wm/flow/getsummary/<start>/<range>/json")
277def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000278 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000279 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000280 else:
281 host = ONOS_GUI3_HOST
282
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000283 url ="%s/wm/flow/getsummary/%s/%s/json" % (host, start, range)
284 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000285
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000286 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000287 return resp
288
289@app.route("/wm/registry/controllers/json")
290def registry_controllers():
291 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000292 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000293 else:
294 host = ONOS_GUI3_HOST
295
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000296 url= "%s/wm/registry/controllers/json" % (host)
297 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000298
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000299 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000300 return resp
301
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000302
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000303@app.route("/wm/registry/switches/json")
304def registry_switches():
305 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000306 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000307 else:
308 host = ONOS_GUI3_HOST
309
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000310 url="%s/wm/registry/switches/json" % (host)
311 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000312
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000313 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000314 return resp
315
Ubuntu82b8a832013-02-06 22:00:11 +0000316def node_id(switch_array, dpid):
317 id = -1
318 for i, val in enumerate(switch_array):
319 if val['name'] == dpid:
320 id = i
321 break
322
323 return id
324
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000325## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000326@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000327def topology_for_gui():
328 try:
329 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
330 result = os.popen(command).read()
331 parsedResult = json.loads(result)
332 except:
333 log_error("REST IF has issue: %s" % command)
334 log_error("%s" % result)
335 sys.exit(0)
336
337 topo = {}
338 switches = []
339 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000340 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000341
342 for v in parsedResult:
343 if v.has_key('dpid'):
344# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
345 dpid = str(v['dpid'])
346 state = str(v['state'])
347 sw = {}
348 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000349 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000350
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000351 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000352 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000353 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000354
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000355 try:
356 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
357 result = os.popen(command).read()
358 parsedResult = json.loads(result)
359 except:
360 log_error("REST IF has issue: %s" % command)
361 log_error("%s" % result)
362
363 for key in parsedResult:
364 dpid = key
365 ctrl = parsedResult[dpid][0]['controllerId']
366 sw_id = node_id(switches, dpid)
367 if sw_id != -1:
368 if switches[sw_id]['group'] != 0:
369 switches[sw_id]['group'] = controllers.index(ctrl) + 1
370
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000371 try:
372 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000373# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000374 p1=1
375 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000376# v2 = "00:00:00:00:00:0d:00:d3"
377 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000378 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
379 result = os.popen(command).read()
380 parsedResult = json.loads(result)
381 except:
382 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000383 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000384
Ubuntu765deff2013-02-28 18:39:13 +0000385 path = []
386 if parsedResult.has_key('flowEntries'):
387 flowEntries= parsedResult['flowEntries']
388 for i, v in enumerate(flowEntries):
389 if i < len(flowEntries) - 1:
390 sdpid= flowEntries[i]['dpid']['value']
391 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700392 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000393
Ubuntu82b8a832013-02-06 22:00:11 +0000394 try:
395 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
396 result = os.popen(command).read()
397 parsedResult = json.loads(result)
398 except:
399 log_error("REST IF has issue: %s" % command)
400 log_error("%s" % result)
401 sys.exit(0)
402
403 for v in parsedResult:
404 link = {}
405 if v.has_key('dst-switch'):
406 dst_dpid = str(v['dst-switch'])
407 dst_id = node_id(switches, dst_dpid)
408 if v.has_key('src-switch'):
409 src_dpid = str(v['src-switch'])
410 src_id = node_id(switches, src_dpid)
411 link['source'] = src_id
412 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000413
414 onpath = 0
415 for (s,d) in path:
416 if s == v['src-switch'] and d == v['dst-switch']:
417 onpath = 1
418 break
419 link['type'] = onpath
420
Ubuntu82b8a832013-02-06 22:00:11 +0000421 links.append(link)
422
423 topo['nodes'] = switches
424 topo['links'] = links
425
Ubuntu82b8a832013-02-06 22:00:11 +0000426 js = json.dumps(topo)
427 resp = Response(js, status=200, mimetype='application/json')
428 return resp
429
Ubuntuaea2a682013-02-08 08:30:10 +0000430#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
431#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
432@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
433def shortest_path(v1, p1, v2, p2):
434 try:
435 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
436 result = os.popen(command).read()
437 parsedResult = json.loads(result)
438 except:
439 log_error("REST IF has issue: %s" % command)
440 log_error("%s" % result)
441 sys.exit(0)
442
443 topo = {}
444 switches = []
445 links = []
446
447 for v in parsedResult:
448 if v.has_key('dpid'):
449 dpid = str(v['dpid'])
450 state = str(v['state'])
451 sw = {}
452 sw['name']=dpid
453 if str(v['state']) == "ACTIVE":
454 if dpid[-2:-1] == "a":
455 sw['group']=1
456 if dpid[-2:-1] == "b":
457 sw['group']=2
458 if dpid[-2:-1] == "c":
459 sw['group']=3
460 if str(v['state']) == "INACTIVE":
461 sw['group']=0
462
463 switches.append(sw)
464
465 try:
466 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
467 result = os.popen(command).read()
468 parsedResult = json.loads(result)
469 except:
470 log_error("No route")
471 parsedResult = []
472# exit(1)
473
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700474 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000475 for i, v in enumerate(parsedResult):
476 if i < len(parsedResult) - 1:
477 sdpid= parsedResult[i]['switch']
478 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700479 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000480
481 try:
482 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
483 result = os.popen(command).read()
484 parsedResult = json.loads(result)
485 except:
486 log_error("REST IF has issue: %s" % command)
487 log_error("%s" % result)
488 sys.exit(0)
489
490 for v in parsedResult:
491 link = {}
492 if v.has_key('dst-switch'):
493 dst_dpid = str(v['dst-switch'])
494 dst_id = node_id(switches, dst_dpid)
495 if v.has_key('src-switch'):
496 src_dpid = str(v['src-switch'])
497 src_id = node_id(switches, src_dpid)
498 link['source'] = src_id
499 link['target'] = dst_id
500 onpath = 0
501 for (s,d) in path:
502 if s == v['src-switch'] and d == v['dst-switch']:
503 onpath = 1
504 break
505
506 link['type'] = onpath
507 links.append(link)
508
509 topo['nodes'] = switches
510 topo['links'] = links
511
Ubuntuaea2a682013-02-08 08:30:10 +0000512 js = json.dumps(topo)
513 resp = Response(js, status=200, mimetype='application/json')
514 return resp
515
Ubuntu82b8a832013-02-06 22:00:11 +0000516@app.route("/wm/core/controller/switches/json")
517def query_switch():
518 try:
519 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
520# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000521 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000522 result = os.popen(command).read()
523 parsedResult = json.loads(result)
524 except:
525 log_error("REST IF has issue: %s" % command)
526 log_error("%s" % result)
527 sys.exit(0)
528
529# print command
530# print result
531 switches_ = []
532 for v in parsedResult:
533 if v.has_key('dpid'):
534 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
535 dpid = str(v['dpid'])
536 state = str(v['state'])
537 sw = {}
538 sw['dpid']=dpid
539 sw['active']=state
540 switches_.append(sw)
541
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000542# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000543 js = json.dumps(switches_)
544 resp = Response(js, status=200, mimetype='application/json')
545 return resp
546
547@app.route("/wm/device/")
548def devices():
549 try:
550 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
551 result = os.popen(command).read()
552 parsedResult = json.loads(result)['results']
553 except:
554 log_error("REST IF has issue: %s" % command)
555 log_error("%s" % result)
556 sys.exit(0)
557
558 devices = []
559 for v in parsedResult:
560 dl_addr = v['dl_addr']
561 nw_addr = v['nw_addr']
562 vertex = v['_id']
563 mac = []
564 mac.append(dl_addr)
565 ip = []
566 ip.append(nw_addr)
567 device = {}
568 device['entryClass']="DefaultEntryClass"
569 device['mac']=mac
570 device['ipv4']=ip
571 device['vlan']=[]
572 device['lastSeen']=0
573 attachpoints =[]
574
575 port, dpid = deviceV_to_attachpoint(vertex)
576 attachpoint = {}
577 attachpoint['port']=port
578 attachpoint['switchDPID']=dpid
579 attachpoints.append(attachpoint)
580 device['attachmentPoint']=attachpoints
581 devices.append(device)
582
Ubuntu82b8a832013-02-06 22:00:11 +0000583 js = json.dumps(devices)
584 resp = Response(js, status=200, mimetype='application/json')
585 return resp
586
587#{"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}
588
Ubuntu82b8a832013-02-06 22:00:11 +0000589## return fake stat for now
590@app.route("/wm/core/switch/<switchId>/<statType>/json")
591def switch_stat(switchId, statType):
592 if statType == "desc":
593 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
594 ret = {}
595 ret[switchId]=desc
596 elif statType == "aggregate":
597 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
598 ret = {}
599 ret[switchId]=aggr
600 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700601 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000602
603 js = json.dumps(ret)
604 resp = Response(js, status=200, mimetype='application/json')
605 return resp
606
607
608@app.route("/wm/topology/links/json")
609def query_links():
610 try:
611 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000612 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000613 result = os.popen(command).read()
614 parsedResult = json.loads(result)['results']
615 except:
616 log_error("REST IF has issue: %s" % command)
617 log_error("%s" % result)
618 sys.exit(0)
619
620 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000621# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000622 sport = []
623 links = []
624 for v in parsedResult:
625 srcport = v['_id']
626 try:
627 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
628 print command
629 result = os.popen(command).read()
630 linkResults = json.loads(result)['results']
631 except:
632 log_error("REST IF has issue: %s" % command)
633 log_error("%s" % result)
634 sys.exit(0)
635
636 for p in linkResults:
637 if p.has_key('type') and p['type'] == "port":
638 dstport = p['_id']
639 (sport, sdpid) = portV_to_port_dpid(srcport)
640 (dport, ddpid) = portV_to_port_dpid(dstport)
641 link = {}
642 link["src-switch"]=sdpid
643 link["src-port"]=sport
644 link["src-port-state"]=0
645 link["dst-switch"]=ddpid
646 link["dst-port"]=dport
647 link["dst-port-state"]=0
648 link["type"]="internal"
649 links.append(link)
650
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000651# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000652 js = json.dumps(links)
653 resp = Response(js, status=200, mimetype='application/json')
654 return resp
655
Ubuntuc016ba12013-02-27 21:53:41 +0000656@app.route("/controller_status")
657def controller_status():
Tim Lindberg201ade22013-04-05 11:52:08 -0700658# onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
659 onos_check="cd; onos status %s | grep %s | awk '{print $2}'"
Ubuntuc016ba12013-02-27 21:53:41 +0000660 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
661
662 cont_status=[]
663 for i in controllers:
664 status={}
665 onos=os.popen(onos_check % i).read()[:-1]
Tim Lindberg201ade22013-04-05 11:52:08 -0700666 onos=os.popen(onos_check % (i, i.lower())).read()[:-1]
Ubuntuc016ba12013-02-27 21:53:41 +0000667 status["name"]=i
668 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000669 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000670 cont_status.append(status)
671
672 js = json.dumps(cont_status)
673 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000674 return resp
675
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000676### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000677@app.route("/gui/controller/<cmd>/<controller_name>")
678def controller_status_change(cmd, controller_name):
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000679 if (TESTBED == "hw"):
680 start_onos="cd; onos start %s" % (controller_name[-1:])
681 stop_onos="cd; onos stop %s" % (controller_name[-1:])
682 else:
683 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
684 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000685
686 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000687 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000688 ret = "controller %s is up" % (controller_name)
689 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000690 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000691 ret = "controller %s is down" % (controller_name)
692
693 return ret
694
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000695@app.route("/gui/switchctrl/<cmd>")
696def switch_controller_setting(cmd):
697 if cmd =="local":
698 print "All aggr switches connects to local controller only"
699 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700700 if (TESTBED == "sw"):
701 for i in range(0, len(controllers)):
702 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-local.sh'" % (controllers[i])
703 result += os.popen(cmd_string).read()
704 else:
705 cmd_string="cd; switch local"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000706 result += os.popen(cmd_string).read()
707 elif cmd =="all":
708 print "All aggr switches connects to all controllers except for core controller"
709 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700710 if (TESTBED == "sw"):
711 for i in range(0, len(controllers)):
712 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i])
713 result += os.popen(cmd_string).read()
714 else:
715 cmd_string="cd; switch all"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000716 result += os.popen(cmd_string).read()
717
718 return result
719
720
721
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000722@app.route("/gui/switch/<cmd>/<dpid>")
723def switch_status_change(cmd, dpid):
Tim Lindberg201ade22013-04-05 11:52:08 -0700724 result = ""
725 if (TESTBED == "hw"):
726 return result
727
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000728 r = re.compile(':')
729 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000730 host=controllers[0]
731 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
732 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000733 print "cmd_string"
734
735 if cmd =="up" or cmd=="down":
736 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000737 os.popen(cmd_string)
738 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000739
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000740 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000741
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000742#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000743#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000744@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
745def link_up(src_dpid, src_port, dst_dpid, dst_port):
Tim Lindberg201ade22013-04-05 11:52:08 -0700746 result = ""
747
748 if (TESTBED == "sw"):
749 result = link_up_sw(src_dpid, src_port, dst_dpid, dst_port)
750 else:
751 result = link_up_hw(src_dpid, src_port, dst_dpid, dst_port)
752 return result
753
754# Link up on software testbed
755def link_up_sw(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000756
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000757 cmd = 'up'
758 result=""
Paul Greyson472da4c2013-03-28 11:43:17 -0700759 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000760 if dpid in core_switches:
761 host = controllers[0]
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000762 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000763 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000764 host = controllers[hostid-1]
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000765
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +0000766 if dpid == src_dpid:
767 (port, dontcare) = get_link_ports(dpid, dst_dpid)
768 else:
769 (port, dontcare) = get_link_ports(dpid, src_dpid)
770
771 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
772 print cmd_string
773 res=os.popen(cmd_string).read()
774 result = result + ' ' + res
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000775
776 return result
777
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +0000778# if hostid == 2 :
779# src_ports = [51]
780# else :
781# src_ports = [26]
782#
783# for port in src_ports :
784# cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
785# print cmd_string
786# res=os.popen(cmd_string).read()
787
788
789
Tim Lindberg201ade22013-04-05 11:52:08 -0700790# Link up on hardware testbed
791def link_up_hw(src_dpid, src_port, dst_dpid, dst_port):
792
793 port1 = src_port
794 port2 = dst_port
795 if src_dpid == "00:00:00:00:ba:5e:ba:11":
796 if dst_dpid == "00:00:00:08:a2:08:f9:01":
797 port1 = 24
798 port2 = 24
799 elif dst_dpid == "00:01:00:16:97:08:9a:46":
800 port1 = 23
801 port2 = 23
802 elif src_dpid == "00:00:00:00:ba:5e:ba:13":
803 if dst_dpid == "00:00:20:4e:7f:51:8a:35":
804 port1 = 22
805 port2 = 22
806 elif dst_dpid == "00:00:00:00:00:00:ba:12":
807 port1 = 23
808 port2 = 23
809 elif src_dpid == "00:00:00:00:00:00:ba:12":
810 if dst_dpid == "00:00:00:00:ba:5e:ba:13":
811 port1 = 23
812 port2 = 23
813 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
814 port1 = 22
815 port2 = 22
816 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
817 port1 = 24
818 port2 = 21
819 elif src_dpid == "00:01:00:16:97:08:9a:46":
820 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
821 port1 = 23
822 port2 = 23
823 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
824 port1 = 24
825 port2 = 24
826 elif src_dpid == "00:00:00:08:a2:08:f9:01":
827 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
828 port1 = 24
829 port2 = 24
830 elif dst_dpid == "00:00:00:00:00:00:ba:12":
831 port1 = 22
832 port2 = 22
833 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
834 port1 = 23
835 port2 = 23
836 elif src_dpid == "00:00:20:4e:7f:51:8a:35":
837 if dst_dpid == "00:00:00:00:00:00:ba:12":
838 port1 = 21
839 port2 = 24
840 elif dst_dpid == "00:00:00:00:ba:5e:ba:13":
841 port1 = 22
842 port2 = 22
843 elif dst_dpid == "00:01:00:16:97:08:9a:46":
844 port1 = 24
845 port2 = 24
846 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
847 port1 = 23
848 port2 = 23
849
850 cmd = 'up'
851 result=""
852 host = controllers[0]
853 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % (src_dpid, port1, cmd)
854 print cmd_string
855 res=os.popen(cmd_string).read()
856 result = result + ' ' + res
857 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % (dst_dpid, port2, cmd)
858 print cmd_string
859 res=os.popen(cmd_string).read()
860 result = result + ' ' + res
861
862
863 return result
864
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000865
866#* Link Down
867#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000868@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000869def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
870
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000871 if src_dpid in core_switches:
872 host = controllers[0]
873 else:
874 hostid=int(src_dpid.split(':')[-2])
875 host = controllers[hostid-1]
876
Tim Lindberg201ade22013-04-05 11:52:08 -0700877 if (TESTBED == "sw"):
878 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
879 else:
Tim Lindberga6c04172013-04-05 17:34:05 -0700880 if ( src_dpid == "00:00:00:08:a2:08:f9:01" ):
881 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % ( dst_dpid, dst_port, cmd)
882 else:
883 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % ( src_dpid, src_port, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000884 print cmd_string
885
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000886 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000887
888 return result
889
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000890#* Create Flow
891#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000892#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 +0000893@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000894def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
Masayoshi Kobayashiab01bcf2013-04-09 04:21:57 +0000895 host = pick_host()
896 url ="%s/wm/flow/getsummary/%s/%s/json" % (host, 0, 0)
897 (code, result) = get_json(url)
898 parsedResult = json.loads(result)
899 flow_nr = int(parsedResult[-1]['flowId'], 16)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000900 flow_nr += 1
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000901 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)
902 flow_nr += 1
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000903 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 +0000904 print command
905 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000906 errcode1 = os.popen(command1).read()
907 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000908
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000909#* Delete Flow
910#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000911@app.route("/gui/delflow/<flow_id>")
912def del_flow(flow_id):
913 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
914 print command
915 errcode = os.popen(command).read()
916 return errcode
917
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000918#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700919#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000920@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
921def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000922 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000923 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000924 print command
925 result = os.popen(command).read()
926 if len(result) == 0:
927 print "No Flow found"
928 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000929 except:
930 print "REST IF has issue"
931 exit
932
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000933 parsedResult = json.loads(result)
934
935 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000936 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
937 src_port = parsedResult['dataPath']['srcPort']['port']['value']
938 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
939 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000940# 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 +0000941
942 if src_dpid in core_switches:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700943 src_host = controllers[0]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000944 else:
945 hostid=int(src_dpid.split(':')[-2])
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700946 src_host = controllers[hostid-1]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000947
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700948 if dst_dpid in core_switches:
949 dst_host = controllers[0]
950 else:
951 hostid=int(dst_dpid.split(':')[-2])
952 dst_host = controllers[hostid-1]
953
954# /runiperf.sh <flowid> <src_dpid> <dst_dpid> svr|client <proto>/<duration>/<interval>/<samples>
955 protocol="udp"
956 interval=0.1
957 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)
958 print cmd_string
959 os.popen(cmd_string)
960
961 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 +0000962 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000963 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000964
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000965 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000966
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000967#* Get Iperf Throughput
968#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000969@app.route("/gui/iperf/rate/<flow_id>")
970def iperf_rate(flow_id):
Tim Lindberga6c04172013-04-05 17:34:05 -0700971 if (TESTBED == "hw"):
972 return "{}"
973
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000974 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000975 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
976 print command
977 result = os.popen(command).read()
978 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000979 resp = Response(result, status=400, mimetype='text/html')
980 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000981 except:
982 print "REST IF has issue"
983 exit
984
985 parsedResult = json.loads(result)
986
987 flowId = int(parsedResult['flowId']['value'], 16)
988 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
989 src_port = parsedResult['dataPath']['srcPort']['port']['value']
990 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
991 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
992
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700993 if dst_dpid in core_switches:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000994 host = controllers[0]
995 else:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700996 hostid=int(dst_dpid.split(':')[-2])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000997 host = controllers[hostid-1]
998
999 try:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -07001000 command = "curl -s http://%s:%s/log/iperfsvr_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001001 print command
1002 result = os.popen(command).read()
1003 except:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001004 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +00001005
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +00001006 if len(result) == 0:
1007 resp = Response(result, status=400, mimetype='text/html')
1008 return "no iperf file found (flowid %s)" % flow_id;
1009 else:
1010 resp = Response(result, status=200, mimetype='application/json')
1011 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +00001012
1013
Ubuntu82b8a832013-02-06 22:00:11 +00001014if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001015 random.seed()
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +00001016 read_config()
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +00001017 read_link_def()
Ubuntu82b8a832013-02-06 22:00:11 +00001018 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001019# 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")
1020# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
1021# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
1022# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
1023# 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 +00001024# print "-- query all switches --"
1025# query_switch()
1026# print "-- query topo --"
1027# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001028# link_change(1,2,3,4)
1029 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001030# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +00001031# print "-- query all devices --"
1032# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001033# iperf_start(1,10,15)
1034# iperf_rate(1)
Masayoshi Kobayashiab01bcf2013-04-09 04:21:57 +00001035# switches()
1036 add_flow(1,2,3,4,5,6)
Ubuntu82b8a832013-02-06 22:00:11 +00001037 else:
1038 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +00001039 app.run(threaded=True, host="0.0.0.0", port=9000)