Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1 | from abc import ABCMeta, abstractmethod |
| 2 | |
| 3 | |
| 4 | class VppObject(object): |
| 5 | """ Abstract vpp object """ |
| 6 | __metaclass__ = ABCMeta |
| 7 | |
| 8 | def __init__(self): |
| 9 | VppObjectRegistry().register(self) |
| 10 | |
| 11 | @abstractmethod |
| 12 | def add_vpp_config(self): |
| 13 | """ Add the configuration for this object to vpp. """ |
| 14 | pass |
| 15 | |
| 16 | @abstractmethod |
| 17 | def query_vpp_config(self): |
| 18 | """Query the vpp configuration. |
| 19 | |
| 20 | :return: True if the object is configured""" |
| 21 | pass |
| 22 | |
| 23 | @abstractmethod |
| 24 | def remove_vpp_config(self): |
| 25 | """ Remove the configuration for this object from vpp. """ |
| 26 | pass |
| 27 | |
| 28 | @abstractmethod |
| 29 | def object_id(self): |
| 30 | """ Return a unique string representing this object. """ |
| 31 | pass |
| 32 | |
| 33 | |
| 34 | class VppObjectRegistry(object): |
| 35 | """ Class which handles automatic configuration cleanup. """ |
| 36 | _shared_state = {} |
| 37 | |
| 38 | def __init__(self): |
| 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() |
| 44 | |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 45 | def register(self, o, logger): |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 46 | """ Register an object in the registry. """ |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 47 | if not o.object_id() in self._object_dict: |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 48 | self._object_registry.append(o) |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 49 | self._object_dict[o.object_id()] = o |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 50 | else: |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 51 | logger.debug("REG: duplicate add, ignoring (%s)" % o) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 52 | |
| 53 | def remove_vpp_config(self, logger): |
| 54 | """ |
| 55 | Remove configuration (if present) from vpp and then remove all objects |
| 56 | from the registry. |
| 57 | """ |
| 58 | if not self._object_registry: |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 59 | logger.info("REG: No objects registered for auto-cleanup.") |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 60 | return |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 61 | logger.info("REG: Removing VPP configuration for registered objects") |
| 62 | # remove the config in reverse order as there might be dependencies |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 63 | for o in reversed(self._object_registry): |
| 64 | if o.query_vpp_config(): |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 65 | logger.info("REG: Removing configuration for %s" % o) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 66 | o.remove_vpp_config() |
| 67 | else: |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 68 | logger.info( |
| 69 | "REG: Skipping removal for %s, configuration not present" % |
| 70 | o) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 71 | failed = [] |
| 72 | for o in self._object_registry: |
| 73 | if o.query_vpp_config(): |
| 74 | failed.append(o) |
| 75 | self._object_registry = [] |
| 76 | self._object_dict = dict() |
| 77 | if failed: |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 78 | logger.error("REG: Couldn't remove configuration for object(s):") |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 79 | for x in failed: |
| 80 | logger.error(repr(x)) |
| 81 | raise Exception("Couldn't remove configuration for object(s): %s" % |
| 82 | (", ".join(str(x) for x in failed))) |