Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved _get_tag_ranges() to kytos/core #175

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 6 additions & 50 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from kytos.core import KytosEvent, KytosNApp, log, rest
from kytos.core.common import EntityStatus
from kytos.core.exceptions import (KytosLinkCreationError,
from kytos.core.exceptions import (KytosInvalidRanges, KytosLinkCreationError,
KytosSetTagRangeError,
KytosTagtypeNotSupported)
from kytos.core.helpers import listen_to, load_spec, now, validate_openapi
Expand All @@ -21,6 +21,7 @@
from kytos.core.rest_api import (HTTPException, JSONResponse, Request,
content_type_json_or_415, get_json_or_400)
from kytos.core.switch import Switch
from kytos.core.tag_ranges import get_tag_ranges
from napps.kytos.topology import settings

from .controllers import TopoController
Expand Down Expand Up @@ -486,62 +487,17 @@ def delete_interface_metadata(self, request: Request) -> JSONResponse:
self.notify_metadata_changes(interface, 'removed')
return JSONResponse("Operation successful")

@staticmethod
def map_singular_values(tag_range):
"""Change integer or singular interger list to
list[int, int] when necessary"""
if isinstance(tag_range, int):
tag_range = [tag_range] * 2
elif len(tag_range) == 1:
tag_range = [tag_range[0]] * 2
return tag_range

def _get_tag_ranges(self, content: dict):
"""Get tag_ranges and check validity:
- It should be ordered
- Not unnecessary partition (eg. [[10,20],[20,30]])
- Singular intergers are changed to ranges (eg. [10] to [[10, 10]])
The ranges are understood as [inclusive, inclusive]"""
ranges = content["tag_ranges"]
if len(ranges) < 1:
detail = "tag_ranges is empty"
raise HTTPException(400, detail=detail)
last_tag = 0
ranges_n = len(ranges)
for i in range(0, ranges_n):
ranges[i] = self.map_singular_values(ranges[i])
if ranges[i][0] > ranges[i][1]:
detail = f"The range {ranges[i]} is not ordered"
raise HTTPException(400, detail=detail)
if last_tag and last_tag > ranges[i][0]:
detail = f"tag_ranges is not ordered. {last_tag}"\
f" is higher than {ranges[i][0]}"
raise HTTPException(400, detail=detail)
if last_tag and last_tag == ranges[i][0] - 1:
detail = f"tag_ranges has an unnecessary partition. "\
f"{last_tag} is before to {ranges[i][0]}"
raise HTTPException(400, detail=detail)
if last_tag and last_tag == ranges[i][0]:
detail = f"tag_ranges has repetition. {ranges[i-1]}"\
f" have same values as {ranges[i]}"
raise HTTPException(400, detail=detail)
last_tag = ranges[i][1]
if ranges[-1][1] > 4095:
detail = "Maximum value for tag_ranges is 4095"
raise HTTPException(400, detail=detail)
if ranges[0][0] < 1:
detail = "Minimum value for tag_ranges is 1"
raise HTTPException(400, detail=detail)
return ranges

@rest('v3/interfaces/{interface_id}/tag_ranges', methods=['POST'])
@validate_openapi(spec)
def set_tag_range(self, request: Request) -> JSONResponse:
"""Set tag range"""
content_type_json_or_415(request)
content = get_json_or_400(request, self.controller.loop)
tag_type = content.get("tag_type")
ranges = self._get_tag_ranges(content)
try:
ranges = get_tag_ranges(content["tag_ranges"])
except KytosInvalidRanges as err:
raise HTTPException(400, detail=err)
interface_id = request.path_params["interface_id"]
interface = self.controller.get_interface_by_id(interface_id)
if not interface:
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-e git+https://github.com/kytos-ng/python-openflow.git#egg=python-openflow
-e git+https://github.com/kytos-ng/kytos.git#egg=kytos[dev]
-e git+https://github.com/kytos-ng/kytos.git@epic/vlan_range#egg=kytos[dev]
viniarck marked this conversation as resolved.
Show resolved Hide resolved
-e .
52 changes: 0 additions & 52 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
KytosTagtypeNotSupported)
from kytos.core.interface import Interface
from kytos.core.link import Link
from kytos.core.rest_api import HTTPException
from kytos.core.switch import Switch
from kytos.lib.helpers import (get_interface_mock, get_link_mock,
get_controller_mock, get_switch_mock,
Expand Down Expand Up @@ -1630,57 +1629,6 @@ def test_interruption_end(
assert mock_notify_link_status_change.call_count == 2
mock_notify_topology_update.assert_called_once()

def test_map_singular_values(self):
"""Test map_singular_values"""
mock_tag = 201
result = self.napp.map_singular_values(mock_tag)
assert result == [201, 201]

mock_tag = [201]
result = self.napp.map_singular_values(mock_tag)
assert result == [201, 201]

def test_get_tag_ranges(self):
"""Test _get_tag_ranges"""
mock_content = {'tag_ranges': [100, [150], [200, 3000]]}
result = self.napp._get_tag_ranges(mock_content)
assert result == [[100, 100], [150, 150], [200, 3000]]

# Empty
mock_content = {'tag_ranges': []}
with pytest.raises(HTTPException):
self.napp._get_tag_ranges(mock_content)

# Range not ordered
mock_content = {'tag_ranges': [[20, 19]]}
with pytest.raises(HTTPException):
self.napp._get_tag_ranges(mock_content)

# Ranges not ordered
mock_content = {'tag_ranges': [[20, 50], [30, 3000]]}
with pytest.raises(HTTPException):
self.napp._get_tag_ranges(mock_content)

# Unnecessary partition
mock_content = {'tag_ranges': [[20, 50], [51, 3000]]}
with pytest.raises(HTTPException):
self.napp._get_tag_ranges(mock_content)

# Repeated tag
mock_content = {'tag_ranges': [[20, 50], [50, 3000]]}
with pytest.raises(HTTPException):
self.napp._get_tag_ranges(mock_content)

# Over 4095
mock_content = {'tag_ranges': [[20, 50], [50, 4096]]}
with pytest.raises(HTTPException):
self.napp._get_tag_ranges(mock_content)

# Under 1
mock_content = {'tag_ranges': [[0, 50], [50, 3000]]}
with pytest.raises(HTTPException):
self.napp._get_tag_ranges(mock_content)

async def test_set_tag_range(self, event_loop):
"""Test set_tag_range"""
self.napp.controller.loop = event_loop
Expand Down