Commit a1256eeb authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

NBI component - TFS-API connector:

- Add error handling for gRPC calls in Services and OpticalServiceAllocation classes
parent 7a9de4ed
Loading
Loading
Loading
Loading
+17 −5
Original line number Diff line number Diff line
@@ -240,7 +240,10 @@ class Services(_Resource):

class Service(_Resource):
    def get(self, context_uuid : str, service_uuid : str):
        try:
            return format_grpc_to_json(self.context_client.GetService(grpc_service_id(context_uuid, service_uuid)))
        except grpc.RpcError as exc:
            return _format_grpc_error(exc)

    def put(self, context_uuid : str, service_uuid : str):
        service = request.get_json()
@@ -249,10 +252,16 @@ class Service(_Resource):
        if service_uuid != service['service_id']['service_uuid']['uuid']:
            raise BadRequest('Mismatching service_uuid')
        svc = format_service_custom_config_rules(service)
        try:
            return format_grpc_to_json(self.service_client.UpdateService(grpc_service(svc)))
        except grpc.RpcError as exc:
            return _format_grpc_error(exc)

    def delete(self, context_uuid : str, service_uuid : str):
        try:
            return format_grpc_to_json(self.service_client.DeleteService(grpc_service_id(context_uuid, service_uuid)))
        except grpc.RpcError as exc:
            return _format_grpc_error(exc)

def _decode_custom_value(value):
    if not isinstance(value, str):
@@ -288,8 +297,11 @@ def _slot_range(slots):
class OpticalServiceAllocation(_Resource):
    def get(self, context_uuid : str, service_uuid : str):
        service_id = grpc_service_id(context_uuid, service_uuid)
        try:
            service = grpc_message_to_json(self.context_client.GetService(service_id))
            connections = grpc_message_to_json(self.context_client.ListConnections(service_id))
        except grpc.RpcError as exc:
            return _format_grpc_error(exc)
        settings = _service_custom_settings(service)
        allocation_settings = settings.get('/settings')
        if not isinstance(allocation_settings, dict):
+23 −1
Original line number Diff line number Diff line
@@ -12,7 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from nbi.service.tfs_api.Resources import _service_custom_settings, _slot_range
import grpc

from nbi.service.tfs_api.Resources import _format_grpc_error, _service_custom_settings, _slot_range


class _RpcError(grpc.RpcError):
    def __init__(self, code, details):
        super().__init__()
        self._code = code
        self._details = details

    def code(self):
        return self._code

    def details(self):
        return self._details


def test_slot_range_compacts_contiguous_slots():
@@ -38,3 +53,10 @@ def test_service_custom_settings_decodes_json_values():
    settings = _service_custom_settings(service)

    assert settings['/settings'] == {'flow_id': 1, 'slots': [51, 52]}


def test_grpc_not_found_maps_to_http_404():
    body, status = _format_grpc_error(_RpcError(grpc.StatusCode.NOT_FOUND, 'service not found'))

    assert status == 404
    assert body['error'] == 'NOT_FOUND'