1 # ============LICENSE_START===============================================
2 # Copyright (C) 2020 Nordix Foundation. All rights reserved.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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 # ============LICENSE_END=================================================
25 from pathlib import Path
26 from flask import Flask, escape, request, make_response
27 from jsonschema import validate
28 from var_declaration import policy_instances, policy_types, policy_status, policy_type_per_instance, hosts_set
29 from maincommon import *
33 app = connexion.App(__name__, specification_dir=apipath)
35 @app.route('/policytypes/<string:policyTypeId>', methods=['PUT','DELETE'])
36 def policy_type(policyTypeId):
37 if request.method == 'PUT':
38 data = request.data.decode("utf-8")
39 data = data.replace("'", "\"")
40 data = json.loads(data)
41 policy_types[policyTypeId] = data
42 return ('The policy type was either created or updated for policy type id: ' + policyTypeId, 200)
43 elif request.method == 'DELETE':
44 if policyTypeId in policy_types.keys():
45 policy_types.pop(policyTypeId)
46 return make_response("policy type successfully deleted for policy type id: " + policyTypeId, 200)
48 return make_response("No policy type defined for the specified id", 404)
50 @app.route('/', methods=['GET'])
52 return("Everything is fine", 200)
54 @app.route('/deleteinstances', methods=['DELETE'])
55 def delete_instances():
56 global policy_instances
58 global policy_type_per_instance
59 policy_instances.clear()
61 policy_type_per_instance.clear()
62 return("All policy instances deleted", 200)
64 @app.route('/deletetypes', methods=['DELETE'])
68 return("All policy types deleted", 200)
70 @app.route('/<string:policyId>/<string:enforceStatus>', methods=['PUT'])
71 def set_status(policyId, enforceStatus):
72 if policyId in policy_instances.keys():
73 if policy_type_per_instance[policyId] == "UNDEFINED":
75 ps["policyId"] = policyId
76 ps["enforceStatus"] = enforceStatus
78 policy_type_id = policy_type_per_instance[policyId]
79 status_schema = policy_types[policy_type_id]["statusSchema"]
81 ps["policyId"] = policyId
82 ps["enforceStatus"] = enforceStatus
84 validate(instance=ps, schema=status_schema)
86 return(set_error(None, "The json does not validate against the status schema.", 400, None, None, None, None, None))
87 policy_status.pop(policyId)
88 policy_status[policyId] = ps
89 return("Status updated for policy: " + policyId, 200)
91 @app.route('/<string:policyId>/<string:enforceStatus>/<string:enforceReason>', methods=['PUT'])
92 def set_status_with_reason(policyId, enforceStatus, enforceReason):
93 if policyId in policy_instances.keys():
94 if policy_type_per_instance[policyId] == "UNDEFINED":
96 ps["policyId"] = policyId
97 ps["enforceStatus"] = enforceStatus
98 ps["enforceReason"] = enforceReason
100 policy_type_id = policy_type_per_instance[policyId]
101 status_schema = policy_types[policy_type_id]["statusSchema"]
103 ps["policyId"] = policyId
104 ps["enforceStatus"] = enforceStatus
105 ps["enforceReason"] = enforceReason
107 validate(instance=ps, schema=status_schema)
109 return(set_error(None, "The json does not validate against the status schema.", 400, None, None, None, None, None))
110 policy_status.pop(policyId)
111 policy_status[policyId] = ps
112 return("Status updated for policy: " + policyId, 200)
116 @app.route('/counter/<string:countername>', methods=['GET'])
117 def getCounter(countername):
118 if (countername == "num_instances"):
119 return str(len(policy_instances)),200
120 elif (countername == "num_types"):
121 return str(len(policy_types)),200
122 elif (countername == "interface"):
125 return str(pp[len(pp)-1]),200
126 elif (countername == "remote_hosts"):
127 hosts=",".join(hosts_set)
128 return str(hosts),200
130 return "Counter name: "+countername+" not found.",404
134 if len(sys.argv) >= 2:
135 if isinstance(sys.argv[1], int):
136 port_number = sys.argv[1]
138 app.add_api('a1-openapi.yaml')
140 app.run(port=port_number, host="127.0.0.1", threaded=False)