1 """ abstract vpp object and object registry """
3 from abc import ABCMeta, abstractmethod
6 class VppObject(object):
7 """ Abstract vpp object """
8 __metaclass__ = ABCMeta
11 def add_vpp_config(self):
12 """ Add the configuration for this object to vpp. """
16 def query_vpp_config(self):
17 """Query the vpp configuration.
19 :return: True if the object is configured"""
23 def remove_vpp_config(self):
24 """ Remove the configuration for this object from vpp. """
29 """ Return a unique string representing this object. """
33 class VppObjectRegistry(object):
34 """ Class which handles automatic configuration cleanup. """
38 self.__dict__ = self._shared_state
39 if not hasattr(self, "_object_registry"):
40 self._object_registry = []
41 if not hasattr(self, "_object_dict"):
42 self._object_dict = dict()
44 def register(self, obj, logger):
45 """ Register an object in the registry. """
46 if obj.object_id() not in self._object_dict:
47 self._object_registry.append(obj)
48 self._object_dict[obj.object_id()] = obj
49 logger.debug("REG: registering %s" % obj)
51 logger.debug("REG: duplicate add, ignoring (%s)" % obj)
53 def unregister_all(self, logger):
54 """ Remove all object registrations from registry. """
55 logger.debug("REG: removing all object registrations")
56 self._object_registry = []
57 self._object_dict = dict()
59 def remove_vpp_config(self, logger):
61 Remove configuration (if present) from vpp and then remove all objects
64 if not self._object_registry:
65 logger.info("REG: No objects registered for auto-cleanup.")
67 logger.info("REG: Removing VPP configuration for registered objects")
68 # remove the config in reverse order as there might be dependencies
70 for obj in reversed(self._object_registry):
71 if obj.query_vpp_config():
72 logger.info("REG: Removing configuration for %s" % obj)
73 obj.remove_vpp_config()
74 if obj.query_vpp_config():
78 "REG: Skipping removal for %s, configuration not present" %
80 self.unregister_all(logger)
82 logger.error("REG: Couldn't remove configuration for object(s):")
84 logger.error(repr(obj))
85 raise Exception("Couldn't remove configuration for object(s): %s" %
86 (", ".join(str(x) for x in failed)))