2017-02-14 02:55:31 +01:00
|
|
|
""" abstract vpp object and object registry """
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
import abc
|
|
|
|
import six
|
2016-09-29 14:43:44 +02:00
|
|
|
|
2018-11-26 09:57:21 -08:00
|
|
|
from six import moves
|
2018-11-25 12:47:04 -08:00
|
|
|
|
2016-09-29 14:43:44 +02:00
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@six.add_metaclass(abc.ABCMeta)
|
2016-09-29 14:43:44 +02:00
|
|
|
class VppObject(object):
|
|
|
|
""" Abstract vpp object """
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2016-09-29 14:43:44 +02:00
|
|
|
def add_vpp_config(self):
|
|
|
|
""" Add the configuration for this object to vpp. """
|
|
|
|
pass
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2016-09-29 14:43:44 +02:00
|
|
|
def query_vpp_config(self):
|
|
|
|
"""Query the vpp configuration.
|
|
|
|
|
|
|
|
:return: True if the object is configured"""
|
|
|
|
pass
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2016-09-29 14:43:44 +02:00
|
|
|
def remove_vpp_config(self):
|
|
|
|
""" Remove the configuration for this object from vpp. """
|
|
|
|
pass
|
|
|
|
|
|
|
|
def object_id(self):
|
|
|
|
""" Return a unique string representing this object. """
|
2019-03-28 09:53:44 -07:00
|
|
|
return "Undefined. for <%s %s>" % (self.__class__.__name__, id(self))
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.object_id()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<%s>' % self.object_id()
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.object_id())
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if not isinstance(other, self.__class__):
|
|
|
|
return NotImplemented
|
|
|
|
if other.object_id() == self.object_id():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
# This can be removed when python2 support is dropped.
|
|
|
|
def __ne__(self, other):
|
|
|
|
return not self.__eq__(other)
|
2016-09-29 14:43:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2018-01-31 11:35:41 -08:00
|
|
|
failed = []
|
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()
|
2018-01-31 11:35:41 -08:00
|
|
|
if obj.query_vpp_config():
|
|
|
|
failed.append(obj)
|
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)
|
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:
|
2019-03-28 09:53:44 -07:00
|
|
|
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)))
|