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