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