blob: 703fddb6371e3b1c1ac51ee0d70374fe52de72bb [file] [log] [blame]
Ubuntu82b8a832013-02-06 22:00:11 +00001#! /usr/bin/env python
Ubuntu82b8a832013-02-06 22:00:11 +00002import os
Ubuntu82b8a832013-02-06 22:00:11 +00003import json
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -08004from urllib2 import Request, urlopen, URLError, HTTPError
Ubuntu82b8a832013-02-06 22:00:11 +00005from flask import Flask, json, Response, render_template, make_response, request
6
Jonathan Harte1239422014-04-28 05:58:42 -07007CONFIG_FILE="config.json"
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +00008
Naoki Shiotad5d0e942014-03-27 18:57:32 -07009## Global Var for this proxy script setting.
10# "0.0.0.0" means any interface
Jonathan Harte1239422014-04-28 05:58:42 -070011GuiIp="0.0.0.0"
12GuiPort=9000
Naoki Shiotad5d0e942014-03-27 18:57:32 -070013
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000014## Global Var for ON.Lab local REST ##
Jonathan Harte1239422014-04-28 05:58:42 -070015# The GUI can be accessed at <this_host>:9000/onos-topology.html
Ubuntuf6ce96c2013-02-07 01:45:07 +000016RestIP="localhost"
Ubuntu82b8a832013-02-06 22:00:11 +000017RestPort=8080
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000018ONOS_DEFAULT_HOST="localhost" ;# Has to set if LB=False
Ubuntu82b8a832013-02-06 22:00:11 +000019DEBUG=1
Ubuntu82b8a832013-02-06 22:00:11 +000020
Masayoshi Kobayashi8158d442013-04-09 02:43:39 +000021app = Flask(__name__)
22
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000023def read_config():
Jonathan Hartb2554482013-04-08 13:40:31 -070024 global LB, TESTBED, controllers, core_switches, ONOS_GUI3_HOST, ONOS_GUI3_CONTROL_HOST
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000025 f = open(CONFIG_FILE)
26 conf = json.load(f)
27 LB = conf['LB']
Jonathan Hartb2554482013-04-08 13:40:31 -070028 TESTBED = conf['TESTBED']
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000029 controllers = conf['controllers']
Jonathan Hartb2554482013-04-08 13:40:31 -070030 core_switches=conf['core_switches']
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +000031 ONOS_GUI3_HOST=conf['ONOS_GUI3_HOST']
32 ONOS_GUI3_CONTROL_HOST=conf['ONOS_GUI3_CONTROL_HOST']
33 f.close()
Tim Lindberg201ade22013-04-05 11:52:08 -070034
Ubuntu82b8a832013-02-06 22:00:11 +000035## Worker Functions ##
36def log_error(txt):
37 print '%s' % (txt)
38
Ubuntu82b8a832013-02-06 22:00:11 +000039### File Fetch ###
40@app.route('/ui/img/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000041@app.route('/js/<filename>', methods=['GET'])
Masayoshi Kobayashi3d049312013-04-02 22:15:16 +000042@app.route('/log/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000043@app.route('/', methods=['GET'])
44@app.route('/<filename>', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +000045def return_file(filename="index.html"):
46 if request.path == "/":
Jonathan Harte1239422014-04-28 05:58:42 -070047 fullpath = "./onos-topology.html"
Ubuntu82b8a832013-02-06 22:00:11 +000048 else:
49 fullpath = str(request.path)[1:]
50
Paul Greysonc090d142013-04-09 16:59:03 -070051 try:
Masayoshi Kobayashi03e64b42013-04-05 05:56:27 +000052 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
Ubuntu82b8a832013-02-06 22:00:11 +000058 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 Greyson2913af82013-03-27 14:53:17 -070069 elif suffix == "svg":
70 response.headers["Content-type"] = "image/svg+xml"
Ubuntu82b8a832013-02-06 22:00:11 +000071
72 return response
73
Tim Lindberg9ec5e222013-04-12 10:46:02 -070074###### ONOS REST API ##############################
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000075## Worker Func ###
76def get_json(url):
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -080077 code = 200;
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000078 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -080079 response = urlopen(url)
80 except URLError, e:
Jonathan Harte1239422014-04-28 05:58:42 -070081 log_error("get_json: REST IF %s has issue. Reason: %s" % (url, e.reason))
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000082 result = ""
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -080083 return (500, result)
84 except HTTPError, e:
Jonathan Harte1239422014-04-28 05:58:42 -070085 log_error("get_json: REST IF %s has issue. Code %s" % (url, e.code))
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -080086 result = ""
87 return (e.code, result)
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000088
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -080089 result = response.read()
Masayoshi Kobayashi2ae891a2013-04-05 02:16:19 +000090 return (code, result)
91
Masayoshi Kobayashi13e2ebe2013-03-26 18:38:41 +000092
Ubuntu82b8a832013-02-06 22:00:11 +000093def 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 Kobayashi13e2ebe2013-03-26 18:38:41 +0000102## API for ON.Lab local GUI ##
Masayoshi Kobayashif63ef2f2013-02-20 21:47:21 +0000103@app.route('/topology', methods=['GET'])
Ubuntu82b8a832013-02-06 22:00:11 +0000104def topology_for_gui():
105 try:
Jonathan Hart5caa0442014-03-19 15:20:07 -0700106 url="http://%s:%s/wm/onos/topology/switches/json" % (RestIP, RestPort)
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800107 (code, result) = get_json(url)
Ubuntu82b8a832013-02-06 22:00:11 +0000108 parsedResult = json.loads(result)
109 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800110 log_error("REST IF has issue: %s" % url)
Ubuntu82b8a832013-02-06 22:00:11 +0000111 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700112 return
Ubuntu82b8a832013-02-06 22:00:11 +0000113
114 topo = {}
115 switches = []
116 links = []
Ubuntu37ebda62013-03-01 00:35:31 +0000117 devices = []
Ubuntu82b8a832013-02-06 22:00:11 +0000118
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
Ubuntu5b2b24a2013-02-27 09:51:13 +0000126 sw['group']= -1
Ubuntu37ebda62013-03-01 00:35:31 +0000127
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000128 if state == "INACTIVE":
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000129 sw['group']=0
Ubuntu82b8a832013-02-06 22:00:11 +0000130 switches.append(sw)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000131
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000132 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800133 url="http://%s:%s/wm/onos/registry/switches/json" % (RestIP, RestPort)
134 (code, result) = get_json(url)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000135 parsedResult = json.loads(result)
136 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800137 log_error("REST IF has issue: %s" % url)
Masayoshi Kobayashi1407a502013-02-27 06:23:08 +0000138 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
Ubuntu82b8a832013-02-06 22:00:11 +0000148 try:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800149 url = "http://%s:%s/wm/onos/topology/links/json" % (RestIP, RestPort)
150 (code, result) = get_json(url)
Ubuntu82b8a832013-02-06 22:00:11 +0000151 parsedResult = json.loads(result)
152 except:
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800153 log_error("REST IF has issue: %s" % url)
Ubuntu82b8a832013-02-06 22:00:11 +0000154 log_error("%s" % result)
Masayoshi Kobayashi7fa3fb82013-06-20 18:10:46 -0700155 return
Ubuntu82b8a832013-02-06 22:00:11 +0000156
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 Kobayashi3bc5fde2013-02-28 01:02:54 +0000167
Ubuntu82b8a832013-02-06 22:00:11 +0000168 links.append(link)
169
170 topo['nodes'] = switches
171 topo['links'] = links
172
Ubuntu82b8a832013-02-06 22:00:11 +0000173 js = json.dumps(topo)
174 resp = Response(js, status=200, mimetype='application/json')
175 return resp
176
Masayoshi Kobayashibcb03c02014-01-22 15:18:49 -0800177@app.route("/controller_status")
178def controller_status():
Masayoshi Kobayashi640ad692014-01-22 23:37:59 -0800179 url= "http://%s:%d/wm/onos/registry/controllers/json" % (RestIP, RestPort)
Masayoshi Kobayashibcb03c02014-01-22 15:18:49 -0800180 (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 Kobayashibcb03c02014-01-22 15:18:49 -0800192 cont_status.append(status)
193
194 js = json.dumps(cont_status)
195 resp = Response(js, status=200, mimetype='application/json')
196 return resp
197
Ubuntu82b8a832013-02-06 22:00:11 +0000198if __name__ == "__main__":
Pavlin Radoslavov092d0e22013-04-07 05:42:51 +0000199 read_config()
Jonathan Harte1239422014-04-28 05:58:42 -0700200 app.debug = True
201 app.run(threaded=True, host=GuiIp, port=GuiPort)