Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 2 | import os |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 3 | import json |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 4 | from urllib2 import Request, urlopen, URLError, HTTPError |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 5 | from flask import Flask, json, Response, render_template, make_response, request |
| 6 | |
Jonathan Hart | e123942 | 2014-04-28 05:58:42 -0700 | [diff] [blame] | 7 | CONFIG_FILE="config.json" |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 8 | |
Naoki Shiota | d5d0e94 | 2014-03-27 18:57:32 -0700 | [diff] [blame] | 9 | ## Global Var for this proxy script setting. |
| 10 | # "0.0.0.0" means any interface |
Jonathan Hart | e123942 | 2014-04-28 05:58:42 -0700 | [diff] [blame] | 11 | GuiIp="0.0.0.0" |
| 12 | GuiPort=9000 |
Naoki Shiota | d5d0e94 | 2014-03-27 18:57:32 -0700 | [diff] [blame] | 13 | |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 14 | ## Global Var for ON.Lab local REST ## |
Jonathan Hart | e123942 | 2014-04-28 05:58:42 -0700 | [diff] [blame] | 15 | # The GUI can be accessed at <this_host>:9000/onos-topology.html |
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 |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 18 | ONOS_DEFAULT_HOST="localhost" ;# Has to set if LB=False |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 19 | DEBUG=1 |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 20 | |
Masayoshi Kobayashi | 8158d44 | 2013-04-09 02:43:39 +0000 | [diff] [blame] | 21 | app = Flask(__name__) |
| 22 | |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 23 | def read_config(): |
Jonathan Hart | b255448 | 2013-04-08 13:40:31 -0700 | [diff] [blame] | 24 | 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] | 25 | f = open(CONFIG_FILE) |
| 26 | conf = json.load(f) |
| 27 | LB = conf['LB'] |
Jonathan Hart | b255448 | 2013-04-08 13:40:31 -0700 | [diff] [blame] | 28 | TESTBED = conf['TESTBED'] |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 29 | controllers = conf['controllers'] |
Jonathan Hart | b255448 | 2013-04-08 13:40:31 -0700 | [diff] [blame] | 30 | core_switches=conf['core_switches'] |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 31 | ONOS_GUI3_HOST=conf['ONOS_GUI3_HOST'] |
| 32 | ONOS_GUI3_CONTROL_HOST=conf['ONOS_GUI3_CONTROL_HOST'] |
| 33 | f.close() |
Tim Lindberg | 201ade2 | 2013-04-05 11:52:08 -0700 | [diff] [blame] | 34 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 35 | ## Worker Functions ## |
| 36 | def log_error(txt): |
| 37 | print '%s' % (txt) |
| 38 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 39 | ### File Fetch ### |
| 40 | @app.route('/ui/img/<filename>', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 41 | @app.route('/js/<filename>', methods=['GET']) |
Masayoshi Kobayashi | 3d04931 | 2013-04-02 22:15:16 +0000 | [diff] [blame] | 42 | @app.route('/log/<filename>', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 43 | @app.route('/', methods=['GET']) |
| 44 | @app.route('/<filename>', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 45 | def return_file(filename="index.html"): |
| 46 | if request.path == "/": |
Jonathan Hart | e123942 | 2014-04-28 05:58:42 -0700 | [diff] [blame] | 47 | fullpath = "./onos-topology.html" |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 48 | else: |
| 49 | fullpath = str(request.path)[1:] |
| 50 | |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 51 | try: |
Masayoshi Kobayashi | 03e64b4 | 2013-04-05 05:56:27 +0000 | [diff] [blame] | 52 | open(fullpath) |
| 53 | except: |
| 54 | response = make_response("Cannot find a file: %s" % (fullpath), 500) |
| 55 | response.headers["Content-type"] = "text/html" |
| 56 | return response |
| 57 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 58 | response = make_response(open(fullpath).read()) |
| 59 | suffix = fullpath.split(".")[-1] |
| 60 | |
| 61 | if suffix == "html" or suffix == "htm": |
| 62 | response.headers["Content-type"] = "text/html" |
| 63 | elif suffix == "js": |
| 64 | response.headers["Content-type"] = "application/javascript" |
| 65 | elif suffix == "css": |
| 66 | response.headers["Content-type"] = "text/css" |
| 67 | elif suffix == "png": |
| 68 | response.headers["Content-type"] = "image/png" |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 69 | elif suffix == "svg": |
| 70 | response.headers["Content-type"] = "image/svg+xml" |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 71 | |
| 72 | return response |
| 73 | |
Tim Lindberg | 9ec5e22 | 2013-04-12 10:46:02 -0700 | [diff] [blame] | 74 | ###### ONOS REST API ############################## |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 75 | ## Worker Func ### |
| 76 | def get_json(url): |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 77 | code = 200; |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 78 | try: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 79 | response = urlopen(url) |
| 80 | except URLError, e: |
Jonathan Hart | e123942 | 2014-04-28 05:58:42 -0700 | [diff] [blame] | 81 | log_error("get_json: REST IF %s has issue. Reason: %s" % (url, e.reason)) |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 82 | result = "" |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 83 | return (500, result) |
| 84 | except HTTPError, e: |
Jonathan Hart | e123942 | 2014-04-28 05:58:42 -0700 | [diff] [blame] | 85 | log_error("get_json: REST IF %s has issue. Code %s" % (url, e.code)) |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 86 | result = "" |
| 87 | return (e.code, result) |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 88 | |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 89 | result = response.read() |
Masayoshi Kobayashi | 2ae891a | 2013-04-05 02:16:19 +0000 | [diff] [blame] | 90 | return (code, result) |
| 91 | |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 92 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 93 | def node_id(switch_array, dpid): |
| 94 | id = -1 |
| 95 | for i, val in enumerate(switch_array): |
| 96 | if val['name'] == dpid: |
| 97 | id = i |
| 98 | break |
| 99 | |
| 100 | return id |
| 101 | |
Masayoshi Kobayashi | 13e2ebe | 2013-03-26 18:38:41 +0000 | [diff] [blame] | 102 | ## API for ON.Lab local GUI ## |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame] | 103 | @app.route('/topology', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 104 | def topology_for_gui(): |
| 105 | try: |
Jonathan Hart | 5caa044 | 2014-03-19 15:20:07 -0700 | [diff] [blame] | 106 | url="http://%s:%s/wm/onos/topology/switches/json" % (RestIP, RestPort) |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 107 | (code, result) = get_json(url) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 108 | parsedResult = json.loads(result) |
| 109 | except: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 110 | log_error("REST IF has issue: %s" % url) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 111 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 112 | return |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 113 | |
| 114 | topo = {} |
| 115 | switches = [] |
| 116 | links = [] |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 117 | devices = [] |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 118 | |
| 119 | for v in parsedResult: |
| 120 | if v.has_key('dpid'): |
| 121 | # if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 122 | dpid = str(v['dpid']) |
| 123 | state = str(v['state']) |
| 124 | sw = {} |
| 125 | sw['name']=dpid |
Ubuntu | 5b2b24a | 2013-02-27 09:51:13 +0000 | [diff] [blame] | 126 | sw['group']= -1 |
Ubuntu | 37ebda6 | 2013-03-01 00:35:31 +0000 | [diff] [blame] | 127 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 128 | if state == "INACTIVE": |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 129 | sw['group']=0 |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 130 | switches.append(sw) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 131 | |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 132 | try: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 133 | url="http://%s:%s/wm/onos/registry/switches/json" % (RestIP, RestPort) |
| 134 | (code, result) = get_json(url) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 135 | parsedResult = json.loads(result) |
| 136 | except: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 137 | log_error("REST IF has issue: %s" % url) |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 138 | log_error("%s" % result) |
| 139 | |
| 140 | for key in parsedResult: |
| 141 | dpid = key |
| 142 | ctrl = parsedResult[dpid][0]['controllerId'] |
| 143 | sw_id = node_id(switches, dpid) |
| 144 | if sw_id != -1: |
| 145 | if switches[sw_id]['group'] != 0: |
| 146 | switches[sw_id]['group'] = controllers.index(ctrl) + 1 |
| 147 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 148 | try: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 149 | url = "http://%s:%s/wm/onos/topology/links/json" % (RestIP, RestPort) |
| 150 | (code, result) = get_json(url) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 151 | parsedResult = json.loads(result) |
| 152 | except: |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 153 | log_error("REST IF has issue: %s" % url) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 154 | log_error("%s" % result) |
Masayoshi Kobayashi | 7fa3fb8 | 2013-06-20 18:10:46 -0700 | [diff] [blame] | 155 | return |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 156 | |
| 157 | for v in parsedResult: |
| 158 | link = {} |
| 159 | if v.has_key('dst-switch'): |
| 160 | dst_dpid = str(v['dst-switch']) |
| 161 | dst_id = node_id(switches, dst_dpid) |
| 162 | if v.has_key('src-switch'): |
| 163 | src_dpid = str(v['src-switch']) |
| 164 | src_id = node_id(switches, src_dpid) |
| 165 | link['source'] = src_id |
| 166 | link['target'] = dst_id |
Masayoshi Kobayashi | 3bc5fde | 2013-02-28 01:02:54 +0000 | [diff] [blame] | 167 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 168 | links.append(link) |
| 169 | |
| 170 | topo['nodes'] = switches |
| 171 | topo['links'] = links |
| 172 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 173 | js = json.dumps(topo) |
| 174 | resp = Response(js, status=200, mimetype='application/json') |
| 175 | return resp |
| 176 | |
Masayoshi Kobayashi | bcb03c0 | 2014-01-22 15:18:49 -0800 | [diff] [blame] | 177 | @app.route("/controller_status") |
| 178 | def controller_status(): |
Masayoshi Kobayashi | 640ad69 | 2014-01-22 23:37:59 -0800 | [diff] [blame] | 179 | url= "http://%s:%d/wm/onos/registry/controllers/json" % (RestIP, RestPort) |
Masayoshi Kobayashi | bcb03c0 | 2014-01-22 15:18:49 -0800 | [diff] [blame] | 180 | (code, result) = get_json(url) |
| 181 | parsedResult = json.loads(result) |
| 182 | |
| 183 | cont_status=[] |
| 184 | for i in controllers: |
| 185 | status={} |
| 186 | if i in parsedResult: |
| 187 | onos=1 |
| 188 | else: |
| 189 | onos=0 |
| 190 | status["name"]=i |
| 191 | status["onos"]=onos |
Masayoshi Kobayashi | bcb03c0 | 2014-01-22 15:18:49 -0800 | [diff] [blame] | 192 | cont_status.append(status) |
| 193 | |
| 194 | js = json.dumps(cont_status) |
| 195 | resp = Response(js, status=200, mimetype='application/json') |
| 196 | return resp |
| 197 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 198 | if __name__ == "__main__": |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 199 | read_config() |
Jonathan Hart | e123942 | 2014-04-28 05:58:42 -0700 | [diff] [blame] | 200 | app.debug = True |
| 201 | app.run(threaded=True, host=GuiIp, port=GuiPort) |