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