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