blob: fbe254bf8df3e291677b1ea6925e9e6903e471ae [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)
Masayoshi Kobayashi8ac33362013-04-05 03:17:26 +0000191 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
192 print "REST %s returned code %s" % (command, parsedResult['code'])
193 code=500
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000194 except:
195 print "REST IF %s has issue" % command
196 result = ""
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000197 code = 500
198
199 return (code, result)
200
201def pick_host():
202 if LB == True:
203 nr_host=len(controllers)
204 r=random.randint(0, nr_host - 1)
205 host=controllers[r]
206 else:
207 host=ONOS_DEFAULT_HOST
208
209 return "http://" + host + ":8080"
210
211## Switch ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000212@app.route("/wm/core/topology/switches/all/json")
213def switches():
214 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000215 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000216 else:
217 host = ONOS_GUI3_HOST
218
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000219 url ="%s/wm/core/topology/switches/all/json" % (host)
220 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000221
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000222 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000223 return resp
224
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000225## Link ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000226@app.route("/wm/core/topology/links/json")
227def links():
228 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000229 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000230 else:
231 host = ONOS_GUI3_HOST
232
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000233 url ="%s/wm/core/topology/links/json" % (host)
234 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000235
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000236 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000237 return resp
238
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000239## FlowSummary ##
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000240@app.route("/wm/flow/getsummary/<start>/<range>/json")
241def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000242 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000243 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000244 else:
245 host = ONOS_GUI3_HOST
246
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000247 url ="%s/wm/flow/getsummary/%s/%s/json" % (host, start, range)
248 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000249
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000250 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000251 return resp
252
253@app.route("/wm/registry/controllers/json")
254def registry_controllers():
255 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000256 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000257 else:
258 host = ONOS_GUI3_HOST
259
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000260 url= "%s/wm/registry/controllers/json" % (host)
261 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000262
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000263 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000264 return resp
265
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000266
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000267@app.route("/wm/registry/switches/json")
268def registry_switches():
269 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000270 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000271 else:
272 host = ONOS_GUI3_HOST
273
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000274 url="%s/wm/registry/switches/json" % (host)
275 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000276
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000277 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000278 return resp
279
Ubuntu82b8a832013-02-06 22:00:11 +0000280def node_id(switch_array, dpid):
281 id = -1
282 for i, val in enumerate(switch_array):
283 if val['name'] == dpid:
284 id = i
285 break
286
287 return id
288
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000289## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000290@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000291def topology_for_gui():
292 try:
293 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
294 result = os.popen(command).read()
295 parsedResult = json.loads(result)
296 except:
297 log_error("REST IF has issue: %s" % command)
298 log_error("%s" % result)
299 sys.exit(0)
300
301 topo = {}
302 switches = []
303 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000304 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000305
306 for v in parsedResult:
307 if v.has_key('dpid'):
308# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
309 dpid = str(v['dpid'])
310 state = str(v['state'])
311 sw = {}
312 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000313 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000314
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000315 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000316 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000317 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000318
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000319 try:
320 command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort)
321 result = os.popen(command).read()
322 parsedResult = json.loads(result)
323 except:
324 log_error("REST IF has issue: %s" % command)
325 log_error("%s" % result)
326
327 for key in parsedResult:
328 dpid = key
329 ctrl = parsedResult[dpid][0]['controllerId']
330 sw_id = node_id(switches, dpid)
331 if sw_id != -1:
332 if switches[sw_id]['group'] != 0:
333 switches[sw_id]['group'] = controllers.index(ctrl) + 1
334
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000335 try:
336 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000337# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000338 p1=1
339 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000340# v2 = "00:00:00:00:00:0d:00:d3"
341 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000342 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
343 result = os.popen(command).read()
344 parsedResult = json.loads(result)
345 except:
346 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000347 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000348
Ubuntu765deff2013-02-28 18:39:13 +0000349 path = []
350 if parsedResult.has_key('flowEntries'):
351 flowEntries= parsedResult['flowEntries']
352 for i, v in enumerate(flowEntries):
353 if i < len(flowEntries) - 1:
354 sdpid= flowEntries[i]['dpid']['value']
355 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700356 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000357
Ubuntu82b8a832013-02-06 22:00:11 +0000358 try:
359 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
360 result = os.popen(command).read()
361 parsedResult = json.loads(result)
362 except:
363 log_error("REST IF has issue: %s" % command)
364 log_error("%s" % result)
365 sys.exit(0)
366
367 for v in parsedResult:
368 link = {}
369 if v.has_key('dst-switch'):
370 dst_dpid = str(v['dst-switch'])
371 dst_id = node_id(switches, dst_dpid)
372 if v.has_key('src-switch'):
373 src_dpid = str(v['src-switch'])
374 src_id = node_id(switches, src_dpid)
375 link['source'] = src_id
376 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000377
378 onpath = 0
379 for (s,d) in path:
380 if s == v['src-switch'] and d == v['dst-switch']:
381 onpath = 1
382 break
383 link['type'] = onpath
384
Ubuntu82b8a832013-02-06 22:00:11 +0000385 links.append(link)
386
387 topo['nodes'] = switches
388 topo['links'] = links
389
Ubuntu82b8a832013-02-06 22:00:11 +0000390 js = json.dumps(topo)
391 resp = Response(js, status=200, mimetype='application/json')
392 return resp
393
Ubuntuaea2a682013-02-08 08:30:10 +0000394#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
395#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
396@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
397def shortest_path(v1, p1, v2, p2):
398 try:
399 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
400 result = os.popen(command).read()
401 parsedResult = json.loads(result)
402 except:
403 log_error("REST IF has issue: %s" % command)
404 log_error("%s" % result)
405 sys.exit(0)
406
407 topo = {}
408 switches = []
409 links = []
410
411 for v in parsedResult:
412 if v.has_key('dpid'):
413 dpid = str(v['dpid'])
414 state = str(v['state'])
415 sw = {}
416 sw['name']=dpid
417 if str(v['state']) == "ACTIVE":
418 if dpid[-2:-1] == "a":
419 sw['group']=1
420 if dpid[-2:-1] == "b":
421 sw['group']=2
422 if dpid[-2:-1] == "c":
423 sw['group']=3
424 if str(v['state']) == "INACTIVE":
425 sw['group']=0
426
427 switches.append(sw)
428
429 try:
430 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
431 result = os.popen(command).read()
432 parsedResult = json.loads(result)
433 except:
434 log_error("No route")
435 parsedResult = []
436# exit(1)
437
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700438 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000439 for i, v in enumerate(parsedResult):
440 if i < len(parsedResult) - 1:
441 sdpid= parsedResult[i]['switch']
442 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700443 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000444
445 try:
446 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
447 result = os.popen(command).read()
448 parsedResult = json.loads(result)
449 except:
450 log_error("REST IF has issue: %s" % command)
451 log_error("%s" % result)
452 sys.exit(0)
453
454 for v in parsedResult:
455 link = {}
456 if v.has_key('dst-switch'):
457 dst_dpid = str(v['dst-switch'])
458 dst_id = node_id(switches, dst_dpid)
459 if v.has_key('src-switch'):
460 src_dpid = str(v['src-switch'])
461 src_id = node_id(switches, src_dpid)
462 link['source'] = src_id
463 link['target'] = dst_id
464 onpath = 0
465 for (s,d) in path:
466 if s == v['src-switch'] and d == v['dst-switch']:
467 onpath = 1
468 break
469
470 link['type'] = onpath
471 links.append(link)
472
473 topo['nodes'] = switches
474 topo['links'] = links
475
Ubuntuaea2a682013-02-08 08:30:10 +0000476 js = json.dumps(topo)
477 resp = Response(js, status=200, mimetype='application/json')
478 return resp
479
Ubuntu82b8a832013-02-06 22:00:11 +0000480@app.route("/wm/core/controller/switches/json")
481def query_switch():
482 try:
483 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
484# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000485 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000486 result = os.popen(command).read()
487 parsedResult = json.loads(result)
488 except:
489 log_error("REST IF has issue: %s" % command)
490 log_error("%s" % result)
491 sys.exit(0)
492
493# print command
494# print result
495 switches_ = []
496 for v in parsedResult:
497 if v.has_key('dpid'):
498 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
499 dpid = str(v['dpid'])
500 state = str(v['state'])
501 sw = {}
502 sw['dpid']=dpid
503 sw['active']=state
504 switches_.append(sw)
505
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000506# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000507 js = json.dumps(switches_)
508 resp = Response(js, status=200, mimetype='application/json')
509 return resp
510
511@app.route("/wm/device/")
512def devices():
513 try:
514 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
515 result = os.popen(command).read()
516 parsedResult = json.loads(result)['results']
517 except:
518 log_error("REST IF has issue: %s" % command)
519 log_error("%s" % result)
520 sys.exit(0)
521
522 devices = []
523 for v in parsedResult:
524 dl_addr = v['dl_addr']
525 nw_addr = v['nw_addr']
526 vertex = v['_id']
527 mac = []
528 mac.append(dl_addr)
529 ip = []
530 ip.append(nw_addr)
531 device = {}
532 device['entryClass']="DefaultEntryClass"
533 device['mac']=mac
534 device['ipv4']=ip
535 device['vlan']=[]
536 device['lastSeen']=0
537 attachpoints =[]
538
539 port, dpid = deviceV_to_attachpoint(vertex)
540 attachpoint = {}
541 attachpoint['port']=port
542 attachpoint['switchDPID']=dpid
543 attachpoints.append(attachpoint)
544 device['attachmentPoint']=attachpoints
545 devices.append(device)
546
Ubuntu82b8a832013-02-06 22:00:11 +0000547 js = json.dumps(devices)
548 resp = Response(js, status=200, mimetype='application/json')
549 return resp
550
551#{"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}
552
Ubuntu82b8a832013-02-06 22:00:11 +0000553## return fake stat for now
554@app.route("/wm/core/switch/<switchId>/<statType>/json")
555def switch_stat(switchId, statType):
556 if statType == "desc":
557 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
558 ret = {}
559 ret[switchId]=desc
560 elif statType == "aggregate":
561 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
562 ret = {}
563 ret[switchId]=aggr
564 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700565 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000566
567 js = json.dumps(ret)
568 resp = Response(js, status=200, mimetype='application/json')
569 return resp
570
571
572@app.route("/wm/topology/links/json")
573def query_links():
574 try:
575 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000576 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000577 result = os.popen(command).read()
578 parsedResult = json.loads(result)['results']
579 except:
580 log_error("REST IF has issue: %s" % command)
581 log_error("%s" % result)
582 sys.exit(0)
583
584 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000585# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000586 sport = []
587 links = []
588 for v in parsedResult:
589 srcport = v['_id']
590 try:
591 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
592 print command
593 result = os.popen(command).read()
594 linkResults = json.loads(result)['results']
595 except:
596 log_error("REST IF has issue: %s" % command)
597 log_error("%s" % result)
598 sys.exit(0)
599
600 for p in linkResults:
601 if p.has_key('type') and p['type'] == "port":
602 dstport = p['_id']
603 (sport, sdpid) = portV_to_port_dpid(srcport)
604 (dport, ddpid) = portV_to_port_dpid(dstport)
605 link = {}
606 link["src-switch"]=sdpid
607 link["src-port"]=sport
608 link["src-port-state"]=0
609 link["dst-switch"]=ddpid
610 link["dst-port"]=dport
611 link["dst-port-state"]=0
612 link["type"]="internal"
613 links.append(link)
614
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000615# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000616 js = json.dumps(links)
617 resp = Response(js, status=200, mimetype='application/json')
618 return resp
619
Ubuntuc016ba12013-02-27 21:53:41 +0000620@app.route("/controller_status")
621def controller_status():
622 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
623 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
624
625 cont_status=[]
626 for i in controllers:
627 status={}
628 onos=os.popen(onos_check % i).read()[:-1]
629 status["name"]=i
630 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000631 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000632 cont_status.append(status)
633
634 js = json.dumps(cont_status)
635 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000636 return resp
637
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000638### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000639@app.route("/gui/controller/<cmd>/<controller_name>")
640def controller_status_change(cmd, controller_name):
641 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
642 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
643
644 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000645 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000646 ret = "controller %s is up" % (controller_name)
647 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000648 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000649 ret = "controller %s is down" % (controller_name)
Umesh Krishnaswamy92d44d22013-04-04 21:41:16 -0700650 elif cmd == "local":
651 ret = "Switch local"
652 elif cmd == "ext":
653 ret = "Switch external"
654
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000655
656 return ret
657
658@app.route("/gui/switch/<cmd>/<dpid>")
659def switch_status_change(cmd, dpid):
660 r = re.compile(':')
661 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000662 host=controllers[0]
663 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
664 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000665 print "cmd_string"
666
667 if cmd =="up" or cmd=="down":
668 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000669 os.popen(cmd_string)
670 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000671
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000672 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000673
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000674#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000675#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000676@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
677def link_up(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000678
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000679 cmd = 'up'
680 result=""
681
Paul Greyson472da4c2013-03-28 11:43:17 -0700682 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000683 if dpid in core_switches:
684 host = controllers[0]
685 src_ports = [1, 2, 3, 4, 5]
686 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000687 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000688 host = controllers[hostid-1]
689 if hostid == 2 :
690 src_ports = [51]
691 else :
692 src_ports = [26]
693
694 for port in src_ports :
695 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
696 print cmd_string
697 res=os.popen(cmd_string).read()
698 result = result + ' ' + res
699
700 return result
701
702
703#* Link Down
704#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000705@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000706def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
707
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000708 if src_dpid in core_switches:
709 host = controllers[0]
710 else:
711 hostid=int(src_dpid.split(':')[-2])
712 host = controllers[hostid-1]
713
714 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
715 print cmd_string
716
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000717 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000718
719 return result
720
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000721#* Create Flow
722#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000723#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 +0000724@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000725def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
726 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
727 print command
728 ret = os.popen(command).read()
729 if ret == "":
730 flow_nr=0
731 else:
732 flow_nr=int(ret)
733
734 flow_nr += 1
Umesh Krishnaswamyf22d3ed2013-04-03 11:57:54 -0700735 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 +0000736 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 +0000737 print command
738 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000739 errcode1 = os.popen(command1).read()
740 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000741
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000742#* Delete Flow
743#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000744@app.route("/gui/delflow/<flow_id>")
745def del_flow(flow_id):
746 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
747 print command
748 errcode = os.popen(command).read()
749 return errcode
750
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000751#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700752#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000753@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
754def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000755 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000756 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000757 print command
758 result = os.popen(command).read()
759 if len(result) == 0:
760 print "No Flow found"
761 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000762 except:
763 print "REST IF has issue"
764 exit
765
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000766 parsedResult = json.loads(result)
767
768 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000769 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
770 src_port = parsedResult['dataPath']['srcPort']['port']['value']
771 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
772 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000773# 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 +0000774
775 if src_dpid in core_switches:
776 host = controllers[0]
777 else:
778 hostid=int(src_dpid.split(':')[-2])
779 host = controllers[hostid-1]
780
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000781# ./runiperf.sh 2 00:00:00:00:00:00:02:02 1 00:00:00:00:00:00:03:02 1 100 15
782 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 +0000783 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000784 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000785
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000786 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000787
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000788#* Get Iperf Throughput
789#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000790@app.route("/gui/iperf/rate/<flow_id>")
791def iperf_rate(flow_id):
792 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000793 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
794 print command
795 result = os.popen(command).read()
796 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000797 resp = Response(result, status=400, mimetype='text/html')
798 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000799 except:
800 print "REST IF has issue"
801 exit
802
803 parsedResult = json.loads(result)
804
805 flowId = int(parsedResult['flowId']['value'], 16)
806 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
807 src_port = parsedResult['dataPath']['srcPort']['port']['value']
808 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
809 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
810
811 if src_dpid in core_switches:
812 host = controllers[0]
813 else:
814 hostid=int(src_dpid.split(':')[-2])
815 host = controllers[hostid-1]
816
817 try:
818 command = "curl -s http://%s:%s/log/iperf_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000819 print command
820 result = os.popen(command).read()
821 except:
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000822 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000823
Masayoshi Kobayashib56f2972013-04-05 02:57:29 +0000824 if len(result) == 0:
825 resp = Response(result, status=400, mimetype='text/html')
826 return "no iperf file found (flowid %s)" % flow_id;
827 else:
828 resp = Response(result, status=200, mimetype='application/json')
829 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000830
831
Ubuntu82b8a832013-02-06 22:00:11 +0000832if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000833 random.seed()
Ubuntu82b8a832013-02-06 22:00:11 +0000834 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000835# 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")
836# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
837# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
838# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
839# 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 +0000840# print "-- query all switches --"
841# query_switch()
842# print "-- query topo --"
843# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000844# link_change(1,2,3,4)
845 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000846# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000847# print "-- query all devices --"
848# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000849# iperf_start(1,10,15)
850# iperf_rate(1)
851 switches()
Ubuntu82b8a832013-02-06 22:00:11 +0000852 else:
853 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000854 app.run(threaded=True, host="0.0.0.0", port=9000)