blob: d3652b1b62c23086f9ae590ac218ea9c4185a10f [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 Sekerad9b0c6f2022-04-26 19:02:15 +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 Sekerad9b0c6f2022-04-26 19:02:15 +020011 """Add the configuration for this object to vpp."""
Klement Sekera0e3c0de2016-09-29 14:43:44 +020012 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 Sekerad9b0c6f2022-04-26 19:02:15 +020023 """Remove the configuration for this object from vpp."""
Klement Sekera0e3c0de2016-09-29 14:43:44 +020024 pass
25
Paul Vinciguerra45eff782020-12-01 11:33:28 -050026 def object_id(self) -> str:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +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:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020034 return "<%s>" % self.object_id()
Paul Vinciguerra3593a912019-03-28 09:53:44 -070035
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 Sekerad9b0c6f2022-04-26 19:02:15 +020052 """Class which handles automatic configuration cleanup."""
53
Klement Sekera0e3c0de2016-09-29 14:43:44 +020054 _shared_state = {}
55
Paul Vinciguerra45eff782020-12-01 11:33:28 -050056 def __init__(self) -> None:
Klement Sekera0e3c0de2016-09-29 14:43:44 +020057 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
Paul Vinciguerra45eff782020-12-01 11:33:28 -050063 def register(self, obj: VppObject, logger) -> None:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020064 """Register an object in the registry."""
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010065 if obj.object_id() not in self._object_dict:
66 self._object_registry.append(obj)
67 self._object_dict[obj.object_id()] = obj
Klement Sekera73884482017-02-23 09:26:30 +010068 logger.debug("REG: registering %s" % obj)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020069 else:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010070 logger.debug("REG: duplicate add, ignoring (%s)" % obj)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020071
Paul Vinciguerra45eff782020-12-01 11:33:28 -050072 def unregister_all(self, logger) -> None:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020073 """Remove all object registrations from registry."""
Klement Sekera1b686402017-03-02 11:29:19 +010074 logger.debug("REG: removing all object registrations")
75 self._object_registry = []
76 self._object_dict = dict()
77
Paul Vinciguerra45eff782020-12-01 11:33:28 -050078 def remove_vpp_config(self, logger) -> None:
Klement Sekera0e3c0de2016-09-29 14:43:44 +020079 """
80 Remove configuration (if present) from vpp and then remove all objects
81 from the registry.
82 """
83 if not self._object_registry:
Klement Sekera10db26f2017-01-11 08:16:53 +010084 logger.info("REG: No objects registered for auto-cleanup.")
Klement Sekera0e3c0de2016-09-29 14:43:44 +020085 return
Klement Sekera10db26f2017-01-11 08:16:53 +010086 logger.info("REG: Removing VPP configuration for registered objects")
87 # remove the config in reverse order as there might be dependencies
Neale Rannsf0510722018-01-31 11:35:41 -080088 failed = []
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010089 for obj in reversed(self._object_registry):
90 if obj.query_vpp_config():
91 logger.info("REG: Removing configuration for %s" % obj)
92 obj.remove_vpp_config()
Neale Rannsf0510722018-01-31 11:35:41 -080093 if obj.query_vpp_config():
94 failed.append(obj)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020095 else:
Klement Sekera10db26f2017-01-11 08:16:53 +010096 logger.info(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020097 "REG: Skipping removal for %s, configuration not present" % obj
98 )
Klement Sekera1b686402017-03-02 11:29:19 +010099 self.unregister_all(logger)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200100 if failed:
Klement Sekera10db26f2017-01-11 08:16:53 +0100101 logger.error("REG: Couldn't remove configuration for object(s):")
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100102 for obj in failed:
Paul Vinciguerra3593a912019-03-28 09:53:44 -0700103 logger.error(repr(obj))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200104 raise Exception(
105 "Couldn't remove configuration for object(s): %s"
106 % (", ".join(str(x) for x in failed))
107 )