Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [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 ## |
Ubuntu | f6ce96c | 2013-02-07 01:45:07 +0000 | [diff] [blame^] | 14 | RestIP="localhost" |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 15 | RestPort=8080 |
| 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 | ## Rest APIs ## |
| 32 | ### File Fetch ### |
| 33 | @app.route('/ui/img/<filename>', methods=['GET']) |
| 34 | @app.route('/img/<filename>', methods=['GET']) |
| 35 | @app.route('/css/<filename>', methods=['GET']) |
| 36 | @app.route('/js/models/<filename>', methods=['GET']) |
| 37 | @app.route('/js/views/<filename>', methods=['GET']) |
| 38 | @app.route('/js/<filename>', methods=['GET']) |
| 39 | @app.route('/lib/<filename>', methods=['GET']) |
| 40 | @app.route('/', methods=['GET']) |
| 41 | @app.route('/<filename>', methods=['GET']) |
| 42 | @app.route('/tpl/<filename>', methods=['GET']) |
| 43 | def return_file(filename="index.html"): |
| 44 | if request.path == "/": |
| 45 | fullpath = "./index.html" |
| 46 | else: |
| 47 | fullpath = str(request.path)[1:] |
| 48 | |
| 49 | response = make_response(open(fullpath).read()) |
| 50 | suffix = fullpath.split(".")[-1] |
| 51 | |
| 52 | if suffix == "html" or suffix == "htm": |
| 53 | response.headers["Content-type"] = "text/html" |
| 54 | elif suffix == "js": |
| 55 | response.headers["Content-type"] = "application/javascript" |
| 56 | elif suffix == "css": |
| 57 | response.headers["Content-type"] = "text/css" |
| 58 | elif suffix == "png": |
| 59 | response.headers["Content-type"] = "image/png" |
| 60 | |
| 61 | return response |
| 62 | |
| 63 | init_topo1 = { |
| 64 | "nodes" : [ |
| 65 | {"name" : "sw0", "group" : 0}, |
| 66 | {"name" : "sw1", "group" : 0}, |
| 67 | {"name" : "sw2", "group" : 0}, |
| 68 | {"name" : "sw3", "group" : 0}, |
| 69 | {"name" : "sw4", "group" : 0}, |
| 70 | {"name" : "sw5", "group" : 0}, |
| 71 | {"name" : "host0", "group" : 1} |
| 72 | ], |
| 73 | "links" : [ |
| 74 | {"source" :0, "target": 1}, |
| 75 | {"source" :1, "target": 0}, |
| 76 | {"source" :0, "target": 2}, |
| 77 | {"source" :2, "target": 0}, |
| 78 | {"source" :1, "target": 3}, |
| 79 | {"source" :3, "target": 1}, |
| 80 | {"source" :2, "target": 3}, |
| 81 | {"source" :3, "target": 2}, |
| 82 | {"source" :2, "target": 4}, |
| 83 | {"source" :4, "target": 2}, |
| 84 | {"source" :3, "target": 5}, |
| 85 | {"source" :5, "target": 3}, |
| 86 | {"source" :4, "target": 5}, |
| 87 | {"source" :5, "target": 4}, |
| 88 | {"source" :6, "target": 0}, |
| 89 | {"source" :0, "target": 6} |
| 90 | ] |
| 91 | } |
| 92 | |
| 93 | def node_id(switch_array, dpid): |
| 94 | id = -1 |
| 95 | for i, val in enumerate(switch_array): |
| 96 | if val['name'] == dpid: |
| 97 | id = i |
| 98 | break |
| 99 | |
| 100 | return id |
| 101 | |
| 102 | @app.route("/topology") |
| 103 | def topology_for_gui(): |
| 104 | try: |
| 105 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 106 | result = os.popen(command).read() |
| 107 | parsedResult = json.loads(result) |
| 108 | except: |
| 109 | log_error("REST IF has issue: %s" % command) |
| 110 | log_error("%s" % result) |
| 111 | sys.exit(0) |
| 112 | |
| 113 | topo = {} |
| 114 | switches = [] |
| 115 | links = [] |
| 116 | |
| 117 | for v in parsedResult: |
| 118 | if v.has_key('dpid'): |
| 119 | # if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 120 | dpid = str(v['dpid']) |
| 121 | state = str(v['state']) |
| 122 | sw = {} |
| 123 | sw['name']=dpid |
| 124 | if str(v['state']) == "ACTIVE": |
| 125 | sw['group']=0 |
| 126 | if str(v['state']) == "INACTIVE": |
| 127 | sw['group']=1 |
| 128 | |
| 129 | switches.append(sw) |
| 130 | |
| 131 | try: |
| 132 | command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort) |
| 133 | result = os.popen(command).read() |
| 134 | parsedResult = json.loads(result) |
| 135 | except: |
| 136 | log_error("REST IF has issue: %s" % command) |
| 137 | log_error("%s" % result) |
| 138 | sys.exit(0) |
| 139 | |
| 140 | for v in parsedResult: |
| 141 | link = {} |
| 142 | if v.has_key('dst-switch'): |
| 143 | dst_dpid = str(v['dst-switch']) |
| 144 | dst_id = node_id(switches, dst_dpid) |
| 145 | if v.has_key('src-switch'): |
| 146 | src_dpid = str(v['src-switch']) |
| 147 | src_id = node_id(switches, src_dpid) |
| 148 | link['source'] = src_id |
| 149 | link['target'] = dst_id |
| 150 | links.append(link) |
| 151 | |
| 152 | topo['nodes'] = switches |
| 153 | topo['links'] = links |
| 154 | |
| 155 | pp.pprint(topo) |
| 156 | js = json.dumps(topo) |
| 157 | resp = Response(js, status=200, mimetype='application/json') |
| 158 | return resp |
| 159 | |
| 160 | |
| 161 | @app.route("/wm/core/controller/switches/json") |
| 162 | def query_switch(): |
| 163 | try: |
| 164 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 165 | # http://localhost:8080/wm/core/topology/switches/active/json |
| 166 | result = os.popen(command).read() |
| 167 | parsedResult = json.loads(result) |
| 168 | except: |
| 169 | log_error("REST IF has issue: %s" % command) |
| 170 | log_error("%s" % result) |
| 171 | sys.exit(0) |
| 172 | |
| 173 | # print command |
| 174 | # print result |
| 175 | switches_ = [] |
| 176 | for v in parsedResult: |
| 177 | if v.has_key('dpid'): |
| 178 | if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 179 | dpid = str(v['dpid']) |
| 180 | state = str(v['state']) |
| 181 | sw = {} |
| 182 | sw['dpid']=dpid |
| 183 | sw['active']=state |
| 184 | switches_.append(sw) |
| 185 | |
| 186 | pp.pprint(switches_) |
| 187 | js = json.dumps(switches_) |
| 188 | resp = Response(js, status=200, mimetype='application/json') |
| 189 | return resp |
| 190 | |
| 191 | @app.route("/wm/device/") |
| 192 | def devices(): |
| 193 | try: |
| 194 | command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName) |
| 195 | result = os.popen(command).read() |
| 196 | parsedResult = json.loads(result)['results'] |
| 197 | except: |
| 198 | log_error("REST IF has issue: %s" % command) |
| 199 | log_error("%s" % result) |
| 200 | sys.exit(0) |
| 201 | |
| 202 | devices = [] |
| 203 | for v in parsedResult: |
| 204 | dl_addr = v['dl_addr'] |
| 205 | nw_addr = v['nw_addr'] |
| 206 | vertex = v['_id'] |
| 207 | mac = [] |
| 208 | mac.append(dl_addr) |
| 209 | ip = [] |
| 210 | ip.append(nw_addr) |
| 211 | device = {} |
| 212 | device['entryClass']="DefaultEntryClass" |
| 213 | device['mac']=mac |
| 214 | device['ipv4']=ip |
| 215 | device['vlan']=[] |
| 216 | device['lastSeen']=0 |
| 217 | attachpoints =[] |
| 218 | |
| 219 | port, dpid = deviceV_to_attachpoint(vertex) |
| 220 | attachpoint = {} |
| 221 | attachpoint['port']=port |
| 222 | attachpoint['switchDPID']=dpid |
| 223 | attachpoints.append(attachpoint) |
| 224 | device['attachmentPoint']=attachpoints |
| 225 | devices.append(device) |
| 226 | |
| 227 | print devices |
| 228 | js = json.dumps(devices) |
| 229 | resp = Response(js, status=200, mimetype='application/json') |
| 230 | return resp |
| 231 | |
| 232 | #{"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} |
| 233 | |
| 234 | |
| 235 | ## return fake stat for now |
| 236 | @app.route("/wm/core/switch/<switchId>/<statType>/json") |
| 237 | def switch_stat(switchId, statType): |
| 238 | if statType == "desc": |
| 239 | desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}] |
| 240 | ret = {} |
| 241 | ret[switchId]=desc |
| 242 | elif statType == "aggregate": |
| 243 | aggr = {"packetCount":0,"byteCount":0,"flowCount":0} |
| 244 | ret = {} |
| 245 | ret[switchId]=aggr |
| 246 | else: |
| 247 | ret = {} |
| 248 | |
| 249 | js = json.dumps(ret) |
| 250 | resp = Response(js, status=200, mimetype='application/json') |
| 251 | return resp |
| 252 | |
| 253 | |
| 254 | @app.route("/wm/topology/links/json") |
| 255 | def query_links(): |
| 256 | try: |
| 257 | command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName) |
| 258 | result = os.popen(command).read() |
| 259 | parsedResult = json.loads(result)['results'] |
| 260 | except: |
| 261 | log_error("REST IF has issue: %s" % command) |
| 262 | log_error("%s" % result) |
| 263 | sys.exit(0) |
| 264 | |
| 265 | debug("query_links %s" % command) |
| 266 | pp.pprint(parsedResult) |
| 267 | sport = [] |
| 268 | links = [] |
| 269 | for v in parsedResult: |
| 270 | srcport = v['_id'] |
| 271 | try: |
| 272 | command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport) |
| 273 | print command |
| 274 | result = os.popen(command).read() |
| 275 | linkResults = json.loads(result)['results'] |
| 276 | except: |
| 277 | log_error("REST IF has issue: %s" % command) |
| 278 | log_error("%s" % result) |
| 279 | sys.exit(0) |
| 280 | |
| 281 | for p in linkResults: |
| 282 | if p.has_key('type') and p['type'] == "port": |
| 283 | dstport = p['_id'] |
| 284 | (sport, sdpid) = portV_to_port_dpid(srcport) |
| 285 | (dport, ddpid) = portV_to_port_dpid(dstport) |
| 286 | link = {} |
| 287 | link["src-switch"]=sdpid |
| 288 | link["src-port"]=sport |
| 289 | link["src-port-state"]=0 |
| 290 | link["dst-switch"]=ddpid |
| 291 | link["dst-port"]=dport |
| 292 | link["dst-port-state"]=0 |
| 293 | link["type"]="internal" |
| 294 | links.append(link) |
| 295 | |
| 296 | pp.pprint(links) |
| 297 | js = json.dumps(links) |
| 298 | resp = Response(js, status=200, mimetype='application/json') |
| 299 | return resp |
| 300 | |
| 301 | if __name__ == "__main__": |
| 302 | if len(sys.argv) > 1 and sys.argv[1] == "-d": |
| 303 | print "-- query all switches --" |
| 304 | query_switch() |
| 305 | print "-- query topo --" |
| 306 | topology_for_gui() |
| 307 | # print "-- query all links --" |
| 308 | # query_links() |
| 309 | # print "-- query all devices --" |
| 310 | # devices() |
| 311 | else: |
| 312 | app.debug = True |
Ubuntu | f6ce96c | 2013-02-07 01:45:07 +0000 | [diff] [blame^] | 313 | # app.run(host="10.0.1.29", port=9000) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 314 | app.run(host="0.0.0.0", port=9000) |