Passed
Push — master ( 2ba592...9b95a3 )
by Beraldo
02:20
created

Link.get_next_available_tag()   A

Complexity

Conditions 3

Size

Total Lines 6

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 6
ccs 0
cts 5
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
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
    @property
41
    def available_tags(self):
42
        """Return the available tags for the link.
43
44
        Based on the endpoint tags.
45
        """
46
        return [tag for tag in self.endpoint_a.available_tags if tag in
47
                self.endpoint_b.available_tags]
48
49
    def enable(self):
50
        """Enable this link instance.
51
52
        Also enable the link's interfaces and the switches they're attached to.
53
        """
54
        self.endpoint_a.enable()
55
        self.endpoint_b.enable()
56
        self.enabled = True
57
58
    def use_tag(self, tag):
59
        """Remove a specific tag from available_tags if it is there."""
60
        if self.is_tag_available(tag):
61
            self.endpoint_a.use_tag(tag)
62
            self.endpoint_b.use_tag(tag)
63
            return True
64
        return False
65
66
    def is_tag_available(self, tag):
67
        """Check if a tag is available."""
68
        return (self.endpoint_a.is_tag_available(tag) and
69
                self.endpoint_b.is_tag_available(tag))
70
71
    def get_next_available_tag(self):
72
        """Return the next available tag if exists."""
73
        for tag in self.endpoint_a.available_tags:
74
            if tag in self.endpoint_b.available_tags:
75
                return tag
76
        return False
77
78
    def make_tag_available(self, tag):
79
        """Add a specific tag in available_tags."""
80
        if not self.is_tag_available(tag):
81
            self.endpoint_a.make_tag_available(tag)
82
            self.endpoint_b.make_tag_available(tag)
83
        else:
84
            return False
85
86
    def as_dict(self):
87
        """Return the Link as a dictionary."""
88
        return {'id': self.id,
89
                'endpoint_a': self.endpoint_a.as_dict(),
90
                'endpoint_b': self.endpoint_b.as_dict(),
91
                'metadata': self.metadata,
92
                'active': self.active,
93
                'enabled': self.enabled}
94
95
    def as_json(self):
96
        """Return the Link as a JSON string."""
97
        return json.dumps(self.as_dict())
98