2 # ============LICENSE_START===============================================
3 # Copyright (C) 2020 Nordix Foundation. All rights reserved.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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 # ============LICENSE_END=================================================
24 from flask import Flask, escape, request, make_response
25 from jsonschema import validate
26 from var_declaration import policy_instances, policy_types, policy_status, policy_type_per_instance
28 app = connexion.App(__name__, specification_dir='.')
30 @app.route('/policytypes/<string:policyTypeId>', methods=['PUT','DELETE'])
31 def policy_type(policyTypeId):
32 if request.method == 'PUT':
33 data = request.data.decode("utf-8")
34 data = data.replace("'", "\"")
35 data = json.loads(data)
36 policy_types[policyTypeId] = data
37 return ('The policy type was either created or updated for policy type id: ' + policyTypeId)
38 elif request.method == 'DELETE':
39 if policyTypeId in policy_types.keys():
40 policy_types.pop(policyTypeId)
41 return make_response("policy type successfully deleted for policy type id: " + policyTypeId, 200)
43 return make_response("No policy type defined for the specified id", 404)
45 @app.route('/', methods=['GET'])
47 return("Everything is fine", 200)
49 @app.route('/deleteinstances', methods=['DELETE'])
50 def delete_instances():
51 global policy_instances
53 global policy_type_per_instance
54 policy_instances.clear()
56 policy_type_per_instance.clear()
57 return("All policy instances deleted", 200)
59 @app.route('/deletetypes', methods=['DELETE'])
63 return("All policy types deleted", 200)
65 @app.route('/<string:policyId>/<string:enforceStatus>', methods=['PUT'])
66 def set_status(policyId, enforceStatus):
67 if policyId in policy_instances.keys():
68 if policy_type_per_instance[policyId] == "UNDEFINED":
70 ps["policyId"] = policyId
71 ps["enforceStatus"] = enforceStatus
73 policy_type_id = policy_type_per_instance[policyId]
74 status_schema = policy_types[policy_type_id]["statusSchema"]
76 ps["policyId"] = policyId
77 ps["enforceStatus"] = enforceStatus
79 validate(instance=ps, schema=status_schema)
81 return(set_error(None, "The json does not validate against the status schema.", 400, None, None, None, None, None))
82 policy_status.pop(policyId)
83 policy_status[policyId] = ps
84 return("Status updated for policy: " + policyId, 200)
86 @app.route('/<string:policyId>/<string:enforceStatus>/<string:enforceReason>', methods=['PUT'])
87 def set_status_with_reason(policyId, enforceStatus, enforceReason):
88 if policyId in policy_instances.keys():
89 if policy_type_per_instance[policyId] == "UNDEFINED":
91 ps["policyId"] = policyId
92 ps["enforceStatus"] = enforceStatus
93 ps["enforceReason"] = enforceReason
95 policy_type_id = policy_type_per_instance[policyId]
96 status_schema = policy_types[policy_type_id]["statusSchema"]
98 ps["policyId"] = policyId
99 ps["enforceStatus"] = enforceStatus
100 ps["enforceReason"] = enforceReason
102 validate(instance=ps, schema=status_schema)
104 return(set_error(None, "The json does not validate against the status schema.", 400, None, None, None, None, None))
105 policy_status.pop(policyId)
106 policy_status[policyId] = ps
107 return("Status updated for policy: " + policyId, 200)
111 @app.route('/counter/<string:countername>', methods=['GET'])
112 def getCounter(countername):
113 if (countername == "num_instances"):
114 return str(len(policy_instances)),200
115 elif (countername == "num_types"):
116 return str(len(policy_types)),200
118 return "Counter name: "+countername+" not found.",404
122 if len(sys.argv) >= 2:
123 if isinstance(sys.argv[1], int):
124 port_number = sys.argv[1]
126 app.add_api('../a1-openapi.yaml')
127 app.run(port=port_number)