blob: a84140b1002718125e77c100796397ac6df18dbc [file] [log] [blame]
Pankaj Berdec4ae4b82013-01-12 09:56:47 -08001#! /usr/bin/python
2
3import os
4import sys
5import subprocess
6import json
7import argparse
8import io
9import time
10
11from flask import Flask, json, Response, render_template, make_response, request
12
13RestIP="127.0.0.1"
14RestPort=8182
15DBName="Cassandra-Netmap"
16
17app = Flask(__name__)
18
19
20## File Fetch ##
21@app.route('/ui/img/<filename>', methods=['GET'])
22@app.route('/img/<filename>', methods=['GET'])
23@app.route('/css/<filename>', methods=['GET'])
24@app.route('/js/models/<filename>', methods=['GET'])
25@app.route('/js/views/<filename>', methods=['GET'])
26@app.route('/js/<filename>', methods=['GET'])
27@app.route('/lib/<filename>', methods=['GET'])
28@app.route('/', methods=['GET'])
29@app.route('/<filename>', methods=['GET'])
30@app.route('/tpl/<filename>', methods=['GET'])
Paul Greysond9872392013-03-18 12:04:15 -070031@app.route('/ons-demo/<filename>', methods=['GET'])
32@app.route('/ons-demo/js/<filename>', methods=['GET'])
33@app.route('/ons-demo/css/<filename>', methods=['GET'])
34@app.route('/ons-demo/assets/<filename>', methods=['GET'])
Paul Greyson6f86d1e2013-03-18 14:40:39 -070035@app.route('/ons-demo/data/<filename>', methods=['GET'])
Pankaj Berdec4ae4b82013-01-12 09:56:47 -080036def return_file(filename="index.html"):
37 if request.path == "/":
38 fullpath = "./index.html"
39 else:
40 fullpath = str(request.path)[1:]
41
42 response = make_response(open(fullpath).read())
43 suffix = fullpath.split(".")[-1]
44
45 if suffix == "html" or suffix == "htm":
46 response.headers["Content-type"] = "text/html"
47 elif suffix == "js":
48 response.headers["Content-type"] = "application/javascript"
49 elif suffix == "css":
50 response.headers["Content-type"] = "text/css"
51 elif suffix == "png":
52 response.headers["Content-type"] = "image/png"
53
54 return response
55
Jonathan Hartff227832013-03-24 16:15:43 -070056## PROXY API (allows development where the webui is served from someplace other than the controller)##
57ONOS_GUI3_HOST="http://gui3.onlab.us:8080"
58ONOS_LOCAL_HOST="http://localhost:8080" ;# for Amazon EC2
Paul Greysonbcd3c772013-03-21 13:16:44 -070059
Jonathan Hart5caa0442014-03-19 15:20:07 -070060@app.route("/wm/onos/topology/switches/json")
Paul Greysonbcd3c772013-03-21 13:16:44 -070061def switches():
Jonathan Hartff227832013-03-24 16:15:43 -070062 if request.args.get('proxy') == None:
63 host = ONOS_LOCAL_HOST
64 else:
65 host = ONOS_GUI3_HOST
66
Paul Greysonbcd3c772013-03-21 13:16:44 -070067 try:
Jonathan Hart5caa0442014-03-19 15:20:07 -070068 command = "curl -s %s/wm/onos/topology/switches/json" % (host)
Paul Greysonbcd3c772013-03-21 13:16:44 -070069 print command
70 result = os.popen(command).read()
71 except:
72 print "REST IF has issue"
73 exit
74
75 resp = Response(result, status=200, mimetype='application/json')
76 return resp
77
Naoki Shiota862cc3b2013-12-13 15:42:50 -080078@app.route("/wm/onos/topology/links/json")
Paul Greysonbcd3c772013-03-21 13:16:44 -070079def links():
Jonathan Hartff227832013-03-24 16:15:43 -070080 if request.args.get('proxy') == None:
81 host = ONOS_LOCAL_HOST
82 else:
83 host = ONOS_GUI3_HOST
84
Paul Greysonbcd3c772013-03-21 13:16:44 -070085 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -080086 command = "curl -s %s/wm/onos/topology/links/json" % (host)
Paul Greysonbcd3c772013-03-21 13:16:44 -070087 print command
88 result = os.popen(command).read()
89 except:
90 print "REST IF has issue"
91 exit
92
93 resp = Response(result, status=200, mimetype='application/json')
94 return resp
95
Naoki Shiota862cc3b2013-12-13 15:42:50 -080096@app.route("/wm/onos/flows/getall/json")
Paul Greysonbcd3c772013-03-21 13:16:44 -070097def flows():
Jonathan Hartff227832013-03-24 16:15:43 -070098 if request.args.get('proxy') == None:
99 host = ONOS_LOCAL_HOST
100 else:
101 host = ONOS_GUI3_HOST
102
Paul Greysonbcd3c772013-03-21 13:16:44 -0700103 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800104 command = "curl -s %s/wm/onos/flows/getall/json" % (host)
Paul Greysonbcd3c772013-03-21 13:16:44 -0700105 print command
106 result = os.popen(command).read()
107 except:
108 print "REST IF has issue"
109 exit
110
111 resp = Response(result, status=200, mimetype='application/json')
112 return resp
113
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800114@app.route("/wm/onos/registry/controllers/json")
Paul Greysonbcd3c772013-03-21 13:16:44 -0700115def registry_controllers():
Jonathan Hartff227832013-03-24 16:15:43 -0700116 if request.args.get('proxy') == None:
117 host = ONOS_LOCAL_HOST
118 else:
119 host = ONOS_GUI3_HOST
120
Paul Greysonbcd3c772013-03-21 13:16:44 -0700121 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800122 command = "curl -s %s/wm/onos/registry/controllers/json" % (host)
Paul Greysonbcd3c772013-03-21 13:16:44 -0700123 print command
124 result = os.popen(command).read()
125 except:
126 print "REST IF has issue"
127 exit
128
129 resp = Response(result, status=200, mimetype='application/json')
130 return resp
131
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800132@app.route("/wm/onos/registry/switches/json")
Paul Greysonbcd3c772013-03-21 13:16:44 -0700133def registry_switches():
Jonathan Hartff227832013-03-24 16:15:43 -0700134 if request.args.get('proxy') == None:
135 host = ONOS_LOCAL_HOST
136 else:
137 host = ONOS_GUI3_HOST
138
Paul Greysonbcd3c772013-03-21 13:16:44 -0700139 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800140 command = "curl -s %s/wm/onos/registry/switches/json" % (host)
Paul Greysonbcd3c772013-03-21 13:16:44 -0700141 print command
142 result = os.popen(command).read()
143 except:
144 print "REST IF has issue"
145 exit
146
147 resp = Response(result, status=200, mimetype='application/json')
148 return resp
149
150
151
152
Pankaj Berdec4ae4b82013-01-12 09:56:47 -0800153## REST API ##
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800154#@app.route("/wm/onos/linkdiscovery/links/json")
Pankaj Berdec4ae4b82013-01-12 09:56:47 -0800155#def links():
156# global links_
157# js = json.dumps(links_)
158# resp = Response(js, status=200, mimetype='application/json')
159# return resp
160
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800161#@app.route("/wm/floodlight/core/controller/switches/json")
Pankaj Berdec4ae4b82013-01-12 09:56:47 -0800162#def switches():
163# global switches_
164# js = json.dumps(switches_)
165# resp = Response(js, status=200, mimetype='application/json')
166# return resp
167
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800168@app.route("/wm/floodlight/device/")
Pankaj Berdec4ae4b82013-01-12 09:56:47 -0800169def devices():
170 try:
171 command = "curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=device" % (RestIP, RestPort, DBName)
172 result = os.popen(command).read()
173 parsedResult = json.loads(result)['results']
174 except:
175 log_error("REST IF has issue")
176 exit
177
178 devices_ = []
179 for v in parsedResult:
180 if v['type'] == "device":
181 dl_addr = v['dl_addr']
182 nw_addr = v['nw_addr']
183 vertex = v['_id']
184 mac = []
185 mac.append(dl_addr)
186 ip = []
187 ip.append(nw_addr)
188 device = {}
189 device['entryClass']="DefaultEntryClass"
190 device['mac']=mac
191 device['ipv4']=ip
192 device['vlan']=[]
193 device['lastSeen']=0
194 attachpoints =[]
195 try:
196 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/in" % (RestIP, RestPort, DBName, vertex)
197 result = os.popen(command).read()
198 parsedResult = json.loads(result)['results']
199 except:
200 log_error("REST IF has issue")
201 exit
202
203 port = parsedResult[0]['number']
204 vertex = parsedResult[0]['_id']
205 dpid = portid_to_switch_dpid(vertex)
206 attachpoint = {}
207 attachpoint['port']=port
208 attachpoint['switchDPID']=dpid
209 attachpoints.append(attachpoint)
210 device['attachmentPoint']=attachpoints
211 devices_.append(device)
212
213 print devices_
214 js = json.dumps(devices_)
215 resp = Response(js, status=200, mimetype='application/json')
216 return resp
217
218#{"entityClass":"DefaultEntityClass","mac":["7c:d1:c3:e0:8c:a3"],"ipv4":["192.168.2.102","10.1.10.35"],"vlan":[],"attachmentPoint":[{"port":13,"switchDPID":"00:01:00:12:e2:78:32:44","errorStatus":null}],"lastSeen":1357333593496}
219
220## return fake stat for now
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800221@app.route("/wm/floodlight/core/switch/<switchId>/<statType>/json")
Pankaj Berdec4ae4b82013-01-12 09:56:47 -0800222def switch_stat(switchId, statType):
223 if statType == "desc":
224 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
225 ret = {}
226 ret[switchId]=desc
227 elif statType == "aggregate":
228 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
229 ret = {}
230 ret[switchId]=aggr
231 else:
Paul Greysond9872392013-03-18 12:04:15 -0700232 ret = {}
Pankaj Berdec4ae4b82013-01-12 09:56:47 -0800233
234 js = json.dumps(ret)
235 resp = Response(js, status=200, mimetype='application/json')
236 return resp
237
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800238@app.route("/wm/floodlight/core/controller/switches/json")
Pankaj Berdec4ae4b82013-01-12 09:56:47 -0800239def query_switch():
240 try:
241 command = "curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=switch" % (RestIP, RestPort, DBName)
242 result = os.popen(command).read()
243 parsedResult = json.loads(result)['results']
244 except:
245 log_error("REST IF has issue")
246 exit
247
248 switches_ = []
249 for v in parsedResult:
250 if v['type'] == "switch":
251 dpid = str(v['dpid']) ;# removing quotation
252 sw = {}
253 sw['dpid']=dpid
254 switches_.append(sw)
255
256 print switches_
257 js = json.dumps(switches_)
258 resp = Response(js, status=200, mimetype='application/json')
259 return resp
260
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800261@app.route("/wm/onos/linkdiscovery/links/json")
Pankaj Berdec4ae4b82013-01-12 09:56:47 -0800262def query_links():
263 try:
264 command = "curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port" % (RestIP, RestPort, DBName)
265 result = os.popen(command).read()
266 parsedResult = json.loads(result)['results']
267 except:
268 log_error("REST IF has issue")
269 exit
270
271 sport = []
272 switches_ = []
273 for v in parsedResult:
274 srcport = v['_id']
275 try:
276 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
277 result = os.popen(command).read()
278 linkResults = json.loads(result)['results']
279 except:
280 log_error("REST IF has issue")
281 exit
282
283 for p in linkResults:
284 dstport = p['_id']
285 (sport, sdpid) = get_port_switch(srcport)
286 (dport, ddpid) = get_port_switch(dstport)
287 link = {}
288 link["src-switch"]=sdpid
289 link["src-port"]=sport
290 link["src-port-state"]=0
291 link["dst-switch"]=ddpid
292 link["dst-port"]=dport
293 link["dst-port-state"]=0
294 link["type"]="internal"
295 switches_.append(link)
296
297 print switches_
298 js = json.dumps(switches_)
299 resp = Response(js, status=200, mimetype='application/json')
300 return resp
301
302def get_port_switch(vertex):
303 try:
304 command = "curl -s http://%s:%s/graphs/%s/vertices/%d" % (RestIP, RestPort, DBName, vertex)
305 result = os.popen(command).read()
306 parsedResult = json.loads(result)['results']
307 except:
308 log_error("REST IF has issue")
309 exit
310
311 port_number = parsedResult['number']
312 vertex_id = parsedResult['_id']
313 switch_dpid = portid_to_switch_dpid(vertex_id)
314
315 return (port_number, switch_dpid)
316
317def portid_to_switch_dpid(vertex):
318 try:
319 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/in" % (RestIP, RestPort, DBName, vertex)
320 result = os.popen(command).read()
321 parsedResult = json.loads(result)['results']
322 except:
323 log_error("REST IF has issue")
324 exit
325
326 for v in parsedResult:
327 if v['type'] == "switch":
328 sw_dpid = v['dpid']
329 break
330
331 return sw_dpid
332
333def id_to_dpid(vertex):
334 try:
335 command = "curl -s http://%s:%s/graphs/%s/vertices/%d" % (RestIP, RestPort, DBName, vertex)
336 result = os.popen(command).read()
337 parsedResult = json.loads(result)['results']
338 except:
339 log_error("REST IF has issue")
340 exit
341
342 if parsedResult['type'] != "switch":
343 print "not a switch vertex"
344 exit
345 else:
346 sw_dpid = parsedResult['dpid']
347
348 return sw_dpid
Paul Greysond9872392013-03-18 12:04:15 -0700349
Pankaj Berdec4ae4b82013-01-12 09:56:47 -0800350
351if __name__ == "__main__":
352 app.debug = True
Masayoshi Kobayashi6db93052013-03-23 01:15:55 +0000353 app.run(threaded=True, host="0.0.0.0", port=9000)
Pankaj Berdec4ae4b82013-01-12 09:56:47 -0800354# query_switch()
355# query_links()
356# devices()