1
|
|
|
"""Module with all classes related to links. |
2
|
|
|
|
3
|
|
|
Links are low level abstractions representing connections between two |
4
|
|
|
interfaces. |
5
|
|
|
""" |
6
|
|
|
|
7
|
|
|
import json |
8
|
|
|
from uuid import uuid4 |
9
|
|
|
|
10
|
|
|
from kytos.core.common import GenericEntity |
11
|
|
|
from kytos.core.interface import TAGType |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class Link(GenericEntity): |
15
|
|
|
"""Define a link between two Endpoints.""" |
16
|
|
|
|
17
|
|
|
def __init__(self, endpoint_a, endpoint_b): |
18
|
|
|
"""Create a Link instance and set its attributes.""" |
19
|
|
|
self.endpoint_a = endpoint_a |
20
|
|
|
self.endpoint_b = endpoint_b |
21
|
|
|
self._uuid = uuid4() |
22
|
|
|
super().__init__() |
23
|
|
|
|
24
|
|
|
def is_enabled(self): |
25
|
|
|
"""Override the is_enabled method. |
26
|
|
|
|
27
|
|
|
We consider a link enabled whether all the interfaces are enabled. |
28
|
|
|
|
29
|
|
|
Returns: |
30
|
|
|
boolean: True if the interfaces are enabled, othewrise False. |
31
|
|
|
|
32
|
|
|
""" |
33
|
|
|
return (self.is_enabled() and self.endpoint_a.is_enabled() and |
34
|
|
|
self.endpoint_b.is_enabled()) |
35
|
|
|
|
36
|
|
|
def is_active(self): |
37
|
|
|
"""Override the is_active method. |
38
|
|
|
|
39
|
|
|
We consider a link active whether all the interfaces are active. |
40
|
|
|
|
41
|
|
|
Returns: |
42
|
|
|
boolean: True if the interfaces are active, othewrise False. |
43
|
|
|
|
44
|
|
|
""" |
45
|
|
|
return (self.is_active() and self.endpoint_a.is_active() and |
46
|
|
|
self.endpoint_b.is_active()) |
47
|
|
|
|
48
|
|
|
def __eq__(self, other): |
49
|
|
|
"""Check if two instances of Link are equal.""" |
50
|
|
|
return ((self.endpoint_a == other.endpoint_a and |
51
|
|
|
self.endpoint_b == other.endpoint_b) or |
52
|
|
|
(self.endpoint_a == other.endpoint_b and |
53
|
|
|
self.endpoint_b == other.endpoint_a)) |
54
|
|
|
|
55
|
|
|
@property |
56
|
|
|
def id(self): # pylint: disable=invalid-name |
57
|
|
|
"""Return id from Link intance. |
58
|
|
|
|
59
|
|
|
Returns: |
60
|
|
|
string: link id. |
61
|
|
|
|
62
|
|
|
""" |
63
|
|
|
return "{}".format(self._uuid) |
64
|
|
|
|
65
|
|
|
@property |
66
|
|
|
def available_tags(self): |
67
|
|
|
"""Return the available tags for the link. |
68
|
|
|
|
69
|
|
|
Based on the endpoint tags. |
70
|
|
|
""" |
71
|
|
|
return [tag for tag in self.endpoint_a.available_tags if tag in |
72
|
|
|
self.endpoint_b.available_tags] |
73
|
|
|
|
74
|
|
|
def use_tag(self, tag): |
75
|
|
|
"""Remove a specific tag from available_tags if it is there.""" |
76
|
|
|
if self.is_tag_available(tag): |
77
|
|
|
self.endpoint_a.use_tag(tag) |
78
|
|
|
self.endpoint_b.use_tag(tag) |
79
|
|
|
return True |
80
|
|
|
return False |
81
|
|
|
|
82
|
|
|
def is_tag_available(self, tag): |
83
|
|
|
"""Check if a tag is available.""" |
84
|
|
|
return (self.endpoint_a.is_tag_available(tag) and |
85
|
|
|
self.endpoint_b.is_tag_available(tag)) |
86
|
|
|
|
87
|
|
|
def get_next_available_tag(self): |
88
|
|
|
"""Return the next available tag if exists.""" |
89
|
|
|
for tag in self.endpoint_a.available_tags: |
90
|
|
|
if tag in self.endpoint_b.available_tags: |
91
|
|
|
return tag |
92
|
|
|
return False |
93
|
|
|
|
94
|
|
|
def make_tag_available(self, tag): |
95
|
|
|
"""Add a specific tag in available_tags.""" |
96
|
|
|
if not self.is_tag_available(tag): |
97
|
|
|
self.endpoint_a.make_tag_available(tag) |
98
|
|
|
self.endpoint_b.make_tag_available(tag) |
99
|
|
|
else: |
100
|
|
|
return False |
101
|
|
|
|
102
|
|
|
def available_vlans(self): |
103
|
|
|
"""Get all available vlans from each interface in the link.""" |
104
|
|
|
vlans_a = self._get_available_vlans(self.endpoint_a) |
105
|
|
|
vlans_b = self._get_available_vlans(self.endpoint_a) |
106
|
|
|
return [vlan for vlan in vlans_a if vlan in vlans_b] |
107
|
|
|
|
108
|
|
|
@staticmethod |
109
|
|
|
def _get_available_vlans(endpoint): |
110
|
|
|
"""Return all vlans from endpoint.""" |
111
|
|
|
tags = endpoint.available_tags |
112
|
|
|
return [tag for tag in tags if tag.tag_type == TAGType.VLAN] |
113
|
|
|
|
114
|
|
|
def as_dict(self): |
115
|
|
|
"""Return the Link as a dictionary.""" |
116
|
|
|
return {'id': self.id, |
117
|
|
|
'endpoint_a': self.endpoint_a.as_dict(), |
118
|
|
|
'endpoint_b': self.endpoint_b.as_dict(), |
119
|
|
|
'metadata': self.get_metadata_as_dict(), |
120
|
|
|
'active': self.is_active(), |
121
|
|
|
'enabled': self.is_enabled()} |
122
|
|
|
|
123
|
|
|
def as_json(self): |
124
|
|
|
"""Return the Link as a JSON string.""" |
125
|
|
|
return json.dumps(self.as_dict()) |
126
|
|
|
|