aboutsummaryrefslogtreecommitdiffstats
path: root/vpp/test/vpp_object.py
blob: 2b71fc1fd39734366e1772a7f96894511db78fa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from abc import ABCMeta, abstractmethod


class VppObject(object):
    """ Abstract vpp object """
    __metaclass__ = ABCMeta

    def __init__(self):
        VppObjectRegistry().register(self)

    @abstractmethod
    def add_vpp_config(self):
        """ Add the configuration for this object to vpp. """
        pass

    @abstractmethod
    def query_vpp_config(self):
        """Query the vpp configuration.

        :return: True if the object is configured"""
        pass

    @abstractmethod
    def remove_vpp_config(self):
        """ Remove the configuration for this object from vpp. """
        pass

    @abstractmethod
    def object_id(self):
        """ Return a unique string representing this object. """
        pass


class VppObjectRegistry(object):
    """ Class which handles automatic configuration cleanup. """
    _shared_state = {}

    def __init__(self):
        self.__dict__ = self._shared_state
        if not hasattr(self, "_object_registry"):
            self._object_registry = []
        if not hasattr(self, "_object_dict"):
            self._object_dict = dict()

    def register(self, o):
        """ Register an object in the registry. """
        if not o.unique_id() in self._object_dict:
            self._object_registry.append(o)
            self._object_dict[o.unique_id()] = o
        else:
            print "not adding duplicate %s" % o

    def remove_vpp_config(self, logger):
        """
        Remove configuration (if present) from vpp and then remove all objects
        from the registry.
        """
        if not self._object_registry:
            logger.info("No objects registered for auto-cleanup.")
            return
        logger.info("Removing VPP configuration for registered objects")
        for o in reversed(self._object_registry):
            if o.query_vpp_config():
                logger.info("Removing %s", o)
                o.remove_vpp_config()
            else:
                logger.info("Skipping %s, configuration not present", o)
        failed = []
        for o in self._object_registry:
            if o.query_vpp_config():
                failed.append(o)
        self._object_registry = []
        self._object_dict = dict()
        if failed:
            logger.error("Couldn't remove configuration for object(s):")
            for x in failed:
                logger.error(repr(x))
            raise Exception("Couldn't remove configuration for object(s): %s" %
                            (", ".join(str(x) for x in failed)))