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