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