blob: 392cb87d46f6ab947963d99213c67b4bb9134bd0 [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
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000016## Global Var for ON.Lab local REST ##
Ubuntuf6ce96c2013-02-07 01:45:07 +000017RestIP="localhost"
Ubuntu82b8a832013-02-06 22:00:11 +000018RestPort=8080
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000019
20## Uncomment the desired block based on your testbed environment
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000021# Settings for running on production
Pankaj Berde39194aa2013-04-05 06:41:59 -070022<<<<<<< HEAD
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000023core_switches=["00:00:00:00:ba:5e:ba:11", "00:00:00:00:00:00:ba:12", "00:00:20:4e:7f:51:8a:35", "00:00:00:00:ba:5e:ba:13", "00:00:00:08:a2:08:f9:01", "00:00:00:16:97:08:9a:46"]
Pankaj Berde2239f0d2013-04-04 09:42:43 -070024ONOS_GUI3_HOST="http://localhost:9000"
25ONOS_GUI3_CONTROL_HOST="http://localhost:9000"
Pankaj Berde39194aa2013-04-05 06:41:59 -070026=======
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +000027#controllers=["onosgui1", "onosgui2", "onosgui3", "onosgui4", "onosgui5", "onosgui6", "onosgui7", "onosgui8"]
28#core_switches=["00:00:00:00:ba:5e:ba:11", "00:00:00:00:00:00:ba:12", "00:00:20:4e:7f:51:8a:35", "00:00:00:00:ba:5e:ba:13", "00:00:00:08:a2:08:f9:01", "00:00:00:16:97:08:9a:46"]
29#ONOS_GUI3_HOST="http://gui3.onlab.us:8080"
30#ONOS_GUI3_CONTROL_HOST="http://gui3.onlab.us:8081"
Pankaj Berde39194aa2013-04-05 06:41:59 -070031>>>>>>> ecee79b1df12c337cba14cbc9e3660741ae72441
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000032
33# Settings for running on dev testbed. Replace dev
34#controllers=["onosdevb1", "onosdevb2", "onosdevb3", "onosdevb4"]
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +000035controllers=["onosdevt1", "onosdevt2", "onosdevt3", "onosdevt4", "onosdevt5", "onosdevt6", "onosdevt7", "onosdevt8"]
36core_switches=["00:00:00:00:00:00:01:01", "00:00:00:00:00:00:01:02", "00:00:00:00:00:00:01:03", "00:00:00:00:00:00:01:04", "00:00:00:00:00:00:01:05", "00:00:00:00:00:00:01:06"]
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000037
Pankaj Berde39194aa2013-04-05 06:41:59 -070038<<<<<<< HEAD
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000039ONOS_LOCAL_HOST="http://localhost:8080" ;# for Amazon EC2
Pankaj Berde2239f0d2013-04-04 09:42:43 -070040controllers=["Berde-MBP.local"]
41#controllers=["onosgui1", "onosgui2", "onosgui3", "onosgui4", "onosgui5", "onosgui6", "onosgui7", "onosgui8"]
42#core_switches=["00:00:00:00:ba:5e:ba:11", "00:00:00:00:00:00:ba:12", "00:00:20:4e:7f:51:8a:35", "00:00:00:00:ba:5e:ba:13", "00:00:00:08:a2:08:f9:01", "00:00:00:16:97:08:9a:46"]
43core_switches=["00:00:00:00:00:00:01:01", "00:00:00:00:00:00:01:02", "00:00:00:00:00:00:01:03", "00:00:00:00:00:00:01:04", "00:00:00:00:00:00:01:05", "00:00:00:00:00:00:01:06"]
Pankaj Berde39194aa2013-04-05 06:41:59 -070044=======
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +000045ONOS_GUI3_HOST="http://devt-gui.onlab.us:8080"
46ONOS_GUI3_CONTROL_HOST="http://devt-gui.onlab.us:8080"
Pankaj Berde39194aa2013-04-05 06:41:59 -070047>>>>>>> ecee79b1df12c337cba14cbc9e3660741ae72441
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +000048
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000049LB=True #; True or False
50ONOS_DEFAULT_HOST="localhost" ;# Has to set if LB=False
Ubuntu82b8a832013-02-06 22:00:11 +000051
52DEBUG=1
Ubuntu82b8a832013-02-06 22:00:11 +000053
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000054pp = pprint.PrettyPrinter(indent=4)
Ubuntu82b8a832013-02-06 22:00:11 +000055app = Flask(__name__)
56
57## Worker Functions ##
58def log_error(txt):
59 print '%s' % (txt)
60
61def debug(txt):
62 if DEBUG:
63 print '%s' % (txt)
64
Ubuntu82b8a832013-02-06 22:00:11 +000065### File Fetch ###
66@app.route('/ui/img/<filename>', methods=['GET'])
67@app.route('/img/<filename>', methods=['GET'])
68@app.route('/css/<filename>', methods=['GET'])
69@app.route('/js/models/<filename>', methods=['GET'])
70@app.route('/js/views/<filename>', methods=['GET'])
71@app.route('/js/<filename>', methods=['GET'])
72@app.route('/lib/<filename>', methods=['GET'])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +000073@app.route('/log/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000074@app.route('/', methods=['GET'])
75@app.route('/<filename>', methods=['GET'])
76@app.route('/tpl/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000077@app.route('/ons-demo/<filename>', methods=['GET'])
78@app.route('/ons-demo/js/<filename>', methods=['GET'])
79@app.route('/ons-demo/css/<filename>', methods=['GET'])
80@app.route('/ons-demo/assets/<filename>', methods=['GET'])
81@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000082def return_file(filename="index.html"):
83 if request.path == "/":
84 fullpath = "./index.html"
85 else:
86 fullpath = str(request.path)[1:]
87
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +000088 try:
89 open(fullpath)
90 except:
91 response = make_response("Cannot find a file: %s" % (fullpath), 500)
92 response.headers["Content-type"] = "text/html"
93 return response
94
Ubuntu82b8a832013-02-06 22:00:11 +000095 response = make_response(open(fullpath).read())
96 suffix = fullpath.split(".")[-1]
97
98 if suffix == "html" or suffix == "htm":
99 response.headers["Content-type"] = "text/html"
100 elif suffix == "js":
101 response.headers["Content-type"] = "application/javascript"
102 elif suffix == "css":
103 response.headers["Content-type"] = "text/css"
104 elif suffix == "png":
105 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -0700106 elif suffix == "svg":
107 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +0000108
109 return response
110
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000111## Proxy ##
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700112@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
113def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
114 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -0700115 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 -0700116 print command
117 result = os.popen(command).read()
118 except:
119 print "REST IF has issue"
120 exit
121
122 resp = Response(result, status=200, mimetype='application/json')
123 return resp
124
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000125@app.route("/proxy/gui/switchctrl/<cmd>")
126def proxy_switch_controller_setting(cmd):
127 try:
128 command = "curl -s %s/gui/switchctrl/%s" % (ONOS_GUI3_CONTROL_HOST, cmd)
129 print command
130 result = os.popen(command).read()
131 except:
132 print "REST IF has issue"
133 exit
134
135 resp = Response(result, status=200, mimetype='application/json')
136 return resp
137
Paul Greyson8d1c6362013-03-27 13:05:24 -0700138@app.route("/proxy/gui/switch/<cmd>/<dpid>")
139def proxy_switch_status_change(cmd, dpid):
140 try:
141 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
142 print command
143 result = os.popen(command).read()
144 except:
145 print "REST IF has issue"
146 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700147
Paul Greyson8d1c6362013-03-27 13:05:24 -0700148 resp = Response(result, status=200, mimetype='application/json')
149 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700150
Paul Greyson2913af82013-03-27 14:53:17 -0700151@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
152def proxy_controller_status_change(cmd, controller_name):
153 try:
154 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
155 print command
156 result = os.popen(command).read()
157 except:
158 print "REST IF has issue"
159 exit
160
161 resp = Response(result, status=200, mimetype='application/json')
162 return resp
163
Paul Greyson472da4c2013-03-28 11:43:17 -0700164@app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
165def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
166 try:
167 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)
168 print command
169 result = os.popen(command).read()
170 except:
171 print "REST IF has issue"
172 exit
173
174 resp = Response(result, status=200, mimetype='application/json')
175 return resp
176
Paul Greyson6f918402013-03-28 12:18:30 -0700177@app.route("/proxy/gui/delflow/<flow_id>")
178def proxy_del_flow(flow_id):
179 try:
180 command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
181 print command
182 result = os.popen(command).read()
183 except:
184 print "REST IF has issue"
185 exit
186
187 resp = Response(result, status=200, mimetype='application/json')
188 return resp
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000189
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700190@app.route("/proxy/gui/iperf/start/<flow_id>/<duration>/<samples>")
191def proxy_iperf_start(flow_id,duration,samples):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000192 try:
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700193 command = "curl -s %s/gui/iperf/start/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id, duration, samples)
Paul Greyson8d1c6362013-03-27 13:05:24 -0700194 print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000195 result = os.popen(command).read()
196 except:
197 print "REST IF has issue"
198 exit
199
200 resp = Response(result, status=200, mimetype='application/json')
201 return resp
202
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700203@app.route("/proxy/gui/iperf/rate/<flow_id>")
204def proxy_iperf_rate(flow_id):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000205 try:
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700206 command = "curl -s %s/gui/iperf/rate/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
207 print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000208 result = os.popen(command).read()
209 except:
210 print "REST IF has issue"
211 exit
212
213 resp = Response(result, status=200, mimetype='application/json')
214 return resp
215
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700216
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000217###### ONOS RESET API ##############################
218## Worker Func ###
219def get_json(url):
220 code = 200
221 try:
222 command = "curl -s %s" % (url)
223 result = os.popen(command).read()
224 parsedResult = json.loads(result)
Masayoshi Kobayashi8ac33362013-04-05 03:17:26 +0000225 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
226 print "REST %s returned code %s" % (command, parsedResult['code'])
227 code=500
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000228 except:
229 print "REST IF %s has issue" % command
230 result = ""
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000231 code = 500
232
233 return (code, result)
234
235def pick_host():
236 if LB == True:
237 nr_host=len(controllers)
238 r=random.randint(0, nr_host - 1)
239 host=controllers[r]
240 else:
241 host=ONOS_DEFAULT_HOST
242
243 return "http://" + host + ":8080"
244
245## Switch ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000246@app.route("/wm/core/topology/switches/all/json")
247def switches():
248 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000249 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000250 else:
251 host = ONOS_GUI3_HOST
252
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000253 url ="%s/wm/core/topology/switches/all/json" % (host)
254 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000255
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000256 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000257 return resp
258
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000259## Link ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000260@app.route("/wm/core/topology/links/json")
261def links():
262 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000263 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000264 else:
265 host = ONOS_GUI3_HOST
266
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000267 url ="%s/wm/core/topology/links/json" % (host)
268 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000269
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000270 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000271 return resp
272
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000273## FlowSummary ##
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000274@app.route("/wm/flow/getsummary/<start>/<range>/json")
275def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000276 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000277 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000278 else:
279 host = ONOS_GUI3_HOST
280
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000281 url ="%s/wm/flow/getsummary/%s/%s/json" % (host, start, range)
282 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000283
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000284 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000285 return resp
286
287@app.route("/wm/registry/controllers/json")
288def registry_controllers():
289 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000290 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000291 else:
292 host = ONOS_GUI3_HOST
293
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000294 url= "%s/wm/registry/controllers/json" % (host)
295 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000296
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000297 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000298 return resp
299
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000300
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000301@app.route("/wm/registry/switches/json")
302def registry_switches():
303 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000304 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000305 else:
306 host = ONOS_GUI3_HOST
307
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000308 url="%s/wm/registry/switches/json" % (host)
309 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000310
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000311 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000312 return resp
313
Ubuntu82b8a832013-02-06 22:00:11 +0000314def node_id(switch_array, dpid):
315 id = -1
316 for i, val in enumerate(switch_array):
317 if val['name'] == dpid:
318 id = i
319 break
320
321 return id
322
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000323## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000324@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000325def topology_for_gui():
326 try:
327 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
328 result = os.popen(command).read()
329 parsedResult = json.loads(result)
330 except:
331 log_error("REST IF has issue: %s" % command)
332 log_error("%s" % result)
333 sys.exit(0)
334
335 topo = {}
336 switches = []
337 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000338 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000339
340 for v in parsedResult:
341 if v.has_key('dpid'):
342# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
343 dpid = str(v['dpid'])
344 state = str(v['state'])
345 sw = {}
346 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000347 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000348
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000349 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000350 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000351 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000352
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000353 try:
354 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
355 result = os.popen(command).read()
356 parsedResult = json.loads(result)
357 except:
358 log_error("REST IF has issue: %s" % command)
359 log_error("%s" % result)
360
361 for key in parsedResult:
362 dpid = key
363 ctrl = parsedResult[dpid][0]['controllerId']
364 sw_id = node_id(switches, dpid)
365 if sw_id != -1:
366 if switches[sw_id]['group'] != 0:
367 switches[sw_id]['group'] = controllers.index(ctrl) + 1
368
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000369 try:
370 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000371# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000372 p1=1
373 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000374# v2 = "00:00:00:00:00:0d:00:d3"
375 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000376 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
377 result = os.popen(command).read()
378 parsedResult = json.loads(result)
379 except:
380 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000381 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000382
Ubuntu765deff2013-02-28 18:39:13 +0000383 path = []
384 if parsedResult.has_key('flowEntries'):
385 flowEntries= parsedResult['flowEntries']
386 for i, v in enumerate(flowEntries):
387 if i < len(flowEntries) - 1:
388 sdpid= flowEntries[i]['dpid']['value']
389 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700390 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000391
Ubuntu82b8a832013-02-06 22:00:11 +0000392 try:
393 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
394 result = os.popen(command).read()
395 parsedResult = json.loads(result)
396 except:
397 log_error("REST IF has issue: %s" % command)
398 log_error("%s" % result)
399 sys.exit(0)
400
401 for v in parsedResult:
402 link = {}
403 if v.has_key('dst-switch'):
404 dst_dpid = str(v['dst-switch'])
405 dst_id = node_id(switches, dst_dpid)
406 if v.has_key('src-switch'):
407 src_dpid = str(v['src-switch'])
408 src_id = node_id(switches, src_dpid)
409 link['source'] = src_id
410 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000411
412 onpath = 0
413 for (s,d) in path:
414 if s == v['src-switch'] and d == v['dst-switch']:
415 onpath = 1
416 break
417 link['type'] = onpath
418
Ubuntu82b8a832013-02-06 22:00:11 +0000419 links.append(link)
420
421 topo['nodes'] = switches
422 topo['links'] = links
423
Ubuntu82b8a832013-02-06 22:00:11 +0000424 js = json.dumps(topo)
425 resp = Response(js, status=200, mimetype='application/json')
426 return resp
427
Ubuntuaea2a682013-02-08 08:30:10 +0000428#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
429#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
430@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
431def shortest_path(v1, p1, v2, p2):
432 try:
433 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
434 result = os.popen(command).read()
435 parsedResult = json.loads(result)
436 except:
437 log_error("REST IF has issue: %s" % command)
438 log_error("%s" % result)
439 sys.exit(0)
440
441 topo = {}
442 switches = []
443 links = []
444
445 for v in parsedResult:
446 if v.has_key('dpid'):
447 dpid = str(v['dpid'])
448 state = str(v['state'])
449 sw = {}
450 sw['name']=dpid
451 if str(v['state']) == "ACTIVE":
452 if dpid[-2:-1] == "a":
453 sw['group']=1
454 if dpid[-2:-1] == "b":
455 sw['group']=2
456 if dpid[-2:-1] == "c":
457 sw['group']=3
458 if str(v['state']) == "INACTIVE":
459 sw['group']=0
460
461 switches.append(sw)
462
463 try:
464 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
465 result = os.popen(command).read()
466 parsedResult = json.loads(result)
467 except:
468 log_error("No route")
469 parsedResult = []
470# exit(1)
471
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700472 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000473 for i, v in enumerate(parsedResult):
474 if i < len(parsedResult) - 1:
475 sdpid= parsedResult[i]['switch']
476 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700477 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000478
479 try:
480 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
481 result = os.popen(command).read()
482 parsedResult = json.loads(result)
483 except:
484 log_error("REST IF has issue: %s" % command)
485 log_error("%s" % result)
486 sys.exit(0)
487
488 for v in parsedResult:
489 link = {}
490 if v.has_key('dst-switch'):
491 dst_dpid = str(v['dst-switch'])
492 dst_id = node_id(switches, dst_dpid)
493 if v.has_key('src-switch'):
494 src_dpid = str(v['src-switch'])
495 src_id = node_id(switches, src_dpid)
496 link['source'] = src_id
497 link['target'] = dst_id
498 onpath = 0
499 for (s,d) in path:
500 if s == v['src-switch'] and d == v['dst-switch']:
501 onpath = 1
502 break
503
504 link['type'] = onpath
505 links.append(link)
506
507 topo['nodes'] = switches
508 topo['links'] = links
509
Ubuntuaea2a682013-02-08 08:30:10 +0000510 js = json.dumps(topo)
511 resp = Response(js, status=200, mimetype='application/json')
512 return resp
513
Ubuntu82b8a832013-02-06 22:00:11 +0000514@app.route("/wm/core/controller/switches/json")
515def query_switch():
516 try:
517 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
518# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000519 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000520 result = os.popen(command).read()
521 parsedResult = json.loads(result)
522 except:
523 log_error("REST IF has issue: %s" % command)
524 log_error("%s" % result)
525 sys.exit(0)
526
527# print command
528# print result
529 switches_ = []
530 for v in parsedResult:
531 if v.has_key('dpid'):
532 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
533 dpid = str(v['dpid'])
534 state = str(v['state'])
535 sw = {}
536 sw['dpid']=dpid
537 sw['active']=state
538 switches_.append(sw)
539
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000540# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000541 js = json.dumps(switches_)
542 resp = Response(js, status=200, mimetype='application/json')
543 return resp
544
545@app.route("/wm/device/")
546def devices():
547 try:
548 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
549 result = os.popen(command).read()
550 parsedResult = json.loads(result)['results']
551 except:
552 log_error("REST IF has issue: %s" % command)
553 log_error("%s" % result)
554 sys.exit(0)
555
556 devices = []
557 for v in parsedResult:
558 dl_addr = v['dl_addr']
559 nw_addr = v['nw_addr']
560 vertex = v['_id']
561 mac = []
562 mac.append(dl_addr)
563 ip = []
564 ip.append(nw_addr)
565 device = {}
566 device['entryClass']="DefaultEntryClass"
567 device['mac']=mac
568 device['ipv4']=ip
569 device['vlan']=[]
570 device['lastSeen']=0
571 attachpoints =[]
572
573 port, dpid = deviceV_to_attachpoint(vertex)
574 attachpoint = {}
575 attachpoint['port']=port
576 attachpoint['switchDPID']=dpid
577 attachpoints.append(attachpoint)
578 device['attachmentPoint']=attachpoints
579 devices.append(device)
580
Ubuntu82b8a832013-02-06 22:00:11 +0000581 js = json.dumps(devices)
582 resp = Response(js, status=200, mimetype='application/json')
583 return resp
584
585#{"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}
586
Ubuntu82b8a832013-02-06 22:00:11 +0000587## return fake stat for now
588@app.route("/wm/core/switch/<switchId>/<statType>/json")
589def switch_stat(switchId, statType):
590 if statType == "desc":
591 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
592 ret = {}
593 ret[switchId]=desc
594 elif statType == "aggregate":
595 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
596 ret = {}
597 ret[switchId]=aggr
598 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700599 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000600
601 js = json.dumps(ret)
602 resp = Response(js, status=200, mimetype='application/json')
603 return resp
604
605
606@app.route("/wm/topology/links/json")
607def query_links():
608 try:
609 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000610 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000611 result = os.popen(command).read()
612 parsedResult = json.loads(result)['results']
613 except:
614 log_error("REST IF has issue: %s" % command)
615 log_error("%s" % result)
616 sys.exit(0)
617
618 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000619# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000620 sport = []
621 links = []
622 for v in parsedResult:
623 srcport = v['_id']
624 try:
625 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
626 print command
627 result = os.popen(command).read()
628 linkResults = json.loads(result)['results']
629 except:
630 log_error("REST IF has issue: %s" % command)
631 log_error("%s" % result)
632 sys.exit(0)
633
634 for p in linkResults:
635 if p.has_key('type') and p['type'] == "port":
636 dstport = p['_id']
637 (sport, sdpid) = portV_to_port_dpid(srcport)
638 (dport, ddpid) = portV_to_port_dpid(dstport)
639 link = {}
640 link["src-switch"]=sdpid
641 link["src-port"]=sport
642 link["src-port-state"]=0
643 link["dst-switch"]=ddpid
644 link["dst-port"]=dport
645 link["dst-port-state"]=0
646 link["type"]="internal"
647 links.append(link)
648
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000649# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000650 js = json.dumps(links)
651 resp = Response(js, status=200, mimetype='application/json')
652 return resp
653
Ubuntuc016ba12013-02-27 21:53:41 +0000654@app.route("/controller_status")
655def controller_status():
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700656 onos_check="sh ~/src/ONOS/start-onos.sh status | awk '{print $1}'"
Ubuntuc016ba12013-02-27 21:53:41 +0000657 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
658
659 cont_status=[]
660 for i in controllers:
661 status={}
662 onos=os.popen(onos_check % i).read()[:-1]
663 status["name"]=i
664 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000665 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000666 cont_status.append(status)
667
668 js = json.dumps(cont_status)
669 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000670 return resp
671
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000672### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000673@app.route("/gui/controller/<cmd>/<controller_name>")
674def controller_status_change(cmd, controller_name):
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700675 start_onos="~/src/ONOS/start-onos.sh start" % (controller_name)
676 stop_onos="~/src/ONOS/start-onos.sh stop" % (controller_name)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000677
678 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000679 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000680 ret = "controller %s is up" % (controller_name)
681 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000682 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000683 ret = "controller %s is down" % (controller_name)
684
685 return ret
686
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000687@app.route("/gui/switchctrl/<cmd>")
688def switch_controller_setting(cmd):
689 if cmd =="local":
690 print "All aggr switches connects to local controller only"
691 result=""
692 for i in range(0, len(controllers)):
693 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-local.sh'" % (controllers[i])
694 result += os.popen(cmd_string).read()
695 elif cmd =="all":
696 print "All aggr switches connects to all controllers except for core controller"
697 result=""
698 for i in range(0, len(controllers)):
699 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i])
700 result += os.popen(cmd_string).read()
701
702 return result
703
704
705
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000706@app.route("/gui/switch/<cmd>/<dpid>")
707def switch_status_change(cmd, dpid):
708 r = re.compile(':')
709 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000710 host=controllers[0]
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700711 cmd_string="'cd ~/src/ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
712 get_status="'cd ~/src/ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000713 print "cmd_string"
714
715 if cmd =="up" or cmd=="down":
716 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000717 os.popen(cmd_string)
718 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000719
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000720 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000721
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000722#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000723#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000724@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
725def link_up(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000726
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000727 cmd = 'up'
728 result=""
729
Paul Greyson472da4c2013-03-28 11:43:17 -0700730 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000731 if dpid in core_switches:
732 host = controllers[0]
733 src_ports = [1, 2, 3, 4, 5]
734 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000735 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000736 host = controllers[hostid-1]
737 if hostid == 2 :
738 src_ports = [51]
739 else :
740 src_ports = [26]
741
742 for port in src_ports :
743 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
744 print cmd_string
745 res=os.popen(cmd_string).read()
746 result = result + ' ' + res
747
748 return result
749
750
751#* Link Down
752#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000753@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000754def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
755
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000756 if src_dpid in core_switches:
757 host = controllers[0]
758 else:
759 hostid=int(src_dpid.split(':')[-2])
760 host = controllers[hostid-1]
761
762 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
763 print cmd_string
764
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000765 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000766
767 return result
768
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000769#* Create Flow
770#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000771#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 +0000772@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000773def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
774 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
775 print command
776 ret = os.popen(command).read()
777 if ret == "":
778 flow_nr=0
779 else:
780 flow_nr=int(ret)
781
782 flow_nr += 1
Umesh Krishnaswamyf22d3ed2013-04-03 11:57:54 -0700783 command = "/home/ubuntu/ONOS/web/add_flow.py -m onos %d %s %s %s %s %s matchSrcMac %s matchDstMac %s" % (flow_nr, "dummy", src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC)
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000784 command1 = "/home/ubuntu/ONOS/web/add_flow.py -m onos %d %s %s %s %s %s matchSrcMac %s matchDstMac %s" % (flow_nr, "dummy", dst_dpid, dst_port, src_dpid, src_port, dstMAC, srcMAC)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000785 print command
786 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000787 errcode1 = os.popen(command1).read()
788 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000789
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000790#* Delete Flow
791#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000792@app.route("/gui/delflow/<flow_id>")
793def del_flow(flow_id):
794 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
795 print command
796 errcode = os.popen(command).read()
797 return errcode
798
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000799#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700800#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000801@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
802def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000803 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000804 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000805 print command
806 result = os.popen(command).read()
807 if len(result) == 0:
808 print "No Flow found"
809 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000810 except:
811 print "REST IF has issue"
812 exit
813
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000814 parsedResult = json.loads(result)
815
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +0000816# flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000817 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000818 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
819 src_port = parsedResult['dataPath']['srcPort']['port']['value']
820 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
821 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000822# 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 +0000823
824 if src_dpid in core_switches:
825 host = controllers[0]
826 else:
827 hostid=int(src_dpid.split(':')[-2])
828 host = controllers[hostid-1]
829
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000830# ./runiperf.sh 2 00:00:00:00:00:00:02:02 1 00:00:00:00:00:00:03:02 1 100 15
831 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./runiperf.sh %d %s %s %s %s %s %s'" % (host, flowId, src_dpid, src_port, dst_dpid, dst_port, duration, samples)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000832 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000833 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000834
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000835 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000836
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000837#* Get Iperf Throughput
838#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000839@app.route("/gui/iperf/rate/<flow_id>")
840def iperf_rate(flow_id):
841 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000842 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
843 print command
844 result = os.popen(command).read()
845 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000846 resp = Response(result, status=400, mimetype='text/html')
847 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000848 except:
849 print "REST IF has issue"
850 exit
851
852 parsedResult = json.loads(result)
853
854 flowId = int(parsedResult['flowId']['value'], 16)
855 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
856 src_port = parsedResult['dataPath']['srcPort']['port']['value']
857 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
858 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
859
860 if src_dpid in core_switches:
861 host = controllers[0]
862 else:
863 hostid=int(src_dpid.split(':')[-2])
864 host = controllers[hostid-1]
865
866 try:
867 command = "curl -s http://%s:%s/log/iperf_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000868 print command
869 result = os.popen(command).read()
870 except:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000871 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000872
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +0000873 if len(result) == 0:
874 resp = Response(result, status=400, mimetype='text/html')
875 return "no iperf file found (flowid %s)" % flow_id;
876 else:
877 resp = Response(result, status=200, mimetype='application/json')
878 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000879
880
Ubuntu82b8a832013-02-06 22:00:11 +0000881if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000882 random.seed()
Ubuntu82b8a832013-02-06 22:00:11 +0000883 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000884# 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")
885# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
886# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
887# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
888# 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 +0000889# print "-- query all switches --"
890# query_switch()
891# print "-- query topo --"
892# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000893# link_change(1,2,3,4)
894 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000895# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000896# print "-- query all devices --"
897# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000898# iperf_start(1,10,15)
899# iperf_rate(1)
900 switches()
Ubuntu82b8a832013-02-06 22:00:11 +0000901 else:
902 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000903 app.run(threaded=True, host="0.0.0.0", port=9000)