blob: 66ac46356e00520f0439c65be2c8b0c04f751edd [file] [log] [blame]
Ubuntu82b8a832013-02-06 22:00:11 +00001#! /usr/bin/env python
Ubuntu82b8a832013-02-06 22:00:11 +00002import os
Jonathan Hart715ddb72014-04-29 06:34:50 -07003import sys
Ubuntu82b8a832013-02-06 22:00:11 +00004import json
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -08005from urllib2 import Request, urlopen, URLError, HTTPError
Ubuntu82b8a832013-02-06 22:00:11 +00006from flask import Flask, json, Response, render_template, make_response, request
7
Jonathan Harte1239422014-04-28 05:58:42 -07008# The GUI can be accessed at <this_host>:9000/onos-topology.html
Jonathan Hart715ddb72014-04-29 06:34:50 -07009
10WEB_DIR = os.path.dirname(os.path.realpath(__file__))
11
12LOCAL_CONFIG_FILE = os.path.join(WEB_DIR, "config.json")
13DEFAULT_CONFIG_FILE = os.path.join(WEB_DIR, "config.json.default")
Ubuntu82b8a832013-02-06 22:00:11 +000014
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +000015app = Flask(__name__)
16
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000017def read_config():
Jonathan Hart715ddb72014-04-29 06:34:50 -070018 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 Lindberg201ade22013-04-05 11:52:08 -070042
Ubuntu82b8a832013-02-06 22:00:11 +000043## Worker Functions ##
44def log_error(txt):
45 print '%s' % (txt)
46
Ubuntu82b8a832013-02-06 22:00:11 +000047### File Fetch ###
48@app.route('/ui/img/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000049@app.route('/js/<filename>', methods=['GET'])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +000050@app.route('/log/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000051@app.route('/', methods=['GET'])
52@app.route('/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000053def return_file(filename="index.html"):
54 if request.path == "/":
Jonathan Hart715ddb72014-04-29 06:34:50 -070055 fullpath = os.path.join(WEB_DIR, "onos-topology.html")
Ubuntu82b8a832013-02-06 22:00:11 +000056 else:
Jonathan Hart715ddb72014-04-29 06:34:50 -070057 fullpath = os.path.join(WEB_DIR, str(request.path)[1:])
Ubuntu82b8a832013-02-06 22:00:11 +000058
Paul Greysonc090d142013-04-09 16:59:03 -070059 try:
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +000060 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
Ubuntu82b8a832013-02-06 22:00:11 +000066 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 Greyson2913af82013-03-27 14:53:17 -070077 elif suffix == "svg":
78 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000079
80 return response
81
Tim Lindberg9ec5e222013-04-12 10:46:02 -070082###### ONOS REST API ##############################
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000083## Worker Func ###
84def get_json(url):
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -080085 code = 200;
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000086 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -080087 response = urlopen(url)
88 except URLError, e:
Jonathan Harte1239422014-04-28 05:58:42 -070089 log_error("get_json: REST IF %s has issue. Reason: %s" % (url, e.reason))
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000090 result = ""
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -080091 return (500, result)
92 except HTTPError, e:
Jonathan Harte1239422014-04-28 05:58:42 -070093 log_error("get_json: REST IF %s has issue. Code %s" % (url, e.code))
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -080094 result = ""
95 return (e.code, result)
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000096
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -080097 result = response.read()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000098 return (code, result)
99
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +0000100
Ubuntu82b8a832013-02-06 22:00:11 +0000101def 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 Kobayashi13e2ebe2013-03-26 18:38:41 +0000110## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000111@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000112def topology_for_gui():
113 try:
Jonathan Hart715ddb72014-04-29 06:34:50 -0700114 url="http://%s:%s/wm/onos/topology/switches/json" % (onosIp, onosPort)
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800115 (code, result) = get_json(url)
Ubuntu82b8a832013-02-06 22:00:11 +0000116 parsedResult = json.loads(result)
117 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800118 log_error("REST IF has issue: %s" % url)
Ubuntu82b8a832013-02-06 22:00:11 +0000119 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700120 return
Ubuntu82b8a832013-02-06 22:00:11 +0000121
122 topo = {}
123 switches = []
124 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000125 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000126
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
Ubuntu5b2b24a2013-02-27 09:51:13 +0000134 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000135
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000136 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000137 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000138 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000139
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000140 try:
Jonathan Hart715ddb72014-04-29 06:34:50 -0700141 url="http://%s:%s/wm/onos/registry/switches/json" % (onosIp, onosPort)
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800142 (code, result) = get_json(url)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000143 parsedResult = json.loads(result)
144 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800145 log_error("REST IF has issue: %s" % url)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000146 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
Ubuntu82b8a832013-02-06 22:00:11 +0000156 try:
Jonathan Hart715ddb72014-04-29 06:34:50 -0700157 url = "http://%s:%s/wm/onos/topology/links/json" % (onosIp, onosPort)
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800158 (code, result) = get_json(url)
Ubuntu82b8a832013-02-06 22:00:11 +0000159 parsedResult = json.loads(result)
160 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800161 log_error("REST IF has issue: %s" % url)
Ubuntu82b8a832013-02-06 22:00:11 +0000162 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700163 return
Ubuntu82b8a832013-02-06 22:00:11 +0000164
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 Kobayashi3bc5fde2013-02-28 01:02:54 +0000175
Ubuntu82b8a832013-02-06 22:00:11 +0000176 links.append(link)
177
178 topo['nodes'] = switches
179 topo['links'] = links
180
Ubuntu82b8a832013-02-06 22:00:11 +0000181 js = json.dumps(topo)
182 resp = Response(js, status=200, mimetype='application/json')
183 return resp
184
Masayoshi Kobayashibcb03c02014-01-22 15:18:49 -0800185@app.route("/controller_status")
186def controller_status():
Jonathan Hart715ddb72014-04-29 06:34:50 -0700187 url= "http://%s:%d/wm/onos/registry/controllers/json" % (onosIp, onosPort)
Masayoshi Kobayashibcb03c02014-01-22 15:18:49 -0800188 (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 Kobayashibcb03c02014-01-22 15:18:49 -0800200 cont_status.append(status)
201
202 js = json.dumps(cont_status)
203 resp = Response(js, status=200, mimetype='application/json')
204 return resp
205
Ubuntu82b8a832013-02-06 22:00:11 +0000206if __name__ == "__main__":
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +0000207 read_config()
Jonathan Hart715ddb72014-04-29 06:34:50 -0700208 #app.debug = True
209 app.run(threaded=True, host=guiIp, port=guiPort)