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=================================================
22 from datetime import datetime
23 from connexion import NoContent
24 from flask import Flask, request, Response
25 from jsonschema import validate
26 from var_declaration import policy_instances, policy_types, policy_status, policy_fingerprint, forced_settings
27 from utils import calcFingerprint
28 from maincommon import *
31 # API Function: Health check
32 def get_healthcheck():
34 if ((r := check_modified_response()) is not None):
39 # API Function: Get all policy type ids
40 def get_all_policy_types():
42 if ((r := check_modified_response()) is not None):
45 return (list(policy_instances.keys()), 200)
47 # API Function: Get a policy type
48 def get_policy_type(policy_type_id):
50 if ((r := check_modified_response()) is not None):
53 policy_type_id=str(policy_type_id)
55 if (policy_type_id not in policy_types.keys()):
58 return Response(json.dumps(policy_types[policy_type_id]), 200, mimetype='application/json')
60 # API Function: Delete a policy type
61 def delete_policy_type(policy_type_id):
63 if ((r := check_modified_response()) is not None):
66 policy_type_id=str(policy_type_id)
68 if (policy_type_id not in policy_instances.keys()):
71 if (len(policy_instances[policy_type_id]) > 0):
74 del policy_instances[policy_type_id]
75 del policy_types[policy_type_id]
80 # API Function: Create a policy type
81 def create_policy_type(policy_type_id):
83 if ((r := check_modified_response()) is not None):
86 policy_type_id=str(policy_type_id)
88 if (policy_type_id in policy_instances.keys()):
89 if (len(policy_instances[policy_type_id]) > 0):
94 data = json.loads(data)
98 if (('name' not in data.keys()) or ('description' not in data.keys()) or ('policy_type_id' not in data.keys()) or ('create_schema' not in data.keys())):
101 if (policy_type_id not in policy_instances.keys()):
102 policy_instances[policy_type_id]={}
104 policy_types[policy_type_id]=data
109 # API Function: Get all policy ids for a type
110 def get_all_policy_identities(policy_type_id):
112 if ((r := check_modified_response()) is not None):
115 policy_type_id=str(policy_type_id)
117 if (policy_type_id not in policy_instances.keys()):
119 return (list(policy_instances[policy_type_id].keys()), 200)
121 # API Function: Get a policy instance
122 def get_policy_instance(policy_type_id, policy_instance_id):
124 if ((r := check_modified_response()) is not None):
127 policy_type_id=str(policy_type_id)
129 if (policy_type_id not in policy_instances.keys()):
132 if (policy_instance_id not in policy_instances[policy_type_id].keys()):
135 return Response(json.dumps(policy_instances[policy_type_id][policy_instance_id]), 200, mimetype='application/json')
137 # API function: Delete a policy
138 def delete_policy_instance(policy_type_id, policy_instance_id):
140 if ((r := check_modified_response()) is not None):
143 policy_type_id=str(policy_type_id)
145 if (policy_type_id not in policy_instances.keys()):
148 if (policy_instance_id not in policy_instances[policy_type_id].keys()):
151 fpPrevious=calcFingerprint(policy_instances[policy_type_id][policy_instance_id])
152 del policy_fingerprint[fpPrevious]
153 del policy_instances[policy_type_id][policy_instance_id]
154 del policy_status[policy_instance_id]
158 # API function: Create/update a policy
159 def create_or_replace_policy_instance(policy_type_id, policy_instance_id):
161 if ((r := check_modified_response()) is not None):
164 policy_type_id=str(policy_type_id)
166 if (policy_type_id not in policy_instances.keys()):
171 data = json.loads(data)
176 validate(instance=data, schema=policy_types[policy_type_id]['create_schema'])
182 if policy_instance_id in policy_instances[policy_type_id].keys():
184 fpPrevious=calcFingerprint(policy_instances[policy_type_id][policy_instance_id])
186 if (policy_instance_id in policy_fingerprint.values()):
189 fp=calcFingerprint(data)
190 if (fp in policy_fingerprint.keys()):
191 id=policy_fingerprint[fp]
192 if (id != policy_instance_id):
195 if (fpPrevious is not None):
196 del policy_fingerprint[fpPrevious]
198 policy_fingerprint[fp]=policy_instance_id
200 policy_instances[policy_type_id][policy_instance_id]=data
202 ps["instance_status"] = "NOT IN EFFECT"
203 ps["has_been_deleted"] = "false"
204 ps["created_at"] = str(datetime.now().strftime("%m/%d/%Y, %H:%M:%S"))
205 policy_status[policy_instance_id]=ps
209 # API function: Get policy status
210 def get_policy_instance_status(policy_type_id, policy_instance_id):
212 if ((r := check_modified_response()) is not None):
215 policy_type_id=str(policy_type_id)
216 if (policy_type_id not in policy_instances.keys()):
219 if (policy_instance_id not in policy_instances[policy_type_id].keys()):
222 return Response(json.dumps(policy_status[policy_instance_id]), 200, mimetype='application/json')
224 # Helper: Create a response object if forced http response code is set
225 def get_forced_response():
227 response_code=forced_settings['code']
228 if (response_code is not None):
229 forced_settings['code'] = None
230 return (None, response_code)
233 # Helper: Delay if delayed response code is set
236 if (forced_settings['delay'] is not None):
238 val=int(forced_settings['delay'])
244 # Helper: Check if response shall be delayed or a forced response shall be sent
245 def check_modified_response():
247 return get_forced_response()