Skip to content

Commit 552d3a7

Browse files
authored
Merge pull request #521 from kytos-ng/release/2024.1.3
Backport: Added feature to take last and avoid vlan 2024.1
2 parents 9e5d25f + 4dc0120 commit 552d3a7

File tree

5 files changed

+60
-16
lines changed

5 files changed

+60
-16
lines changed

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ All notable changes to the kytos project will be documented in this file.
66
UNRELEASED - Under development
77
******************************
88

9+
[2024.1.3] - 2024-12-05
10+
***********************
11+
12+
Added
13+
=====
14+
- Added option for links to get last TAG from ``interface.available_tags``.
15+
- Added option for links to try to avoid a TAG value from ``interface.available_tags``.
16+
917
[2024.1.2] - 2024-09-16
1018
***********************
1119

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@
6464
# built documents.
6565
#
6666
# The short X.Y version.
67-
version = u'2024.1.2'
67+
version = u'2024.1.3'
6868
show_version = False
6969
# The full version, including alpha/beta/rc tags.
70-
release = u'2024.1.2'
70+
release = u'2024.1.3'
7171

7272
# The language for content autogenerated by Sphinx. Refer to documentation
7373
# for a list of supported languages.

kytos/core/link.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,42 @@ def is_tag_available(self, tag: int, tag_type: str = 'vlan'):
143143
def get_next_available_tag(
144144
self,
145145
controller,
146-
link_id,
147-
tag_type: str = 'vlan'
146+
link_id: str,
147+
take_last: bool = False,
148+
tag_type: str = 'vlan',
149+
try_avoid_value: int = None,
148150
) -> int:
149-
"""Return the next available tag if exists."""
151+
"""Return the next available tag if exists. By default this
152+
method returns the smallest tag available. Apply options to
153+
change behavior.
154+
Options:
155+
- take_last (bool): Choose the largest tag available.
156+
- try_avoid_value (int): Avoid given tag if possible. Otherwise
157+
return what is available.
158+
"""
150159
with self._get_available_vlans_lock[link_id]:
151160
with self.endpoint_a._tag_lock:
152161
with self.endpoint_b._tag_lock:
153162
ava_tags_a = self.endpoint_a.available_tags[tag_type]
154163
ava_tags_b = self.endpoint_b.available_tags[tag_type]
155-
tags = range_intersection(ava_tags_a,
156-
ava_tags_b)
164+
tags = range_intersection(ava_tags_a, ava_tags_b,
165+
take_last)
157166
try:
158-
tag, _ = next(tags)
167+
tag_range = next(tags)
168+
if (try_avoid_value is not None and
169+
tag_range[take_last] == try_avoid_value):
170+
if (tag_range[take_last] !=
171+
tag_range[not take_last]):
172+
tag = tag_range[take_last]
173+
tag += (+1) if not take_last else (-1)
174+
else:
175+
try:
176+
tag = next(tags)[take_last]
177+
except StopIteration:
178+
tag = tag_range[take_last]
179+
else:
180+
tag = tag_range[take_last]
181+
159182
self.endpoint_a.use_tags(
160183
controller, tag, use_lock=False, check_order=False
161184
)

kytos/core/metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
The present metadata is intended to be used mainly on the setup.
44
"""
5-
__version__ = '2024.1.2'
5+
__version__ = '2024.1.3'
66
__author__ = 'Kytos Team'
77
__author_email__ = '[email protected]'
88
__license__ = 'MIT'

kytos/core/tag_ranges.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def get_validated_tags(
9393

9494
def range_intersection(
9595
ranges_a: list[list[int]],
96-
ranges_b: list[list[int]]
96+
ranges_b: list[list[int]],
97+
reverse: bool = False,
9798
) -> Iterator[list[int]]:
9899
"""Returns an iterator of an intersection between
99100
two validated list of ranges.
@@ -105,23 +106,35 @@ def range_intersection(
105106
get_validated_tags() for also list[int]
106107
"""
107108
a_i, b_i = 0, 0
108-
while a_i < len(ranges_a) and b_i < len(ranges_b):
109+
index_diff = 1
110+
if reverse:
111+
a_i = len(ranges_a) - 1
112+
b_i = len(ranges_b) - 1
113+
index_diff = -1
114+
while 0 <= a_i < len(ranges_a) and 0 <= b_i < len(ranges_b):
109115
fst_a, snd_a = ranges_a[a_i]
110116
fst_b, snd_b = ranges_b[b_i]
111117
# Moving forward with non-intersection
112118
if snd_a < fst_b:
113-
a_i += 1
119+
if not reverse:
120+
a_i += index_diff
121+
else:
122+
b_i += index_diff
114123
elif snd_b < fst_a:
115-
b_i += 1
124+
if not reverse:
125+
b_i += index_diff
126+
else:
127+
a_i += index_diff
116128
else:
117129
# Intersection
118130
intersection_start = max(fst_a, fst_b)
119131
intersection_end = min(snd_a, snd_b)
120132
yield [intersection_start, intersection_end]
121-
if snd_a < snd_b:
122-
a_i += 1
133+
move_from_a = snd_a < snd_b if not reverse else fst_a > fst_b
134+
if move_from_a:
135+
a_i += index_diff
123136
else:
124-
b_i += 1
137+
b_i += index_diff
125138

126139

127140
def range_difference(

0 commit comments

Comments
 (0)