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