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