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