blob: 91812489424234444e82ad5845634b980e7dd361 [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'])
Paul Greysonc090d142013-04-09 16:59:03 -070062@app.route('/ons-demo/d3/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000063@app.route('/ons-demo/css/<filename>', methods=['GET'])
64@app.route('/ons-demo/assets/<filename>', methods=['GET'])
65@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000066def return_file(filename="index.html"):
67 if request.path == "/":
68 fullpath = "./index.html"
69 else:
70 fullpath = str(request.path)[1:]
71
Paul Greysonc090d142013-04-09 16:59:03 -070072 try:
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +000073 open(fullpath)
74 except:
75 response = make_response("Cannot find a file: %s" % (fullpath), 500)
76 response.headers["Content-type"] = "text/html"
77 return response
78
Ubuntu82b8a832013-02-06 22:00:11 +000079 response = make_response(open(fullpath).read())
80 suffix = fullpath.split(".")[-1]
81
82 if suffix == "html" or suffix == "htm":
83 response.headers["Content-type"] = "text/html"
84 elif suffix == "js":
85 response.headers["Content-type"] = "application/javascript"
86 elif suffix == "css":
87 response.headers["Content-type"] = "text/css"
88 elif suffix == "png":
89 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -070090 elif suffix == "svg":
91 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000092
93 return response
94
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000095## Proxy ##
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070096@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
97def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
98 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -070099 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 -0700100 print command
101 result = os.popen(command).read()
102 except:
103 print "REST IF has issue"
104 exit
105
106 resp = Response(result, status=200, mimetype='application/json')
107 return resp
108
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000109@app.route("/proxy/gui/switchctrl/<cmd>")
110def proxy_switch_controller_setting(cmd):
111 try:
112 command = "curl -s %s/gui/switchctrl/%s" % (ONOS_GUI3_CONTROL_HOST, cmd)
113 print command
114 result = os.popen(command).read()
115 except:
116 print "REST IF has issue"
117 exit
118
119 resp = Response(result, status=200, mimetype='application/json')
120 return resp
121
Paul Greyson8d1c6362013-03-27 13:05:24 -0700122@app.route("/proxy/gui/switch/<cmd>/<dpid>")
123def proxy_switch_status_change(cmd, dpid):
124 try:
125 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
126 print command
127 result = os.popen(command).read()
128 except:
129 print "REST IF has issue"
130 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700131
Paul Greyson8d1c6362013-03-27 13:05:24 -0700132 resp = Response(result, status=200, mimetype='application/json')
133 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700134
Paul Greyson2913af82013-03-27 14:53:17 -0700135@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
136def proxy_controller_status_change(cmd, controller_name):
137 try:
138 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
139 print command
140 result = os.popen(command).read()
141 except:
142 print "REST IF has issue"
143 exit
144
145 resp = Response(result, status=200, mimetype='application/json')
146 return resp
147
Paul Greyson472da4c2013-03-28 11:43:17 -0700148@app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
149def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
150 try:
151 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)
152 print command
153 result = os.popen(command).read()
154 except:
155 print "REST IF has issue"
156 exit
157
158 resp = Response(result, status=200, mimetype='application/json')
159 return resp
160
Paul Greyson6f918402013-03-28 12:18:30 -0700161@app.route("/proxy/gui/delflow/<flow_id>")
162def proxy_del_flow(flow_id):
163 try:
164 command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
165 print command
166 result = os.popen(command).read()
167 except:
168 print "REST IF has issue"
169 exit
170
171 resp = Response(result, status=200, mimetype='application/json')
172 return resp
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000173
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700174@app.route("/proxy/gui/iperf/start/<flow_id>/<duration>/<samples>")
175def proxy_iperf_start(flow_id,duration,samples):
176 try:
177 command = "curl -s %s/gui/iperf/start/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id, duration, samples)
178 print command
179 result = os.popen(command).read()
180 except:
181 print "REST IF has issue"
182 exit
183
184 resp = Response(result, status=200, mimetype='application/json')
185 return resp
186
187@app.route("/proxy/gui/iperf/rate/<flow_id>")
188def proxy_iperf_rate(flow_id):
189 try:
190 command = "curl -s %s/gui/iperf/rate/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
191 print command
192 result = os.popen(command).read()
193 except:
194 print "REST IF has issue"
195 exit
196
197 resp = Response(result, status=200, mimetype='application/json')
198 return resp
199
200
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000201###### ONOS RESET API ##############################
202## Worker Func ###
203def get_json(url):
204 code = 200
205 try:
206 command = "curl -s %s" % (url)
207 result = os.popen(command).read()
Paul Greysonc090d142013-04-09 16:59:03 -0700208 parsedResult = json.loads(result)
Masayoshi Kobayashi8ac33362013-04-05 03:17:26 +0000209 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
210 print "REST %s returned code %s" % (command, parsedResult['code'])
211 code=500
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000212 except:
213 print "REST IF %s has issue" % command
214 result = ""
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000215 code = 500
216
217 return (code, result)
218
219def pick_host():
220 if LB == True:
221 nr_host=len(controllers)
222 r=random.randint(0, nr_host - 1)
223 host=controllers[r]
224 else:
225 host=ONOS_DEFAULT_HOST
Paul Greysonc090d142013-04-09 16:59:03 -0700226
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000227 return "http://" + host + ":8080"
228
229## Switch ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000230@app.route("/wm/core/topology/switches/all/json")
231def switches():
232 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000233 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000234 else:
235 host = ONOS_GUI3_HOST
236
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000237 url ="%s/wm/core/topology/switches/all/json" % (host)
238 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000239
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000240 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000241 return resp
242
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000243## Link ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000244@app.route("/wm/core/topology/links/json")
245def links():
246 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000247 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000248 else:
249 host = ONOS_GUI3_HOST
250
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000251 url ="%s/wm/core/topology/links/json" % (host)
252 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000253
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000254 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000255 return resp
256
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000257## FlowSummary ##
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000258@app.route("/wm/flow/getsummary/<start>/<range>/json")
259def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000260 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000261 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000262 else:
263 host = ONOS_GUI3_HOST
264
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000265 url ="%s/wm/flow/getsummary/%s/%s/json" % (host, start, range)
266 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000267
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000268 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000269 return resp
270
271@app.route("/wm/registry/controllers/json")
272def registry_controllers():
273 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000274 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000275 else:
276 host = ONOS_GUI3_HOST
277
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000278 url= "%s/wm/registry/controllers/json" % (host)
279 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000280
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000281 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000282 return resp
283
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000284
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000285@app.route("/wm/registry/switches/json")
286def registry_switches():
287 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000288 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000289 else:
290 host = ONOS_GUI3_HOST
291
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000292 url="%s/wm/registry/switches/json" % (host)
293 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000294
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000295 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000296 return resp
297
Ubuntu82b8a832013-02-06 22:00:11 +0000298def node_id(switch_array, dpid):
299 id = -1
300 for i, val in enumerate(switch_array):
301 if val['name'] == dpid:
302 id = i
303 break
304
305 return id
306
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000307## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000308@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000309def topology_for_gui():
310 try:
311 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
312 result = os.popen(command).read()
313 parsedResult = json.loads(result)
314 except:
315 log_error("REST IF has issue: %s" % command)
316 log_error("%s" % result)
317 sys.exit(0)
318
319 topo = {}
320 switches = []
321 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000322 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000323
324 for v in parsedResult:
325 if v.has_key('dpid'):
326# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
327 dpid = str(v['dpid'])
328 state = str(v['state'])
329 sw = {}
330 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000331 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000332
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000333 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000334 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000335 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000336
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000337 try:
338 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
339 result = os.popen(command).read()
340 parsedResult = json.loads(result)
341 except:
342 log_error("REST IF has issue: %s" % command)
343 log_error("%s" % result)
344
345 for key in parsedResult:
346 dpid = key
347 ctrl = parsedResult[dpid][0]['controllerId']
348 sw_id = node_id(switches, dpid)
349 if sw_id != -1:
350 if switches[sw_id]['group'] != 0:
351 switches[sw_id]['group'] = controllers.index(ctrl) + 1
352
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000353 try:
354 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000355# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000356 p1=1
357 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000358# v2 = "00:00:00:00:00:0d:00:d3"
359 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000360 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
361 result = os.popen(command).read()
362 parsedResult = json.loads(result)
363 except:
364 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000365 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000366
Ubuntu765deff2013-02-28 18:39:13 +0000367 path = []
368 if parsedResult.has_key('flowEntries'):
369 flowEntries= parsedResult['flowEntries']
370 for i, v in enumerate(flowEntries):
371 if i < len(flowEntries) - 1:
372 sdpid= flowEntries[i]['dpid']['value']
373 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700374 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000375
Ubuntu82b8a832013-02-06 22:00:11 +0000376 try:
377 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
378 result = os.popen(command).read()
379 parsedResult = json.loads(result)
380 except:
381 log_error("REST IF has issue: %s" % command)
382 log_error("%s" % result)
383 sys.exit(0)
384
385 for v in parsedResult:
386 link = {}
387 if v.has_key('dst-switch'):
388 dst_dpid = str(v['dst-switch'])
389 dst_id = node_id(switches, dst_dpid)
390 if v.has_key('src-switch'):
391 src_dpid = str(v['src-switch'])
392 src_id = node_id(switches, src_dpid)
393 link['source'] = src_id
394 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000395
396 onpath = 0
397 for (s,d) in path:
398 if s == v['src-switch'] and d == v['dst-switch']:
399 onpath = 1
400 break
401 link['type'] = onpath
402
Ubuntu82b8a832013-02-06 22:00:11 +0000403 links.append(link)
404
405 topo['nodes'] = switches
406 topo['links'] = links
407
Ubuntu82b8a832013-02-06 22:00:11 +0000408 js = json.dumps(topo)
409 resp = Response(js, status=200, mimetype='application/json')
410 return resp
411
Ubuntuaea2a682013-02-08 08:30:10 +0000412#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
413#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
414@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
415def shortest_path(v1, p1, v2, p2):
416 try:
417 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
418 result = os.popen(command).read()
419 parsedResult = json.loads(result)
420 except:
421 log_error("REST IF has issue: %s" % command)
422 log_error("%s" % result)
423 sys.exit(0)
424
425 topo = {}
426 switches = []
427 links = []
428
429 for v in parsedResult:
430 if v.has_key('dpid'):
431 dpid = str(v['dpid'])
432 state = str(v['state'])
433 sw = {}
434 sw['name']=dpid
435 if str(v['state']) == "ACTIVE":
436 if dpid[-2:-1] == "a":
437 sw['group']=1
438 if dpid[-2:-1] == "b":
439 sw['group']=2
440 if dpid[-2:-1] == "c":
441 sw['group']=3
442 if str(v['state']) == "INACTIVE":
443 sw['group']=0
444
445 switches.append(sw)
446
447 try:
448 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
449 result = os.popen(command).read()
450 parsedResult = json.loads(result)
451 except:
452 log_error("No route")
453 parsedResult = []
454# exit(1)
455
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700456 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000457 for i, v in enumerate(parsedResult):
458 if i < len(parsedResult) - 1:
459 sdpid= parsedResult[i]['switch']
460 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700461 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000462
463 try:
464 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
465 result = os.popen(command).read()
466 parsedResult = json.loads(result)
467 except:
468 log_error("REST IF has issue: %s" % command)
469 log_error("%s" % result)
470 sys.exit(0)
471
472 for v in parsedResult:
473 link = {}
474 if v.has_key('dst-switch'):
475 dst_dpid = str(v['dst-switch'])
476 dst_id = node_id(switches, dst_dpid)
477 if v.has_key('src-switch'):
478 src_dpid = str(v['src-switch'])
479 src_id = node_id(switches, src_dpid)
480 link['source'] = src_id
481 link['target'] = dst_id
482 onpath = 0
483 for (s,d) in path:
484 if s == v['src-switch'] and d == v['dst-switch']:
485 onpath = 1
486 break
487
488 link['type'] = onpath
489 links.append(link)
490
491 topo['nodes'] = switches
492 topo['links'] = links
493
Ubuntuaea2a682013-02-08 08:30:10 +0000494 js = json.dumps(topo)
495 resp = Response(js, status=200, mimetype='application/json')
496 return resp
497
Ubuntu82b8a832013-02-06 22:00:11 +0000498@app.route("/wm/core/controller/switches/json")
499def query_switch():
500 try:
501 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
502# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000503 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000504 result = os.popen(command).read()
505 parsedResult = json.loads(result)
506 except:
507 log_error("REST IF has issue: %s" % command)
508 log_error("%s" % result)
509 sys.exit(0)
510
511# print command
512# print result
513 switches_ = []
514 for v in parsedResult:
515 if v.has_key('dpid'):
516 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
517 dpid = str(v['dpid'])
518 state = str(v['state'])
519 sw = {}
520 sw['dpid']=dpid
521 sw['active']=state
522 switches_.append(sw)
523
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000524# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000525 js = json.dumps(switches_)
526 resp = Response(js, status=200, mimetype='application/json')
527 return resp
528
529@app.route("/wm/device/")
530def devices():
531 try:
532 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
533 result = os.popen(command).read()
534 parsedResult = json.loads(result)['results']
535 except:
536 log_error("REST IF has issue: %s" % command)
537 log_error("%s" % result)
538 sys.exit(0)
539
540 devices = []
541 for v in parsedResult:
542 dl_addr = v['dl_addr']
543 nw_addr = v['nw_addr']
544 vertex = v['_id']
545 mac = []
546 mac.append(dl_addr)
547 ip = []
548 ip.append(nw_addr)
549 device = {}
550 device['entryClass']="DefaultEntryClass"
551 device['mac']=mac
552 device['ipv4']=ip
553 device['vlan']=[]
554 device['lastSeen']=0
555 attachpoints =[]
556
557 port, dpid = deviceV_to_attachpoint(vertex)
558 attachpoint = {}
559 attachpoint['port']=port
560 attachpoint['switchDPID']=dpid
561 attachpoints.append(attachpoint)
562 device['attachmentPoint']=attachpoints
563 devices.append(device)
564
Ubuntu82b8a832013-02-06 22:00:11 +0000565 js = json.dumps(devices)
566 resp = Response(js, status=200, mimetype='application/json')
567 return resp
568
569#{"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}
570
Ubuntu82b8a832013-02-06 22:00:11 +0000571## return fake stat for now
572@app.route("/wm/core/switch/<switchId>/<statType>/json")
573def switch_stat(switchId, statType):
574 if statType == "desc":
575 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
576 ret = {}
577 ret[switchId]=desc
578 elif statType == "aggregate":
579 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
580 ret = {}
581 ret[switchId]=aggr
582 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700583 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000584
585 js = json.dumps(ret)
586 resp = Response(js, status=200, mimetype='application/json')
587 return resp
588
589
590@app.route("/wm/topology/links/json")
591def query_links():
592 try:
593 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000594 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000595 result = os.popen(command).read()
596 parsedResult = json.loads(result)['results']
597 except:
598 log_error("REST IF has issue: %s" % command)
599 log_error("%s" % result)
600 sys.exit(0)
601
602 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000603# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000604 sport = []
605 links = []
606 for v in parsedResult:
607 srcport = v['_id']
608 try:
609 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
610 print command
611 result = os.popen(command).read()
612 linkResults = json.loads(result)['results']
613 except:
614 log_error("REST IF has issue: %s" % command)
615 log_error("%s" % result)
616 sys.exit(0)
617
618 for p in linkResults:
619 if p.has_key('type') and p['type'] == "port":
620 dstport = p['_id']
621 (sport, sdpid) = portV_to_port_dpid(srcport)
622 (dport, ddpid) = portV_to_port_dpid(dstport)
623 link = {}
624 link["src-switch"]=sdpid
625 link["src-port"]=sport
626 link["src-port-state"]=0
627 link["dst-switch"]=ddpid
628 link["dst-port"]=dport
629 link["dst-port-state"]=0
630 link["type"]="internal"
631 links.append(link)
632
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000633# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000634 js = json.dumps(links)
635 resp = Response(js, status=200, mimetype='application/json')
636 return resp
637
Ubuntuc016ba12013-02-27 21:53:41 +0000638@app.route("/controller_status")
639def controller_status():
Tim Lindberg201ade22013-04-05 11:52:08 -0700640# onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
641 onos_check="cd; onos status %s | grep %s | awk '{print $2}'"
Ubuntuc016ba12013-02-27 21:53:41 +0000642 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
643
644 cont_status=[]
645 for i in controllers:
646 status={}
647 onos=os.popen(onos_check % i).read()[:-1]
Tim Lindberg201ade22013-04-05 11:52:08 -0700648 onos=os.popen(onos_check % (i, i.lower())).read()[:-1]
Ubuntuc016ba12013-02-27 21:53:41 +0000649 status["name"]=i
650 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000651 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000652 cont_status.append(status)
653
654 js = json.dumps(cont_status)
655 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000656 return resp
657
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000658### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000659@app.route("/gui/controller/<cmd>/<controller_name>")
660def controller_status_change(cmd, controller_name):
Tim Lindberg201ade22013-04-05 11:52:08 -0700661# start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
662# stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
663 start_onos="cd; onos start %s" % (controller_name[-1:])
664 stop_onos="cd; onos stop %s" % (controller_name[-1:])
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000665
666 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000667 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000668 ret = "controller %s is up" % (controller_name)
669 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000670 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000671 ret = "controller %s is down" % (controller_name)
672
673 return ret
674
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000675@app.route("/gui/switchctrl/<cmd>")
676def switch_controller_setting(cmd):
677 if cmd =="local":
678 print "All aggr switches connects to local controller only"
679 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700680 if (TESTBED == "sw"):
Paul Greysonc090d142013-04-09 16:59:03 -0700681 for i in range(0, len(controllers)):
Tim Lindberg201ade22013-04-05 11:52:08 -0700682 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-local.sh'" % (controllers[i])
683 result += os.popen(cmd_string).read()
684 else:
685 cmd_string="cd; switch local"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000686 result += os.popen(cmd_string).read()
687 elif cmd =="all":
688 print "All aggr switches connects to all controllers except for core controller"
689 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700690 if (TESTBED == "sw"):
Paul Greysonc090d142013-04-09 16:59:03 -0700691 for i in range(0, len(controllers)):
Tim Lindberg201ade22013-04-05 11:52:08 -0700692 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i])
693 result += os.popen(cmd_string).read()
Paul Greysonc090d142013-04-09 16:59:03 -0700694 else:
Tim Lindberg201ade22013-04-05 11:52:08 -0700695 cmd_string="cd; switch all"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000696 result += os.popen(cmd_string).read()
697
698 return result
699
700
701
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000702@app.route("/gui/switch/<cmd>/<dpid>")
703def switch_status_change(cmd, dpid):
Tim Lindberg201ade22013-04-05 11:52:08 -0700704 result = ""
705 if (TESTBED == "hw"):
706 return result
707
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000708 r = re.compile(':')
709 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000710 host=controllers[0]
711 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
712 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000713 print "cmd_string"
714
715 if cmd =="up" or cmd=="down":
716 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000717 os.popen(cmd_string)
718 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000719
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000720 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000721
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000722#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000723#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000724@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
725def link_up(src_dpid, src_port, dst_dpid, dst_port):
Tim Lindberg201ade22013-04-05 11:52:08 -0700726 result = ""
727
Paul Greysonc090d142013-04-09 16:59:03 -0700728 if (TESTBED == "sw"):
Tim Lindberg201ade22013-04-05 11:52:08 -0700729 result = link_up_sw(src_dpid, src_port, dst_dpid, dst_port)
730 else:
731 result = link_up_hw(src_dpid, src_port, dst_dpid, dst_port)
732 return result
733
734# Link up on software testbed
735def link_up_sw(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000736
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000737 cmd = 'up'
738 result=""
739
Paul Greyson472da4c2013-03-28 11:43:17 -0700740 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000741 if dpid in core_switches:
742 host = controllers[0]
743 src_ports = [1, 2, 3, 4, 5]
744 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000745 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000746 host = controllers[hostid-1]
747 if hostid == 2 :
748 src_ports = [51]
749 else :
750 src_ports = [26]
751
752 for port in src_ports :
753 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
754 print cmd_string
755 res=os.popen(cmd_string).read()
756 result = result + ' ' + res
757
758 return result
759
Tim Lindberg201ade22013-04-05 11:52:08 -0700760# Link up on hardware testbed
761def link_up_hw(src_dpid, src_port, dst_dpid, dst_port):
762
763 port1 = src_port
764 port2 = dst_port
765 if src_dpid == "00:00:00:00:ba:5e:ba:11":
766 if dst_dpid == "00:00:00:08:a2:08:f9:01":
767 port1 = 24
768 port2 = 24
769 elif dst_dpid == "00:01:00:16:97:08:9a:46":
770 port1 = 23
771 port2 = 23
772 elif src_dpid == "00:00:00:00:ba:5e:ba:13":
773 if dst_dpid == "00:00:20:4e:7f:51:8a:35":
774 port1 = 22
775 port2 = 22
776 elif dst_dpid == "00:00:00:00:00:00:ba:12":
777 port1 = 23
778 port2 = 23
779 elif src_dpid == "00:00:00:00:00:00:ba:12":
780 if dst_dpid == "00:00:00:00:ba:5e:ba:13":
781 port1 = 23
782 port2 = 23
783 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
784 port1 = 22
785 port2 = 22
786 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
787 port1 = 24
788 port2 = 21
789 elif src_dpid == "00:01:00:16:97:08:9a:46":
790 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
791 port1 = 23
792 port2 = 23
793 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
794 port1 = 24
795 port2 = 24
796 elif src_dpid == "00:00:00:08:a2:08:f9:01":
797 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
798 port1 = 24
799 port2 = 24
800 elif dst_dpid == "00:00:00:00:00:00:ba:12":
801 port1 = 22
802 port2 = 22
803 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
804 port1 = 23
805 port2 = 23
806 elif src_dpid == "00:00:20:4e:7f:51:8a:35":
807 if dst_dpid == "00:00:00:00:00:00:ba:12":
808 port1 = 21
809 port2 = 24
810 elif dst_dpid == "00:00:00:00:ba:5e:ba:13":
811 port1 = 22
812 port2 = 22
813 elif dst_dpid == "00:01:00:16:97:08:9a:46":
814 port1 = 24
815 port2 = 24
816 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
817 port1 = 23
818 port2 = 23
819
820 cmd = 'up'
821 result=""
822 host = controllers[0]
823 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % (src_dpid, port1, cmd)
824 print cmd_string
825 res=os.popen(cmd_string).read()
826 result = result + ' ' + res
827 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % (dst_dpid, port2, cmd)
828 print cmd_string
829 res=os.popen(cmd_string).read()
830 result = result + ' ' + res
831
832
833 return result
834
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000835
836#* Link Down
837#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000838@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000839def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
840
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000841 if src_dpid in core_switches:
842 host = controllers[0]
843 else:
844 hostid=int(src_dpid.split(':')[-2])
845 host = controllers[hostid-1]
846
Tim Lindberg201ade22013-04-05 11:52:08 -0700847 if (TESTBED == "sw"):
848 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
849 else:
Tim Lindberga6c04172013-04-05 17:34:05 -0700850 if ( src_dpid == "00:00:00:08:a2:08:f9:01" ):
851 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % ( dst_dpid, dst_port, cmd)
852 else:
853 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % ( src_dpid, src_port, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000854 print cmd_string
855
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000856 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000857
858 return result
859
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000860#* Create Flow
861#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000862#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 +0000863@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000864def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
865 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
866 print command
867 ret = os.popen(command).read()
868 if ret == "":
869 flow_nr=0
870 else:
871 flow_nr=int(ret)
872
873 flow_nr += 1
Umesh Krishnaswamyf22d3ed2013-04-03 11:57:54 -0700874 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 +0000875 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 +0000876 print command
877 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000878 errcode1 = os.popen(command1).read()
879 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000880
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000881#* Delete Flow
882#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000883@app.route("/gui/delflow/<flow_id>")
884def del_flow(flow_id):
885 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
886 print command
887 errcode = os.popen(command).read()
888 return errcode
889
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000890#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700891#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000892@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
893def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000894 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000895 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000896 print command
897 result = os.popen(command).read()
898 if len(result) == 0:
899 print "No Flow found"
900 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000901 except:
902 print "REST IF has issue"
903 exit
904
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000905 parsedResult = json.loads(result)
906
907 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000908 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
909 src_port = parsedResult['dataPath']['srcPort']['port']['value']
910 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
911 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000912# 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 +0000913
914 if src_dpid in core_switches:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700915 src_host = controllers[0]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000916 else:
917 hostid=int(src_dpid.split(':')[-2])
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700918 src_host = controllers[hostid-1]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000919
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700920 if dst_dpid in core_switches:
921 dst_host = controllers[0]
922 else:
923 hostid=int(dst_dpid.split(':')[-2])
924 dst_host = controllers[hostid-1]
925
926# /runiperf.sh <flowid> <src_dpid> <dst_dpid> svr|client <proto>/<duration>/<interval>/<samples>
927 protocol="udp"
928 interval=0.1
929 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)
930 print cmd_string
931 os.popen(cmd_string)
932
933 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 +0000934 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000935 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000936
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000937 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000938
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000939#* Get Iperf Throughput
940#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000941@app.route("/gui/iperf/rate/<flow_id>")
942def iperf_rate(flow_id):
Tim Lindberga6c04172013-04-05 17:34:05 -0700943 if (TESTBED == "hw"):
944 return "{}"
945
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000946 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000947 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
948 print command
949 result = os.popen(command).read()
950 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000951 resp = Response(result, status=400, mimetype='text/html')
952 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000953 except:
954 print "REST IF has issue"
955 exit
956
957 parsedResult = json.loads(result)
958
959 flowId = int(parsedResult['flowId']['value'], 16)
960 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
961 src_port = parsedResult['dataPath']['srcPort']['port']['value']
962 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
963 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
964
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700965 if dst_dpid in core_switches:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000966 host = controllers[0]
967 else:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700968 hostid=int(dst_dpid.split(':')[-2])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000969 host = controllers[hostid-1]
970
971 try:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700972 command = "curl -s http://%s:%s/log/iperfsvr_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000973 print command
974 result = os.popen(command).read()
975 except:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000976 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000977
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +0000978 if len(result) == 0:
979 resp = Response(result, status=400, mimetype='text/html')
980 return "no iperf file found (flowid %s)" % flow_id;
981 else:
982 resp = Response(result, status=200, mimetype='application/json')
983 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000984
985
Ubuntu82b8a832013-02-06 22:00:11 +0000986if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000987 random.seed()
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +0000988 read_config()
Ubuntu82b8a832013-02-06 22:00:11 +0000989 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000990# 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")
991# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
992# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
993# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
994# 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 +0000995# print "-- query all switches --"
996# query_switch()
997# print "-- query topo --"
998# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000999# link_change(1,2,3,4)
1000 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001001# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +00001002# print "-- query all devices --"
1003# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001004# iperf_start(1,10,15)
1005# iperf_rate(1)
1006 switches()
Ubuntu82b8a832013-02-06 22:00:11 +00001007 else:
1008 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +00001009 app.run(threaded=True, host="0.0.0.0", port=9000)