blob: 1b0fadae569c9fda56a5af5cc4dec9f7cf38bde5 [file] [log] [blame]
Klement Sekeraacb9b8e2017-02-14 02:55:31 +01001""" abstract vpp object and object registry """
2
Klement Sekera0e3c0de2016-09-29 14:43:44 +02003from abc import ABCMeta, abstractmethod
4
Paul Vinciguerra6c746172018-11-26 09:57:21 -08005from six import moves
Paul Vinciguerra00671cf2018-11-25 12:47:04 -08006
Klement Sekera0e3c0de2016-09-29 14:43:44 +02007
8class VppObject(object):
9 """ Abstract vpp object """
10 __metaclass__ = ABCMeta
11
Klement Sekera0e3c0de2016-09-29 14:43:44 +020012 @abstractmethod
13 def add_vpp_config(self):
14 """ Add the configuration for this object to vpp. """
15 pass
16
17 @abstractmethod
18 def query_vpp_config(self):
19 """Query the vpp configuration.
20
21 :return: True if the object is configured"""
22 pass
23
24 @abstractmethod
25 def remove_vpp_config(self):
26 """ Remove the configuration for this object from vpp. """
27 pass
28
29 @abstractmethod
30 def object_id(self):
31 """ Return a unique string representing this object. """
32 pass
33
34
35class VppObjectRegistry(object):
36 """ Class which handles automatic configuration cleanup. """
37 _shared_state = {}
38
39 def __init__(self):
40 self.__dict__ = self._shared_state
41 if not hasattr(self, "_object_registry"):
42 self._object_registry = []
43 if not hasattr(self, "_object_dict"):
44 self._object_dict = dict()
45
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010046 def register(self, obj, logger):
Klement Sekera0e3c0de2016-09-29 14:43:44 +020047 """ Register an object in the registry. """
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010048 if obj.object_id() not in self._object_dict:
49 self._object_registry.append(obj)
50 self._object_dict[obj.object_id()] = obj
Klement Sekera73884482017-02-23 09:26:30 +010051 logger.debug("REG: registering %s" % obj)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020052 else:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010053 logger.debug("REG: duplicate add, ignoring (%s)" % obj)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020054
Klement Sekera1b686402017-03-02 11:29:19 +010055 def unregister_all(self, logger):
56 """ Remove all object registrations from registry. """
57 logger.debug("REG: removing all object registrations")
58 self._object_registry = []
59 self._object_dict = dict()
60
Klement Sekera0e3c0de2016-09-29 14:43:44 +020061 def remove_vpp_config(self, logger):
62 """
63 Remove configuration (if present) from vpp and then remove all objects
64 from the registry.
65 """
66 if not self._object_registry:
Klement Sekera10db26f2017-01-11 08:16:53 +010067 logger.info("REG: No objects registered for auto-cleanup.")
Klement Sekera0e3c0de2016-09-29 14:43:44 +020068 return
Klement Sekera10db26f2017-01-11 08:16:53 +010069 logger.info("REG: Removing VPP configuration for registered objects")
70 # remove the config in reverse order as there might be dependencies
Neale Rannsf0510722018-01-31 11:35:41 -080071 failed = []
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010072 for obj in reversed(self._object_registry):
73 if obj.query_vpp_config():
74 logger.info("REG: Removing configuration for %s" % obj)
75 obj.remove_vpp_config()
Neale Rannsf0510722018-01-31 11:35:41 -080076 if obj.query_vpp_config():
77 failed.append(obj)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020078 else:
Klement Sekera10db26f2017-01-11 08:16:53 +010079 logger.info(
80 "REG: Skipping removal for %s, configuration not present" %
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010081 obj)
Klement Sekera1b686402017-03-02 11:29:19 +010082 self.unregister_all(logger)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020083 if failed:
Klement Sekera10db26f2017-01-11 08:16:53 +010084 logger.error("REG: Couldn't remove configuration for object(s):")
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010085 for obj in failed:
Paul Vinciguerra6c746172018-11-26 09:57:21 -080086 logger.error(moves.reprlib.repr(obj))
Klement Sekera0e3c0de2016-09-29 14:43:44 +020087 raise Exception("Couldn't remove configuration for object(s): %s" %
88 (", ".join(str(x) for x in failed)))