blob: ce7b84ed7dae8c070b503a4c2b83a36d3fd8f57e [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
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000022#controllers=["onosgui1", "onosgui2", "onosgui3", "onosgui4", "onosgui5", "onosgui6", "onosgui7", "onosgui8"]
23#core_switches=["00:00:00:00:ba:5e:ba:11", "00:00:00:00:00:00:ba:12", "00:00:20:4e:7f:51:8a:35", "00:00:00:00:ba:5e:ba:13", "00:00:00:08:a2:08:f9:01", "00:00:00:16:97:08:9a:46"]
24#ONOS_GUI3_HOST="http://gui3.onlab.us:8080"
25#ONOS_GUI3_CONTROL_HOST="http://gui3.onlab.us:8081"
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000026
27# Settings for running on dev testbed. Replace dev
28#controllers=["onosdevb1", "onosdevb2", "onosdevb3", "onosdevb4"]
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000029controllers=["onosdevt1", "onosdevt2", "onosdevt3", "onosdevt4", "onosdevt5", "onosdevt6", "onosdevt7", "onosdevt8"]
30core_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 Kobayashi2ae891a2013-04-05 02:16:19 +000032ONOS_GUI3_HOST="http://devt-gui.onlab.us:8080"
33ONOS_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
156###### ONOS RESET API ##############################
157## Worker Func ###
158def get_json(url):
159 code = 200
160 try:
161 command = "curl -s %s" % (url)
162 result = os.popen(command).read()
163 parsedResult = json.loads(result)
164 except:
165 print "REST IF %s has issue" % command
166 result = ""
167
168 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
169 print "REST %s returned code %s" % (command, parsedResult['code'])
170 code=500
171
172 if result == "":
173 code = 500
174
175 return (code, result)
176
177def pick_host():
178 if LB == True:
179 nr_host=len(controllers)
180 r=random.randint(0, nr_host - 1)
181 host=controllers[r]
182 else:
183 host=ONOS_DEFAULT_HOST
184
185 return "http://" + host + ":8080"
186
187## Switch ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000188@app.route("/wm/core/topology/switches/all/json")
189def switches():
190 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000191 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000192 else:
193 host = ONOS_GUI3_HOST
194
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000195 url ="%s/wm/core/topology/switches/all/json" % (host)
196 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000197
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000198 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000199 return resp
200
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000201## Link ##
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000202@app.route("/wm/core/topology/links/json")
203def links():
204 if request.args.get('proxy') == None:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000205 host = pick_host()
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000206 else:
207 host = ONOS_GUI3_HOST
208
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000209 url ="%s/wm/core/topology/links/json" % (host)
210 (code, result) = get_json(url)
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000211
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000212 resp = Response(result, status=code, mimetype='application/json')
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000213 return resp
214
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000215## FlowSummary ##
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000216@app.route("/wm/flow/getsummary/<start>/<range>/json")
217def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000218 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/flow/getsummary/%s/%s/json" % (host, start, range)
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
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000230@app.route("/wm/registry/controllers/json")
231def registry_controllers():
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/registry/controllers/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
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000244@app.route("/wm/registry/switches/json")
245def registry_switches():
246 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/registry/switches/json" % (host)
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
Ubuntu82b8a832013-02-06 22:00:11 +0000257def node_id(switch_array, dpid):
258 id = -1
259 for i, val in enumerate(switch_array):
260 if val['name'] == dpid:
261 id = i
262 break
263
264 return id
265
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000266## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000267@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000268def topology_for_gui():
269 try:
270 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
271 result = os.popen(command).read()
272 parsedResult = json.loads(result)
273 except:
274 log_error("REST IF has issue: %s" % command)
275 log_error("%s" % result)
276 sys.exit(0)
277
278 topo = {}
279 switches = []
280 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000281 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000282
283 for v in parsedResult:
284 if v.has_key('dpid'):
285# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
286 dpid = str(v['dpid'])
287 state = str(v['state'])
288 sw = {}
289 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000290 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000291
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000292 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000293 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000294 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000295
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000296 try:
297 command = "curl -s \'http://%s:%s/wm/registry/switches/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
304 for key in parsedResult:
305 dpid = key
306 ctrl = parsedResult[dpid][0]['controllerId']
307 sw_id = node_id(switches, dpid)
308 if sw_id != -1:
309 if switches[sw_id]['group'] != 0:
310 switches[sw_id]['group'] = controllers.index(ctrl) + 1
311
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000312 try:
313 v1 = "00:00:00:00:00:0a:0d:00"
Ubuntu765deff2013-02-28 18:39:13 +0000314# v1 = "00:00:00:00:00:0d:00:d1"
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000315 p1=1
316 v2 = "00:00:00:00:00:0b:0d:03"
Ubuntu765deff2013-02-28 18:39:13 +0000317# v2 = "00:00:00:00:00:0d:00:d3"
318 p2=1
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000319 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
320 result = os.popen(command).read()
321 parsedResult = json.loads(result)
322 except:
323 log_error("No route")
Ubuntu765deff2013-02-28 18:39:13 +0000324 parsedResult = {}
Ubuntu5b2b24a2013-02-27 09:51:13 +0000325
Ubuntu765deff2013-02-28 18:39:13 +0000326 path = []
327 if parsedResult.has_key('flowEntries'):
328 flowEntries= parsedResult['flowEntries']
329 for i, v in enumerate(flowEntries):
330 if i < len(flowEntries) - 1:
331 sdpid= flowEntries[i]['dpid']['value']
332 ddpid = flowEntries[i+1]['dpid']['value']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700333 path.append( (sdpid, ddpid))
Ubuntu5b2b24a2013-02-27 09:51:13 +0000334
Ubuntu82b8a832013-02-06 22:00:11 +0000335 try:
336 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
337 result = os.popen(command).read()
338 parsedResult = json.loads(result)
339 except:
340 log_error("REST IF has issue: %s" % command)
341 log_error("%s" % result)
342 sys.exit(0)
343
344 for v in parsedResult:
345 link = {}
346 if v.has_key('dst-switch'):
347 dst_dpid = str(v['dst-switch'])
348 dst_id = node_id(switches, dst_dpid)
349 if v.has_key('src-switch'):
350 src_dpid = str(v['src-switch'])
351 src_id = node_id(switches, src_dpid)
352 link['source'] = src_id
353 link['target'] = dst_id
Masayoshi Kobayashi3bc5fde2013-02-28 01:02:54 +0000354
355 onpath = 0
356 for (s,d) in path:
357 if s == v['src-switch'] and d == v['dst-switch']:
358 onpath = 1
359 break
360 link['type'] = onpath
361
Ubuntu82b8a832013-02-06 22:00:11 +0000362 links.append(link)
363
364 topo['nodes'] = switches
365 topo['links'] = links
366
Ubuntu82b8a832013-02-06 22:00:11 +0000367 js = json.dumps(topo)
368 resp = Response(js, status=200, mimetype='application/json')
369 return resp
370
Ubuntuaea2a682013-02-08 08:30:10 +0000371#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
372#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
373@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
374def shortest_path(v1, p1, v2, p2):
375 try:
376 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
377 result = os.popen(command).read()
378 parsedResult = json.loads(result)
379 except:
380 log_error("REST IF has issue: %s" % command)
381 log_error("%s" % result)
382 sys.exit(0)
383
384 topo = {}
385 switches = []
386 links = []
387
388 for v in parsedResult:
389 if v.has_key('dpid'):
390 dpid = str(v['dpid'])
391 state = str(v['state'])
392 sw = {}
393 sw['name']=dpid
394 if str(v['state']) == "ACTIVE":
395 if dpid[-2:-1] == "a":
396 sw['group']=1
397 if dpid[-2:-1] == "b":
398 sw['group']=2
399 if dpid[-2:-1] == "c":
400 sw['group']=3
401 if str(v['state']) == "INACTIVE":
402 sw['group']=0
403
404 switches.append(sw)
405
406 try:
407 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
408 result = os.popen(command).read()
409 parsedResult = json.loads(result)
410 except:
411 log_error("No route")
412 parsedResult = []
413# exit(1)
414
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700415 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000416 for i, v in enumerate(parsedResult):
417 if i < len(parsedResult) - 1:
418 sdpid= parsedResult[i]['switch']
419 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700420 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000421
422 try:
423 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
424 result = os.popen(command).read()
425 parsedResult = json.loads(result)
426 except:
427 log_error("REST IF has issue: %s" % command)
428 log_error("%s" % result)
429 sys.exit(0)
430
431 for v in parsedResult:
432 link = {}
433 if v.has_key('dst-switch'):
434 dst_dpid = str(v['dst-switch'])
435 dst_id = node_id(switches, dst_dpid)
436 if v.has_key('src-switch'):
437 src_dpid = str(v['src-switch'])
438 src_id = node_id(switches, src_dpid)
439 link['source'] = src_id
440 link['target'] = dst_id
441 onpath = 0
442 for (s,d) in path:
443 if s == v['src-switch'] and d == v['dst-switch']:
444 onpath = 1
445 break
446
447 link['type'] = onpath
448 links.append(link)
449
450 topo['nodes'] = switches
451 topo['links'] = links
452
Ubuntuaea2a682013-02-08 08:30:10 +0000453 js = json.dumps(topo)
454 resp = Response(js, status=200, mimetype='application/json')
455 return resp
456
Ubuntu82b8a832013-02-06 22:00:11 +0000457@app.route("/wm/core/controller/switches/json")
458def query_switch():
459 try:
460 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
461# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000462 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000463 result = os.popen(command).read()
464 parsedResult = json.loads(result)
465 except:
466 log_error("REST IF has issue: %s" % command)
467 log_error("%s" % result)
468 sys.exit(0)
469
470# print command
471# print result
472 switches_ = []
473 for v in parsedResult:
474 if v.has_key('dpid'):
475 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
476 dpid = str(v['dpid'])
477 state = str(v['state'])
478 sw = {}
479 sw['dpid']=dpid
480 sw['active']=state
481 switches_.append(sw)
482
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000483# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000484 js = json.dumps(switches_)
485 resp = Response(js, status=200, mimetype='application/json')
486 return resp
487
488@app.route("/wm/device/")
489def devices():
490 try:
491 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
492 result = os.popen(command).read()
493 parsedResult = json.loads(result)['results']
494 except:
495 log_error("REST IF has issue: %s" % command)
496 log_error("%s" % result)
497 sys.exit(0)
498
499 devices = []
500 for v in parsedResult:
501 dl_addr = v['dl_addr']
502 nw_addr = v['nw_addr']
503 vertex = v['_id']
504 mac = []
505 mac.append(dl_addr)
506 ip = []
507 ip.append(nw_addr)
508 device = {}
509 device['entryClass']="DefaultEntryClass"
510 device['mac']=mac
511 device['ipv4']=ip
512 device['vlan']=[]
513 device['lastSeen']=0
514 attachpoints =[]
515
516 port, dpid = deviceV_to_attachpoint(vertex)
517 attachpoint = {}
518 attachpoint['port']=port
519 attachpoint['switchDPID']=dpid
520 attachpoints.append(attachpoint)
521 device['attachmentPoint']=attachpoints
522 devices.append(device)
523
Ubuntu82b8a832013-02-06 22:00:11 +0000524 js = json.dumps(devices)
525 resp = Response(js, status=200, mimetype='application/json')
526 return resp
527
528#{"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}
529
Ubuntu82b8a832013-02-06 22:00:11 +0000530## return fake stat for now
531@app.route("/wm/core/switch/<switchId>/<statType>/json")
532def switch_stat(switchId, statType):
533 if statType == "desc":
534 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
535 ret = {}
536 ret[switchId]=desc
537 elif statType == "aggregate":
538 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
539 ret = {}
540 ret[switchId]=aggr
541 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700542 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000543
544 js = json.dumps(ret)
545 resp = Response(js, status=200, mimetype='application/json')
546 return resp
547
548
549@app.route("/wm/topology/links/json")
550def query_links():
551 try:
552 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000553 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000554 result = os.popen(command).read()
555 parsedResult = json.loads(result)['results']
556 except:
557 log_error("REST IF has issue: %s" % command)
558 log_error("%s" % result)
559 sys.exit(0)
560
561 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000562# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000563 sport = []
564 links = []
565 for v in parsedResult:
566 srcport = v['_id']
567 try:
568 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
569 print command
570 result = os.popen(command).read()
571 linkResults = json.loads(result)['results']
572 except:
573 log_error("REST IF has issue: %s" % command)
574 log_error("%s" % result)
575 sys.exit(0)
576
577 for p in linkResults:
578 if p.has_key('type') and p['type'] == "port":
579 dstport = p['_id']
580 (sport, sdpid) = portV_to_port_dpid(srcport)
581 (dport, ddpid) = portV_to_port_dpid(dstport)
582 link = {}
583 link["src-switch"]=sdpid
584 link["src-port"]=sport
585 link["src-port-state"]=0
586 link["dst-switch"]=ddpid
587 link["dst-port"]=dport
588 link["dst-port-state"]=0
589 link["type"]="internal"
590 links.append(link)
591
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000592# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000593 js = json.dumps(links)
594 resp = Response(js, status=200, mimetype='application/json')
595 return resp
596
Ubuntuc016ba12013-02-27 21:53:41 +0000597@app.route("/controller_status")
598def controller_status():
599 onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'"
600 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
601
602 cont_status=[]
603 for i in controllers:
604 status={}
605 onos=os.popen(onos_check % i).read()[:-1]
606 status["name"]=i
607 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000608 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000609 cont_status.append(status)
610
611 js = json.dumps(cont_status)
612 resp = Response(js, status=200, mimetype='application/json')
Ubuntuc016ba12013-02-27 21:53:41 +0000613 return resp
614
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000615### Command ###
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000616@app.route("/gui/controller/<cmd>/<controller_name>")
617def controller_status_change(cmd, controller_name):
618 start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name)
619 stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name)
620
621 if cmd == "up":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000622 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000623 ret = "controller %s is up" % (controller_name)
624 elif cmd == "down":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000625 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000626 ret = "controller %s is down" % (controller_name)
627
628 return ret
629
630@app.route("/gui/switch/<cmd>/<dpid>")
631def switch_status_change(cmd, dpid):
632 r = re.compile(':')
633 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000634 host=controllers[0]
635 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
636 get_status="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000637 print "cmd_string"
638
639 if cmd =="up" or cmd=="down":
640 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000641 os.popen(cmd_string)
642 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000643
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000644 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000645
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000646#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000647#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000648@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
649def link_up(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000650
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000651 cmd = 'up'
652 result=""
653
Paul Greyson472da4c2013-03-28 11:43:17 -0700654 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000655 if dpid in core_switches:
656 host = controllers[0]
657 src_ports = [1, 2, 3, 4, 5]
658 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000659 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000660 host = controllers[hostid-1]
661 if hostid == 2 :
662 src_ports = [51]
663 else :
664 src_ports = [26]
665
666 for port in src_ports :
667 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
668 print cmd_string
669 res=os.popen(cmd_string).read()
670 result = result + ' ' + res
671
672 return result
673
674
675#* Link Down
676#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000677@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000678def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
679
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000680 if src_dpid in core_switches:
681 host = controllers[0]
682 else:
683 hostid=int(src_dpid.split(':')[-2])
684 host = controllers[hostid-1]
685
686 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
687 print cmd_string
688
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000689 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000690
691 return result
692
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000693#* Create Flow
694#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000695#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 +0000696@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000697def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
698 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
699 print command
700 ret = os.popen(command).read()
701 if ret == "":
702 flow_nr=0
703 else:
704 flow_nr=int(ret)
705
706 flow_nr += 1
Umesh Krishnaswamyf22d3ed2013-04-03 11:57:54 -0700707 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 +0000708 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 +0000709 print command
710 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000711 errcode1 = os.popen(command1).read()
712 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000713
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000714#* Delete Flow
715#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000716@app.route("/gui/delflow/<flow_id>")
717def del_flow(flow_id):
718 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
719 print command
720 errcode = os.popen(command).read()
721 return errcode
722
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000723#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700724#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000725@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
726def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000727 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000728 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000729 print command
730 result = os.popen(command).read()
731 if len(result) == 0:
732 print "No Flow found"
733 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000734 except:
735 print "REST IF has issue"
736 exit
737
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000738 parsedResult = json.loads(result)
739
740 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000741 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
742 src_port = parsedResult['dataPath']['srcPort']['port']['value']
743 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
744 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000745# 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 +0000746
747 if src_dpid in core_switches:
748 host = controllers[0]
749 else:
750 hostid=int(src_dpid.split(':')[-2])
751 host = controllers[hostid-1]
752
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000753# ./runiperf.sh 2 00:00:00:00:00:00:02:02 1 00:00:00:00:00:00:03:02 1 100 15
754 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 +0000755 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000756 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000757
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000758 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000759
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000760#* Get Iperf Throughput
761#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000762@app.route("/gui/iperf/rate/<flow_id>")
763def iperf_rate(flow_id):
764 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000765 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
766 print command
767 result = os.popen(command).read()
768 if len(result) == 0:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000769 resp = Response(result, status=400, mimetype='text/html')
770 return "no such iperf flow (flowid %s)" % flow_id;
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000771 except:
772 print "REST IF has issue"
773 exit
774
775 parsedResult = json.loads(result)
776
777 flowId = int(parsedResult['flowId']['value'], 16)
778 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
779 src_port = parsedResult['dataPath']['srcPort']['port']['value']
780 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
781 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
782
783 if src_dpid in core_switches:
784 host = controllers[0]
785 else:
786 hostid=int(src_dpid.split(':')[-2])
787 host = controllers[hostid-1]
788
789 try:
790 command = "curl -s http://%s:%s/log/iperf_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000791 print command
792 result = os.popen(command).read()
793 except:
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000794
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000795 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000796
Masayoshi Kobayashi911f2632013-04-01 18:44:33 +0000797 resp = Response(result, status=200, mimetype='application/json')
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000798 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000799
800
Ubuntu82b8a832013-02-06 22:00:11 +0000801if __name__ == "__main__":
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000802 random.seed()
Ubuntu82b8a832013-02-06 22:00:11 +0000803 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000804# 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")
805# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
806# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
807# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
808# 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 +0000809# print "-- query all switches --"
810# query_switch()
811# print "-- query topo --"
812# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000813# link_change(1,2,3,4)
814 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000815# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000816# print "-- query all devices --"
817# devices()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +0000818# iperf_start(1,10,15)
819# iperf_rate(1)
820 switches()
Ubuntu82b8a832013-02-06 22:00:11 +0000821 else:
822 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000823 app.run(threaded=True, host="0.0.0.0", port=9000)