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