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