blob: a8188d82b01e6622a09963865034bd7452d6a0d4 [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
Paul Greysonbcd3c772013-03-21 13:16:44 -070056## PROXY API (allows development where the webui is served from someplace other than the ONOS_HOST)##
57ONOS_HOST="http://gui3.onlab.us:8080"
58
59@app.route("/proxy/wm/core/topology/switches/all/json")
60def switches():
61 try:
62 command = "curl -s %s/wm/core/topology/switches/all/json" % (ONOS_HOST)
63 print command
64 result = os.popen(command).read()
65 except:
66 print "REST IF has issue"
67 exit
68
69 resp = Response(result, status=200, mimetype='application/json')
70 return resp
71
72@app.route("/proxy/wm/core/topology/links/json")
73def links():
74 try:
75 command = "curl -s %s/wm/core/topology/links/json" % (ONOS_HOST)
76 print command
77 result = os.popen(command).read()
78 except:
79 print "REST IF has issue"
80 exit
81
82 resp = Response(result, status=200, mimetype='application/json')
83 return resp
84
85@app.route("/proxy/wm/flow/getall/json")
86def flows():
87 try:
88 command = "curl -s %s/wm/flow/getall/json" % (ONOS_HOST)
89 print command
90 result = os.popen(command).read()
91 except:
92 print "REST IF has issue"
93 exit
94
95 resp = Response(result, status=200, mimetype='application/json')
96 return resp
97
98@app.route("/proxy/wm/registry/controllers/json")
99def registry_controllers():
100 try:
101 command = "curl -s %s/wm/registry/controllers/json" % (ONOS_HOST)
102 print command
103 result = os.popen(command).read()
104 except:
105 print "REST IF has issue"
106 exit
107
108 resp = Response(result, status=200, mimetype='application/json')
109 return resp
110
111@app.route("/proxy/wm/registry/switches/json")
112def registry_switches():
113 try:
114 command = "curl -s %s/wm/registry/switches/json" % (ONOS_HOST)
115 print command
116 result = os.popen(command).read()
117 except:
118 print "REST IF has issue"
119 exit
120
121 resp = Response(result, status=200, mimetype='application/json')
122 return resp
123
124
125
126
Pankaj Berdec4ae4b82013-01-12 09:56:47 -0800127## REST API ##
128#@app.route("/wm/topology/links/json")
129#def links():
130# global links_
131# js = json.dumps(links_)
132# resp = Response(js, status=200, mimetype='application/json')
133# return resp
134
135#@app.route("/wm/core/controller/switches/json")
136#def switches():
137# global switches_
138# js = json.dumps(switches_)
139# resp = Response(js, status=200, mimetype='application/json')
140# return resp
141
142@app.route("/wm/device/")
143def devices():
144 try:
145 command = "curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=device" % (RestIP, RestPort, DBName)
146 result = os.popen(command).read()
147 parsedResult = json.loads(result)['results']
148 except:
149 log_error("REST IF has issue")
150 exit
151
152 devices_ = []
153 for v in parsedResult:
154 if v['type'] == "device":
155 dl_addr = v['dl_addr']
156 nw_addr = v['nw_addr']
157 vertex = v['_id']
158 mac = []
159 mac.append(dl_addr)
160 ip = []
161 ip.append(nw_addr)
162 device = {}
163 device['entryClass']="DefaultEntryClass"
164 device['mac']=mac
165 device['ipv4']=ip
166 device['vlan']=[]
167 device['lastSeen']=0
168 attachpoints =[]
169 try:
170 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/in" % (RestIP, RestPort, DBName, vertex)
171 result = os.popen(command).read()
172 parsedResult = json.loads(result)['results']
173 except:
174 log_error("REST IF has issue")
175 exit
176
177 port = parsedResult[0]['number']
178 vertex = parsedResult[0]['_id']
179 dpid = portid_to_switch_dpid(vertex)
180 attachpoint = {}
181 attachpoint['port']=port
182 attachpoint['switchDPID']=dpid
183 attachpoints.append(attachpoint)
184 device['attachmentPoint']=attachpoints
185 devices_.append(device)
186
187 print devices_
188 js = json.dumps(devices_)
189 resp = Response(js, status=200, mimetype='application/json')
190 return resp
191
192#{"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}
193
194## return fake stat for now
195@app.route("/wm/core/switch/<switchId>/<statType>/json")
196def switch_stat(switchId, statType):
197 if statType == "desc":
198 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
199 ret = {}
200 ret[switchId]=desc
201 elif statType == "aggregate":
202 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
203 ret = {}
204 ret[switchId]=aggr
205 else:
Paul Greysond9872392013-03-18 12:04:15 -0700206 ret = {}
Pankaj Berdec4ae4b82013-01-12 09:56:47 -0800207
208 js = json.dumps(ret)
209 resp = Response(js, status=200, mimetype='application/json')
210 return resp
211
212@app.route("/wm/core/controller/switches/json")
213def query_switch():
214 try:
215 command = "curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=switch" % (RestIP, RestPort, DBName)
216 result = os.popen(command).read()
217 parsedResult = json.loads(result)['results']
218 except:
219 log_error("REST IF has issue")
220 exit
221
222 switches_ = []
223 for v in parsedResult:
224 if v['type'] == "switch":
225 dpid = str(v['dpid']) ;# removing quotation
226 sw = {}
227 sw['dpid']=dpid
228 switches_.append(sw)
229
230 print switches_
231 js = json.dumps(switches_)
232 resp = Response(js, status=200, mimetype='application/json')
233 return resp
234
235@app.route("/wm/topology/links/json")
236def query_links():
237 try:
238 command = "curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port" % (RestIP, RestPort, DBName)
239 result = os.popen(command).read()
240 parsedResult = json.loads(result)['results']
241 except:
242 log_error("REST IF has issue")
243 exit
244
245 sport = []
246 switches_ = []
247 for v in parsedResult:
248 srcport = v['_id']
249 try:
250 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
251 result = os.popen(command).read()
252 linkResults = json.loads(result)['results']
253 except:
254 log_error("REST IF has issue")
255 exit
256
257 for p in linkResults:
258 dstport = p['_id']
259 (sport, sdpid) = get_port_switch(srcport)
260 (dport, ddpid) = get_port_switch(dstport)
261 link = {}
262 link["src-switch"]=sdpid
263 link["src-port"]=sport
264 link["src-port-state"]=0
265 link["dst-switch"]=ddpid
266 link["dst-port"]=dport
267 link["dst-port-state"]=0
268 link["type"]="internal"
269 switches_.append(link)
270
271 print switches_
272 js = json.dumps(switches_)
273 resp = Response(js, status=200, mimetype='application/json')
274 return resp
275
276def get_port_switch(vertex):
277 try:
278 command = "curl -s http://%s:%s/graphs/%s/vertices/%d" % (RestIP, RestPort, DBName, vertex)
279 result = os.popen(command).read()
280 parsedResult = json.loads(result)['results']
281 except:
282 log_error("REST IF has issue")
283 exit
284
285 port_number = parsedResult['number']
286 vertex_id = parsedResult['_id']
287 switch_dpid = portid_to_switch_dpid(vertex_id)
288
289 return (port_number, switch_dpid)
290
291def portid_to_switch_dpid(vertex):
292 try:
293 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/in" % (RestIP, RestPort, DBName, vertex)
294 result = os.popen(command).read()
295 parsedResult = json.loads(result)['results']
296 except:
297 log_error("REST IF has issue")
298 exit
299
300 for v in parsedResult:
301 if v['type'] == "switch":
302 sw_dpid = v['dpid']
303 break
304
305 return sw_dpid
306
307def id_to_dpid(vertex):
308 try:
309 command = "curl -s http://%s:%s/graphs/%s/vertices/%d" % (RestIP, RestPort, DBName, vertex)
310 result = os.popen(command).read()
311 parsedResult = json.loads(result)['results']
312 except:
313 log_error("REST IF has issue")
314 exit
315
316 if parsedResult['type'] != "switch":
317 print "not a switch vertex"
318 exit
319 else:
320 sw_dpid = parsedResult['dpid']
321
322 return sw_dpid
Paul Greysond9872392013-03-18 12:04:15 -0700323
Pankaj Berdec4ae4b82013-01-12 09:56:47 -0800324
325if __name__ == "__main__":
326 app.debug = True
327 app.run(host="0.0.0.0", port=9000)
328# query_switch()
329# query_links()
330# devices()