Jonathan Hart | 0198f90 | 2014-02-21 10:45:17 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import json |
| 4 | from urllib2 import Request, urlopen, URLError, HTTPError |
| 5 | from flask import Flask, json, Response, render_template, make_response, request |
| 6 | |
| 7 | ## Global Var for ON.Lab local REST ## |
| 8 | RestIP="localhost" |
| 9 | RestPort=8080 |
| 10 | ONOS_DEFAULT_HOST="localhost" ;# Has to set if LB=False |
| 11 | DEBUG=1 |
| 12 | controllers=["ubuntu1","ubuntu2","ubuntu3","ubuntu4"] |
| 13 | |
| 14 | app = Flask(__name__) |
| 15 | |
| 16 | ## Worker Functions ## |
| 17 | def log_error(txt): |
| 18 | print '%s' % (txt) |
| 19 | |
| 20 | def debug(txt): |
| 21 | if DEBUG: |
| 22 | print '%s' % (txt) |
| 23 | |
| 24 | def node_id(switch_array, dpid): |
| 25 | id = -1 |
| 26 | for i, val in enumerate(switch_array): |
| 27 | if val['name'] == dpid: |
| 28 | id = i |
| 29 | break |
| 30 | |
| 31 | return id |
| 32 | |
| 33 | ###### ONOS REST API ############################## |
| 34 | ## Worker Func ### |
| 35 | def get_json(url): |
| 36 | code = 200; |
| 37 | try: |
| 38 | response = urlopen(url) |
| 39 | except URLError, e: |
| 40 | print "get_json: REST IF %s has issue. Reason: %s" % (url, e.reason) |
| 41 | result = "" |
| 42 | return (500, result) |
| 43 | except HTTPError, e: |
| 44 | print "get_json: REST IF %s has issue. Code %s" % (url, e.code) |
| 45 | result = "" |
| 46 | return (e.code, result) |
| 47 | |
| 48 | result = response.read() |
| 49 | # parsedResult = json.loads(result) |
| 50 | return (code, result) |
| 51 | |
| 52 | ### File Fetch ### |
| 53 | @app.route('/ui/img/<filename>', methods=['GET']) |
| 54 | @app.route('/img/<filename>', methods=['GET']) |
| 55 | @app.route('/css/<filename>', methods=['GET']) |
| 56 | @app.route('/js/models/<filename>', methods=['GET']) |
| 57 | @app.route('/js/views/<filename>', methods=['GET']) |
| 58 | @app.route('/js/<filename>', methods=['GET']) |
| 59 | @app.route('/lib/<filename>', methods=['GET']) |
| 60 | @app.route('/log/<filename>', methods=['GET']) |
| 61 | @app.route('/', methods=['GET']) |
| 62 | @app.route('/<filename>', methods=['GET']) |
| 63 | @app.route('/tpl/<filename>', methods=['GET']) |
| 64 | @app.route('/ons-demo/<filename>', methods=['GET']) |
| 65 | @app.route('/ons-demo/js/<filename>', methods=['GET']) |
| 66 | @app.route('/ons-demo/d3/<filename>', methods=['GET']) |
| 67 | @app.route('/ons-demo/css/<filename>', methods=['GET']) |
| 68 | @app.route('/ons-demo/assets/<filename>', methods=['GET']) |
| 69 | @app.route('/ons-demo/data/<filename>', methods=['GET']) |
| 70 | def return_file(filename="index.html"): |
| 71 | if request.path == "/": |
| 72 | fullpath = "./index.html" |
| 73 | else: |
| 74 | fullpath = str(request.path)[1:] |
| 75 | |
| 76 | try: |
| 77 | open(fullpath) |
| 78 | except: |
| 79 | response = make_response("Cannot find a file: %s" % (fullpath), 500) |
| 80 | response.headers["Content-type"] = "text/html" |
| 81 | return response |
| 82 | |
| 83 | response = make_response(open(fullpath).read()) |
| 84 | suffix = fullpath.split(".")[-1] |
| 85 | |
| 86 | if suffix == "html" or suffix == "htm": |
| 87 | response.headers["Content-type"] = "text/html" |
| 88 | elif suffix == "js": |
| 89 | response.headers["Content-type"] = "application/javascript" |
| 90 | elif suffix == "css": |
| 91 | response.headers["Content-type"] = "text/css" |
| 92 | elif suffix == "png": |
| 93 | response.headers["Content-type"] = "image/png" |
| 94 | elif suffix == "svg": |
| 95 | response.headers["Content-type"] = "image/svg+xml" |
| 96 | |
| 97 | return response |
| 98 | |
| 99 | ## API for ON.Lab local GUI ## |
| 100 | @app.route('/topology', methods=['GET']) |
| 101 | def topology_for_gui(): |
| 102 | try: |
| 103 | #url="http://%s:%s/wm/onos/topology/switches/all/json" % (RestIP, RestPort) |
| 104 | url="http://%s:%s/wm/onos/ng/switches/json" % (RestIP, RestPort) |
| 105 | (code, result) = get_json(url) |
| 106 | parsedResult = json.loads(result) |
| 107 | except: |
| 108 | log_error("REST IF has issue: %s" % url) |
| 109 | log_error("%s" % result) |
| 110 | return |
| 111 | |
| 112 | topo = {} |
| 113 | switches = [] |
| 114 | links = [] |
| 115 | devices = [] |
| 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 | sw['group']= -1 |
| 125 | |
| 126 | if state == "INACTIVE": |
| 127 | sw['group']=0 |
| 128 | switches.append(sw) |
| 129 | |
| 130 | try: |
| 131 | url="http://%s:%s/wm/onos/registry/switches/json" % (RestIP, RestPort) |
| 132 | (code, result) = get_json(url) |
| 133 | parsedResult = json.loads(result) |
| 134 | except: |
| 135 | log_error("REST IF has issue: %s" % url) |
| 136 | log_error("%s" % result) |
| 137 | |
| 138 | for key in parsedResult: |
| 139 | dpid = key |
| 140 | ctrl = parsedResult[dpid][0]['controllerId'] |
| 141 | sw_id = node_id(switches, dpid) |
| 142 | if sw_id != -1: |
| 143 | if switches[sw_id]['group'] != 0: |
| 144 | switches[sw_id]['group'] = controllers.index(ctrl) + 1 |
| 145 | |
| 146 | try: |
| 147 | #url = "http://%s:%s/wm/onos/topology/links/json" % (RestIP, RestPort) |
| 148 | url = "http://%s:%s/wm/onos/ng/links/json" % (RestIP, RestPort) |
| 149 | (code, result) = get_json(url) |
| 150 | parsedResult = json.loads(result) |
| 151 | except: |
| 152 | log_error("REST IF has issue: %s" % url) |
| 153 | log_error("%s" % result) |
| 154 | return |
| 155 | # sys.exit(0) |
| 156 | |
| 157 | for v in parsedResult: |
| 158 | link = {} |
| 159 | if v.has_key('dst-switch'): |
| 160 | dst_dpid = str(v['dst-switch']) |
| 161 | dst_id = node_id(switches, dst_dpid) |
| 162 | if v.has_key('src-switch'): |
| 163 | src_dpid = str(v['src-switch']) |
| 164 | src_id = node_id(switches, src_dpid) |
| 165 | link['source'] = src_id |
| 166 | link['target'] = dst_id |
| 167 | |
| 168 | #onpath = 0 |
| 169 | #for (s,d) in path: |
| 170 | # if s == v['src-switch'] and d == v['dst-switch']: |
| 171 | # onpath = 1 |
| 172 | # break |
| 173 | #link['type'] = onpath |
| 174 | |
| 175 | links.append(link) |
| 176 | |
| 177 | topo['nodes'] = switches |
| 178 | topo['links'] = links |
| 179 | |
| 180 | js = json.dumps(topo) |
| 181 | resp = Response(js, status=200, mimetype='application/json') |
| 182 | return resp |
| 183 | |
| 184 | @app.route("/controller_status") |
| 185 | def controller_status(): |
| 186 | url= "http://%s:%d/wm/onos/registry/controllers/json" % (RestIP, RestPort) |
| 187 | (code, result) = get_json(url) |
| 188 | parsedResult = json.loads(result) |
| 189 | |
| 190 | cont_status=[] |
| 191 | for i in controllers: |
| 192 | status={} |
| 193 | if i in parsedResult: |
| 194 | onos=1 |
| 195 | else: |
| 196 | onos=0 |
| 197 | status["name"]=i |
| 198 | status["onos"]=onos |
| 199 | status["cassandra"]=0 |
| 200 | cont_status.append(status) |
| 201 | |
| 202 | js = json.dumps(cont_status) |
| 203 | resp = Response(js, status=200, mimetype='application/json') |
| 204 | return resp |
| 205 | |
| 206 | if __name__ == "__main__": |
| 207 | app.debug = True |
| 208 | app.run(threaded=True, host="0.0.0.0", port=9000) |