Skip to content

Commit

Permalink
Improved docstring
Browse files Browse the repository at this point in the history
- Change parameter avoid_tag: TAG to try_avoid_value: int.
  • Loading branch information
Alopalao committed Nov 21, 2024
1 parent 543eb21 commit 435dd35
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
17 changes: 12 additions & 5 deletions kytos/core/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from kytos.core.exceptions import (KytosLinkCreationError,
KytosNoTagAvailableError)
from kytos.core.id import LinkID
from kytos.core.interface import TAG, Interface, TAGType
from kytos.core.interface import Interface, TAGType
from kytos.core.tag_ranges import range_intersection


Expand Down Expand Up @@ -146,9 +146,16 @@ def get_next_available_tag(
link_id: str,
take_last: bool = False,
tag_type: str = 'vlan',
avoid_tag: TAG = None,
try_avoid_value: int = None,
) -> int:
"""Return the next available tag if exists."""
"""Return the next available tag if exists. By default this
method returns the smallest tag available. Apply options to
change behavior.
Options:
- take_last (bool): Choose the largest tag available.
- try_avoid_value (int): Avoid given tag if possible. Otherwise
return what is available.
"""
with self._get_available_vlans_lock[link_id]:
with self.endpoint_a._tag_lock:
with self.endpoint_b._tag_lock:
Expand All @@ -158,8 +165,8 @@ def get_next_available_tag(
take_last)
try:
tag_range: list = next(tags)
if (avoid_tag and
tag_range[take_last] == avoid_tag.value):
if (try_avoid_value is not None and
tag_range[take_last] == try_avoid_value):
if (tag_range[take_last] !=
tag_range[not take_last]):
tag = tag_range[take_last]
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/test_core/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from kytos.core.common import EntityStatus
from kytos.core.exceptions import KytosLinkCreationError
from kytos.core.interface import TAG, Interface, TAGType
from kytos.core.interface import Interface, TAGType
from kytos.core.link import Link
from kytos.core.switch import Switch

Expand Down Expand Up @@ -223,44 +223,44 @@ def test_get_next_available_tag_reverse(self, controller):
def test_get_next_available_tag_avoid_tag(self, controller):
"""Test get next available tag avoiding a tag"""
link = Link(self.iface1, self.iface2)
avoid_tag = TAG('vlan', 1)
avoid_vlan = 1

# Tag different that avoid tag is available in the same range
link.endpoint_a.available_tags['vlan'] = [[1, 5]]
tag = link.get_next_available_tag(
controller, "link_id", avoid_tag=avoid_tag
controller, "link_id", try_avoid_value=avoid_vlan
)
assert tag == 2

# The next tag is the next range
link.endpoint_a.available_tags['vlan'] = [[1, 1], [100, 300]]
tag = link.get_next_available_tag(
controller, "link_id", avoid_tag=avoid_tag
controller, "link_id", try_avoid_value=avoid_vlan
)
assert tag == 100

# No more tags available
link.endpoint_a.available_tags['vlan'] = [[1, 1]]
tag = link.get_next_available_tag(
controller, "link_id", avoid_tag=avoid_tag
controller, "link_id", try_avoid_value=avoid_vlan
)
assert tag == 1

# Same cases but with reversed
avoid_tag = TAG('vlan', 50)
avoid_vlan = 50
link.endpoint_a.available_tags['vlan'] = [[3, 50]]
tag = link.get_next_available_tag(
controller, "link_id", take_last=True, avoid_tag=avoid_tag
controller, "link_id", True, try_avoid_value=avoid_vlan
)
assert tag == 49
link.endpoint_a.available_tags['vlan'] = [[1, 20], [50, 50]]
tag = link.get_next_available_tag(
controller, "link_id", take_last=True, avoid_tag=avoid_tag
controller, "link_id", True, try_avoid_value=avoid_vlan
)
assert tag == 20
link.endpoint_a.available_tags['vlan'] = [[50, 50]]
tag = link.get_next_available_tag(
controller, "link_id", take_last=True, avoid_tag=avoid_tag
controller, "link_id", True, try_avoid_value=avoid_vlan
)
assert tag == 50

Expand Down

0 comments on commit 435dd35

Please sign in to comment.