Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2020-present Open Networking Foundation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import time |
| 18 | from flask import Flask, jsonify, abort, request |
| 19 | |
| 20 | app = Flask(__name__) |
| 21 | edges = [ |
| 22 | { |
| 23 | 'name': 'production-edge-example', |
| 24 | 'status': { |
| 25 | 'control_plane': 'connected', |
| 26 | 'user_plane': 'connected' |
| 27 | }, |
| 28 | 'last_update': time.time() |
| 29 | } |
| 30 | ] |
| 31 | |
| 32 | |
| 33 | @app.route('/edges/healthz', methods=['GET']) |
| 34 | def get_health(): |
| 35 | return {'message': 'healthy'} |
| 36 | |
| 37 | |
| 38 | @app.route('/edges', methods=['GET']) |
| 39 | def get_edges(): |
| 40 | return jsonify({'edges': edges}) |
| 41 | |
| 42 | |
| 43 | @app.route('/edges/<string:name>', methods=['GET']) |
| 44 | def get_edge(name): |
| 45 | edge = [edge for edge in edges if edge['name'] == name] |
| 46 | if len(edge) == 0: |
| 47 | abort(404) |
| 48 | return jsonify({'edge': edge[0]}) |
| 49 | |
| 50 | |
| 51 | @app.route('/edges', methods=['POST']) |
| 52 | def create_or_update_edge(): |
| 53 | if not request.json: |
| 54 | abort(400) |
| 55 | if 'name' not in request.json: |
| 56 | abort(400) |
| 57 | if 'status' not in request.json: |
| 58 | abort(400) |
| 59 | |
| 60 | req_edge = { |
| 61 | 'name': request.json['name'], |
| 62 | 'status': { |
| 63 | 'control_plane': request.json['status']['control_plane'], |
| 64 | 'user_plane': request.json['status']['user_plane'] |
| 65 | }, |
| 66 | 'last_update': time.time() |
| 67 | } |
| 68 | |
| 69 | edge = [edge for edge in edges if edge['name'] == req_edge['name']] |
| 70 | if len(edge) == 0: |
| 71 | print("new edge request " + req_edge['name']) |
| 72 | edges.append(req_edge) |
| 73 | else: |
| 74 | edge[0]['status']['control_plane'] = req_edge['status']['control_plane'] |
| 75 | edge[0]['status']['user_plane'] = req_edge['status']['user_plane'] |
| 76 | edge[0]['last_update'] = req_edge['last_update'] |
| 77 | |
| 78 | return jsonify({'edge': req_edge}), 201 |
| 79 | |
| 80 | |
| 81 | if __name__ == '__main__': |
| 82 | app.run(debug=True, host='0.0.0.0', port=80) |