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