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 | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 215 | @app.route("/wm/core/topology/switches/all/json") |
| 216 | def switches(): |
| 217 | if request.args.get('proxy') == None: |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 218 | host = pick_host() |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 219 | else: |
| 220 | host = ONOS_GUI3_HOST |
| 221 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 222 | url ="%s/wm/core/topology/switches/all/json" % (host) |
| 223 | (code, result) = get_json(url) |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 224 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 225 | resp = Response(result, status=code, mimetype='application/json') |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 226 | return resp |
| 227 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 228 | ## Link ## |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 229 | @app.route("/wm/core/topology/links/json") |
| 230 | def links(): |
| 231 | if request.args.get('proxy') == None: |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 232 | host = pick_host() |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 233 | else: |
| 234 | host = ONOS_GUI3_HOST |
| 235 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 236 | url ="%s/wm/core/topology/links/json" % (host) |
| 237 | (code, result) = get_json(url) |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 238 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 239 | resp = Response(result, status=code, mimetype='application/json') |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 240 | return resp |
| 241 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 242 | ## FlowSummary ## |
Masayoshi Kobayashi | cdb652f | 2013-04-04 18:24:29 +0000 | [diff] [blame] | 243 | @app.route("/wm/flow/getsummary/<start>/<range>/json") |
| 244 | def flows(start, range): |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 245 | if request.args.get('proxy') == None: |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 246 | host = pick_host() |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 247 | else: |
| 248 | host = ONOS_GUI3_HOST |
| 249 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 250 | url ="%s/wm/flow/getsummary/%s/%s/json" % (host, start, range) |
| 251 | (code, result) = get_json(url) |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 252 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 253 | resp = Response(result, status=code, mimetype='application/json') |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 254 | return resp |
| 255 | |
| 256 | @app.route("/wm/registry/controllers/json") |
| 257 | def registry_controllers(): |
| 258 | if request.args.get('proxy') == None: |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 259 | host = pick_host() |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 260 | else: |
| 261 | host = ONOS_GUI3_HOST |
| 262 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 263 | url= "%s/wm/registry/controllers/json" % (host) |
| 264 | (code, result) = get_json(url) |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 265 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 266 | resp = Response(result, status=code, mimetype='application/json') |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 267 | return resp |
| 268 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 269 | |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 270 | @app.route("/wm/registry/switches/json") |
| 271 | def registry_switches(): |
| 272 | if request.args.get('proxy') == None: |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 273 | host = pick_host() |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 274 | else: |
| 275 | host = ONOS_GUI3_HOST |
| 276 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 277 | url="%s/wm/registry/switches/json" % (host) |
| 278 | (code, result) = get_json(url) |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 279 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 280 | resp = Response(result, status=code, mimetype='application/json') |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 281 | return resp |
| 282 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 283 | def node_id(switch_array, dpid): |
| 284 | id = -1 |
| 285 | for i, val in enumerate(switch_array): |
| 286 | if val['name'] == dpid: |
| 287 | id = i |
| 288 | break |
| 289 | |
| 290 | return id |
| 291 | |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 292 | ## API for ON.Lab local GUI ## |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 293 | @app.route('/topology', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 294 | def topology_for_gui(): |
| 295 | try: |
| 296 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 297 | result = os.popen(command).read() |
| 298 | parsedResult = json.loads(result) |
| 299 | except: |
| 300 | log_error("REST IF has issue: %s" % command) |
| 301 | log_error("%s" % result) |
| 302 | sys.exit(0) |
| 303 | |
| 304 | topo = {} |
| 305 | switches = [] |
| 306 | links = [] |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 307 | devices = [] |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 308 | |
| 309 | for v in parsedResult: |
| 310 | if v.has_key('dpid'): |
| 311 | # if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 312 | dpid = str(v['dpid']) |
| 313 | state = str(v['state']) |
| 314 | sw = {} |
| 315 | sw['name']=dpid |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 316 | sw['group']= -1 |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 317 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 318 | if state == "INACTIVE": |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 319 | sw['group']=0 |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 320 | switches.append(sw) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 321 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 322 | try: |
| 323 | command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort) |
| 324 | result = os.popen(command).read() |
| 325 | parsedResult = json.loads(result) |
| 326 | except: |
| 327 | log_error("REST IF has issue: %s" % command) |
| 328 | log_error("%s" % result) |
| 329 | |
| 330 | for key in parsedResult: |
| 331 | dpid = key |
| 332 | ctrl = parsedResult[dpid][0]['controllerId'] |
| 333 | sw_id = node_id(switches, dpid) |
| 334 | if sw_id != -1: |
| 335 | if switches[sw_id]['group'] != 0: |
| 336 | switches[sw_id]['group'] = controllers.index(ctrl) + 1 |
| 337 | |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 338 | try: |
| 339 | v1 = "00:00:00:00:00:0a:0d:00" |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 340 | # v1 = "00:00:00:00:00:0d:00:d1" |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 341 | p1=1 |
| 342 | v2 = "00:00:00:00:00:0b:0d:03" |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 343 | # v2 = "00:00:00:00:00:0d:00:d3" |
| 344 | p2=1 |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 345 | command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2) |
| 346 | result = os.popen(command).read() |
| 347 | parsedResult = json.loads(result) |
| 348 | except: |
| 349 | log_error("No route") |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 350 | parsedResult = {} |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 351 | |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 352 | path = [] |
| 353 | if parsedResult.has_key('flowEntries'): |
| 354 | flowEntries= parsedResult['flowEntries'] |
| 355 | for i, v in enumerate(flowEntries): |
| 356 | if i < len(flowEntries) - 1: |
| 357 | sdpid= flowEntries[i]['dpid']['value'] |
| 358 | ddpid = flowEntries[i+1]['dpid']['value'] |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 359 | path.append( (sdpid, ddpid)) |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 360 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 361 | try: |
| 362 | command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort) |
| 363 | result = os.popen(command).read() |
| 364 | parsedResult = json.loads(result) |
| 365 | except: |
| 366 | log_error("REST IF has issue: %s" % command) |
| 367 | log_error("%s" % result) |
| 368 | sys.exit(0) |
| 369 | |
| 370 | for v in parsedResult: |
| 371 | link = {} |
| 372 | if v.has_key('dst-switch'): |
| 373 | dst_dpid = str(v['dst-switch']) |
| 374 | dst_id = node_id(switches, dst_dpid) |
| 375 | if v.has_key('src-switch'): |
| 376 | src_dpid = str(v['src-switch']) |
| 377 | src_id = node_id(switches, src_dpid) |
| 378 | link['source'] = src_id |
| 379 | link['target'] = dst_id |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 380 | |
| 381 | onpath = 0 |
| 382 | for (s,d) in path: |
| 383 | if s == v['src-switch'] and d == v['dst-switch']: |
| 384 | onpath = 1 |
| 385 | break |
| 386 | link['type'] = onpath |
| 387 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 388 | links.append(link) |
| 389 | |
| 390 | topo['nodes'] = switches |
| 391 | topo['links'] = links |
| 392 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 393 | js = json.dumps(topo) |
| 394 | resp = Response(js, status=200, mimetype='application/json') |
| 395 | return resp |
| 396 | |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 397 | #@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json") |
| 398 | #@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json") |
| 399 | @app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json") |
| 400 | def shortest_path(v1, p1, v2, p2): |
| 401 | try: |
| 402 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 403 | result = os.popen(command).read() |
| 404 | parsedResult = json.loads(result) |
| 405 | except: |
| 406 | log_error("REST IF has issue: %s" % command) |
| 407 | log_error("%s" % result) |
| 408 | sys.exit(0) |
| 409 | |
| 410 | topo = {} |
| 411 | switches = [] |
| 412 | links = [] |
| 413 | |
| 414 | for v in parsedResult: |
| 415 | if v.has_key('dpid'): |
| 416 | dpid = str(v['dpid']) |
| 417 | state = str(v['state']) |
| 418 | sw = {} |
| 419 | sw['name']=dpid |
| 420 | if str(v['state']) == "ACTIVE": |
| 421 | if dpid[-2:-1] == "a": |
| 422 | sw['group']=1 |
| 423 | if dpid[-2:-1] == "b": |
| 424 | sw['group']=2 |
| 425 | if dpid[-2:-1] == "c": |
| 426 | sw['group']=3 |
| 427 | if str(v['state']) == "INACTIVE": |
| 428 | sw['group']=0 |
| 429 | |
| 430 | switches.append(sw) |
| 431 | |
| 432 | try: |
| 433 | command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2) |
| 434 | result = os.popen(command).read() |
| 435 | parsedResult = json.loads(result) |
| 436 | except: |
| 437 | log_error("No route") |
| 438 | parsedResult = [] |
| 439 | # exit(1) |
| 440 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 441 | path = []; |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 442 | for i, v in enumerate(parsedResult): |
| 443 | if i < len(parsedResult) - 1: |
| 444 | sdpid= parsedResult[i]['switch'] |
| 445 | ddpid = parsedResult[i+1]['switch'] |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 446 | path.append( (sdpid, ddpid)) |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 447 | |
| 448 | try: |
| 449 | command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort) |
| 450 | result = os.popen(command).read() |
| 451 | parsedResult = json.loads(result) |
| 452 | except: |
| 453 | log_error("REST IF has issue: %s" % command) |
| 454 | log_error("%s" % result) |
| 455 | sys.exit(0) |
| 456 | |
| 457 | for v in parsedResult: |
| 458 | link = {} |
| 459 | if v.has_key('dst-switch'): |
| 460 | dst_dpid = str(v['dst-switch']) |
| 461 | dst_id = node_id(switches, dst_dpid) |
| 462 | if v.has_key('src-switch'): |
| 463 | src_dpid = str(v['src-switch']) |
| 464 | src_id = node_id(switches, src_dpid) |
| 465 | link['source'] = src_id |
| 466 | link['target'] = dst_id |
| 467 | onpath = 0 |
| 468 | for (s,d) in path: |
| 469 | if s == v['src-switch'] and d == v['dst-switch']: |
| 470 | onpath = 1 |
| 471 | break |
| 472 | |
| 473 | link['type'] = onpath |
| 474 | links.append(link) |
| 475 | |
| 476 | topo['nodes'] = switches |
| 477 | topo['links'] = links |
| 478 | |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 479 | js = json.dumps(topo) |
| 480 | resp = Response(js, status=200, mimetype='application/json') |
| 481 | return resp |
| 482 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 483 | @app.route("/wm/core/controller/switches/json") |
| 484 | def query_switch(): |
| 485 | try: |
| 486 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 487 | # http://localhost:8080/wm/core/topology/switches/active/json |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 488 | print command |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 489 | result = os.popen(command).read() |
| 490 | parsedResult = json.loads(result) |
| 491 | except: |
| 492 | log_error("REST IF has issue: %s" % command) |
| 493 | log_error("%s" % result) |
| 494 | sys.exit(0) |
| 495 | |
| 496 | # print command |
| 497 | # print result |
| 498 | switches_ = [] |
| 499 | for v in parsedResult: |
| 500 | if v.has_key('dpid'): |
| 501 | if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 502 | dpid = str(v['dpid']) |
| 503 | state = str(v['state']) |
| 504 | sw = {} |
| 505 | sw['dpid']=dpid |
| 506 | sw['active']=state |
| 507 | switches_.append(sw) |
| 508 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 509 | # pp.pprint(switches_) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 510 | js = json.dumps(switches_) |
| 511 | resp = Response(js, status=200, mimetype='application/json') |
| 512 | return resp |
| 513 | |
| 514 | @app.route("/wm/device/") |
| 515 | def devices(): |
| 516 | try: |
| 517 | command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName) |
| 518 | result = os.popen(command).read() |
| 519 | parsedResult = json.loads(result)['results'] |
| 520 | except: |
| 521 | log_error("REST IF has issue: %s" % command) |
| 522 | log_error("%s" % result) |
| 523 | sys.exit(0) |
| 524 | |
| 525 | devices = [] |
| 526 | for v in parsedResult: |
| 527 | dl_addr = v['dl_addr'] |
| 528 | nw_addr = v['nw_addr'] |
| 529 | vertex = v['_id'] |
| 530 | mac = [] |
| 531 | mac.append(dl_addr) |
| 532 | ip = [] |
| 533 | ip.append(nw_addr) |
| 534 | device = {} |
| 535 | device['entryClass']="DefaultEntryClass" |
| 536 | device['mac']=mac |
| 537 | device['ipv4']=ip |
| 538 | device['vlan']=[] |
| 539 | device['lastSeen']=0 |
| 540 | attachpoints =[] |
| 541 | |
| 542 | port, dpid = deviceV_to_attachpoint(vertex) |
| 543 | attachpoint = {} |
| 544 | attachpoint['port']=port |
| 545 | attachpoint['switchDPID']=dpid |
| 546 | attachpoints.append(attachpoint) |
| 547 | device['attachmentPoint']=attachpoints |
| 548 | devices.append(device) |
| 549 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 550 | js = json.dumps(devices) |
| 551 | resp = Response(js, status=200, mimetype='application/json') |
| 552 | return resp |
| 553 | |
| 554 | #{"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} |
| 555 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 556 | ## return fake stat for now |
| 557 | @app.route("/wm/core/switch/<switchId>/<statType>/json") |
| 558 | def switch_stat(switchId, statType): |
| 559 | if statType == "desc": |
| 560 | desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}] |
| 561 | ret = {} |
| 562 | ret[switchId]=desc |
| 563 | elif statType == "aggregate": |
| 564 | aggr = {"packetCount":0,"byteCount":0,"flowCount":0} |
| 565 | ret = {} |
| 566 | ret[switchId]=aggr |
| 567 | else: |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 568 | ret = {} |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 569 | |
| 570 | js = json.dumps(ret) |
| 571 | resp = Response(js, status=200, mimetype='application/json') |
| 572 | return resp |
| 573 | |
| 574 | |
| 575 | @app.route("/wm/topology/links/json") |
| 576 | def query_links(): |
| 577 | try: |
| 578 | 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] | 579 | print command |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 580 | result = os.popen(command).read() |
| 581 | parsedResult = json.loads(result)['results'] |
| 582 | except: |
| 583 | log_error("REST IF has issue: %s" % command) |
| 584 | log_error("%s" % result) |
| 585 | sys.exit(0) |
| 586 | |
| 587 | debug("query_links %s" % command) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 588 | # pp.pprint(parsedResult) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 589 | sport = [] |
| 590 | links = [] |
| 591 | for v in parsedResult: |
| 592 | srcport = v['_id'] |
| 593 | try: |
| 594 | command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport) |
| 595 | print command |
| 596 | result = os.popen(command).read() |
| 597 | linkResults = json.loads(result)['results'] |
| 598 | except: |
| 599 | log_error("REST IF has issue: %s" % command) |
| 600 | log_error("%s" % result) |
| 601 | sys.exit(0) |
| 602 | |
| 603 | for p in linkResults: |
| 604 | if p.has_key('type') and p['type'] == "port": |
| 605 | dstport = p['_id'] |
| 606 | (sport, sdpid) = portV_to_port_dpid(srcport) |
| 607 | (dport, ddpid) = portV_to_port_dpid(dstport) |
| 608 | link = {} |
| 609 | link["src-switch"]=sdpid |
| 610 | link["src-port"]=sport |
| 611 | link["src-port-state"]=0 |
| 612 | link["dst-switch"]=ddpid |
| 613 | link["dst-port"]=dport |
| 614 | link["dst-port-state"]=0 |
| 615 | link["type"]="internal" |
| 616 | links.append(link) |
| 617 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 618 | # pp.pprint(links) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 619 | js = json.dumps(links) |
| 620 | resp = Response(js, status=200, mimetype='application/json') |
| 621 | return resp |
| 622 | |
Ubuntu | c016ba1 | 2013-02-27 21:53:41 +0000 | [diff] [blame] | 623 | @app.route("/controller_status") |
| 624 | def controller_status(): |
| 625 | onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'" |
| 626 | #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status" |
| 627 | |
| 628 | cont_status=[] |
| 629 | for i in controllers: |
| 630 | status={} |
| 631 | onos=os.popen(onos_check % i).read()[:-1] |
| 632 | status["name"]=i |
| 633 | status["onos"]=onos |
Masayoshi Kobayashi | 5e91bdf | 2013-03-15 01:22:51 +0000 | [diff] [blame] | 634 | status["cassandra"]=0 |
Ubuntu | c016ba1 | 2013-02-27 21:53:41 +0000 | [diff] [blame] | 635 | cont_status.append(status) |
| 636 | |
| 637 | js = json.dumps(cont_status) |
| 638 | resp = Response(js, status=200, mimetype='application/json') |
Ubuntu | c016ba1 | 2013-02-27 21:53:41 +0000 | [diff] [blame] | 639 | return resp |
| 640 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 641 | ### Command ### |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 642 | @app.route("/gui/controller/<cmd>/<controller_name>") |
| 643 | def controller_status_change(cmd, controller_name): |
| 644 | start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name) |
| 645 | stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name) |
| 646 | |
| 647 | if cmd == "up": |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 648 | result=os.popen(start_onos).read() |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 649 | ret = "controller %s is up" % (controller_name) |
| 650 | elif cmd == "down": |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 651 | result=os.popen(stop_onos).read() |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 652 | ret = "controller %s is down" % (controller_name) |
| 653 | |
| 654 | return ret |
| 655 | |
| 656 | @app.route("/gui/switch/<cmd>/<dpid>") |
| 657 | def switch_status_change(cmd, dpid): |
| 658 | r = re.compile(':') |
| 659 | dpid = re.sub(r, '', dpid) |
Masayoshi Kobayashi | c0bc319 | 2013-03-27 23:12:03 +0000 | [diff] [blame] | 660 | host=controllers[0] |
| 661 | cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd) |
| 662 | 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] | 663 | print "cmd_string" |
| 664 | |
| 665 | if cmd =="up" or cmd=="down": |
| 666 | print "make dpid %s %s" % (dpid, cmd) |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 667 | os.popen(cmd_string) |
| 668 | result=os.popen(get_status).read() |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 669 | |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 670 | return result |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 671 | |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 672 | #* Link Up |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 673 | #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] | 674 | @app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>") |
| 675 | def link_up(src_dpid, src_port, dst_dpid, dst_port): |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 676 | |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 677 | cmd = 'up' |
| 678 | result="" |
| 679 | |
Paul Greyson | 472da4c | 2013-03-28 11:43:17 -0700 | [diff] [blame] | 680 | for dpid in (src_dpid, dst_dpid): |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 681 | if dpid in core_switches: |
| 682 | host = controllers[0] |
| 683 | src_ports = [1, 2, 3, 4, 5] |
| 684 | else: |
Masayoshi Kobayashi | 05f12b3 | 2013-04-01 09:08:09 +0000 | [diff] [blame] | 685 | hostid=int(dpid.split(':')[-2]) |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 686 | host = controllers[hostid-1] |
| 687 | if hostid == 2 : |
| 688 | src_ports = [51] |
| 689 | else : |
| 690 | src_ports = [26] |
| 691 | |
| 692 | for port in src_ports : |
| 693 | cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd) |
| 694 | print cmd_string |
| 695 | res=os.popen(cmd_string).read() |
| 696 | result = result + ' ' + res |
| 697 | |
| 698 | return result |
| 699 | |
| 700 | |
| 701 | #* Link Down |
| 702 | #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] | 703 | @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] | 704 | def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port): |
| 705 | |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 706 | if src_dpid in core_switches: |
| 707 | host = controllers[0] |
| 708 | else: |
| 709 | hostid=int(src_dpid.split(':')[-2]) |
| 710 | host = controllers[hostid-1] |
| 711 | |
| 712 | cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd) |
| 713 | print cmd_string |
| 714 | |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 715 | result=os.popen(cmd_string).read() |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 716 | |
| 717 | return result |
| 718 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 719 | #* Create Flow |
| 720 | #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] | 721 | #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] | 722 | @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] | 723 | def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC): |
| 724 | command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1" |
| 725 | print command |
| 726 | ret = os.popen(command).read() |
| 727 | if ret == "": |
| 728 | flow_nr=0 |
| 729 | else: |
| 730 | flow_nr=int(ret) |
| 731 | |
| 732 | flow_nr += 1 |
Umesh Krishnaswamy | f22d3ed | 2013-04-03 11:57:54 -0700 | [diff] [blame] | 733 | 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] | 734 | 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] | 735 | print command |
| 736 | errcode = os.popen(command).read() |
Masayoshi Kobayashi | cdb652f | 2013-04-04 18:24:29 +0000 | [diff] [blame] | 737 | errcode1 = os.popen(command1).read() |
| 738 | return errcode+" "+errcode1 |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 739 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 740 | #* Delete Flow |
| 741 | #http://localhost:9000/gui/delflow/<flow_id> |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 742 | @app.route("/gui/delflow/<flow_id>") |
| 743 | def del_flow(flow_id): |
| 744 | command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id) |
| 745 | print command |
| 746 | errcode = os.popen(command).read() |
| 747 | return errcode |
| 748 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 749 | #* Start Iperf Througput |
Umesh Krishnaswamy | 6689be3 | 2013-03-27 18:12:26 -0700 | [diff] [blame] | 750 | #http://localhost:9000/gui/iperf/start/<flow_id>/<duration> |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 751 | @app.route("/gui/iperf/start/<flow_id>/<duration>/<samples>") |
| 752 | def iperf_start(flow_id,duration,samples): |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 753 | try: |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 754 | 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] | 755 | print command |
| 756 | result = os.popen(command).read() |
| 757 | if len(result) == 0: |
| 758 | print "No Flow found" |
| 759 | return; |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 760 | except: |
| 761 | print "REST IF has issue" |
| 762 | exit |
| 763 | |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 764 | parsedResult = json.loads(result) |
| 765 | |
| 766 | flowId = int(parsedResult['flowId']['value'], 16) |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 767 | src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value'] |
| 768 | src_port = parsedResult['dataPath']['srcPort']['port']['value'] |
| 769 | dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value'] |
| 770 | dst_port = parsedResult['dataPath']['dstPort']['port']['value'] |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 771 | # 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] | 772 | |
| 773 | if src_dpid in core_switches: |
| 774 | host = controllers[0] |
| 775 | else: |
| 776 | hostid=int(src_dpid.split(':')[-2]) |
| 777 | host = controllers[hostid-1] |
| 778 | |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 779 | # ./runiperf.sh 2 00:00:00:00:00:00:02:02 1 00:00:00:00:00:00:03:02 1 100 15 |
| 780 | 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] | 781 | print cmd_string |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 782 | os.popen(cmd_string) |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 783 | |
Masayoshi Kobayashi | cdb652f | 2013-04-04 18:24:29 +0000 | [diff] [blame] | 784 | return cmd_string |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 785 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 786 | #* Get Iperf Throughput |
| 787 | #http://localhost:9000/gui/iperf/rate/<flow_id> |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 788 | @app.route("/gui/iperf/rate/<flow_id>") |
| 789 | def iperf_rate(flow_id): |
| 790 | try: |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 791 | command = "curl -s \'http://%s:%s/wm/flow/get/%s/json\'" % (RestIP, RestPort, flow_id) |
| 792 | print command |
| 793 | result = os.popen(command).read() |
| 794 | if len(result) == 0: |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 795 | resp = Response(result, status=400, mimetype='text/html') |
| 796 | return "no such iperf flow (flowid %s)" % flow_id; |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 797 | except: |
| 798 | print "REST IF has issue" |
| 799 | exit |
| 800 | |
| 801 | parsedResult = json.loads(result) |
| 802 | |
| 803 | flowId = int(parsedResult['flowId']['value'], 16) |
| 804 | src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value'] |
| 805 | src_port = parsedResult['dataPath']['srcPort']['port']['value'] |
| 806 | dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value'] |
| 807 | dst_port = parsedResult['dataPath']['dstPort']['port']['value'] |
| 808 | |
| 809 | if src_dpid in core_switches: |
| 810 | host = controllers[0] |
| 811 | else: |
| 812 | hostid=int(src_dpid.split(':')[-2]) |
| 813 | host = controllers[hostid-1] |
| 814 | |
| 815 | try: |
| 816 | 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] | 817 | print command |
| 818 | result = os.popen(command).read() |
| 819 | except: |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 820 | exit |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 821 | |
Masayoshi Kobayashi | b56f297 | 2013-04-05 02:57:29 +0000 | [diff] [blame] | 822 | if len(result) == 0: |
| 823 | resp = Response(result, status=400, mimetype='text/html') |
| 824 | return "no iperf file found (flowid %s)" % flow_id; |
| 825 | else: |
| 826 | resp = Response(result, status=200, mimetype='application/json') |
| 827 | return resp |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 828 | |
| 829 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 830 | if __name__ == "__main__": |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 831 | random.seed() |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 832 | if len(sys.argv) > 1 and sys.argv[1] == "-d": |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 833 | # 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") |
| 834 | # link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1) |
| 835 | # link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1) |
| 836 | # link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1) |
| 837 | # 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] | 838 | # print "-- query all switches --" |
| 839 | # query_switch() |
| 840 | # print "-- query topo --" |
| 841 | # topology_for_gui() |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 842 | # link_change(1,2,3,4) |
| 843 | print "-- query all links --" |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 844 | # query_links() |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 845 | # print "-- query all devices --" |
| 846 | # devices() |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 847 | # iperf_start(1,10,15) |
| 848 | # iperf_rate(1) |
| 849 | switches() |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 850 | else: |
| 851 | app.debug = True |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 852 | app.run(threaded=True, host="0.0.0.0", port=9000) |