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 | |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame^] | 102 | @app.route('/topology', methods=['GET']) |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 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": |
Masayoshi Kobayashi | 274c07d | 2013-02-20 21:38:16 +0000 | [diff] [blame] | 125 | if dpid[-2:-1] == "a": |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 126 | sw['group']=1 |
Masayoshi Kobayashi | 274c07d | 2013-02-20 21:38:16 +0000 | [diff] [blame] | 127 | if dpid[-2:-1] == "b": |
| 128 | sw['group']=2 |
| 129 | if dpid[-2:-1] == "c": |
| 130 | sw['group']=3 |
| 131 | if str(v['state']) == "INACTIVE": |
| 132 | sw['group']=0 |
| 133 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 134 | |
| 135 | switches.append(sw) |
| 136 | |
| 137 | try: |
| 138 | command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort) |
| 139 | result = os.popen(command).read() |
| 140 | parsedResult = json.loads(result) |
| 141 | except: |
| 142 | log_error("REST IF has issue: %s" % command) |
| 143 | log_error("%s" % result) |
| 144 | sys.exit(0) |
| 145 | |
| 146 | for v in parsedResult: |
| 147 | link = {} |
| 148 | if v.has_key('dst-switch'): |
| 149 | dst_dpid = str(v['dst-switch']) |
| 150 | dst_id = node_id(switches, dst_dpid) |
| 151 | if v.has_key('src-switch'): |
| 152 | src_dpid = str(v['src-switch']) |
| 153 | src_id = node_id(switches, src_dpid) |
| 154 | link['source'] = src_id |
| 155 | link['target'] = dst_id |
| 156 | links.append(link) |
| 157 | |
| 158 | topo['nodes'] = switches |
| 159 | topo['links'] = links |
| 160 | |
| 161 | pp.pprint(topo) |
| 162 | js = json.dumps(topo) |
| 163 | resp = Response(js, status=200, mimetype='application/json') |
| 164 | return resp |
| 165 | |
| 166 | |
Ubuntu | aea2a68 | 2013-02-08 08:30:10 +0000 | [diff] [blame] | 167 | #@app.route("/wm/topology/toporoute/00:00:00:00:00:a1/2/00:00:00:00:00:c1/3/json") |
| 168 | #@app.route("/wm/topology/toporoute/<srcdpid>/<srcport>/<destdpid>/<destport>/json") |
| 169 | @app.route("/wm/topology/toporoute/<v1>/<p1>/<v2>/<p2>/json") |
| 170 | def shortest_path(v1, p1, v2, p2): |
| 171 | try: |
| 172 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 173 | result = os.popen(command).read() |
| 174 | parsedResult = json.loads(result) |
| 175 | except: |
| 176 | log_error("REST IF has issue: %s" % command) |
| 177 | log_error("%s" % result) |
| 178 | sys.exit(0) |
| 179 | |
| 180 | topo = {} |
| 181 | switches = [] |
| 182 | links = [] |
| 183 | |
| 184 | for v in parsedResult: |
| 185 | if v.has_key('dpid'): |
| 186 | dpid = str(v['dpid']) |
| 187 | state = str(v['state']) |
| 188 | sw = {} |
| 189 | sw['name']=dpid |
| 190 | if str(v['state']) == "ACTIVE": |
| 191 | if dpid[-2:-1] == "a": |
| 192 | sw['group']=1 |
| 193 | if dpid[-2:-1] == "b": |
| 194 | sw['group']=2 |
| 195 | if dpid[-2:-1] == "c": |
| 196 | sw['group']=3 |
| 197 | if str(v['state']) == "INACTIVE": |
| 198 | sw['group']=0 |
| 199 | |
| 200 | switches.append(sw) |
| 201 | |
| 202 | try: |
| 203 | command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (RestIP, RestPort, v1, p1, v2, p2) |
| 204 | result = os.popen(command).read() |
| 205 | parsedResult = json.loads(result) |
| 206 | except: |
| 207 | log_error("No route") |
| 208 | parsedResult = [] |
| 209 | # exit(1) |
| 210 | |
| 211 | path = []; |
| 212 | for i, v in enumerate(parsedResult): |
| 213 | if i < len(parsedResult) - 1: |
| 214 | sdpid= parsedResult[i]['switch'] |
| 215 | ddpid = parsedResult[i+1]['switch'] |
| 216 | path.append( (sdpid, ddpid)) |
| 217 | |
| 218 | try: |
| 219 | command = "curl -s \'http://%s:%s/wm/core/topology/links/json\'" % (RestIP, RestPort) |
| 220 | result = os.popen(command).read() |
| 221 | parsedResult = json.loads(result) |
| 222 | except: |
| 223 | log_error("REST IF has issue: %s" % command) |
| 224 | log_error("%s" % result) |
| 225 | sys.exit(0) |
| 226 | |
| 227 | for v in parsedResult: |
| 228 | link = {} |
| 229 | if v.has_key('dst-switch'): |
| 230 | dst_dpid = str(v['dst-switch']) |
| 231 | dst_id = node_id(switches, dst_dpid) |
| 232 | if v.has_key('src-switch'): |
| 233 | src_dpid = str(v['src-switch']) |
| 234 | src_id = node_id(switches, src_dpid) |
| 235 | link['source'] = src_id |
| 236 | link['target'] = dst_id |
| 237 | onpath = 0 |
| 238 | for (s,d) in path: |
| 239 | if s == v['src-switch'] and d == v['dst-switch']: |
| 240 | onpath = 1 |
| 241 | break |
| 242 | |
| 243 | link['type'] = onpath |
| 244 | links.append(link) |
| 245 | |
| 246 | topo['nodes'] = switches |
| 247 | topo['links'] = links |
| 248 | |
| 249 | pp.pprint(topo) |
| 250 | js = json.dumps(topo) |
| 251 | resp = Response(js, status=200, mimetype='application/json') |
| 252 | return resp |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 258 | @app.route("/wm/core/controller/switches/json") |
| 259 | def query_switch(): |
| 260 | try: |
| 261 | command = "curl -s \'http://%s:%s/wm/core/topology/switches/all/json\'" % (RestIP, RestPort) |
| 262 | # http://localhost:8080/wm/core/topology/switches/active/json |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame^] | 263 | print command |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 264 | result = os.popen(command).read() |
| 265 | parsedResult = json.loads(result) |
| 266 | except: |
| 267 | log_error("REST IF has issue: %s" % command) |
| 268 | log_error("%s" % result) |
| 269 | sys.exit(0) |
| 270 | |
| 271 | # print command |
| 272 | # print result |
| 273 | switches_ = [] |
| 274 | for v in parsedResult: |
| 275 | if v.has_key('dpid'): |
| 276 | if v.has_key('dpid') and str(v['state']) == "ACTIVE":#;if you want only ACTIVE nodes |
| 277 | dpid = str(v['dpid']) |
| 278 | state = str(v['state']) |
| 279 | sw = {} |
| 280 | sw['dpid']=dpid |
| 281 | sw['active']=state |
| 282 | switches_.append(sw) |
| 283 | |
| 284 | pp.pprint(switches_) |
| 285 | js = json.dumps(switches_) |
| 286 | resp = Response(js, status=200, mimetype='application/json') |
| 287 | return resp |
| 288 | |
| 289 | @app.route("/wm/device/") |
| 290 | def devices(): |
| 291 | try: |
| 292 | command = "curl -s http://%s:%s/graphs/%s/vertices\?key=type\&value=device" % (RestIP, RestPort, DBName) |
| 293 | result = os.popen(command).read() |
| 294 | parsedResult = json.loads(result)['results'] |
| 295 | except: |
| 296 | log_error("REST IF has issue: %s" % command) |
| 297 | log_error("%s" % result) |
| 298 | sys.exit(0) |
| 299 | |
| 300 | devices = [] |
| 301 | for v in parsedResult: |
| 302 | dl_addr = v['dl_addr'] |
| 303 | nw_addr = v['nw_addr'] |
| 304 | vertex = v['_id'] |
| 305 | mac = [] |
| 306 | mac.append(dl_addr) |
| 307 | ip = [] |
| 308 | ip.append(nw_addr) |
| 309 | device = {} |
| 310 | device['entryClass']="DefaultEntryClass" |
| 311 | device['mac']=mac |
| 312 | device['ipv4']=ip |
| 313 | device['vlan']=[] |
| 314 | device['lastSeen']=0 |
| 315 | attachpoints =[] |
| 316 | |
| 317 | port, dpid = deviceV_to_attachpoint(vertex) |
| 318 | attachpoint = {} |
| 319 | attachpoint['port']=port |
| 320 | attachpoint['switchDPID']=dpid |
| 321 | attachpoints.append(attachpoint) |
| 322 | device['attachmentPoint']=attachpoints |
| 323 | devices.append(device) |
| 324 | |
| 325 | print devices |
| 326 | js = json.dumps(devices) |
| 327 | resp = Response(js, status=200, mimetype='application/json') |
| 328 | return resp |
| 329 | |
| 330 | #{"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} |
| 331 | |
| 332 | |
| 333 | ## return fake stat for now |
| 334 | @app.route("/wm/core/switch/<switchId>/<statType>/json") |
| 335 | def switch_stat(switchId, statType): |
| 336 | if statType == "desc": |
| 337 | desc=[{"length":1056,"serialNumber":"None","manufacturerDescription":"Nicira Networks, Inc.","hardwareDescription":"Open vSwitch","softwareDescription":"1.4.0+build0","datapathDescription":"None"}] |
| 338 | ret = {} |
| 339 | ret[switchId]=desc |
| 340 | elif statType == "aggregate": |
| 341 | aggr = {"packetCount":0,"byteCount":0,"flowCount":0} |
| 342 | ret = {} |
| 343 | ret[switchId]=aggr |
| 344 | else: |
| 345 | ret = {} |
| 346 | |
| 347 | js = json.dumps(ret) |
| 348 | resp = Response(js, status=200, mimetype='application/json') |
| 349 | return resp |
| 350 | |
| 351 | |
| 352 | @app.route("/wm/topology/links/json") |
| 353 | def query_links(): |
| 354 | try: |
| 355 | command = 'curl -s http://%s:%s/graphs/%s/vertices?key=type\&value=port' % (RestIP, RestPort, DBName) |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame^] | 356 | print command |
Ubuntu | 82b8a83 | 2013-02-06 22:00:11 +0000 | [diff] [blame] | 357 | result = os.popen(command).read() |
| 358 | parsedResult = json.loads(result)['results'] |
| 359 | except: |
| 360 | log_error("REST IF has issue: %s" % command) |
| 361 | log_error("%s" % result) |
| 362 | sys.exit(0) |
| 363 | |
| 364 | debug("query_links %s" % command) |
| 365 | pp.pprint(parsedResult) |
| 366 | sport = [] |
| 367 | links = [] |
| 368 | for v in parsedResult: |
| 369 | srcport = v['_id'] |
| 370 | try: |
| 371 | command = "curl -s http://%s:%s/graphs/%s/vertices/%d/out?_label=link" % (RestIP, RestPort, DBName, srcport) |
| 372 | print command |
| 373 | result = os.popen(command).read() |
| 374 | linkResults = json.loads(result)['results'] |
| 375 | except: |
| 376 | log_error("REST IF has issue: %s" % command) |
| 377 | log_error("%s" % result) |
| 378 | sys.exit(0) |
| 379 | |
| 380 | for p in linkResults: |
| 381 | if p.has_key('type') and p['type'] == "port": |
| 382 | dstport = p['_id'] |
| 383 | (sport, sdpid) = portV_to_port_dpid(srcport) |
| 384 | (dport, ddpid) = portV_to_port_dpid(dstport) |
| 385 | link = {} |
| 386 | link["src-switch"]=sdpid |
| 387 | link["src-port"]=sport |
| 388 | link["src-port-state"]=0 |
| 389 | link["dst-switch"]=ddpid |
| 390 | link["dst-port"]=dport |
| 391 | link["dst-port-state"]=0 |
| 392 | link["type"]="internal" |
| 393 | links.append(link) |
| 394 | |
| 395 | pp.pprint(links) |
| 396 | js = json.dumps(links) |
| 397 | resp = Response(js, status=200, mimetype='application/json') |
| 398 | return resp |
| 399 | |
| 400 | if __name__ == "__main__": |
| 401 | if len(sys.argv) > 1 and sys.argv[1] == "-d": |
| 402 | print "-- query all switches --" |
| 403 | query_switch() |
| 404 | print "-- query topo --" |
| 405 | topology_for_gui() |
| 406 | # print "-- query all links --" |
| 407 | # query_links() |
| 408 | # print "-- query all devices --" |
| 409 | # devices() |
| 410 | else: |
| 411 | app.debug = True |
Masayoshi Kobayashi | f63ef2f | 2013-02-20 21:47:21 +0000 | [diff] [blame^] | 412 | app.run(threaded=True, host="0.0.0.0", port=9000) |