blob: 32386564eab3dce68ba4248da84d2ae78937bf4c [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
10
Masayoshi Kobayashi51011522013-03-27 00:18:12 +000011import re
12
Ubuntu82b8a832013-02-06 22:00:11 +000013from flask import Flask, json, Response, render_template, make_response, request
14
15## Global Var ##
Ubuntuf6ce96c2013-02-07 01:45:07 +000016RestIP="localhost"
Ubuntu82b8a832013-02-06 22:00:11 +000017RestPort=8080
18#DBName="onos-network-map"
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000019
20## Uncomment the desired block based on your testbed environment
21
22# Settings for running on production
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000023core_switches=["00:00:00:00:ba:5e:ba:11", "00:00:00:00:00:00:ba:12", "00:00:20:4e:7f:51:8a:35", "00:00:00:00:ba:5e:ba:13", "00:00:00:08:a2:08:f9:01", "00:00:00:16:97:08:9a:46"]
Pankaj Berde2239f0d2013-04-04 09:42:43 -070024ONOS_GUI3_HOST="http://localhost:9000"
25ONOS_GUI3_CONTROL_HOST="http://localhost:9000"
Masayoshi Kobayashia9ed33a2013-03-27 23:14:18 +000026
27# Settings for running on dev testbed. Replace dev
28#controllers=["onosdevb1", "onosdevb2", "onosdevb3", "onosdevb4"]
29#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"]
30#ONOS_GUI3_HOST="http://devb-gui.onlab.us:8080"
31#ONOS_GUI3_CONTROL_HOST="http://devb-gui.onlab.us:8080"
32
33ONOS_LOCAL_HOST="http://localhost:8080" ;# for Amazon EC2
Pankaj Berde2239f0d2013-04-04 09:42:43 -070034controllers=["Berde-MBP.local"]
35#controllers=["onosgui1", "onosgui2", "onosgui3", "onosgui4", "onosgui5", "onosgui6", "onosgui7", "onosgui8"]
36#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"]
37core_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 Kobayashi1e072382013-03-27 05:17:09 +000038
39nr_flow=0
Ubuntu82b8a832013-02-06 22:00:11 +000040
41DEBUG=1
42pp = pprint.PrettyPrinter(indent=4)
43
44app = Flask(__name__)
45
46## Worker Functions ##
47def log_error(txt):
48 print '%s' % (txt)
49
50def debug(txt):
51 if DEBUG:
52 print '%s' % (txt)
53
54## Rest APIs ##
55### File Fetch ###
56@app.route('/ui/img/<filename>', methods=['GET'])
57@app.route('/img/<filename>', methods=['GET'])
58@app.route('/css/<filename>', methods=['GET'])
59@app.route('/js/models/<filename>', methods=['GET'])
60@app.route('/js/views/<filename>', methods=['GET'])
61@app.route('/js/<filename>', methods=['GET'])
62@app.route('/lib/<filename>', methods=['GET'])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +000063@app.route('/log/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000064@app.route('/', methods=['GET'])
65@app.route('/<filename>', methods=['GET'])
66@app.route('/tpl/<filename>', methods=['GET'])
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000067@app.route('/ons-demo/<filename>', methods=['GET'])
68@app.route('/ons-demo/js/<filename>', methods=['GET'])
69@app.route('/ons-demo/css/<filename>', methods=['GET'])
70@app.route('/ons-demo/assets/<filename>', methods=['GET'])
71@app.route('/ons-demo/data/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000072def return_file(filename="index.html"):
73 if request.path == "/":
74 fullpath = "./index.html"
75 else:
76 fullpath = str(request.path)[1:]
77
78 response = make_response(open(fullpath).read())
79 suffix = fullpath.split(".")[-1]
80
81 if suffix == "html" or suffix == "htm":
82 response.headers["Content-type"] = "text/html"
83 elif suffix == "js":
84 response.headers["Content-type"] = "application/javascript"
85 elif suffix == "css":
86 response.headers["Content-type"] = "text/css"
87 elif suffix == "png":
88 response.headers["Content-type"] = "image/png"
Paul Greyson2913af82013-03-27 14:53:17 -070089 elif suffix == "svg":
90 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000091
92 return response
93
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000094
Paul Greyson4e6dc3a2013-03-27 11:37:14 -070095@app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
96def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port):
97 try:
Paul Greyson8d1c6362013-03-27 13:05:24 -070098 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 -070099 print command
100 result = os.popen(command).read()
101 except:
102 print "REST IF has issue"
103 exit
104
105 resp = Response(result, status=200, mimetype='application/json')
106 return resp
107
Paul Greyson8d1c6362013-03-27 13:05:24 -0700108@app.route("/proxy/gui/switch/<cmd>/<dpid>")
109def proxy_switch_status_change(cmd, dpid):
110 try:
111 command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid)
112 print command
113 result = os.popen(command).read()
114 except:
115 print "REST IF has issue"
116 exit
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700117
Paul Greyson8d1c6362013-03-27 13:05:24 -0700118 resp = Response(result, status=200, mimetype='application/json')
119 return resp
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700120
Paul Greyson2913af82013-03-27 14:53:17 -0700121@app.route("/proxy/gui/controller/<cmd>/<controller_name>")
122def proxy_controller_status_change(cmd, controller_name):
123 try:
124 command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name)
125 print command
126 result = os.popen(command).read()
127 except:
128 print "REST IF has issue"
129 exit
130
131 resp = Response(result, status=200, mimetype='application/json')
132 return resp
133
Paul Greyson472da4c2013-03-28 11:43:17 -0700134@app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
135def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
136 try:
137 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)
138 print command
139 result = os.popen(command).read()
140 except:
141 print "REST IF has issue"
142 exit
143
144 resp = Response(result, status=200, mimetype='application/json')
145 return resp
146
Paul Greyson6f918402013-03-28 12:18:30 -0700147@app.route("/proxy/gui/delflow/<flow_id>")
148def proxy_del_flow(flow_id):
149 try:
150 command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id)
151 print command
152 result = os.popen(command).read()
153 except:
154 print "REST IF has issue"
155 exit
156
157 resp = Response(result, status=200, mimetype='application/json')
158 return resp
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000159@app.route("/wm/core/topology/switches/all/json")
160def switches():
161 if request.args.get('proxy') == None:
162 host = ONOS_LOCAL_HOST
163 else:
164 host = ONOS_GUI3_HOST
165
166 try:
167 command = "curl -s %s/wm/core/topology/switches/all/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700168# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000169 result = os.popen(command).read()
170 except:
171 print "REST IF has issue"
172 exit
173
174 resp = Response(result, status=200, mimetype='application/json')
175 return resp
176
177@app.route("/wm/core/topology/links/json")
178def links():
179 if request.args.get('proxy') == None:
180 host = ONOS_LOCAL_HOST
181 else:
182 host = ONOS_GUI3_HOST
183
184 try:
185 command = "curl -s %s/wm/core/topology/links/json" % (host)
Paul Greyson8d1c6362013-03-27 13:05:24 -0700186 print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000187 result = os.popen(command).read()
188 except:
189 print "REST IF has issue"
190 exit
191
192 resp = Response(result, status=200, mimetype='application/json')
193 return resp
194
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000195@app.route("/wm/flow/getsummary/<start>/<range>/json")
196def flows(start, range):
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000197 if request.args.get('proxy') == None:
198 host = ONOS_LOCAL_HOST
199 else:
200 host = ONOS_GUI3_HOST
201
202 try:
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000203 command = "curl -s %s/wm/flow/getsummary/%s/%s/json" % (host, start, range)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700204# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000205 result = os.popen(command).read()
206 except:
207 print "REST IF has issue"
208 exit
209
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000210
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000211 resp = Response(result, status=200, mimetype='application/json')
212 return resp
213
214@app.route("/wm/registry/controllers/json")
215def registry_controllers():
216 if request.args.get('proxy') == None:
217 host = ONOS_LOCAL_HOST
218 else:
219 host = ONOS_GUI3_HOST
220
221 try:
222 command = "curl -s %s/wm/registry/controllers/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700223# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000224 result = os.popen(command).read()
225 except:
226 print "REST IF has issue"
227 exit
228
229 resp = Response(result, status=200, mimetype='application/json')
230 return resp
231
232@app.route("/wm/registry/switches/json")
233def registry_switches():
234 if request.args.get('proxy') == None:
235 host = ONOS_LOCAL_HOST
236 else:
237 host = ONOS_GUI3_HOST
238
239 try:
240 command = "curl -s %s/wm/registry/switches/json" % (host)
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700241# print command
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000242 result = os.popen(command).read()
243 except:
244 print "REST IF has issue"
245 exit
246
247 resp = Response(result, status=200, mimetype='application/json')
248 return resp
249
Ubuntu82b8a832013-02-06 22:00:11 +0000250
251def node_id(switch_array, dpid):
252 id = -1
253 for i, val in enumerate(switch_array):
254 if val['name'] == dpid:
255 id = i
256 break
257
258 return id
259
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000260## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000261@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000262def topology_for_gui():
263 try:
264 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
265 result = os.popen(command).read()
266 parsedResult = json.loads(result)
267 except:
268 log_error("REST IF has issue: %s" % command)
269 log_error("%s" % result)
270 sys.exit(0)
271
272 topo = {}
273 switches = []
274 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000275 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000276
277 for v in parsedResult:
278 if v.has_key('dpid'):
279# if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
280 dpid = str(v['dpid'])
281 state = str(v['state'])
282 sw = {}
283 sw['name']=dpid
Ubuntu5b2b24a2013-02-27 09:51:13 +0000284 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000285
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000286 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000287 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000288 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000289
Ubuntu37ebda62013-03-01 00:35:31 +0000290## Comment in if we need devies
291# sw_index = len(switches) - 1
292# for p in v['ports']:
293# for d in p['devices']:
294# device = {}
295# device['attached_switch']=dpid
296# device['name']=d['mac']
297# if d['state'] == "ACTIVE":
298# device['group']=1000
299# else:
300# device['group']=1001
301#
302# switches.append(device)
303# device_index = len (switches) -1
304# link = {}
305# link['source'] = device_index
306# link['target'] = sw_index
307# link['type'] = -1
308# links.append(link)
309# link = {}
310# link['source'] = sw_index
311# link['target'] = device_index
312# link['type'] = -1
313# links.append(link)
314
Ubuntu5b2b24a2013-02-27 09:51:13 +0000315# try:
316# command = "curl -s \'http://%s:%s/wm/registry/controllers/json\'" % (RestIP, RestPort)
317# result = os.popen(command).read()
318# controllers = json.loads(result)
319# except:
320# log_error("xx REST IF has issue: %s" % command)
321# log_error("%s" % result)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000322
323 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
Ubuntu37ebda62013-03-01 00:35:31 +0000394 pp.pprint(topo)
Ubuntu82b8a832013-02-06 22:00:11 +0000395 js = json.dumps(topo)
396 resp = Response(js, status=200, mimetype='application/json')
397 return resp
398
Ubuntuaea2a682013-02-08 08:30:10 +0000399#@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json")
400#@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
401@app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json")
402def shortest_path(v1, p1, v2, p2):
403 try:
404 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
405 result = os.popen(command).read()
406 parsedResult = json.loads(result)
407 except:
408 log_error("REST IF has issue: %s" % command)
409 log_error("%s" % result)
410 sys.exit(0)
411
412 topo = {}
413 switches = []
414 links = []
415
416 for v in parsedResult:
417 if v.has_key('dpid'):
418 dpid = str(v['dpid'])
419 state = str(v['state'])
420 sw = {}
421 sw['name']=dpid
422 if str(v['state']) == "ACTIVE":
423 if dpid[-2:-1] == "a":
424 sw['group']=1
425 if dpid[-2:-1] == "b":
426 sw['group']=2
427 if dpid[-2:-1] == "c":
428 sw['group']=3
429 if str(v['state']) == "INACTIVE":
430 sw['group']=0
431
432 switches.append(sw)
433
434 try:
435 command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2)
436 result = os.popen(command).read()
437 parsedResult = json.loads(result)
438 except:
439 log_error("No route")
440 parsedResult = []
441# exit(1)
442
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700443 path = [];
Ubuntuaea2a682013-02-08 08:30:10 +0000444 for i, v in enumerate(parsedResult):
445 if i < len(parsedResult) - 1:
446 sdpid= parsedResult[i]['switch']
447 ddpid = parsedResult[i+1]['switch']
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700448 path.append( (sdpid, ddpid))
Ubuntuaea2a682013-02-08 08:30:10 +0000449
450 try:
451 command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort)
452 result = os.popen(command).read()
453 parsedResult = json.loads(result)
454 except:
455 log_error("REST IF has issue: %s" % command)
456 log_error("%s" % result)
457 sys.exit(0)
458
459 for v in parsedResult:
460 link = {}
461 if v.has_key('dst-switch'):
462 dst_dpid = str(v['dst-switch'])
463 dst_id = node_id(switches, dst_dpid)
464 if v.has_key('src-switch'):
465 src_dpid = str(v['src-switch'])
466 src_id = node_id(switches, src_dpid)
467 link['source'] = src_id
468 link['target'] = dst_id
469 onpath = 0
470 for (s,d) in path:
471 if s == v['src-switch'] and d == v['dst-switch']:
472 onpath = 1
473 break
474
475 link['type'] = onpath
476 links.append(link)
477
478 topo['nodes'] = switches
479 topo['links'] = links
480
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000481# pp.pprint(topo)
Ubuntuaea2a682013-02-08 08:30:10 +0000482 js = json.dumps(topo)
483 resp = Response(js, status=200, mimetype='application/json')
484 return resp
485
Ubuntu82b8a832013-02-06 22:00:11 +0000486@app.route("/wm/core/controller/switches/json")
487def query_switch():
488 try:
489 command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort)
490# http://localhost:8080/wm/core/topology/switches/active/json
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000491 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000492 result = os.popen(command).read()
493 parsedResult = json.loads(result)
494 except:
495 log_error("REST IF has issue: %s" % command)
496 log_error("%s" % result)
497 sys.exit(0)
498
499# print command
500# print result
501 switches_ = []
502 for v in parsedResult:
503 if v.has_key('dpid'):
504 if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes
505 dpid = str(v['dpid'])
506 state = str(v['state'])
507 sw = {}
508 sw['dpid']=dpid
509 sw['active']=state
510 switches_.append(sw)
511
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000512# pp.pprint(switches_)
Ubuntu82b8a832013-02-06 22:00:11 +0000513 js = json.dumps(switches_)
514 resp = Response(js, status=200, mimetype='application/json')
515 return resp
516
517@app.route("/wm/device/")
518def devices():
519 try:
520 command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName)
521 result = os.popen(command).read()
522 parsedResult = json.loads(result)['results']
523 except:
524 log_error("REST IF has issue: %s" % command)
525 log_error("%s" % result)
526 sys.exit(0)
527
528 devices = []
529 for v in parsedResult:
530 dl_addr = v['dl_addr']
531 nw_addr = v['nw_addr']
532 vertex = v['_id']
533 mac = []
534 mac.append(dl_addr)
535 ip = []
536 ip.append(nw_addr)
537 device = {}
538 device['entryClass']="DefaultEntryClass"
539 device['mac']=mac
540 device['ipv4']=ip
541 device['vlan']=[]
542 device['lastSeen']=0
543 attachpoints =[]
544
545 port, dpid = deviceV_to_attachpoint(vertex)
546 attachpoint = {}
547 attachpoint['port']=port
548 attachpoint['switchDPID']=dpid
549 attachpoints.append(attachpoint)
550 device['attachmentPoint']=attachpoints
551 devices.append(device)
552
553 print devices
554 js = json.dumps(devices)
555 resp = Response(js, status=200, mimetype='application/json')
556 return resp
557
558#{"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}
559
Ubuntu82b8a832013-02-06 22:00:11 +0000560## return fake stat for now
561@app.route("/wm/core/switch/<switchId>/<statType>/json")
562def switch_stat(switchId, statType):
563 if statType == "desc":
564 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
565 ret = {}
566 ret[switchId]=desc
567 elif statType == "aggregate":
568 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
569 ret = {}
570 ret[switchId]=aggr
571 else:
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700572 ret = {}
Ubuntu82b8a832013-02-06 22:00:11 +0000573
574 js = json.dumps(ret)
575 resp = Response(js, status=200, mimetype='application/json')
576 return resp
577
578
579@app.route("/wm/topology/links/json")
580def query_links():
581 try:
582 command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName)
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000583 print command
Ubuntu82b8a832013-02-06 22:00:11 +0000584 result = os.popen(command).read()
585 parsedResult = json.loads(result)['results']
586 except:
587 log_error("REST IF has issue: %s" % command)
588 log_error("%s" % result)
589 sys.exit(0)
590
591 debug("query_links %s" % command)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000592# pp.pprint(parsedResult)
Ubuntu82b8a832013-02-06 22:00:11 +0000593 sport = []
594 links = []
595 for v in parsedResult:
596 srcport = v['_id']
597 try:
598 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
599 print command
600 result = os.popen(command).read()
601 linkResults = json.loads(result)['results']
602 except:
603 log_error("REST IF has issue: %s" % command)
604 log_error("%s" % result)
605 sys.exit(0)
606
607 for p in linkResults:
608 if p.has_key('type') and p['type'] == "port":
609 dstport = p['_id']
610 (sport, sdpid) = portV_to_port_dpid(srcport)
611 (dport, ddpid) = portV_to_port_dpid(dstport)
612 link = {}
613 link["src-switch"]=sdpid
614 link["src-port"]=sport
615 link["src-port-state"]=0
616 link["dst-switch"]=ddpid
617 link["dst-port"]=dport
618 link["dst-port-state"]=0
619 link["type"]="internal"
620 links.append(link)
621
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000622# pp.pprint(links)
Ubuntu82b8a832013-02-06 22:00:11 +0000623 js = json.dumps(links)
624 resp = Response(js, status=200, mimetype='application/json')
625 return resp
626
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700627topo_less = {
628 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000629 {"name" : "00:a0", "group" : 1},
630 {"name" : "00:a1", "group" : 1},
631 {"name" : "00:a2", "group" : 1},
632 ],
633 "links" : [
634 {"source" :0, "target": 1},
635 {"source" :1, "target": 0},
636 {"source" :0, "target": 2},
637 {"source" :2, "target": 0},
638 {"source" :1, "target": 2},
639 {"source" :2, "target": 1},
640 ]
641}
642
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700643topo_more = {
644 "nodes" : [
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000645 {"name" : "00:a3", "group" : 2},
646 {"name" : "00:a0", "group" : 1},
647 {"name" : "00:a1", "group" : 1},
648 {"name" : "00:a2", "group" : 1},
649 ],
650 "links" : [
651 {"source" :1, "target": 2},
652 {"source" :2, "target": 1},
653 {"source" :1, "target": 3},
654 {"source" :3, "target": 1},
655 {"source" :2, "target": 3},
656 {"source" :3, "target": 2},
657 {"source" :0, "target": 2},
658 ]
659}
660
661@app.route("/topology_more")
662def topology_more():
663 topo = topo_more
664 js = json.dumps(topo)
665 resp = Response(js, status=200, mimetype='application/json')
666 return resp
667
668@app.route("/topology_less")
669def topology_less():
670 topo = topo_less
671 js = json.dumps(topo)
672 resp = Response(js, status=200, mimetype='application/json')
673 return resp
674
675cont_status1 = [
676 {"name":"onos9vpc", "onos": 1, "cassandra": 1},
677 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
678 {"name":"onos11vpc", "onos": 1, "cassandra": 0},
679 {"name":"onos12vpc", "onos": 1, "cassandra": 0}]
680
681cont_status2 = [
682 {"name":"onos9vpc", "onos": 0, "cassandra": 1},
683 {"name":"onos10vpc", "onos": 0, "cassandra": 1},
684 {"name":"onos11vpc", "onos": 0, "cassandra": 1},
685 {"name":"onos12vpc", "onos": 0, "cassandra": 1}]
686
687@app.route("/controller_status1")
688def controller_status1():
689 status = cont_status1
690 js = json.dumps(status)
691 resp = Response(js, status=200, mimetype='application/json')
692 pp.pprint(resp)
693 return resp
694
695@app.route("/controller_status2")
696def controller_status2():
697 status = cont_status2
698 js = json.dumps(status)
699 resp = Response(js, status=200, mimetype='application/json')
700 pp.pprint(resp)
701 return resp
702
Ubuntuc016ba12013-02-27 21:53:41 +0000703@app.route("/controller_status")
704def controller_status():
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700705 onos_check="sh ~/src/ONOS/start-onos.sh status | awk '{print $1}'"
Ubuntuc016ba12013-02-27 21:53:41 +0000706 #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status"
707
708 cont_status=[]
709 for i in controllers:
710 status={}
711 onos=os.popen(onos_check % i).read()[:-1]
712 status["name"]=i
713 status["onos"]=onos
Masayoshi Kobayashi5e91bdf2013-03-15 01:22:51 +0000714 status["cassandra"]=0
Ubuntuc016ba12013-02-27 21:53:41 +0000715 cont_status.append(status)
716
717 js = json.dumps(cont_status)
718 resp = Response(js, status=200, mimetype='application/json')
719 pp.pprint(js)
720 return resp
721
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000722@app.route("/gui/controller/<cmd>/<controller_name>")
723def controller_status_change(cmd, controller_name):
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700724 start_onos="~/src/ONOS/start-onos.sh start" % (controller_name)
725 stop_onos="~/src/ONOS/start-onos.sh stop" % (controller_name)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000726
727 if cmd == "up":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000728 print start_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000729 result=os.popen(start_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000730 ret = "controller %s is up" % (controller_name)
731 elif cmd == "down":
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000732 print stop_onos
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000733 result=os.popen(stop_onos).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000734 ret = "controller %s is down" % (controller_name)
735
736 return ret
737
738@app.route("/gui/switch/<cmd>/<dpid>")
739def switch_status_change(cmd, dpid):
740 r = re.compile(':')
741 dpid = re.sub(r, '', dpid)
Masayoshi Kobayashic0bc3192013-03-27 23:12:03 +0000742 host=controllers[0]
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700743 cmd_string="'cd ~/src/ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd)
744 get_status="'cd ~/src/ONOS/scripts; ./switch.sh %s'" % (host, dpid)
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000745 print "cmd_string"
746
747 if cmd =="up" or cmd=="down":
748 print "make dpid %s %s" % (dpid, cmd)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000749 os.popen(cmd_string)
750 result=os.popen(get_status).read()
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000751
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000752 return result
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000753
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000754#* Link Up
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000755#http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000756@app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
757def link_up(src_dpid, src_port, dst_dpid, dst_port):
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000758
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000759 cmd = 'up'
760 result=""
761
Paul Greyson472da4c2013-03-28 11:43:17 -0700762 for dpid in (src_dpid, dst_dpid):
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000763 if dpid in core_switches:
764 host = controllers[0]
765 src_ports = [1, 2, 3, 4, 5]
766 else:
Masayoshi Kobayashi05f12b32013-04-01 09:08:09 +0000767 hostid=int(dpid.split(':')[-2])
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000768 host = controllers[hostid-1]
769 if hostid == 2 :
770 src_ports = [51]
771 else :
772 src_ports = [26]
773
774 for port in src_ports :
775 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd)
776 print cmd_string
777 res=os.popen(cmd_string).read()
778 result = result + ' ' + res
779
780 return result
781
782
783#* Link Down
784#http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000785@app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>")
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000786def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port):
787
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000788 if src_dpid in core_switches:
789 host = controllers[0]
790 else:
791 hostid=int(src_dpid.split(':')[-2])
792 host = controllers[hostid-1]
793
794 cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd)
795 print cmd_string
796
Masayoshi Kobayashidecab442013-03-28 11:19:13 +0000797 result=os.popen(cmd_string).read()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000798
799 return result
800
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000801#* Create Flow
802#http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000803#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 +0000804@app.route("/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>")
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000805def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC):
806 command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1"
807 print command
808 ret = os.popen(command).read()
809 if ret == "":
810 flow_nr=0
811 else:
812 flow_nr=int(ret)
813
814 flow_nr += 1
Umesh Krishnaswamyf22d3ed2013-04-03 11:57:54 -0700815 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 +0000816 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 +0000817 print command
818 errcode = os.popen(command).read()
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000819 errcode1 = os.popen(command1).read()
820 return errcode+" "+errcode1
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000821
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000822#* Delete Flow
823#http://localhost:9000/gui/delflow/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000824@app.route("/gui/delflow/<flow_id>")
825def del_flow(flow_id):
826 command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id)
827 print command
828 errcode = os.popen(command).read()
829 return errcode
830
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000831#* Start Iperf Througput
Umesh Krishnaswamy6689be32013-03-27 18:12:26 -0700832#http://localhost:9000/gui/iperf/start/<flow_id>/<duration>
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000833@app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>")
834def iperf_start(flow_id,duration,samples):
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000835 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000836 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000837 print command
838 result = os.popen(command).read()
839 if len(result) == 0:
840 print "No Flow found"
841 return;
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000842 except:
843 print "REST IF has issue"
844 exit
845
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000846 parsedResult = json.loads(result)
847
848 flowId = int(parsedResult['flowId']['value'], 16)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000849 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
850 src_port = parsedResult['dataPath']['srcPort']['port']['value']
851 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
852 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000853# 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 +0000854
855 if src_dpid in core_switches:
856 host = controllers[0]
857 else:
858 hostid=int(src_dpid.split(':')[-2])
859 host = controllers[hostid-1]
860
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000861# ./runiperf.sh 2 00:00:00:00:00:00:02:02 1 00:00:00:00:00:00:03:02 1 100 15
862 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 +0000863 print cmd_string
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000864 os.popen(cmd_string)
Masayoshi Kobayashifd566312013-04-01 10:34:01 +0000865
Masayoshi Kobayashicdb652f2013-04-04 18:24:29 +0000866 return cmd_string
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000867
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000868#* Get Iperf Throughput
869#http://localhost:9000/gui/iperf/rate/<flow_id>
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000870@app.route("/gui/iperf/rate/<flow_id>")
871def iperf_rate(flow_id):
872 try:
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000873 command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id)
874 print command
875 result = os.popen(command).read()
876 if len(result) == 0:
877 print "No Flow found"
878 return;
879 except:
880 print "REST IF has issue"
881 exit
882
883 parsedResult = json.loads(result)
884
885 flowId = int(parsedResult['flowId']['value'], 16)
886 src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value']
887 src_port = parsedResult['dataPath']['srcPort']['port']['value']
888 dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value']
889 dst_port = parsedResult['dataPath']['dstPort']['port']['value']
890
891 if src_dpid in core_switches:
892 host = controllers[0]
893 else:
894 hostid=int(src_dpid.split(':')[-2])
895 host = controllers[hostid-1]
896
897 try:
898 command = "curl -s http://%s:%s/log/iperf_%s.out" % (host, 9000, flow_id)
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000899 print command
900 result = os.popen(command).read()
901 except:
902 print "REST IF has issue"
903 exit
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000904
Masayoshi Kobayashi911f2632013-04-01 18:44:33 +0000905 resp = Response(result, status=200, mimetype='application/json')
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000906 return resp
Masayoshi Kobayashi51011522013-03-27 00:18:12 +0000907
908
Ubuntu82b8a832013-02-06 22:00:11 +0000909if __name__ == "__main__":
910 if len(sys.argv) > 1 and sys.argv[1] == "-d":
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000911# 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")
912# link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1)
913# link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1)
914# link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1)
915# 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 +0000916# print "-- query all switches --"
917# query_switch()
918# print "-- query topo --"
919# topology_for_gui()
Masayoshi Kobayashi1e072382013-03-27 05:17:09 +0000920# link_change(1,2,3,4)
921 print "-- query all links --"
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000922# query_links()
Ubuntu82b8a832013-02-06 22:00:11 +0000923# print "-- query all devices --"
924# devices()
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +0000925 iperf_start(1,10,15)
926 iperf_rate(1)
Ubuntu82b8a832013-02-06 22:00:11 +0000927 else:
928 app.debug = True
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000929 app.run(threaded=True, host="0.0.0.0", port=9000)