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) as exc:
41 except (exceptions.PolicyTypeNotFound, exceptions.PolicyInstanceNotFound) as exc:
44 except BaseException as exc:
45 # catch all, should never happen...
47 return Response(status=500)
50 def _gen_body_to_handler(operation, policy_type_id, policy_instance_id, payload=None):
52 used to create the payloads that get sent to downstream policy handlers
55 "operation": operation,
56 "policy_type_id": policy_type_id,
57 "policy_instance_id": policy_instance_id,
65 def get_healthcheck():
67 Handles healthcheck GET
68 Currently, this basically checks the server is alive
76 def get_all_policy_types():
78 Handles GET /a1-p/policytypes
80 return _try_func_return(data.get_type_list)
83 def create_policy_type(policy_type_id):
85 Handles PUT /a1-p/policytypes/policy_type_id
88 def put_type_handler():
89 data.store_policy_type(policy_type_id, body)
92 body = connexion.request.json
93 return _try_func_return(put_type_handler)
96 def get_policy_type(policy_type_id):
98 Handles GET /a1-p/policytypes/policy_type_id
100 return _try_func_return(lambda: data.get_policy_type(policy_type_id))
103 def delete_policy_type(policy_type_id):
105 Handles DELETE /a1-p/policytypes/policy_type_id
108 def delete_policy_type_handler():
109 data.delete_policy_type(policy_type_id)
112 return _try_func_return(delete_policy_type_handler)
118 def get_all_instances_for_type(policy_type_id):
120 Handles GET /a1-p/policytypes/policy_type_id/policies
122 return _try_func_return(lambda: data.get_instance_list(policy_type_id))
125 def get_policy_instance(policy_type_id, policy_instance_id):
127 Handles GET /a1-p/policytypes/polidyid/policies/policy_instance_id
129 return _try_func_return(lambda: data.get_policy_instance(policy_type_id, policy_instance_id))
132 def get_policy_instance_status(policy_type_id, policy_instance_id):
134 Handles GET /a1-p/policytypes/polidyid/policies/policy_instance_id/status
136 Return the aggregated status. The order of rules is as follows:
137 1. If a1 has received at least one status, and *all* received statuses are "DELETED", we blow away the instance and return a 404
138 2. if a1 has received at least one status and at least one is OK, we return "IN EFFECT"
139 3. "NOT IN EFFECT" otherwise (no statuses, or none are OK but not all are deleted)
142 def get_status_handler():
143 vector = data.get_policy_instance_statuses(policy_type_id, policy_instance_id)
146 return "IN EFFECT", 200
147 return "NOT IN EFFECT", 200
149 return _try_func_return(get_status_handler)
152 def create_or_replace_policy_instance(policy_type_id, policy_instance_id):
154 Handles PUT /a1-p/policytypes/polidyid/policies/policy_instance_id
156 instance = connexion.request.json
158 def put_instance_handler():
160 Handles policy instance put
162 For now, policy_type_id is used as the message type
164 # validate the PUT against the schema
165 schema = data.get_policy_type(policy_type_id)["create_schema"]
166 validate(instance=instance, schema=schema)
169 data.store_policy_instance(policy_type_id, policy_instance_id, instance)
171 # send rmr (best effort)
172 body = _gen_body_to_handler("CREATE", policy_type_id, policy_instance_id, payload=instance)
173 a1rmr.send(json.dumps(body), message_type=policy_type_id)
177 return _try_func_return(put_instance_handler)
180 def delete_policy_instance(policy_type_id, policy_instance_id):
182 Handles DELETE /a1-p/policytypes/polidyid/policies/policy_instance_id
185 def delete_instance_handler():
187 here we send out the DELETEs but we don't delete the instance until a GET is called where we check the statuses
189 data.instance_is_valid(policy_type_id, policy_instance_id)
191 # send rmr (best effort)
192 body = _gen_body_to_handler("DELETE", policy_type_id, policy_instance_id)
193 a1rmr.send(json.dumps(body), message_type=policy_type_id)
197 return _try_func_return(delete_instance_handler)