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 |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 10 | import random |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 11 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 12 | import re |
| 13 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 14 | from flask import Flask, json, Response, render_template, make_response, request |
| 15 | |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 16 | CONFIG_FILE=os.getenv("HOME") + "/ONOS/web/config.json" |
Masayoshi Kobayashi | 8158d44 | 2013-04-09 02:43:39 +0000 | [diff] [blame] | 17 | LINK_FILE=os.getenv("HOME") + "/ONOS/web/link.json" |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 18 | ONOSDIR=os.getenv("HOME") + "/ONOS" |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 19 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 20 | ## Global Var for ON.Lab local REST ## |
Ubuntu | f6ce96c | 2013-02-07 01:45:07 +0000 | [diff] [blame] | 21 | RestIP="localhost" |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 22 | RestPort=8080 |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 23 | ONOS_DEFAULT_HOST="localhost" ;# Has to set if LB=False |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 24 | DEBUG=1 |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 25 | |
Masayoshi Kobayashi | 8158d44 | 2013-04-09 02:43:39 +0000 | [diff] [blame] | 26 | pp = pprint.PrettyPrinter(indent=4) |
| 27 | app = Flask(__name__) |
| 28 | |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 29 | def read_config(): |
Jonathan Hart | b255448 | 2013-04-08 13:40:31 -0700 | [diff] [blame] | 30 | global LB, TESTBED, controllers, core_switches, ONOS_GUI3_HOST, ONOS_GUI3_CONTROL_HOST |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 31 | f = open(CONFIG_FILE) |
| 32 | conf = json.load(f) |
| 33 | LB = conf['LB'] |
Jonathan Hart | b255448 | 2013-04-08 13:40:31 -0700 | [diff] [blame] | 34 | TESTBED = conf['TESTBED'] |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 35 | controllers = conf['controllers'] |
Jonathan Hart | b255448 | 2013-04-08 13:40:31 -0700 | [diff] [blame] | 36 | core_switches=conf['core_switches'] |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 37 | ONOS_GUI3_HOST=conf['ONOS_GUI3_HOST'] |
| 38 | ONOS_GUI3_CONTROL_HOST=conf['ONOS_GUI3_CONTROL_HOST'] |
| 39 | f.close() |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 40 | |
Masayoshi Kobayashi | 8158d44 | 2013-04-09 02:43:39 +0000 | [diff] [blame] | 41 | def read_link_def(): |
| 42 | global link_def |
| 43 | f=open(LINK_FILE) |
| 44 | try: |
| 45 | link_def=json.load(f) |
| 46 | f.close() |
| 47 | except: |
| 48 | print "Can't read link def file (link.json)" |
| 49 | sys.exit(1) |
| 50 | |
| 51 | def get_link_ports(src_dpid, dst_dpid): |
| 52 | ret = (-1, -1) |
| 53 | for link in link_def: |
| 54 | if link['src-switch'] == src_dpid and link['dst-switch'] == dst_dpid: |
| 55 | ret = (link['src-port'], link['dst-port']) |
| 56 | break |
| 57 | return ret |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 58 | |
| 59 | ## Worker Functions ## |
| 60 | def log_error(txt): |
| 61 | print '%s' % (txt) |
| 62 | |
| 63 | def debug(txt): |
| 64 | if DEBUG: |
| 65 | print '%s' % (txt) |
| 66 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 67 | ### File Fetch ### |
| 68 | @app.route('/ui/img/<filename>', methods=['GET']) |
| 69 | @app.route('/img/<filename>', methods=['GET']) |
| 70 | @app.route('/css/<filename>', methods=['GET']) |
| 71 | @app.route('/js/models/<filename>', methods=['GET']) |
| 72 | @app.route('/js/views/<filename>', methods=['GET']) |
| 73 | @app.route('/js/<filename>', methods=['GET']) |
| 74 | @app.route('/lib/<filename>', methods=['GET']) |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 75 | @app.route('/log/<filename>', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 76 | @app.route('/', methods=['GET']) |
| 77 | @app.route('/<filename>', methods=['GET']) |
| 78 | @app.route('/tpl/<filename>', methods=['GET']) |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 79 | @app.route('/ons-demo/<filename>', methods=['GET']) |
| 80 | @app.route('/ons-demo/js/<filename>', methods=['GET']) |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 81 | @app.route('/ons-demo/d3/<filename>', methods=['GET']) |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 82 | @app.route('/ons-demo/css/<filename>', methods=['GET']) |
| 83 | @app.route('/ons-demo/assets/<filename>', methods=['GET']) |
| 84 | @app.route('/ons-demo/data/<filename>', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 85 | def return_file(filename="index.html"): |
| 86 | if request.path == "/": |
| 87 | fullpath = "./index.html" |
| 88 | else: |
| 89 | fullpath = str(request.path)[1:] |
| 90 | |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 91 | try: |
Masayoshi Kobayashi | 03e64b4 | 2013-04-05 05:56:27 +0000 | [diff] [blame] | 92 | open(fullpath) |
| 93 | except: |
| 94 | response = make_response("Cannot find a file: %s" % (fullpath), 500) |
| 95 | response.headers["Content-type"] = "text/html" |
| 96 | return response |
| 97 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 98 | response = make_response(open(fullpath).read()) |
| 99 | suffix = fullpath.split(".")[-1] |
| 100 | |
| 101 | if suffix == "html" or suffix == "htm": |
| 102 | response.headers["Content-type"] = "text/html" |
| 103 | elif suffix == "js": |
| 104 | response.headers["Content-type"] = "application/javascript" |
| 105 | elif suffix == "css": |
| 106 | response.headers["Content-type"] = "text/css" |
| 107 | elif suffix == "png": |
| 108 | response.headers["Content-type"] = "image/png" |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 109 | elif suffix == "svg": |
| 110 | response.headers["Content-type"] = "image/svg+xml" |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 111 | |
| 112 | return response |
| 113 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 114 | ## Proxy ## |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 115 | @app.route("/proxy/gui/link/<cmd>/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>") |
| 116 | def proxy_link_change(cmd, src_dpid, src_port, dst_dpid, dst_port): |
| 117 | try: |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 118 | 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] | 119 | print command |
| 120 | result = os.popen(command).read() |
| 121 | except: |
| 122 | print "REST IF has issue" |
| 123 | exit |
| 124 | |
| 125 | resp = Response(result, status=200, mimetype='application/json') |
| 126 | return resp |
| 127 | |
Masayoshi Kobayashi | 03e64b4 | 2013-04-05 05:56:27 +0000 | [diff] [blame] | 128 | @app.route("/proxy/gui/switchctrl/<cmd>") |
| 129 | def proxy_switch_controller_setting(cmd): |
| 130 | try: |
| 131 | command = "curl -s %s/gui/switchctrl/%s" % (ONOS_GUI3_CONTROL_HOST, cmd) |
| 132 | print command |
| 133 | result = os.popen(command).read() |
| 134 | except: |
| 135 | print "REST IF has issue" |
| 136 | exit |
| 137 | |
| 138 | resp = Response(result, status=200, mimetype='application/json') |
| 139 | return resp |
| 140 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 141 | @app.route("/proxy/gui/switch/<cmd>/<dpid>") |
| 142 | def proxy_switch_status_change(cmd, dpid): |
| 143 | try: |
| 144 | command = "curl -s %s/gui/switch/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, dpid) |
| 145 | print command |
| 146 | result = os.popen(command).read() |
| 147 | except: |
| 148 | print "REST IF has issue" |
| 149 | exit |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 150 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 151 | resp = Response(result, status=200, mimetype='application/json') |
| 152 | return resp |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 153 | |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 154 | @app.route("/proxy/gui/controller/<cmd>/<controller_name>") |
| 155 | def proxy_controller_status_change(cmd, controller_name): |
| 156 | try: |
| 157 | command = "curl -s %s/gui/controller/%s/%s" % (ONOS_GUI3_CONTROL_HOST, cmd, controller_name) |
| 158 | print command |
| 159 | result = os.popen(command).read() |
| 160 | except: |
| 161 | print "REST IF has issue" |
| 162 | exit |
| 163 | |
| 164 | resp = Response(result, status=200, mimetype='application/json') |
| 165 | return resp |
| 166 | |
Paul Greyson | 472da4c | 2013-03-28 11:43:17 -0700 | [diff] [blame] | 167 | @app.route("/proxy/gui/addflow/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>/<srcMAC>/<dstMAC>") |
| 168 | def proxy_add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC): |
| 169 | try: |
| 170 | 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) |
| 171 | print command |
| 172 | result = os.popen(command).read() |
| 173 | except: |
| 174 | print "REST IF has issue" |
| 175 | exit |
| 176 | |
| 177 | resp = Response(result, status=200, mimetype='application/json') |
| 178 | return resp |
| 179 | |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 180 | @app.route("/proxy/gui/delflow/<flow_id>") |
| 181 | def proxy_del_flow(flow_id): |
| 182 | try: |
| 183 | command = "curl -s %s/gui/delflow/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id) |
| 184 | print command |
| 185 | result = os.popen(command).read() |
| 186 | except: |
| 187 | print "REST IF has issue" |
| 188 | exit |
| 189 | |
| 190 | resp = Response(result, status=200, mimetype='application/json') |
| 191 | return resp |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 192 | |
Paul Greyson | 4b4c8af | 2013-04-04 09:02:09 -0700 | [diff] [blame] | 193 | @app.route("/proxy/gui/iperf/start/<flow_id>/<duration>/<samples>") |
| 194 | def proxy_iperf_start(flow_id,duration,samples): |
| 195 | try: |
Tim Lindberg | 6445a18 | 2013-04-15 10:17:18 -0700 | [diff] [blame] | 196 | command = "curl -m 40 -s %s/gui/iperf/start/%s/%s/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id, duration, samples) |
Paul Greyson | 4b4c8af | 2013-04-04 09:02:09 -0700 | [diff] [blame] | 197 | print command |
| 198 | result = os.popen(command).read() |
| 199 | except: |
| 200 | print "REST IF has issue" |
| 201 | exit |
| 202 | |
| 203 | resp = Response(result, status=200, mimetype='application/json') |
| 204 | return resp |
| 205 | |
| 206 | @app.route("/proxy/gui/iperf/rate/<flow_id>") |
| 207 | def proxy_iperf_rate(flow_id): |
| 208 | try: |
| 209 | command = "curl -s %s/gui/iperf/rate/%s" % (ONOS_GUI3_CONTROL_HOST, flow_id) |
| 210 | print command |
| 211 | result = os.popen(command).read() |
| 212 | except: |
| 213 | print "REST IF has issue" |
| 214 | exit |
| 215 | |
| 216 | resp = Response(result, status=200, mimetype='application/json') |
| 217 | return resp |
| 218 | |
Paul Greyson | e6266b9 | 2013-04-09 23:15:27 -0700 | [diff] [blame] | 219 | @app.route("/proxy/gui/switchctrl/<cmd>") |
| 220 | def proxy_switch_controller_setting(cmd): |
| 221 | try: |
| 222 | command = "curl -s %s/gui/switchctrl/%s" % (ONOS_GUI3_CONTROL_HOST, cmd) |
| 223 | print command |
| 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 | |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 232 | @app.route("/proxy/gui/reset") |
| 233 | def proxy_gui_reset(): |
| 234 | result = "" |
| 235 | try: |
| 236 | command = "curl -m 300 -s %s/gui/reset" % (ONOS_GUI3_CONTROL_HOST) |
| 237 | print command |
| 238 | result = os.popen(command).read() |
| 239 | except: |
| 240 | print "REST IF has issue" |
| 241 | exit |
Paul Greyson | 4b4c8af | 2013-04-04 09:02:09 -0700 | [diff] [blame] | 242 | |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 243 | resp = Response(result, status=200, mimetype='application/json') |
| 244 | return resp |
| 245 | |
| 246 | @app.route("/proxy/gui/scale") |
| 247 | def proxy_gui_scale(): |
| 248 | result = "" |
| 249 | try: |
| 250 | command = "curl -m 300 -s %s/gui/scale" % (ONOS_GUI3_CONTROL_HOST) |
| 251 | print command |
| 252 | result = os.popen(command).read() |
| 253 | except: |
| 254 | print "REST IF has issue" |
| 255 | exit |
| 256 | |
| 257 | resp = Response(result, status=200, mimetype='application/json') |
| 258 | return resp |
| 259 | |
| 260 | ###### ONOS REST API ############################## |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 261 | ## Worker Func ### |
| 262 | def get_json(url): |
| 263 | code = 200 |
| 264 | try: |
Tim Lindberg | 6445a18 | 2013-04-15 10:17:18 -0700 | [diff] [blame] | 265 | command = "curl -m 60 -s %s" % (url) |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 266 | result = os.popen(command).read() |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 267 | parsedResult = json.loads(result) |
Masayoshi Kobayashi | 8ac3336 | 2013-04-05 03:17:26 +0000 | [diff] [blame] | 268 | if type(parsedResult) == 'dict' and parsedResult.has_key('code'): |
| 269 | print "REST %s returned code %s" % (command, parsedResult['code']) |
| 270 | code=500 |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 271 | except: |
| 272 | print "REST IF %s has issue" % command |
| 273 | result = "" |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 274 | code = 500 |
| 275 | |
| 276 | return (code, result) |
| 277 | |
| 278 | def pick_host(): |
| 279 | if LB == True: |
| 280 | nr_host=len(controllers) |
| 281 | r=random.randint(0, nr_host - 1) |
| 282 | host=controllers[r] |
| 283 | else: |
| 284 | host=ONOS_DEFAULT_HOST |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 285 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 286 | return "http://" + host + ":8080" |
| 287 | |
| 288 | ## Switch ## |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 289 | @app.route("/wm/core/topology/switches/all/json") |
| 290 | def switches(): |
| 291 | if request.args.get('proxy') == None: |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 292 | host = pick_host() |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 293 | else: |
| 294 | host = ONOS_GUI3_HOST |
| 295 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 296 | url ="%s/wm/core/topology/switches/all/json" % (host) |
| 297 | (code, result) = get_json(url) |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 298 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 299 | resp = Response(result, status=code, mimetype='application/json') |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 300 | return resp |
| 301 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 302 | ## Link ## |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 303 | @app.route("/wm/core/topology/links/json") |
| 304 | def links(): |
| 305 | if request.args.get('proxy') == None: |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 306 | host = pick_host() |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 307 | else: |
| 308 | host = ONOS_GUI3_HOST |
| 309 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 310 | url ="%s/wm/core/topology/links/json" % (host) |
| 311 | (code, result) = get_json(url) |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 312 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 313 | resp = Response(result, status=code, mimetype='application/json') |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 314 | return resp |
| 315 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 316 | ## FlowSummary ## |
Masayoshi Kobayashi | cdb652f | 2013-04-04 18:24:29 +0000 | [diff] [blame] | 317 | @app.route("/wm/flow/getsummary/<start>/<range>/json") |
| 318 | def flows(start, range): |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 319 | if request.args.get('proxy') == None: |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 320 | host = pick_host() |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 321 | else: |
| 322 | host = ONOS_GUI3_HOST |
| 323 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 324 | url ="%s/wm/flow/getsummary/%s/%s/json" % (host, start, range) |
| 325 | (code, result) = get_json(url) |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 326 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 327 | resp = Response(result, status=code, mimetype='application/json') |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 328 | return resp |
| 329 | |
| 330 | @app.route("/wm/registry/controllers/json") |
| 331 | def registry_controllers(): |
| 332 | if request.args.get('proxy') == None: |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 333 | host = pick_host() |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 334 | else: |
| 335 | host = ONOS_GUI3_HOST |
| 336 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 337 | url= "%s/wm/registry/controllers/json" % (host) |
| 338 | (code, result) = get_json(url) |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 339 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 340 | resp = Response(result, status=code, mimetype='application/json') |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 341 | return resp |
| 342 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 343 | |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 344 | @app.route("/wm/registry/switches/json") |
| 345 | def registry_switches(): |
| 346 | if request.args.get('proxy') == None: |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 347 | host = pick_host() |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 348 | else: |
| 349 | host = ONOS_GUI3_HOST |
| 350 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 351 | url="%s/wm/registry/switches/json" % (host) |
| 352 | (code, result) = get_json(url) |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 353 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 354 | resp = Response(result, status=code, mimetype='application/json') |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 355 | return resp |
| 356 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 357 | def node_id(switch_array, dpid): |
| 358 | id = -1 |
| 359 | for i, val in enumerate(switch_array): |
| 360 | if val['name'] == dpid: |
| 361 | id = i |
| 362 | break |
| 363 | |
| 364 | return id |
| 365 | |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 366 | ## API for ON.Lab local GUI ## |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 367 | @app.route('/topology', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 368 | def topology_for_gui(): |
| 369 | try: |
| 370 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 371 | result = os.popen(command).read() |
| 372 | parsedResult = json.loads(result) |
| 373 | except: |
| 374 | log_error("REST IF has issue: %s" % command) |
| 375 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 376 | return |
| 377 | # sys.exit(0) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 378 | |
| 379 | topo = {} |
| 380 | switches = [] |
| 381 | links = [] |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 382 | devices = [] |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 383 | |
| 384 | for v in parsedResult: |
| 385 | if v.has_key('dpid'): |
| 386 | # if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 387 | dpid = str(v['dpid']) |
| 388 | state = str(v['state']) |
| 389 | sw = {} |
| 390 | sw['name']=dpid |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 391 | sw['group']= -1 |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 392 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 393 | if state == "INACTIVE": |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 394 | sw['group']=0 |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 395 | switches.append(sw) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 396 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 397 | try: |
| 398 | command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort) |
| 399 | result = os.popen(command).read() |
| 400 | parsedResult = json.loads(result) |
| 401 | except: |
| 402 | log_error("REST IF has issue: %s" % command) |
| 403 | log_error("%s" % result) |
| 404 | |
| 405 | for key in parsedResult: |
| 406 | dpid = key |
| 407 | ctrl = parsedResult[dpid][0]['controllerId'] |
| 408 | sw_id = node_id(switches, dpid) |
| 409 | if sw_id != -1: |
| 410 | if switches[sw_id]['group'] != 0: |
| 411 | switches[sw_id]['group'] = controllers.index(ctrl) + 1 |
| 412 | |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 413 | try: |
| 414 | v1 = "00:00:00:00:00:0a:0d:00" |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 415 | # v1 = "00:00:00:00:00:0d:00:d1" |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 416 | p1=1 |
| 417 | v2 = "00:00:00:00:00:0b:0d:03" |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 418 | # v2 = "00:00:00:00:00:0d:00:d3" |
| 419 | p2=1 |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 420 | command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2) |
| 421 | result = os.popen(command).read() |
| 422 | parsedResult = json.loads(result) |
| 423 | except: |
| 424 | log_error("No route") |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 425 | parsedResult = {} |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 426 | |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 427 | path = [] |
| 428 | if parsedResult.has_key('flowEntries'): |
| 429 | flowEntries= parsedResult['flowEntries'] |
| 430 | for i, v in enumerate(flowEntries): |
| 431 | if i < len(flowEntries) - 1: |
| 432 | sdpid= flowEntries[i]['dpid']['value'] |
| 433 | ddpid = flowEntries[i+1]['dpid']['value'] |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 434 | path.append( (sdpid, ddpid)) |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 435 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 436 | try: |
| 437 | command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort) |
| 438 | result = os.popen(command).read() |
| 439 | parsedResult = json.loads(result) |
| 440 | except: |
| 441 | log_error("REST IF has issue: %s" % command) |
| 442 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 443 | return |
| 444 | # sys.exit(0) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 445 | |
| 446 | for v in parsedResult: |
| 447 | link = {} |
| 448 | if v.has_key('dst-switch'): |
| 449 | dst_dpid = str(v['dst-switch']) |
| 450 | dst_id = node_id(switches, dst_dpid) |
| 451 | if v.has_key('src-switch'): |
| 452 | src_dpid = str(v['src-switch']) |
| 453 | src_id = node_id(switches, src_dpid) |
| 454 | link['source'] = src_id |
| 455 | link['target'] = dst_id |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 456 | |
| 457 | onpath = 0 |
| 458 | for (s,d) in path: |
| 459 | if s == v['src-switch'] and d == v['dst-switch']: |
| 460 | onpath = 1 |
| 461 | break |
| 462 | link['type'] = onpath |
| 463 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 464 | links.append(link) |
| 465 | |
| 466 | topo['nodes'] = switches |
| 467 | topo['links'] = links |
| 468 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 469 | js = json.dumps(topo) |
| 470 | resp = Response(js, status=200, mimetype='application/json') |
| 471 | return resp |
| 472 | |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 473 | #@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json") |
| 474 | #@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json") |
| 475 | @app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json") |
| 476 | def shortest_path(v1, p1, v2, p2): |
| 477 | try: |
| 478 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 479 | result = os.popen(command).read() |
| 480 | parsedResult = json.loads(result) |
| 481 | except: |
| 482 | log_error("REST IF has issue: %s" % command) |
| 483 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 484 | return |
| 485 | # sys.exit(0) |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 486 | |
| 487 | topo = {} |
| 488 | switches = [] |
| 489 | links = [] |
| 490 | |
| 491 | for v in parsedResult: |
| 492 | if v.has_key('dpid'): |
| 493 | dpid = str(v['dpid']) |
| 494 | state = str(v['state']) |
| 495 | sw = {} |
| 496 | sw['name']=dpid |
| 497 | if str(v['state']) == "ACTIVE": |
| 498 | if dpid[-2:-1] == "a": |
| 499 | sw['group']=1 |
| 500 | if dpid[-2:-1] == "b": |
| 501 | sw['group']=2 |
| 502 | if dpid[-2:-1] == "c": |
| 503 | sw['group']=3 |
| 504 | if str(v['state']) == "INACTIVE": |
| 505 | sw['group']=0 |
| 506 | |
| 507 | switches.append(sw) |
| 508 | |
| 509 | try: |
| 510 | command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2) |
| 511 | result = os.popen(command).read() |
| 512 | parsedResult = json.loads(result) |
| 513 | except: |
| 514 | log_error("No route") |
| 515 | parsedResult = [] |
| 516 | # exit(1) |
| 517 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 518 | path = []; |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 519 | for i, v in enumerate(parsedResult): |
| 520 | if i < len(parsedResult) - 1: |
| 521 | sdpid= parsedResult[i]['switch'] |
| 522 | ddpid = parsedResult[i+1]['switch'] |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 523 | path.append( (sdpid, ddpid)) |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 524 | |
| 525 | try: |
| 526 | command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort) |
| 527 | result = os.popen(command).read() |
| 528 | parsedResult = json.loads(result) |
| 529 | except: |
| 530 | log_error("REST IF has issue: %s" % command) |
| 531 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 532 | return |
| 533 | # sys.exit(0) |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 534 | |
| 535 | for v in parsedResult: |
| 536 | link = {} |
| 537 | if v.has_key('dst-switch'): |
| 538 | dst_dpid = str(v['dst-switch']) |
| 539 | dst_id = node_id(switches, dst_dpid) |
| 540 | if v.has_key('src-switch'): |
| 541 | src_dpid = str(v['src-switch']) |
| 542 | src_id = node_id(switches, src_dpid) |
| 543 | link['source'] = src_id |
| 544 | link['target'] = dst_id |
| 545 | onpath = 0 |
| 546 | for (s,d) in path: |
| 547 | if s == v['src-switch'] and d == v['dst-switch']: |
| 548 | onpath = 1 |
| 549 | break |
| 550 | |
| 551 | link['type'] = onpath |
| 552 | links.append(link) |
| 553 | |
| 554 | topo['nodes'] = switches |
| 555 | topo['links'] = links |
| 556 | |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 557 | js = json.dumps(topo) |
| 558 | resp = Response(js, status=200, mimetype='application/json') |
| 559 | return resp |
| 560 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 561 | @app.route("/wm/core/controller/switches/json") |
| 562 | def query_switch(): |
| 563 | try: |
| 564 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 565 | # http://localhost:8080/wm/core/topology/switches/active/json |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 566 | print command |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 567 | result = os.popen(command).read() |
| 568 | parsedResult = json.loads(result) |
| 569 | except: |
| 570 | log_error("REST IF has issue: %s" % command) |
| 571 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 572 | return |
| 573 | # sys.exit(0) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 574 | |
| 575 | # print command |
| 576 | # print result |
| 577 | switches_ = [] |
| 578 | for v in parsedResult: |
| 579 | if v.has_key('dpid'): |
| 580 | if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 581 | dpid = str(v['dpid']) |
| 582 | state = str(v['state']) |
| 583 | sw = {} |
| 584 | sw['dpid']=dpid |
| 585 | sw['active']=state |
| 586 | switches_.append(sw) |
| 587 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 588 | # pp.pprint(switches_) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 589 | js = json.dumps(switches_) |
| 590 | resp = Response(js, status=200, mimetype='application/json') |
| 591 | return resp |
| 592 | |
| 593 | @app.route("/wm/device/") |
| 594 | def devices(): |
| 595 | try: |
| 596 | command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName) |
| 597 | result = os.popen(command).read() |
| 598 | parsedResult = json.loads(result)['results'] |
| 599 | except: |
| 600 | log_error("REST IF has issue: %s" % command) |
| 601 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 602 | return |
| 603 | # sys.exit(0) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 604 | |
| 605 | devices = [] |
| 606 | for v in parsedResult: |
| 607 | dl_addr = v['dl_addr'] |
| 608 | nw_addr = v['nw_addr'] |
| 609 | vertex = v['_id'] |
| 610 | mac = [] |
| 611 | mac.append(dl_addr) |
| 612 | ip = [] |
| 613 | ip.append(nw_addr) |
| 614 | device = {} |
| 615 | device['entryClass']="DefaultEntryClass" |
| 616 | device['mac']=mac |
| 617 | device['ipv4']=ip |
| 618 | device['vlan']=[] |
| 619 | device['lastSeen']=0 |
| 620 | attachpoints =[] |
| 621 | |
| 622 | port, dpid = deviceV_to_attachpoint(vertex) |
| 623 | attachpoint = {} |
| 624 | attachpoint['port']=port |
| 625 | attachpoint['switchDPID']=dpid |
| 626 | attachpoints.append(attachpoint) |
| 627 | device['attachmentPoint']=attachpoints |
| 628 | devices.append(device) |
| 629 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 630 | js = json.dumps(devices) |
| 631 | resp = Response(js, status=200, mimetype='application/json') |
| 632 | return resp |
| 633 | |
| 634 | #{"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} |
| 635 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 636 | ## return fake stat for now |
| 637 | @app.route("/wm/core/switch/<switchId>/<statType>/json") |
| 638 | def switch_stat(switchId, statType): |
| 639 | if statType == "desc": |
| 640 | desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}] |
| 641 | ret = {} |
| 642 | ret[switchId]=desc |
| 643 | elif statType == "aggregate": |
| 644 | aggr = {"packetCount":0,"byteCount":0,"flowCount":0} |
| 645 | ret = {} |
| 646 | ret[switchId]=aggr |
| 647 | else: |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 648 | ret = {} |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 649 | |
| 650 | js = json.dumps(ret) |
| 651 | resp = Response(js, status=200, mimetype='application/json') |
| 652 | return resp |
| 653 | |
| 654 | |
| 655 | @app.route("/wm/topology/links/json") |
| 656 | def query_links(): |
| 657 | try: |
| 658 | 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] | 659 | print command |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 660 | result = os.popen(command).read() |
| 661 | parsedResult = json.loads(result)['results'] |
| 662 | except: |
| 663 | log_error("REST IF has issue: %s" % command) |
| 664 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 665 | return |
| 666 | # sys.exit(0) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 667 | |
| 668 | debug("query_links %s" % command) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 669 | # pp.pprint(parsedResult) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 670 | sport = [] |
| 671 | links = [] |
| 672 | for v in parsedResult: |
| 673 | srcport = v['_id'] |
| 674 | try: |
| 675 | command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport) |
| 676 | print command |
| 677 | result = os.popen(command).read() |
| 678 | linkResults = json.loads(result)['results'] |
| 679 | except: |
| 680 | log_error("REST IF has issue: %s" % command) |
| 681 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 682 | return |
| 683 | # sys.exit(0) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 684 | |
| 685 | for p in linkResults: |
| 686 | if p.has_key('type') and p['type'] == "port": |
| 687 | dstport = p['_id'] |
| 688 | (sport, sdpid) = portV_to_port_dpid(srcport) |
| 689 | (dport, ddpid) = portV_to_port_dpid(dstport) |
| 690 | link = {} |
| 691 | link["src-switch"]=sdpid |
| 692 | link["src-port"]=sport |
| 693 | link["src-port-state"]=0 |
| 694 | link["dst-switch"]=ddpid |
| 695 | link["dst-port"]=dport |
| 696 | link["dst-port-state"]=0 |
| 697 | link["type"]="internal" |
| 698 | links.append(link) |
| 699 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 700 | # pp.pprint(links) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 701 | js = json.dumps(links) |
| 702 | resp = Response(js, status=200, mimetype='application/json') |
| 703 | return resp |
| 704 | |
Ubuntu | c016ba1 | 2013-02-27 21:53:41 +0000 | [diff] [blame] | 705 | @app.route("/controller_status") |
| 706 | def controller_status(): |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 707 | # onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'" |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 708 | onos_check="cd; onos status | grep %s | awk '{print $2}'" |
Ubuntu | c016ba1 | 2013-02-27 21:53:41 +0000 | [diff] [blame] | 709 | #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status" |
| 710 | |
| 711 | cont_status=[] |
| 712 | for i in controllers: |
| 713 | status={} |
| 714 | onos=os.popen(onos_check % i).read()[:-1] |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 715 | # onos=os.popen(onos_check % (i, i.lower())).read()[:-1] |
Ubuntu | c016ba1 | 2013-02-27 21:53:41 +0000 | [diff] [blame] | 716 | status["name"]=i |
| 717 | status["onos"]=onos |
Masayoshi Kobayashi | 5e91bdf | 2013-03-15 01:22:51 +0000 | [diff] [blame] | 718 | status["cassandra"]=0 |
Ubuntu | c016ba1 | 2013-02-27 21:53:41 +0000 | [diff] [blame] | 719 | cont_status.append(status) |
| 720 | |
| 721 | js = json.dumps(cont_status) |
| 722 | resp = Response(js, status=200, mimetype='application/json') |
Ubuntu | c016ba1 | 2013-02-27 21:53:41 +0000 | [diff] [blame] | 723 | return resp |
| 724 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 725 | ### Command ### |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 726 | @app.route("/gui/controller/<cmd>/<controller_name>") |
| 727 | def controller_status_change(cmd, controller_name): |
Masayoshi Kobayashi | df787a4 | 2013-04-09 01:18:48 +0000 | [diff] [blame] | 728 | if (TESTBED == "hw"): |
Tim Lindberg | 34c9ff6 | 2013-04-10 15:06:17 -0700 | [diff] [blame] | 729 | start_onos="/home/admin/bin/onos start %s" % (controller_name[-1:]) |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 730 | # start_onos="/home/admin/bin/onos start %s > /tmp/debug " % (controller_name[-1:]) |
Tim Lindberg | 34c9ff6 | 2013-04-10 15:06:17 -0700 | [diff] [blame] | 731 | stop_onos="/home/admin/bin/onos stop %s" % (controller_name[-1:]) |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 732 | # stop_onos="/home/admin/bin/onos stop %s > /tmp/debug " % (controller_name[-1:]) |
| 733 | # print "Debug: Controller command %s called %s" % (cmd, controller_name) |
Masayoshi Kobayashi | df787a4 | 2013-04-09 01:18:48 +0000 | [diff] [blame] | 734 | else: |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 735 | # No longer use -i to specify keys (use .ssh/config to specify it) |
| 736 | start_onos="ssh %s ONOS/start-onos.sh start" % (controller_name) |
| 737 | stop_onos="ssh %s ONOS/start-onos.sh stop" % (controller_name) |
| 738 | # start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name) |
| 739 | # stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name) |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 740 | |
| 741 | if cmd == "up": |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 742 | result=os.popen(start_onos).read() |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 743 | ret = "controller %s is up: %s" % (controller_name, result) |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 744 | elif cmd == "down": |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 745 | result=os.popen(stop_onos).read() |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 746 | ret = "controller %s is down: %s" % (controller_name, result) |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 747 | |
| 748 | return ret |
| 749 | |
Masayoshi Kobayashi | 03e64b4 | 2013-04-05 05:56:27 +0000 | [diff] [blame] | 750 | @app.route("/gui/switchctrl/<cmd>") |
| 751 | def switch_controller_setting(cmd): |
| 752 | if cmd =="local": |
| 753 | print "All aggr switches connects to local controller only" |
| 754 | result="" |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 755 | if (TESTBED == "sw"): |
Jonathan Hart | 4dbaa50 | 2013-04-13 20:21:29 -0700 | [diff] [blame] | 756 | for i in range(1, len(controllers)): |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 757 | cmd_string="ssh %s 'cd ONOS/scripts; ./ctrl-local.sh'" % (controllers[i]) |
| 758 | result += os.popen(cmd_string).read() |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 759 | else: |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 760 | cmd_string="cd; switch local > /tmp/watch" |
Masayoshi Kobayashi | 03e64b4 | 2013-04-05 05:56:27 +0000 | [diff] [blame] | 761 | result += os.popen(cmd_string).read() |
| 762 | elif cmd =="all": |
| 763 | print "All aggr switches connects to all controllers except for core controller" |
| 764 | result="" |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 765 | if (TESTBED == "sw"): |
Jonathan Hart | 4dbaa50 | 2013-04-13 20:21:29 -0700 | [diff] [blame] | 766 | for i in range(1, len(controllers)): |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 767 | cmd_string="ssh %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i]) |
| 768 | # cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./ctrl-add-ext.sh'" % (controllers[i]) |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 769 | print "cmd is: "+cmd_string |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 770 | result += os.popen(cmd_string).read() |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 771 | else: |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 772 | cmd_string="/home/admin/bin/switch all > /tmp/watch" |
Masayoshi Kobayashi | 03e64b4 | 2013-04-05 05:56:27 +0000 | [diff] [blame] | 773 | result += os.popen(cmd_string).read() |
| 774 | |
| 775 | return result |
| 776 | |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 777 | @app.route("/gui/reset") |
| 778 | def reset_demo(): |
Masayoshi Kobayashi | 1ef9ec0 | 2013-04-12 18:03:12 +0000 | [diff] [blame] | 779 | if (TESTBED == "hw"): |
| 780 | cmd_string="cd ~/bin; ./demo-reset-hw.sh > /tmp/watch &" |
| 781 | else: |
| 782 | cmd_string="cd ~/ONOS/scripts; ./demo-reset-sw.sh > /tmp/watch &" |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 783 | os.popen(cmd_string) |
| 784 | return "Reset" |
| 785 | |
| 786 | @app.route("/gui/scale") |
| 787 | def scale_demo(): |
Masayoshi Kobayashi | 1ef9ec0 | 2013-04-12 18:03:12 +0000 | [diff] [blame] | 788 | if (TESTBED == "hw"): |
| 789 | cmd_string="cd ~/bin; ~/bin/demo-scale-out-hw.sh > /tmp/watch &" |
| 790 | else: |
| 791 | cmd_string="cd ~/ONOS/scripts; ./demo-scale-out-sw.sh > /tmp/watch &" |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 792 | os.popen(cmd_string) |
| 793 | return "scale" |
Masayoshi Kobayashi | 03e64b4 | 2013-04-05 05:56:27 +0000 | [diff] [blame] | 794 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 795 | @app.route("/gui/switch/<cmd>/<dpid>") |
| 796 | def switch_status_change(cmd, dpid): |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 797 | result = "" |
| 798 | if (TESTBED == "hw"): |
| 799 | return result |
| 800 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 801 | r = re.compile(':') |
| 802 | dpid = re.sub(r, '', dpid) |
Masayoshi Kobayashi | c0bc319 | 2013-03-27 23:12:03 +0000 | [diff] [blame] | 803 | host=controllers[0] |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 804 | cmd_string="ssh %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd) |
| 805 | # cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd) |
Masayoshi Kobayashi | c0bc319 | 2013-03-27 23:12:03 +0000 | [diff] [blame] | 806 | 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] | 807 | print "cmd_string" |
| 808 | |
| 809 | if cmd =="up" or cmd=="down": |
| 810 | print "make dpid %s %s" % (dpid, cmd) |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 811 | os.popen(cmd_string) |
| 812 | result=os.popen(get_status).read() |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 813 | |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 814 | return result |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 815 | |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 816 | #* Link Up |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 817 | #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] | 818 | @app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>") |
| 819 | def link_up(src_dpid, src_port, dst_dpid, dst_port): |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 820 | result = "" |
| 821 | |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 822 | if (TESTBED == "sw"): |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 823 | result = link_up_sw(src_dpid, src_port, dst_dpid, dst_port) |
| 824 | else: |
| 825 | result = link_up_hw(src_dpid, src_port, dst_dpid, dst_port) |
| 826 | return result |
| 827 | |
| 828 | # Link up on software testbed |
| 829 | def link_up_sw(src_dpid, src_port, dst_dpid, dst_port): |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 830 | |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 831 | cmd = 'up' |
| 832 | result="" |
Paul Greyson | 472da4c | 2013-03-28 11:43:17 -0700 | [diff] [blame] | 833 | for dpid in (src_dpid, dst_dpid): |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 834 | if dpid in core_switches: |
| 835 | host = controllers[0] |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 836 | else: |
Masayoshi Kobayashi | 05f12b3 | 2013-04-01 09:08:09 +0000 | [diff] [blame] | 837 | hostid=int(dpid.split(':')[-2]) |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 838 | host = controllers[hostid-1] |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 839 | |
Masayoshi Kobayashi | 8158d44 | 2013-04-09 02:43:39 +0000 | [diff] [blame] | 840 | if dpid == src_dpid: |
| 841 | (port, dontcare) = get_link_ports(dpid, dst_dpid) |
| 842 | else: |
| 843 | (port, dontcare) = get_link_ports(dpid, src_dpid) |
| 844 | |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 845 | # cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd) |
| 846 | cmd_string="ssh %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd) |
Masayoshi Kobayashi | 8158d44 | 2013-04-09 02:43:39 +0000 | [diff] [blame] | 847 | print cmd_string |
| 848 | res=os.popen(cmd_string).read() |
| 849 | result = result + ' ' + res |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 850 | |
| 851 | return result |
| 852 | |
Masayoshi Kobayashi | 8158d44 | 2013-04-09 02:43:39 +0000 | [diff] [blame] | 853 | # if hostid == 2 : |
| 854 | # src_ports = [51] |
| 855 | # else : |
| 856 | # src_ports = [26] |
| 857 | # |
| 858 | # for port in src_ports : |
| 859 | # cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd) |
| 860 | # print cmd_string |
| 861 | # res=os.popen(cmd_string).read() |
| 862 | |
| 863 | |
| 864 | |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 865 | # Link up on hardware testbed |
| 866 | def link_up_hw(src_dpid, src_port, dst_dpid, dst_port): |
| 867 | |
| 868 | port1 = src_port |
| 869 | port2 = dst_port |
| 870 | if src_dpid == "00:00:00:00:ba:5e:ba:11": |
| 871 | if dst_dpid == "00:00:00:08:a2:08:f9:01": |
| 872 | port1 = 24 |
| 873 | port2 = 24 |
| 874 | elif dst_dpid == "00:01:00:16:97:08:9a:46": |
| 875 | port1 = 23 |
| 876 | port2 = 23 |
| 877 | elif src_dpid == "00:00:00:00:ba:5e:ba:13": |
| 878 | if dst_dpid == "00:00:20:4e:7f:51:8a:35": |
| 879 | port1 = 22 |
| 880 | port2 = 22 |
| 881 | elif dst_dpid == "00:00:00:00:00:00:ba:12": |
| 882 | port1 = 23 |
| 883 | port2 = 23 |
| 884 | elif src_dpid == "00:00:00:00:00:00:ba:12": |
| 885 | if dst_dpid == "00:00:00:00:ba:5e:ba:13": |
| 886 | port1 = 23 |
| 887 | port2 = 23 |
| 888 | elif dst_dpid == "00:00:00:08:a2:08:f9:01": |
| 889 | port1 = 22 |
| 890 | port2 = 22 |
| 891 | elif dst_dpid == "00:00:20:4e:7f:51:8a:35": |
| 892 | port1 = 24 |
| 893 | port2 = 21 |
| 894 | elif src_dpid == "00:01:00:16:97:08:9a:46": |
| 895 | if dst_dpid == "00:00:00:00:ba:5e:ba:11": |
| 896 | port1 = 23 |
| 897 | port2 = 23 |
| 898 | elif dst_dpid == "00:00:20:4e:7f:51:8a:35": |
| 899 | port1 = 24 |
| 900 | port2 = 24 |
| 901 | elif src_dpid == "00:00:00:08:a2:08:f9:01": |
| 902 | if dst_dpid == "00:00:00:00:ba:5e:ba:11": |
| 903 | port1 = 24 |
| 904 | port2 = 24 |
| 905 | elif dst_dpid == "00:00:00:00:00:00:ba:12": |
| 906 | port1 = 22 |
| 907 | port2 = 22 |
| 908 | elif dst_dpid == "00:00:20:4e:7f:51:8a:35": |
| 909 | port1 = 23 |
| 910 | port2 = 23 |
| 911 | elif src_dpid == "00:00:20:4e:7f:51:8a:35": |
| 912 | if dst_dpid == "00:00:00:00:00:00:ba:12": |
| 913 | port1 = 21 |
| 914 | port2 = 24 |
| 915 | elif dst_dpid == "00:00:00:00:ba:5e:ba:13": |
| 916 | port1 = 22 |
| 917 | port2 = 22 |
| 918 | elif dst_dpid == "00:01:00:16:97:08:9a:46": |
| 919 | port1 = 24 |
| 920 | port2 = 24 |
| 921 | elif dst_dpid == "00:00:00:08:a2:08:f9:01": |
| 922 | port1 = 23 |
| 923 | port2 = 23 |
| 924 | |
| 925 | cmd = 'up' |
| 926 | result="" |
| 927 | host = controllers[0] |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 928 | cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % (src_dpid, port1, cmd) |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 929 | print cmd_string |
| 930 | res=os.popen(cmd_string).read() |
| 931 | result = result + ' ' + res |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 932 | cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % (dst_dpid, port2, cmd) |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 933 | print cmd_string |
| 934 | res=os.popen(cmd_string).read() |
| 935 | result = result + ' ' + res |
| 936 | |
| 937 | |
| 938 | return result |
| 939 | |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 940 | |
| 941 | #* Link Down |
| 942 | #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] | 943 | @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] | 944 | def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port): |
| 945 | |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 946 | if src_dpid in core_switches: |
| 947 | host = controllers[0] |
| 948 | else: |
| 949 | hostid=int(src_dpid.split(':')[-2]) |
| 950 | host = controllers[hostid-1] |
| 951 | |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 952 | if (TESTBED == "sw"): |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 953 | cmd_string="ssh %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd) |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 954 | else: |
Tim Lindberg | a6c0417 | 2013-04-05 17:34:05 -0700 | [diff] [blame] | 955 | if ( src_dpid == "00:00:00:08:a2:08:f9:01" ): |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 956 | cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % ( dst_dpid, dst_port, cmd) |
Tim Lindberg | a6c0417 | 2013-04-05 17:34:05 -0700 | [diff] [blame] | 957 | else: |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 958 | cmd_string="~/ONOS/scripts/link-hw.sh %s %s %s " % ( src_dpid, src_port, cmd) |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 959 | print cmd_string |
| 960 | |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 961 | result=os.popen(cmd_string).read() |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 962 | |
| 963 | return result |
| 964 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 965 | #* Create Flow |
| 966 | #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] | 967 | #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] | 968 | @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] | 969 | def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC): |
Masayoshi Kobayashi | ab01bcf | 2013-04-09 04:21:57 +0000 | [diff] [blame] | 970 | host = pick_host() |
| 971 | url ="%s/wm/flow/getsummary/%s/%s/json" % (host, 0, 0) |
| 972 | (code, result) = get_json(url) |
| 973 | parsedResult = json.loads(result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 974 | if len(parsedResult) > 0: |
| 975 | if parsedResult[-1].has_key('flowId'): |
| 976 | flow_nr = int(parsedResult[-1]['flowId'], 16) |
| 977 | else: |
| 978 | flow_nr = -1 # first flow |
| 979 | print "first flow" |
| 980 | |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 981 | flow_nr += 1 |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 982 | command = "%s/web/add_flow.py -m onos %d %s %s %s %s %s matchSrcMac %s matchDstMac %s" % (ONOSDIR, flow_nr, "dummy", src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC) |
Masayoshi Kobayashi | df787a4 | 2013-04-09 01:18:48 +0000 | [diff] [blame] | 983 | flow_nr += 1 |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 984 | command1 = "%s/web/add_flow.py -m onos %d %s %s %s %s %s matchSrcMac %s matchDstMac %s" % (ONOSDIR, flow_nr, "dummy", dst_dpid, dst_port, src_dpid, src_port, dstMAC, srcMAC) |
Tim Lindberg | 6445a18 | 2013-04-15 10:17:18 -0700 | [diff] [blame] | 985 | print "add flow: %s, %s" % (command, command1) |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 986 | errcode = os.popen(command).read() |
Masayoshi Kobayashi | cdb652f | 2013-04-04 18:24:29 +0000 | [diff] [blame] | 987 | errcode1 = os.popen(command1).read() |
Tim Lindberg | 6445a18 | 2013-04-15 10:17:18 -0700 | [diff] [blame] | 988 | ret=command+":"+errcode+" "+command1+":"+errcode1 |
| 989 | print ret |
| 990 | return ret |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 991 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 992 | #* Delete Flow |
| 993 | #http://localhost:9000/gui/delflow/<flow_id> |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 994 | @app.route("/gui/delflow/<flow_id>") |
| 995 | def del_flow(flow_id): |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 996 | command = "%/web/delete_flow.py %s" % (ONOSDIR, flow_id) |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 997 | print command |
| 998 | errcode = os.popen(command).read() |
| 999 | return errcode |
| 1000 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 1001 | #* Start Iperf Througput |
Umesh Krishnaswamy | 6689be3 | 2013-03-27 18:12:26 -0700 | [diff] [blame] | 1002 | #http://localhost:9000/gui/iperf/start/<flow_id>/<duration> |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 1003 | @app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>") |
| 1004 | def iperf_start(flow_id,duration,samples): |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 1005 | try: |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 1006 | 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] | 1007 | print command |
| 1008 | result = os.popen(command).read() |
| 1009 | if len(result) == 0: |
| 1010 | print "No Flow found" |
Tim Lindberg | 6445a18 | 2013-04-15 10:17:18 -0700 | [diff] [blame] | 1011 | return "Flow %s not found" % (flow_id); |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 1012 | except: |
| 1013 | print "REST IF has issue" |
Tim Lindberg | 6445a18 | 2013-04-15 10:17:18 -0700 | [diff] [blame] | 1014 | return "REST IF has issue" |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 1015 | exit |
| 1016 | |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 1017 | parsedResult = json.loads(result) |
| 1018 | |
| 1019 | flowId = int(parsedResult['flowId']['value'], 16) |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 1020 | src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value'] |
| 1021 | src_port = parsedResult['dataPath']['srcPort']['port']['value'] |
| 1022 | dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value'] |
| 1023 | dst_port = parsedResult['dataPath']['dstPort']['port']['value'] |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 1024 | # 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] | 1025 | |
| 1026 | if src_dpid in core_switches: |
Umesh Krishnaswamy | 5c3eddf | 2013-04-06 00:24:01 -0700 | [diff] [blame] | 1027 | src_host = controllers[0] |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 1028 | else: |
| 1029 | hostid=int(src_dpid.split(':')[-2]) |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 1030 | if TESTBED == "hw": |
| 1031 | src_host = "mininet%i" % hostid |
| 1032 | else: |
| 1033 | src_host = controllers[hostid-1] |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 1034 | |
Umesh Krishnaswamy | 5c3eddf | 2013-04-06 00:24:01 -0700 | [diff] [blame] | 1035 | if dst_dpid in core_switches: |
| 1036 | dst_host = controllers[0] |
| 1037 | else: |
| 1038 | hostid=int(dst_dpid.split(':')[-2]) |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 1039 | if TESTBED == "hw": |
| 1040 | dst_host = "mininet%i" % hostid |
| 1041 | else: |
| 1042 | dst_host = controllers[hostid-1] |
Umesh Krishnaswamy | 5c3eddf | 2013-04-06 00:24:01 -0700 | [diff] [blame] | 1043 | |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 1044 | # /runiperf.sh <flowid> <src_dpid> <dst_dpid> hw:svr|sw:svr|hw:client|sw:client <proto>/<duration>/<interval>/<samples> |
Umesh Krishnaswamy | 5c3eddf | 2013-04-06 00:24:01 -0700 | [diff] [blame] | 1045 | protocol="udp" |
| 1046 | interval=0.1 |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 1047 | if TESTBED == "hw": |
| 1048 | cmd_string="dsh -w %s 'cd ONOS/scripts; " % dst_host |
| 1049 | else: |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 1050 | cmd_string="ssh %s 'cd ONOS/scripts; " % dst_host |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 1051 | cmd_string += "./runiperf.sh %d %s %s %s:%s %s/%s/%s/%s'" % (flowId, src_dpid, dst_dpid, TESTBED, "svr", protocol, duration, interval, samples) |
Umesh Krishnaswamy | 5c3eddf | 2013-04-06 00:24:01 -0700 | [diff] [blame] | 1052 | print cmd_string |
| 1053 | os.popen(cmd_string) |
| 1054 | |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 1055 | if TESTBED == "hw": |
| 1056 | cmd_string="dsh -w %s 'cd ONOS/scripts; " % src_host |
| 1057 | else: |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 1058 | cmd_string="ssh %s 'cd ONOS/scripts;" % src_host |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 1059 | cmd_string+="./runiperf.sh %d %s %s %s:%s %s/%s/%s/%s'" % (flowId, src_dpid, dst_dpid, TESTBED, "client", protocol, duration, interval, samples) |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 1060 | print cmd_string |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 1061 | os.popen(cmd_string) |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 1062 | |
Masayoshi Kobayashi | cdb652f | 2013-04-04 18:24:29 +0000 | [diff] [blame] | 1063 | return cmd_string |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 1064 | |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 1065 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 1066 | #* Get Iperf Throughput |
| 1067 | #http://localhost:9000/gui/iperf/rate/<flow_id> |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 1068 | @app.route("/gui/iperf/rate/<flow_id>") |
| 1069 | def iperf_rate(flow_id): |
| 1070 | try: |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 1071 | command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id) |
| 1072 | print command |
| 1073 | result = os.popen(command).read() |
| 1074 | if len(result) == 0: |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 1075 | resp = Response(result, status=400, mimetype='text/html') |
| 1076 | return "no such iperf flow (flowid %s)" % flow_id; |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 1077 | except: |
| 1078 | print "REST IF has issue" |
| 1079 | exit |
| 1080 | |
| 1081 | parsedResult = json.loads(result) |
| 1082 | |
| 1083 | flowId = int(parsedResult['flowId']['value'], 16) |
| 1084 | src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value'] |
| 1085 | src_port = parsedResult['dataPath']['srcPort']['port']['value'] |
| 1086 | dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value'] |
| 1087 | dst_port = parsedResult['dataPath']['dstPort']['port']['value'] |
| 1088 | |
Umesh Krishnaswamy | 5c3eddf | 2013-04-06 00:24:01 -0700 | [diff] [blame] | 1089 | if dst_dpid in core_switches: |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 1090 | host = controllers[0] |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 1091 | else: |
Tim Lindberg | b03673d | 2013-04-10 13:47:31 -0700 | [diff] [blame] | 1092 | hostid=int(dst_dpid.split(':')[-2]) |
| 1093 | if TESTBED == "hw": |
| 1094 | host = "mininet%i" % hostid |
| 1095 | else: |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 1096 | host = controllers[hostid-1] |
| 1097 | |
| 1098 | try: |
Umesh Krishnaswamy | 5c3eddf | 2013-04-06 00:24:01 -0700 | [diff] [blame] | 1099 | command = "curl -s http://%s:%s/log/iperfsvr_%s.out" % (host, 9000, flow_id) |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 1100 | print command |
| 1101 | result = os.popen(command).read() |
| 1102 | except: |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 1103 | exit |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 1104 | |
Tim Lindberg | 6445a18 | 2013-04-15 10:17:18 -0700 | [diff] [blame] | 1105 | if re.match("Cannot", result): |
Masayoshi Kobayashi | b56f297 | 2013-04-05 02:57:29 +0000 | [diff] [blame] | 1106 | resp = Response(result, status=400, mimetype='text/html') |
Tim Lindberg | 6445a18 | 2013-04-15 10:17:18 -0700 | [diff] [blame] | 1107 | return "no iperf file found (host %s flowid %s): %s" % (host, flow_id, result) |
Masayoshi Kobayashi | b56f297 | 2013-04-05 02:57:29 +0000 | [diff] [blame] | 1108 | else: |
| 1109 | resp = Response(result, status=200, mimetype='application/json') |
| 1110 | return resp |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 1111 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 1112 | if __name__ == "__main__": |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 1113 | random.seed() |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 1114 | read_config() |
Masayoshi Kobayashi | 8158d44 | 2013-04-09 02:43:39 +0000 | [diff] [blame] | 1115 | read_link_def() |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 1116 | if len(sys.argv) > 1 and sys.argv[1] == "-d": |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 1117 | # 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") |
| 1118 | # link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1) |
| 1119 | # link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1) |
| 1120 | # link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1) |
| 1121 | # 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] | 1122 | # print "-- query all switches --" |
| 1123 | # query_switch() |
| 1124 | # print "-- query topo --" |
| 1125 | # topology_for_gui() |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 1126 | # link_change(1,2,3,4) |
| 1127 | print "-- query all links --" |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 1128 | # query_links() |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 1129 | # print "-- query all devices --" |
| 1130 | # devices() |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 1131 | # iperf_start(1,10,15) |
| 1132 | # iperf_rate(1) |
Masayoshi Kobayashi | ab01bcf | 2013-04-09 04:21:57 +0000 | [diff] [blame] | 1133 | # switches() |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 1134 | # add_flow(1,2,3,4,5,6) |
| 1135 | reset_demo() |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 1136 | else: |
| 1137 | app.debug = True |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 1138 | app.run(threaded=True, host="0.0.0.0", port=9000) |