1 """ abstract vpp object and object registry """
9 @six.add_metaclass(abc.ABCMeta)
10 class VppObject(object):
11 """ Abstract vpp object """
14 def add_vpp_config(self):
15 """ Add the configuration for this object to vpp. """
19 def query_vpp_config(self):
20 """Query the vpp configuration.
22 :return: True if the object is configured"""
26 def remove_vpp_config(self):
27 """ Remove the configuration for this object from vpp. """
32 """ Return a unique string representing this object. """
36 class VppObjectRegistry(object):
37 """ Class which handles automatic configuration cleanup. """
41 self.__dict__ = self._shared_state
42 if not hasattr(self, "_object_registry"):
43 self._object_registry = []
44 if not hasattr(self, "_object_dict"):
45 self._object_dict = dict()
47 def register(self, obj, logger):
48 """ Register an object in the registry. """
49 if obj.object_id() not in self._object_dict:
50 self._object_registry.append(obj)
51 self._object_dict[obj.object_id()] = obj
52 logger.debug("REG: registering %s" % obj)
54 logger.debug("REG: duplicate add, ignoring (%s)" % obj)
56 def unregister_all(self, logger):
57 """ Remove all object registrations from registry. """
58 logger.debug("REG: removing all object registrations")
59 self._object_registry = []
60 self._object_dict = dict()
62 def remove_vpp_config(self, logger):
64 Remove configuration (if present) from vpp and then remove all objects
67 if not self._object_registry:
68 logger.info("REG: No objects registered for auto-cleanup.")
70 logger.info("REG: Removing VPP configuration for registered objects")
71 # remove the config in reverse order as there might be dependencies
73 for obj in reversed(self._object_registry):
74 if obj.query_vpp_config():
75 logger.info("REG: Removing configuration for %s" % obj)
76 obj.remove_vpp_config()
77 if obj.query_vpp_config():
81 "REG: Skipping removal for %s, configuration not present" %
83 self.unregister_all(logger)
85 logger.error("REG: Couldn't remove configuration for object(s):")
87 logger.error(moves.reprlib.repr(obj))
88 raise Exception("Couldn't remove configuration for object(s): %s" %
89 (", ".join(str(x) for x in failed)))