Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | import pprint |
| 3 | import os |
| 4 | import sys |
| 5 | import subprocess |
| 6 | import json |
| 7 | import argparse |
| 8 | import io |
| 9 | import time |
| 10 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 11 | import re |
| 12 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 13 | from flask import Flask, json, Response, render_template, make_response, request |
| 14 | |
| 15 | ## Global Var ## |
Ubuntu | f6ce96c | 2013-02-07 01:45:07 +0000 | [diff] [blame] | 16 | RestIP="localhost" |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 17 | RestPort=8080 |
| 18 | #DBName="onos-network-map" |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 19 | #controllers=["onosgui1", "onosgui2", "onosgui3", "onosgui4"] |
| 20 | controllers=["onosgui1", "onosgui2", "onosgui3", "onosgui4", "onosgui5", "onosgui6", "onosgui7", "onosgui8"] |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 21 | 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"] |
| 22 | |
| 23 | nr_flow=0 |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 24 | |
| 25 | DEBUG=1 |
| 26 | pp = pprint.PrettyPrinter(indent=4) |
| 27 | |
| 28 | app = Flask(__name__) |
| 29 | |
| 30 | ## Worker Functions ## |
| 31 | def log_error(txt): |
| 32 | print '%s' % (txt) |
| 33 | |
| 34 | def debug(txt): |
| 35 | if DEBUG: |
| 36 | print '%s' % (txt) |
| 37 | |
| 38 | ## Rest APIs ## |
| 39 | ### File Fetch ### |
| 40 | @app.route('/ui/img/<filename>', methods=['GET']) |
| 41 | @app.route('/img/<filename>', methods=['GET']) |
| 42 | @app.route('/css/<filename>', methods=['GET']) |
| 43 | @app.route('/js/models/<filename>', methods=['GET']) |
| 44 | @app.route('/js/views/<filename>', methods=['GET']) |
| 45 | @app.route('/js/<filename>', methods=['GET']) |
| 46 | @app.route('/lib/<filename>', methods=['GET']) |
| 47 | @app.route('/', methods=['GET']) |
| 48 | @app.route('/<filename>', methods=['GET']) |
| 49 | @app.route('/tpl/<filename>', methods=['GET']) |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 50 | @app.route('/ons-demo/<filename>', methods=['GET']) |
| 51 | @app.route('/ons-demo/js/<filename>', methods=['GET']) |
| 52 | @app.route('/ons-demo/css/<filename>', methods=['GET']) |
| 53 | @app.route('/ons-demo/assets/<filename>', methods=['GET']) |
| 54 | @app.route('/ons-demo/data/<filename>', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 55 | def return_file(filename="index.html"): |
| 56 | if request.path == "/": |
| 57 | fullpath = "./index.html" |
| 58 | else: |
| 59 | fullpath = str(request.path)[1:] |
| 60 | |
| 61 | response = make_response(open(fullpath).read()) |
| 62 | suffix = fullpath.split(".")[-1] |
| 63 | |
| 64 | if suffix == "html" or suffix == "htm": |
| 65 | response.headers["Content-type"] = "text/html" |
| 66 | elif suffix == "js": |
| 67 | response.headers["Content-type"] = "application/javascript" |
| 68 | elif suffix == "css": |
| 69 | response.headers["Content-type"] = "text/css" |
| 70 | elif suffix == "png": |
| 71 | response.headers["Content-type"] = "image/png" |
| 72 | |
| 73 | return response |
| 74 | |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 75 | ## PROXY API (allows development where the webui is served from someplace other than the controller)## |
| 76 | ONOS_GUI3_HOST="http://gui3.onlab.us:8080" |
| 77 | ONOS_LOCAL_HOST="http://localhost:8080" ;# for Amazon EC2 |
| 78 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame^] | 79 | @app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>") |
| 80 | def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port): |
| 81 | try: |
| 82 | command = "curl -s %s/gui/link/%s/%s/%s/%s/%s" % (ONOS_GUI3_HOST, cmd, src_dpid, src_port, dst_dpid, dst_port) |
| 83 | print command |
| 84 | result = os.popen(command).read() |
| 85 | except: |
| 86 | print "REST IF has issue" |
| 87 | exit |
| 88 | |
| 89 | resp = Response(result, status=200, mimetype='application/json') |
| 90 | return resp |
| 91 | |
| 92 | |
| 93 | |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 94 | @app.route("/wm/core/topology/switches/all/json") |
| 95 | def switches(): |
| 96 | if request.args.get('proxy') == None: |
| 97 | host = ONOS_LOCAL_HOST |
| 98 | else: |
| 99 | host = ONOS_GUI3_HOST |
| 100 | |
| 101 | try: |
| 102 | command = "curl -s %s/wm/core/topology/switches/all/json" % (host) |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame^] | 103 | # print command |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 104 | result = os.popen(command).read() |
| 105 | except: |
| 106 | print "REST IF has issue" |
| 107 | exit |
| 108 | |
| 109 | resp = Response(result, status=200, mimetype='application/json') |
| 110 | return resp |
| 111 | |
| 112 | @app.route("/wm/core/topology/links/json") |
| 113 | def links(): |
| 114 | if request.args.get('proxy') == None: |
| 115 | host = ONOS_LOCAL_HOST |
| 116 | else: |
| 117 | host = ONOS_GUI3_HOST |
| 118 | |
| 119 | try: |
| 120 | command = "curl -s %s/wm/core/topology/links/json" % (host) |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame^] | 121 | # print command |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 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 | |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 130 | @app.route("/wm/flow/getsummary/0/0/json") |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 131 | def flows(): |
| 132 | if request.args.get('proxy') == None: |
| 133 | host = ONOS_LOCAL_HOST |
| 134 | else: |
| 135 | host = ONOS_GUI3_HOST |
| 136 | |
| 137 | try: |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 138 | command = "curl -s %s/wm/flow/getsummary/0/0/json" % (host) |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame^] | 139 | # print command |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 140 | result = os.popen(command).read() |
| 141 | except: |
| 142 | print "REST IF has issue" |
| 143 | exit |
| 144 | |
| 145 | resp = Response(result, status=200, mimetype='application/json') |
| 146 | return resp |
| 147 | |
| 148 | @app.route("/wm/registry/controllers/json") |
| 149 | def registry_controllers(): |
| 150 | if request.args.get('proxy') == None: |
| 151 | host = ONOS_LOCAL_HOST |
| 152 | else: |
| 153 | host = ONOS_GUI3_HOST |
| 154 | |
| 155 | try: |
| 156 | command = "curl -s %s/wm/registry/controllers/json" % (host) |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame^] | 157 | # print command |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 158 | result = os.popen(command).read() |
| 159 | except: |
| 160 | print "REST IF has issue" |
| 161 | exit |
| 162 | |
| 163 | resp = Response(result, status=200, mimetype='application/json') |
| 164 | return resp |
| 165 | |
| 166 | @app.route("/wm/registry/switches/json") |
| 167 | def registry_switches(): |
| 168 | if request.args.get('proxy') == None: |
| 169 | host = ONOS_LOCAL_HOST |
| 170 | else: |
| 171 | host = ONOS_GUI3_HOST |
| 172 | |
| 173 | try: |
| 174 | command = "curl -s %s/wm/registry/switches/json" % (host) |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame^] | 175 | # print command |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 176 | result = os.popen(command).read() |
| 177 | except: |
| 178 | print "REST IF has issue" |
| 179 | exit |
| 180 | |
| 181 | resp = Response(result, status=200, mimetype='application/json') |
| 182 | return resp |
| 183 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 184 | |
| 185 | def node_id(switch_array, dpid): |
| 186 | id = -1 |
| 187 | for i, val in enumerate(switch_array): |
| 188 | if val['name'] == dpid: |
| 189 | id = i |
| 190 | break |
| 191 | |
| 192 | return id |
| 193 | |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 194 | ## API for ON.Lab local GUI ## |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 195 | @app.route('/topology', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 196 | def topology_for_gui(): |
| 197 | try: |
| 198 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 199 | result = os.popen(command).read() |
| 200 | parsedResult = json.loads(result) |
| 201 | except: |
| 202 | log_error("REST IF has issue: %s" % command) |
| 203 | log_error("%s" % result) |
| 204 | sys.exit(0) |
| 205 | |
| 206 | topo = {} |
| 207 | switches = [] |
| 208 | links = [] |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 209 | devices = [] |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 210 | |
| 211 | for v in parsedResult: |
| 212 | if v.has_key('dpid'): |
| 213 | # if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 214 | dpid = str(v['dpid']) |
| 215 | state = str(v['state']) |
| 216 | sw = {} |
| 217 | sw['name']=dpid |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 218 | sw['group']= -1 |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 219 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 220 | if state == "INACTIVE": |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 221 | sw['group']=0 |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 222 | switches.append(sw) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 223 | |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 224 | ## Comment in if we need devies |
| 225 | # sw_index = len(switches) - 1 |
| 226 | # for p in v['ports']: |
| 227 | # for d in p['devices']: |
| 228 | # device = {} |
| 229 | # device['attached_switch']=dpid |
| 230 | # device['name']=d['mac'] |
| 231 | # if d['state'] == "ACTIVE": |
| 232 | # device['group']=1000 |
| 233 | # else: |
| 234 | # device['group']=1001 |
| 235 | # |
| 236 | # switches.append(device) |
| 237 | # device_index = len (switches) -1 |
| 238 | # link = {} |
| 239 | # link['source'] = device_index |
| 240 | # link['target'] = sw_index |
| 241 | # link['type'] = -1 |
| 242 | # links.append(link) |
| 243 | # link = {} |
| 244 | # link['source'] = sw_index |
| 245 | # link['target'] = device_index |
| 246 | # link['type'] = -1 |
| 247 | # links.append(link) |
| 248 | |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 249 | # try: |
| 250 | # command = "curl -s \'http://%s:%s/wm/registry/controllers/json\'" % (RestIP, RestPort) |
| 251 | # result = os.popen(command).read() |
| 252 | # controllers = json.loads(result) |
| 253 | # except: |
| 254 | # log_error("xx REST IF has issue: %s" % command) |
| 255 | # log_error("%s" % result) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 256 | |
| 257 | try: |
| 258 | command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort) |
| 259 | result = os.popen(command).read() |
| 260 | parsedResult = json.loads(result) |
| 261 | except: |
| 262 | log_error("REST IF has issue: %s" % command) |
| 263 | log_error("%s" % result) |
| 264 | |
| 265 | for key in parsedResult: |
| 266 | dpid = key |
| 267 | ctrl = parsedResult[dpid][0]['controllerId'] |
| 268 | sw_id = node_id(switches, dpid) |
| 269 | if sw_id != -1: |
| 270 | if switches[sw_id]['group'] != 0: |
| 271 | switches[sw_id]['group'] = controllers.index(ctrl) + 1 |
| 272 | |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 273 | try: |
| 274 | v1 = "00:00:00:00:00:0a:0d:00" |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 275 | # v1 = "00:00:00:00:00:0d:00:d1" |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 276 | p1=1 |
| 277 | v2 = "00:00:00:00:00:0b:0d:03" |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 278 | # v2 = "00:00:00:00:00:0d:00:d3" |
| 279 | p2=1 |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 280 | command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2) |
| 281 | result = os.popen(command).read() |
| 282 | parsedResult = json.loads(result) |
| 283 | except: |
| 284 | log_error("No route") |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 285 | parsedResult = {} |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 286 | |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 287 | path = [] |
| 288 | if parsedResult.has_key('flowEntries'): |
| 289 | flowEntries= parsedResult['flowEntries'] |
| 290 | for i, v in enumerate(flowEntries): |
| 291 | if i < len(flowEntries) - 1: |
| 292 | sdpid= flowEntries[i]['dpid']['value'] |
| 293 | ddpid = flowEntries[i+1]['dpid']['value'] |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame^] | 294 | path.append( (sdpid, ddpid)) |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 295 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 296 | try: |
| 297 | command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort) |
| 298 | result = os.popen(command).read() |
| 299 | parsedResult = json.loads(result) |
| 300 | except: |
| 301 | log_error("REST IF has issue: %s" % command) |
| 302 | log_error("%s" % result) |
| 303 | sys.exit(0) |
| 304 | |
| 305 | for v in parsedResult: |
| 306 | link = {} |
| 307 | if v.has_key('dst-switch'): |
| 308 | dst_dpid = str(v['dst-switch']) |
| 309 | dst_id = node_id(switches, dst_dpid) |
| 310 | if v.has_key('src-switch'): |
| 311 | src_dpid = str(v['src-switch']) |
| 312 | src_id = node_id(switches, src_dpid) |
| 313 | link['source'] = src_id |
| 314 | link['target'] = dst_id |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 315 | |
| 316 | onpath = 0 |
| 317 | for (s,d) in path: |
| 318 | if s == v['src-switch'] and d == v['dst-switch']: |
| 319 | onpath = 1 |
| 320 | break |
| 321 | link['type'] = onpath |
| 322 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 323 | links.append(link) |
| 324 | |
| 325 | topo['nodes'] = switches |
| 326 | topo['links'] = links |
| 327 | |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 328 | pp.pprint(topo) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 329 | js = json.dumps(topo) |
| 330 | resp = Response(js, status=200, mimetype='application/json') |
| 331 | return resp |
| 332 | |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 333 | #@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json") |
| 334 | #@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json") |
| 335 | @app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json") |
| 336 | def shortest_path(v1, p1, v2, p2): |
| 337 | try: |
| 338 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 339 | result = os.popen(command).read() |
| 340 | parsedResult = json.loads(result) |
| 341 | except: |
| 342 | log_error("REST IF has issue: %s" % command) |
| 343 | log_error("%s" % result) |
| 344 | sys.exit(0) |
| 345 | |
| 346 | topo = {} |
| 347 | switches = [] |
| 348 | links = [] |
| 349 | |
| 350 | for v in parsedResult: |
| 351 | if v.has_key('dpid'): |
| 352 | dpid = str(v['dpid']) |
| 353 | state = str(v['state']) |
| 354 | sw = {} |
| 355 | sw['name']=dpid |
| 356 | if str(v['state']) == "ACTIVE": |
| 357 | if dpid[-2:-1] == "a": |
| 358 | sw['group']=1 |
| 359 | if dpid[-2:-1] == "b": |
| 360 | sw['group']=2 |
| 361 | if dpid[-2:-1] == "c": |
| 362 | sw['group']=3 |
| 363 | if str(v['state']) == "INACTIVE": |
| 364 | sw['group']=0 |
| 365 | |
| 366 | switches.append(sw) |
| 367 | |
| 368 | try: |
| 369 | command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2) |
| 370 | result = os.popen(command).read() |
| 371 | parsedResult = json.loads(result) |
| 372 | except: |
| 373 | log_error("No route") |
| 374 | parsedResult = [] |
| 375 | # exit(1) |
| 376 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame^] | 377 | path = []; |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 378 | for i, v in enumerate(parsedResult): |
| 379 | if i < len(parsedResult) - 1: |
| 380 | sdpid= parsedResult[i]['switch'] |
| 381 | ddpid = parsedResult[i+1]['switch'] |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame^] | 382 | path.append( (sdpid, ddpid)) |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 383 | |
| 384 | try: |
| 385 | command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort) |
| 386 | result = os.popen(command).read() |
| 387 | parsedResult = json.loads(result) |
| 388 | except: |
| 389 | log_error("REST IF has issue: %s" % command) |
| 390 | log_error("%s" % result) |
| 391 | sys.exit(0) |
| 392 | |
| 393 | for v in parsedResult: |
| 394 | link = {} |
| 395 | if v.has_key('dst-switch'): |
| 396 | dst_dpid = str(v['dst-switch']) |
| 397 | dst_id = node_id(switches, dst_dpid) |
| 398 | if v.has_key('src-switch'): |
| 399 | src_dpid = str(v['src-switch']) |
| 400 | src_id = node_id(switches, src_dpid) |
| 401 | link['source'] = src_id |
| 402 | link['target'] = dst_id |
| 403 | onpath = 0 |
| 404 | for (s,d) in path: |
| 405 | if s == v['src-switch'] and d == v['dst-switch']: |
| 406 | onpath = 1 |
| 407 | break |
| 408 | |
| 409 | link['type'] = onpath |
| 410 | links.append(link) |
| 411 | |
| 412 | topo['nodes'] = switches |
| 413 | topo['links'] = links |
| 414 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 415 | # pp.pprint(topo) |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 416 | js = json.dumps(topo) |
| 417 | resp = Response(js, status=200, mimetype='application/json') |
| 418 | return resp |
| 419 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 420 | @app.route("/wm/core/controller/switches/json") |
| 421 | def query_switch(): |
| 422 | try: |
| 423 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 424 | # http://localhost:8080/wm/core/topology/switches/active/json |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 425 | print command |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 426 | result = os.popen(command).read() |
| 427 | parsedResult = json.loads(result) |
| 428 | except: |
| 429 | log_error("REST IF has issue: %s" % command) |
| 430 | log_error("%s" % result) |
| 431 | sys.exit(0) |
| 432 | |
| 433 | # print command |
| 434 | # print result |
| 435 | switches_ = [] |
| 436 | for v in parsedResult: |
| 437 | if v.has_key('dpid'): |
| 438 | if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 439 | dpid = str(v['dpid']) |
| 440 | state = str(v['state']) |
| 441 | sw = {} |
| 442 | sw['dpid']=dpid |
| 443 | sw['active']=state |
| 444 | switches_.append(sw) |
| 445 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 446 | # pp.pprint(switches_) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 447 | js = json.dumps(switches_) |
| 448 | resp = Response(js, status=200, mimetype='application/json') |
| 449 | return resp |
| 450 | |
| 451 | @app.route("/wm/device/") |
| 452 | def devices(): |
| 453 | try: |
| 454 | command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName) |
| 455 | result = os.popen(command).read() |
| 456 | parsedResult = json.loads(result)['results'] |
| 457 | except: |
| 458 | log_error("REST IF has issue: %s" % command) |
| 459 | log_error("%s" % result) |
| 460 | sys.exit(0) |
| 461 | |
| 462 | devices = [] |
| 463 | for v in parsedResult: |
| 464 | dl_addr = v['dl_addr'] |
| 465 | nw_addr = v['nw_addr'] |
| 466 | vertex = v['_id'] |
| 467 | mac = [] |
| 468 | mac.append(dl_addr) |
| 469 | ip = [] |
| 470 | ip.append(nw_addr) |
| 471 | device = {} |
| 472 | device['entryClass']="DefaultEntryClass" |
| 473 | device['mac']=mac |
| 474 | device['ipv4']=ip |
| 475 | device['vlan']=[] |
| 476 | device['lastSeen']=0 |
| 477 | attachpoints =[] |
| 478 | |
| 479 | port, dpid = deviceV_to_attachpoint(vertex) |
| 480 | attachpoint = {} |
| 481 | attachpoint['port']=port |
| 482 | attachpoint['switchDPID']=dpid |
| 483 | attachpoints.append(attachpoint) |
| 484 | device['attachmentPoint']=attachpoints |
| 485 | devices.append(device) |
| 486 | |
| 487 | print devices |
| 488 | js = json.dumps(devices) |
| 489 | resp = Response(js, status=200, mimetype='application/json') |
| 490 | return resp |
| 491 | |
| 492 | #{"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} |
| 493 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 494 | ## return fake stat for now |
| 495 | @app.route("/wm/core/switch/<switchId>/<statType>/json") |
| 496 | def switch_stat(switchId, statType): |
| 497 | if statType == "desc": |
| 498 | desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}] |
| 499 | ret = {} |
| 500 | ret[switchId]=desc |
| 501 | elif statType == "aggregate": |
| 502 | aggr = {"packetCount":0,"byteCount":0,"flowCount":0} |
| 503 | ret = {} |
| 504 | ret[switchId]=aggr |
| 505 | else: |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame^] | 506 | ret = {} |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 507 | |
| 508 | js = json.dumps(ret) |
| 509 | resp = Response(js, status=200, mimetype='application/json') |
| 510 | return resp |
| 511 | |
| 512 | |
| 513 | @app.route("/wm/topology/links/json") |
| 514 | def query_links(): |
| 515 | try: |
| 516 | command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName) |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 517 | print command |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 518 | result = os.popen(command).read() |
| 519 | parsedResult = json.loads(result)['results'] |
| 520 | except: |
| 521 | log_error("REST IF has issue: %s" % command) |
| 522 | log_error("%s" % result) |
| 523 | sys.exit(0) |
| 524 | |
| 525 | debug("query_links %s" % command) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 526 | # pp.pprint(parsedResult) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 527 | sport = [] |
| 528 | links = [] |
| 529 | for v in parsedResult: |
| 530 | srcport = v['_id'] |
| 531 | try: |
| 532 | command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport) |
| 533 | print command |
| 534 | result = os.popen(command).read() |
| 535 | linkResults = json.loads(result)['results'] |
| 536 | except: |
| 537 | log_error("REST IF has issue: %s" % command) |
| 538 | log_error("%s" % result) |
| 539 | sys.exit(0) |
| 540 | |
| 541 | for p in linkResults: |
| 542 | if p.has_key('type') and p['type'] == "port": |
| 543 | dstport = p['_id'] |
| 544 | (sport, sdpid) = portV_to_port_dpid(srcport) |
| 545 | (dport, ddpid) = portV_to_port_dpid(dstport) |
| 546 | link = {} |
| 547 | link["src-switch"]=sdpid |
| 548 | link["src-port"]=sport |
| 549 | link["src-port-state"]=0 |
| 550 | link["dst-switch"]=ddpid |
| 551 | link["dst-port"]=dport |
| 552 | link["dst-port-state"]=0 |
| 553 | link["type"]="internal" |
| 554 | links.append(link) |
| 555 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 556 | # pp.pprint(links) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 557 | js = json.dumps(links) |
| 558 | resp = Response(js, status=200, mimetype='application/json') |
| 559 | return resp |
| 560 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame^] | 561 | topo_less = { |
| 562 | "nodes" : [ |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 563 | {"name" : "00:a0", "group" : 1}, |
| 564 | {"name" : "00:a1", "group" : 1}, |
| 565 | {"name" : "00:a2", "group" : 1}, |
| 566 | ], |
| 567 | "links" : [ |
| 568 | {"source" :0, "target": 1}, |
| 569 | {"source" :1, "target": 0}, |
| 570 | {"source" :0, "target": 2}, |
| 571 | {"source" :2, "target": 0}, |
| 572 | {"source" :1, "target": 2}, |
| 573 | {"source" :2, "target": 1}, |
| 574 | ] |
| 575 | } |
| 576 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame^] | 577 | topo_more = { |
| 578 | "nodes" : [ |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 579 | {"name" : "00:a3", "group" : 2}, |
| 580 | {"name" : "00:a0", "group" : 1}, |
| 581 | {"name" : "00:a1", "group" : 1}, |
| 582 | {"name" : "00:a2", "group" : 1}, |
| 583 | ], |
| 584 | "links" : [ |
| 585 | {"source" :1, "target": 2}, |
| 586 | {"source" :2, "target": 1}, |
| 587 | {"source" :1, "target": 3}, |
| 588 | {"source" :3, "target": 1}, |
| 589 | {"source" :2, "target": 3}, |
| 590 | {"source" :3, "target": 2}, |
| 591 | {"source" :0, "target": 2}, |
| 592 | ] |
| 593 | } |
| 594 | |
| 595 | @app.route("/topology_more") |
| 596 | def topology_more(): |
| 597 | topo = topo_more |
| 598 | js = json.dumps(topo) |
| 599 | resp = Response(js, status=200, mimetype='application/json') |
| 600 | return resp |
| 601 | |
| 602 | @app.route("/topology_less") |
| 603 | def topology_less(): |
| 604 | topo = topo_less |
| 605 | js = json.dumps(topo) |
| 606 | resp = Response(js, status=200, mimetype='application/json') |
| 607 | return resp |
| 608 | |
| 609 | cont_status1 = [ |
| 610 | {"name":"onos9vpc", "onos": 1, "cassandra": 1}, |
| 611 | {"name":"onos10vpc", "onos": 0, "cassandra": 1}, |
| 612 | {"name":"onos11vpc", "onos": 1, "cassandra": 0}, |
| 613 | {"name":"onos12vpc", "onos": 1, "cassandra": 0}] |
| 614 | |
| 615 | cont_status2 = [ |
| 616 | {"name":"onos9vpc", "onos": 0, "cassandra": 1}, |
| 617 | {"name":"onos10vpc", "onos": 0, "cassandra": 1}, |
| 618 | {"name":"onos11vpc", "onos": 0, "cassandra": 1}, |
| 619 | {"name":"onos12vpc", "onos": 0, "cassandra": 1}] |
| 620 | |
| 621 | @app.route("/controller_status1") |
| 622 | def controller_status1(): |
| 623 | status = cont_status1 |
| 624 | js = json.dumps(status) |
| 625 | resp = Response(js, status=200, mimetype='application/json') |
| 626 | pp.pprint(resp) |
| 627 | return resp |
| 628 | |
| 629 | @app.route("/controller_status2") |
| 630 | def controller_status2(): |
| 631 | status = cont_status2 |
| 632 | js = json.dumps(status) |
| 633 | resp = Response(js, status=200, mimetype='application/json') |
| 634 | pp.pprint(resp) |
| 635 | return resp |
| 636 | |
Ubuntu | c016ba1 | 2013-02-27 21:53:41 +0000 | [diff] [blame] | 637 | @app.route("/controller_status") |
| 638 | def controller_status(): |
| 639 | onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'" |
| 640 | #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status" |
| 641 | |
| 642 | cont_status=[] |
| 643 | for i in controllers: |
| 644 | status={} |
| 645 | onos=os.popen(onos_check % i).read()[:-1] |
| 646 | status["name"]=i |
| 647 | status["onos"]=onos |
Masayoshi Kobayashi | 5e91bdf | 2013-03-15 01:22:51 +0000 | [diff] [blame] | 648 | status["cassandra"]=0 |
Ubuntu | c016ba1 | 2013-02-27 21:53:41 +0000 | [diff] [blame] | 649 | cont_status.append(status) |
| 650 | |
| 651 | js = json.dumps(cont_status) |
| 652 | resp = Response(js, status=200, mimetype='application/json') |
| 653 | pp.pprint(js) |
| 654 | return resp |
| 655 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 656 | @app.route("/gui/controller/<cmd>/<controller_name>") |
| 657 | def controller_status_change(cmd, controller_name): |
| 658 | start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name) |
| 659 | stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name) |
| 660 | |
| 661 | if cmd == "up": |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 662 | result=os.popen(start_onos).read() |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 663 | ret = "controller %s is up" % (controller_name) |
| 664 | elif cmd == "down": |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 665 | result=os.popen(stop_onos).read() |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 666 | ret = "controller %s is down" % (controller_name) |
| 667 | |
| 668 | return ret |
| 669 | |
| 670 | @app.route("/gui/switch/<cmd>/<dpid>") |
| 671 | def switch_status_change(cmd, dpid): |
| 672 | r = re.compile(':') |
| 673 | dpid = re.sub(r, '', dpid) |
| 674 | cmd_string="ssh -i ~/.ssh/onlabkey.pem onosgui1 'cd ONOS/scripts; ./switch.sh %s %s'" % (dpid, cmd) |
| 675 | get_status="ssh -i ~/.ssh/onlabkey.pem onosgui1 'cd ONOS/scripts; ./switch.sh %s'" % (dpid) |
| 676 | print "cmd_string" |
| 677 | |
| 678 | if cmd =="up" or cmd=="down": |
| 679 | print "make dpid %s %s" % (dpid, cmd) |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 680 | os.popen(cmd_string) |
| 681 | result=os.popen(get_status).read() |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 682 | |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 683 | return result |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 684 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 685 | #* Link Up/Down |
| 686 | #http://localhost:9000/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port> |
| 687 | #http://localhost:9000/gui/link/down/<src_dpid>/<src_port>/<dst_dpid>/<dst_port> |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 688 | |
| 689 | @app.route("/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>") |
| 690 | def link_change(cmd, src_dpid, src_port, dst_dpid, dst_port): |
| 691 | if src_dpid in core_switches: |
| 692 | host = controllers[0] |
| 693 | else: |
| 694 | hostid=int(src_dpid.split(':')[-2]) |
| 695 | host = controllers[hostid-1] |
| 696 | |
| 697 | cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd) |
| 698 | print cmd_string |
| 699 | |
| 700 | if cmd =="up" or cmd=="down": |
| 701 | result=os.popen(cmd_string).read() |
| 702 | |
| 703 | return result |
| 704 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 705 | #* Create Flow |
| 706 | #http://localhost:9000/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC> |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 707 | #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 |
| 708 | @app.route("/gui/addfow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>") |
| 709 | def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC): |
| 710 | command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1" |
| 711 | print command |
| 712 | ret = os.popen(command).read() |
| 713 | if ret == "": |
| 714 | flow_nr=0 |
| 715 | else: |
| 716 | flow_nr=int(ret) |
| 717 | |
| 718 | flow_nr += 1 |
| 719 | command = "/home/ubuntu/ONOS/web/add_flow.py %d %s %s %s %s %s matchSrcMac %s matchDstMac %s" % (flow_nr, "dummy", src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC) |
| 720 | print command |
| 721 | errcode = os.popen(command).read() |
| 722 | return errcode |
| 723 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 724 | #* Delete Flow |
| 725 | #http://localhost:9000/gui/delflow/<flow_id> |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 726 | @app.route("/gui/delflow/<flow_id>") |
| 727 | def del_flow(flow_id): |
| 728 | command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id) |
| 729 | print command |
| 730 | errcode = os.popen(command).read() |
| 731 | return errcode |
| 732 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 733 | #* Start Iperf Througput |
| 734 | #http://localhost:9000/gui/iperf/start/<flow_id> |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 735 | @app.route("/gui/iperf/start/<flow_id>") |
| 736 | def iperf_start(flow_id): |
| 737 | command = "iperf -xCMSV -t30 -i1 -u -c 127.0.0.1 > iperf_%s.out &" % (flow_id) |
| 738 | print command |
| 739 | errcode = os.popen(command).read() |
| 740 | return errcode |
| 741 | |
| 742 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 743 | #* Get Iperf Throughput |
| 744 | #http://localhost:9000/gui/iperf/rate/<flow_id> |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 745 | @app.route("/gui/iperf/rate/<flow_id>") |
| 746 | def iperf_rate(flow_id): |
| 747 | try: |
| 748 | command = "curl -s http://%s:%s/iperf_%s.out" % (RestIP, 9000, flow_id) |
| 749 | print command |
| 750 | result = os.popen(command).read() |
| 751 | except: |
| 752 | print "REST IF has issue" |
| 753 | exit |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 754 | |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 755 | resp = Response(result, status=200, mimetype='text/html') |
| 756 | return resp |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 757 | |
| 758 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 759 | if __name__ == "__main__": |
| 760 | if len(sys.argv) > 1 and sys.argv[1] == "-d": |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 761 | # 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") |
| 762 | # link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1) |
| 763 | # link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1) |
| 764 | # link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1) |
| 765 | # link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1) |
| 766 | |
| 767 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 768 | print "-- query all switches --" |
| 769 | query_switch() |
| 770 | print "-- query topo --" |
| 771 | topology_for_gui() |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 772 | # link_change(1,2,3,4) |
| 773 | print "-- query all links --" |
| 774 | query_links() |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 775 | # print "-- query all devices --" |
| 776 | # devices() |
| 777 | else: |
| 778 | app.debug = True |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 779 | app.run(threaded=True, host="0.0.0.0", port=9000) |