blob: 8dd2afac97c05cf96c2a61786fd40acae105628a [file] [log] [blame]
Klement Sekeraacb9b8e2017-02-14 02:55:31 +01001""" abstract vpp object and object registry """
2
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -08003import abc
Paul Vinciguerra00671cf2018-11-25 12:47:04 -08004
Klement Sekera0e3c0de2016-09-29 14:43:44 +02005
Paul Vinciguerra090096b2020-12-03 00:42:46 -05006class VppObject(metaclass=abc.ABCMeta):
Klement Sekera0e3c0de2016-09-29 14:43:44 +02007 """ Abstract vpp object """
Klement Sekera0e3c0de2016-09-29 14:43:44 +02008
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -08009 @abc.abstractmethod
Paul Vinciguerra45eff782020-12-01 11:33:28 -050010 def add_vpp_config(self) -> None:
Klement Sekera0e3c0de2016-09-29 14:43:44 +020011 """ Add the configuration for this object to vpp. """
12 pass
13
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080014 @abc.abstractmethod
Paul Vinciguerra45eff782020-12-01 11:33:28 -050015 def query_vpp_config(self) -> bool:
Klement Sekera0e3c0de2016-09-29 14:43:44 +020016 """Query the vpp configuration.
17
18 :return: True if the object is configured"""
19 pass
20
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080021 @abc.abstractmethod
Paul Vinciguerra45eff782020-12-01 11:33:28 -050022 def remove_vpp_config(self) -> None:
Klement Sekera0e3c0de2016-09-29 14:43:44 +020023 """ Remove the configuration for this object from vpp. """
24 pass
25
Paul Vinciguerra45eff782020-12-01 11:33:28 -050026 def object_id(self) -> str:
Klement Sekera0e3c0de2016-09-29 14:43:44 +020027 """ Return a unique string representing this object. """
Paul Vinciguerra3593a912019-03-28 09:53:44 -070028 return "Undefined. for <%s %s>" % (self.__class__.__name__, id(self))
29
Paul Vinciguerra45eff782020-12-01 11:33:28 -050030 def __str__(self) -> str:
Paul Vinciguerra3593a912019-03-28 09:53:44 -070031 return self.object_id()
32
Paul Vinciguerra45eff782020-12-01 11:33:28 -050033 def __repr__(self) -> str:
Paul Vinciguerra3593a912019-03-28 09:53:44 -070034 return '<%s>' % self.object_id()
35
Paul Vinciguerra45eff782020-12-01 11:33:28 -050036 def __hash__(self) -> int:
Paul Vinciguerra3593a912019-03-28 09:53:44 -070037 return hash(self.object_id())
38
Paul Vinciguerra45eff782020-12-01 11:33:28 -050039 def __eq__(self, other) -> bool:
Paul Vinciguerra3593a912019-03-28 09:53:44 -070040 if not isinstance(other, self.__class__):
41 return NotImplemented
42 if other.object_id() == self.object_id():
43 return True
44 return False
45
46 # This can be removed when python2 support is dropped.
47 def __ne__(self, other):
48 return not self.__eq__(other)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020049
50
Paul Vinciguerrae061dad2020-12-04 14:57:51 -050051class VppObjectRegistry:
Klement Sekera0e3c0de2016-09-29 14:43:44 +020052 """ Class which handles automatic configuration cleanup. """
53 _shared_state = {}
54
Paul Vinciguerra45eff782020-12-01 11:33:28 -050055 def __init__(self) -> None:
Klement Sekera0e3c0de2016-09-29 14:43:44 +020056 self.__dict__ = self._shared_state
57 if not hasattr(self, "_object_registry"):
58 self._object_registry = []
59 if not hasattr(self, "_object_dict"):
60 self._object_dict = dict()
61
Paul Vinciguerra45eff782020-12-01 11:33:28 -050062 def register(self, obj: VppObject, logger) -> None:
Klement Sekera0e3c0de2016-09-29 14:43:44 +020063 """ Register an object in the registry. """
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010064 if obj.object_id() not in self._object_dict:
65 self._object_registry.append(obj)
66 self._object_dict[obj.object_id()] = obj
Klement Sekera73884482017-02-23 09:26:30 +010067 logger.debug("REG: registering %s" % obj)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020068 else:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010069 logger.debug("REG: duplicate add, ignoring (%s)" % obj)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020070
Paul Vinciguerra45eff782020-12-01 11:33:28 -050071 def unregister_all(self, logger) -> None:
Klement Sekera1b686402017-03-02 11:29:19 +010072 """ Remove all object registrations from registry. """
73 logger.debug("REG: removing all object registrations")
74 self._object_registry = []
75 self._object_dict = dict()
76
Paul Vinciguerra45eff782020-12-01 11:33:28 -050077 def remove_vpp_config(self, logger) -> None:
Klement Sekera0e3c0de2016-09-29 14:43:44 +020078 """
79 Remove configuration (if present) from vpp and then remove all objects
80 from the registry.
81 """
82 if not self._object_registry:
Klement Sekera10db26f2017-01-11 08:16:53 +010083 logger.info("REG: No objects registered for auto-cleanup.")
Klement Sekera0e3c0de2016-09-29 14:43:44 +020084 return
Klement Sekera10db26f2017-01-11 08:16:53 +010085 logger.info("REG: Removing VPP configuration for registered objects")
86 # remove the config in reverse order as there might be dependencies
Neale Rannsf0510722018-01-31 11:35:41 -080087 failed = []
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010088 for obj in reversed(self._object_registry):
89 if obj.query_vpp_config():
90 logger.info("REG: Removing configuration for %s" % obj)
91 obj.remove_vpp_config()
Neale Rannsf0510722018-01-31 11:35:41 -080092 if obj.query_vpp_config():
93 failed.append(obj)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020094 else:
Klement Sekera10db26f2017-01-11 08:16:53 +010095 logger.info(
96 "REG: Skipping removal for %s, configuration not present" %
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010097 obj)
Klement Sekera1b686402017-03-02 11:29:19 +010098 self.unregister_all(logger)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020099 if failed:
Klement Sekera10db26f2017-01-11 08:16:53 +0100100 logger.error("REG: Couldn't remove configuration for object(s):")
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100101 for obj in failed:
Paul Vinciguerra3593a912019-03-28 09:53:44 -0700102 logger.error(repr(obj))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200103 raise Exception("Couldn't remove configuration for object(s): %s" %
104 (", ".join(str(x) for x in failed)))