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 SDL.set(A1NS, key, body)
172 def delete_policy_type(policy_type_id):
174 delete a policy type; can only be done if there are no instances (business logic)
176 pil = get_instance_list(policy_type_id)
177 if pil == []: # empty, can delete
178 SDL.delete(A1NS, _generate_type_key(policy_type_id))
180 raise CantDeleteNonEmptyType()
183 def get_policy_type(policy_type_id):
187 _type_is_valid(policy_type_id)
188 return SDL.get(A1NS, _generate_type_key(policy_type_id))
194 def store_policy_instance(policy_type_id, policy_instance_id, instance):
196 Store a policy instance
198 _type_is_valid(policy_type_id)
199 creation_timestamp = time.time()
202 key = _generate_instance_key(policy_type_id, policy_instance_id)
203 if SDL.get(A1NS, key) is not None:
204 # Reset the statuses because this is a new policy instance, even if it was overwritten
205 _clear_handlers(policy_type_id, policy_instance_id) # delete all the handlers
206 SDL.set(A1NS, key, instance)
208 metadata_key = _generate_instance_metadata_key(policy_type_id, policy_instance_id)
209 SDL.set(A1NS, metadata_key, {"created_at": creation_timestamp, "has_been_deleted": False})
212 def get_policy_instance(policy_type_id, policy_instance_id):
214 Retrieve a policy instance
216 _instance_is_valid(policy_type_id, policy_instance_id)
217 return SDL.get(A1NS, _generate_instance_key(policy_type_id, policy_instance_id))
220 def get_instance_list(policy_type_id):
222 retrieve all instance ids for a type
224 return _get_instance_list(policy_type_id)
227 def delete_policy_instance(policy_type_id, policy_instance_id):
229 initially sets has_been_deleted in the status
230 then launches a thread that waits until the relevent timer expires, and finally deletes the instance
232 _instance_is_valid(policy_type_id, policy_instance_id)
234 # set the metadata first
235 deleted_timestamp = time.time()
236 metadata_key = _generate_instance_metadata_key(policy_type_id, policy_instance_id)
237 existing_metadata = _get_metadata(policy_type_id, policy_instance_id)
241 {"created_at": existing_metadata["created_at"], "has_been_deleted": True, "deleted_at": deleted_timestamp},
245 vector = _get_statuses(policy_type_id, policy_instance_id)
247 # handler is empty; we wait for t1 to expire then goodnight
248 clos = lambda: _delete_after(policy_type_id, policy_instance_id, INSTANCE_DELETE_NO_RESP_TTL)
250 # handler is not empty, we wait max t1,t2 to expire then goodnight
251 clos = lambda: _delete_after(
252 policy_type_id, policy_instance_id, max(INSTANCE_DELETE_RESP_TTL, INSTANCE_DELETE_NO_RESP_TTL)
254 Thread(target=clos).start()
260 def set_policy_instance_status(policy_type_id, policy_instance_id, handler_id, status):
262 update the database status for a handler
263 called from a1's rmr thread
265 _type_is_valid(policy_type_id)
266 _instance_is_valid(policy_type_id, policy_instance_id)
267 SDL.set(A1NS, _generate_handler_key(policy_type_id, policy_instance_id, handler_id), status)
270 def get_policy_instance_status(policy_type_id, policy_instance_id):
272 Gets the status of an instance
274 _instance_is_valid(policy_type_id, policy_instance_id)
275 metadata = _get_metadata(policy_type_id, policy_instance_id)
276 metadata["instance_status"] = "NOT IN EFFECT"
277 for i in _get_statuses(policy_type_id, policy_instance_id):
279 metadata["instance_status"] = "IN EFFECT"