Rizwan Haider | 8e5f477 | 2016-08-17 18:04:35 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import copy |
| 4 | import json |
| 5 | from flask import Flask, make_response, request |
| 6 | from flask.ext.cors import CORS |
| 7 | from subprocess import call |
| 8 | |
| 9 | app = Flask(__name__) |
| 10 | app.debug = True |
| 11 | CORS(app) |
| 12 | |
| 13 | e_lines = {} |
| 14 | |
| 15 | |
| 16 | @app.route('/SCA_ETH_FDFr_EC/findByState', methods=['GET']) |
| 17 | def get_elines_by_state(): |
| 18 | resp = make_response(json.dumps(e_lines.values())) |
| 19 | resp.mimetype = 'application/json' |
| 20 | resp.headers['Access-Control-Allow-Origin'] = '*' |
| 21 | return resp |
| 22 | |
| 23 | |
| 24 | @app.route('/SCA_ETH_FDFr_EC', methods=['GET']) |
| 25 | def get_elines(): |
| 26 | resp = make_response(json.dumps(e_lines.values())) |
| 27 | resp.mimetype = 'application/json' |
| 28 | resp.headers['Access-Control-Allow-Origin'] = '*' |
| 29 | return resp |
| 30 | |
| 31 | |
| 32 | @app.route('/SCA_ETH_FDFr_EC/<name>/', methods=['GET']) |
| 33 | def get_eline(name): |
| 34 | resp = make_response(json.dumps(e_lines[int(name)])) |
| 35 | resp.mimetype = 'application/json' |
| 36 | resp.headers['Access-Control-Allow-Origin'] = '*' |
| 37 | return resp |
| 38 | |
| 39 | |
| 40 | # Convert long value to dotted hex value with specified length in bytes |
| 41 | def longToHex(l, length=6): |
| 42 | h = ("%x" % l) |
| 43 | if len(h) % 2 != 0: |
| 44 | h = '0' + h |
| 45 | result = ':'.join([h[i:i+2] for i in range(0, len(h), 2)]) |
| 46 | prefix = '00:' * (length - (len(h) / 2) - (len(h) % 2)) |
| 47 | return prefix + result |
| 48 | |
| 49 | |
| 50 | @app.route('/SCA_ETH_FDFr_EC', methods=['POST']) |
| 51 | def create_eline(): |
| 52 | # Store E-Line |
| 53 | e_line = json.loads(request.data) |
| 54 | e_line['id'] = len(e_lines) + 1 |
| 55 | |
| 56 | e_lines[e_line['id']] = e_line |
| 57 | |
| 58 | # Create E-Line in ONOS |
| 59 | flow_points = e_line['SCA_ETH_Flow_Points'] |
| 60 | |
| 61 | # src_host = flow_points[0]['scaEthFppUniN']['transportPort']['Hostname'] + '/-' + flow_points[0]['scaEthFppUniN']['transportPort']['Port'] |
| 62 | # dst_host = flow_points[1]['scaEthFppUniN']['transportPort']['Hostname'] + '/-' + flow_points[1]['scaEthFppUniN']['transportPort']['Port'] |
| 63 | |
| 64 | src_index = int(flow_points[0]['scaEthFppUniN']['transportPort']['Hostname'][-2:]) |
| 65 | dst_index = int(flow_points[1]['scaEthFppUniN']['transportPort']['Hostname'][-2:]) |
| 66 | |
| 67 | src_host = str(longToHex(src_index, 6)) + '/-1' |
| 68 | dst_host = str(longToHex(dst_index, 6)) + '/-1' |
| 69 | |
| 70 | print 'Creating E-Line between %s (%s) and %s (%s)' % (src_index, src_host, dst_index, dst_host) |
| 71 | call(['onos', 'localhost', 'add-host-intent', src_host, dst_host]) |
| 72 | |
| 73 | # Return response |
| 74 | resp = make_response(json.dumps(e_line)) |
| 75 | resp.mimetype = 'application/json' |
| 76 | resp.headers['Access-Control-Allow-Origin'] = '*' |
| 77 | return resp |
| 78 | |
| 79 | if __name__ == '__main__': |
| 80 | app.run(host='0.0.0.0', port=6000) |