Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | import subprocess |
| 6 | import json |
| 7 | import argparse |
| 8 | import io |
| 9 | import time |
| 10 | |
| 11 | from flask import Flask, json, Response, render_template, make_response, request |
| 12 | |
| 13 | RestIP="127.0.0.1" |
| 14 | RestPort=8182 |
| 15 | DBName="Cassandra-Netmap" |
| 16 | |
| 17 | app = 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 Greyson | d987239 | 2013-03-18 12:04:15 -0700 | [diff] [blame] | 31 | @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 Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 35 | @app.route('/ons-demo/data/<filename>', methods=['GET']) |
Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 36 | def 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 Hart | ff22783 | 2013-03-24 16:15:43 -0700 | [diff] [blame] | 56 | ## PROXY API (allows development where the webui is served from someplace other than the controller)## |
| 57 | ONOS_GUI3_HOST="http://gui3.onlab.us:8080" |
| 58 | ONOS_LOCAL_HOST="http://localhost:8080" ;# for Amazon EC2 |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 59 | |
Jonathan Hart | 5caa044 | 2014-03-19 15:20:07 -0700 | [diff] [blame^] | 60 | @app.route("/wm/onos/topology/switches/json") |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 61 | def switches(): |
Jonathan Hart | ff22783 | 2013-03-24 16:15:43 -0700 | [diff] [blame] | 62 | if request.args.get('proxy') == None: |
| 63 | host = ONOS_LOCAL_HOST |
| 64 | else: |
| 65 | host = ONOS_GUI3_HOST |
| 66 | |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 67 | try: |
Jonathan Hart | 5caa044 | 2014-03-19 15:20:07 -0700 | [diff] [blame^] | 68 | command = "curl -s %s/wm/onos/topology/switches/json" % (host) |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 69 | 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 Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 78 | @app.route("/wm/onos/topology/links/json") |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 79 | def links(): |
Jonathan Hart | ff22783 | 2013-03-24 16:15:43 -0700 | [diff] [blame] | 80 | if request.args.get('proxy') == None: |
| 81 | host = ONOS_LOCAL_HOST |
| 82 | else: |
| 83 | host = ONOS_GUI3_HOST |
| 84 | |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 85 | try: |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 86 | command = "curl -s %s/wm/onos/topology/links/json" % (host) |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 87 | 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 Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 96 | @app.route("/wm/onos/flows/getall/json") |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 97 | def flows(): |
Jonathan Hart | ff22783 | 2013-03-24 16:15:43 -0700 | [diff] [blame] | 98 | if request.args.get('proxy') == None: |
| 99 | host = ONOS_LOCAL_HOST |
| 100 | else: |
| 101 | host = ONOS_GUI3_HOST |
| 102 | |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 103 | try: |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 104 | command = "curl -s %s/wm/onos/flows/getall/json" % (host) |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 105 | 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 Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 114 | @app.route("/wm/onos/registry/controllers/json") |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 115 | def registry_controllers(): |
Jonathan Hart | ff22783 | 2013-03-24 16:15:43 -0700 | [diff] [blame] | 116 | if request.args.get('proxy') == None: |
| 117 | host = ONOS_LOCAL_HOST |
| 118 | else: |
| 119 | host = ONOS_GUI3_HOST |
| 120 | |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 121 | try: |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 122 | command = "curl -s %s/wm/onos/registry/controllers/json" % (host) |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 123 | 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 Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 132 | @app.route("/wm/onos/registry/switches/json") |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 133 | def registry_switches(): |
Jonathan Hart | ff22783 | 2013-03-24 16:15:43 -0700 | [diff] [blame] | 134 | if request.args.get('proxy') == None: |
| 135 | host = ONOS_LOCAL_HOST |
| 136 | else: |
| 137 | host = ONOS_GUI3_HOST |
| 138 | |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 139 | try: |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 140 | command = "curl -s %s/wm/onos/registry/switches/json" % (host) |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 141 | 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 Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 153 | ## REST API ## |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 154 | #@app.route("/wm/onos/linkdiscovery/links/json") |
Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 155 | #def links(): |
| 156 | # global links_ |
| 157 | # js = json.dumps(links_) |
| 158 | # resp = Response(js, status=200, mimetype='application/json') |
| 159 | # return resp |
| 160 | |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 161 | #@app.route("/wm/floodlight/core/controller/switches/json") |
Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 162 | #def switches(): |
| 163 | # global switches_ |
| 164 | # js = json.dumps(switches_) |
| 165 | # resp = Response(js, status=200, mimetype='application/json') |
| 166 | # return resp |
| 167 | |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 168 | @app.route("/wm/floodlight/device/") |
Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 169 | def 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 Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 221 | @app.route("/wm/floodlight/core/switch/<switchId>/<statType>/json") |
Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 222 | def 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 Greyson | d987239 | 2013-03-18 12:04:15 -0700 | [diff] [blame] | 232 | ret = {} |
Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 233 | |
| 234 | js = json.dumps(ret) |
| 235 | resp = Response(js, status=200, mimetype='application/json') |
| 236 | return resp |
| 237 | |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 238 | @app.route("/wm/floodlight/core/controller/switches/json") |
Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 239 | def 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 Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 261 | @app.route("/wm/onos/linkdiscovery/links/json") |
Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 262 | def 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 | |
| 302 | def 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 | |
| 317 | def 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 | |
| 333 | def 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 Greyson | d987239 | 2013-03-18 12:04:15 -0700 | [diff] [blame] | 349 | |
Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 350 | |
| 351 | if __name__ == "__main__": |
| 352 | app.debug = True |
Masayoshi Kobayashi | 6db9305 | 2013-03-23 01:15:55 +0000 | [diff] [blame] | 353 | app.run(threaded=True, host="0.0.0.0", port=9000) |
Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 354 | # query_switch() |
| 355 | # query_links() |
| 356 | # devices() |