4 # ==================================================================================
5 # Copyright (c) 2019 Nokia
6 # Copyright (c) 2018-2019 AT&T Intellectual Property.
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ==================================================================================
21 from flask import Response
22 from jsonschema import validate
23 from jsonschema.exceptions import ValidationError
25 from a1 import get_module_logger
26 from a1 import a1rmr, exceptions, data
29 logger = get_module_logger(__name__)
32 def _try_func_return(func):
34 generic caller that returns the apporp http response if exceptions are raised
38 except (ValidationError, exceptions.PolicyTypeAlreadyExists, exceptions.CantDeleteNonEmptyType):
40 except (exceptions.PolicyTypeNotFound, exceptions.PolicyInstanceNotFound):
42 except BaseException as exc:
43 # catch all, should never happen...
45 return Response(status=500)
48 def _gen_body_to_handler(operation, policy_type_id, policy_instance_id, payload=None):
50 used to create the payloads that get sent to downstream policy handlers
53 "operation": operation,
54 "policy_type_id": policy_type_id,
55 "policy_instance_id": policy_instance_id,
63 def get_healthcheck():
65 Handles healthcheck GET
66 Currently, this checks:
67 1. whether the a1 webserver is up (if it isn't, this won't even be called, so even entering this function confirms it is)
68 2. checks whether the rmr thread is running and has completed a loop recently
69 TODO: make "seconds" to pass in a configurable parameter?
71 if a1rmr.healthcheck_rmr_thread():
73 return "rmr thread is unhealthy", 500
79 def get_all_policy_types():
81 Handles GET /a1-p/policytypes
83 return _try_func_return(data.get_type_list)
86 def create_policy_type(policy_type_id):
88 Handles PUT /a1-p/policytypes/policy_type_id
91 def put_type_handler():
92 data.store_policy_type(policy_type_id, body)
95 body = connexion.request.json
96 return _try_func_return(put_type_handler)
99 def get_policy_type(policy_type_id):
101 Handles GET /a1-p/policytypes/policy_type_id
103 return _try_func_return(lambda: data.get_policy_type(policy_type_id))
106 def delete_policy_type(policy_type_id):
108 Handles DELETE /a1-p/policytypes/policy_type_id
111 def delete_policy_type_handler():
112 data.delete_policy_type(policy_type_id)
115 return _try_func_return(delete_policy_type_handler)
121 def get_all_instances_for_type(policy_type_id):
123 Handles GET /a1-p/policytypes/policy_type_id/policies
125 return _try_func_return(lambda: data.get_instance_list(policy_type_id))
128 def get_policy_instance(policy_type_id, policy_instance_id):
130 Handles GET /a1-p/policytypes/polidyid/policies/policy_instance_id
132 return _try_func_return(lambda: data.get_policy_instance(policy_type_id, policy_instance_id))
135 def get_policy_instance_status(policy_type_id, policy_instance_id):
137 Handles GET /a1-p/policytypes/polidyid/policies/policy_instance_id/status
139 Return the aggregated status. The order of rules is as follows:
140 1. If a1 has received at least one status, and *all* received statuses are "DELETED", we blow away the instance and return a 404
141 2. if a1 has received at least one status and at least one is OK, we return "IN EFFECT"
142 3. "NOT IN EFFECT" otherwise (no statuses, or none are OK but not all are deleted)
145 def get_status_handler():
146 vector = data.get_policy_instance_statuses(policy_type_id, policy_instance_id)
149 return "IN EFFECT", 200
150 return "NOT IN EFFECT", 200
152 return _try_func_return(get_status_handler)
155 def create_or_replace_policy_instance(policy_type_id, policy_instance_id):
157 Handles PUT /a1-p/policytypes/polidyid/policies/policy_instance_id
159 instance = connexion.request.json
161 def put_instance_handler():
163 Handles policy instance put
165 For now, policy_type_id is used as the message type
167 # validate the PUT against the schema
168 schema = data.get_policy_type(policy_type_id)["create_schema"]
169 validate(instance=instance, schema=schema)
172 data.store_policy_instance(policy_type_id, policy_instance_id, instance)
174 # send rmr (best effort)
175 body = _gen_body_to_handler("CREATE", policy_type_id, policy_instance_id, payload=instance)
176 a1rmr.queue_work({"payload": json.dumps(body), "msg type": policy_type_id})
180 return _try_func_return(put_instance_handler)
183 def delete_policy_instance(policy_type_id, policy_instance_id):
185 Handles DELETE /a1-p/policytypes/polidyid/policies/policy_instance_id
188 def delete_instance_handler():
190 here we send out the DELETEs but we don't delete the instance until a GET is called where we check the statuses
192 data.instance_is_valid(policy_type_id, policy_instance_id)
194 # send rmr (best effort)
195 body = _gen_body_to_handler("DELETE", policy_type_id, policy_instance_id)
196 a1rmr.queue_work({"payload": json.dumps(body), "msg type": policy_type_id})
200 return _try_func_return(delete_instance_handler)