blob: bf049fd1aa5c6179072d05e63414303fbf5f11c5 [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'])
31def return_file(filename="index.html"):
32 if request.path == "/":
33 fullpath = "./index.html"
34 else:
35 fullpath = str(request.path)[1:]
36
37 response = make_response(open(fullpath).read())
38 suffix = fullpath.split(".")[-1]
39
40 if suffix == "html" or suffix == "htm":
41 response.headers["Content-type"] = "text/html"
42 elif suffix == "js":
43 response.headers["Content-type"] = "application/javascript"
44 elif suffix == "css":
45 response.headers["Content-type"] = "text/css"
46 elif suffix == "png":
47 response.headers["Content-type"] = "image/png"
48
49 return response
50
51## REST API ##
52#@app.route("/wm/topology/links/json")
53#def links():
54# global links_
55# js = json.dumps(links_)
56# resp = Response(js, status=200, mimetype='application/json')
57# return resp
58
59#@app.route("/wm/core/controller/switches/json")
60#def switches():
61# global switches_
62# js = json.dumps(switches_)
63# resp = Response(js, status=200, mimetype='application/json')
64# return resp
65
66@app.route("/wm/device/")
67def devices():
68 try:
69 command = "curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=device" % (RestIP, RestPort, DBName)
70 result = os.popen(command).read()
71 parsedResult = json.loads(result)['results']
72 except:
73 log_error("REST IF has issue")
74 exit
75
76 devices_ = []
77 for v in parsedResult:
78 if v['type'] == "device":
79 dl_addr = v['dl_addr']
80 nw_addr = v['nw_addr']
81 vertex = v['_id']
82 mac = []
83 mac.append(dl_addr)
84 ip = []
85 ip.append(nw_addr)
86 device = {}
87 device['entryClass']="DefaultEntryClass"
88 device['mac']=mac
89 device['ipv4']=ip
90 device['vlan']=[]
91 device['lastSeen']=0
92 attachpoints =[]
93 try:
94 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/in" % (RestIP, RestPort, DBName, vertex)
95 result = os.popen(command).read()
96 parsedResult = json.loads(result)['results']
97 except:
98 log_error("REST IF has issue")
99 exit
100
101 port = parsedResult[0]['number']
102 vertex = parsedResult[0]['_id']
103 dpid = portid_to_switch_dpid(vertex)
104 attachpoint = {}
105 attachpoint['port']=port
106 attachpoint['switchDPID']=dpid
107 attachpoints.append(attachpoint)
108 device['attachmentPoint']=attachpoints
109 devices_.append(device)
110
111 print devices_
112 js = json.dumps(devices_)
113 resp = Response(js, status=200, mimetype='application/json')
114 return resp
115
116#{"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}
117
118## return fake stat for now
119@app.route("/wm/core/switch/<switchId>/<statType>/json")
120def switch_stat(switchId, statType):
121 if statType == "desc":
122 desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}]
123 ret = {}
124 ret[switchId]=desc
125 elif statType == "aggregate":
126 aggr = {"packetCount":0,"byteCount":0,"flowCount":0}
127 ret = {}
128 ret[switchId]=aggr
129 else:
130 ret = {}
131
132 js = json.dumps(ret)
133 resp = Response(js, status=200, mimetype='application/json')
134 return resp
135
136@app.route("/wm/core/controller/switches/json")
137def query_switch():
138 try:
139 command = "curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=switch" % (RestIP, RestPort, DBName)
140 result = os.popen(command).read()
141 parsedResult = json.loads(result)['results']
142 except:
143 log_error("REST IF has issue")
144 exit
145
146 switches_ = []
147 for v in parsedResult:
148 if v['type'] == "switch":
149 dpid = str(v['dpid']) ;# removing quotation
150 sw = {}
151 sw['dpid']=dpid
152 switches_.append(sw)
153
154 print switches_
155 js = json.dumps(switches_)
156 resp = Response(js, status=200, mimetype='application/json')
157 return resp
158
159@app.route("/wm/topology/links/json")
160def query_links():
161 try:
162 command = "curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port" % (RestIP, RestPort, DBName)
163 result = os.popen(command).read()
164 parsedResult = json.loads(result)['results']
165 except:
166 log_error("REST IF has issue")
167 exit
168
169 sport = []
170 switches_ = []
171 for v in parsedResult:
172 srcport = v['_id']
173 try:
174 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport)
175 result = os.popen(command).read()
176 linkResults = json.loads(result)['results']
177 except:
178 log_error("REST IF has issue")
179 exit
180
181 for p in linkResults:
182 dstport = p['_id']
183 (sport, sdpid) = get_port_switch(srcport)
184 (dport, ddpid) = get_port_switch(dstport)
185 link = {}
186 link["src-switch"]=sdpid
187 link["src-port"]=sport
188 link["src-port-state"]=0
189 link["dst-switch"]=ddpid
190 link["dst-port"]=dport
191 link["dst-port-state"]=0
192 link["type"]="internal"
193 switches_.append(link)
194
195 print switches_
196 js = json.dumps(switches_)
197 resp = Response(js, status=200, mimetype='application/json')
198 return resp
199
200def get_port_switch(vertex):
201 try:
202 command = "curl -s http://%s:%s/graphs/%s/vertices/%d" % (RestIP, RestPort, DBName, vertex)
203 result = os.popen(command).read()
204 parsedResult = json.loads(result)['results']
205 except:
206 log_error("REST IF has issue")
207 exit
208
209 port_number = parsedResult['number']
210 vertex_id = parsedResult['_id']
211 switch_dpid = portid_to_switch_dpid(vertex_id)
212
213 return (port_number, switch_dpid)
214
215def portid_to_switch_dpid(vertex):
216 try:
217 command = "curl -s http://%s:%s/graphs/%s/vertices/%d/in" % (RestIP, RestPort, DBName, vertex)
218 result = os.popen(command).read()
219 parsedResult = json.loads(result)['results']
220 except:
221 log_error("REST IF has issue")
222 exit
223
224 for v in parsedResult:
225 if v['type'] == "switch":
226 sw_dpid = v['dpid']
227 break
228
229 return sw_dpid
230
231def id_to_dpid(vertex):
232 try:
233 command = "curl -s http://%s:%s/graphs/%s/vertices/%d" % (RestIP, RestPort, DBName, vertex)
234 result = os.popen(command).read()
235 parsedResult = json.loads(result)['results']
236 except:
237 log_error("REST IF has issue")
238 exit
239
240 if parsedResult['type'] != "switch":
241 print "not a switch vertex"
242 exit
243 else:
244 sw_dpid = parsedResult['dpid']
245
246 return sw_dpid
247
248
249if __name__ == "__main__":
250 app.debug = True
251 app.run(host="0.0.0.0", port=9000)
252# query_switch()
253# query_links()
254# devices()