Passed
Push — master ( 26785f...0f84e3 )
by Beraldo
02:28 queued 10s
created

Link._get_available_vlans()   A

Complexity

Conditions 3

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 12
rs 9.4285
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 __eq__(self, other):
25
        """Check if two instances of Link are equal."""
26
        return ((self.endpoint_a == other.endpoint_a and
27
                 self.endpoint_b == other.endpoint_b) or
28
                (self.endpoint_a == other.endpoint_b and
29
                 self.endpoint_b == other.endpoint_a))
30
31
    @property
32
    def id(self):  # pylint: disable=invalid-name
33
        """Return id from Link intance.
34
35
        Returns:
36
            string: link id.
37
38
        """
39
        return "{}".format(self._uuid)
40
41
    @property
42
    def available_tags(self):
43
        """Return the available tags for the link.
44
45
        Based on the endpoint tags.
46
        """
47
        return [tag for tag in self.endpoint_a.available_tags if tag in
48
                self.endpoint_b.available_tags]
49
50
    def enable(self):
51
        """Enable this link instance.
52
53
        Also enable the link's interfaces and the switches they're attached to.
54
        """
55
        self.endpoint_a.enable()
56
        self.endpoint_b.enable()
57
        self.enabled = True
58
59
    def use_tag(self, tag):
60
        """Remove a specific tag from available_tags if it is there."""
61
        if self.is_tag_available(tag):
62
            self.endpoint_a.use_tag(tag)
63
            self.endpoint_b.use_tag(tag)
64
            return True
65
        return False
66
67
    def is_tag_available(self, tag):
68
        """Check if a tag is available."""
69
        return (self.endpoint_a.is_tag_available(tag) and
70
                self.endpoint_b.is_tag_available(tag))
71
72
    def get_next_available_tag(self):
73
        """Return the next available tag if exists."""
74
        for tag in self.endpoint_a.available_tags:
75
            if tag in self.endpoint_b.available_tags:
76
                return tag
77
        return False
78
79
    def make_tag_available(self, tag):
80
        """Add a specific tag in available_tags."""
81
        if not self.is_tag_available(tag):
82
            self.endpoint_a.make_tag_available(tag)
83
            self.endpoint_b.make_tag_available(tag)
84
        else:
85
            return False
86
87
    def available_vlans(self):
88
        """Get all available vlans from each interface in the link."""
89
        vlans_a = self._get_available_vlans(self.endpoint_a)
90
        vlans_b = self._get_available_vlans(self.endpoint_a)
91
        return [vlan for vlan in vlans_a if vlan in vlans_b]
92
93
    @staticmethod
94
    def _get_available_vlans(endpoint):
95
        """Return all vlans from endpoint."""
96
        tags = endpoint.available_tags
97
        return [tag for tag in tags if tag.tag_type == TAGType.VLAN]
98
99
    def as_dict(self):
100
        """Return the Link as a dictionary."""
101
        return {'id': self.id,
102
                'endpoint_a': self.endpoint_a.as_dict(),
103
                'endpoint_b': self.endpoint_b.as_dict(),
104
                'metadata': self.get_metadata_as_dict(),
105
                'active': self.active,
106
                'enabled': self.enabled}
107
108
    def as_json(self):
109
        """Return the Link as a JSON string."""
110
        return json.dumps(self.as_dict())
111