Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame^] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Rizwan Haider | 8e5f477 | 2016-08-17 18:04:35 -0400 | [diff] [blame] | 17 | #!/usr/bin/python |
| 18 | |
| 19 | import copy |
| 20 | import json |
| 21 | from flask import Flask, make_response, request |
| 22 | from flask.ext.cors import CORS |
| 23 | from subprocess import call |
| 24 | |
| 25 | app = Flask(__name__) |
| 26 | app.debug = True |
| 27 | CORS(app) |
| 28 | |
| 29 | e_lines = {} |
| 30 | |
| 31 | |
| 32 | @app.route('/SCA_ETH_FDFr_EC/findByState', methods=['GET']) |
| 33 | def get_elines_by_state(): |
| 34 | resp = make_response(json.dumps(e_lines.values())) |
| 35 | resp.mimetype = 'application/json' |
| 36 | resp.headers['Access-Control-Allow-Origin'] = '*' |
| 37 | return resp |
| 38 | |
| 39 | |
| 40 | @app.route('/SCA_ETH_FDFr_EC', methods=['GET']) |
| 41 | def get_elines(): |
| 42 | resp = make_response(json.dumps(e_lines.values())) |
| 43 | resp.mimetype = 'application/json' |
| 44 | resp.headers['Access-Control-Allow-Origin'] = '*' |
| 45 | return resp |
| 46 | |
| 47 | |
| 48 | @app.route('/SCA_ETH_FDFr_EC/<name>/', methods=['GET']) |
| 49 | def get_eline(name): |
| 50 | resp = make_response(json.dumps(e_lines[int(name)])) |
| 51 | resp.mimetype = 'application/json' |
| 52 | resp.headers['Access-Control-Allow-Origin'] = '*' |
| 53 | return resp |
| 54 | |
| 55 | |
| 56 | # Convert long value to dotted hex value with specified length in bytes |
| 57 | def longToHex(l, length=6): |
| 58 | h = ("%x" % l) |
| 59 | if len(h) % 2 != 0: |
| 60 | h = '0' + h |
| 61 | result = ':'.join([h[i:i+2] for i in range(0, len(h), 2)]) |
| 62 | prefix = '00:' * (length - (len(h) / 2) - (len(h) % 2)) |
| 63 | return prefix + result |
| 64 | |
| 65 | |
| 66 | @app.route('/SCA_ETH_FDFr_EC', methods=['POST']) |
| 67 | def create_eline(): |
| 68 | # Store E-Line |
| 69 | e_line = json.loads(request.data) |
| 70 | e_line['id'] = len(e_lines) + 1 |
| 71 | |
| 72 | e_lines[e_line['id']] = e_line |
| 73 | |
| 74 | # Create E-Line in ONOS |
| 75 | flow_points = e_line['SCA_ETH_Flow_Points'] |
| 76 | |
| 77 | # src_host = flow_points[0]['scaEthFppUniN']['transportPort']['Hostname'] + '/-' + flow_points[0]['scaEthFppUniN']['transportPort']['Port'] |
| 78 | # dst_host = flow_points[1]['scaEthFppUniN']['transportPort']['Hostname'] + '/-' + flow_points[1]['scaEthFppUniN']['transportPort']['Port'] |
| 79 | |
| 80 | src_index = int(flow_points[0]['scaEthFppUniN']['transportPort']['Hostname'][-2:]) |
| 81 | dst_index = int(flow_points[1]['scaEthFppUniN']['transportPort']['Hostname'][-2:]) |
| 82 | |
| 83 | src_host = str(longToHex(src_index, 6)) + '/-1' |
| 84 | dst_host = str(longToHex(dst_index, 6)) + '/-1' |
| 85 | |
| 86 | print 'Creating E-Line between %s (%s) and %s (%s)' % (src_index, src_host, dst_index, dst_host) |
| 87 | call(['onos', 'localhost', 'add-host-intent', src_host, dst_host]) |
| 88 | |
| 89 | # Return response |
| 90 | resp = make_response(json.dumps(e_line)) |
| 91 | resp.mimetype = 'application/json' |
| 92 | resp.headers['Access-Control-Allow-Origin'] = '*' |
| 93 | return resp |
| 94 | |
| 95 | if __name__ == '__main__': |
| 96 | app.run(host='0.0.0.0', port=6000) |