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
|
|
|
|
12
|
|
|
|
13
|
|
|
class Link(GenericEntity): |
14
|
|
|
"""Define a link between two Endpoints.""" |
15
|
|
|
|
16
|
|
|
def __init__(self, endpoint_a, endpoint_b): |
17
|
|
|
"""Create a Link instance and set its attributes.""" |
18
|
|
|
self.endpoint_a = endpoint_a |
19
|
|
|
self.endpoint_b = endpoint_b |
20
|
|
|
self._uuid = uuid4() |
21
|
|
|
super().__init__() |
22
|
|
|
|
23
|
|
|
def __eq__(self, other): |
24
|
|
|
"""Check if two instances of Link are equal.""" |
25
|
|
|
return ((self.endpoint_a == other.endpoint_a and |
26
|
|
|
self.endpoint_b == other.endpoint_b) or |
27
|
|
|
(self.endpoint_a == other.endpoint_b and |
28
|
|
|
self.endpoint_b == other.endpoint_a)) |
29
|
|
|
|
30
|
|
|
@property |
31
|
|
|
def id(self): # pylint: disable=invalid-name |
32
|
|
|
"""Return id from Link intance. |
33
|
|
|
|
34
|
|
|
Returns: |
35
|
|
|
string: link id. |
36
|
|
|
|
37
|
|
|
""" |
38
|
|
|
return "{}".format(self._uuid) |
39
|
|
|
|
40
|
|
|
def enable(self): |
41
|
|
|
"""Enable this link instance. |
42
|
|
|
|
43
|
|
|
Also enable the link's interfaces and the switches they're attached to. |
44
|
|
|
""" |
45
|
|
|
self.endpoint_a.enable() |
46
|
|
|
self.endpoint_b.enable() |
47
|
|
|
self.enabled = True |
48
|
|
|
|
49
|
|
|
def as_dict(self): |
50
|
|
|
"""Return the Link as a dictionary.""" |
51
|
|
|
return {'id': self.id, |
52
|
|
|
'endpoint_a': self.endpoint_a.as_dict(), |
53
|
|
|
'endpoint_b': self.endpoint_b.as_dict(), |
54
|
|
|
'metadata': self.metadata, |
55
|
|
|
'active': self.active, |
56
|
|
|
'enabled': self.enabled} |
57
|
|
|
|
58
|
|
|
def as_json(self): |
59
|
|
|
"""Return the Link as a JSON string.""" |
60
|
|
|
return json.dumps(self.as_dict()) |
61
|
|
|
|