Ray Milkey | 644472e | 2017-06-19 13:34:25 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | Copyright 2017-present Open Networking Laboratory |
| 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 | |
| 17 | from flask import Flask, jsonify, request |
| 18 | |
| 19 | app = Flask(__name__) |
| 20 | controller_list = {} |
| 21 | |
| 22 | """ |
| 23 | Onos Distributed Manager |
| 24 | this app handles controller information in system |
| 25 | |
| 26 | Message Samples : |
| 27 | --Adding node |
| 28 | curl -H "Content-Type: application/json" -X POST |
| 29 | --data '{"Id":"1.2.3.4","IpAddress":"1.2.3.4","Port":3456,"IsEnable":true}' http://localhost:5000 |
| 30 | --Updating node |
| 31 | curl -H "Content-Type: application/json" -X PUT --data '{"Id":"1.2.3.4","IsEnable":false}' http://localhost:5000 |
| 32 | --Deleting node |
| 33 | curl -H "Content-Type: application/json" -X DELETE --data '{"Id":"1.2.3.4","IsEnable":false}' http://localhost:5000 |
| 34 | --Getting node data |
| 35 | curl -X GET http://10.15.176.228:5000/cluster.json |
| 36 | |
| 37 | """ |
| 38 | |
| 39 | httpStatusOK = 200 |
| 40 | httpStatusNotFound = 404 |
| 41 | httpStatusBadRequest = 400 |
| 42 | |
| 43 | @app.route('/', methods=['GET']) |
| 44 | def data_get_handler(): |
| 45 | |
| 46 | pagereturn = "<h2> Onos Distributed Controller Manager2 </h2>" |
| 47 | pagereturn += "<br><h3> Status of Added controllers </h3><br>" |
| 48 | pagereturn += " Id,  Ip,  Port,  Is Active <br> " |
| 49 | |
| 50 | for key in controller_list.keys(): |
| 51 | pagereturn += controller_list[key]["Id"] + ",  " + \ |
| 52 | controller_list[key]["IpAddress"] + ",  " + \ |
| 53 | str(controller_list[key]["Port"]) + ",  " + \ |
| 54 | str(controller_list[key]["IsEnable"]) |
| 55 | pagereturn += " <br>" |
| 56 | |
| 57 | return pagereturn, httpStatusOK |
| 58 | |
| 59 | |
| 60 | @app.route('/', methods=['POST']) |
| 61 | def data_post_handler(): |
| 62 | |
| 63 | if request.is_json: |
| 64 | content = dict(request.json) |
| 65 | |
| 66 | if content["Id"] in controller_list: |
| 67 | return "Content Id is already in the list", httpStatusBadRequest |
| 68 | |
| 69 | else: |
| 70 | controller_list[content["Id"]] = content |
| 71 | |
| 72 | else: |
| 73 | return "json required", httpStatusBadRequest |
| 74 | |
| 75 | return "POST called with content", httpStatusOK |
| 76 | |
| 77 | |
| 78 | @app.route('/', methods=['PUT']) |
| 79 | def data_put_handler(): |
| 80 | |
| 81 | if request.is_json: |
| 82 | content = dict(request.json) |
| 83 | |
| 84 | if content["Id"] in controller_list: |
| 85 | controller_list[content["Id"]] = content |
| 86 | return "Update succeeded", httpStatusOK |
| 87 | |
| 88 | else: |
| 89 | return "Id %s is not found " %(content["Id"]), httpStatusNotFound |
| 90 | else: |
| 91 | return "json data is missing", httpStatusBadRequest |
| 92 | |
| 93 | |
| 94 | @app.route('/', methods=['DELETE']) |
| 95 | def data_delete_handler(): |
| 96 | |
| 97 | if request.is_json: |
| 98 | content = dict(request.json) |
| 99 | |
| 100 | else: |
| 101 | return "No json is found", httpStatusBadRequest |
| 102 | |
| 103 | if content["Id"] in controller_list: |
| 104 | del controller_list[content["Id"]] |
| 105 | return "Deletion succeed.", httpStatusOK |
| 106 | |
| 107 | else: |
| 108 | return "Id is not found", httpStatusNotFound |
| 109 | |
| 110 | """ |
| 111 | This function returns onos cluster information |
| 112 | based on data in memory |
| 113 | """ |
| 114 | |
| 115 | |
| 116 | @app.route('/cluster.json', methods=['GET']) |
| 117 | def cluster_responder(): |
| 118 | |
| 119 | cluster_info = dict() |
| 120 | nodes = list() |
| 121 | partition = dict() |
| 122 | # Todo: For first release , only 1 partition implemented |
| 123 | cluster_members = list() |
| 124 | |
| 125 | # "nodes" array |
| 126 | for controller_id in controller_list: |
| 127 | controller_node = controller_list[controller_id] |
| 128 | if controller_node["IsEnable"]: |
| 129 | node_data = dict() |
| 130 | node_data["ip"] = controller_node["IpAddress"] |
| 131 | node_data["id"] = controller_node["Id"] |
| 132 | node_data["port"] = controller_node["Port"] |
| 133 | nodes.append(node_data) |
| 134 | cluster_members.append(controller_node["Id"]) |
| 135 | |
| 136 | partition["id"] = 1 # Todo: this will be updated . |
| 137 | partition["members"] = cluster_members |
| 138 | |
| 139 | cluster_info["nodes"] = nodes |
| 140 | cluster_info["name"] = -1394421542720337000 |
| 141 | cluster_info["partitions"] = partition |
| 142 | return jsonify(cluster_info), httpStatusOK |
| 143 | |
| 144 | if __name__ == '__main__': |
| 145 | app.run(host="0.0.0.0", debug=True) |