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