Class which handles automatic configuration cleanup.
| 49 | |
| 50 | |
| 51 | class VppObjectRegistry: |
| 52 | """Class which handles automatic configuration cleanup.""" |
| 53 | |
| 54 | _shared_state = {} |
| 55 | |
| 56 | def __init__(self) -> None: |
| 57 | self.__dict__ = self._shared_state |
| 58 | if not hasattr(self, "_object_registry"): |
| 59 | self._object_registry = [] |
| 60 | if not hasattr(self, "_object_dict"): |
| 61 | self._object_dict = dict() |
| 62 | |
| 63 | def register_exclusive(self, obj: VppObject, logger) -> None: |
| 64 | """Register an object in the registry. Raise exception if duplicate.""" |
| 65 | if obj.object_id() not in self._object_dict: |
| 66 | self._object_registry.append(obj) |
| 67 | self._object_dict[obj.object_id()] = obj |
| 68 | logger.debug("REG: registering %s" % obj) |
| 69 | else: |
| 70 | raise Exception("REG: duplicate add: %s" % obj) |
| 71 | |
| 72 | def register(self, obj: VppObject, logger) -> None: |
| 73 | """Register an object in the registry.""" |
| 74 | if obj.object_id() not in self._object_dict: |
| 75 | self._object_registry.append(obj) |
| 76 | self._object_dict[obj.object_id()] = obj |
| 77 | logger.debug("REG: registering %s" % obj) |
| 78 | else: |
| 79 | logger.debug("REG: duplicate add, ignoring (%s)" % obj) |
| 80 | |
| 81 | def unregister_all(self, logger) -> None: |
| 82 | """Remove all object registrations from registry.""" |
| 83 | logger.debug("REG: removing all object registrations") |
| 84 | self._object_registry = [] |
| 85 | self._object_dict = dict() |
| 86 | |
| 87 | def remove_vpp_config(self, logger) -> None: |
| 88 | """ |
| 89 | Remove configuration (if present) from vpp and then remove all objects |
| 90 | from the registry. |
| 91 | """ |
| 92 | if not self._object_registry: |
| 93 | logger.info("REG: No objects registered for auto-cleanup.") |
| 94 | return |
| 95 | logger.info("REG: Removing VPP configuration for registered objects") |
| 96 | # remove the config in reverse order as there might be dependencies |
| 97 | failed = [] |
| 98 | for obj in reversed(self._object_registry): |
| 99 | if obj.query_vpp_config(): |
| 100 | logger.info("REG: Removing configuration for %s" % obj) |
| 101 | obj.remove_vpp_config() |
| 102 | if obj.query_vpp_config(): |
| 103 | failed.append(obj) |
| 104 | else: |
| 105 | logger.info( |
| 106 | "REG: Skipping removal for %s, configuration not present" % obj |
| 107 | ) |
| 108 | self.unregister_all(logger) |
no outgoing calls
no test coverage detected