blob: 6cb57269b6a695a3d9337585ff803010ed7495f6 [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
Masayoshi Kobayashi51011522013-03-27 00:18:12 +000011import re
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -080012from urllib2 import Request, urlopen, URLError, HTTPError
Masayoshi Kobayashi51011522013-03-27 00:18:12 +000013
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):
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800117 url = "%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 -0700118 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800119 response = urlopen(url)
120 result = response.read()
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700121 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800122 result = ""
123 print "REST IF has issue %s" % url
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700124
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):
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800130 url = "%s/gui/switchctrl/%s" % (ONOS_GUI3_CONTROL_HOST, cmd)
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000131 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800132 response = urlopen(url)
133 result = response.read()
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000134 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800135 result = ""
136 print "REST IF has issue %s" % url
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000137
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):
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800143 url = "%s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
Paul Greyson8d1c6362013-03-27 13:05:24 -0700144 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800145 response = urlopen(url)
146 result = response.read()
Paul Greyson8d1c6362013-03-27 13:05:24 -0700147 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800148 result = ""
149 print "REST IF has issue %s" % url
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):
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800156 url = "%s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
Paul Greyson2913af82013-03-27 14:53:17 -0700157 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800158 response = urlopen(url)
159 result = response.read()
Paul Greyson2913af82013-03-27 14:53:17 -0700160 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800161 result = ""
162 print "REST IF has issue %s" % url
163
Paul Greyson2913af82013-03-27 14:53:17 -0700164 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:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800170 url = "%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 "proxy gui addflow " + url
172 (code, result) = get_json(url)
Paul Greyson472da4c2013-03-28 11:43:17 -0700173 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800174 print "REST IF has issue %s" % url
175 print "Result %s" % result
176 exit()
Paul Greyson472da4c2013-03-28 11:43:17 -0700177
178 resp = Response(result, status=200, mimetype='application/json')
179 return resp
180
Paul Greyson6f918402013-03-28 12:18:30 -0700181@app.route("/proxy/gui/delflow/<flow_id>")
182def proxy_del_flow(flow_id):
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800183 url = "%s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
Paul Greyson6f918402013-03-28 12:18:30 -0700184 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800185 response = urlopen(url)
186 result = response.read()
Paul Greyson6f918402013-03-28 12:18:30 -0700187 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800188 result = ""
189 print "REST IF has issue %s" % url
Paul Greyson6f918402013-03-28 12:18:30 -0700190
191 resp = Response(result, status=200, mimetype='application/json')
192 return resp
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000193
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700194@app.route("/proxy/gui/iperf/start/<flow_id>/<duration>/<samples>")
195def proxy_iperf_start(flow_id,duration,samples):
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800196 url = "%s/gui/iperf/start/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id, duration, samples)
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700197 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800198 response = urlopen(url)
199 result = response.read()
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700200 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800201 result = ""
202 print "REST IF has issue %s" % url
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700203
204 resp = Response(result, status=200, mimetype='application/json')
205 return resp
206
207@app.route("/proxy/gui/iperf/rate/<flow_id>")
208def proxy_iperf_rate(flow_id):
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800209 url = "%s/gui/iperf/rate/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700210 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800211 response = urlopen(url)
212 result = response.read()
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700213 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800214 result = ""
215 print "REST IF has issue %s" % url
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700216
217 resp = Response(result, status=200, mimetype='application/json')
218 return resp
219
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700220@app.route("/proxy/gui/reset")
221def proxy_gui_reset():
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800222 url = "%s/gui/reset" % (ONOS_GUI3_CONTROL_HOST)
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700223 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800224 response = urlopen(url)
225 result = response.read()
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700226 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800227 result = ""
228 print "REST IF has issue %s" % url
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():
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800235 url = "%s/gui/scale" % (ONOS_GUI3_CONTROL_HOST)
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700236 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800237 response = urlopen(url)
238 result = response.read()
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700239 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800240 result = ""
241 print "REST IF has issue %s" % url
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700242
243 resp = Response(result, status=200, mimetype='application/json')
244 return resp
245
246###### ONOS REST API ##############################
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000247## Worker Func ###
248def get_json(url):
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800249 code = 200;
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000250 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800251 response = urlopen(url)
252 except URLError, e:
253 print "get_json: REST IF %s has issue. Reason: %s" % (url, e.reason)
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000254 result = ""
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800255 return (500, result)
256 except HTTPError, e:
257 print "get_json: REST IF %s has issue. Code %s" % (url, e.code)
258 result = ""
259 return (e.code, result)
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000260
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800261 result = response.read()
262# parsedResult = json.loads(result)
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000263 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 ##
Jonathan Hart5caa0442014-03-19 15:20:07 -0700276@app.route("/wm/onos/topology/switches/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
Jonathan Hart5caa0442014-03-19 15:20:07 -0700283 url ="%s/wm/onos/topology/switches/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:
Jonathan Hart5caa0442014-03-19 15:20:07 -0700357 url="http://%s:%s/wm/onos/topology/switches/json" % (RestIP, RestPort)
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800358 (code, result) = get_json(url)
Ubuntu82b8a832013-02-06 22:00:11 +0000359 parsedResult = json.loads(result)
360 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800361 log_error("REST IF has issue: %s" % url)
Ubuntu82b8a832013-02-06 22:00:11 +0000362 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700363 return
Ubuntu82b8a832013-02-06 22:00:11 +0000364
365 topo = {}
366 switches = []
367 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000368 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000369
370 for v in parsedResult:
371 if v.has_key('dpid'):
372# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
373 dpid = str(v['dpid'])
374 state = str(v['state'])
375 sw = {}
376 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000377 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000378
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000379 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000380 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000381 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000382
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000383 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800384 url="http://%s:%s/wm/onos/registry/switches/json" % (RestIP, RestPort)
385 (code, result) = get_json(url)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000386 parsedResult = json.loads(result)
387 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800388 log_error("REST IF has issue: %s" % url)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000389 log_error("%s" % result)
390
391 for key in parsedResult:
392 dpid = key
393 ctrl = parsedResult[dpid][0]['controllerId']
394 sw_id = node_id(switches, dpid)
395 if sw_id != -1:
396 if switches[sw_id]['group'] != 0:
397 switches[sw_id]['group'] = controllers.index(ctrl) + 1
398
Ubuntu82b8a832013-02-06 22:00:11 +0000399 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800400 url = "http://%s:%s/wm/onos/topology/links/json" % (RestIP, RestPort)
401 (code, result) = get_json(url)
Ubuntu82b8a832013-02-06 22:00:11 +0000402 parsedResult = json.loads(result)
403 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800404 log_error("REST IF has issue: %s" % url)
Ubuntu82b8a832013-02-06 22:00:11 +0000405 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700406 return
407# sys.exit(0)
Ubuntu82b8a832013-02-06 22:00:11 +0000408
409 for v in parsedResult:
410 link = {}
411 if v.has_key('dst-switch'):
412 dst_dpid = str(v['dst-switch'])
413 dst_id = node_id(switches, dst_dpid)
414 if v.has_key('src-switch'):
415 src_dpid = str(v['src-switch'])
416 src_id = node_id(switches, src_dpid)
417 link['source'] = src_id
418 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000419
Jonathan Hartbf66dff2013-10-28 14:07:41 -0700420 #onpath = 0
421 #for (s,d) in path:
422 # if s == v['src-switch'] and d == v['dst-switch']:
423 # onpath = 1
424 # break
425 #link['type'] = onpath
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000426
Ubuntu82b8a832013-02-06 22:00:11 +0000427 links.append(link)
428
429 topo['nodes'] = switches
430 topo['links'] = links
431
Ubuntu82b8a832013-02-06 22:00:11 +0000432 js = json.dumps(topo)
433 resp = Response(js, status=200, mimetype='application/json')
434 return resp
435
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800436@app.route("/wm/floodlight/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
Ubuntuaea2a682013-02-08 08:30:10 +0000437def shortest_path(v1, p1, v2, p2):
438 try:
Jonathan Hart5caa0442014-03-19 15:20:07 -0700439 url = "http://%s:%s/wm/onos/topology/switches/json" % (RestIP, RestPort)
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800440 (code, result) = get_json(url)
Ubuntuaea2a682013-02-08 08:30:10 +0000441 parsedResult = json.loads(result)
442 except:
443 log_error("REST IF has issue: %s" % command)
444 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700445 return
Ubuntuaea2a682013-02-08 08:30:10 +0000446
447 topo = {}
448 switches = []
449 links = []
450
451 for v in parsedResult:
452 if v.has_key('dpid'):
453 dpid = str(v['dpid'])
454 state = str(v['state'])
455 sw = {}
456 sw['name']=dpid
457 if str(v['state']) == "ACTIVE":
458 if dpid[-2:-1] == "a":
459 sw['group']=1
460 if dpid[-2:-1] == "b":
461 sw['group']=2
462 if dpid[-2:-1] == "c":
463 sw['group']=3
464 if str(v['state']) == "INACTIVE":
465 sw['group']=0
466
467 switches.append(sw)
468
469 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800470 url = "http://%s:%s/wm/onos/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
471 (code, result) = get_json(url)
Ubuntuaea2a682013-02-08 08:30:10 +0000472 parsedResult = json.loads(result)
473 except:
474 log_error("No route")
475 parsedResult = []
Ubuntuaea2a682013-02-08 08:30:10 +0000476
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700477 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000478 for i, v in enumerate(parsedResult):
479 if i < len(parsedResult) - 1:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800480 sdpid= parsedResult['flowEntries'][i]['dpid']['value']
481 ddpid= parsedResult['flowEntries'][i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700482 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000483
484 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800485 url = "http://%s:%s/wm/onos/topology/links/json" % (RestIP, RestPort)
486 (code, result) = get_json(url)
Ubuntuaea2a682013-02-08 08:30:10 +0000487 parsedResult = json.loads(result)
488 except:
489 log_error("REST IF has issue: %s" % command)
490 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700491 return
Ubuntuaea2a682013-02-08 08:30:10 +0000492
493 for v in parsedResult:
494 link = {}
495 if v.has_key('dst-switch'):
496 dst_dpid = str(v['dst-switch'])
497 dst_id = node_id(switches, dst_dpid)
498 if v.has_key('src-switch'):
499 src_dpid = str(v['src-switch'])
500 src_id = node_id(switches, src_dpid)
501 link['source'] = src_id
502 link['target'] = dst_id
503 onpath = 0
504 for (s,d) in path:
505 if s == v['src-switch'] and d == v['dst-switch']:
506 onpath = 1
507 break
508
509 link['type'] = onpath
510 links.append(link)
511
512 topo['nodes'] = switches
513 topo['links'] = links
514
Ubuntuaea2a682013-02-08 08:30:10 +0000515 js = json.dumps(topo)
516 resp = Response(js, status=200, mimetype='application/json')
517 return resp
518
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800519@app.route("/wm/floodlight/core/controller/switches/json")
Ubuntu82b8a832013-02-06 22:00:11 +0000520def query_switch():
521 try:
Jonathan Hart5caa0442014-03-19 15:20:07 -0700522 url = "http://%s:%s/wm/onos/topology/switches/json" % (RestIP, RestPort)
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800523 (code, result) = get_json(url)
Ubuntu82b8a832013-02-06 22:00:11 +0000524 parsedResult = json.loads(result)
525 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800526 log_error("REST IF has issue: %s" % url)
Ubuntu82b8a832013-02-06 22:00:11 +0000527 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700528 return
529# sys.exit(0)
Ubuntu82b8a832013-02-06 22:00:11 +0000530
531# print command
532# print result
533 switches_ = []
534 for v in parsedResult:
535 if v.has_key('dpid'):
536 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
537 dpid = str(v['dpid'])
538 state = str(v['state'])
539 sw = {}
540 sw['dpid']=dpid
541 sw['active']=state
542 switches_.append(sw)
543
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000544# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000545 js = json.dumps(switches_)
546 resp = Response(js, status=200, mimetype='application/json')
547 return resp
548
Ubuntu82b8a832013-02-06 22:00:11 +0000549## return fake stat for now
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800550@app.route("/wm/floodlight/core/switch/<switchId>/<statType>/json")
Ubuntu82b8a832013-02-06 22:00:11 +0000551def switch_stat(switchId, statType):
552 if statType == "desc":
553 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
554 ret = {}
555 ret[switchId]=desc
556 elif statType == "aggregate":
557 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
558 ret = {}
559 ret[switchId]=aggr
560 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700561 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000562
563 js = json.dumps(ret)
564 resp = Response(js, status=200, mimetype='application/json')
565 return resp
566
Masayoshi Kobayashibcb03c02014-01-22 15:18:49 -0800567@app.route("/controller_status")
568def controller_status():
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800569 url= "http://%s:%d/wm/onos/registry/controllers/json" % (RestIP, RestPort)
Masayoshi Kobayashibcb03c02014-01-22 15:18:49 -0800570 (code, result) = get_json(url)
571 parsedResult = json.loads(result)
572
573 cont_status=[]
574 for i in controllers:
575 status={}
576 if i in parsedResult:
577 onos=1
578 else:
579 onos=0
580 status["name"]=i
581 status["onos"]=onos
582 status["cassandra"]=0
583 cont_status.append(status)
584
585 js = json.dumps(cont_status)
586 resp = Response(js, status=200, mimetype='application/json')
587 return resp
588
589
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000590### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000591@app.route("/gui/controller/<cmd>/<controller_name>")
592def controller_status_change(cmd, controller_name):
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000593 if (TESTBED == "hw"):
Tim Lindberg34c9ff62013-04-10 15:06:17 -0700594 start_onos="/home/admin/bin/onos start %s" % (controller_name[-1:])
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700595# start_onos="/home/admin/bin/onos start %s > /tmp/debug " % (controller_name[-1:])
Tim Lindberg34c9ff62013-04-10 15:06:17 -0700596 stop_onos="/home/admin/bin/onos stop %s" % (controller_name[-1:])
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700597# stop_onos="/home/admin/bin/onos stop %s > /tmp/debug " % (controller_name[-1:])
598# print "Debug: Controller command %s called %s" % (cmd, controller_name)
Masayoshi Kobayashidf787a42013-04-09 01:18:48 +0000599 else:
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700600 # No longer use -i to specify keys (use .ssh/config to specify it)
Pavlin Radoslavov1a9c23d2013-11-27 11:23:46 -0800601 start_onos="ssh %s \"cd ONOS; ./start-onos.sh start\"" % (controller_name)
602 stop_onos="ssh %s \"cd ONOS; ./start-onos.sh stop\"" % (controller_name)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700603# start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
604# stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000605
606 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000607 result=os.popen(start_onos).read()
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700608 ret = "controller %s is up: %s" % (controller_name, result)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000609 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000610 result=os.popen(stop_onos).read()
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700611 ret = "controller %s is down: %s" % (controller_name, result)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000612
613 return ret
614
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000615@app.route("/gui/switchctrl/<cmd>")
616def switch_controller_setting(cmd):
617 if cmd =="local":
618 print "All aggr switches connects to local controller only"
619 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700620 if (TESTBED == "sw"):
Jonathan Hart4dbaa502013-04-13 20:21:29 -0700621 for i in range(1, len(controllers)):
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700622 cmd_string="ssh %s 'cd ONOS/scripts; ./ctrl-local.sh'" % (controllers[i])
623 result += os.popen(cmd_string).read()
Tim Lindberg201ade22013-04-05 11:52:08 -0700624 else:
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700625 cmd_string="cd; switch local > /tmp/watch"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000626 result += os.popen(cmd_string).read()
627 elif cmd =="all":
628 print "All aggr switches connects to all controllers except for core controller"
629 result=""
Tim Lindberg201ade22013-04-05 11:52:08 -0700630 if (TESTBED == "sw"):
Jonathan Hart4dbaa502013-04-13 20:21:29 -0700631 for i in range(1, len(controllers)):
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700632 cmd_string="ssh %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i])
633# cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i])
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700634 print "cmd is: "+cmd_string
Tim Lindberg201ade22013-04-05 11:52:08 -0700635 result += os.popen(cmd_string).read()
Paul Greysonc090d142013-04-09 16:59:03 -0700636 else:
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700637 cmd_string="/home/admin/bin/switch all > /tmp/watch"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000638 result += os.popen(cmd_string).read()
639
640 return result
641
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700642@app.route("/gui/reset")
643def reset_demo():
Masayoshi Kobayashi1ef9ec02013-04-12 18:03:12 +0000644 if (TESTBED == "hw"):
645 cmd_string="cd ~/bin; ./demo-reset-hw.sh > /tmp/watch &"
646 else:
647 cmd_string="cd ~/ONOS/scripts; ./demo-reset-sw.sh > /tmp/watch &"
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700648 os.popen(cmd_string)
649 return "Reset"
650
651@app.route("/gui/scale")
652def scale_demo():
Masayoshi Kobayashi1ef9ec02013-04-12 18:03:12 +0000653 if (TESTBED == "hw"):
654 cmd_string="cd ~/bin; ~/bin/demo-scale-out-hw.sh > /tmp/watch &"
655 else:
656 cmd_string="cd ~/ONOS/scripts; ./demo-scale-out-sw.sh > /tmp/watch &"
Tim Lindberg9ec5e222013-04-12 10:46:02 -0700657 os.popen(cmd_string)
658 return "scale"
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000659
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000660@app.route("/gui/switch/<cmd>/<dpid>")
661def switch_status_change(cmd, dpid):
Tim Lindberg201ade22013-04-05 11:52:08 -0700662 result = ""
663 if (TESTBED == "hw"):
664 return result
665
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000666 r = re.compile(':')
667 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000668 host=controllers[0]
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700669 cmd_string="ssh %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
670# 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 +0000671 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000672 print "cmd_string"
673
674 if cmd =="up" or cmd=="down":
675 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000676 os.popen(cmd_string)
677 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000678
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000679 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000680
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000681#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000682#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000683@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
684def link_up(src_dpid, src_port, dst_dpid, dst_port):
Tim Lindberg201ade22013-04-05 11:52:08 -0700685 result = ""
686
Paul Greysonc090d142013-04-09 16:59:03 -0700687 if (TESTBED == "sw"):
Tim Lindberg201ade22013-04-05 11:52:08 -0700688 result = link_up_sw(src_dpid, src_port, dst_dpid, dst_port)
689 else:
690 result = link_up_hw(src_dpid, src_port, dst_dpid, dst_port)
691 return result
692
693# Link up on software testbed
694def link_up_sw(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000695
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000696 cmd = 'up'
697 result=""
Paul Greyson472da4c2013-03-28 11:43:17 -0700698 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000699 if dpid in core_switches:
700 host = controllers[0]
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000701 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000702 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000703 host = controllers[hostid-1]
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000704
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +0000705 if dpid == src_dpid:
706 (port, dontcare) = get_link_ports(dpid, dst_dpid)
707 else:
708 (port, dontcare) = get_link_ports(dpid, src_dpid)
709
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700710# cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
711 cmd_string="ssh %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +0000712 print cmd_string
713 res=os.popen(cmd_string).read()
714 result = result + ' ' + res
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000715
716 return result
717
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +0000718# if hostid == 2 :
719# src_ports = [51]
720# else :
721# src_ports = [26]
722#
723# for port in src_ports :
724# cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
725# print cmd_string
726# res=os.popen(cmd_string).read()
727
728
729
Tim Lindberg201ade22013-04-05 11:52:08 -0700730# Link up on hardware testbed
731def link_up_hw(src_dpid, src_port, dst_dpid, dst_port):
732
733 port1 = src_port
734 port2 = dst_port
735 if src_dpid == "00:00:00:00:ba:5e:ba:11":
736 if dst_dpid == "00:00:00:08:a2:08:f9:01":
737 port1 = 24
738 port2 = 24
739 elif dst_dpid == "00:01:00:16:97:08:9a:46":
740 port1 = 23
741 port2 = 23
742 elif src_dpid == "00:00:00:00:ba:5e:ba:13":
743 if dst_dpid == "00:00:20:4e:7f:51:8a:35":
744 port1 = 22
745 port2 = 22
746 elif dst_dpid == "00:00:00:00:00:00:ba:12":
747 port1 = 23
748 port2 = 23
749 elif src_dpid == "00:00:00:00:00:00:ba:12":
750 if dst_dpid == "00:00:00:00:ba:5e:ba:13":
751 port1 = 23
752 port2 = 23
753 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
754 port1 = 22
755 port2 = 22
756 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
757 port1 = 24
758 port2 = 21
759 elif src_dpid == "00:01:00:16:97:08:9a:46":
760 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
761 port1 = 23
762 port2 = 23
763 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
764 port1 = 24
765 port2 = 24
766 elif src_dpid == "00:00:00:08:a2:08:f9:01":
767 if dst_dpid == "00:00:00:00:ba:5e:ba:11":
768 port1 = 24
769 port2 = 24
770 elif dst_dpid == "00:00:00:00:00:00:ba:12":
771 port1 = 22
772 port2 = 22
773 elif dst_dpid == "00:00:20:4e:7f:51:8a:35":
774 port1 = 23
775 port2 = 23
776 elif src_dpid == "00:00:20:4e:7f:51:8a:35":
777 if dst_dpid == "00:00:00:00:00:00:ba:12":
778 port1 = 21
779 port2 = 24
780 elif dst_dpid == "00:00:00:00:ba:5e:ba:13":
781 port1 = 22
782 port2 = 22
783 elif dst_dpid == "00:01:00:16:97:08:9a:46":
784 port1 = 24
785 port2 = 24
786 elif dst_dpid == "00:00:00:08:a2:08:f9:01":
787 port1 = 23
788 port2 = 23
789
790 cmd = 'up'
791 result=""
792 host = controllers[0]
Tim Lindbergb03673d2013-04-10 13:47:31 -0700793 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % (src_dpid, port1, cmd)
Tim Lindberg201ade22013-04-05 11:52:08 -0700794 print cmd_string
795 res=os.popen(cmd_string).read()
796 result = result + ' ' + res
Tim Lindbergb03673d2013-04-10 13:47:31 -0700797 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % (dst_dpid, port2, cmd)
Tim Lindberg201ade22013-04-05 11:52:08 -0700798 print cmd_string
799 res=os.popen(cmd_string).read()
800 result = result + ' ' + res
801
802
803 return result
804
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000805
806#* Link Down
807#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000808@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000809def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
810
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000811 if src_dpid in core_switches:
812 host = controllers[0]
813 else:
814 hostid=int(src_dpid.split(':')[-2])
815 host = controllers[hostid-1]
816
Tim Lindberg201ade22013-04-05 11:52:08 -0700817 if (TESTBED == "sw"):
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700818 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 -0700819 else:
Tim Lindberga6c04172013-04-05 17:34:05 -0700820 if ( src_dpid == "00:00:00:08:a2:08:f9:01" ):
Tim Lindbergb03673d2013-04-10 13:47:31 -0700821 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % ( dst_dpid, dst_port, cmd)
Tim Lindberga6c04172013-04-05 17:34:05 -0700822 else:
Tim Lindbergb03673d2013-04-10 13:47:31 -0700823 cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % ( src_dpid, src_port, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000824 print cmd_string
825
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000826 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000827
828 return result
829
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000830#* Create Flow
831#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000832#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 +0000833@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000834def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
Masayoshi Kobayashiab01bcf2013-04-09 04:21:57 +0000835 host = pick_host()
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800836 url ="%s/wm/onos/flows/getsummary/%s/%s/json" % (host, 0, 0)
Masayoshi Kobayashiab01bcf2013-04-09 04:21:57 +0000837 (code, result) = get_json(url)
838 parsedResult = json.loads(result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700839 if len(parsedResult) > 0:
840 if parsedResult[-1].has_key('flowId'):
Pavlin Radoslavov2e742572013-11-26 18:50:23 -0800841 flow_nr = int(parsedResult[-1]['flowId']['value'], 16)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700842 else:
843 flow_nr = -1 # first flow
844 print "first flow"
845
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000846 flow_nr += 1
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700847 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 +0000848 flow_nr += 1
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700849 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 -0700850 print "add flow: %s, %s" % (command, command1)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000851 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000852 errcode1 = os.popen(command1).read()
Tim Lindberg6445a182013-04-15 10:17:18 -0700853 ret=command+":"+errcode+" "+command1+":"+errcode1
854 print ret
855 return ret
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000856
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000857#* Delete Flow
858#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000859@app.route("/gui/delflow/<flow_id>")
860def del_flow(flow_id):
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700861 command = "%/web/delete_flow.py %s" % (ONOSDIR, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000862 print command
863 errcode = os.popen(command).read()
864 return errcode
865
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000866#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700867#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000868@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
869def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800870 url = "http://%s:%s/wm/onos/flows/get/%s/json" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000871 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800872 response = urlopen(url)
873 result = response.read()
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000874 if len(result) == 0:
875 print "No Flow found"
Tim Lindberg6445a182013-04-15 10:17:18 -0700876 return "Flow %s not found" % (flow_id);
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000877 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800878 print "REST IF has issue %s" % url
879 return "REST IF has issue %s" % url
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000880
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000881 parsedResult = json.loads(result)
882
883 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000884 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
885 src_port = parsedResult['dataPath']['srcPort']['port']['value']
886 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
887 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000888# 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 +0000889
890 if src_dpid in core_switches:
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700891 src_host = controllers[0]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000892 else:
893 hostid=int(src_dpid.split(':')[-2])
Tim Lindbergb03673d2013-04-10 13:47:31 -0700894 if TESTBED == "hw":
895 src_host = "mininet%i" % hostid
896 else:
897 src_host = controllers[hostid-1]
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000898
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700899 if dst_dpid in core_switches:
900 dst_host = controllers[0]
901 else:
902 hostid=int(dst_dpid.split(':')[-2])
Tim Lindbergb03673d2013-04-10 13:47:31 -0700903 if TESTBED == "hw":
904 dst_host = "mininet%i" % hostid
905 else:
906 dst_host = controllers[hostid-1]
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700907
Tim Lindbergb03673d2013-04-10 13:47:31 -0700908# /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 -0700909 protocol="udp"
910 interval=0.1
Tim Lindbergb03673d2013-04-10 13:47:31 -0700911 if TESTBED == "hw":
912 cmd_string="dsh -w %s 'cd ONOS/scripts; " % dst_host
913 else:
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700914 cmd_string="ssh %s 'cd ONOS/scripts; " % dst_host
Tim Lindbergb03673d2013-04-10 13:47:31 -0700915 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 -0700916 print cmd_string
917 os.popen(cmd_string)
918
Tim Lindbergb03673d2013-04-10 13:47:31 -0700919 if TESTBED == "hw":
920 cmd_string="dsh -w %s 'cd ONOS/scripts; " % src_host
921 else:
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700922 cmd_string="ssh %s 'cd ONOS/scripts;" % src_host
Tim Lindbergb03673d2013-04-10 13:47:31 -0700923 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 +0000924 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000925 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000926
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000927 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000928
Tim Lindbergb03673d2013-04-10 13:47:31 -0700929
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000930#* Get Iperf Throughput
931#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000932@app.route("/gui/iperf/rate/<flow_id>")
933def iperf_rate(flow_id):
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800934 url = "http://%s:%s/wm/onos/flows/get/%s/json" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000935 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800936 response = urlopen(url)
937 result = response.read()
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000938 if len(result) == 0:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800939 return "no such iperf flow (flowid %s)" % flow_id
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000940 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800941 print "REST IF has issue %s" % url
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000942 exit
943
944 parsedResult = json.loads(result)
945
946 flowId = int(parsedResult['flowId']['value'], 16)
947 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
948 src_port = parsedResult['dataPath']['srcPort']['port']['value']
949 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
950 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
951
Umesh Krishnaswamy5c3eddf2013-04-06 00:24:01 -0700952 if dst_dpid in core_switches:
Tim Lindbergb03673d2013-04-10 13:47:31 -0700953 host = controllers[0]
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000954 else:
Tim Lindbergb03673d2013-04-10 13:47:31 -0700955 hostid=int(dst_dpid.split(':')[-2])
956 if TESTBED == "hw":
957 host = "mininet%i" % hostid
958 else:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000959 host = controllers[hostid-1]
960
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800961 url="http://%s:%s/log/iperfsvr_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000962 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800963 response = urlopen(url)
964 result = response.read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000965 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800966 print "REST IF has issue %s" % url
967 return
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000968
Tim Lindberg6445a182013-04-15 10:17:18 -0700969 if re.match("Cannot", result):
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +0000970 resp = Response(result, status=400, mimetype='text/html')
Tim Lindberg6445a182013-04-15 10:17:18 -0700971 return "no iperf file found (host %s flowid %s): %s" % (host, flow_id, result)
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +0000972 else:
973 resp = Response(result, status=200, mimetype='application/json')
974 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000975
Ubuntu82b8a832013-02-06 22:00:11 +0000976if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000977 random.seed()
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +0000978 read_config()
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +0000979 read_link_def()
Ubuntu82b8a832013-02-06 22:00:11 +0000980 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800981 # for debugging
982 #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")
983 #proxy_link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
984 #proxy_link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
985 #proxy_link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
986 #proxy_link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1)
987 #print "-- query all switches --"
988 #query_switch()
989 #print "-- query topo --"
990 #topology_for_gui()
991 ##print "-- query all links --"
992 ##query_links()
993 #print "-- query all devices --"
994 #devices()
995 #links()
996 #switches()
997 #reset_demo()
998 pass
Ubuntu82b8a832013-02-06 22:00:11 +0000999 else:
1000 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +00001001 app.run(threaded=True, host="0.0.0.0", port=9000)