2017-02-14 02:55:31 +01:00
|
|
|
""" abstract vpp object and object registry """
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
import abc
|
2016-09-29 14:43:44 +02:00
|
|
|
|
2018-11-25 12:47:04 -08:00
|
|
|
|
2020-12-03 00:42:46 -05:00
|
|
|
class VppObject(metaclass=abc.ABCMeta):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Abstract vpp object"""
|
2016-09-29 14:43:44 +02:00
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2020-12-01 11:33:28 -05:00
|
|
|
def add_vpp_config(self) -> None:
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Add the configuration for this object to vpp."""
|
2016-09-29 14:43:44 +02:00
|
|
|
pass
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2020-12-01 11:33:28 -05:00
|
|
|
def query_vpp_config(self) -> bool:
|
2016-09-29 14:43:44 +02:00
|
|
|
"""Query the vpp configuration.
|
|
|
|
|
|
|
|
:return: True if the object is configured"""
|
|
|
|
pass
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2020-12-01 11:33:28 -05:00
|
|
|
def remove_vpp_config(self) -> None:
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Remove the configuration for this object from vpp."""
|
2016-09-29 14:43:44 +02:00
|
|
|
pass
|
|
|
|
|
2020-12-01 11:33:28 -05:00
|
|
|
def object_id(self) -> str:
|
2022-04-26 19:02:15 +02:00
|
|
|
"""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))
|
|
|
|
|
2020-12-01 11:33:28 -05:00
|
|
|
def __str__(self) -> str:
|
2019-03-28 09:53:44 -07:00
|
|
|
return self.object_id()
|
|
|
|
|
2020-12-01 11:33:28 -05:00
|
|
|
def __repr__(self) -> str:
|
2022-04-26 19:02:15 +02:00
|
|
|
return "<%s>" % self.object_id()
|
2019-03-28 09:53:44 -07:00
|
|
|
|
2020-12-01 11:33:28 -05:00
|
|
|
def __hash__(self) -> int:
|
2019-03-28 09:53:44 -07:00
|
|
|
return hash(self.object_id())
|
|
|
|
|
2020-12-01 11:33:28 -05:00
|
|
|
def __eq__(self, other) -> bool:
|
2019-03-28 09:53:44 -07:00
|
|
|
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
|
|
|
|
|
|
|
|
2020-12-04 14:57:51 -05:00
|
|
|
class VppObjectRegistry:
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Class which handles automatic configuration cleanup."""
|
|
|
|
|
2016-09-29 14:43:44 +02:00
|
|
|
_shared_state = {}
|
|
|
|
|
2020-12-01 11:33:28 -05:00
|
|
|
def __init__(self) -> None:
|
2016-09-29 14:43:44 +02:00
|
|
|
self.__dict__ = self._shared_state
|
|
|
|
if not hasattr(self, "_object_registry"):
|
|
|
|
self._object_registry = []
|
|
|
|
if not hasattr(self, "_object_dict"):
|
|
|
|
self._object_dict = dict()
|
|
|
|
|
2020-12-01 11:33:28 -05:00
|
|
|
def register(self, obj: VppObject, logger) -> None:
|
2022-04-26 19:02:15 +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
|
|
|
|
2020-12-01 11:33:28 -05:00
|
|
|
def unregister_all(self, logger) -> None:
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Remove all object registrations from registry."""
|
2017-03-02 11:29:19 +01:00
|
|
|
logger.debug("REG: removing all object registrations")
|
|
|
|
self._object_registry = []
|
|
|
|
self._object_dict = dict()
|
|
|
|
|
2020-12-01 11:33:28 -05:00
|
|
|
def remove_vpp_config(self, logger) -> None:
|
2016-09-29 14:43:44 +02:00
|
|
|
"""
|
|
|
|
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(
|
2022-04-26 19:02:15 +02:00
|
|
|
"REG: Skipping removal for %s, configuration not present" % 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))
|
2022-04-26 19:02:15 +02:00
|
|
|
raise Exception(
|
|
|
|
"Couldn't remove configuration for object(s): %s"
|
|
|
|
% (", ".join(str(x) for x in failed))
|
|
|
|
)
|