blob: 1f61160f8e7881a118b294b410f38b8a855498fe [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 +000016CONFIG_FILE=os.getenv("HOME") + "/ONOS/web/config.json"
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +000017LINK_FILE=os.getenv("HOME") + "/ONOS/web/link.json"
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -070018ONOSDIR=os.getenv("HOME") + "/ONOS"
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
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700219@app.route("/proxy/gui/reset")
220def proxy_gui_reset():
221 result = ""
222 try:
223 command = "curl -m 300 -s %s/gui/reset" % (ONOS_GUI3_CONTROL_HOST)
224 print command
225 result = os.popen(command).read()
226 except:
227 print "REST IF has issue"
228 exit
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700229
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700230 resp = Response(result, status=200, mimetype='application/json')
231 return resp
232
233@app.route("/proxy/gui/scale")
234def proxy_gui_scale():
235 result = ""
236 try:
237 command = "curl -m 300 -s %s/gui/scale" % (ONOS_GUI3_CONTROL_HOST)
238 print command
239 result = os.popen(command).read()
240 except:
241 print "REST IF has issue"
242 exit
243
244 resp = Response(result, status=200, mimetype='application/json')
245 return resp
246
247###### ONOS REST API ##############################
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000248## Worker Func ###
249def get_json(url):
250 code = 200
251 try:
Tim Lindberg6445a182013-04-15 10:17:18 -0700252 command = "curl -m 60 -s %s" % (url)
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000253 result = os.popen(command).read()
Paul Greysonc090d142013-04-09 16:59:03 -0700254 parsedResult = json.loads(result)
Masayoshi Kobayashi8ac33362013-04-05 03:17:26 +0000255 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
256 print "REST %s returned code %s" % (command, parsedResult['code'])
257 code=500
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000258 except:
259 print "REST IF %s has issue" % command
260 result = ""
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000261 code = 500
262
263 return (code, result)
264
265def pick_host():
266 if LB == True:
267 nr_host=len(controllers)
268 r=random.randint(0, nr_host - 1)
269 host=controllers[r]
270 else:
271 host=ONOS_DEFAULT_HOST
Paul Greysonc090d142013-04-09 16:59:03 -0700272
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000273 return "http://" + host + ":8080"
274
275## Switch ##
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800276@app.route("/wm/onos/topology/switches/all/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000277def switches():
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
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800283 url ="%s/wm/onos/topology/switches/all/json" % (host)
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000284 (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## Link ##
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800290@app.route("/wm/onos/topology/links/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000291def links():
292 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
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800297 url ="%s/wm/onos/topology/links/json" % (host)
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000298 (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
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000303## FlowSummary ##
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800304@app.route("/wm/onos/flows/getsummary/<start>/<range>/json")
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000305def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000306 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000307 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000308 else:
309 host = ONOS_GUI3_HOST
310
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800311 url ="%s/wm/onos/flows/getsummary/%s/%s/json" % (host, start, range)
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000312 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000313
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000314 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000315 return resp
316
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800317@app.route("/wm/onos/registry/controllers/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000318def registry_controllers():
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
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800324 url= "%s/wm/onos/registry/controllers/json" % (host)
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000325 (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
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000330
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800331@app.route("/wm/onos/registry/switches/json")
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000332def registry_switches():
333 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000334 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000335 else:
336 host = ONOS_GUI3_HOST
337
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800338 url="%s/wm/onos/registry/switches/json" % (host)
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000339 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000340
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000341 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000342 return resp
343
Ubuntu82b8a832013-02-06 22:00:11 +0000344def node_id(switch_array, dpid):
345 id = -1
346 for i, val in enumerate(switch_array):
347 if val['name'] == dpid:
348 id = i
349 break
350
351 return id
352
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000353## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000354@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000355def topology_for_gui():
356 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800357 command = "curl -s \'http://%s:%s/wm/onos/topology/switches/all/json\'" % (RestIP, RestPort)
Ubuntu82b8a832013-02-06 22:00:11 +0000358 result = os.popen(command).read()
359 parsedResult = json.loads(result)
360 except:
361 log_error("REST IF has issue: %s" % command)
362 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700363 return
364# sys.exit(0)
Ubuntu82b8a832013-02-06 22:00:11 +0000365
366 topo = {}
367 switches = []
368 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000369 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000370
371 for v in parsedResult:
372 if v.has_key('dpid'):
373# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
374 dpid = str(v['dpid'])
375 state = str(v['state'])
376 sw = {}
377 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000378 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000379
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000380 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000381 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000382 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000383
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000384 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800385 command = "curl -s \'http://%s:%s/wm/onos/registry/switches/json\'" % (RestIP, RestPort)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000386 result = os.popen(command).read()
387 parsedResult = json.loads(result)
388 except:
389 log_error("REST IF has issue: %s" % command)
390 log_error("%s" % result)
391
392 for key in parsedResult:
393 dpid = key
394 ctrl = parsedResult[dpid][0]['controllerId']
395 sw_id = node_id(switches, dpid)
396 if sw_id != -1:
397 if switches[sw_id]['group'] != 0:
398 switches[sw_id]['group'] = controllers.index(ctrl) + 1
399
Jonathan Hartbf66dff2013-10-28 14:07:41 -0700400# try:
401# v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000402# v1 = "00:00:00:00:00:0d:00:d1"
Jonathan Hartbf66dff2013-10-28 14:07:41 -0700403# p1=1
404# v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000405# v2 = "00:00:00:00:00:0d:00:d3"
Jonathan Hartbf66dff2013-10-28 14:07:41 -0700406# p2=1
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800407# command = "curl -s http://%s:%s/wm/onos/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
Jonathan Hartbf66dff2013-10-28 14:07:41 -0700408# result = os.popen(command).read()
409# parsedResult = json.loads(result)
410# except:
411# log_error("No route")
412# parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000413
Jonathan Hartbf66dff2013-10-28 14:07:41 -0700414 #path = []
415 #if parsedResult.has_key('flowEntries'):
416 # flowEntries= parsedResult['flowEntries']
417 # for i, v in enumerate(flowEntries):
418 # if i < len(flowEntries) - 1:
419 # sdpid= flowEntries[i]['dpid']['value']
420 # ddpid = flowEntries[i+1]['dpid']['value']
421 # path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000422
Ubuntu82b8a832013-02-06 22:00:11 +0000423 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800424 command = "curl -s \'http://%s:%s/wm/onos/topology/links/json\'" % (RestIP, RestPort)
Ubuntu82b8a832013-02-06 22:00:11 +0000425 result = os.popen(command).read()
426 parsedResult = json.loads(result)
427 except:
428 log_error("REST IF has issue: %s" % command)
429 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700430 return
431# sys.exit(0)
Ubuntu82b8a832013-02-06 22:00:11 +0000432
433 for v in parsedResult:
434 link = {}
435 if v.has_key('dst-switch'):
436 dst_dpid = str(v['dst-switch'])
437 dst_id = node_id(switches, dst_dpid)
438 if v.has_key('src-switch'):
439 src_dpid = str(v['src-switch'])
440 src_id = node_id(switches, src_dpid)
441 link['source'] = src_id
442 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000443
Jonathan Hartbf66dff2013-10-28 14:07:41 -0700444 #onpath = 0
445 #for (s,d) in path:
446 # if s == v['src-switch'] and d == v['dst-switch']:
447 # onpath = 1
448 # break
449 #link['type'] = onpath
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000450
Ubuntu82b8a832013-02-06 22:00:11 +0000451 links.append(link)
452
453 topo['nodes'] = switches
454 topo['links'] = links
455
Ubuntu82b8a832013-02-06 22:00:11 +0000456 js = json.dumps(topo)
457 resp = Response(js, status=200, mimetype='application/json')
458 return resp
459
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800460#@app.route("/wm/floodlight/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
461#@app.route("/wm/floodlight/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
462@app.route("/wm/floodlight/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
Ubuntuaea2a682013-02-08 08:30:10 +0000463def shortest_path(v1, p1, v2, p2):
464 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800465 command = "curl -s \'http://%s:%s/wm/onos/topology/switches/all/json\'" % (RestIP, RestPort)
Ubuntuaea2a682013-02-08 08:30:10 +0000466 result = os.popen(command).read()
467 parsedResult = json.loads(result)
468 except:
469 log_error("REST IF has issue: %s" % command)
470 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700471 return
472# sys.exit(0)
Ubuntuaea2a682013-02-08 08:30:10 +0000473
474 topo = {}
475 switches = []
476 links = []
477
478 for v in parsedResult:
479 if v.has_key('dpid'):
480 dpid = str(v['dpid'])
481 state = str(v['state'])
482 sw = {}
483 sw['name']=dpid
484 if str(v['state']) == "ACTIVE":
485 if dpid[-2:-1] == "a":
486 sw['group']=1
487 if dpid[-2:-1] == "b":
488 sw['group']=2
489 if dpid[-2:-1] == "c":
490 sw['group']=3
491 if str(v['state']) == "INACTIVE":
492 sw['group']=0
493
494 switches.append(sw)
495
496 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800497 command = "curl -s http://%s:%s/wm/onos/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
Ubuntuaea2a682013-02-08 08:30:10 +0000498 result = os.popen(command).read()
499 parsedResult = json.loads(result)
500 except:
501 log_error("No route")
502 parsedResult = []
503# exit(1)
504
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700505 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000506 for i, v in enumerate(parsedResult):
507 if i < len(parsedResult) - 1:
508 sdpid= parsedResult[i]['switch']
509 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700510 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000511
512 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800513 command = "curl -s \'http://%s:%s/wm/onos/topology/links/json\'" % (RestIP, RestPort)
Ubuntuaea2a682013-02-08 08:30:10 +0000514 result = os.popen(command).read()
515 parsedResult = json.loads(result)
516 except:
517 log_error("REST IF has issue: %s" % command)
518 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700519 return
520# sys.exit(0)
Ubuntuaea2a682013-02-08 08:30:10 +0000521
522 for v in parsedResult:
523 link = {}
524 if v.has_key('dst-switch'):
525 dst_dpid = str(v['dst-switch'])
526 dst_id = node_id(switches, dst_dpid)
527 if v.has_key('src-switch'):
528 src_dpid = str(v['src-switch'])
529 src_id = node_id(switches, src_dpid)
530 link['source'] = src_id
531 link['target'] = dst_id
532 onpath = 0
533 for (s,d) in path:
534 if s == v['src-switch'] and d == v['dst-switch']:
535 onpath = 1
536 break
537
538 link['type'] = onpath
539 links.append(link)
540
541 topo['nodes'] = switches
542 topo['links'] = links
543
Ubuntuaea2a682013-02-08 08:30:10 +0000544 js = json.dumps(topo)
545 resp = Response(js, status=200, mimetype='application/json')
546 return resp
547
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800548@app.route("/wm/floodlight/core/controller/switches/json")
Ubuntu82b8a832013-02-06 22:00:11 +0000549def query_switch():
550 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800551 command = "curl -s \'http://%s:%s/wm/onos/topology/switches/all/json\'" % (RestIP, RestPort)
552# http://localhost:8080/wm/onos/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000553 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000554 result = os.popen(command).read()
555 parsedResult = json.loads(result)
556 except:
557 log_error("REST IF has issue: %s" % command)
558 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700559 return
560# sys.exit(0)
Ubuntu82b8a832013-02-06 22:00:11 +0000561
562# print command
563# print result
564 switches_ = []
565 for v in parsedResult:
566 if v.has_key('dpid'):
567 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
568 dpid = str(v['dpid'])
569 state = str(v['state'])
570 sw = {}
571 sw['dpid']=dpid
572 sw['active']=state
573 switches_.append(sw)
574
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000575# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000576 js = json.dumps(switches_)
577 resp = Response(js, status=200, mimetype='application/json')
578 return resp
579
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800580@app.route("/wm/floodlight/device/")
Ubuntu82b8a832013-02-06 22:00:11 +0000581def devices():
582 try:
583 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
584 result = os.popen(command).read()
585 parsedResult = json.loads(result)['results']
586 except:
587 log_error("REST IF has issue: %s" % command)
588 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700589 return
590# sys.exit(0)
Ubuntu82b8a832013-02-06 22:00:11 +0000591
592 devices = []
593 for v in parsedResult:
594 dl_addr = v['dl_addr']
595 nw_addr = v['nw_addr']
596 vertex = v['_id']
597 mac = []
598 mac.append(dl_addr)
599 ip = []
600 ip.append(nw_addr)
601 device = {}
602 device['entryClass']="DefaultEntryClass"
603 device['mac']=mac
604 device['ipv4']=ip
605 device['vlan']=[]
606 device['lastSeen']=0
607 attachpoints =[]
608
609 port, dpid = deviceV_to_attachpoint(vertex)
610 attachpoint = {}
611 attachpoint['port']=port
612 attachpoint['switchDPID']=dpid
613 attachpoints.append(attachpoint)
614 device['attachmentPoint']=attachpoints
615 devices.append(device)
616
Ubuntu82b8a832013-02-06 22:00:11 +0000617 js = json.dumps(devices)
618 resp = Response(js, status=200, mimetype='application/json')
619 return resp
620
621#{"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}
622
Ubuntu82b8a832013-02-06 22:00:11 +0000623## return fake stat for now
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800624@app.route("/wm/floodlight/core/switch/<switchId>/<statType>/json")
Ubuntu82b8a832013-02-06 22:00:11 +0000625def switch_stat(switchId, statType):
626 if statType == "desc":
627 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
628 ret = {}
629 ret[switchId]=desc
630 elif statType == "aggregate":
631 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
632 ret = {}
633 ret[switchId]=aggr
634 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700635 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000636
637 js = json.dumps(ret)
638 resp = Response(js, status=200, mimetype='application/json')
639 return resp
640
641
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800642@app.route("/wm/onos/linkdiscovery/links/json")
Ubuntu82b8a832013-02-06 22:00:11 +0000643def query_links():
644 try:
645 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000646 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000647 result = os.popen(command).read()
648 parsedResult = json.loads(result)['results']
649 except:
650 log_error("REST IF has issue: %s" % command)
651 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700652 return
653# sys.exit(0)
Ubuntu82b8a832013-02-06 22:00:11 +0000654
655 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000656# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000657 sport = []
658 links = []
659 for v in parsedResult:
660 srcport = v['_id']
661 try:
662 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
663 print command
664 result = os.popen(command).read()
665 linkResults = json.loads(result)['results']
666 except:
667 log_error("REST IF has issue: %s" % command)
668 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700669 return
670# sys.exit(0)
Ubuntu82b8a832013-02-06 22:00:11 +0000671
672 for p in linkResults:
673 if p.has_key('type') and p['type'] == "port":
674 dstport = p['_id']
675 (sport, sdpid) = portV_to_port_dpid(srcport)
676 (dport, ddpid) = portV_to_port_dpid(dstport)
677 link = {}
678 link["src-switch"]=sdpid
679 link["src-port"]=sport
680 link["src-port-state"]=0
681 link["dst-switch"]=ddpid
682 link["dst-port"]=dport
683 link["dst-port-state"]=0
684 link["type"]="internal"
685 links.append(link)
686
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000687# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000688 js = json.dumps(links)
689 resp = Response(js, status=200, mimetype='application/json')
690 return resp
691
Masayoshi Kobayashibcb03c02014-01-22 15:18:49 -0800692@app.route("/controller_status_old")
693def controller_status_old():
Tim Lindberg201ade22013-04-05 11:52:08 -0700694# onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700695 onos_check="cd; onos status | grep %s | awk '{print $2}'"
Ubuntuc016ba12013-02-27 21:53:41 +0000696 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
697
698 cont_status=[]
699 for i in controllers:
700 status={}
701 onos=os.popen(onos_check % i).read()[:-1]
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700702# onos=os.popen(onos_check % (i, i.lower())).read()[:-1]
Ubuntuc016ba12013-02-27 21:53:41 +0000703 status["name"]=i
704 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000705 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000706 cont_status.append(status)
707
708 js = json.dumps(cont_status)
709 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000710 return resp
711
Masayoshi Kobayashibcb03c02014-01-22 15:18:49 -0800712
713@app.route("/controller_status")
714def controller_status():
715 url= "%s:%d/wm/onos/registry/controllers/json" % (RestIP, RestPort)
716 (code, result) = get_json(url)
717 parsedResult = json.loads(result)
718
719 cont_status=[]
720 for i in controllers:
721 status={}
722 if i in parsedResult:
723 onos=1
724 else:
725 onos=0
726 status["name"]=i
727 status["onos"]=onos
728 status["cassandra"]=0
729 cont_status.append(status)
730
731 js = json.dumps(cont_status)
732 resp = Response(js, status=200, mimetype='application/json')
733 return resp
734
735
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000736### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000737@app.route("/gui/controller/<cmd>/<controller_name>")
738def controller_status_change(cmd, controller_name):
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000739 if (TESTBED == "hw"):
Tim Lindberg34c9ff62013-04-10 15:06:17 -0700740 start_onos="/home/admin/bin/onos start %s" % (controller_name[-1:])
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700741# start_onos="/home/admin/bin/onos start %s > /tmp/debug " % (controller_name[-1:])
Tim Lindberg34c9ff62013-04-10 15:06:17 -0700742 stop_onos="/home/admin/bin/onos stop %s" % (controller_name[-1:])
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700743# stop_onos="/home/admin/bin/onos stop %s > /tmp/debug " % (controller_name[-1:])
744# print "Debug: Controller command %s called %s" % (cmd, controller_name)
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000745 else:
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700746 # No longer use -i to specify keys (use .ssh/config to specify it)
Pavlin Radoslavov1a9c23d2013-11-27 11:23:46 -0800747 start_onos="ssh %s \"cd ONOS; ./start-onos.sh start\"" % (controller_name)
748 stop_onos="ssh %s \"cd ONOS; ./start-onos.sh stop\"" % (controller_name)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700749# start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
750# stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000751
752 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000753 result=os.popen(start_onos).read()
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700754 ret = "controller %s is up: %s" % (controller_name, result)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000755 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000756 result=os.popen(stop_onos).read()
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700757 ret = "controller %s is down: %s" % (controller_name, result)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000758
759 return ret
760
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000761@app.route("/gui/switchctrl/<cmd>")
762def switch_controller_setting(cmd):
763 if cmd =="local":
764 print "All aggr switches connects to local controller only"
765 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700766 if (TESTBED == "sw"):
Jonathan Hart4dbaa502013-04-13 20:21:29 -0700767 for i in range(1, len(controllers)):
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700768 cmd_string="ssh %s 'cd ONOS/scripts; ./ctrl-local.sh'" % (controllers[i])
769 result += os.popen(cmd_string).read()
Tim Lindberg201ade22013-04-05 11:52:08 -0700770 else:
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700771 cmd_string="cd; switch local > /tmp/watch"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000772 result += os.popen(cmd_string).read()
773 elif cmd =="all":
774 print "All aggr switches connects to all controllers except for core controller"
775 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700776 if (TESTBED == "sw"):
Jonathan Hart4dbaa502013-04-13 20:21:29 -0700777 for i in range(1, len(controllers)):
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700778 cmd_string="ssh %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i])
779# cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i])
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700780 print "cmd is: "+cmd_string
Tim Lindberg201ade22013-04-05 11:52:08 -0700781 result += os.popen(cmd_string).read()
Paul Greysonc090d142013-04-09 16:59:03 -0700782 else:
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700783 cmd_string="/home/admin/bin/switch all > /tmp/watch"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000784 result += os.popen(cmd_string).read()
785
786 return result
787
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700788@app.route("/gui/reset")
789def reset_demo():
Masayoshi Kobayashi1ef9ec02013-04-12 18:03:12 +0000790 if (TESTBED == "hw"):
791 cmd_string="cd ~/bin; ./demo-reset-hw.sh > /tmp/watch &"
792 else:
793 cmd_string="cd ~/ONOS/scripts; ./demo-reset-sw.sh > /tmp/watch &"
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700794 os.popen(cmd_string)
795 return "Reset"
796
797@app.route("/gui/scale")
798def scale_demo():
Masayoshi Kobayashi1ef9ec02013-04-12 18:03:12 +0000799 if (TESTBED == "hw"):
800 cmd_string="cd ~/bin; ~/bin/demo-scale-out-hw.sh > /tmp/watch &"
801 else:
802 cmd_string="cd ~/ONOS/scripts; ./demo-scale-out-sw.sh > /tmp/watch &"
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700803 os.popen(cmd_string)
804 return "scale"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000805
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000806@app.route("/gui/switch/<cmd>/<dpid>")
807def switch_status_change(cmd, dpid):
Tim Lindberg201ade22013-04-05 11:52:08 -0700808 result = ""
809 if (TESTBED == "hw"):
810 return result
811
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000812 r = re.compile(':')
813 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000814 host=controllers[0]
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700815 cmd_string="ssh %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
816# cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000817 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000818 print "cmd_string"
819
820 if cmd =="up" or cmd=="down":
821 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000822 os.popen(cmd_string)
823 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000824
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000825 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000826
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000827#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000828#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000829@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
830def link_up(src_dpid, src_port, dst_dpid, dst_port):
Tim Lindberg201ade22013-04-05 11:52:08 -0700831 result = ""
832
Paul Greysonc090d142013-04-09 16:59:03 -0700833 if (TESTBED == "sw"):
Tim Lindberg201ade22013-04-05 11:52:08 -0700834 result = link_up_sw(src_dpid, src_port, dst_dpid, dst_port)
835 else:
836 result = link_up_hw(src_dpid, src_port, dst_dpid, dst_port)
837 return result
838
839# Link up on software testbed
840def link_up_sw(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000841
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000842 cmd = 'up'
843 result=""
Paul Greyson472da4c2013-03-28 11:43:17 -0700844 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000845 if dpid in core_switches:
846 host = controllers[0]
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000847 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000848 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000849 host = controllers[hostid-1]
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000850
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +0000851 if dpid == src_dpid:
852 (port, dontcare) = get_link_ports(dpid, dst_dpid)
853 else:
854 (port, dontcare) = get_link_ports(dpid, src_dpid)
855
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700856# cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
857 cmd_string="ssh %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +0000858 print cmd_string
859 res=os.popen(cmd_string).read()
860 result = result + ' ' + res
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000861
862 return result
863
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +0000864# if hostid == 2 :
865# src_ports = [51]
866# else :
867# src_ports = [26]
868#
869# for port in src_ports :
870# cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
871# print cmd_string
872# res=os.popen(cmd_string).read()
873
874
875
Tim Lindberg201ade22013-04-05 11:52:08 -0700876# Link up on hardware testbed
877def link_up_hw(src_dpid, src_port, dst_dpid, dst_port):
878
879 port1 = src_port
880 port2 = dst_port
881 if src_dpid == "00:00:00:00:ba:5e:ba:11":
882 if dst_dpid == "00:00:00:08:a2:08:f9:01":
883 port1 = 24
884 port2 = 24
885 elif dst_dpid == "00:01:00:16:97:08:9a:46":
886 port1 = 23
887 port2 = 23
888 elif src_dpid == "00:00:00:00:ba:5e:ba:13":
889 if dst_dpid == "00:00:20:4e:7f:51:8a:35":
890 port1 = 22
891 port2 = 22
892 elif dst_dpid == "00:00:00:00:00:00:ba:12":
893 port1 = 23
894 port2 = 23
895 elif src_dpid == "00:00:00:00:00:00:ba:12":
896 if dst_dpid == "00:00:00:00:ba:5e:ba:13":
897 port1 = 23
898 port2 = 23
899 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
900 port1 = 22
901 port2 = 22
902 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
903 port1 = 24
904 port2 = 21
905 elif src_dpid == "00:01:00:16:97:08:9a:46":
906 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
907 port1 = 23
908 port2 = 23
909 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
910 port1 = 24
911 port2 = 24
912 elif src_dpid == "00:00:00:08:a2:08:f9:01":
913 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
914 port1 = 24
915 port2 = 24
916 elif dst_dpid == "00:00:00:00:00:00:ba:12":
917 port1 = 22
918 port2 = 22
919 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
920 port1 = 23
921 port2 = 23
922 elif src_dpid == "00:00:20:4e:7f:51:8a:35":
923 if dst_dpid == "00:00:00:00:00:00:ba:12":
924 port1 = 21
925 port2 = 24
926 elif dst_dpid == "00:00:00:00:ba:5e:ba:13":
927 port1 = 22
928 port2 = 22
929 elif dst_dpid == "00:01:00:16:97:08:9a:46":
930 port1 = 24
931 port2 = 24
932 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
933 port1 = 23
934 port2 = 23
935
936 cmd = 'up'
937 result=""
938 host = controllers[0]
Tim Lindbergb03673d2013-04-10 13:47:31 -0700939 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % (src_dpid, port1, cmd)
Tim Lindberg201ade22013-04-05 11:52:08 -0700940 print cmd_string
941 res=os.popen(cmd_string).read()
942 result = result + ' ' + res
Tim Lindbergb03673d2013-04-10 13:47:31 -0700943 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % (dst_dpid, port2, cmd)
Tim Lindberg201ade22013-04-05 11:52:08 -0700944 print cmd_string
945 res=os.popen(cmd_string).read()
946 result = result + ' ' + res
947
948
949 return result
950
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000951
952#* Link Down
953#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000954@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000955def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
956
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000957 if src_dpid in core_switches:
958 host = controllers[0]
959 else:
960 hostid=int(src_dpid.split(':')[-2])
961 host = controllers[hostid-1]
962
Tim Lindberg201ade22013-04-05 11:52:08 -0700963 if (TESTBED == "sw"):
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700964 cmd_string="ssh %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
Tim Lindberg201ade22013-04-05 11:52:08 -0700965 else:
Tim Lindberga6c04172013-04-05 17:34:05 -0700966 if ( src_dpid == "00:00:00:08:a2:08:f9:01" ):
Tim Lindbergb03673d2013-04-10 13:47:31 -0700967 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % ( dst_dpid, dst_port, cmd)
Tim Lindberga6c04172013-04-05 17:34:05 -0700968 else:
Tim Lindbergb03673d2013-04-10 13:47:31 -0700969 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % ( src_dpid, src_port, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000970 print cmd_string
971
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000972 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000973
974 return result
975
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000976#* Create Flow
977#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000978#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 +0000979@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000980def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
Masayoshi Kobayashiab01bcf2013-04-09 04:21:57 +0000981 host = pick_host()
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800982 url ="%s/wm/onos/flows/getsummary/%s/%s/json" % (host, 0, 0)
Masayoshi Kobayashiab01bcf2013-04-09 04:21:57 +0000983 (code, result) = get_json(url)
984 parsedResult = json.loads(result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700985 if len(parsedResult) > 0:
986 if parsedResult[-1].has_key('flowId'):
Pavlin Radoslavov2e742572013-11-26 18:50:23 -0800987 flow_nr = int(parsedResult[-1]['flowId']['value'], 16)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700988 else:
989 flow_nr = -1 # first flow
990 print "first flow"
991
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000992 flow_nr += 1
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700993 command = "%s/web/add_flow.py -m onos %d %s %s %s %s %s matchSrcMac %s matchDstMac %s" % (ONOSDIR, flow_nr, "dummy", src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC)
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000994 flow_nr += 1
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700995 command1 = "%s/web/add_flow.py -m onos %d %s %s %s %s %s matchSrcMac %s matchDstMac %s" % (ONOSDIR, flow_nr, "dummy", dst_dpid, dst_port, src_dpid, src_port, dstMAC, srcMAC)
Tim Lindberg6445a182013-04-15 10:17:18 -0700996 print "add flow: %s, %s" % (command, command1)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000997 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000998 errcode1 = os.popen(command1).read()
Tim Lindberg6445a182013-04-15 10:17:18 -0700999 ret=command+":"+errcode+" "+command1+":"+errcode1
1000 print ret
1001 return ret
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001002
Masayoshi Kobayashi51011522013-03-27 00:18:12 +00001003#* Delete Flow
1004#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001005@app.route("/gui/delflow/<flow_id>")
1006def del_flow(flow_id):
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -07001007 command = "%/web/delete_flow.py %s" % (ONOSDIR, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001008 print command
1009 errcode = os.popen(command).read()
1010 return errcode
1011
Masayoshi Kobayashi51011522013-03-27 00:18:12 +00001012#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -07001013#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001014@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
1015def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +00001016 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -08001017 command = "curl -s \'http://%s:%s/wm/onos/flows/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +00001018 print command
1019 result = os.popen(command).read()
1020 if len(result) == 0:
1021 print "No Flow found"
Tim Lindberg6445a182013-04-15 10:17:18 -07001022 return "Flow %s not found" % (flow_id);
Masayoshi Kobayashifd566312013-04-01 10:34:01 +00001023 except:
1024 print "REST IF has issue"
Tim Lindberg6445a182013-04-15 10:17:18 -07001025 return "REST IF has issue"
Masayoshi Kobayashifd566312013-04-01 10:34:01 +00001026 exit
1027
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001028 parsedResult = json.loads(result)
1029
1030 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +00001031 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
1032 src_port = parsedResult['dataPath']['srcPort']['port']['value']
1033 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
1034 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001035# 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 +00001036
1037 if src_dpid in core_switches:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -07001038 src_host = controllers[0]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +00001039 else:
1040 hostid=int(src_dpid.split(':')[-2])
Tim Lindbergb03673d2013-04-10 13:47:31 -07001041 if TESTBED == "hw":
1042 src_host = "mininet%i" % hostid
1043 else:
1044 src_host = controllers[hostid-1]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +00001045
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -07001046 if dst_dpid in core_switches:
1047 dst_host = controllers[0]
1048 else:
1049 hostid=int(dst_dpid.split(':')[-2])
Tim Lindbergb03673d2013-04-10 13:47:31 -07001050 if TESTBED == "hw":
1051 dst_host = "mininet%i" % hostid
1052 else:
1053 dst_host = controllers[hostid-1]
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -07001054
Tim Lindbergb03673d2013-04-10 13:47:31 -07001055# /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 -07001056 protocol="udp"
1057 interval=0.1
Tim Lindbergb03673d2013-04-10 13:47:31 -07001058 if TESTBED == "hw":
1059 cmd_string="dsh -w %s 'cd ONOS/scripts; " % dst_host
1060 else:
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -07001061 cmd_string="ssh %s 'cd ONOS/scripts; " % dst_host
Tim Lindbergb03673d2013-04-10 13:47:31 -07001062 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 -07001063 print cmd_string
1064 os.popen(cmd_string)
1065
Tim Lindbergb03673d2013-04-10 13:47:31 -07001066 if TESTBED == "hw":
1067 cmd_string="dsh -w %s 'cd ONOS/scripts; " % src_host
1068 else:
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -07001069 cmd_string="ssh %s 'cd ONOS/scripts;" % src_host
Tim Lindbergb03673d2013-04-10 13:47:31 -07001070 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 +00001071 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001072 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +00001073
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +00001074 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001075
Tim Lindbergb03673d2013-04-10 13:47:31 -07001076
Masayoshi Kobayashi51011522013-03-27 00:18:12 +00001077#* Get Iperf Throughput
1078#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001079@app.route("/gui/iperf/rate/<flow_id>")
1080def iperf_rate(flow_id):
1081 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -08001082 command = "curl -s \'http://%s:%s/wm/onos/flows/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001083 print command
1084 result = os.popen(command).read()
1085 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001086 resp = Response(result, status=400, mimetype='text/html')
1087 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001088 except:
1089 print "REST IF has issue"
1090 exit
1091
1092 parsedResult = json.loads(result)
1093
1094 flowId = int(parsedResult['flowId']['value'], 16)
1095 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
1096 src_port = parsedResult['dataPath']['srcPort']['port']['value']
1097 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
1098 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
1099
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -07001100 if dst_dpid in core_switches:
Tim Lindbergb03673d2013-04-10 13:47:31 -07001101 host = controllers[0]
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001102 else:
Tim Lindbergb03673d2013-04-10 13:47:31 -07001103 hostid=int(dst_dpid.split(':')[-2])
1104 if TESTBED == "hw":
1105 host = "mininet%i" % hostid
1106 else:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001107 host = controllers[hostid-1]
1108
1109 try:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -07001110 command = "curl -s http://%s:%s/log/iperfsvr_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001111 print command
1112 result = os.popen(command).read()
1113 except:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001114 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +00001115
Tim Lindberg6445a182013-04-15 10:17:18 -07001116 if re.match("Cannot", result):
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +00001117 resp = Response(result, status=400, mimetype='text/html')
Tim Lindberg6445a182013-04-15 10:17:18 -07001118 return "no iperf file found (host %s flowid %s): %s" % (host, flow_id, result)
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +00001119 else:
1120 resp = Response(result, status=200, mimetype='application/json')
1121 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +00001122
Ubuntu82b8a832013-02-06 22:00:11 +00001123if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001124 random.seed()
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +00001125 read_config()
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +00001126 read_link_def()
Ubuntu82b8a832013-02-06 22:00:11 +00001127 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001128# 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")
1129# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
1130# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
1131# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
1132# 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 +00001133# print "-- query all switches --"
1134# query_switch()
1135# print "-- query topo --"
1136# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +00001137# link_change(1,2,3,4)
1138 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +00001139# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +00001140# print "-- query all devices --"
1141# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +00001142# iperf_start(1,10,15)
1143# iperf_rate(1)
Masayoshi Kobayashiab01bcf2013-04-09 04:21:57 +00001144# switches()
Tim Lindberg9ec5e222013-04-12 10:46:02 -07001145# add_flow(1,2,3,4,5,6)
1146 reset_demo()
Ubuntu82b8a832013-02-06 22:00:11 +00001147 else:
1148 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +00001149 app.run(threaded=True, host="0.0.0.0", port=9000)