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