Murat Parlakisik | 042c3a5 | 2017-04-17 10:25:43 -0700 | [diff] [blame] | 1 | #!/bin/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 | |
| 40 | @app.route('/', methods=['GET', 'POST', 'DELETE', 'PUT']) |
| 41 | def data_handler(): |
| 42 | |
| 43 | if request.method == 'GET': |
| 44 | pagereturn = "<h2> Onos Distributed Controller Manager2 </h2>" |
| 45 | pagereturn += "<br><h3> Status of Added controllers </h3><br>" |
| 46 | pagereturn += " Id,  Ip,  Port,  Is Active <br> " |
| 47 | |
| 48 | for key in controller_list.keys(): |
| 49 | pagereturn += controller_list[key]["Id"] + ",  " + \ |
| 50 | controller_list[key]["IpAddress"] + ",  " + \ |
| 51 | str(controller_list[key]["Port"]) + ",  " + \ |
| 52 | str(controller_list[key]["IsEnable"]) |
| 53 | pagereturn += " <br>" |
| 54 | |
| 55 | return pagereturn |
| 56 | elif request.method == 'POST': |
| 57 | |
| 58 | if request.is_json: |
| 59 | content = dict(request.json) |
| 60 | |
| 61 | if content["Id"] in controller_list: |
| 62 | return "Content Id is already in the list" |
| 63 | |
| 64 | else: |
| 65 | controller_list[content["Id"]] = content |
| 66 | |
| 67 | else: |
| 68 | return "json required" |
| 69 | |
| 70 | return "POST called with content" |
| 71 | |
| 72 | elif request.method == 'PUT': |
| 73 | if request.is_json: |
| 74 | content = dict(request.json) |
| 75 | |
| 76 | if content["Id"] in controller_list: |
| 77 | controller_list[content["Id"]] = content |
| 78 | |
| 79 | else: |
| 80 | return "Id %s is not found ", content["Id"] |
| 81 | else: |
| 82 | return "json data is missing" |
| 83 | |
| 84 | elif request.method == 'DELETE': |
| 85 | if request.is_json: |
| 86 | content = dict(request.json) |
| 87 | |
| 88 | else: |
| 89 | return "No json is found" |
| 90 | |
| 91 | if content["Id"] in controller_list: |
| 92 | del controller_list[content["Id"]] |
| 93 | return "Deletion succeed." |
| 94 | |
| 95 | else: |
| 96 | return "Id is not found" |
| 97 | |
| 98 | else: |
| 99 | return "Undefined method call" |
| 100 | """ |
| 101 | This function returns onos cluster information |
| 102 | based on data is |
| 103 | """ |
| 104 | |
| 105 | |
| 106 | @app.route('/cluster.json', methods=['GET']) |
| 107 | def cluster_responder(): |
| 108 | |
| 109 | cluster_info = dict() |
| 110 | nodes = list() |
| 111 | partition = dict() |
| 112 | # Todo: For first release , only 1 partition implemented |
| 113 | cluster_members = list() |
| 114 | |
| 115 | # "nodes" array |
| 116 | for controller_id in controller_list: |
| 117 | controller_node = controller_list[controller_id] |
| 118 | if controller_node["IsEnable"]: |
| 119 | node_data = dict() |
| 120 | node_data["ip"] = controller_node["IpAddress"] |
| 121 | node_data["id"] = controller_node["Id"] |
| 122 | node_data["port"] = controller_node["Port"] |
| 123 | nodes.append(node_data) |
| 124 | cluster_members.append(controller_node["Id"]) |
| 125 | |
| 126 | partition["id"] = 1 # Todo: this will be updated . |
| 127 | partition["members"] = cluster_members |
| 128 | |
| 129 | cluster_info["nodes"] = nodes |
| 130 | cluster_info["name"] = -1394421542720337000 |
| 131 | cluster_info["partitions"] = partition |
| 132 | return jsonify(cluster_info) |
| 133 | |
| 134 | if __name__ == '__main__': |
| 135 | app.run(host="0.0.0.0", debug=True) |