blob: 088cc39ce0a90742e0022c433881a01ac764b74b [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
5
6class VppObject(object):
7 """ Abstract vpp object """
8 __metaclass__ = ABCMeta
9
Klement Sekera0e3c0de2016-09-29 14:43:44 +020010 @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
33class 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 Sekeraacb9b8e2017-02-14 02:55:31 +010044 def register(self, obj, logger):
Klement Sekera0e3c0de2016-09-29 14:43:44 +020045 """ Register an object in the registry. """
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010046 if obj.object_id() not in self._object_dict:
47 self._object_registry.append(obj)
48 self._object_dict[obj.object_id()] = obj
Klement Sekera73884482017-02-23 09:26:30 +010049 logger.debug("REG: registering %s" % obj)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020050 else:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010051 logger.debug("REG: duplicate add, ignoring (%s)" % obj)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020052
Klement Sekera1b686402017-03-02 11:29:19 +010053 def unregister_all(self, logger):
54 """ Remove all object registrations from registry. """
55 logger.debug("REG: removing all object registrations")
56 self._object_registry = []
57 self._object_dict = dict()
58
Klement Sekera0e3c0de2016-09-29 14:43:44 +020059 def remove_vpp_config(self, logger):
60 """
61 Remove configuration (if present) from vpp and then remove all objects
62 from the registry.
63 """
64 if not self._object_registry:
Klement Sekera10db26f2017-01-11 08:16:53 +010065 logger.info("REG: No objects registered for auto-cleanup.")
Klement Sekera0e3c0de2016-09-29 14:43:44 +020066 return
Klement Sekera10db26f2017-01-11 08:16:53 +010067 logger.info("REG: Removing VPP configuration for registered objects")
68 # remove the config in reverse order as there might be dependencies
Neale Rannsf0510722018-01-31 11:35:41 -080069 failed = []
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010070 for obj in reversed(self._object_registry):
71 if obj.query_vpp_config():
72 logger.info("REG: Removing configuration for %s" % obj)
73 obj.remove_vpp_config()
Neale Rannsf0510722018-01-31 11:35:41 -080074 if obj.query_vpp_config():
75 failed.append(obj)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020076 else:
Klement Sekera10db26f2017-01-11 08:16:53 +010077 logger.info(
78 "REG: Skipping removal for %s, configuration not present" %
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010079 obj)
Klement Sekera1b686402017-03-02 11:29:19 +010080 self.unregister_all(logger)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020081 if failed:
Klement Sekera10db26f2017-01-11 08:16:53 +010082 logger.error("REG: Couldn't remove configuration for object(s):")
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010083 for obj in failed:
84 logger.error(repr(obj))
Klement Sekera0e3c0de2016-09-29 14:43:44 +020085 raise Exception("Couldn't remove configuration for object(s): %s" %
86 (", ".join(str(x) for x in failed)))