blob: efc395f2ed337da608ead6107534d775781d22bb [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
22controllers=["onosgui1", "onosgui2", "onosgui3", "onosgui4", "onosgui5", "onosgui6", "onosgui7", "onosgui8"]
23core_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"]
24ONOS_GUI3_HOST="http://gui3.onlab.us:8080"
25ONOS_GUI3_CONTROL_HOST="http://gui3.onlab.us:8081"
26
27# Settings for running on dev testbed. Replace dev
28#controllers=["onosdevb1", "onosdevb2", "onosdevb3", "onosdevb4"]
Masayoshi Kobayashif205dcf2013-04-05 02:21:48 +000029#controllers=["onosdevt1", "onosdevt2", "onosdevt3", "onosdevt4", "onosdevt5", "onosdevt6", "onosdevt7", "onosdevt8"]
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000030#core_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 +000031
Masayoshi Kobayashif205dcf2013-04-05 02:21:48 +000032#ONOS_GUI3_HOST="http://devt-gui.onlab.us:8080"
33#ONOS_GUI3_CONTROL_HOST="http://devt-gui.onlab.us:8080"
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +000034
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000035LB=True #; True or False
36ONOS_DEFAULT_HOST="localhost" ;# Has to set if LB=False
Ubuntu82b8a832013-02-06 22:00:11 +000037
38DEBUG=1
Ubuntu82b8a832013-02-06 22:00:11 +000039
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000040pp = pprint.PrettyPrinter(indent=4)
Ubuntu82b8a832013-02-06 22:00:11 +000041app = Flask(__name__)
42
43## Worker Functions ##
44def log_error(txt):
45 print '%s' % (txt)
46
47def debug(txt):
48 if DEBUG:
49 print '%s' % (txt)
50
Ubuntu82b8a832013-02-06 22:00:11 +000051### File Fetch ###
52@app.route('/ui/img/<filename>', methods=['GET'])
53@app.route('/img/<filename>', methods=['GET'])
54@app.route('/css/<filename>', methods=['GET'])
55@app.route('/js/models/<filename>', methods=['GET'])
56@app.route('/js/views/<filename>', methods=['GET'])
57@app.route('/js/<filename>', methods=['GET'])
58@app.route('/lib/<filename>', methods=['GET'])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +000059@app.route('/log/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000060@app.route('/', methods=['GET'])
61@app.route('/<filename>', methods=['GET'])
62@app.route('/tpl/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000063@app.route('/ons-demo/<filename>', methods=['GET'])
64@app.route('/ons-demo/js/<filename>', methods=['GET'])
65@app.route('/ons-demo/css/<filename>', methods=['GET'])
66@app.route('/ons-demo/assets/<filename>', methods=['GET'])
67@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000068def return_file(filename="index.html"):
69 if request.path == "/":
70 fullpath = "./index.html"
71 else:
72 fullpath = str(request.path)[1:]
73
74 response = make_response(open(fullpath).read())
75 suffix = fullpath.split(".")[-1]
76
77 if suffix == "html" or suffix == "htm":
78 response.headers["Content-type"] = "text/html"
79 elif suffix == "js":
80 response.headers["Content-type"] = "application/javascript"
81 elif suffix == "css":
82 response.headers["Content-type"] = "text/css"
83 elif suffix == "png":
84 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -070085 elif suffix == "svg":
86 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000087
88 return response
89
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000090## Proxy ##
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070091@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
92def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
93 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -070094 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 -070095 print command
96 result = os.popen(command).read()
97 except:
98 print "REST IF has issue"
99 exit
100
101 resp = Response(result, status=200, mimetype='application/json')
102 return resp
103
Paul Greyson8d1c6362013-03-27 13:05:24 -0700104@app.route("/proxy/gui/switch/<cmd>/<dpid>")
105def proxy_switch_status_change(cmd, dpid):
106 try:
107 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
108 print command
109 result = os.popen(command).read()
110 except:
111 print "REST IF has issue"
112 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700113
Paul Greyson8d1c6362013-03-27 13:05:24 -0700114 resp = Response(result, status=200, mimetype='application/json')
115 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700116
Paul Greyson2913af82013-03-27 14:53:17 -0700117@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
118def proxy_controller_status_change(cmd, controller_name):
119 try:
120 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
121 print command
122 result = os.popen(command).read()
123 except:
124 print "REST IF has issue"
125 exit
126
127 resp = Response(result, status=200, mimetype='application/json')
128 return resp
129
Paul Greyson472da4c2013-03-28 11:43:17 -0700130@app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
131def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
132 try:
133 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)
134 print command
135 result = os.popen(command).read()
136 except:
137 print "REST IF has issue"
138 exit
139
140 resp = Response(result, status=200, mimetype='application/json')
141 return resp
142
Paul Greyson6f918402013-03-28 12:18:30 -0700143@app.route("/proxy/gui/delflow/<flow_id>")
144def proxy_del_flow(flow_id):
145 try:
146 command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
147 print command
148 result = os.popen(command).read()
149 except:
150 print "REST IF has issue"
151 exit
152
153 resp = Response(result, status=200, mimetype='application/json')
154 return resp
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000155
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700156@app.route("/proxy/gui/iperf/start/<flow_id>/<duration>/<samples>")
157def proxy_iperf_start(flow_id,duration,samples):
158 try:
159 command = "curl -s %s/gui/iperf/start/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id, duration, samples)
160 print command
161 result = os.popen(command).read()
162 except:
163 print "REST IF has issue"
164 exit
165
166 resp = Response(result, status=200, mimetype='application/json')
167 return resp
168
169@app.route("/proxy/gui/iperf/rate/<flow_id>")
170def proxy_iperf_rate(flow_id):
171 try:
172 command = "curl -s %s/gui/iperf/rate/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
173 print command
174 result = os.popen(command).read()
175 except:
176 print "REST IF has issue"
177 exit
178
179 resp = Response(result, status=200, mimetype='application/json')
180 return resp
181
182
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000183###### ONOS RESET API ##############################
184## Worker Func ###
185def get_json(url):
186 code = 200
187 try:
188 command = "curl -s %s" % (url)
189 result = os.popen(command).read()
190 parsedResult = json.loads(result)
191 except:
192 print "REST IF %s has issue" % command
193 result = ""
194
195 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
196 print "REST %s returned code %s" % (command, parsedResult['code'])
197 code=500
198
199 if result == "":
200 code = 500
201
202 return (code, result)
203
204def pick_host():
205 if LB == True:
206 nr_host=len(controllers)
207 r=random.randint(0, nr_host - 1)
208 host=controllers[r]
209 else:
210 host=ONOS_DEFAULT_HOST
211
212 return "http://" + host + ":8080"
213
214## Switch ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000215@app.route("/wm/core/topology/switches/all/json")
216def switches():
217 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000218 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000219 else:
220 host = ONOS_GUI3_HOST
221
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000222 url ="%s/wm/core/topology/switches/all/json" % (host)
223 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000224
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000225 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000226 return resp
227
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000228## Link ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000229@app.route("/wm/core/topology/links/json")
230def links():
231 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000232 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000233 else:
234 host = ONOS_GUI3_HOST
235
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000236 url ="%s/wm/core/topology/links/json" % (host)
237 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000238
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000239 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000240 return resp
241
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000242## FlowSummary ##
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000243@app.route("/wm/flow/getsummary/<start>/<range>/json")
244def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000245 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000246 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000247 else:
248 host = ONOS_GUI3_HOST
249
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000250 url ="%s/wm/flow/getsummary/%s/%s/json" % (host, start, range)
251 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000252
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000253 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000254 return resp
255
256@app.route("/wm/registry/controllers/json")
257def registry_controllers():
258 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000259 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000260 else:
261 host = ONOS_GUI3_HOST
262
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000263 url= "%s/wm/registry/controllers/json" % (host)
264 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000265
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000266 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000267 return resp
268
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000269
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000270@app.route("/wm/registry/switches/json")
271def registry_switches():
272 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000273 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000274 else:
275 host = ONOS_GUI3_HOST
276
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000277 url="%s/wm/registry/switches/json" % (host)
278 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000279
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000280 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000281 return resp
282
Ubuntu82b8a832013-02-06 22:00:11 +0000283def node_id(switch_array, dpid):
284 id = -1
285 for i, val in enumerate(switch_array):
286 if val['name'] == dpid:
287 id = i
288 break
289
290 return id
291
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000292## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000293@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000294def topology_for_gui():
295 try:
296 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
297 result = os.popen(command).read()
298 parsedResult = json.loads(result)
299 except:
300 log_error("REST IF has issue: %s" % command)
301 log_error("%s" % result)
302 sys.exit(0)
303
304 topo = {}
305 switches = []
306 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000307 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000308
309 for v in parsedResult:
310 if v.has_key('dpid'):
311# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
312 dpid = str(v['dpid'])
313 state = str(v['state'])
314 sw = {}
315 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000316 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000317
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000318 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000319 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000320 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000321
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000322 try:
323 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
324 result = os.popen(command).read()
325 parsedResult = json.loads(result)
326 except:
327 log_error("REST IF has issue: %s" % command)
328 log_error("%s" % result)
329
330 for key in parsedResult:
331 dpid = key
332 ctrl = parsedResult[dpid][0]['controllerId']
333 sw_id = node_id(switches, dpid)
334 if sw_id != -1:
335 if switches[sw_id]['group'] != 0:
336 switches[sw_id]['group'] = controllers.index(ctrl) + 1
337
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000338 try:
339 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000340# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000341 p1=1
342 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000343# v2 = "00:00:00:00:00:0d:00:d3"
344 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000345 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
346 result = os.popen(command).read()
347 parsedResult = json.loads(result)
348 except:
349 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000350 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000351
Ubuntu765deff2013-02-28 18:39:13 +0000352 path = []
353 if parsedResult.has_key('flowEntries'):
354 flowEntries= parsedResult['flowEntries']
355 for i, v in enumerate(flowEntries):
356 if i < len(flowEntries) - 1:
357 sdpid= flowEntries[i]['dpid']['value']
358 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700359 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000360
Ubuntu82b8a832013-02-06 22:00:11 +0000361 try:
362 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
363 result = os.popen(command).read()
364 parsedResult = json.loads(result)
365 except:
366 log_error("REST IF has issue: %s" % command)
367 log_error("%s" % result)
368 sys.exit(0)
369
370 for v in parsedResult:
371 link = {}
372 if v.has_key('dst-switch'):
373 dst_dpid = str(v['dst-switch'])
374 dst_id = node_id(switches, dst_dpid)
375 if v.has_key('src-switch'):
376 src_dpid = str(v['src-switch'])
377 src_id = node_id(switches, src_dpid)
378 link['source'] = src_id
379 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000380
381 onpath = 0
382 for (s,d) in path:
383 if s == v['src-switch'] and d == v['dst-switch']:
384 onpath = 1
385 break
386 link['type'] = onpath
387
Ubuntu82b8a832013-02-06 22:00:11 +0000388 links.append(link)
389
390 topo['nodes'] = switches
391 topo['links'] = links
392
Ubuntu82b8a832013-02-06 22:00:11 +0000393 js = json.dumps(topo)
394 resp = Response(js, status=200, mimetype='application/json')
395 return resp
396
Ubuntuaea2a682013-02-08 08:30:10 +0000397#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
398#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
399@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
400def shortest_path(v1, p1, v2, p2):
401 try:
402 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
403 result = os.popen(command).read()
404 parsedResult = json.loads(result)
405 except:
406 log_error("REST IF has issue: %s" % command)
407 log_error("%s" % result)
408 sys.exit(0)
409
410 topo = {}
411 switches = []
412 links = []
413
414 for v in parsedResult:
415 if v.has_key('dpid'):
416 dpid = str(v['dpid'])
417 state = str(v['state'])
418 sw = {}
419 sw['name']=dpid
420 if str(v['state']) == "ACTIVE":
421 if dpid[-2:-1] == "a":
422 sw['group']=1
423 if dpid[-2:-1] == "b":
424 sw['group']=2
425 if dpid[-2:-1] == "c":
426 sw['group']=3
427 if str(v['state']) == "INACTIVE":
428 sw['group']=0
429
430 switches.append(sw)
431
432 try:
433 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
434 result = os.popen(command).read()
435 parsedResult = json.loads(result)
436 except:
437 log_error("No route")
438 parsedResult = []
439# exit(1)
440
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700441 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000442 for i, v in enumerate(parsedResult):
443 if i < len(parsedResult) - 1:
444 sdpid= parsedResult[i]['switch']
445 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700446 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000447
448 try:
449 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
450 result = os.popen(command).read()
451 parsedResult = json.loads(result)
452 except:
453 log_error("REST IF has issue: %s" % command)
454 log_error("%s" % result)
455 sys.exit(0)
456
457 for v in parsedResult:
458 link = {}
459 if v.has_key('dst-switch'):
460 dst_dpid = str(v['dst-switch'])
461 dst_id = node_id(switches, dst_dpid)
462 if v.has_key('src-switch'):
463 src_dpid = str(v['src-switch'])
464 src_id = node_id(switches, src_dpid)
465 link['source'] = src_id
466 link['target'] = dst_id
467 onpath = 0
468 for (s,d) in path:
469 if s == v['src-switch'] and d == v['dst-switch']:
470 onpath = 1
471 break
472
473 link['type'] = onpath
474 links.append(link)
475
476 topo['nodes'] = switches
477 topo['links'] = links
478
Ubuntuaea2a682013-02-08 08:30:10 +0000479 js = json.dumps(topo)
480 resp = Response(js, status=200, mimetype='application/json')
481 return resp
482
Ubuntu82b8a832013-02-06 22:00:11 +0000483@app.route("/wm/core/controller/switches/json")
484def query_switch():
485 try:
486 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
487# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000488 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000489 result = os.popen(command).read()
490 parsedResult = json.loads(result)
491 except:
492 log_error("REST IF has issue: %s" % command)
493 log_error("%s" % result)
494 sys.exit(0)
495
496# print command
497# print result
498 switches_ = []
499 for v in parsedResult:
500 if v.has_key('dpid'):
501 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
502 dpid = str(v['dpid'])
503 state = str(v['state'])
504 sw = {}
505 sw['dpid']=dpid
506 sw['active']=state
507 switches_.append(sw)
508
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000509# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000510 js = json.dumps(switches_)
511 resp = Response(js, status=200, mimetype='application/json')
512 return resp
513
514@app.route("/wm/device/")
515def devices():
516 try:
517 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
518 result = os.popen(command).read()
519 parsedResult = json.loads(result)['results']
520 except:
521 log_error("REST IF has issue: %s" % command)
522 log_error("%s" % result)
523 sys.exit(0)
524
525 devices = []
526 for v in parsedResult:
527 dl_addr = v['dl_addr']
528 nw_addr = v['nw_addr']
529 vertex = v['_id']
530 mac = []
531 mac.append(dl_addr)
532 ip = []
533 ip.append(nw_addr)
534 device = {}
535 device['entryClass']="DefaultEntryClass"
536 device['mac']=mac
537 device['ipv4']=ip
538 device['vlan']=[]
539 device['lastSeen']=0
540 attachpoints =[]
541
542 port, dpid = deviceV_to_attachpoint(vertex)
543 attachpoint = {}
544 attachpoint['port']=port
545 attachpoint['switchDPID']=dpid
546 attachpoints.append(attachpoint)
547 device['attachmentPoint']=attachpoints
548 devices.append(device)
549
Ubuntu82b8a832013-02-06 22:00:11 +0000550 js = json.dumps(devices)
551 resp = Response(js, status=200, mimetype='application/json')
552 return resp
553
554#{"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}
555
Ubuntu82b8a832013-02-06 22:00:11 +0000556## return fake stat for now
557@app.route("/wm/core/switch/<switchId>/<statType>/json")
558def switch_stat(switchId, statType):
559 if statType == "desc":
560 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
561 ret = {}
562 ret[switchId]=desc
563 elif statType == "aggregate":
564 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
565 ret = {}
566 ret[switchId]=aggr
567 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700568 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000569
570 js = json.dumps(ret)
571 resp = Response(js, status=200, mimetype='application/json')
572 return resp
573
574
575@app.route("/wm/topology/links/json")
576def query_links():
577 try:
578 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000579 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000580 result = os.popen(command).read()
581 parsedResult = json.loads(result)['results']
582 except:
583 log_error("REST IF has issue: %s" % command)
584 log_error("%s" % result)
585 sys.exit(0)
586
587 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000588# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000589 sport = []
590 links = []
591 for v in parsedResult:
592 srcport = v['_id']
593 try:
594 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
595 print command
596 result = os.popen(command).read()
597 linkResults = json.loads(result)['results']
598 except:
599 log_error("REST IF has issue: %s" % command)
600 log_error("%s" % result)
601 sys.exit(0)
602
603 for p in linkResults:
604 if p.has_key('type') and p['type'] == "port":
605 dstport = p['_id']
606 (sport, sdpid) = portV_to_port_dpid(srcport)
607 (dport, ddpid) = portV_to_port_dpid(dstport)
608 link = {}
609 link["src-switch"]=sdpid
610 link["src-port"]=sport
611 link["src-port-state"]=0
612 link["dst-switch"]=ddpid
613 link["dst-port"]=dport
614 link["dst-port-state"]=0
615 link["type"]="internal"
616 links.append(link)
617
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000618# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000619 js = json.dumps(links)
620 resp = Response(js, status=200, mimetype='application/json')
621 return resp
622
Ubuntuc016ba12013-02-27 21:53:41 +0000623@app.route("/controller_status")
624def controller_status():
625 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
626 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
627
628 cont_status=[]
629 for i in controllers:
630 status={}
631 onos=os.popen(onos_check % i).read()[:-1]
632 status["name"]=i
633 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000634 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000635 cont_status.append(status)
636
637 js = json.dumps(cont_status)
638 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000639 return resp
640
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000641### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000642@app.route("/gui/controller/<cmd>/<controller_name>")
643def controller_status_change(cmd, controller_name):
644 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
645 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
646
647 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000648 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000649 ret = "controller %s is up" % (controller_name)
650 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000651 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000652 ret = "controller %s is down" % (controller_name)
653
654 return ret
655
656@app.route("/gui/switch/<cmd>/<dpid>")
657def switch_status_change(cmd, dpid):
658 r = re.compile(':')
659 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000660 host=controllers[0]
661 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
662 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000663 print "cmd_string"
664
665 if cmd =="up" or cmd=="down":
666 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000667 os.popen(cmd_string)
668 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000669
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000670 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000671
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000672#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000673#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000674@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
675def link_up(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000676
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000677 cmd = 'up'
678 result=""
679
Paul Greyson472da4c2013-03-28 11:43:17 -0700680 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000681 if dpid in core_switches:
682 host = controllers[0]
683 src_ports = [1, 2, 3, 4, 5]
684 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000685 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000686 host = controllers[hostid-1]
687 if hostid == 2 :
688 src_ports = [51]
689 else :
690 src_ports = [26]
691
692 for port in src_ports :
693 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
694 print cmd_string
695 res=os.popen(cmd_string).read()
696 result = result + ' ' + res
697
698 return result
699
700
701#* Link Down
702#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000703@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000704def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
705
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000706 if src_dpid in core_switches:
707 host = controllers[0]
708 else:
709 hostid=int(src_dpid.split(':')[-2])
710 host = controllers[hostid-1]
711
712 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
713 print cmd_string
714
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000715 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000716
717 return result
718
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000719#* Create Flow
720#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000721#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 +0000722@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000723def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
724 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
725 print command
726 ret = os.popen(command).read()
727 if ret == "":
728 flow_nr=0
729 else:
730 flow_nr=int(ret)
731
732 flow_nr += 1
Umesh Krishnaswamyf22d3ed2013-04-03 11:57:54 -0700733 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 +0000734 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 +0000735 print command
736 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000737 errcode1 = os.popen(command1).read()
738 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000739
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000740#* Delete Flow
741#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000742@app.route("/gui/delflow/<flow_id>")
743def del_flow(flow_id):
744 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
745 print command
746 errcode = os.popen(command).read()
747 return errcode
748
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000749#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700750#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000751@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
752def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000753 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000754 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000755 print command
756 result = os.popen(command).read()
757 if len(result) == 0:
758 print "No Flow found"
759 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000760 except:
761 print "REST IF has issue"
762 exit
763
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000764 parsedResult = json.loads(result)
765
766 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000767 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
768 src_port = parsedResult['dataPath']['srcPort']['port']['value']
769 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
770 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000771# 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 +0000772
773 if src_dpid in core_switches:
774 host = controllers[0]
775 else:
776 hostid=int(src_dpid.split(':')[-2])
777 host = controllers[hostid-1]
778
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000779# ./runiperf.sh 2 00:00:00:00:00:00:02:02 1 00:00:00:00:00:00:03:02 1 100 15
780 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 +0000781 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000782 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000783
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000784 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000785
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000786#* Get Iperf Throughput
787#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000788@app.route("/gui/iperf/rate/<flow_id>")
789def iperf_rate(flow_id):
790 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000791 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
792 print command
793 result = os.popen(command).read()
794 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000795 resp = Response(result, status=400, mimetype='text/html')
796 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000797 except:
798 print "REST IF has issue"
799 exit
800
801 parsedResult = json.loads(result)
802
803 flowId = int(parsedResult['flowId']['value'], 16)
804 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
805 src_port = parsedResult['dataPath']['srcPort']['port']['value']
806 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
807 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
808
809 if src_dpid in core_switches:
810 host = controllers[0]
811 else:
812 hostid=int(src_dpid.split(':')[-2])
813 host = controllers[hostid-1]
814
815 try:
816 command = "curl -s http://%s:%s/log/iperf_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000817 print command
818 result = os.popen(command).read()
819 except:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000820 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000821
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +0000822 if len(result) == 0:
823 resp = Response(result, status=400, mimetype='text/html')
824 return "no iperf file found (flowid %s)" % flow_id;
825 else:
826 resp = Response(result, status=200, mimetype='application/json')
827 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000828
829
Ubuntu82b8a832013-02-06 22:00:11 +0000830if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000831 random.seed()
Ubuntu82b8a832013-02-06 22:00:11 +0000832 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000833# 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")
834# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
835# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
836# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
837# 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 +0000838# print "-- query all switches --"
839# query_switch()
840# print "-- query topo --"
841# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000842# link_change(1,2,3,4)
843 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000844# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000845# print "-- query all devices --"
846# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000847# iperf_start(1,10,15)
848# iperf_rate(1)
849 switches()
Ubuntu82b8a832013-02-06 22:00:11 +0000850 else:
851 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000852 app.run(threaded=True, host="0.0.0.0", port=9000)