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