blob: 976737cb0ab571e3049f3f237e838119834ebffe [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'])
Paul Greysonc090d142013-04-09 16:59:03 -070081@app.route('/ons-demo/d3/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000082@app.route('/ons-demo/css/<filename>', methods=['GET'])
83@app.route('/ons-demo/assets/<filename>', methods=['GET'])
84@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000085def return_file(filename="index.html"):
86 if request.path == "/":
87 fullpath = "./index.html"
88 else:
89 fullpath = str(request.path)[1:]
90
Paul Greysonc090d142013-04-09 16:59:03 -070091 try:
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +000092 open(fullpath)
93 except:
94 response = make_response("Cannot find a file: %s" % (fullpath), 500)
95 response.headers["Content-type"] = "text/html"
96 return response
97
Ubuntu82b8a832013-02-06 22:00:11 +000098 response = make_response(open(fullpath).read())
99 suffix = fullpath.split(".")[-1]
100
101 if suffix == "html" or suffix == "htm":
102 response.headers["Content-type"] = "text/html"
103 elif suffix == "js":
104 response.headers["Content-type"] = "application/javascript"
105 elif suffix == "css":
106 response.headers["Content-type"] = "text/css"
107 elif suffix == "png":
108 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -0700109 elif suffix == "svg":
110 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +0000111
112 return response
113
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000114## Proxy ##
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700115@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
116def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
117 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -0700118 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 -0700119 print command
120 result = os.popen(command).read()
121 except:
122 print "REST IF has issue"
123 exit
124
125 resp = Response(result, status=200, mimetype='application/json')
126 return resp
127
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000128@app.route("/proxy/gui/switchctrl/<cmd>")
129def proxy_switch_controller_setting(cmd):
130 try:
131 command = "curl -s %s/gui/switchctrl/%s" % (ONOS_GUI3_CONTROL_HOST, cmd)
132 print command
133 result = os.popen(command).read()
134 except:
135 print "REST IF has issue"
136 exit
137
138 resp = Response(result, status=200, mimetype='application/json')
139 return resp
140
Paul Greyson8d1c6362013-03-27 13:05:24 -0700141@app.route("/proxy/gui/switch/<cmd>/<dpid>")
142def proxy_switch_status_change(cmd, dpid):
143 try:
144 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
145 print command
146 result = os.popen(command).read()
147 except:
148 print "REST IF has issue"
149 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700150
Paul Greyson8d1c6362013-03-27 13:05:24 -0700151 resp = Response(result, status=200, mimetype='application/json')
152 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700153
Paul Greyson2913af82013-03-27 14:53:17 -0700154@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
155def proxy_controller_status_change(cmd, controller_name):
156 try:
157 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
158 print command
159 result = os.popen(command).read()
160 except:
161 print "REST IF has issue"
162 exit
163
164 resp = Response(result, status=200, mimetype='application/json')
165 return resp
166
Paul Greyson472da4c2013-03-28 11:43:17 -0700167@app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
168def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
169 try:
170 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)
171 print command
172 result = os.popen(command).read()
173 except:
174 print "REST IF has issue"
175 exit
176
177 resp = Response(result, status=200, mimetype='application/json')
178 return resp
179
Paul Greyson6f918402013-03-28 12:18:30 -0700180@app.route("/proxy/gui/delflow/<flow_id>")
181def proxy_del_flow(flow_id):
182 try:
183 command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
184 print command
185 result = os.popen(command).read()
186 except:
187 print "REST IF has issue"
188 exit
189
190 resp = Response(result, status=200, mimetype='application/json')
191 return resp
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000192
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700193@app.route("/proxy/gui/iperf/start/<flow_id>/<duration>/<samples>")
194def proxy_iperf_start(flow_id,duration,samples):
195 try:
196 command = "curl -s %s/gui/iperf/start/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id, duration, samples)
197 print command
198 result = os.popen(command).read()
199 except:
200 print "REST IF has issue"
201 exit
202
203 resp = Response(result, status=200, mimetype='application/json')
204 return resp
205
206@app.route("/proxy/gui/iperf/rate/<flow_id>")
207def proxy_iperf_rate(flow_id):
208 try:
209 command = "curl -s %s/gui/iperf/rate/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
210 print command
211 result = os.popen(command).read()
212 except:
213 print "REST IF has issue"
214 exit
215
216 resp = Response(result, status=200, mimetype='application/json')
217 return resp
218
Paul Greysone6266b92013-04-09 23:15:27 -0700219@app.route("/proxy/gui/switchctrl/<cmd>")
220def proxy_switch_controller_setting(cmd):
221 try:
222 command = "curl -s %s/gui/switchctrl/%s" % (ONOS_GUI3_CONTROL_HOST, cmd)
223 print command
224 result = os.popen(command).read()
225 except:
226 print "REST IF has issue"
227 exit
228
229 resp = Response(result, status=200, mimetype='application/json')
230 return resp
231
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700232
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000233###### ONOS RESET API ##############################
234## Worker Func ###
235def get_json(url):
236 code = 200
237 try:
238 command = "curl -s %s" % (url)
239 result = os.popen(command).read()
Paul Greysonc090d142013-04-09 16:59:03 -0700240 parsedResult = json.loads(result)
Masayoshi Kobayashi8ac33362013-04-05 03:17:26 +0000241 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
242 print "REST %s returned code %s" % (command, parsedResult['code'])
243 code=500
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000244 except:
245 print "REST IF %s has issue" % command
246 result = ""
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000247 code = 500
248
249 return (code, result)
250
251def pick_host():
252 if LB == True:
253 nr_host=len(controllers)
254 r=random.randint(0, nr_host - 1)
255 host=controllers[r]
256 else:
257 host=ONOS_DEFAULT_HOST
Paul Greysonc090d142013-04-09 16:59:03 -0700258
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000259 return "http://" + host + ":8080"
260
261## Switch ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000262@app.route("/wm/core/topology/switches/all/json")
263def switches():
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/switches/all/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## Link ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000276@app.route("/wm/core/topology/links/json")
277def links():
278 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/core/topology/links/json" % (host)
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
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000289## FlowSummary ##
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000290@app.route("/wm/flow/getsummary/<start>/<range>/json")
291def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000292 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000293 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000294 else:
295 host = ONOS_GUI3_HOST
296
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000297 url ="%s/wm/flow/getsummary/%s/%s/json" % (host, start, range)
298 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000299
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000300 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000301 return resp
302
303@app.route("/wm/registry/controllers/json")
304def registry_controllers():
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/controllers/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
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000316
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000317@app.route("/wm/registry/switches/json")
318def registry_switches():
319 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000320 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000321 else:
322 host = ONOS_GUI3_HOST
323
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000324 url="%s/wm/registry/switches/json" % (host)
325 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000326
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000327 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000328 return resp
329
Ubuntu82b8a832013-02-06 22:00:11 +0000330def node_id(switch_array, dpid):
331 id = -1
332 for i, val in enumerate(switch_array):
333 if val['name'] == dpid:
334 id = i
335 break
336
337 return id
338
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000339## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000340@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000341def topology_for_gui():
342 try:
343 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
344 result = os.popen(command).read()
345 parsedResult = json.loads(result)
346 except:
347 log_error("REST IF has issue: %s" % command)
348 log_error("%s" % result)
349 sys.exit(0)
350
351 topo = {}
352 switches = []
353 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000354 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000355
356 for v in parsedResult:
357 if v.has_key('dpid'):
358# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
359 dpid = str(v['dpid'])
360 state = str(v['state'])
361 sw = {}
362 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000363 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000364
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000365 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000366 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000367 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000368
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000369 try:
370 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
371 result = os.popen(command).read()
372 parsedResult = json.loads(result)
373 except:
374 log_error("REST IF has issue: %s" % command)
375 log_error("%s" % result)
376
377 for key in parsedResult:
378 dpid = key
379 ctrl = parsedResult[dpid][0]['controllerId']
380 sw_id = node_id(switches, dpid)
381 if sw_id != -1:
382 if switches[sw_id]['group'] != 0:
383 switches[sw_id]['group'] = controllers.index(ctrl) + 1
384
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000385 try:
386 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000387# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000388 p1=1
389 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000390# v2 = "00:00:00:00:00:0d:00:d3"
391 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000392 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
393 result = os.popen(command).read()
394 parsedResult = json.loads(result)
395 except:
396 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000397 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000398
Ubuntu765deff2013-02-28 18:39:13 +0000399 path = []
400 if parsedResult.has_key('flowEntries'):
401 flowEntries= parsedResult['flowEntries']
402 for i, v in enumerate(flowEntries):
403 if i < len(flowEntries) - 1:
404 sdpid= flowEntries[i]['dpid']['value']
405 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700406 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000407
Ubuntu82b8a832013-02-06 22:00:11 +0000408 try:
409 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
410 result = os.popen(command).read()
411 parsedResult = json.loads(result)
412 except:
413 log_error("REST IF has issue: %s" % command)
414 log_error("%s" % result)
415 sys.exit(0)
416
417 for v in parsedResult:
418 link = {}
419 if v.has_key('dst-switch'):
420 dst_dpid = str(v['dst-switch'])
421 dst_id = node_id(switches, dst_dpid)
422 if v.has_key('src-switch'):
423 src_dpid = str(v['src-switch'])
424 src_id = node_id(switches, src_dpid)
425 link['source'] = src_id
426 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000427
428 onpath = 0
429 for (s,d) in path:
430 if s == v['src-switch'] and d == v['dst-switch']:
431 onpath = 1
432 break
433 link['type'] = onpath
434
Ubuntu82b8a832013-02-06 22:00:11 +0000435 links.append(link)
436
437 topo['nodes'] = switches
438 topo['links'] = links
439
Ubuntu82b8a832013-02-06 22:00:11 +0000440 js = json.dumps(topo)
441 resp = Response(js, status=200, mimetype='application/json')
442 return resp
443
Ubuntuaea2a682013-02-08 08:30:10 +0000444#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
445#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
446@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
447def shortest_path(v1, p1, v2, p2):
448 try:
449 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
450 result = os.popen(command).read()
451 parsedResult = json.loads(result)
452 except:
453 log_error("REST IF has issue: %s" % command)
454 log_error("%s" % result)
455 sys.exit(0)
456
457 topo = {}
458 switches = []
459 links = []
460
461 for v in parsedResult:
462 if v.has_key('dpid'):
463 dpid = str(v['dpid'])
464 state = str(v['state'])
465 sw = {}
466 sw['name']=dpid
467 if str(v['state']) == "ACTIVE":
468 if dpid[-2:-1] == "a":
469 sw['group']=1
470 if dpid[-2:-1] == "b":
471 sw['group']=2
472 if dpid[-2:-1] == "c":
473 sw['group']=3
474 if str(v['state']) == "INACTIVE":
475 sw['group']=0
476
477 switches.append(sw)
478
479 try:
480 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
481 result = os.popen(command).read()
482 parsedResult = json.loads(result)
483 except:
484 log_error("No route")
485 parsedResult = []
486# exit(1)
487
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700488 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000489 for i, v in enumerate(parsedResult):
490 if i < len(parsedResult) - 1:
491 sdpid= parsedResult[i]['switch']
492 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700493 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000494
495 try:
496 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
497 result = os.popen(command).read()
498 parsedResult = json.loads(result)
499 except:
500 log_error("REST IF has issue: %s" % command)
501 log_error("%s" % result)
502 sys.exit(0)
503
504 for v in parsedResult:
505 link = {}
506 if v.has_key('dst-switch'):
507 dst_dpid = str(v['dst-switch'])
508 dst_id = node_id(switches, dst_dpid)
509 if v.has_key('src-switch'):
510 src_dpid = str(v['src-switch'])
511 src_id = node_id(switches, src_dpid)
512 link['source'] = src_id
513 link['target'] = dst_id
514 onpath = 0
515 for (s,d) in path:
516 if s == v['src-switch'] and d == v['dst-switch']:
517 onpath = 1
518 break
519
520 link['type'] = onpath
521 links.append(link)
522
523 topo['nodes'] = switches
524 topo['links'] = links
525
Ubuntuaea2a682013-02-08 08:30:10 +0000526 js = json.dumps(topo)
527 resp = Response(js, status=200, mimetype='application/json')
528 return resp
529
Ubuntu82b8a832013-02-06 22:00:11 +0000530@app.route("/wm/core/controller/switches/json")
531def query_switch():
532 try:
533 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
534# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000535 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000536 result = os.popen(command).read()
537 parsedResult = json.loads(result)
538 except:
539 log_error("REST IF has issue: %s" % command)
540 log_error("%s" % result)
541 sys.exit(0)
542
543# print command
544# print result
545 switches_ = []
546 for v in parsedResult:
547 if v.has_key('dpid'):
548 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
549 dpid = str(v['dpid'])
550 state = str(v['state'])
551 sw = {}
552 sw['dpid']=dpid
553 sw['active']=state
554 switches_.append(sw)
555
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000556# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000557 js = json.dumps(switches_)
558 resp = Response(js, status=200, mimetype='application/json')
559 return resp
560
561@app.route("/wm/device/")
562def devices():
563 try:
564 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
565 result = os.popen(command).read()
566 parsedResult = json.loads(result)['results']
567 except:
568 log_error("REST IF has issue: %s" % command)
569 log_error("%s" % result)
570 sys.exit(0)
571
572 devices = []
573 for v in parsedResult:
574 dl_addr = v['dl_addr']
575 nw_addr = v['nw_addr']
576 vertex = v['_id']
577 mac = []
578 mac.append(dl_addr)
579 ip = []
580 ip.append(nw_addr)
581 device = {}
582 device['entryClass']="DefaultEntryClass"
583 device['mac']=mac
584 device['ipv4']=ip
585 device['vlan']=[]
586 device['lastSeen']=0
587 attachpoints =[]
588
589 port, dpid = deviceV_to_attachpoint(vertex)
590 attachpoint = {}
591 attachpoint['port']=port
592 attachpoint['switchDPID']=dpid
593 attachpoints.append(attachpoint)
594 device['attachmentPoint']=attachpoints
595 devices.append(device)
596
Ubuntu82b8a832013-02-06 22:00:11 +0000597 js = json.dumps(devices)
598 resp = Response(js, status=200, mimetype='application/json')
599 return resp
600
601#{"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}
602
Ubuntu82b8a832013-02-06 22:00:11 +0000603## return fake stat for now
604@app.route("/wm/core/switch/<switchId>/<statType>/json")
605def switch_stat(switchId, statType):
606 if statType == "desc":
607 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
608 ret = {}
609 ret[switchId]=desc
610 elif statType == "aggregate":
611 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
612 ret = {}
613 ret[switchId]=aggr
614 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700615 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000616
617 js = json.dumps(ret)
618 resp = Response(js, status=200, mimetype='application/json')
619 return resp
620
621
622@app.route("/wm/topology/links/json")
623def query_links():
624 try:
625 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000626 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000627 result = os.popen(command).read()
628 parsedResult = json.loads(result)['results']
629 except:
630 log_error("REST IF has issue: %s" % command)
631 log_error("%s" % result)
632 sys.exit(0)
633
634 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000635# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000636 sport = []
637 links = []
638 for v in parsedResult:
639 srcport = v['_id']
640 try:
641 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
642 print command
643 result = os.popen(command).read()
644 linkResults = json.loads(result)['results']
645 except:
646 log_error("REST IF has issue: %s" % command)
647 log_error("%s" % result)
648 sys.exit(0)
649
650 for p in linkResults:
651 if p.has_key('type') and p['type'] == "port":
652 dstport = p['_id']
653 (sport, sdpid) = portV_to_port_dpid(srcport)
654 (dport, ddpid) = portV_to_port_dpid(dstport)
655 link = {}
656 link["src-switch"]=sdpid
657 link["src-port"]=sport
658 link["src-port-state"]=0
659 link["dst-switch"]=ddpid
660 link["dst-port"]=dport
661 link["dst-port-state"]=0
662 link["type"]="internal"
663 links.append(link)
664
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000665# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000666 js = json.dumps(links)
667 resp = Response(js, status=200, mimetype='application/json')
668 return resp
669
Ubuntuc016ba12013-02-27 21:53:41 +0000670@app.route("/controller_status")
671def controller_status():
Tim Lindberg201ade22013-04-05 11:52:08 -0700672# onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
673 onos_check="cd; onos status %s | grep %s | awk '{print $2}'"
Ubuntuc016ba12013-02-27 21:53:41 +0000674 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
675
676 cont_status=[]
677 for i in controllers:
678 status={}
679 onos=os.popen(onos_check % i).read()[:-1]
Tim Lindberg201ade22013-04-05 11:52:08 -0700680 onos=os.popen(onos_check % (i, i.lower())).read()[:-1]
Ubuntuc016ba12013-02-27 21:53:41 +0000681 status["name"]=i
682 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000683 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000684 cont_status.append(status)
685
686 js = json.dumps(cont_status)
687 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000688 return resp
689
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000690### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000691@app.route("/gui/controller/<cmd>/<controller_name>")
692def controller_status_change(cmd, controller_name):
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000693 if (TESTBED == "hw"):
Tim Lindberg34c9ff62013-04-10 15:06:17 -0700694 start_onos="/home/admin/bin/onos start %s" % (controller_name[-1:])
695 stop_onos="/home/admin/bin/onos stop %s" % (controller_name[-1:])
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000696 else:
697 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
698 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000699
700 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000701 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000702 ret = "controller %s is up" % (controller_name)
703 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000704 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000705 ret = "controller %s is down" % (controller_name)
706
707 return ret
708
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000709@app.route("/gui/switchctrl/<cmd>")
710def switch_controller_setting(cmd):
711 if cmd =="local":
712 print "All aggr switches connects to local controller only"
713 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700714 if (TESTBED == "sw"):
Paul Greysonc090d142013-04-09 16:59:03 -0700715 for i in range(0, len(controllers)):
Tim Lindberg201ade22013-04-05 11:52:08 -0700716 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-local.sh'" % (controllers[i])
717 result += os.popen(cmd_string).read()
718 else:
719 cmd_string="cd; switch local"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000720 result += os.popen(cmd_string).read()
721 elif cmd =="all":
722 print "All aggr switches connects to all controllers except for core controller"
723 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700724 if (TESTBED == "sw"):
Paul Greysonc090d142013-04-09 16:59:03 -0700725 for i in range(0, len(controllers)):
Tim Lindberg201ade22013-04-05 11:52:08 -0700726 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i])
727 result += os.popen(cmd_string).read()
Paul Greysonc090d142013-04-09 16:59:03 -0700728 else:
Tim Lindberg201ade22013-04-05 11:52:08 -0700729 cmd_string="cd; switch all"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000730 result += os.popen(cmd_string).read()
731
732 return result
733
734
735
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000736@app.route("/gui/switch/<cmd>/<dpid>")
737def switch_status_change(cmd, dpid):
Tim Lindberg201ade22013-04-05 11:52:08 -0700738 result = ""
739 if (TESTBED == "hw"):
740 return result
741
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000742 r = re.compile(':')
743 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000744 host=controllers[0]
745 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
746 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000747 print "cmd_string"
748
749 if cmd =="up" or cmd=="down":
750 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000751 os.popen(cmd_string)
752 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000753
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000754 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000755
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000756#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000757#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000758@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
759def link_up(src_dpid, src_port, dst_dpid, dst_port):
Tim Lindberg201ade22013-04-05 11:52:08 -0700760 result = ""
761
Paul Greysonc090d142013-04-09 16:59:03 -0700762 if (TESTBED == "sw"):
Tim Lindberg201ade22013-04-05 11:52:08 -0700763 result = link_up_sw(src_dpid, src_port, dst_dpid, dst_port)
764 else:
765 result = link_up_hw(src_dpid, src_port, dst_dpid, dst_port)
766 return result
767
768# Link up on software testbed
769def link_up_sw(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000770
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000771 cmd = 'up'
772 result=""
Paul Greyson472da4c2013-03-28 11:43:17 -0700773 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000774 if dpid in core_switches:
775 host = controllers[0]
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000776 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000777 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000778 host = controllers[hostid-1]
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000779
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +0000780 if dpid == src_dpid:
781 (port, dontcare) = get_link_ports(dpid, dst_dpid)
782 else:
783 (port, dontcare) = get_link_ports(dpid, src_dpid)
784
785 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
786 print cmd_string
787 res=os.popen(cmd_string).read()
788 result = result + ' ' + res
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000789
790 return result
791
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +0000792# if hostid == 2 :
793# src_ports = [51]
794# else :
795# src_ports = [26]
796#
797# for port in src_ports :
798# cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
799# print cmd_string
800# res=os.popen(cmd_string).read()
801
802
803
Tim Lindberg201ade22013-04-05 11:52:08 -0700804# Link up on hardware testbed
805def link_up_hw(src_dpid, src_port, dst_dpid, dst_port):
806
807 port1 = src_port
808 port2 = dst_port
809 if src_dpid == "00:00:00:00:ba:5e:ba:11":
810 if dst_dpid == "00:00:00:08:a2:08:f9:01":
811 port1 = 24
812 port2 = 24
813 elif dst_dpid == "00:01:00:16:97:08:9a:46":
814 port1 = 23
815 port2 = 23
816 elif src_dpid == "00:00:00:00:ba:5e:ba:13":
817 if dst_dpid == "00:00:20:4e:7f:51:8a:35":
818 port1 = 22
819 port2 = 22
820 elif dst_dpid == "00:00:00:00:00:00:ba:12":
821 port1 = 23
822 port2 = 23
823 elif src_dpid == "00:00:00:00:00:00:ba:12":
824 if dst_dpid == "00:00:00:00:ba:5e:ba:13":
825 port1 = 23
826 port2 = 23
827 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
828 port1 = 22
829 port2 = 22
830 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
831 port1 = 24
832 port2 = 21
833 elif src_dpid == "00:01:00:16:97:08:9a:46":
834 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
835 port1 = 23
836 port2 = 23
837 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
838 port1 = 24
839 port2 = 24
840 elif src_dpid == "00:00:00:08:a2:08:f9:01":
841 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
842 port1 = 24
843 port2 = 24
844 elif dst_dpid == "00:00:00:00:00:00:ba:12":
845 port1 = 22
846 port2 = 22
847 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
848 port1 = 23
849 port2 = 23
850 elif src_dpid == "00:00:20:4e:7f:51:8a:35":
851 if dst_dpid == "00:00:00:00:00:00:ba:12":
852 port1 = 21
853 port2 = 24
854 elif dst_dpid == "00:00:00:00:ba:5e:ba:13":
855 port1 = 22
856 port2 = 22
857 elif dst_dpid == "00:01:00:16:97:08:9a:46":
858 port1 = 24
859 port2 = 24
860 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
861 port1 = 23
862 port2 = 23
863
864 cmd = 'up'
865 result=""
866 host = controllers[0]
Tim Lindbergb03673d2013-04-10 13:47:31 -0700867 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % (src_dpid, port1, cmd)
Tim Lindberg201ade22013-04-05 11:52:08 -0700868 print cmd_string
869 res=os.popen(cmd_string).read()
870 result = result + ' ' + res
Tim Lindbergb03673d2013-04-10 13:47:31 -0700871 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % (dst_dpid, port2, cmd)
Tim Lindberg201ade22013-04-05 11:52:08 -0700872 print cmd_string
873 res=os.popen(cmd_string).read()
874 result = result + ' ' + res
875
876
877 return result
878
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000879
880#* Link Down
881#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000882@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000883def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
884
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000885 if src_dpid in core_switches:
886 host = controllers[0]
887 else:
888 hostid=int(src_dpid.split(':')[-2])
889 host = controllers[hostid-1]
890
Tim Lindberg201ade22013-04-05 11:52:08 -0700891 if (TESTBED == "sw"):
892 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
893 else:
Tim Lindberga6c04172013-04-05 17:34:05 -0700894 if ( src_dpid == "00:00:00:08:a2:08:f9:01" ):
Tim Lindbergb03673d2013-04-10 13:47:31 -0700895 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % ( dst_dpid, dst_port, cmd)
Tim Lindberga6c04172013-04-05 17:34:05 -0700896 else:
Tim Lindbergb03673d2013-04-10 13:47:31 -0700897 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % ( src_dpid, src_port, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000898 print cmd_string
899
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000900 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000901
902 return result
903
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000904#* Create Flow
905#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000906#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 +0000907@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000908def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
Masayoshi Kobayashiab01bcf2013-04-09 04:21:57 +0000909 host = pick_host()
910 url ="%s/wm/flow/getsummary/%s/%s/json" % (host, 0, 0)
911 (code, result) = get_json(url)
912 parsedResult = json.loads(result)
913 flow_nr = int(parsedResult[-1]['flowId'], 16)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000914 flow_nr += 1
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000915 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)
916 flow_nr += 1
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000917 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 +0000918 print command
919 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000920 errcode1 = os.popen(command1).read()
921 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000922
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000923#* Delete Flow
924#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000925@app.route("/gui/delflow/<flow_id>")
926def del_flow(flow_id):
927 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
928 print command
929 errcode = os.popen(command).read()
930 return errcode
931
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000932#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700933#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000934@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
935def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000936 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000937 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000938 print command
939 result = os.popen(command).read()
940 if len(result) == 0:
941 print "No Flow found"
942 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000943 except:
944 print "REST IF has issue"
945 exit
946
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000947 parsedResult = json.loads(result)
948
949 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000950 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
951 src_port = parsedResult['dataPath']['srcPort']['port']['value']
952 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
953 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000954# 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 +0000955
956 if src_dpid in core_switches:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700957 src_host = controllers[0]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000958 else:
959 hostid=int(src_dpid.split(':')[-2])
Tim Lindbergb03673d2013-04-10 13:47:31 -0700960 if TESTBED == "hw":
961 src_host = "mininet%i" % hostid
962 else:
963 src_host = controllers[hostid-1]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000964
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700965 if dst_dpid in core_switches:
966 dst_host = controllers[0]
967 else:
968 hostid=int(dst_dpid.split(':')[-2])
Tim Lindbergb03673d2013-04-10 13:47:31 -0700969 if TESTBED == "hw":
970 dst_host = "mininet%i" % hostid
971 else:
972 dst_host = controllers[hostid-1]
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700973
Tim Lindbergb03673d2013-04-10 13:47:31 -0700974# /runiperf.sh <flowid> <src_dpid> <dst_dpid> hw:svr|sw:svr|hw:client|sw:client <proto>/<duration>/<interval>/<samples>
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700975 protocol="udp"
976 interval=0.1
Tim Lindbergb03673d2013-04-10 13:47:31 -0700977 if TESTBED == "hw":
978 cmd_string="dsh -w %s 'cd ONOS/scripts; " % dst_host
979 else:
980 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s '" % dst_host
981 cmd_string += "./runiperf.sh %d %s %s %s:%s %s/%s/%s/%s'" % (flowId, src_dpid, dst_dpid, TESTBED, "svr", protocol, duration, interval, samples)
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700982 print cmd_string
983 os.popen(cmd_string)
984
Tim Lindbergb03673d2013-04-10 13:47:31 -0700985 if TESTBED == "hw":
986 cmd_string="dsh -w %s 'cd ONOS/scripts; " % src_host
987 else:
988 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s'" % src_host
989 cmd_string+="./runiperf.sh %d %s %s %s:%s %s/%s/%s/%s'" % (flowId, src_dpid, dst_dpid, TESTBED, "client", protocol, duration, interval, samples)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000990 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000991 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000992
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000993 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000994
Tim Lindbergb03673d2013-04-10 13:47:31 -0700995
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000996#* Get Iperf Throughput
997#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000998@app.route("/gui/iperf/rate/<flow_id>")
999def iperf_rate(flow_id):
1000 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001001 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
1002 print command
1003 result = os.popen(command).read()
1004 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001005 resp = Response(result, status=400, mimetype='text/html')
1006 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001007 except:
1008 print "REST IF has issue"
1009 exit
1010
1011 parsedResult = json.loads(result)
1012
1013 flowId = int(parsedResult['flowId']['value'], 16)
1014 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
1015 src_port = parsedResult['dataPath']['srcPort']['port']['value']
1016 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
1017 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
1018
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -07001019 if dst_dpid in core_switches:
Tim Lindbergb03673d2013-04-10 13:47:31 -07001020 host = controllers[0]
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001021 else:
Tim Lindbergb03673d2013-04-10 13:47:31 -07001022 hostid=int(dst_dpid.split(':')[-2])
1023 if TESTBED == "hw":
1024 host = "mininet%i" % hostid
1025 else:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001026 host = controllers[hostid-1]
1027
1028 try:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -07001029 command = "curl -s http://%s:%s/log/iperfsvr_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001030 print command
1031 result = os.popen(command).read()
1032 except:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001033 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +00001034
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +00001035 if len(result) == 0:
1036 resp = Response(result, status=400, mimetype='text/html')
1037 return "no iperf file found (flowid %s)" % flow_id;
1038 else:
1039 resp = Response(result, status=200, mimetype='application/json')
1040 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +00001041
Ubuntu82b8a832013-02-06 22:00:11 +00001042if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001043 random.seed()
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +00001044 read_config()
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +00001045 read_link_def()
Ubuntu82b8a832013-02-06 22:00:11 +00001046 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001047# 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")
1048# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
1049# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
1050# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
1051# 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 +00001052# print "-- query all switches --"
1053# query_switch()
1054# print "-- query topo --"
1055# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001056# link_change(1,2,3,4)
1057 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001058# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +00001059# print "-- query all devices --"
1060# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001061# iperf_start(1,10,15)
1062# iperf_rate(1)
Masayoshi Kobayashiab01bcf2013-04-09 04:21:57 +00001063# switches()
1064 add_flow(1,2,3,4,5,6)
Ubuntu82b8a832013-02-06 22:00:11 +00001065 else:
1066 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +00001067 app.run(threaded=True, host="0.0.0.0", port=9000)