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