Commit 7a9de4ed authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Service component:

- Add error handling for optical spectrum reservation conflicts
- Updated existing functions to utilize the new error handling for lightpath additions.
- Added a test case to verify that a reservation conflict raises the appropriate exception.
parent b1326b64
Loading
Loading
Loading
Loading
+21 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@
import functools, json, logging, requests, uuid
from typing import Dict, List, Optional, Tuple
from urllib.parse import urlencode
from common.method_wrappers.ServiceExceptions import NotFoundException, OperationFailedException
from common.method_wrappers.ServiceExceptions import AlreadyExistsException, NotFoundException, OperationFailedException
from common.proto.context_pb2 import(
    ConfigActionEnum, ConfigRule, ConfigRule_Custom, Connection, ContextId,
    Device, DeviceId, Empty, EndPointId, OpticalBand, OpticalBandId, OpticalBandList,
@@ -88,6 +88,20 @@ def get_optical_controller_base_url() -> str:
    return base_url


def _raise_for_opticalcontroller_error(response, operation: str, spectrum_reservation: Optional[Dict] = None) -> None:
    status_code = getattr(response, 'status_code', 200)
    response_text = getattr(response, 'text', '')
    if status_code == 409:
        raise AlreadyExistsException(
            'OpticalSpectrumReservation', str(spectrum_reservation), extra_details=response_text
        )
    if status_code >= 400:
        raise OperationFailedException(
            operation,
            extra_details='HTTP {:d}: {:s}'.format(status_code, response_text)
        )


def get_uuids_from_names(
    devices : List[Device], device_name : str, port_name : str
) -> Tuple[str, str]:
@@ -183,6 +197,9 @@ def add_flex_lightpath(
            urlx = '{:s}?{:s}'.format(urlx, urlencode(query))
        r = requests.put(urlx, headers=headers)
        LOGGER.debug(f"addpathlight {r}")
        _raise_for_opticalcontroller_error(
            r, 'optical-controller-add-flex-lightpath', spectrum_reservation=spectrum_reservation
        )
        reply = r.text 
        return reply
    else:
@@ -221,6 +238,9 @@ def add_lightpath(src, dst, bitrate, bidir, spectrum_reservation: Optional[Dict]
            urlx = '{:s}?{:s}'.format(urlx, urlencode(query))
        r = requests.put(urlx, headers=headers)
        LOGGER.debug(f"addpathlight {r}")
        _raise_for_opticalcontroller_error(
            r, 'optical-controller-add-lightpath', spectrum_reservation=spectrum_reservation
        )
        reply = r.text 
        return reply
    else:
+25 −0
Original line number Diff line number Diff line
@@ -12,6 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

from common.method_wrappers.ServiceExceptions import AlreadyExistsException
from common.proto.context_pb2 import Constraint, Constraint_Custom
from service.service.tools.OpticalSpectrumReservation import parse_optical_spectrum_reservation_constraints
from service.service.tools import OpticalTools
@@ -101,3 +104,25 @@ def test_add_lightpath_forwards_reservation_query(monkeypatch):
    assert "reservation_band=c_slots" in captured["url"]
    assert "reservation_start=51" in captured["url"]
    assert "reservation_end=66" in captured["url"]


def test_add_lightpath_maps_reservation_conflict_to_already_exists(monkeypatch):
    class Response:
        status_code = 409
        text = "Requested optical spectrum is unavailable"

    def put(url, headers=None):
        return Response()

    monkeypatch.setattr(OpticalTools, "get_optical_controller_base_url", lambda: "http://optical/OpticalTFS")
    monkeypatch.setattr(OpticalTools.requests, "put", put)
    monkeypatch.setattr(OpticalTools, "TESTING", False)

    with pytest.raises(AlreadyExistsException):
        OpticalTools.add_lightpath(
            "T1",
            "T2",
            100,
            0,
            spectrum_reservation={"band": "c_slots", "n_start": 51, "n_end": 66},
        )