Passed
Push — master ( ac62bd...26785f )
by Beraldo
02:38
created

GenericEntity.get_metadata_as_dict()   A

Complexity

Conditions 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 8.2077

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 7
ccs 1
cts 6
cp 0.1666
crap 8.2077
rs 9.4285
c 0
b 0
f 0
1
"""Module with common classes for the controller."""
2 1
from enum import Enum
3
4 1
from kytos.core.config import KytosConfig
5
6 1
__all__ = ('GenericEntity',)
7
8
9 1
class EntityStatus(Enum):
10
    """Enumeration of possible statuses for GenericEntity instances."""
11
12 1
    UP = 1  # pylint: disable=invalid-name
13 1
    DISABLED = 2
14 1
    DOWN = 3
15
16
17 1
class GenericEntity:
18
    """Generic Class that represents any Entity."""
19
20 1
    def __init__(self):
21
        """Create the GenericEntity object with empty metadata dictionary."""
22 1
        options = KytosConfig().options['daemon']
23 1
        self.metadata = {}
24
        # operational status with True or False
25 1
        self.active = True
26
        # administrative status with True or False
27 1
        self.enabled = options.enable_entities_by_default
28
29 1
    @property
30
    def status(self):
31
        """Return the current status of the Entity."""
32
        if self.enabled and self.active:
33
            return EntityStatus.UP
34
        elif self.is_administrative_down():
35
            return EntityStatus.DISABLED
36
        return EntityStatus.DOWN
37
38 1
    def is_administrative_down(self):
39
        """Return True for disabled Entities."""
40
        return not self.enabled
41
42 1
    def enable(self):
43
        """Administratively enable the Entity.
44
45
        Although this method only sets an 'enabled' flag, always prefer to use
46
        it instead of setting it manually. This allows us to change the
47
        behavior on the future.
48
        """
49
        self.enabled = True
50
51 1
    def disable(self):
52
        """Administratively disable the Entity.
53
54
        This method can disable other related entities. For this behavior,
55
        rewrite it on the child classes.
56
        """
57
        self.enabled = False
58
59 1
    def add_metadata(self, key, value):
60
        """Add a new metadata (key, value)."""
61
        if key in self.metadata:
62
            return False
63
64
        self.metadata[key] = value
65
        return True
66
67 1
    def remove_metadata(self, key):
68
        """Try to remove a specific metadata."""
69
        try:
70
            del self.metadata[key]
71
            return True
72
        except KeyError:
73
            return False
74
75 1
    def get_metadata(self, key):
76
        """Try to get a specific metadata."""
77
        return self.metadata.get(key)
78
79 1
    def get_metadata_as_dict(self):
80
        """Get all metadata values as dict."""
81
        metadata = dict(self.metadata)
82
        for key, value in self.metadata.items():
83
            if hasattr(value, 'as_dict'):
84
                metadata[key] = value.as_dict()
85
        return metadata
86
87 1
    def update_metadata(self, key, value):
88
        """Overwrite a specific metadata."""
89
        self.metadata[key] = value
90
91 1
    def clear_metadata(self):
92
        """Remove all metadata information."""
93
        self.metadata = {}
94
95 1
    def extend_metadata(self, metadatas, force=True):
96
        """Extend the metadata information.
97
98
        If force is True any existing value is overwritten.
99
        """
100
        if force:
101
            return self.metadata.update(metadatas)
102
103
        for key, value in metadatas.items():
104
            self.add_metadata(key, value)
105