blob: 036776f5ad8b9bf9b97c5c7dd83589917b183551 [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
Paul Greysone6266b92013-04-09 23:15:27 -0700200@app.route("/proxy/gui/switchctrl/<cmd>")
201def proxy_switch_controller_setting(cmd):
202 try:
203 command = "curl -s %s/gui/switchctrl/%s" % (ONOS_GUI3_CONTROL_HOST, cmd)
204 print command
205 result = os.popen(command).read()
206 except:
207 print "REST IF has issue"
208 exit
209
210 resp = Response(result, status=200, mimetype='application/json')
211 return resp
212
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700213
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000214###### ONOS RESET API ##############################
215## Worker Func ###
216def get_json(url):
217 code = 200
218 try:
219 command = "curl -s %s" % (url)
220 result = os.popen(command).read()
Paul Greysonc090d142013-04-09 16:59:03 -0700221 parsedResult = json.loads(result)
Masayoshi Kobayashi8ac33362013-04-05 03:17:26 +0000222 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
223 print "REST %s returned code %s" % (command, parsedResult['code'])
224 code=500
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000225 except:
226 print "REST IF %s has issue" % command
227 result = ""
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000228 code = 500
229
230 return (code, result)
231
232def pick_host():
233 if LB == True:
234 nr_host=len(controllers)
235 r=random.randint(0, nr_host - 1)
236 host=controllers[r]
237 else:
238 host=ONOS_DEFAULT_HOST
Paul Greysonc090d142013-04-09 16:59:03 -0700239
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000240 return "http://" + host + ":8080"
241
242## Switch ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000243@app.route("/wm/core/topology/switches/all/json")
244def switches():
245 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000246 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000247 else:
248 host = ONOS_GUI3_HOST
249
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000250 url ="%s/wm/core/topology/switches/all/json" % (host)
251 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000252
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000253 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000254 return resp
255
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000256## Link ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000257@app.route("/wm/core/topology/links/json")
258def links():
259 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000260 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000261 else:
262 host = ONOS_GUI3_HOST
263
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000264 url ="%s/wm/core/topology/links/json" % (host)
265 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000266
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000267 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000268 return resp
269
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000270## FlowSummary ##
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000271@app.route("/wm/flow/getsummary/<start>/<range>/json")
272def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000273 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/flow/getsummary/%s/%s/json" % (host, start, range)
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
284@app.route("/wm/registry/controllers/json")
285def registry_controllers():
286 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000287 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000288 else:
289 host = ONOS_GUI3_HOST
290
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000291 url= "%s/wm/registry/controllers/json" % (host)
292 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000293
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000294 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000295 return resp
296
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000297
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000298@app.route("/wm/registry/switches/json")
299def registry_switches():
300 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000301 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000302 else:
303 host = ONOS_GUI3_HOST
304
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000305 url="%s/wm/registry/switches/json" % (host)
306 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000307
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000308 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000309 return resp
310
Ubuntu82b8a832013-02-06 22:00:11 +0000311def node_id(switch_array, dpid):
312 id = -1
313 for i, val in enumerate(switch_array):
314 if val['name'] == dpid:
315 id = i
316 break
317
318 return id
319
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000320## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000321@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000322def topology_for_gui():
323 try:
324 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
325 result = os.popen(command).read()
326 parsedResult = json.loads(result)
327 except:
328 log_error("REST IF has issue: %s" % command)
329 log_error("%s" % result)
330 sys.exit(0)
331
332 topo = {}
333 switches = []
334 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000335 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000336
337 for v in parsedResult:
338 if v.has_key('dpid'):
339# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
340 dpid = str(v['dpid'])
341 state = str(v['state'])
342 sw = {}
343 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000344 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000345
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000346 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000347 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000348 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000349
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000350 try:
351 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
352 result = os.popen(command).read()
353 parsedResult = json.loads(result)
354 except:
355 log_error("REST IF has issue: %s" % command)
356 log_error("%s" % result)
357
358 for key in parsedResult:
359 dpid = key
360 ctrl = parsedResult[dpid][0]['controllerId']
361 sw_id = node_id(switches, dpid)
362 if sw_id != -1:
363 if switches[sw_id]['group'] != 0:
364 switches[sw_id]['group'] = controllers.index(ctrl) + 1
365
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000366 try:
367 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000368# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000369 p1=1
370 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000371# v2 = "00:00:00:00:00:0d:00:d3"
372 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000373 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
374 result = os.popen(command).read()
375 parsedResult = json.loads(result)
376 except:
377 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000378 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000379
Ubuntu765deff2013-02-28 18:39:13 +0000380 path = []
381 if parsedResult.has_key('flowEntries'):
382 flowEntries= parsedResult['flowEntries']
383 for i, v in enumerate(flowEntries):
384 if i < len(flowEntries) - 1:
385 sdpid= flowEntries[i]['dpid']['value']
386 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700387 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000388
Ubuntu82b8a832013-02-06 22:00:11 +0000389 try:
390 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
391 result = os.popen(command).read()
392 parsedResult = json.loads(result)
393 except:
394 log_error("REST IF has issue: %s" % command)
395 log_error("%s" % result)
396 sys.exit(0)
397
398 for v in parsedResult:
399 link = {}
400 if v.has_key('dst-switch'):
401 dst_dpid = str(v['dst-switch'])
402 dst_id = node_id(switches, dst_dpid)
403 if v.has_key('src-switch'):
404 src_dpid = str(v['src-switch'])
405 src_id = node_id(switches, src_dpid)
406 link['source'] = src_id
407 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000408
409 onpath = 0
410 for (s,d) in path:
411 if s == v['src-switch'] and d == v['dst-switch']:
412 onpath = 1
413 break
414 link['type'] = onpath
415
Ubuntu82b8a832013-02-06 22:00:11 +0000416 links.append(link)
417
418 topo['nodes'] = switches
419 topo['links'] = links
420
Ubuntu82b8a832013-02-06 22:00:11 +0000421 js = json.dumps(topo)
422 resp = Response(js, status=200, mimetype='application/json')
423 return resp
424
Ubuntuaea2a682013-02-08 08:30:10 +0000425#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
426#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
427@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
428def shortest_path(v1, p1, v2, p2):
429 try:
430 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
431 result = os.popen(command).read()
432 parsedResult = json.loads(result)
433 except:
434 log_error("REST IF has issue: %s" % command)
435 log_error("%s" % result)
436 sys.exit(0)
437
438 topo = {}
439 switches = []
440 links = []
441
442 for v in parsedResult:
443 if v.has_key('dpid'):
444 dpid = str(v['dpid'])
445 state = str(v['state'])
446 sw = {}
447 sw['name']=dpid
448 if str(v['state']) == "ACTIVE":
449 if dpid[-2:-1] == "a":
450 sw['group']=1
451 if dpid[-2:-1] == "b":
452 sw['group']=2
453 if dpid[-2:-1] == "c":
454 sw['group']=3
455 if str(v['state']) == "INACTIVE":
456 sw['group']=0
457
458 switches.append(sw)
459
460 try:
461 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
462 result = os.popen(command).read()
463 parsedResult = json.loads(result)
464 except:
465 log_error("No route")
466 parsedResult = []
467# exit(1)
468
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700469 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000470 for i, v in enumerate(parsedResult):
471 if i < len(parsedResult) - 1:
472 sdpid= parsedResult[i]['switch']
473 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700474 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000475
476 try:
477 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
478 result = os.popen(command).read()
479 parsedResult = json.loads(result)
480 except:
481 log_error("REST IF has issue: %s" % command)
482 log_error("%s" % result)
483 sys.exit(0)
484
485 for v in parsedResult:
486 link = {}
487 if v.has_key('dst-switch'):
488 dst_dpid = str(v['dst-switch'])
489 dst_id = node_id(switches, dst_dpid)
490 if v.has_key('src-switch'):
491 src_dpid = str(v['src-switch'])
492 src_id = node_id(switches, src_dpid)
493 link['source'] = src_id
494 link['target'] = dst_id
495 onpath = 0
496 for (s,d) in path:
497 if s == v['src-switch'] and d == v['dst-switch']:
498 onpath = 1
499 break
500
501 link['type'] = onpath
502 links.append(link)
503
504 topo['nodes'] = switches
505 topo['links'] = links
506
Ubuntuaea2a682013-02-08 08:30:10 +0000507 js = json.dumps(topo)
508 resp = Response(js, status=200, mimetype='application/json')
509 return resp
510
Ubuntu82b8a832013-02-06 22:00:11 +0000511@app.route("/wm/core/controller/switches/json")
512def query_switch():
513 try:
514 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
515# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000516 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000517 result = os.popen(command).read()
518 parsedResult = json.loads(result)
519 except:
520 log_error("REST IF has issue: %s" % command)
521 log_error("%s" % result)
522 sys.exit(0)
523
524# print command
525# print result
526 switches_ = []
527 for v in parsedResult:
528 if v.has_key('dpid'):
529 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
530 dpid = str(v['dpid'])
531 state = str(v['state'])
532 sw = {}
533 sw['dpid']=dpid
534 sw['active']=state
535 switches_.append(sw)
536
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000537# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000538 js = json.dumps(switches_)
539 resp = Response(js, status=200, mimetype='application/json')
540 return resp
541
542@app.route("/wm/device/")
543def devices():
544 try:
545 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
546 result = os.popen(command).read()
547 parsedResult = json.loads(result)['results']
548 except:
549 log_error("REST IF has issue: %s" % command)
550 log_error("%s" % result)
551 sys.exit(0)
552
553 devices = []
554 for v in parsedResult:
555 dl_addr = v['dl_addr']
556 nw_addr = v['nw_addr']
557 vertex = v['_id']
558 mac = []
559 mac.append(dl_addr)
560 ip = []
561 ip.append(nw_addr)
562 device = {}
563 device['entryClass']="DefaultEntryClass"
564 device['mac']=mac
565 device['ipv4']=ip
566 device['vlan']=[]
567 device['lastSeen']=0
568 attachpoints =[]
569
570 port, dpid = deviceV_to_attachpoint(vertex)
571 attachpoint = {}
572 attachpoint['port']=port
573 attachpoint['switchDPID']=dpid
574 attachpoints.append(attachpoint)
575 device['attachmentPoint']=attachpoints
576 devices.append(device)
577
Ubuntu82b8a832013-02-06 22:00:11 +0000578 js = json.dumps(devices)
579 resp = Response(js, status=200, mimetype='application/json')
580 return resp
581
582#{"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}
583
Ubuntu82b8a832013-02-06 22:00:11 +0000584## return fake stat for now
585@app.route("/wm/core/switch/<switchId>/<statType>/json")
586def switch_stat(switchId, statType):
587 if statType == "desc":
588 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
589 ret = {}
590 ret[switchId]=desc
591 elif statType == "aggregate":
592 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
593 ret = {}
594 ret[switchId]=aggr
595 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700596 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000597
598 js = json.dumps(ret)
599 resp = Response(js, status=200, mimetype='application/json')
600 return resp
601
602
603@app.route("/wm/topology/links/json")
604def query_links():
605 try:
606 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000607 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000608 result = os.popen(command).read()
609 parsedResult = json.loads(result)['results']
610 except:
611 log_error("REST IF has issue: %s" % command)
612 log_error("%s" % result)
613 sys.exit(0)
614
615 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000616# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000617 sport = []
618 links = []
619 for v in parsedResult:
620 srcport = v['_id']
621 try:
622 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
623 print command
624 result = os.popen(command).read()
625 linkResults = json.loads(result)['results']
626 except:
627 log_error("REST IF has issue: %s" % command)
628 log_error("%s" % result)
629 sys.exit(0)
630
631 for p in linkResults:
632 if p.has_key('type') and p['type'] == "port":
633 dstport = p['_id']
634 (sport, sdpid) = portV_to_port_dpid(srcport)
635 (dport, ddpid) = portV_to_port_dpid(dstport)
636 link = {}
637 link["src-switch"]=sdpid
638 link["src-port"]=sport
639 link["src-port-state"]=0
640 link["dst-switch"]=ddpid
641 link["dst-port"]=dport
642 link["dst-port-state"]=0
643 link["type"]="internal"
644 links.append(link)
645
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000646# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000647 js = json.dumps(links)
648 resp = Response(js, status=200, mimetype='application/json')
649 return resp
650
Ubuntuc016ba12013-02-27 21:53:41 +0000651@app.route("/controller_status")
652def controller_status():
Tim Lindberg201ade22013-04-05 11:52:08 -0700653# onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
654 onos_check="cd; onos status %s | grep %s | awk '{print $2}'"
Ubuntuc016ba12013-02-27 21:53:41 +0000655 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
656
657 cont_status=[]
658 for i in controllers:
659 status={}
660 onos=os.popen(onos_check % i).read()[:-1]
Tim Lindberg201ade22013-04-05 11:52:08 -0700661 onos=os.popen(onos_check % (i, i.lower())).read()[:-1]
Ubuntuc016ba12013-02-27 21:53:41 +0000662 status["name"]=i
663 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000664 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000665 cont_status.append(status)
666
667 js = json.dumps(cont_status)
668 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000669 return resp
670
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000671### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000672@app.route("/gui/controller/<cmd>/<controller_name>")
673def controller_status_change(cmd, controller_name):
Tim Lindberg201ade22013-04-05 11:52:08 -0700674# start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
675# stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
676 start_onos="cd; onos start %s" % (controller_name[-1:])
677 stop_onos="cd; onos stop %s" % (controller_name[-1:])
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000678
679 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000680 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000681 ret = "controller %s is up" % (controller_name)
682 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000683 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000684 ret = "controller %s is down" % (controller_name)
685
686 return ret
687
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000688@app.route("/gui/switchctrl/<cmd>")
689def switch_controller_setting(cmd):
690 if cmd =="local":
691 print "All aggr switches connects to local controller only"
692 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700693 if (TESTBED == "sw"):
Paul Greysonc090d142013-04-09 16:59:03 -0700694 for i in range(0, len(controllers)):
Tim Lindberg201ade22013-04-05 11:52:08 -0700695 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-local.sh'" % (controllers[i])
696 result += os.popen(cmd_string).read()
697 else:
698 cmd_string="cd; switch local"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000699 result += os.popen(cmd_string).read()
700 elif cmd =="all":
701 print "All aggr switches connects to all controllers except for core controller"
702 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700703 if (TESTBED == "sw"):
Paul Greysonc090d142013-04-09 16:59:03 -0700704 for i in range(0, len(controllers)):
Tim Lindberg201ade22013-04-05 11:52:08 -0700705 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i])
706 result += os.popen(cmd_string).read()
Paul Greysonc090d142013-04-09 16:59:03 -0700707 else:
Tim Lindberg201ade22013-04-05 11:52:08 -0700708 cmd_string="cd; switch all"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000709 result += os.popen(cmd_string).read()
710
711 return result
712
713
714
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000715@app.route("/gui/switch/<cmd>/<dpid>")
716def switch_status_change(cmd, dpid):
Tim Lindberg201ade22013-04-05 11:52:08 -0700717 result = ""
718 if (TESTBED == "hw"):
719 return result
720
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000721 r = re.compile(':')
722 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000723 host=controllers[0]
724 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
725 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000726 print "cmd_string"
727
728 if cmd =="up" or cmd=="down":
729 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000730 os.popen(cmd_string)
731 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000732
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000733 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000734
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000735#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000736#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000737@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
738def link_up(src_dpid, src_port, dst_dpid, dst_port):
Tim Lindberg201ade22013-04-05 11:52:08 -0700739 result = ""
740
Paul Greysonc090d142013-04-09 16:59:03 -0700741 if (TESTBED == "sw"):
Tim Lindberg201ade22013-04-05 11:52:08 -0700742 result = link_up_sw(src_dpid, src_port, dst_dpid, dst_port)
743 else:
744 result = link_up_hw(src_dpid, src_port, dst_dpid, dst_port)
745 return result
746
747# Link up on software testbed
748def link_up_sw(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000749
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000750 cmd = 'up'
751 result=""
752
Paul Greyson472da4c2013-03-28 11:43:17 -0700753 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000754 if dpid in core_switches:
755 host = controllers[0]
756 src_ports = [1, 2, 3, 4, 5]
757 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000758 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000759 host = controllers[hostid-1]
760 if hostid == 2 :
761 src_ports = [51]
762 else :
763 src_ports = [26]
764
765 for port in src_ports :
766 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
767 print cmd_string
768 res=os.popen(cmd_string).read()
769 result = result + ' ' + res
770
771 return result
772
Tim Lindberg201ade22013-04-05 11:52:08 -0700773# Link up on hardware testbed
774def link_up_hw(src_dpid, src_port, dst_dpid, dst_port):
775
776 port1 = src_port
777 port2 = dst_port
778 if src_dpid == "00:00:00:00:ba:5e:ba:11":
779 if dst_dpid == "00:00:00:08:a2:08:f9:01":
780 port1 = 24
781 port2 = 24
782 elif dst_dpid == "00:01:00:16:97:08:9a:46":
783 port1 = 23
784 port2 = 23
785 elif src_dpid == "00:00:00:00:ba:5e:ba:13":
786 if dst_dpid == "00:00:20:4e:7f:51:8a:35":
787 port1 = 22
788 port2 = 22
789 elif dst_dpid == "00:00:00:00:00:00:ba:12":
790 port1 = 23
791 port2 = 23
792 elif src_dpid == "00:00:00:00:00:00:ba:12":
793 if dst_dpid == "00:00:00:00:ba:5e:ba:13":
794 port1 = 23
795 port2 = 23
796 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
797 port1 = 22
798 port2 = 22
799 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
800 port1 = 24
801 port2 = 21
802 elif src_dpid == "00:01:00:16:97:08:9a:46":
803 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
804 port1 = 23
805 port2 = 23
806 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
807 port1 = 24
808 port2 = 24
809 elif src_dpid == "00:00:00:08:a2:08:f9:01":
810 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
811 port1 = 24
812 port2 = 24
813 elif dst_dpid == "00:00:00:00:00:00:ba:12":
814 port1 = 22
815 port2 = 22
816 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
817 port1 = 23
818 port2 = 23
819 elif src_dpid == "00:00:20:4e:7f:51:8a:35":
820 if dst_dpid == "00:00:00:00:00:00:ba:12":
821 port1 = 21
822 port2 = 24
823 elif dst_dpid == "00:00:00:00:ba:5e:ba:13":
824 port1 = 22
825 port2 = 22
826 elif dst_dpid == "00:01:00:16:97:08:9a:46":
827 port1 = 24
828 port2 = 24
829 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
830 port1 = 23
831 port2 = 23
832
833 cmd = 'up'
834 result=""
835 host = controllers[0]
836 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % (src_dpid, port1, cmd)
837 print cmd_string
838 res=os.popen(cmd_string).read()
839 result = result + ' ' + res
840 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % (dst_dpid, port2, cmd)
841 print cmd_string
842 res=os.popen(cmd_string).read()
843 result = result + ' ' + res
844
845
846 return result
847
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000848
849#* Link Down
850#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000851@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000852def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
853
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000854 if src_dpid in core_switches:
855 host = controllers[0]
856 else:
857 hostid=int(src_dpid.split(':')[-2])
858 host = controllers[hostid-1]
859
Tim Lindberg201ade22013-04-05 11:52:08 -0700860 if (TESTBED == "sw"):
861 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
862 else:
Tim Lindberga6c04172013-04-05 17:34:05 -0700863 if ( src_dpid == "00:00:00:08:a2:08:f9:01" ):
864 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % ( dst_dpid, dst_port, cmd)
865 else:
866 cmd_string="~/ONOS/scripts/link.sh %s %s %s " % ( src_dpid, src_port, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000867 print cmd_string
868
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000869 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000870
871 return result
872
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000873#* Create Flow
874#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000875#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 +0000876@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000877def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
878 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
879 print command
880 ret = os.popen(command).read()
881 if ret == "":
882 flow_nr=0
883 else:
884 flow_nr=int(ret)
885
886 flow_nr += 1
Umesh Krishnaswamyf22d3ed2013-04-03 11:57:54 -0700887 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 +0000888 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 +0000889 print command
890 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000891 errcode1 = os.popen(command1).read()
892 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000893
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000894#* Delete Flow
895#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000896@app.route("/gui/delflow/<flow_id>")
897def del_flow(flow_id):
898 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
899 print command
900 errcode = os.popen(command).read()
901 return errcode
902
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000903#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700904#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000905@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
906def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000907 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000908 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000909 print command
910 result = os.popen(command).read()
911 if len(result) == 0:
912 print "No Flow found"
913 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000914 except:
915 print "REST IF has issue"
916 exit
917
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000918 parsedResult = json.loads(result)
919
920 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000921 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
922 src_port = parsedResult['dataPath']['srcPort']['port']['value']
923 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
924 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000925# 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 +0000926
927 if src_dpid in core_switches:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700928 src_host = controllers[0]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000929 else:
930 hostid=int(src_dpid.split(':')[-2])
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700931 src_host = controllers[hostid-1]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000932
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700933 if dst_dpid in core_switches:
934 dst_host = controllers[0]
935 else:
936 hostid=int(dst_dpid.split(':')[-2])
937 dst_host = controllers[hostid-1]
938
939# /runiperf.sh <flowid> <src_dpid> <dst_dpid> svr|client <proto>/<duration>/<interval>/<samples>
940 protocol="udp"
941 interval=0.1
942 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)
943 print cmd_string
944 os.popen(cmd_string)
945
946 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 +0000947 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000948 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000949
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000950 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000951
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000952#* Get Iperf Throughput
953#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000954@app.route("/gui/iperf/rate/<flow_id>")
955def iperf_rate(flow_id):
Tim Lindberga6c04172013-04-05 17:34:05 -0700956 if (TESTBED == "hw"):
957 return "{}"
958
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000959 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000960 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
961 print command
962 result = os.popen(command).read()
963 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000964 resp = Response(result, status=400, mimetype='text/html')
965 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000966 except:
967 print "REST IF has issue"
968 exit
969
970 parsedResult = json.loads(result)
971
972 flowId = int(parsedResult['flowId']['value'], 16)
973 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
974 src_port = parsedResult['dataPath']['srcPort']['port']['value']
975 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
976 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
977
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700978 if dst_dpid in core_switches:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000979 host = controllers[0]
980 else:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700981 hostid=int(dst_dpid.split(':')[-2])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000982 host = controllers[hostid-1]
983
984 try:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700985 command = "curl -s http://%s:%s/log/iperfsvr_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000986 print command
987 result = os.popen(command).read()
988 except:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000989 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000990
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +0000991 if len(result) == 0:
992 resp = Response(result, status=400, mimetype='text/html')
993 return "no iperf file found (flowid %s)" % flow_id;
994 else:
995 resp = Response(result, status=200, mimetype='application/json')
996 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000997
998
Ubuntu82b8a832013-02-06 22:00:11 +0000999if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001000 random.seed()
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +00001001 read_config()
Ubuntu82b8a832013-02-06 22:00:11 +00001002 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001003# 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")
1004# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
1005# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
1006# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
1007# 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 +00001008# print "-- query all switches --"
1009# query_switch()
1010# print "-- query topo --"
1011# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001012# link_change(1,2,3,4)
1013 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001014# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +00001015# print "-- query all devices --"
1016# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001017# iperf_start(1,10,15)
1018# iperf_rate(1)
1019 switches()
Ubuntu82b8a832013-02-06 22:00:11 +00001020 else:
1021 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +00001022 app.run(threaded=True, host="0.0.0.0", port=9000)