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