blob: 6c2ea15933da2b9d6ee1a226fdff0e3a40324a13 [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:
Tim Lindberg6445a182013-04-15 10:17:18 -0700196 command = "curl -m 40 -s %s/gui/iperf/start/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id, duration, samples)
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700197 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
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700232@app.route("/proxy/gui/reset")
233def proxy_gui_reset():
234 result = ""
235 try:
236 command = "curl -m 300 -s %s/gui/reset" % (ONOS_GUI3_CONTROL_HOST)
237 print command
238 result = os.popen(command).read()
239 except:
240 print "REST IF has issue"
241 exit
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700242
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700243 resp = Response(result, status=200, mimetype='application/json')
244 return resp
245
246@app.route("/proxy/gui/scale")
247def proxy_gui_scale():
248 result = ""
249 try:
250 command = "curl -m 300 -s %s/gui/scale" % (ONOS_GUI3_CONTROL_HOST)
251 print command
252 result = os.popen(command).read()
253 except:
254 print "REST IF has issue"
255 exit
256
257 resp = Response(result, status=200, mimetype='application/json')
258 return resp
259
260###### ONOS REST API ##############################
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000261## Worker Func ###
262def get_json(url):
263 code = 200
264 try:
Tim Lindberg6445a182013-04-15 10:17:18 -0700265 command = "curl -m 60 -s %s" % (url)
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000266 result = os.popen(command).read()
Paul Greysonc090d142013-04-09 16:59:03 -0700267 parsedResult = json.loads(result)
Masayoshi Kobayashi8ac33362013-04-05 03:17:26 +0000268 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
269 print "REST %s returned code %s" % (command, parsedResult['code'])
270 code=500
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000271 except:
272 print "REST IF %s has issue" % command
273 result = ""
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000274 code = 500
275
276 return (code, result)
277
278def pick_host():
279 if LB == True:
280 nr_host=len(controllers)
281 r=random.randint(0, nr_host - 1)
282 host=controllers[r]
283 else:
284 host=ONOS_DEFAULT_HOST
Paul Greysonc090d142013-04-09 16:59:03 -0700285
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000286 return "http://" + host + ":8080"
287
288## Switch ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000289@app.route("/wm/core/topology/switches/all/json")
290def switches():
291 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000292 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000293 else:
294 host = ONOS_GUI3_HOST
295
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000296 url ="%s/wm/core/topology/switches/all/json" % (host)
297 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000298
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000299 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000300 return resp
301
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000302## Link ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000303@app.route("/wm/core/topology/links/json")
304def links():
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/core/topology/links/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## FlowSummary ##
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000317@app.route("/wm/flow/getsummary/<start>/<range>/json")
318def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000319 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/flow/getsummary/%s/%s/json" % (host, start, range)
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
330@app.route("/wm/registry/controllers/json")
331def registry_controllers():
332 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000333 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000334 else:
335 host = ONOS_GUI3_HOST
336
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000337 url= "%s/wm/registry/controllers/json" % (host)
338 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000339
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000340 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000341 return resp
342
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000343
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000344@app.route("/wm/registry/switches/json")
345def registry_switches():
346 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000347 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000348 else:
349 host = ONOS_GUI3_HOST
350
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000351 url="%s/wm/registry/switches/json" % (host)
352 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000353
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000354 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000355 return resp
356
Ubuntu82b8a832013-02-06 22:00:11 +0000357def node_id(switch_array, dpid):
358 id = -1
359 for i, val in enumerate(switch_array):
360 if val['name'] == dpid:
361 id = i
362 break
363
364 return id
365
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000366## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000367@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000368def topology_for_gui():
369 try:
370 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/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 sys.exit(0)
377
378 topo = {}
379 switches = []
380 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000381 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000382
383 for v in parsedResult:
384 if v.has_key('dpid'):
385# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
386 dpid = str(v['dpid'])
387 state = str(v['state'])
388 sw = {}
389 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000390 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000391
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000392 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000393 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000394 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000395
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000396 try:
397 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
398 result = os.popen(command).read()
399 parsedResult = json.loads(result)
400 except:
401 log_error("REST IF has issue: %s" % command)
402 log_error("%s" % result)
403
404 for key in parsedResult:
405 dpid = key
406 ctrl = parsedResult[dpid][0]['controllerId']
407 sw_id = node_id(switches, dpid)
408 if sw_id != -1:
409 if switches[sw_id]['group'] != 0:
410 switches[sw_id]['group'] = controllers.index(ctrl) + 1
411
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000412 try:
413 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000414# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000415 p1=1
416 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000417# v2 = "00:00:00:00:00:0d:00:d3"
418 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000419 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
420 result = os.popen(command).read()
421 parsedResult = json.loads(result)
422 except:
423 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000424 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000425
Ubuntu765deff2013-02-28 18:39:13 +0000426 path = []
427 if parsedResult.has_key('flowEntries'):
428 flowEntries= parsedResult['flowEntries']
429 for i, v in enumerate(flowEntries):
430 if i < len(flowEntries) - 1:
431 sdpid= flowEntries[i]['dpid']['value']
432 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700433 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000434
Ubuntu82b8a832013-02-06 22:00:11 +0000435 try:
436 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
437 result = os.popen(command).read()
438 parsedResult = json.loads(result)
439 except:
440 log_error("REST IF has issue: %s" % command)
441 log_error("%s" % result)
442 sys.exit(0)
443
444 for v in parsedResult:
445 link = {}
446 if v.has_key('dst-switch'):
447 dst_dpid = str(v['dst-switch'])
448 dst_id = node_id(switches, dst_dpid)
449 if v.has_key('src-switch'):
450 src_dpid = str(v['src-switch'])
451 src_id = node_id(switches, src_dpid)
452 link['source'] = src_id
453 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000454
455 onpath = 0
456 for (s,d) in path:
457 if s == v['src-switch'] and d == v['dst-switch']:
458 onpath = 1
459 break
460 link['type'] = onpath
461
Ubuntu82b8a832013-02-06 22:00:11 +0000462 links.append(link)
463
464 topo['nodes'] = switches
465 topo['links'] = links
466
Ubuntu82b8a832013-02-06 22:00:11 +0000467 js = json.dumps(topo)
468 resp = Response(js, status=200, mimetype='application/json')
469 return resp
470
Ubuntuaea2a682013-02-08 08:30:10 +0000471#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
472#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
473@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
474def shortest_path(v1, p1, v2, p2):
475 try:
476 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
477 result = os.popen(command).read()
478 parsedResult = json.loads(result)
479 except:
480 log_error("REST IF has issue: %s" % command)
481 log_error("%s" % result)
482 sys.exit(0)
483
484 topo = {}
485 switches = []
486 links = []
487
488 for v in parsedResult:
489 if v.has_key('dpid'):
490 dpid = str(v['dpid'])
491 state = str(v['state'])
492 sw = {}
493 sw['name']=dpid
494 if str(v['state']) == "ACTIVE":
495 if dpid[-2:-1] == "a":
496 sw['group']=1
497 if dpid[-2:-1] == "b":
498 sw['group']=2
499 if dpid[-2:-1] == "c":
500 sw['group']=3
501 if str(v['state']) == "INACTIVE":
502 sw['group']=0
503
504 switches.append(sw)
505
506 try:
507 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
508 result = os.popen(command).read()
509 parsedResult = json.loads(result)
510 except:
511 log_error("No route")
512 parsedResult = []
513# exit(1)
514
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700515 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000516 for i, v in enumerate(parsedResult):
517 if i < len(parsedResult) - 1:
518 sdpid= parsedResult[i]['switch']
519 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700520 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000521
522 try:
523 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
524 result = os.popen(command).read()
525 parsedResult = json.loads(result)
526 except:
527 log_error("REST IF has issue: %s" % command)
528 log_error("%s" % result)
529 sys.exit(0)
530
531 for v in parsedResult:
532 link = {}
533 if v.has_key('dst-switch'):
534 dst_dpid = str(v['dst-switch'])
535 dst_id = node_id(switches, dst_dpid)
536 if v.has_key('src-switch'):
537 src_dpid = str(v['src-switch'])
538 src_id = node_id(switches, src_dpid)
539 link['source'] = src_id
540 link['target'] = dst_id
541 onpath = 0
542 for (s,d) in path:
543 if s == v['src-switch'] and d == v['dst-switch']:
544 onpath = 1
545 break
546
547 link['type'] = onpath
548 links.append(link)
549
550 topo['nodes'] = switches
551 topo['links'] = links
552
Ubuntuaea2a682013-02-08 08:30:10 +0000553 js = json.dumps(topo)
554 resp = Response(js, status=200, mimetype='application/json')
555 return resp
556
Ubuntu82b8a832013-02-06 22:00:11 +0000557@app.route("/wm/core/controller/switches/json")
558def query_switch():
559 try:
560 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
561# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000562 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000563 result = os.popen(command).read()
564 parsedResult = json.loads(result)
565 except:
566 log_error("REST IF has issue: %s" % command)
567 log_error("%s" % result)
568 sys.exit(0)
569
570# print command
571# print result
572 switches_ = []
573 for v in parsedResult:
574 if v.has_key('dpid'):
575 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
576 dpid = str(v['dpid'])
577 state = str(v['state'])
578 sw = {}
579 sw['dpid']=dpid
580 sw['active']=state
581 switches_.append(sw)
582
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000583# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000584 js = json.dumps(switches_)
585 resp = Response(js, status=200, mimetype='application/json')
586 return resp
587
588@app.route("/wm/device/")
589def devices():
590 try:
591 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
592 result = os.popen(command).read()
593 parsedResult = json.loads(result)['results']
594 except:
595 log_error("REST IF has issue: %s" % command)
596 log_error("%s" % result)
597 sys.exit(0)
598
599 devices = []
600 for v in parsedResult:
601 dl_addr = v['dl_addr']
602 nw_addr = v['nw_addr']
603 vertex = v['_id']
604 mac = []
605 mac.append(dl_addr)
606 ip = []
607 ip.append(nw_addr)
608 device = {}
609 device['entryClass']="DefaultEntryClass"
610 device['mac']=mac
611 device['ipv4']=ip
612 device['vlan']=[]
613 device['lastSeen']=0
614 attachpoints =[]
615
616 port, dpid = deviceV_to_attachpoint(vertex)
617 attachpoint = {}
618 attachpoint['port']=port
619 attachpoint['switchDPID']=dpid
620 attachpoints.append(attachpoint)
621 device['attachmentPoint']=attachpoints
622 devices.append(device)
623
Ubuntu82b8a832013-02-06 22:00:11 +0000624 js = json.dumps(devices)
625 resp = Response(js, status=200, mimetype='application/json')
626 return resp
627
628#{"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}
629
Ubuntu82b8a832013-02-06 22:00:11 +0000630## return fake stat for now
631@app.route("/wm/core/switch/<switchId>/<statType>/json")
632def switch_stat(switchId, statType):
633 if statType == "desc":
634 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
635 ret = {}
636 ret[switchId]=desc
637 elif statType == "aggregate":
638 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
639 ret = {}
640 ret[switchId]=aggr
641 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700642 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000643
644 js = json.dumps(ret)
645 resp = Response(js, status=200, mimetype='application/json')
646 return resp
647
648
649@app.route("/wm/topology/links/json")
650def query_links():
651 try:
652 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000653 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000654 result = os.popen(command).read()
655 parsedResult = json.loads(result)['results']
656 except:
657 log_error("REST IF has issue: %s" % command)
658 log_error("%s" % result)
659 sys.exit(0)
660
661 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000662# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000663 sport = []
664 links = []
665 for v in parsedResult:
666 srcport = v['_id']
667 try:
668 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
669 print command
670 result = os.popen(command).read()
671 linkResults = json.loads(result)['results']
672 except:
673 log_error("REST IF has issue: %s" % command)
674 log_error("%s" % result)
675 sys.exit(0)
676
677 for p in linkResults:
678 if p.has_key('type') and p['type'] == "port":
679 dstport = p['_id']
680 (sport, sdpid) = portV_to_port_dpid(srcport)
681 (dport, ddpid) = portV_to_port_dpid(dstport)
682 link = {}
683 link["src-switch"]=sdpid
684 link["src-port"]=sport
685 link["src-port-state"]=0
686 link["dst-switch"]=ddpid
687 link["dst-port"]=dport
688 link["dst-port-state"]=0
689 link["type"]="internal"
690 links.append(link)
691
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000692# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000693 js = json.dumps(links)
694 resp = Response(js, status=200, mimetype='application/json')
695 return resp
696
Ubuntuc016ba12013-02-27 21:53:41 +0000697@app.route("/controller_status")
698def controller_status():
Tim Lindberg201ade22013-04-05 11:52:08 -0700699# onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
700 onos_check="cd; onos status %s | grep %s | awk '{print $2}'"
Ubuntuc016ba12013-02-27 21:53:41 +0000701 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
702
703 cont_status=[]
704 for i in controllers:
705 status={}
706 onos=os.popen(onos_check % i).read()[:-1]
Tim Lindberg201ade22013-04-05 11:52:08 -0700707 onos=os.popen(onos_check % (i, i.lower())).read()[:-1]
Ubuntuc016ba12013-02-27 21:53:41 +0000708 status["name"]=i
709 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000710 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000711 cont_status.append(status)
712
713 js = json.dumps(cont_status)
714 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000715 return resp
716
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000717### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000718@app.route("/gui/controller/<cmd>/<controller_name>")
719def controller_status_change(cmd, controller_name):
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000720 if (TESTBED == "hw"):
Tim Lindberg34c9ff62013-04-10 15:06:17 -0700721 start_onos="/home/admin/bin/onos start %s" % (controller_name[-1:])
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700722# start_onos="/home/admin/bin/onos start %s > /tmp/debug " % (controller_name[-1:])
Tim Lindberg34c9ff62013-04-10 15:06:17 -0700723 stop_onos="/home/admin/bin/onos stop %s" % (controller_name[-1:])
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700724# stop_onos="/home/admin/bin/onos stop %s > /tmp/debug " % (controller_name[-1:])
725# print "Debug: Controller command %s called %s" % (cmd, controller_name)
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000726 else:
727 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
728 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000729
730 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000731 result=os.popen(start_onos).read()
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700732 ret = "controller %s is up: %s" % (controller_name, result)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000733 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000734 result=os.popen(stop_onos).read()
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700735 ret = "controller %s is down: %s" % (controller_name, result)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000736
737 return ret
738
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000739@app.route("/gui/switchctrl/<cmd>")
740def switch_controller_setting(cmd):
741 if cmd =="local":
742 print "All aggr switches connects to local controller only"
743 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700744 if (TESTBED == "sw"):
Jonathan Hart4dbaa502013-04-13 20:21:29 -0700745 for i in range(1, len(controllers)):
Tim Lindberg201ade22013-04-05 11:52:08 -0700746 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-local.sh'" % (controllers[i])
747 result += os.popen(cmd_string).read()
748 else:
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700749 cmd_string="cd; switch local > /tmp/watch"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000750 result += os.popen(cmd_string).read()
751 elif cmd =="all":
752 print "All aggr switches connects to all controllers except for core controller"
753 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700754 if (TESTBED == "sw"):
Jonathan Hart4dbaa502013-04-13 20:21:29 -0700755 for i in range(1, len(controllers)):
Tim Lindberg201ade22013-04-05 11:52:08 -0700756 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i])
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700757 print "cmd is: "+cmd_string
Tim Lindberg201ade22013-04-05 11:52:08 -0700758 result += os.popen(cmd_string).read()
Paul Greysonc090d142013-04-09 16:59:03 -0700759 else:
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700760 cmd_string="/home/admin/bin/switch all > /tmp/watch"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000761 result += os.popen(cmd_string).read()
762
763 return result
764
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700765@app.route("/gui/reset")
766def reset_demo():
Masayoshi Kobayashi1ef9ec02013-04-12 18:03:12 +0000767 if (TESTBED == "hw"):
768 cmd_string="cd ~/bin; ./demo-reset-hw.sh > /tmp/watch &"
769 else:
770 cmd_string="cd ~/ONOS/scripts; ./demo-reset-sw.sh > /tmp/watch &"
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700771 os.popen(cmd_string)
772 return "Reset"
773
774@app.route("/gui/scale")
775def scale_demo():
Masayoshi Kobayashi1ef9ec02013-04-12 18:03:12 +0000776 if (TESTBED == "hw"):
777 cmd_string="cd ~/bin; ~/bin/demo-scale-out-hw.sh > /tmp/watch &"
778 else:
779 cmd_string="cd ~/ONOS/scripts; ./demo-scale-out-sw.sh > /tmp/watch &"
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700780 os.popen(cmd_string)
781 return "scale"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000782
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000783@app.route("/gui/switch/<cmd>/<dpid>")
784def switch_status_change(cmd, dpid):
Tim Lindberg201ade22013-04-05 11:52:08 -0700785 result = ""
786 if (TESTBED == "hw"):
787 return result
788
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000789 r = re.compile(':')
790 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000791 host=controllers[0]
792 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
793 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000794 print "cmd_string"
795
796 if cmd =="up" or cmd=="down":
797 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000798 os.popen(cmd_string)
799 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000800
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000801 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000802
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000803#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000804#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000805@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
806def link_up(src_dpid, src_port, dst_dpid, dst_port):
Tim Lindberg201ade22013-04-05 11:52:08 -0700807 result = ""
808
Paul Greysonc090d142013-04-09 16:59:03 -0700809 if (TESTBED == "sw"):
Tim Lindberg201ade22013-04-05 11:52:08 -0700810 result = link_up_sw(src_dpid, src_port, dst_dpid, dst_port)
811 else:
812 result = link_up_hw(src_dpid, src_port, dst_dpid, dst_port)
813 return result
814
815# Link up on software testbed
816def link_up_sw(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000817
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000818 cmd = 'up'
819 result=""
Paul Greyson472da4c2013-03-28 11:43:17 -0700820 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000821 if dpid in core_switches:
822 host = controllers[0]
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000823 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000824 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000825 host = controllers[hostid-1]
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000826
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +0000827 if dpid == src_dpid:
828 (port, dontcare) = get_link_ports(dpid, dst_dpid)
829 else:
830 (port, dontcare) = get_link_ports(dpid, src_dpid)
831
832 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
833 print cmd_string
834 res=os.popen(cmd_string).read()
835 result = result + ' ' + res
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000836
837 return result
838
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +0000839# if hostid == 2 :
840# src_ports = [51]
841# else :
842# src_ports = [26]
843#
844# for port in src_ports :
845# cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
846# print cmd_string
847# res=os.popen(cmd_string).read()
848
849
850
Tim Lindberg201ade22013-04-05 11:52:08 -0700851# Link up on hardware testbed
852def link_up_hw(src_dpid, src_port, dst_dpid, dst_port):
853
854 port1 = src_port
855 port2 = dst_port
856 if src_dpid == "00:00:00:00:ba:5e:ba:11":
857 if dst_dpid == "00:00:00:08:a2:08:f9:01":
858 port1 = 24
859 port2 = 24
860 elif dst_dpid == "00:01:00:16:97:08:9a:46":
861 port1 = 23
862 port2 = 23
863 elif src_dpid == "00:00:00:00:ba:5e:ba:13":
864 if dst_dpid == "00:00:20:4e:7f:51:8a:35":
865 port1 = 22
866 port2 = 22
867 elif dst_dpid == "00:00:00:00:00:00:ba:12":
868 port1 = 23
869 port2 = 23
870 elif src_dpid == "00:00:00:00:00:00:ba:12":
871 if dst_dpid == "00:00:00:00:ba:5e:ba:13":
872 port1 = 23
873 port2 = 23
874 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
875 port1 = 22
876 port2 = 22
877 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
878 port1 = 24
879 port2 = 21
880 elif src_dpid == "00:01:00:16:97:08:9a:46":
881 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
882 port1 = 23
883 port2 = 23
884 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
885 port1 = 24
886 port2 = 24
887 elif src_dpid == "00:00:00:08:a2:08:f9:01":
888 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
889 port1 = 24
890 port2 = 24
891 elif dst_dpid == "00:00:00:00:00:00:ba:12":
892 port1 = 22
893 port2 = 22
894 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
895 port1 = 23
896 port2 = 23
897 elif src_dpid == "00:00:20:4e:7f:51:8a:35":
898 if dst_dpid == "00:00:00:00:00:00:ba:12":
899 port1 = 21
900 port2 = 24
901 elif dst_dpid == "00:00:00:00:ba:5e:ba:13":
902 port1 = 22
903 port2 = 22
904 elif dst_dpid == "00:01:00:16:97:08:9a:46":
905 port1 = 24
906 port2 = 24
907 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
908 port1 = 23
909 port2 = 23
910
911 cmd = 'up'
912 result=""
913 host = controllers[0]
Tim Lindbergb03673d2013-04-10 13:47:31 -0700914 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % (src_dpid, port1, cmd)
Tim Lindberg201ade22013-04-05 11:52:08 -0700915 print cmd_string
916 res=os.popen(cmd_string).read()
917 result = result + ' ' + res
Tim Lindbergb03673d2013-04-10 13:47:31 -0700918 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % (dst_dpid, port2, cmd)
Tim Lindberg201ade22013-04-05 11:52:08 -0700919 print cmd_string
920 res=os.popen(cmd_string).read()
921 result = result + ' ' + res
922
923
924 return result
925
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000926
927#* Link Down
928#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000929@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000930def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
931
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000932 if src_dpid in core_switches:
933 host = controllers[0]
934 else:
935 hostid=int(src_dpid.split(':')[-2])
936 host = controllers[hostid-1]
937
Tim Lindberg201ade22013-04-05 11:52:08 -0700938 if (TESTBED == "sw"):
939 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
940 else:
Tim Lindberga6c04172013-04-05 17:34:05 -0700941 if ( src_dpid == "00:00:00:08:a2:08:f9:01" ):
Tim Lindbergb03673d2013-04-10 13:47:31 -0700942 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % ( dst_dpid, dst_port, cmd)
Tim Lindberga6c04172013-04-05 17:34:05 -0700943 else:
Tim Lindbergb03673d2013-04-10 13:47:31 -0700944 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % ( src_dpid, src_port, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000945 print cmd_string
946
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000947 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000948
949 return result
950
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000951#* Create Flow
952#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000953#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 +0000954@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000955def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
Masayoshi Kobayashiab01bcf2013-04-09 04:21:57 +0000956 host = pick_host()
957 url ="%s/wm/flow/getsummary/%s/%s/json" % (host, 0, 0)
958 (code, result) = get_json(url)
959 parsedResult = json.loads(result)
960 flow_nr = int(parsedResult[-1]['flowId'], 16)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000961 flow_nr += 1
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000962 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)
963 flow_nr += 1
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000964 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)
Tim Lindberg6445a182013-04-15 10:17:18 -0700965 print "add flow: %s, %s" % (command, command1)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000966 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000967 errcode1 = os.popen(command1).read()
Tim Lindberg6445a182013-04-15 10:17:18 -0700968 ret=command+":"+errcode+" "+command1+":"+errcode1
969 print ret
970 return ret
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000971
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000972#* Delete Flow
973#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000974@app.route("/gui/delflow/<flow_id>")
975def del_flow(flow_id):
976 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
977 print command
978 errcode = os.popen(command).read()
979 return errcode
980
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000981#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700982#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000983@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
984def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000985 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000986 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000987 print command
988 result = os.popen(command).read()
989 if len(result) == 0:
990 print "No Flow found"
Tim Lindberg6445a182013-04-15 10:17:18 -0700991 return "Flow %s not found" % (flow_id);
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000992 except:
993 print "REST IF has issue"
Tim Lindberg6445a182013-04-15 10:17:18 -0700994 return "REST IF has issue"
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000995 exit
996
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000997 parsedResult = json.loads(result)
998
999 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +00001000 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
1001 src_port = parsedResult['dataPath']['srcPort']['port']['value']
1002 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
1003 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001004# 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 +00001005
1006 if src_dpid in core_switches:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -07001007 src_host = controllers[0]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +00001008 else:
1009 hostid=int(src_dpid.split(':')[-2])
Tim Lindbergb03673d2013-04-10 13:47:31 -07001010 if TESTBED == "hw":
1011 src_host = "mininet%i" % hostid
1012 else:
1013 src_host = controllers[hostid-1]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +00001014
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -07001015 if dst_dpid in core_switches:
1016 dst_host = controllers[0]
1017 else:
1018 hostid=int(dst_dpid.split(':')[-2])
Tim Lindbergb03673d2013-04-10 13:47:31 -07001019 if TESTBED == "hw":
1020 dst_host = "mininet%i" % hostid
1021 else:
1022 dst_host = controllers[hostid-1]
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -07001023
Tim Lindbergb03673d2013-04-10 13:47:31 -07001024# /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 -07001025 protocol="udp"
1026 interval=0.1
Tim Lindbergb03673d2013-04-10 13:47:31 -07001027 if TESTBED == "hw":
1028 cmd_string="dsh -w %s 'cd ONOS/scripts; " % dst_host
1029 else:
Masayoshi Kobayashi96263f92013-04-11 02:21:46 +00001030 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; " % dst_host
Tim Lindbergb03673d2013-04-10 13:47:31 -07001031 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 -07001032 print cmd_string
1033 os.popen(cmd_string)
1034
Tim Lindbergb03673d2013-04-10 13:47:31 -07001035 if TESTBED == "hw":
1036 cmd_string="dsh -w %s 'cd ONOS/scripts; " % src_host
1037 else:
Masayoshi Kobayashi96263f92013-04-11 02:21:46 +00001038 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts;" % src_host
Tim Lindbergb03673d2013-04-10 13:47:31 -07001039 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 +00001040 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001041 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +00001042
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +00001043 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001044
Tim Lindbergb03673d2013-04-10 13:47:31 -07001045
Masayoshi Kobayashi51011522013-03-27 00:18:12 +00001046#* Get Iperf Throughput
1047#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001048@app.route("/gui/iperf/rate/<flow_id>")
1049def iperf_rate(flow_id):
1050 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001051 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
1052 print command
1053 result = os.popen(command).read()
1054 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001055 resp = Response(result, status=400, mimetype='text/html')
1056 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001057 except:
1058 print "REST IF has issue"
1059 exit
1060
1061 parsedResult = json.loads(result)
1062
1063 flowId = int(parsedResult['flowId']['value'], 16)
1064 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
1065 src_port = parsedResult['dataPath']['srcPort']['port']['value']
1066 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
1067 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
1068
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -07001069 if dst_dpid in core_switches:
Tim Lindbergb03673d2013-04-10 13:47:31 -07001070 host = controllers[0]
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001071 else:
Tim Lindbergb03673d2013-04-10 13:47:31 -07001072 hostid=int(dst_dpid.split(':')[-2])
1073 if TESTBED == "hw":
1074 host = "mininet%i" % hostid
1075 else:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001076 host = controllers[hostid-1]
1077
1078 try:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -07001079 command = "curl -s http://%s:%s/log/iperfsvr_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001080 print command
1081 result = os.popen(command).read()
1082 except:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001083 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +00001084
Tim Lindberg6445a182013-04-15 10:17:18 -07001085 if re.match("Cannot", result):
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +00001086 resp = Response(result, status=400, mimetype='text/html')
Tim Lindberg6445a182013-04-15 10:17:18 -07001087 return "no iperf file found (host %s flowid %s): %s" % (host, flow_id, result)
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +00001088 else:
1089 resp = Response(result, status=200, mimetype='application/json')
1090 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +00001091
Ubuntu82b8a832013-02-06 22:00:11 +00001092if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001093 random.seed()
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +00001094 read_config()
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +00001095 read_link_def()
Ubuntu82b8a832013-02-06 22:00:11 +00001096 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001097# 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")
1098# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
1099# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
1100# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
1101# 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 +00001102# print "-- query all switches --"
1103# query_switch()
1104# print "-- query topo --"
1105# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001106# link_change(1,2,3,4)
1107 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001108# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +00001109# print "-- query all devices --"
1110# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001111# iperf_start(1,10,15)
1112# iperf_rate(1)
Masayoshi Kobayashiab01bcf2013-04-09 04:21:57 +00001113# switches()
Tim Lindberg9ec5e222013-04-12 10:46:02 -07001114# add_flow(1,2,3,4,5,6)
1115 reset_demo()
Ubuntu82b8a832013-02-06 22:00:11 +00001116 else:
1117 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +00001118 app.run(threaded=True, host="0.0.0.0", port=9000)