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