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 | |
Naoki Shiota | d5d0e94 | 2014-03-27 18:57:32 -0700 | [diff] [blame] | 20 | ## Global Var for this proxy script setting. |
| 21 | # "0.0.0.0" means any interface |
| 22 | ProxyIP="0.0.0.0" |
| 23 | ProxyPort=9000 |
| 24 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 25 | ## Global Var for ON.Lab local REST ## |
Ubuntu | f6ce96c | 2013-02-07 01:45:07 +0000 | [diff] [blame] | 26 | RestIP="localhost" |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 27 | RestPort=8080 |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 28 | ONOS_DEFAULT_HOST="localhost" ;# Has to set if LB=False |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 29 | DEBUG=1 |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 30 | |
Masayoshi Kobayashi | 8158d44 | 2013-04-09 02:43:39 +0000 | [diff] [blame] | 31 | pp = pprint.PrettyPrinter(indent=4) |
| 32 | app = Flask(__name__) |
| 33 | |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 34 | def read_config(): |
Jonathan Hart | b255448 | 2013-04-08 13:40:31 -0700 | [diff] [blame] | 35 | 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] | 36 | f = open(CONFIG_FILE) |
| 37 | conf = json.load(f) |
| 38 | LB = conf['LB'] |
Jonathan Hart | b255448 | 2013-04-08 13:40:31 -0700 | [diff] [blame] | 39 | TESTBED = conf['TESTBED'] |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 40 | controllers = conf['controllers'] |
Jonathan Hart | b255448 | 2013-04-08 13:40:31 -0700 | [diff] [blame] | 41 | core_switches=conf['core_switches'] |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 42 | ONOS_GUI3_HOST=conf['ONOS_GUI3_HOST'] |
| 43 | ONOS_GUI3_CONTROL_HOST=conf['ONOS_GUI3_CONTROL_HOST'] |
| 44 | f.close() |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 45 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 46 | ## Worker Functions ## |
| 47 | def log_error(txt): |
| 48 | print '%s' % (txt) |
| 49 | |
| 50 | def debug(txt): |
| 51 | if DEBUG: |
| 52 | print '%s' % (txt) |
| 53 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 54 | ### File Fetch ### |
| 55 | @app.route('/ui/img/<filename>', methods=['GET']) |
| 56 | @app.route('/img/<filename>', methods=['GET']) |
| 57 | @app.route('/css/<filename>', methods=['GET']) |
| 58 | @app.route('/js/models/<filename>', methods=['GET']) |
| 59 | @app.route('/js/views/<filename>', methods=['GET']) |
| 60 | @app.route('/js/<filename>', methods=['GET']) |
| 61 | @app.route('/lib/<filename>', methods=['GET']) |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 62 | @app.route('/log/<filename>', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 63 | @app.route('/', methods=['GET']) |
| 64 | @app.route('/<filename>', methods=['GET']) |
| 65 | @app.route('/tpl/<filename>', methods=['GET']) |
| 66 | def return_file(filename="index.html"): |
| 67 | if request.path == "/": |
| 68 | fullpath = "./index.html" |
| 69 | else: |
| 70 | fullpath = str(request.path)[1:] |
| 71 | |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 72 | try: |
Masayoshi Kobayashi | 03e64b4 | 2013-04-05 05:56:27 +0000 | [diff] [blame] | 73 | open(fullpath) |
| 74 | except: |
| 75 | response = make_response("Cannot find a file: %s" % (fullpath), 500) |
| 76 | response.headers["Content-type"] = "text/html" |
| 77 | return response |
| 78 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 79 | response = make_response(open(fullpath).read()) |
| 80 | suffix = fullpath.split(".")[-1] |
| 81 | |
| 82 | if suffix == "html" or suffix == "htm": |
| 83 | response.headers["Content-type"] = "text/html" |
| 84 | elif suffix == "js": |
| 85 | response.headers["Content-type"] = "application/javascript" |
| 86 | elif suffix == "css": |
| 87 | response.headers["Content-type"] = "text/css" |
| 88 | elif suffix == "png": |
| 89 | response.headers["Content-type"] = "image/png" |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 90 | elif suffix == "svg": |
| 91 | response.headers["Content-type"] = "image/svg+xml" |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 92 | |
| 93 | return response |
| 94 | |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 95 | ###### ONOS REST API ############################## |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 96 | ## Worker Func ### |
| 97 | def get_json(url): |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 98 | code = 200; |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 99 | try: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 100 | response = urlopen(url) |
| 101 | except URLError, e: |
| 102 | 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] | 103 | result = "" |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 104 | return (500, result) |
| 105 | except HTTPError, e: |
| 106 | print "get_json: REST IF %s has issue. Code %s" % (url, e.code) |
| 107 | result = "" |
| 108 | return (e.code, result) |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 109 | |
Naoki Shiota | d5d0e94 | 2014-03-27 18:57:32 -0700 | [diff] [blame] | 110 | print response |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 111 | result = response.read() |
| 112 | # parsedResult = json.loads(result) |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 113 | return (code, result) |
| 114 | |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 115 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 116 | def node_id(switch_array, dpid): |
| 117 | id = -1 |
| 118 | for i, val in enumerate(switch_array): |
| 119 | if val['name'] == dpid: |
| 120 | id = i |
| 121 | break |
| 122 | |
| 123 | return id |
| 124 | |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 125 | ## API for ON.Lab local GUI ## |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 126 | @app.route('/topology', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 127 | def topology_for_gui(): |
| 128 | try: |
Jonathan Hart | 5caa044 | 2014-03-19 15:20:07 -0700 | [diff] [blame] | 129 | url="http://%s:%s/wm/onos/topology/switches/json" % (RestIP, RestPort) |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 130 | (code, result) = get_json(url) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 131 | parsedResult = json.loads(result) |
| 132 | except: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 133 | log_error("REST IF has issue: %s" % url) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 134 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 135 | return |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 136 | |
| 137 | topo = {} |
| 138 | switches = [] |
| 139 | links = [] |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 140 | devices = [] |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 141 | |
| 142 | for v in parsedResult: |
| 143 | if v.has_key('dpid'): |
| 144 | # if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 145 | dpid = str(v['dpid']) |
| 146 | state = str(v['state']) |
| 147 | sw = {} |
| 148 | sw['name']=dpid |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 149 | sw['group']= -1 |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 150 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 151 | if state == "INACTIVE": |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 152 | sw['group']=0 |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 153 | switches.append(sw) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 154 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 155 | try: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 156 | url="http://%s:%s/wm/onos/registry/switches/json" % (RestIP, RestPort) |
| 157 | (code, result) = get_json(url) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 158 | parsedResult = json.loads(result) |
| 159 | except: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 160 | log_error("REST IF has issue: %s" % url) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 161 | log_error("%s" % result) |
| 162 | |
| 163 | for key in parsedResult: |
| 164 | dpid = key |
| 165 | ctrl = parsedResult[dpid][0]['controllerId'] |
| 166 | sw_id = node_id(switches, dpid) |
| 167 | if sw_id != -1: |
| 168 | if switches[sw_id]['group'] != 0: |
| 169 | switches[sw_id]['group'] = controllers.index(ctrl) + 1 |
| 170 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 171 | try: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 172 | url = "http://%s:%s/wm/onos/topology/links/json" % (RestIP, RestPort) |
| 173 | (code, result) = get_json(url) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 174 | parsedResult = json.loads(result) |
| 175 | except: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 176 | log_error("REST IF has issue: %s" % url) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 177 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 178 | return |
| 179 | # sys.exit(0) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 180 | |
| 181 | for v in parsedResult: |
| 182 | link = {} |
| 183 | if v.has_key('dst-switch'): |
| 184 | dst_dpid = str(v['dst-switch']) |
| 185 | dst_id = node_id(switches, dst_dpid) |
| 186 | if v.has_key('src-switch'): |
| 187 | src_dpid = str(v['src-switch']) |
| 188 | src_id = node_id(switches, src_dpid) |
| 189 | link['source'] = src_id |
| 190 | link['target'] = dst_id |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 191 | |
Jonathan Hart | bf66dff | 2013-10-28 14:07:41 -0700 | [diff] [blame] | 192 | #onpath = 0 |
| 193 | #for (s,d) in path: |
| 194 | # if s == v['src-switch'] and d == v['dst-switch']: |
| 195 | # onpath = 1 |
| 196 | # break |
| 197 | #link['type'] = onpath |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 198 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 199 | links.append(link) |
| 200 | |
| 201 | topo['nodes'] = switches |
| 202 | topo['links'] = links |
| 203 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 204 | js = json.dumps(topo) |
| 205 | resp = Response(js, status=200, mimetype='application/json') |
| 206 | return resp |
| 207 | |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 208 | @app.route("/wm/floodlight/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json") |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 209 | def shortest_path(v1, p1, v2, p2): |
| 210 | try: |
Jonathan Hart | 5caa044 | 2014-03-19 15:20:07 -0700 | [diff] [blame] | 211 | url = "http://%s:%s/wm/onos/topology/switches/json" % (RestIP, RestPort) |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 212 | (code, result) = get_json(url) |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 213 | parsedResult = json.loads(result) |
| 214 | except: |
| 215 | log_error("REST IF has issue: %s" % command) |
| 216 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 217 | return |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 218 | |
| 219 | topo = {} |
| 220 | switches = [] |
| 221 | links = [] |
| 222 | |
| 223 | for v in parsedResult: |
| 224 | if v.has_key('dpid'): |
| 225 | dpid = str(v['dpid']) |
| 226 | state = str(v['state']) |
| 227 | sw = {} |
| 228 | sw['name']=dpid |
| 229 | if str(v['state']) == "ACTIVE": |
| 230 | if dpid[-2:-1] == "a": |
| 231 | sw['group']=1 |
| 232 | if dpid[-2:-1] == "b": |
| 233 | sw['group']=2 |
| 234 | if dpid[-2:-1] == "c": |
| 235 | sw['group']=3 |
| 236 | if str(v['state']) == "INACTIVE": |
| 237 | sw['group']=0 |
| 238 | |
| 239 | switches.append(sw) |
| 240 | |
| 241 | try: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 242 | url = "http://%s:%s/wm/onos/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2) |
| 243 | (code, result) = get_json(url) |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 244 | parsedResult = json.loads(result) |
| 245 | except: |
| 246 | log_error("No route") |
| 247 | parsedResult = [] |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 248 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 249 | path = []; |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 250 | for i, v in enumerate(parsedResult): |
| 251 | if i < len(parsedResult) - 1: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 252 | sdpid= parsedResult['flowEntries'][i]['dpid']['value'] |
| 253 | ddpid= parsedResult['flowEntries'][i+1]['dpid']['value'] |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 254 | path.append( (sdpid, ddpid)) |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 255 | |
| 256 | try: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 257 | url = "http://%s:%s/wm/onos/topology/links/json" % (RestIP, RestPort) |
| 258 | (code, result) = get_json(url) |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 259 | parsedResult = json.loads(result) |
| 260 | except: |
| 261 | log_error("REST IF has issue: %s" % command) |
| 262 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 263 | return |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 264 | |
| 265 | for v in parsedResult: |
| 266 | link = {} |
| 267 | if v.has_key('dst-switch'): |
| 268 | dst_dpid = str(v['dst-switch']) |
| 269 | dst_id = node_id(switches, dst_dpid) |
| 270 | if v.has_key('src-switch'): |
| 271 | src_dpid = str(v['src-switch']) |
| 272 | src_id = node_id(switches, src_dpid) |
| 273 | link['source'] = src_id |
| 274 | link['target'] = dst_id |
| 275 | onpath = 0 |
| 276 | for (s,d) in path: |
| 277 | if s == v['src-switch'] and d == v['dst-switch']: |
| 278 | onpath = 1 |
| 279 | break |
| 280 | |
| 281 | link['type'] = onpath |
| 282 | links.append(link) |
| 283 | |
| 284 | topo['nodes'] = switches |
| 285 | topo['links'] = links |
| 286 | |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 287 | js = json.dumps(topo) |
| 288 | resp = Response(js, status=200, mimetype='application/json') |
| 289 | return resp |
| 290 | |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 291 | @app.route("/wm/floodlight/core/controller/switches/json") |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 292 | def query_switch(): |
| 293 | try: |
Jonathan Hart | 5caa044 | 2014-03-19 15:20:07 -0700 | [diff] [blame] | 294 | url = "http://%s:%s/wm/onos/topology/switches/json" % (RestIP, RestPort) |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 295 | (code, result) = get_json(url) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 296 | parsedResult = json.loads(result) |
| 297 | except: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 298 | log_error("REST IF has issue: %s" % url) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 299 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 300 | return |
| 301 | # sys.exit(0) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 302 | |
| 303 | # print command |
| 304 | # print result |
| 305 | switches_ = [] |
| 306 | for v in parsedResult: |
| 307 | if v.has_key('dpid'): |
| 308 | if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 309 | dpid = str(v['dpid']) |
| 310 | state = str(v['state']) |
| 311 | sw = {} |
| 312 | sw['dpid']=dpid |
| 313 | sw['active']=state |
| 314 | switches_.append(sw) |
| 315 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 316 | # pp.pprint(switches_) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 317 | js = json.dumps(switches_) |
| 318 | resp = Response(js, status=200, mimetype='application/json') |
| 319 | return resp |
| 320 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 321 | ## return fake stat for now |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 322 | @app.route("/wm/floodlight/core/switch/<switchId>/<statType>/json") |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 323 | def switch_stat(switchId, statType): |
| 324 | if statType == "desc": |
| 325 | desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}] |
| 326 | ret = {} |
| 327 | ret[switchId]=desc |
| 328 | elif statType == "aggregate": |
| 329 | aggr = {"packetCount":0,"byteCount":0,"flowCount":0} |
| 330 | ret = {} |
| 331 | ret[switchId]=aggr |
| 332 | else: |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 333 | ret = {} |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 334 | |
| 335 | js = json.dumps(ret) |
| 336 | resp = Response(js, status=200, mimetype='application/json') |
| 337 | return resp |
| 338 | |
Masayoshi Kobayashi | bcb03c0 | 2014-01-22 15:18:49 -0800 | [diff] [blame] | 339 | @app.route("/controller_status") |
| 340 | def controller_status(): |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 341 | url= "http://%s:%d/wm/onos/registry/controllers/json" % (RestIP, RestPort) |
Masayoshi Kobayashi | bcb03c0 | 2014-01-22 15:18:49 -0800 | [diff] [blame] | 342 | (code, result) = get_json(url) |
| 343 | parsedResult = json.loads(result) |
| 344 | |
| 345 | cont_status=[] |
| 346 | for i in controllers: |
| 347 | status={} |
| 348 | if i in parsedResult: |
| 349 | onos=1 |
| 350 | else: |
| 351 | onos=0 |
| 352 | status["name"]=i |
| 353 | status["onos"]=onos |
| 354 | status["cassandra"]=0 |
| 355 | cont_status.append(status) |
| 356 | |
| 357 | js = json.dumps(cont_status) |
| 358 | resp = Response(js, status=200, mimetype='application/json') |
| 359 | return resp |
| 360 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 361 | if __name__ == "__main__": |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 362 | random.seed() |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 363 | read_config() |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 364 | if len(sys.argv) > 1 and sys.argv[1] == "-d": |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 365 | # for debugging |
| 366 | #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") |
| 367 | #proxy_link_change("up", "00:00:00:00:ba:5e:ba:11", 1, "00:00:00:00:00:00:00:00", 1) |
| 368 | #proxy_link_change("down", "00:00:20:4e:7f:51:8a:35", 1, "00:00:00:00:00:00:00:00", 1) |
| 369 | #proxy_link_change("up", "00:00:00:00:00:00:02:03", 1, "00:00:00:00:00:00:00:00", 1) |
| 370 | #proxy_link_change("down", "00:00:00:00:00:00:07:12", 1, "00:00:00:00:00:00:00:00", 1) |
| 371 | #print "-- query all switches --" |
| 372 | #query_switch() |
| 373 | #print "-- query topo --" |
| 374 | #topology_for_gui() |
| 375 | ##print "-- query all links --" |
| 376 | ##query_links() |
| 377 | #print "-- query all devices --" |
| 378 | #devices() |
| 379 | #links() |
| 380 | #switches() |
| 381 | #reset_demo() |
| 382 | pass |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 383 | else: |
| 384 | app.debug = True |
Naoki Shiota | d5d0e94 | 2014-03-27 18:57:32 -0700 | [diff] [blame] | 385 | app.run(threaded=True, host=ProxyIP, port=ProxyPort) |