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, hosts_set
27 from utils import calcFingerprint
28 from maincommon import extract_host_name
31 # API Function: Health check
32 def get_healthcheck():
34 extract_host_name(hosts_set, request)
36 if ((r := check_modified_response()) is not None):
41 # API Function: Get all policy type ids
42 def get_all_policy_types():
44 extract_host_name(hosts_set, request)
46 if ((r := check_modified_response()) is not None):
49 res = list(policy_instances.keys())
50 res = list(map(int, res))
53 # API Function: Get a policy type
54 def get_policy_type(policy_type_id):
56 extract_host_name(hosts_set, request)
58 if ((r := check_modified_response()) is not None):
61 policy_type_id=str(policy_type_id)
63 if (policy_type_id not in policy_types.keys()):
66 return Response(json.dumps(policy_types[policy_type_id]), 200, mimetype='application/json')
68 # API Function: Delete a policy type
69 def delete_policy_type(policy_type_id):
71 extract_host_name(hosts_set, request)
73 if ((r := check_modified_response()) is not None):
76 policy_type_id=str(policy_type_id)
78 if (policy_type_id not in policy_instances.keys()):
81 if (len(policy_instances[policy_type_id]) > 0):
84 del policy_instances[policy_type_id]
85 del policy_types[policy_type_id]
90 # API Function: Create a policy type
91 def create_policy_type(policy_type_id):
93 extract_host_name(hosts_set, request)
95 if ((r := check_modified_response()) is not None):
99 val=int(policy_type_id)
101 return Response("The policy type id is not an int", 400, mimetype='text/plain')
103 policy_type_id=str(policy_type_id)
105 if (policy_type_id in policy_instances.keys()):
106 if (len(policy_instances[policy_type_id]) > 0):
111 data = json.loads(data)
115 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())):
118 if (policy_type_id not in policy_instances.keys()):
119 policy_instances[policy_type_id]={}
121 policy_types[policy_type_id]=data
126 # API Function: Get all policy ids for a type
127 def get_all_policy_identities(policy_type_id):
129 extract_host_name(hosts_set, request)
131 if ((r := check_modified_response()) is not None):
134 policy_type_id=str(policy_type_id)
136 if (policy_type_id not in policy_instances.keys()):
138 return (list(policy_instances[policy_type_id].keys()), 200)
140 # API Function: Get a policy instance
141 def get_policy_instance(policy_type_id, policy_instance_id):
143 extract_host_name(hosts_set, request)
145 if ((r := check_modified_response()) is not None):
148 policy_type_id=str(policy_type_id)
150 if (policy_type_id not in policy_instances.keys()):
153 if (policy_instance_id not in policy_instances[policy_type_id].keys()):
156 return Response(json.dumps(policy_instances[policy_type_id][policy_instance_id]), 200, mimetype='application/json')
158 # API function: Delete a policy
159 def delete_policy_instance(policy_type_id, policy_instance_id):
161 extract_host_name(hosts_set, request)
163 if ((r := check_modified_response()) is not None):
166 policy_type_id=str(policy_type_id)
168 if (policy_type_id not in policy_instances.keys()):
171 if (policy_instance_id not in policy_instances[policy_type_id].keys()):
174 fpPrevious=calcFingerprint(policy_instances[policy_type_id][policy_instance_id])
175 del policy_fingerprint[fpPrevious]
176 del policy_instances[policy_type_id][policy_instance_id]
177 del policy_status[policy_instance_id]
181 # API function: Create/update a policy
182 def create_or_replace_policy_instance(policy_type_id, policy_instance_id):
184 extract_host_name(hosts_set, request)
186 if ((r := check_modified_response()) is not None):
189 policy_type_id=str(policy_type_id)
191 if (policy_type_id not in policy_instances.keys()):
196 data = json.loads(data)
201 validate(instance=data, schema=policy_types[policy_type_id]['create_schema'])
207 if policy_instance_id in policy_instances[policy_type_id].keys():
209 fpPrevious=calcFingerprint(policy_instances[policy_type_id][policy_instance_id])
211 if (policy_instance_id in policy_fingerprint.values()):
214 fp=calcFingerprint(data)
215 if (fp in policy_fingerprint.keys()):
216 id=policy_fingerprint[fp]
217 if (id != policy_instance_id):
220 if (fpPrevious is not None):
221 del policy_fingerprint[fpPrevious]
223 policy_fingerprint[fp]=policy_instance_id
225 policy_instances[policy_type_id][policy_instance_id]=data
227 ps["instance_status"] = "NOT IN EFFECT"
228 ps["has_been_deleted"] = "false"
229 ps["created_at"] = str(datetime.now().strftime("%m/%d/%Y, %H:%M:%S"))
230 policy_status[policy_instance_id]=ps
234 # API function: Get policy status
235 def get_policy_instance_status(policy_type_id, policy_instance_id):
237 extract_host_name(hosts_set, request)
239 if ((r := check_modified_response()) is not None):
242 policy_type_id=str(policy_type_id)
243 if (policy_type_id not in policy_instances.keys()):
246 if (policy_instance_id not in policy_instances[policy_type_id].keys()):
249 return Response(json.dumps(policy_status[policy_instance_id]), 200, mimetype='application/json')
251 # Helper: Create a response object if forced http response code is set
252 def get_forced_response():
254 response_code=forced_settings['code']
255 if (response_code is not None):
256 forced_settings['code'] = None
257 return (None, response_code)
260 # Helper: Delay if delayed response code is set
263 if (forced_settings['delay'] is not None):
265 val=int(forced_settings['delay'])
271 # Helper: Check if response shall be delayed or a forced response shall be sent
272 def check_modified_response():
274 return get_forced_response()