Passed
Push — master ( cf72e6...2ad18d )
by Humberto
06:56 queued 03:57
created

kytos.core.common.GenericEntity.is_enabled()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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
25 1
        self._active: bool = True
26 1
        self._enabled: bool = options.enable_entities_by_default
27
28 1
    def is_enabled(self) -> bool:
29
        """Return the *administrative* status of the entity."""
30 1
        return self._enabled
31
32 1
    def is_active(self) -> bool:
33
        """Return the *operational* status of the entity."""
34 1
        return self._active
35
36 1
    def activate(self):
37
        """Activate the entity."""
38 1
        self._active = True
39
40 1
    def deactivate(self):
41
        """Deactivate the entity."""
42 1
        self._active = False
43
44 1
    @property
45
    def status(self):
46
        """Return the current status of the Entity."""
47 1
        if self.is_enabled() and self.is_active():
48 1
            return EntityStatus.UP
49 1
        if self.is_administrative_down():
50 1
            return EntityStatus.DISABLED
51 1
        return EntityStatus.DOWN
52
53 1
    def is_administrative_down(self):
54
        """Return True for disabled Entities."""
55 1
        return not self.is_enabled()
56
57 1
    def enable(self):
58
        """Administratively enable the Entity.
59
60
        Although this method only sets an 'enabled' flag, always prefer to use
61
        it instead of setting it manually. This allows us to change the
62
        behavior in the future.
63
        """
64 1
        self._enabled = True
65
66 1
    def disable(self):
67
        """Administratively disable the Entity.
68
69
        This method can disable other related entities. For this behavior,
70
        rewrite it on the child classes.
71
        """
72 1
        self._enabled = False
73
74 1
    def add_metadata(self, key, value):
75
        """Add a new metadata (key, value)."""
76 1
        if key in self.metadata:
77 1
            return False
78
79 1
        self.metadata[key] = value
80 1
        return True
81
82 1
    def remove_metadata(self, key):
83
        """Try to remove a specific metadata."""
84 1
        try:
85 1
            del self.metadata[key]
86 1
            return True
87 1
        except KeyError:
88 1
            return False
89
90 1
    def get_metadata(self, key):
91
        """Try to get a specific metadata."""
92 1
        return self.metadata.get(key)
93
94 1
    def get_metadata_as_dict(self):
95
        """Get all metadata values as dict."""
96 1
        metadata = dict(self.metadata)
97 1
        for key, value in self.metadata.items():
98 1
            if hasattr(value, 'as_dict'):
99 1
                metadata[key] = value.as_dict()
100 1
        return metadata
101
102 1
    def update_metadata(self, key, value):
103
        """Overwrite a specific metadata."""
104 1
        self.metadata[key] = value
105
106 1
    def clear_metadata(self):
107
        """Remove all metadata information."""
108 1
        self.metadata = {}
109
110 1
    def extend_metadata(self, metadatas, force=True):
111
        """Extend the metadata information.
112
113
        If force is True any existing value is overwritten.
114
        """
115 1
        if force:
116 1
            self.metadata.update(metadatas)
117 1
            return
118
119 1
        for key, value in metadatas.items():
120
            self.add_metadata(key, value)
121