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 |
| 10 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 11 | import re |
| 12 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 13 | from flask import Flask, json, Response, render_template, make_response, request |
| 14 | |
| 15 | ## Global Var ## |
Ubuntu | f6ce96c | 2013-02-07 01:45:07 +0000 | [diff] [blame] | 16 | RestIP="localhost" |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 17 | RestPort=8080 |
| 18 | #DBName="onos-network-map" |
Masayoshi Kobayashi | a9ed33a | 2013-03-27 23:14:18 +0000 | [diff] [blame] | 19 | |
| 20 | ## Uncomment the desired block based on your testbed environment |
| 21 | |
| 22 | # Settings for running on production |
| 23 | controllers=["onosgui1", "onosgui2", "onosgui3", "onosgui4", "onosgui5", "onosgui6", "onosgui7", "onosgui8"] |
| 24 | 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"] |
| 25 | ONOS_GUI3_HOST="http://gui3.onlab.us:8080" |
| 26 | ONOS_GUI3_CONTROL_HOST="http://gui3.onlab.us:8081" |
| 27 | |
| 28 | # Settings for running on dev testbed. Replace dev |
| 29 | #controllers=["onosdevb1", "onosdevb2", "onosdevb3", "onosdevb4"] |
| 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"] |
| 31 | #ONOS_GUI3_HOST="http://devb-gui.onlab.us:8080" |
| 32 | #ONOS_GUI3_CONTROL_HOST="http://devb-gui.onlab.us:8080" |
| 33 | |
| 34 | ONOS_LOCAL_HOST="http://localhost:8080" ;# for Amazon EC2 |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 35 | |
| 36 | nr_flow=0 |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 37 | |
| 38 | DEBUG=1 |
| 39 | pp = pprint.PrettyPrinter(indent=4) |
| 40 | |
| 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 | |
| 51 | ## Rest APIs ## |
| 52 | ### File Fetch ### |
| 53 | @app.route('/ui/img/<filename>', methods=['GET']) |
| 54 | @app.route('/img/<filename>', methods=['GET']) |
| 55 | @app.route('/css/<filename>', methods=['GET']) |
| 56 | @app.route('/js/models/<filename>', methods=['GET']) |
| 57 | @app.route('/js/views/<filename>', methods=['GET']) |
| 58 | @app.route('/js/<filename>', methods=['GET']) |
| 59 | @app.route('/lib/<filename>', methods=['GET']) |
| 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 | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 90 | |
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 | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 155 | @app.route("/wm/core/topology/switches/all/json") |
| 156 | def switches(): |
| 157 | if request.args.get('proxy') == None: |
| 158 | host = ONOS_LOCAL_HOST |
| 159 | else: |
| 160 | host = ONOS_GUI3_HOST |
| 161 | |
| 162 | try: |
| 163 | command = "curl -s %s/wm/core/topology/switches/all/json" % (host) |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 164 | # print command |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 165 | result = os.popen(command).read() |
| 166 | except: |
| 167 | print "REST IF has issue" |
| 168 | exit |
| 169 | |
| 170 | resp = Response(result, status=200, mimetype='application/json') |
| 171 | return resp |
| 172 | |
| 173 | @app.route("/wm/core/topology/links/json") |
| 174 | def links(): |
| 175 | if request.args.get('proxy') == None: |
| 176 | host = ONOS_LOCAL_HOST |
| 177 | else: |
| 178 | host = ONOS_GUI3_HOST |
| 179 | |
| 180 | try: |
| 181 | command = "curl -s %s/wm/core/topology/links/json" % (host) |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 182 | print command |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 183 | result = os.popen(command).read() |
| 184 | except: |
| 185 | print "REST IF has issue" |
| 186 | exit |
| 187 | |
| 188 | resp = Response(result, status=200, mimetype='application/json') |
| 189 | return resp |
| 190 | |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 191 | @app.route("/wm/flow/getsummary/0/0/json") |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 192 | def flows(): |
| 193 | if request.args.get('proxy') == None: |
| 194 | host = ONOS_LOCAL_HOST |
| 195 | else: |
| 196 | host = ONOS_GUI3_HOST |
| 197 | |
| 198 | try: |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 199 | command = "curl -s %s/wm/flow/getsummary/0/0/json" % (host) |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 200 | # print command |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 201 | result = os.popen(command).read() |
| 202 | except: |
| 203 | print "REST IF has issue" |
| 204 | exit |
| 205 | |
Masayoshi Kobayashi | 05f12b3 | 2013-04-01 09:08:09 +0000 | [diff] [blame] | 206 | |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 207 | resp = Response(result, status=200, mimetype='application/json') |
| 208 | return resp |
| 209 | |
| 210 | @app.route("/wm/registry/controllers/json") |
| 211 | def registry_controllers(): |
| 212 | if request.args.get('proxy') == None: |
| 213 | host = ONOS_LOCAL_HOST |
| 214 | else: |
| 215 | host = ONOS_GUI3_HOST |
| 216 | |
| 217 | try: |
| 218 | command = "curl -s %s/wm/registry/controllers/json" % (host) |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 219 | # print command |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 220 | result = os.popen(command).read() |
| 221 | except: |
| 222 | print "REST IF has issue" |
| 223 | exit |
| 224 | |
| 225 | resp = Response(result, status=200, mimetype='application/json') |
| 226 | return resp |
| 227 | |
| 228 | @app.route("/wm/registry/switches/json") |
| 229 | def registry_switches(): |
| 230 | if request.args.get('proxy') == None: |
| 231 | host = ONOS_LOCAL_HOST |
| 232 | else: |
| 233 | host = ONOS_GUI3_HOST |
| 234 | |
| 235 | try: |
| 236 | command = "curl -s %s/wm/registry/switches/json" % (host) |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 237 | # print command |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 238 | result = os.popen(command).read() |
| 239 | except: |
| 240 | print "REST IF has issue" |
| 241 | exit |
| 242 | |
| 243 | resp = Response(result, status=200, mimetype='application/json') |
| 244 | return resp |
| 245 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 246 | |
| 247 | def node_id(switch_array, dpid): |
| 248 | id = -1 |
| 249 | for i, val in enumerate(switch_array): |
| 250 | if val['name'] == dpid: |
| 251 | id = i |
| 252 | break |
| 253 | |
| 254 | return id |
| 255 | |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 256 | ## API for ON.Lab local GUI ## |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 257 | @app.route('/topology', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 258 | def topology_for_gui(): |
| 259 | try: |
| 260 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 261 | result = os.popen(command).read() |
| 262 | parsedResult = json.loads(result) |
| 263 | except: |
| 264 | log_error("REST IF has issue: %s" % command) |
| 265 | log_error("%s" % result) |
| 266 | sys.exit(0) |
| 267 | |
| 268 | topo = {} |
| 269 | switches = [] |
| 270 | links = [] |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 271 | devices = [] |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 272 | |
| 273 | for v in parsedResult: |
| 274 | if v.has_key('dpid'): |
| 275 | # if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 276 | dpid = str(v['dpid']) |
| 277 | state = str(v['state']) |
| 278 | sw = {} |
| 279 | sw['name']=dpid |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 280 | sw['group']= -1 |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 281 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 282 | if state == "INACTIVE": |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 283 | sw['group']=0 |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 284 | switches.append(sw) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 285 | |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 286 | ## Comment in if we need devies |
| 287 | # sw_index = len(switches) - 1 |
| 288 | # for p in v['ports']: |
| 289 | # for d in p['devices']: |
| 290 | # device = {} |
| 291 | # device['attached_switch']=dpid |
| 292 | # device['name']=d['mac'] |
| 293 | # if d['state'] == "ACTIVE": |
| 294 | # device['group']=1000 |
| 295 | # else: |
| 296 | # device['group']=1001 |
| 297 | # |
| 298 | # switches.append(device) |
| 299 | # device_index = len (switches) -1 |
| 300 | # link = {} |
| 301 | # link['source'] = device_index |
| 302 | # link['target'] = sw_index |
| 303 | # link['type'] = -1 |
| 304 | # links.append(link) |
| 305 | # link = {} |
| 306 | # link['source'] = sw_index |
| 307 | # link['target'] = device_index |
| 308 | # link['type'] = -1 |
| 309 | # links.append(link) |
| 310 | |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 311 | # try: |
| 312 | # command = "curl -s \'http://%s:%s/wm/registry/controllers/json\'" % (RestIP, RestPort) |
| 313 | # result = os.popen(command).read() |
| 314 | # controllers = json.loads(result) |
| 315 | # except: |
| 316 | # log_error("xx REST IF has issue: %s" % command) |
| 317 | # log_error("%s" % result) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 318 | |
| 319 | try: |
| 320 | command = "curl -s \'http://%s:%s/wm/registry/switches/json\'" % (RestIP, RestPort) |
| 321 | result = os.popen(command).read() |
| 322 | parsedResult = json.loads(result) |
| 323 | except: |
| 324 | log_error("REST IF has issue: %s" % command) |
| 325 | log_error("%s" % result) |
| 326 | |
| 327 | for key in parsedResult: |
| 328 | dpid = key |
| 329 | ctrl = parsedResult[dpid][0]['controllerId'] |
| 330 | sw_id = node_id(switches, dpid) |
| 331 | if sw_id != -1: |
| 332 | if switches[sw_id]['group'] != 0: |
| 333 | switches[sw_id]['group'] = controllers.index(ctrl) + 1 |
| 334 | |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 335 | try: |
| 336 | v1 = "00:00:00:00:00:0a:0d:00" |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 337 | # v1 = "00:00:00:00:00:0d:00:d1" |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 338 | p1=1 |
| 339 | v2 = "00:00:00:00:00:0b:0d:03" |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 340 | # v2 = "00:00:00:00:00:0d:00:d3" |
| 341 | p2=1 |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 342 | command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2) |
| 343 | result = os.popen(command).read() |
| 344 | parsedResult = json.loads(result) |
| 345 | except: |
| 346 | log_error("No route") |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 347 | parsedResult = {} |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 348 | |
Ubuntu | 765deff | 2013-02-28 18:39:13 +0000 | [diff] [blame] | 349 | path = [] |
| 350 | if parsedResult.has_key('flowEntries'): |
| 351 | flowEntries= parsedResult['flowEntries'] |
| 352 | for i, v in enumerate(flowEntries): |
| 353 | if i < len(flowEntries) - 1: |
| 354 | sdpid= flowEntries[i]['dpid']['value'] |
| 355 | ddpid = flowEntries[i+1]['dpid']['value'] |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 356 | path.append( (sdpid, ddpid)) |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 357 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 358 | try: |
| 359 | command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort) |
| 360 | result = os.popen(command).read() |
| 361 | parsedResult = json.loads(result) |
| 362 | except: |
| 363 | log_error("REST IF has issue: %s" % command) |
| 364 | log_error("%s" % result) |
| 365 | sys.exit(0) |
| 366 | |
| 367 | for v in parsedResult: |
| 368 | link = {} |
| 369 | if v.has_key('dst-switch'): |
| 370 | dst_dpid = str(v['dst-switch']) |
| 371 | dst_id = node_id(switches, dst_dpid) |
| 372 | if v.has_key('src-switch'): |
| 373 | src_dpid = str(v['src-switch']) |
| 374 | src_id = node_id(switches, src_dpid) |
| 375 | link['source'] = src_id |
| 376 | link['target'] = dst_id |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 377 | |
| 378 | onpath = 0 |
| 379 | for (s,d) in path: |
| 380 | if s == v['src-switch'] and d == v['dst-switch']: |
| 381 | onpath = 1 |
| 382 | break |
| 383 | link['type'] = onpath |
| 384 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 385 | links.append(link) |
| 386 | |
| 387 | topo['nodes'] = switches |
| 388 | topo['links'] = links |
| 389 | |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 390 | pp.pprint(topo) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 391 | js = json.dumps(topo) |
| 392 | resp = Response(js, status=200, mimetype='application/json') |
| 393 | return resp |
| 394 | |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 395 | #@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json") |
| 396 | #@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json") |
| 397 | @app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json") |
| 398 | def shortest_path(v1, p1, v2, p2): |
| 399 | try: |
| 400 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 401 | result = os.popen(command).read() |
| 402 | parsedResult = json.loads(result) |
| 403 | except: |
| 404 | log_error("REST IF has issue: %s" % command) |
| 405 | log_error("%s" % result) |
| 406 | sys.exit(0) |
| 407 | |
| 408 | topo = {} |
| 409 | switches = [] |
| 410 | links = [] |
| 411 | |
| 412 | for v in parsedResult: |
| 413 | if v.has_key('dpid'): |
| 414 | dpid = str(v['dpid']) |
| 415 | state = str(v['state']) |
| 416 | sw = {} |
| 417 | sw['name']=dpid |
| 418 | if str(v['state']) == "ACTIVE": |
| 419 | if dpid[-2:-1] == "a": |
| 420 | sw['group']=1 |
| 421 | if dpid[-2:-1] == "b": |
| 422 | sw['group']=2 |
| 423 | if dpid[-2:-1] == "c": |
| 424 | sw['group']=3 |
| 425 | if str(v['state']) == "INACTIVE": |
| 426 | sw['group']=0 |
| 427 | |
| 428 | switches.append(sw) |
| 429 | |
| 430 | try: |
| 431 | command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2) |
| 432 | result = os.popen(command).read() |
| 433 | parsedResult = json.loads(result) |
| 434 | except: |
| 435 | log_error("No route") |
| 436 | parsedResult = [] |
| 437 | # exit(1) |
| 438 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 439 | path = []; |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 440 | for i, v in enumerate(parsedResult): |
| 441 | if i < len(parsedResult) - 1: |
| 442 | sdpid= parsedResult[i]['switch'] |
| 443 | ddpid = parsedResult[i+1]['switch'] |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 444 | path.append( (sdpid, ddpid)) |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 445 | |
| 446 | try: |
| 447 | command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort) |
| 448 | result = os.popen(command).read() |
| 449 | parsedResult = json.loads(result) |
| 450 | except: |
| 451 | log_error("REST IF has issue: %s" % command) |
| 452 | log_error("%s" % result) |
| 453 | sys.exit(0) |
| 454 | |
| 455 | for v in parsedResult: |
| 456 | link = {} |
| 457 | if v.has_key('dst-switch'): |
| 458 | dst_dpid = str(v['dst-switch']) |
| 459 | dst_id = node_id(switches, dst_dpid) |
| 460 | if v.has_key('src-switch'): |
| 461 | src_dpid = str(v['src-switch']) |
| 462 | src_id = node_id(switches, src_dpid) |
| 463 | link['source'] = src_id |
| 464 | link['target'] = dst_id |
| 465 | onpath = 0 |
| 466 | for (s,d) in path: |
| 467 | if s == v['src-switch'] and d == v['dst-switch']: |
| 468 | onpath = 1 |
| 469 | break |
| 470 | |
| 471 | link['type'] = onpath |
| 472 | links.append(link) |
| 473 | |
| 474 | topo['nodes'] = switches |
| 475 | topo['links'] = links |
| 476 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 477 | # pp.pprint(topo) |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 478 | js = json.dumps(topo) |
| 479 | resp = Response(js, status=200, mimetype='application/json') |
| 480 | return resp |
| 481 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 482 | @app.route("/wm/core/controller/switches/json") |
| 483 | def query_switch(): |
| 484 | try: |
| 485 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 486 | # http://localhost:8080/wm/core/topology/switches/active/json |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 487 | print command |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 488 | result = os.popen(command).read() |
| 489 | parsedResult = json.loads(result) |
| 490 | except: |
| 491 | log_error("REST IF has issue: %s" % command) |
| 492 | log_error("%s" % result) |
| 493 | sys.exit(0) |
| 494 | |
| 495 | # print command |
| 496 | # print result |
| 497 | switches_ = [] |
| 498 | for v in parsedResult: |
| 499 | if v.has_key('dpid'): |
| 500 | if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 501 | dpid = str(v['dpid']) |
| 502 | state = str(v['state']) |
| 503 | sw = {} |
| 504 | sw['dpid']=dpid |
| 505 | sw['active']=state |
| 506 | switches_.append(sw) |
| 507 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 508 | # pp.pprint(switches_) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 509 | js = json.dumps(switches_) |
| 510 | resp = Response(js, status=200, mimetype='application/json') |
| 511 | return resp |
| 512 | |
| 513 | @app.route("/wm/device/") |
| 514 | def devices(): |
| 515 | try: |
| 516 | command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName) |
| 517 | result = os.popen(command).read() |
| 518 | parsedResult = json.loads(result)['results'] |
| 519 | except: |
| 520 | log_error("REST IF has issue: %s" % command) |
| 521 | log_error("%s" % result) |
| 522 | sys.exit(0) |
| 523 | |
| 524 | devices = [] |
| 525 | for v in parsedResult: |
| 526 | dl_addr = v['dl_addr'] |
| 527 | nw_addr = v['nw_addr'] |
| 528 | vertex = v['_id'] |
| 529 | mac = [] |
| 530 | mac.append(dl_addr) |
| 531 | ip = [] |
| 532 | ip.append(nw_addr) |
| 533 | device = {} |
| 534 | device['entryClass']="DefaultEntryClass" |
| 535 | device['mac']=mac |
| 536 | device['ipv4']=ip |
| 537 | device['vlan']=[] |
| 538 | device['lastSeen']=0 |
| 539 | attachpoints =[] |
| 540 | |
| 541 | port, dpid = deviceV_to_attachpoint(vertex) |
| 542 | attachpoint = {} |
| 543 | attachpoint['port']=port |
| 544 | attachpoint['switchDPID']=dpid |
| 545 | attachpoints.append(attachpoint) |
| 546 | device['attachmentPoint']=attachpoints |
| 547 | devices.append(device) |
| 548 | |
| 549 | print devices |
| 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 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 623 | topo_less = { |
| 624 | "nodes" : [ |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 625 | {"name" : "00:a0", "group" : 1}, |
| 626 | {"name" : "00:a1", "group" : 1}, |
| 627 | {"name" : "00:a2", "group" : 1}, |
| 628 | ], |
| 629 | "links" : [ |
| 630 | {"source" :0, "target": 1}, |
| 631 | {"source" :1, "target": 0}, |
| 632 | {"source" :0, "target": 2}, |
| 633 | {"source" :2, "target": 0}, |
| 634 | {"source" :1, "target": 2}, |
| 635 | {"source" :2, "target": 1}, |
| 636 | ] |
| 637 | } |
| 638 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 639 | topo_more = { |
| 640 | "nodes" : [ |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 641 | {"name" : "00:a3", "group" : 2}, |
| 642 | {"name" : "00:a0", "group" : 1}, |
| 643 | {"name" : "00:a1", "group" : 1}, |
| 644 | {"name" : "00:a2", "group" : 1}, |
| 645 | ], |
| 646 | "links" : [ |
| 647 | {"source" :1, "target": 2}, |
| 648 | {"source" :2, "target": 1}, |
| 649 | {"source" :1, "target": 3}, |
| 650 | {"source" :3, "target": 1}, |
| 651 | {"source" :2, "target": 3}, |
| 652 | {"source" :3, "target": 2}, |
| 653 | {"source" :0, "target": 2}, |
| 654 | ] |
| 655 | } |
| 656 | |
| 657 | @app.route("/topology_more") |
| 658 | def topology_more(): |
| 659 | topo = topo_more |
| 660 | js = json.dumps(topo) |
| 661 | resp = Response(js, status=200, mimetype='application/json') |
| 662 | return resp |
| 663 | |
| 664 | @app.route("/topology_less") |
| 665 | def topology_less(): |
| 666 | topo = topo_less |
| 667 | js = json.dumps(topo) |
| 668 | resp = Response(js, status=200, mimetype='application/json') |
| 669 | return resp |
| 670 | |
| 671 | cont_status1 = [ |
| 672 | {"name":"onos9vpc", "onos": 1, "cassandra": 1}, |
| 673 | {"name":"onos10vpc", "onos": 0, "cassandra": 1}, |
| 674 | {"name":"onos11vpc", "onos": 1, "cassandra": 0}, |
| 675 | {"name":"onos12vpc", "onos": 1, "cassandra": 0}] |
| 676 | |
| 677 | cont_status2 = [ |
| 678 | {"name":"onos9vpc", "onos": 0, "cassandra": 1}, |
| 679 | {"name":"onos10vpc", "onos": 0, "cassandra": 1}, |
| 680 | {"name":"onos11vpc", "onos": 0, "cassandra": 1}, |
| 681 | {"name":"onos12vpc", "onos": 0, "cassandra": 1}] |
| 682 | |
| 683 | @app.route("/controller_status1") |
| 684 | def controller_status1(): |
| 685 | status = cont_status1 |
| 686 | js = json.dumps(status) |
| 687 | resp = Response(js, status=200, mimetype='application/json') |
| 688 | pp.pprint(resp) |
| 689 | return resp |
| 690 | |
| 691 | @app.route("/controller_status2") |
| 692 | def controller_status2(): |
| 693 | status = cont_status2 |
| 694 | js = json.dumps(status) |
| 695 | resp = Response(js, status=200, mimetype='application/json') |
| 696 | pp.pprint(resp) |
| 697 | return resp |
| 698 | |
Ubuntu | c016ba1 | 2013-02-27 21:53:41 +0000 | [diff] [blame] | 699 | @app.route("/controller_status") |
| 700 | def controller_status(): |
| 701 | onos_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh status | awk '{print $1}'" |
| 702 | #cassandra_check="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-cassandra.sh status" |
| 703 | |
| 704 | cont_status=[] |
| 705 | for i in controllers: |
| 706 | status={} |
| 707 | onos=os.popen(onos_check % i).read()[:-1] |
| 708 | status["name"]=i |
| 709 | status["onos"]=onos |
Masayoshi Kobayashi | 5e91bdf | 2013-03-15 01:22:51 +0000 | [diff] [blame] | 710 | status["cassandra"]=0 |
Ubuntu | c016ba1 | 2013-02-27 21:53:41 +0000 | [diff] [blame] | 711 | cont_status.append(status) |
| 712 | |
| 713 | js = json.dumps(cont_status) |
| 714 | resp = Response(js, status=200, mimetype='application/json') |
| 715 | pp.pprint(js) |
| 716 | return resp |
| 717 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 718 | @app.route("/gui/controller/<cmd>/<controller_name>") |
| 719 | def controller_status_change(cmd, controller_name): |
| 720 | start_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh start" % (controller_name) |
| 721 | stop_onos="ssh -i ~/.ssh/onlabkey.pem %s ONOS/start-onos.sh stop" % (controller_name) |
| 722 | |
| 723 | if cmd == "up": |
Masayoshi Kobayashi | c0bc319 | 2013-03-27 23:12:03 +0000 | [diff] [blame] | 724 | print start_onos |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 725 | result=os.popen(start_onos).read() |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 726 | ret = "controller %s is up" % (controller_name) |
| 727 | elif cmd == "down": |
Masayoshi Kobayashi | c0bc319 | 2013-03-27 23:12:03 +0000 | [diff] [blame] | 728 | print stop_onos |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 729 | result=os.popen(stop_onos).read() |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 730 | ret = "controller %s is down" % (controller_name) |
| 731 | |
| 732 | return ret |
| 733 | |
| 734 | @app.route("/gui/switch/<cmd>/<dpid>") |
| 735 | def switch_status_change(cmd, dpid): |
| 736 | r = re.compile(':') |
| 737 | dpid = re.sub(r, '', dpid) |
Masayoshi Kobayashi | c0bc319 | 2013-03-27 23:12:03 +0000 | [diff] [blame] | 738 | host=controllers[0] |
| 739 | cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./switch.sh %s %s'" % (host, dpid, cmd) |
| 740 | 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] | 741 | print "cmd_string" |
| 742 | |
| 743 | if cmd =="up" or cmd=="down": |
| 744 | print "make dpid %s %s" % (dpid, cmd) |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 745 | os.popen(cmd_string) |
| 746 | result=os.popen(get_status).read() |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 747 | |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 748 | return result |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 749 | |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 750 | #* Link Up |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 751 | #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] | 752 | @app.route("/gui/link/up/<src_dpid>/<src_port>/<dst_dpid>/<dst_port>") |
| 753 | def link_up(src_dpid, src_port, dst_dpid, dst_port): |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 754 | |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 755 | cmd = 'up' |
| 756 | result="" |
| 757 | |
Paul Greyson | 472da4c | 2013-03-28 11:43:17 -0700 | [diff] [blame] | 758 | for dpid in (src_dpid, dst_dpid): |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 759 | if dpid in core_switches: |
| 760 | host = controllers[0] |
| 761 | src_ports = [1, 2, 3, 4, 5] |
| 762 | else: |
Masayoshi Kobayashi | 05f12b3 | 2013-04-01 09:08:09 +0000 | [diff] [blame] | 763 | hostid=int(dpid.split(':')[-2]) |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 764 | host = controllers[hostid-1] |
| 765 | if hostid == 2 : |
| 766 | src_ports = [51] |
| 767 | else : |
| 768 | src_ports = [26] |
| 769 | |
| 770 | for port in src_ports : |
| 771 | cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, dpid, port, cmd) |
| 772 | print cmd_string |
| 773 | res=os.popen(cmd_string).read() |
| 774 | result = result + ' ' + res |
| 775 | |
| 776 | return result |
| 777 | |
| 778 | |
| 779 | #* Link Down |
| 780 | #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] | 781 | @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] | 782 | def link_down(cmd, src_dpid, src_port, dst_dpid, dst_port): |
| 783 | |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 784 | if src_dpid in core_switches: |
| 785 | host = controllers[0] |
| 786 | else: |
| 787 | hostid=int(src_dpid.split(':')[-2]) |
| 788 | host = controllers[hostid-1] |
| 789 | |
| 790 | cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./link.sh %s %s %s'" % (host, src_dpid, src_port, cmd) |
| 791 | print cmd_string |
| 792 | |
Masayoshi Kobayashi | decab44 | 2013-03-28 11:19:13 +0000 | [diff] [blame] | 793 | result=os.popen(cmd_string).read() |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 794 | |
| 795 | return result |
| 796 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 797 | #* Create Flow |
| 798 | #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] | 799 | #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] | 800 | @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] | 801 | def add_flow(src_dpid, src_port, dst_dpid, dst_port, srcMAC, dstMAC): |
| 802 | command = "/home/ubuntu/ONOS/web/get_flow.py all |grep FlowPath |gawk '{print strtonum($4)}'| sort -n | tail -n 1" |
| 803 | print command |
| 804 | ret = os.popen(command).read() |
| 805 | if ret == "": |
| 806 | flow_nr=0 |
| 807 | else: |
| 808 | flow_nr=int(ret) |
| 809 | |
| 810 | flow_nr += 1 |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 811 | command = "/home/ubuntu/ONOS/web/add_flow.py %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 | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 812 | print command |
| 813 | errcode = os.popen(command).read() |
| 814 | return errcode |
| 815 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 816 | #* Delete Flow |
| 817 | #http://localhost:9000/gui/delflow/<flow_id> |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 818 | @app.route("/gui/delflow/<flow_id>") |
| 819 | def del_flow(flow_id): |
| 820 | command = "/home/ubuntu/ONOS/web/delete_flow.py %s" % (flow_id) |
| 821 | print command |
| 822 | errcode = os.popen(command).read() |
| 823 | return errcode |
| 824 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 825 | #* Start Iperf Througput |
Umesh Krishnaswamy | 6689be3 | 2013-03-27 18:12:26 -0700 | [diff] [blame] | 826 | #http://localhost:9000/gui/iperf/start/<flow_id>/<duration> |
| 827 | @app.route("/gui/iperf/start/<flow_id>/<duration>") |
| 828 | def iperf_start(flow_id,duration): |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 829 | try: |
| 830 | command = "curl -s http://%s:%s/wm/flow/get/%s/json\"" % (RESTIP, 9000, flow_id) |
| 831 | print command |
| 832 | result = os.popen(command).read() |
| 833 | if len(result) == 0: |
| 834 | print "No Flow found" |
| 835 | return; |
| 836 | parsedResult = json.load(result) |
| 837 | except: |
| 838 | print "REST IF has issue" |
| 839 | exit |
| 840 | |
| 841 | flowId = parsedResult['flowId']['value'] |
| 842 | src_dpid = parsedResult['dataPath']['srcPort']['dpid']['value'] |
| 843 | src_port = parsedResult['dataPath']['srcPort']['port']['value'] |
| 844 | dst_dpid = parsedResult['dataPath']['dstPort']['dpid']['value'] |
| 845 | dst_port = parsedResult['dataPath']['dstPort']['port']['value'] |
| 846 | print "FlowPath: (flowId = %s src = %s/%s dst = %s/%s" % (flowId, src_dpid, src_port, dst_dpid, dst_port) |
| 847 | |
| 848 | if src_dpid in core_switches: |
| 849 | host = controllers[0] |
| 850 | else: |
| 851 | hostid=int(src_dpid.split(':')[-2]) |
| 852 | host = controllers[hostid-1] |
| 853 | |
| 854 | cmd_string="ssh -i ~/.ssh/onlabkey.pem %s 'cd ONOS/scripts; ./runiperf.sh %s %s %s %s'" % (host, src_dpid, src_port, dst_dpid, dst_port) |
| 855 | print cmd_string |
| 856 | command = "/home/ubuntu/ONOS/web/scripts/iperf -t%s -i0.1 -yJ -o /tmp/iperf_%s.out -c 127.0.0.1 &" % (duration, flow_id) |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 857 | print command |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 858 | |
| 859 | return |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 860 | |
| 861 | |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 862 | #* Get Iperf Throughput |
| 863 | #http://localhost:9000/gui/iperf/rate/<flow_id> |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 864 | @app.route("/gui/iperf/rate/<flow_id>") |
| 865 | def iperf_rate(flow_id): |
| 866 | try: |
Masayoshi Kobayashi | fd56631 | 2013-04-01 10:34:01 +0000 | [diff] [blame] | 867 | command = "head -1 /home/ubuntu/ONOS/web/iperf.out" |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 868 | print command |
| 869 | result = os.popen(command).read() |
| 870 | except: |
| 871 | print "REST IF has issue" |
| 872 | exit |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 873 | |
Masayoshi Kobayashi | 911f263 | 2013-04-01 18:44:33 +0000 | [diff] [blame] | 874 | resp = Response(result, status=200, mimetype='application/json') |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 875 | return resp |
Masayoshi Kobayashi | 5101152 | 2013-03-27 00:18:12 +0000 | [diff] [blame] | 876 | |
| 877 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 878 | if __name__ == "__main__": |
| 879 | if len(sys.argv) > 1 and sys.argv[1] == "-d": |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 880 | # 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") |
| 881 | # link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1) |
| 882 | # link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1) |
| 883 | # link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1) |
| 884 | # link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1) |
| 885 | |
| 886 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 887 | print "-- query all switches --" |
| 888 | query_switch() |
| 889 | print "-- query topo --" |
| 890 | topology_for_gui() |
Masayoshi Kobayashi | 1e07238 | 2013-03-27 05:17:09 +0000 | [diff] [blame] | 891 | # link_change(1,2,3,4) |
| 892 | print "-- query all links --" |
| 893 | query_links() |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 894 | # print "-- query all devices --" |
| 895 | # devices() |
| 896 | else: |
| 897 | app.debug = True |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 898 | app.run(threaded=True, host="0.0.0.0", port=9000) |