1 from abc import ABCMeta, abstractmethod
4 class VppObject(object):
5 """ Abstract vpp object """
6 __metaclass__ = ABCMeta
9 VppObjectRegistry().register(self)
12 def add_vpp_config(self):
13 """ Add the configuration for this object to vpp. """
17 def query_vpp_config(self):
18 """Query the vpp configuration.
20 :return: True if the object is configured"""
24 def remove_vpp_config(self):
25 """ Remove the configuration for this object from vpp. """
30 """ Return a unique string representing this object. """
34 class VppObjectRegistry(object):
35 """ Class which handles automatic configuration cleanup. """
39 self.__dict__ = self._shared_state
40 if not hasattr(self, "_object_registry"):
41 self._object_registry = []
42 if not hasattr(self, "_object_dict"):
43 self._object_dict = dict()
45 def register(self, o, logger):
46 """ Register an object in the registry. """
47 if not o.object_id() in self._object_dict:
48 self._object_registry.append(o)
49 self._object_dict[o.object_id()] = o
51 logger.debug("REG: duplicate add, ignoring (%s)" % o)
53 def remove_vpp_config(self, logger):
55 Remove configuration (if present) from vpp and then remove all objects
58 if not self._object_registry:
59 logger.info("REG: No objects registered for auto-cleanup.")
61 logger.info("REG: Removing VPP configuration for registered objects")
62 # remove the config in reverse order as there might be dependencies
63 for o in reversed(self._object_registry):
64 if o.query_vpp_config():
65 logger.info("REG: Removing configuration for %s" % o)
69 "REG: Skipping removal for %s, configuration not present" %
72 for o in self._object_registry:
73 if o.query_vpp_config():
75 self._object_registry = []
76 self._object_dict = dict()
78 logger.error("REG: Couldn't remove configuration for object(s):")
81 raise Exception("Couldn't remove configuration for object(s): %s" %
82 (", ".join(str(x) for x in failed)))