Skip to content

Commit

Permalink
Updated exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alopalao committed Nov 9, 2023
1 parent ccac2a2 commit edfca44
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class TAGDoc(BaseModel):
"""TAG model"""
tag_type: str
value: Union[int, str, list[list[int]]]
mask_list: Optional[list[str, int]]

@validator('value')
def validate_value(cls, value):
Expand Down
30 changes: 23 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pydantic import ValidationError

from kytos.core import KytosNApp, log, rest
from kytos.core.exceptions import KytosInvalidTagRanges, KytosTagsAreNotAvailable, KytosTagError
from kytos.core.exceptions import KytosTagError
from kytos.core.helpers import (alisten_to, listen_to, load_spec,
validate_openapi)
from kytos.core.interface import TAG, UNI, TAGRange
Expand Down Expand Up @@ -273,6 +273,11 @@ def create_circuit(self, request: Request) -> JSONResponse:
detail=f"backup_path is not valid: {exception}"
) from exception

if self._is_duplicated_evc(evc):
result = "The EVC already exists."
log.debug("create_circuit result %s %s", result, 409)
raise HTTPException(409, detail=result)

if not evc._tag_lists_equal():
detail = "UNI_A and UNI_Z tag lists should be the same."
raise HTTPException(400, detail=detail)
Expand All @@ -284,7 +289,7 @@ def create_circuit(self, request: Request) -> JSONResponse:

try:
self._use_uni_tags(evc)
except KytosTagsAreNotAvailable as exception:
except KytosTagError as exception:
raise HTTPException(400, detail=str(exception)) from exception

# save circuit
Expand Down Expand Up @@ -319,7 +324,7 @@ def _use_uni_tags(evc):
try:
uni_z = evc.uni_z
evc._use_uni_vlan(uni_z)
except KytosTagsAreNotAvailable as err:
except KytosTagError as err:
evc.make_uni_vlan_available(uni_a)
raise err

Expand Down Expand Up @@ -370,9 +375,6 @@ def update(self, request: Request) -> JSONResponse:
except ValueError as exception:
log.debug("update result %s %s", exception, 400)
raise HTTPException(400, detail=str(exception)) from exception
except KytosTagsAreNotAvailable as exception:
log.debug("update result %s %s", exception, 400)
raise HTTPException(400, detail=str(exception)) from exception
except DisabledSwitch as exception:
log.debug("update result %s %s", exception, 409)
raise HTTPException(
Expand Down Expand Up @@ -708,6 +710,21 @@ def delete_schedule(self, request: Request) -> JSONResponse:
log.debug("delete_schedule result %s %s", result, status)
return JSONResponse(result, status_code=status)

def _is_duplicated_evc(self, evc):
"""Verify if the circuit given is duplicated with the stored evcs.
Args:
evc (EVC): circuit to be analysed.
Returns:
boolean: True if the circuit is duplicated, otherwise False.
"""
for circuit in tuple(self.circuits.values()):
if not circuit.archived and circuit.shares_uni(evc):
return True
return False

@listen_to("kytos/topology.link_up")
def on_link_up(self, event):
"""Change circuit when link is up or end_maintenance."""
Expand Down Expand Up @@ -987,7 +1004,6 @@ def _uni_from_dict(self, uni_dict):
else:
tag = None
uni = UNI(interface, tag)

return uni

def _link_from_dict(self, link_dict):
Expand Down
15 changes: 6 additions & 9 deletions models/evc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
from kytos.core import log
from kytos.core.common import EntityStatus, GenericEntity
from kytos.core.exceptions import (KytosNoTagAvailableError,
KytosTagsAreNotAvailable,
KytosTagsNotInTagRanges)
KytosTagError)
from kytos.core.helpers import get_time, now
from kytos.core.interface import UNI, Interface, TAGRange
from kytos.core.link import Link
Expand Down Expand Up @@ -190,16 +189,14 @@ def _get_unis_use_tags(self, **kwargs) -> tuple[UNI, UNI]:
try:
self._use_uni_vlan(uni_z, uni_dif=self.uni_z)
self.make_uni_vlan_available(self.uni_z, uni_dif=uni_z)
except KytosTagsAreNotAvailable as err:
except KytosTagError as err:
if uni_a_flag:
# self.uni_a - uni_a
self.make_uni_vlan_available(uni_a, uni_dif=self.uni_a)
raise err
else:
uni_z = self.uni_z

if uni_a_flag:
# self.uni_a - uni_a
self.make_uni_vlan_available(self.uni_a, uni_dif=uni_a)
else:
uni_a = self.uni_a
Expand Down Expand Up @@ -482,7 +479,7 @@ def make_uni_vlan_available(
conflict = uni.interface.make_tags_available(
self._controller, tag, tag_type
)
except KytosTagsNotInTagRanges as err:
except KytosTagError as err:
log.error(f"Error in circuit {self._id}: {err}")
if conflict:
intf = uni.interface.id
Expand Down Expand Up @@ -699,7 +696,7 @@ def remove_failover_flows(self, exclude_uni_switches=True,
)
try:
self.failover_path.make_vlans_available(self._controller)
except KytosTagsNotInTagRanges as err:
except KytosTagError as err:
log.error(f"Error when removing failover flows: {err}")
self.failover_path = Path([])
if sync:
Expand Down Expand Up @@ -732,7 +729,7 @@ def remove_current_flows(self, current_path=None, force=True):
)
try:
current_path.make_vlans_available(self._controller)
except KytosTagsNotInTagRanges as err:
except KytosTagError as err:
log.error(f"Error when removing current path flows: {err}")
self.current_path = Path([])
self.deactivate()
Expand Down Expand Up @@ -789,7 +786,7 @@ def remove_path_flows(self, path=None, force=True):
)
try:
path.make_vlans_available(self._controller)
except KytosTagsNotInTagRanges as err:
except KytosTagError as err:
log.error(f"Error when removing path flows: {err}")

@staticmethod
Expand Down

0 comments on commit edfca44

Please sign in to comment.