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 # ==================================================================================
20 from flask import Response
21 from jsonschema import validate
22 from jsonschema.exceptions import ValidationError
24 from mdclogpy import Logger
25 from a1 import a1rmr, exceptions, data
28 mdc_logger = Logger(name=__name__)
31 def _try_func_return(func):
33 generic caller that returns the apporp http response if exceptions are raised
37 except (ValidationError, exceptions.PolicyTypeAlreadyExists, exceptions.CantDeleteNonEmptyType):
39 except (exceptions.PolicyTypeNotFound, exceptions.PolicyInstanceNotFound):
41 except BaseException as exc:
42 # catch all, should never happen...
43 mdc_logger.exception(exc)
44 return Response(status=500)
50 def get_healthcheck():
52 Handles healthcheck GET
53 Currently, this checks:
54 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)
55 2. checks whether the rmr thread is running and has completed a loop recently
56 TODO: make "seconds" to pass in a configurable parameter?
58 if a1rmr.healthcheck_rmr_thread():
60 return "rmr thread is unhealthy", 500
66 def get_all_policy_types():
68 Handles GET /a1-p/policytypes
70 return _try_func_return(data.get_type_list)
73 def create_policy_type(policy_type_id):
75 Handles PUT /a1-p/policytypes/policy_type_id
78 def put_type_handler():
79 data.store_policy_type(policy_type_id, body)
82 body = connexion.request.json
83 return _try_func_return(put_type_handler)
86 def get_policy_type(policy_type_id):
88 Handles GET /a1-p/policytypes/policy_type_id
90 return _try_func_return(lambda: data.get_policy_type(policy_type_id))
93 def delete_policy_type(policy_type_id):
95 Handles DELETE /a1-p/policytypes/policy_type_id
98 def delete_policy_type_handler():
99 data.delete_policy_type(policy_type_id)
102 return _try_func_return(delete_policy_type_handler)
108 def get_all_instances_for_type(policy_type_id):
110 Handles GET /a1-p/policytypes/policy_type_id/policies
112 return _try_func_return(lambda: data.get_instance_list(policy_type_id))
115 def get_policy_instance(policy_type_id, policy_instance_id):
117 Handles GET /a1-p/policytypes/polidyid/policies/policy_instance_id
119 return _try_func_return(lambda: data.get_policy_instance(policy_type_id, policy_instance_id))
122 def get_policy_instance_status(policy_type_id, policy_instance_id):
124 Handles GET /a1-p/policytypes/polidyid/policies/policy_instance_id/status
126 Return the aggregated status. The order of rules is as follows:
127 1. If a1 has received at least one status, and *all* received statuses are "DELETED", we blow away the instance and return a 404
128 2. if a1 has received at least one status and at least one is OK, we return "IN EFFECT"
129 3. "NOT IN EFFECT" otherwise (no statuses, or none are OK but not all are deleted)
132 return _try_func_return(lambda: data.get_policy_instance_status(policy_type_id, policy_instance_id))
135 def create_or_replace_policy_instance(policy_type_id, policy_instance_id):
137 Handles PUT /a1-p/policytypes/polidyid/policies/policy_instance_id
139 instance = connexion.request.json
141 def put_instance_handler():
143 Handles policy instance put
145 For now, policy_type_id is used as the message type
147 # validate the PUT against the schema
148 schema = data.get_policy_type(policy_type_id)["create_schema"]
149 validate(instance=instance, schema=schema)
152 data.store_policy_instance(policy_type_id, policy_instance_id, instance)
154 # queue rmr send (best effort)
155 a1rmr.queue_instance_send(("CREATE", policy_type_id, policy_instance_id, instance))
159 return _try_func_return(put_instance_handler)
162 def delete_policy_instance(policy_type_id, policy_instance_id):
164 Handles DELETE /a1-p/policytypes/polidyid/policies/policy_instance_id
167 def delete_instance_handler():
169 here we send out the DELETEs but we don't delete the instance until a GET is called where we check the statuses
170 We also set the status as deleted which would be reflected in a GET to ../status (before the DELETE completes)
172 data.delete_policy_instance(policy_type_id, policy_instance_id)
174 # queue rmr send (best effort)
175 a1rmr.queue_instance_send(("DELETE", policy_type_id, policy_instance_id, ""))
179 return _try_func_return(delete_instance_handler)