blob: bc52992f553a17bbe9d0719f431bb0b27bf3692a [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 Kobayashif205dcf2013-04-05 02:21:48 +0000215=======
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000216@app.route("/wm/core/topology/switches/all/json")
217def switches():
218 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000219 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000220 else:
221 host = ONOS_GUI3_HOST
222
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000223 url ="%s/wm/core/topology/switches/all/json" % (host)
224 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000225
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000226 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000227 return resp
228
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000229## Link ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000230@app.route("/wm/core/topology/links/json")
231def links():
232 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000233 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000234 else:
235 host = ONOS_GUI3_HOST
236
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000237 url ="%s/wm/core/topology/links/json" % (host)
238 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000239
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000240 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000241 return resp
242
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000243## FlowSummary ##
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000244@app.route("/wm/flow/getsummary/<start>/<range>/json")
245def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000246 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000247 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000248 else:
249 host = ONOS_GUI3_HOST
250
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000251 url ="%s/wm/flow/getsummary/%s/%s/json" % (host, start, range)
252 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000253
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000254 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000255 return resp
256
257@app.route("/wm/registry/controllers/json")
258def registry_controllers():
259 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000260 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000261 else:
262 host = ONOS_GUI3_HOST
263
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000264 url= "%s/wm/registry/controllers/json" % (host)
265 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000266
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000267 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000268 return resp
269
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000270
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000271@app.route("/wm/registry/switches/json")
272def registry_switches():
273 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000274 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000275 else:
276 host = ONOS_GUI3_HOST
277
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000278 url="%s/wm/registry/switches/json" % (host)
279 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000280
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000281 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000282 return resp
283
Ubuntu82b8a832013-02-06 22:00:11 +0000284def node_id(switch_array, dpid):
285 id = -1
286 for i, val in enumerate(switch_array):
287 if val['name'] == dpid:
288 id = i
289 break
290
291 return id
292
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000293## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000294@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000295def topology_for_gui():
296 try:
297 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
298 result = os.popen(command).read()
299 parsedResult = json.loads(result)
300 except:
301 log_error("REST IF has issue: %s" % command)
302 log_error("%s" % result)
303 sys.exit(0)
304
305 topo = {}
306 switches = []
307 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000308 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000309
310 for v in parsedResult:
311 if v.has_key('dpid'):
312# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
313 dpid = str(v['dpid'])
314 state = str(v['state'])
315 sw = {}
316 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000317 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000318
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000319 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000320 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000321 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000322
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000323 try:
324 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
325 result = os.popen(command).read()
326 parsedResult = json.loads(result)
327 except:
328 log_error("REST IF has issue: %s" % command)
329 log_error("%s" % result)
330
331 for key in parsedResult:
332 dpid = key
333 ctrl = parsedResult[dpid][0]['controllerId']
334 sw_id = node_id(switches, dpid)
335 if sw_id != -1:
336 if switches[sw_id]['group'] != 0:
337 switches[sw_id]['group'] = controllers.index(ctrl) + 1
338
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000339 try:
340 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000341# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000342 p1=1
343 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000344# v2 = "00:00:00:00:00:0d:00:d3"
345 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000346 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
347 result = os.popen(command).read()
348 parsedResult = json.loads(result)
349 except:
350 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000351 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000352
Ubuntu765deff2013-02-28 18:39:13 +0000353 path = []
354 if parsedResult.has_key('flowEntries'):
355 flowEntries= parsedResult['flowEntries']
356 for i, v in enumerate(flowEntries):
357 if i < len(flowEntries) - 1:
358 sdpid= flowEntries[i]['dpid']['value']
359 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700360 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000361
Ubuntu82b8a832013-02-06 22:00:11 +0000362 try:
363 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
364 result = os.popen(command).read()
365 parsedResult = json.loads(result)
366 except:
367 log_error("REST IF has issue: %s" % command)
368 log_error("%s" % result)
369 sys.exit(0)
370
371 for v in parsedResult:
372 link = {}
373 if v.has_key('dst-switch'):
374 dst_dpid = str(v['dst-switch'])
375 dst_id = node_id(switches, dst_dpid)
376 if v.has_key('src-switch'):
377 src_dpid = str(v['src-switch'])
378 src_id = node_id(switches, src_dpid)
379 link['source'] = src_id
380 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000381
382 onpath = 0
383 for (s,d) in path:
384 if s == v['src-switch'] and d == v['dst-switch']:
385 onpath = 1
386 break
387 link['type'] = onpath
388
Ubuntu82b8a832013-02-06 22:00:11 +0000389 links.append(link)
390
391 topo['nodes'] = switches
392 topo['links'] = links
393
Ubuntu82b8a832013-02-06 22:00:11 +0000394 js = json.dumps(topo)
395 resp = Response(js, status=200, mimetype='application/json')
396 return resp
397
Ubuntuaea2a682013-02-08 08:30:10 +0000398#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
399#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
400@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
401def shortest_path(v1, p1, v2, p2):
402 try:
403 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
404 result = os.popen(command).read()
405 parsedResult = json.loads(result)
406 except:
407 log_error("REST IF has issue: %s" % command)
408 log_error("%s" % result)
409 sys.exit(0)
410
411 topo = {}
412 switches = []
413 links = []
414
415 for v in parsedResult:
416 if v.has_key('dpid'):
417 dpid = str(v['dpid'])
418 state = str(v['state'])
419 sw = {}
420 sw['name']=dpid
421 if str(v['state']) == "ACTIVE":
422 if dpid[-2:-1] == "a":
423 sw['group']=1
424 if dpid[-2:-1] == "b":
425 sw['group']=2
426 if dpid[-2:-1] == "c":
427 sw['group']=3
428 if str(v['state']) == "INACTIVE":
429 sw['group']=0
430
431 switches.append(sw)
432
433 try:
434 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
435 result = os.popen(command).read()
436 parsedResult = json.loads(result)
437 except:
438 log_error("No route")
439 parsedResult = []
440# exit(1)
441
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700442 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000443 for i, v in enumerate(parsedResult):
444 if i < len(parsedResult) - 1:
445 sdpid= parsedResult[i]['switch']
446 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700447 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000448
449 try:
450 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
451 result = os.popen(command).read()
452 parsedResult = json.loads(result)
453 except:
454 log_error("REST IF has issue: %s" % command)
455 log_error("%s" % result)
456 sys.exit(0)
457
458 for v in parsedResult:
459 link = {}
460 if v.has_key('dst-switch'):
461 dst_dpid = str(v['dst-switch'])
462 dst_id = node_id(switches, dst_dpid)
463 if v.has_key('src-switch'):
464 src_dpid = str(v['src-switch'])
465 src_id = node_id(switches, src_dpid)
466 link['source'] = src_id
467 link['target'] = dst_id
468 onpath = 0
469 for (s,d) in path:
470 if s == v['src-switch'] and d == v['dst-switch']:
471 onpath = 1
472 break
473
474 link['type'] = onpath
475 links.append(link)
476
477 topo['nodes'] = switches
478 topo['links'] = links
479
Ubuntuaea2a682013-02-08 08:30:10 +0000480 js = json.dumps(topo)
481 resp = Response(js, status=200, mimetype='application/json')
482 return resp
483
Ubuntu82b8a832013-02-06 22:00:11 +0000484@app.route("/wm/core/controller/switches/json")
485def query_switch():
486 try:
487 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
488# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000489 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000490 result = os.popen(command).read()
491 parsedResult = json.loads(result)
492 except:
493 log_error("REST IF has issue: %s" % command)
494 log_error("%s" % result)
495 sys.exit(0)
496
497# print command
498# print result
499 switches_ = []
500 for v in parsedResult:
501 if v.has_key('dpid'):
502 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
503 dpid = str(v['dpid'])
504 state = str(v['state'])
505 sw = {}
506 sw['dpid']=dpid
507 sw['active']=state
508 switches_.append(sw)
509
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000510# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000511 js = json.dumps(switches_)
512 resp = Response(js, status=200, mimetype='application/json')
513 return resp
514
515@app.route("/wm/device/")
516def devices():
517 try:
518 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
519 result = os.popen(command).read()
520 parsedResult = json.loads(result)['results']
521 except:
522 log_error("REST IF has issue: %s" % command)
523 log_error("%s" % result)
524 sys.exit(0)
525
526 devices = []
527 for v in parsedResult:
528 dl_addr = v['dl_addr']
529 nw_addr = v['nw_addr']
530 vertex = v['_id']
531 mac = []
532 mac.append(dl_addr)
533 ip = []
534 ip.append(nw_addr)
535 device = {}
536 device['entryClass']="DefaultEntryClass"
537 device['mac']=mac
538 device['ipv4']=ip
539 device['vlan']=[]
540 device['lastSeen']=0
541 attachpoints =[]
542
543 port, dpid = deviceV_to_attachpoint(vertex)
544 attachpoint = {}
545 attachpoint['port']=port
546 attachpoint['switchDPID']=dpid
547 attachpoints.append(attachpoint)
548 device['attachmentPoint']=attachpoints
549 devices.append(device)
550
Ubuntu82b8a832013-02-06 22:00:11 +0000551 js = json.dumps(devices)
552 resp = Response(js, status=200, mimetype='application/json')
553 return resp
554
555#{"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}
556
Ubuntu82b8a832013-02-06 22:00:11 +0000557## return fake stat for now
558@app.route("/wm/core/switch/<switchId>/<statType>/json")
559def switch_stat(switchId, statType):
560 if statType == "desc":
561 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
562 ret = {}
563 ret[switchId]=desc
564 elif statType == "aggregate":
565 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
566 ret = {}
567 ret[switchId]=aggr
568 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700569 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000570
571 js = json.dumps(ret)
572 resp = Response(js, status=200, mimetype='application/json')
573 return resp
574
575
576@app.route("/wm/topology/links/json")
577def query_links():
578 try:
579 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000580 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000581 result = os.popen(command).read()
582 parsedResult = json.loads(result)['results']
583 except:
584 log_error("REST IF has issue: %s" % command)
585 log_error("%s" % result)
586 sys.exit(0)
587
588 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000589# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000590 sport = []
591 links = []
592 for v in parsedResult:
593 srcport = v['_id']
594 try:
595 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
596 print command
597 result = os.popen(command).read()
598 linkResults = json.loads(result)['results']
599 except:
600 log_error("REST IF has issue: %s" % command)
601 log_error("%s" % result)
602 sys.exit(0)
603
604 for p in linkResults:
605 if p.has_key('type') and p['type'] == "port":
606 dstport = p['_id']
607 (sport, sdpid) = portV_to_port_dpid(srcport)
608 (dport, ddpid) = portV_to_port_dpid(dstport)
609 link = {}
610 link["src-switch"]=sdpid
611 link["src-port"]=sport
612 link["src-port-state"]=0
613 link["dst-switch"]=ddpid
614 link["dst-port"]=dport
615 link["dst-port-state"]=0
616 link["type"]="internal"
617 links.append(link)
618
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000619# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000620 js = json.dumps(links)
621 resp = Response(js, status=200, mimetype='application/json')
622 return resp
623
Ubuntuc016ba12013-02-27 21:53:41 +0000624@app.route("/controller_status")
625def controller_status():
626 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
627 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
628
629 cont_status=[]
630 for i in controllers:
631 status={}
632 onos=os.popen(onos_check % i).read()[:-1]
633 status["name"]=i
634 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000635 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000636 cont_status.append(status)
637
638 js = json.dumps(cont_status)
639 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000640 return resp
641
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000642### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000643@app.route("/gui/controller/<cmd>/<controller_name>")
644def controller_status_change(cmd, controller_name):
645 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
646 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
647
648 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000649 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000650 ret = "controller %s is up" % (controller_name)
651 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000652 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000653 ret = "controller %s is down" % (controller_name)
654
655 return ret
656
657@app.route("/gui/switch/<cmd>/<dpid>")
658def switch_status_change(cmd, dpid):
659 r = re.compile(':')
660 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000661 host=controllers[0]
662 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
663 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000664 print "cmd_string"
665
666 if cmd =="up" or cmd=="down":
667 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000668 os.popen(cmd_string)
669 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000670
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000671 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000672
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000673#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000674#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000675@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
676def link_up(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000677
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000678 cmd = 'up'
679 result=""
680
Paul Greyson472da4c2013-03-28 11:43:17 -0700681 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000682 if dpid in core_switches:
683 host = controllers[0]
684 src_ports = [1, 2, 3, 4, 5]
685 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000686 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000687 host = controllers[hostid-1]
688 if hostid == 2 :
689 src_ports = [51]
690 else :
691 src_ports = [26]
692
693 for port in src_ports :
694 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
695 print cmd_string
696 res=os.popen(cmd_string).read()
697 result = result + ' ' + res
698
699 return result
700
701
702#* Link Down
703#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000704@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000705def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
706
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000707 if src_dpid in core_switches:
708 host = controllers[0]
709 else:
710 hostid=int(src_dpid.split(':')[-2])
711 host = controllers[hostid-1]
712
713 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
714 print cmd_string
715
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000716 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000717
718 return result
719
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000720#* Create Flow
721#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000722#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 +0000723@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000724def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
725 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
726 print command
727 ret = os.popen(command).read()
728 if ret == "":
729 flow_nr=0
730 else:
731 flow_nr=int(ret)
732
733 flow_nr += 1
Umesh Krishnaswamyf22d3ed2013-04-03 11:57:54 -0700734 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 +0000735 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 +0000736 print command
737 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000738 errcode1 = os.popen(command1).read()
739 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000740
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000741#* Delete Flow
742#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000743@app.route("/gui/delflow/<flow_id>")
744def del_flow(flow_id):
745 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
746 print command
747 errcode = os.popen(command).read()
748 return errcode
749
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000750#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700751#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000752@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
753def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000754 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000755 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000756 print command
757 result = os.popen(command).read()
758 if len(result) == 0:
759 print "No Flow found"
760 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000761 except:
762 print "REST IF has issue"
763 exit
764
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000765 parsedResult = json.loads(result)
766
767 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000768 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
769 src_port = parsedResult['dataPath']['srcPort']['port']['value']
770 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
771 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000772# 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 +0000773
774 if src_dpid in core_switches:
775 host = controllers[0]
776 else:
777 hostid=int(src_dpid.split(':')[-2])
778 host = controllers[hostid-1]
779
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000780# ./runiperf.sh 2 00:00:00:00:00:00:02:02 1 00:00:00:00:00:00:03:02 1 100 15
781 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 +0000782 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000783 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000784
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000785 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000786
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000787#* Get Iperf Throughput
788#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000789@app.route("/gui/iperf/rate/<flow_id>")
790def iperf_rate(flow_id):
791 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000792 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
793 print command
794 result = os.popen(command).read()
795 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000796 resp = Response(result, status=400, mimetype='text/html')
797 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000798 except:
799 print "REST IF has issue"
800 exit
801
802 parsedResult = json.loads(result)
803
804 flowId = int(parsedResult['flowId']['value'], 16)
805 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
806 src_port = parsedResult['dataPath']['srcPort']['port']['value']
807 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
808 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
809
810 if src_dpid in core_switches:
811 host = controllers[0]
812 else:
813 hostid=int(src_dpid.split(':')[-2])
814 host = controllers[hostid-1]
815
816 try:
817 command = "curl -s http://%s:%s/log/iperf_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000818 print command
819 result = os.popen(command).read()
820 except:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000821
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000822 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000823
Masayoshi Kobayashi911f2632013-04-01 18:44:33 +0000824 resp = Response(result, status=200, mimetype='application/json')
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000825 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000826
827
Ubuntu82b8a832013-02-06 22:00:11 +0000828if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000829 random.seed()
Ubuntu82b8a832013-02-06 22:00:11 +0000830 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000831# 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")
832# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
833# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
834# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
835# 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 +0000836# print "-- query all switches --"
837# query_switch()
838# print "-- query topo --"
839# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000840# link_change(1,2,3,4)
841 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000842# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000843# print "-- query all devices --"
844# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000845# iperf_start(1,10,15)
846# iperf_rate(1)
847 switches()
Ubuntu82b8a832013-02-06 22:00:11 +0000848 else:
849 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000850 app.run(threaded=True, host="0.0.0.0", port=9000)