- SDL.delete(k)
-
-
-def _clean_up_type(policy_type_id):
- """
- pop through a1s mailbox, updating a1s db of all policy statuses
- for all instances of type, see if it can be deleted
- """
- type_is_valid(policy_type_id)
- for msg in a1rmr.dequeue_all_waiting_messages([21024]):
- # try to parse the messages as responses. Drop those that are malformed
- pay = json.loads(msg["payload"])
- if "policy_type_id" in pay and "policy_instance_id" in pay and "handler_id" in pay and "status" in pay:
- # We don't use the parameters "policy_type_id, policy_instance" from above here,
- # because we are popping the whole mailbox, which might include other statuses
- pti = pay["policy_type_id"]
- pii = pay["policy_instance_id"]
-
- try:
- """
- can't raise an exception here e.g.:
- because this is called on many functions; just drop bad status messages.
- We def don't want bad messages that happen to hit a1s mailbox to blow up anything
-
- """
- type_is_valid(pti)
- instance_is_valid(pti, pii)
- SDL.set(_generate_handler_key(pti, pii, pay["handler_id"]), pay["status"])
- except (PolicyTypeNotFound, PolicyInstanceNotFound):
- pass
-
- else:
- logger.debug("Dropping message")
- logger.debug(pay)
-
- for policy_instance_id in _get_instance_list(policy_type_id):
- # see if we can delete
- vector = _get_statuses(policy_type_id, policy_instance_id)
-
- """
- TODO: not being able to delete if the list is [] is prolematic.
- There are cases, such as a bad routing file, where this type will never be able to be deleted because it never went to any xapps
- However, A1 cannot distinguish between the case where [] was never going to work, and the case where it hasn't worked *yet*
-
- However, removing this constraint also leads to problems.
- Deleting the instance when the vector is empty, for example doing so “shortly after” the PUT, can lead to a worse race condition where the xapps get the policy after that, implement it, but because the DELETE triggered “too soon”, you can never get the status or do the delete on it again, so the xapps are all implementing the instance roguely.
-
- This requires some thought to address.
- For now we stick with the "less bad problem".
- """
- if vector != []:
- all_deleted = True
- for i in vector:
- if i != "DELETED":
- all_deleted = False
- break # have at least one not DELETED, do nothing
-
- # blow away from a1 db
- if all_deleted:
- _clear_handlers(policy_type_id, policy_instance_id) # delete all the handlers
- SDL.delete(_generate_instance_key(policy_type_id, policy_instance_id)) # delete instance
+ SDL.delete(A1NS, k)
+
+
+def _get_metadata(policy_type_id, policy_instance_id):
+ """
+ get instance metadata
+ """
+ _instance_is_valid(policy_type_id, policy_instance_id)
+ metadata_key = _generate_instance_metadata_key(policy_type_id, policy_instance_id)
+ return SDL.get(A1NS, metadata_key)
+
+
+def _delete_after(policy_type_id, policy_instance_id, ttl):
+ """
+ this is a blocking function, must call this in a thread to not block!
+ waits ttl seconds, then deletes the instance
+ """
+ _instance_is_valid(policy_type_id, policy_instance_id)
+
+ time.sleep(ttl)
+
+ # ready to delete
+ _clear_handlers(policy_type_id, policy_instance_id) # delete all the handlers
+ SDL.delete(A1NS, _generate_instance_key(policy_type_id, policy_instance_id)) # delete instance
+ SDL.delete(A1NS, _generate_instance_metadata_key(policy_type_id, policy_instance_id)) # delete instance metadata
+ mdc_logger.debug("type {0} instance {1} deleted".format(policy_type_id, policy_instance_id))