2 Represents A1s database and database access functions.
4 # ==================================================================================
5 # Copyright (c) 2019-2020 Nokia
6 # Copyright (c) 2018-2020 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 # ==================================================================================
22 from threading import Thread
23 from mdclogpy import Logger
24 from ricxappframe.xapp_sdl import SDLWrapper
25 from a1.exceptions import PolicyTypeNotFound, PolicyInstanceNotFound, PolicyTypeAlreadyExists, CantDeleteNonEmptyType
29 INSTANCE_DELETE_NO_RESP_TTL = int(os.environ.get("INSTANCE_DELETE_NO_RESP_TTL", 5))
30 INSTANCE_DELETE_RESP_TTL = int(os.environ.get("INSTANCE_DELETE_RESP_TTL", 5))
32 TYPE_PREFIX = "a1.policy_type."
33 INSTANCE_PREFIX = "a1.policy_instance."
34 METADATA_PREFIX = "a1.policy_inst_metadata."
35 HANDLER_PREFIX = "a1.policy_handler."
38 mdc_logger = Logger(name=__name__)
44 def _generate_type_key(policy_type_id):
46 generate a key for a policy type
48 return "{0}{1}".format(TYPE_PREFIX, policy_type_id)
51 def _generate_instance_key(policy_type_id, policy_instance_id):
53 generate a key for a policy instance
55 return "{0}{1}.{2}".format(INSTANCE_PREFIX, policy_type_id, policy_instance_id)
58 def _generate_instance_metadata_key(policy_type_id, policy_instance_id):
60 generate a key for a policy instance metadata
62 return "{0}{1}.{2}".format(METADATA_PREFIX, policy_type_id, policy_instance_id)
65 def _generate_handler_prefix(policy_type_id, policy_instance_id):
67 generate the prefix to a handler key
69 return "{0}{1}.{2}.".format(HANDLER_PREFIX, policy_type_id, policy_instance_id)
72 def _generate_handler_key(policy_type_id, policy_instance_id, handler_id):
74 generate a key for a policy handler
76 return "{0}{1}".format(_generate_handler_prefix(policy_type_id, policy_instance_id), handler_id)
79 def _type_is_valid(policy_type_id):
81 check that a type is valid
83 if SDL.get(A1NS, _generate_type_key(policy_type_id)) is None:
84 raise PolicyTypeNotFound()
87 def _instance_is_valid(policy_type_id, policy_instance_id):
89 check that an instance is valid
91 _type_is_valid(policy_type_id)
92 if SDL.get(A1NS, _generate_instance_key(policy_type_id, policy_instance_id)) is None:
93 raise PolicyInstanceNotFound
96 def _get_statuses(policy_type_id, policy_instance_id):
98 shared helper to get statuses for an instance
100 _instance_is_valid(policy_type_id, policy_instance_id)
101 prefixes_for_handler = "{0}{1}.{2}.".format(HANDLER_PREFIX, policy_type_id, policy_instance_id)
102 return list(SDL.find_and_get(A1NS, prefixes_for_handler).values())
105 def _get_instance_list(policy_type_id):
107 shared helper to get instance list for a type
109 _type_is_valid(policy_type_id)
110 prefixes_for_type = "{0}{1}.".format(INSTANCE_PREFIX, policy_type_id)
111 instancekeys = SDL.find_and_get(A1NS, prefixes_for_type).keys()
112 return [k.split(prefixes_for_type)[1] for k in instancekeys]
115 def _clear_handlers(policy_type_id, policy_instance_id):
117 delete all the handlers for a policy instance
119 all_handlers_pref = _generate_handler_prefix(policy_type_id, policy_instance_id)
120 keys = SDL.find_and_get(A1NS, all_handlers_pref)
125 def _get_metadata(policy_type_id, policy_instance_id):
127 get instance metadata
129 _instance_is_valid(policy_type_id, policy_instance_id)
130 metadata_key = _generate_instance_metadata_key(policy_type_id, policy_instance_id)
131 return SDL.get(A1NS, metadata_key)
134 def _delete_after(policy_type_id, policy_instance_id, ttl):
136 this is a blocking function, must call this in a thread to not block!
137 waits ttl seconds, then deletes the instance
139 _instance_is_valid(policy_type_id, policy_instance_id)
144 _clear_handlers(policy_type_id, policy_instance_id) # delete all the handlers
145 SDL.delete(A1NS, _generate_instance_key(policy_type_id, policy_instance_id)) # delete instance
146 SDL.delete(A1NS, _generate_instance_metadata_key(policy_type_id, policy_instance_id)) # delete instance metadata
147 mdc_logger.debug("type {0} instance {1} deleted".format(policy_type_id, policy_instance_id))
155 retrieve all type ids
157 typekeys = SDL.find_and_get(A1NS, TYPE_PREFIX).keys()
158 # policy types are ints but they get butchered to strings in the KV
159 return [int(k.split(TYPE_PREFIX)[1]) for k in typekeys]
162 def store_policy_type(policy_type_id, body):
164 store a policy type if it doesn't already exist
166 key = _generate_type_key(policy_type_id)
167 if SDL.get(A1NS, key) is not None:
168 raise PolicyTypeAlreadyExists()
169 # overwrite ID in body to enforce consistency
170 body['policy_type_id'] = policy_type_id
171 SDL.set(A1NS, key, body)
174 def delete_policy_type(policy_type_id):
176 delete a policy type; can only be done if there are no instances (business logic)
178 pil = get_instance_list(policy_type_id)
179 if pil == []: # empty, can delete
180 SDL.delete(A1NS, _generate_type_key(policy_type_id))
182 raise CantDeleteNonEmptyType()
185 def get_policy_type(policy_type_id):
189 _type_is_valid(policy_type_id)
190 return SDL.get(A1NS, _generate_type_key(policy_type_id))
196 def store_policy_instance(policy_type_id, policy_instance_id, instance):
198 Store a policy instance
200 _type_is_valid(policy_type_id)
201 creation_timestamp = time.time()
204 key = _generate_instance_key(policy_type_id, policy_instance_id)
205 if SDL.get(A1NS, key) is not None:
206 # Reset the statuses because this is a new policy instance, even if it was overwritten
207 _clear_handlers(policy_type_id, policy_instance_id) # delete all the handlers
208 SDL.set(A1NS, key, instance)
210 metadata_key = _generate_instance_metadata_key(policy_type_id, policy_instance_id)
211 SDL.set(A1NS, metadata_key, {"created_at": creation_timestamp, "has_been_deleted": False})
214 def get_policy_instance(policy_type_id, policy_instance_id):
216 Retrieve a policy instance
218 _instance_is_valid(policy_type_id, policy_instance_id)
219 return SDL.get(A1NS, _generate_instance_key(policy_type_id, policy_instance_id))
222 def get_instance_list(policy_type_id):
224 retrieve all instance ids for a type
226 return _get_instance_list(policy_type_id)
229 def delete_policy_instance(policy_type_id, policy_instance_id):
231 initially sets has_been_deleted in the status
232 then launches a thread that waits until the relevent timer expires, and finally deletes the instance
234 _instance_is_valid(policy_type_id, policy_instance_id)
236 # set the metadata first
237 deleted_timestamp = time.time()
238 metadata_key = _generate_instance_metadata_key(policy_type_id, policy_instance_id)
239 existing_metadata = _get_metadata(policy_type_id, policy_instance_id)
243 {"created_at": existing_metadata["created_at"], "has_been_deleted": True, "deleted_at": deleted_timestamp},
247 vector = _get_statuses(policy_type_id, policy_instance_id)
249 # handler is empty; we wait for t1 to expire then goodnight
250 clos = lambda: _delete_after(policy_type_id, policy_instance_id, INSTANCE_DELETE_NO_RESP_TTL)
252 # handler is not empty, we wait max t1,t2 to expire then goodnight
253 clos = lambda: _delete_after(
254 policy_type_id, policy_instance_id, max(INSTANCE_DELETE_RESP_TTL, INSTANCE_DELETE_NO_RESP_TTL)
256 Thread(target=clos).start()
262 def set_policy_instance_status(policy_type_id, policy_instance_id, handler_id, status):
264 update the database status for a handler
265 called from a1's rmr thread
267 _type_is_valid(policy_type_id)
268 _instance_is_valid(policy_type_id, policy_instance_id)
269 SDL.set(A1NS, _generate_handler_key(policy_type_id, policy_instance_id, handler_id), status)
272 def get_policy_instance_status(policy_type_id, policy_instance_id):
274 Gets the status of an instance
276 _instance_is_valid(policy_type_id, policy_instance_id)
277 metadata = _get_metadata(policy_type_id, policy_instance_id)
278 metadata["instance_status"] = "NOT IN EFFECT"
279 for i in _get_statuses(policy_type_id, policy_instance_id):
281 metadata["instance_status"] = "IN EFFECT"