2017-02-14 02:55:31 +01:00
|
|
|
""" abstract vpp object and object registry """
|
|
|
|
|
2016-09-29 14:43:44 +02:00
|
|
|
from abc import ABCMeta, abstractmethod
|
|
|
|
|
|
|
|
|
|
|
|
class VppObject(object):
|
|
|
|
""" Abstract vpp object """
|
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def add_vpp_config(self):
|
|
|
|
""" Add the configuration for this object to vpp. """
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def query_vpp_config(self):
|
|
|
|
"""Query the vpp configuration.
|
|
|
|
|
|
|
|
:return: True if the object is configured"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def remove_vpp_config(self):
|
|
|
|
""" Remove the configuration for this object from vpp. """
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def object_id(self):
|
|
|
|
""" Return a unique string representing this object. """
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class VppObjectRegistry(object):
|
|
|
|
""" Class which handles automatic configuration cleanup. """
|
|
|
|
_shared_state = {}
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.__dict__ = self._shared_state
|
|
|
|
if not hasattr(self, "_object_registry"):
|
|
|
|
self._object_registry = []
|
|
|
|
if not hasattr(self, "_object_dict"):
|
|
|
|
self._object_dict = dict()
|
|
|
|
|
2017-02-14 02:55:31 +01:00
|
|
|
def register(self, obj, logger):
|
2016-09-29 14:43:44 +02:00
|
|
|
""" Register an object in the registry. """
|
2017-02-14 02:55:31 +01:00
|
|
|
if obj.object_id() not in self._object_dict:
|
|
|
|
self._object_registry.append(obj)
|
|
|
|
self._object_dict[obj.object_id()] = obj
|
2017-02-23 09:26:30 +01:00
|
|
|
logger.debug("REG: registering %s" % obj)
|
2016-09-29 14:43:44 +02:00
|
|
|
else:
|
2017-02-14 02:55:31 +01:00
|
|
|
logger.debug("REG: duplicate add, ignoring (%s)" % obj)
|
2016-09-29 14:43:44 +02:00
|
|
|
|
2017-03-02 11:29:19 +01:00
|
|
|
def unregister_all(self, logger):
|
|
|
|
""" Remove all object registrations from registry. """
|
|
|
|
logger.debug("REG: removing all object registrations")
|
|
|
|
self._object_registry = []
|
|
|
|
self._object_dict = dict()
|
|
|
|
|
2016-09-29 14:43:44 +02:00
|
|
|
def remove_vpp_config(self, logger):
|
|
|
|
"""
|
|
|
|
Remove configuration (if present) from vpp and then remove all objects
|
|
|
|
from the registry.
|
|
|
|
"""
|
|
|
|
if not self._object_registry:
|
2017-01-11 08:16:53 +01:00
|
|
|
logger.info("REG: No objects registered for auto-cleanup.")
|
2016-09-29 14:43:44 +02:00
|
|
|
return
|
2017-01-11 08:16:53 +01:00
|
|
|
logger.info("REG: Removing VPP configuration for registered objects")
|
|
|
|
# remove the config in reverse order as there might be dependencies
|
2017-02-14 02:55:31 +01:00
|
|
|
for obj in reversed(self._object_registry):
|
|
|
|
if obj.query_vpp_config():
|
|
|
|
logger.info("REG: Removing configuration for %s" % obj)
|
|
|
|
obj.remove_vpp_config()
|
2016-09-29 14:43:44 +02:00
|
|
|
else:
|
2017-01-11 08:16:53 +01:00
|
|
|
logger.info(
|
|
|
|
"REG: Skipping removal for %s, configuration not present" %
|
2017-02-14 02:55:31 +01:00
|
|
|
obj)
|
2016-09-29 14:43:44 +02:00
|
|
|
failed = []
|
2017-02-14 02:55:31 +01:00
|
|
|
for obj in self._object_registry:
|
|
|
|
if obj.query_vpp_config():
|
|
|
|
failed.append(obj)
|
2017-03-02 11:29:19 +01:00
|
|
|
self.unregister_all(logger)
|
2016-09-29 14:43:44 +02:00
|
|
|
if failed:
|
2017-01-11 08:16:53 +01:00
|
|
|
logger.error("REG: Couldn't remove configuration for object(s):")
|
2017-02-14 02:55:31 +01:00
|
|
|
for obj in failed:
|
|
|
|
logger.error(repr(obj))
|
2016-09-29 14:43:44 +02:00
|
|
|
raise Exception("Couldn't remove configuration for object(s): %s" %
|
|
|
|
(", ".join(str(x) for x in failed)))
|