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

Device component - IETF Slice:

- Simplified code and removed unneeded files
- Moved old code to separate folder
- Multiple bug fixes
parent b0d4c140
Loading
Loading
Loading
Loading
+0 −25
Original line number Diff line number Diff line
# Copyright 2022-2025 ETSI SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from device.service.driver_api._Driver import (
    RESOURCE_ENDPOINTS,
    RESOURCE_INTERFACES,
    RESOURCE_NETWORK_INSTANCES,
)

SPECIAL_RESOURCE_MAPPINGS = {
    RESOURCE_ENDPOINTS: "/endpoints",
    RESOURCE_INTERFACES: "/interfaces",
    RESOURCE_NETWORK_INSTANCES: "/net-instances",
}
+48 −108
Original line number Diff line number Diff line
@@ -13,16 +13,13 @@
# limitations under the License.


import anytree, json, logging, re, threading
import json, logging, re, threading
from typing import Any, Iterator, List, Optional, Tuple, Union
from common.method_wrappers.Decorator import MetricsPool, metered_subclass_method
from common.type_checkers.Checkers import chk_length, chk_string, chk_type
from common.type_checkers.Checkers import chk_string, chk_type
from device.service.driver_api._Driver import _Driver, RESOURCE_ENDPOINTS, RESOURCE_SERVICES
from device.service.driver_api.AnyTreeTools import TreeNode, dump_subtree, get_subnode, set_subnode_value
from device.service.driver_api.ImportTopologyEnum import ImportTopologyEnum, get_import_topology
from .Constants import SPECIAL_RESOURCE_MAPPINGS
from .TfsApiClient import TfsApiClient
from .Tools import compose_resource_endpoint


LOGGER = logging.getLogger(__name__)
@@ -34,8 +31,8 @@ ALL_RESOURCE_KEYS = [
]


RE_IETF_SLICE_DATA = re.compile(r'^\/service\[[^\]]+\]\/IETFSlice$')
RE_IETF_SLICE_OPERATION = re.compile(r'^\/service\[[^\]]+\]\/IETFSlice\/operation$')
RE_IETF_SLICE_DATA = re.compile(r'^\/service\[([^\]]+)\]\/IETFSlice$')


DRIVER_NAME = 'ietf_slice'
METRICS_POOL = MetricsPool('Device', 'Driver', labels={'driver': DRIVER_NAME})
@@ -47,7 +44,7 @@ class IetfSliceDriver(_Driver):
        self.__lock = threading.Lock()
        self.__started = threading.Event()
        self.__terminate = threading.Event()
        self.__running = TreeNode('.')

        username = self.settings.get('username')
        password = self.settings.get('password')
        scheme   = self.settings.get('scheme', 'http')
@@ -65,52 +62,6 @@ class IetfSliceDriver(_Driver):
        #                 (not supported by XR driver)
        self.__import_topology = get_import_topology(self.settings, default=ImportTopologyEnum.DEVICES)

        endpoints = self.settings.get("endpoints", [])
        endpoint_resources = []
        for endpoint in endpoints:
            endpoint_resource = compose_resource_endpoint(endpoint)
            if endpoint_resource is None:
                continue
            endpoint_resources.append(endpoint_resource)
        self._set_initial_config(endpoint_resources)

    def _set_initial_config(
        self, resources: List[Tuple[str, Any]]
    ) -> List[Union[bool, Exception]]:
        chk_type("resources", resources, list)
        if len(resources) == 0:
            return []
        results = []
        resolver = anytree.Resolver(pathattr="name")
        with self.__lock:
            for i, resource in enumerate(resources):
                str_resource_name = "resources[#{:d}]".format(i)
                try:
                    chk_type(str_resource_name, resource, (list, tuple))
                    chk_length(str_resource_name, resource, min_length=2, max_length=2)
                    resource_key, resource_value = resource
                    chk_string(str_resource_name, resource_key, allow_empty=False)
                    resource_path = resource_key.split("/")
                except Exception as e:
                    LOGGER.exception(
                        "Exception validating {:s}: {:s}".format(
                            str_resource_name, str(resource_key)
                        )
                    )
                    results.append(e)  # if validation fails, store the exception
                    continue

                try:
                    resource_value = json.loads(resource_value)
                except:  # pylint: disable=bare-except
                    pass

                set_subnode_value(
                    resolver, self.__running, resource_path, resource_value
                )

                results.append(True)
        return results

    def Connect(self) -> bool:
        with self.__lock:
@@ -119,16 +70,19 @@ class IetfSliceDriver(_Driver):
            if checked: self.__started.set()
            return checked


    def Disconnect(self) -> bool:
        with self.__lock:
            self.__terminate.set()
            return True


    @metered_subclass_method(METRICS_POOL)
    def GetInitialConfig(self) -> List[Tuple[str, Any]]:
        with self.__lock:
            return []


    @metered_subclass_method(METRICS_POOL)
    def GetConfig(
        self, resource_keys : List[str] = []
@@ -137,9 +91,8 @@ class IetfSliceDriver(_Driver):
        results = []
        with self.__lock:
            self.tac.check_credentials()
            if len(resource_keys) == 0:
                return dump_subtree(self.__running)
            resolver = anytree.Resolver(pathattr='name')
            if len(resource_keys) == 0: resource_keys = ALL_RESOURCE_KEYS

            for i, resource_key in enumerate(resource_keys):
                str_resource_name = 'resource_key[#{:d}]'.format(i)
                try:
@@ -147,23 +100,40 @@ class IetfSliceDriver(_Driver):
                    if resource_key == RESOURCE_ENDPOINTS:
                        # return endpoints through TFS NBI API and list-devices method
                        results.extend(self.tac.get_devices_endpoints(self.__import_topology))
                    else:
                        resource_key = SPECIAL_RESOURCE_MAPPINGS.get(
                            resource_key, resource_key
                    elif resource_key == RESOURCE_SERVICES:
                        slices_data = self.tac.list_slices()
                        slices_list = (
                            slices_data
                            .get('network-slice-services', dict())
                            .get('slice-service', list())
                        )
                        resource_path = resource_key.split('/')
                        resource_node = get_subnode(
                            resolver, self.__running, resource_path, default=None
                        for slice_data in slices_list:
                            slice_name = slice_data['id']
                            slice_resource_key = '/service[{:s}]/IETFSlice'.format(str(slice_name))
                            results.append((slice_resource_key, slice_data))
                    else:
                        match_slice_data = RE_IETF_SLICE_DATA.match(resource_key)
                        if match_slice_data is not None:
                            slice_name = match_slice_data.groups()[0]
                            slices_data = self.tac.retrieve_slice(slice_name)
                            slices_list = (
                                slices_data
                                .get('network-slice-services', dict())
                                .get('slice-service', list())
                            )
                        # if not found, resource_node is None
                        if resource_node is None: continue
                        results.extend(dump_subtree(resource_node))
                            for slice_data in slices_list:
                                slice_name = slice_data['id']
                                slice_resource_key = '/service[{:s}]/IETFSlice'.format(str(slice_name))
                                results.append((slice_resource_key, slice_data))
                        else:
                            results.append((resource_key, None))
                except Exception as e:
                    MSG = 'Unhandled error processing {:s}: resource_key({:s})'
                    LOGGER.exception(MSG.format(str_resource_name, str(resource_key)))
                    results.append((resource_key, e))
        return results


    @metered_subclass_method(METRICS_POOL)
    def SetConfig(
        self, resources : List[Tuple[str, Any]]
@@ -171,46 +141,16 @@ class IetfSliceDriver(_Driver):
        results = []
        if len(resources) == 0: return results
        with self.__lock:
            for resource in resources:
                resource_key, resource_value = resource
                if RE_IETF_SLICE_OPERATION.match(resource_key):
                    operation_type = json.loads(resource_value)['type']
                    results.append((resource_key, True))
                    break
            else:
                raise Exception('operation type not found in resources')

            for i, resource in enumerate(resources):
                str_resource_name = 'resource_key[#{:d}]'.format(i)
                LOGGER.info('resource = {:s}'.format(str(resource)))
                resource_key, resource_value = resource
                if not RE_IETF_SLICE_DATA.match(resource_key):
                    continue
                try:
                    resource_value = json.loads(resource_value)

                    slice_data = resource_value['network-slice-services'][
                        'slice-service'
                    ][0]
                    slice_name = slice_data['id']
                if not RE_IETF_SLICE_DATA.match(resource_key): continue

                    if operation_type == 'create':
                try:
                    resource_value = json.loads(resource_value)
                    self.tac.create_slice(resource_value)
                    elif operation_type == 'update':
                        connection_groups = slice_data['connection-groups']['connection-group']
                        if len(connection_groups) != 1:
                            MSG = 'Exactly one ConnectionGroup({:s}) is supported'
                            raise Exception(MSG.format(str(connection_groups)))
                        connection_group = connection_groups[0]
                        self.tac.update_slice(
                            slice_name, connection_group['id'], connection_group
                        )
                    elif operation_type == 'delete':
                        self.tac.delete_slice(slice_name)
                    else:
                        MSG = 'OperationType({:s}) not supported'
                        raise Exception(MSG.format(str(operation_type)))

                    results.append((resource_key, True))
                except Exception as e:
                    MSG = 'Unhandled error processing {:s}: resource_key({:s})'
@@ -218,27 +158,24 @@ class IetfSliceDriver(_Driver):
                    results.append((resource_key, e))
        return results


    @metered_subclass_method(METRICS_POOL)
    def DeleteConfig(
        self, resources : List[Tuple[str, Any]]
    ) -> List[Union[bool, Exception]]:
        results = []
        if len(resources) == 0:
            return results
        if len(resources) == 0: return results
        with self.__lock:
            for i, resource in enumerate(resources):
                str_resource_name = 'resource_key[#{:d}]'.format(i)
                LOGGER.info('resource = {:s}'.format(str(resource)))
                resource_key, resource_value = resource

                if not RE_IETF_SLICE_DATA.match(resource_key):
                    continue
                if not RE_IETF_SLICE_DATA.match(resource_key): continue

                try:
                    resource_value = json.loads(resource_value)
                    slice_name = resource_value['network-slice-services'][
                        'slice-service'
                    ][0]['id']
                    slice_name = resource_value['network-slice-services']['slice-service'][0]['id']
                    self.tac.delete_slice(slice_name)
                    results.append((resource_key, True))
                except Exception as e:
@@ -247,6 +184,7 @@ class IetfSliceDriver(_Driver):
                    results.append((resource_key, e))
        return results


    @metered_subclass_method(METRICS_POOL)
    def SubscribeState(
        self, subscriptions : List[Tuple[str, float, float]]
@@ -254,6 +192,7 @@ class IetfSliceDriver(_Driver):
        # TODO: does not support monitoring by now
        return [False for _ in subscriptions]


    @metered_subclass_method(METRICS_POOL)
    def UnsubscribeState(
        self, subscriptions : List[Tuple[str, float, float]]
@@ -261,6 +200,7 @@ class IetfSliceDriver(_Driver):
        # TODO: does not support monitoring by now
        return [False for _ in subscriptions]


    def GetState(
        self, blocking=False, terminate : Optional[threading.Event] = None
    ) -> Iterator[Tuple[float, str, Any]]:
+23 −0
Original line number Diff line number Diff line
@@ -180,6 +180,29 @@ class TfsApiClient(RestApiClient):
            raise Exception(MSG) from e


    def list_slices(self) -> Dict:
        try:
            MSG = '[list_slices] GET {:s}'
            LOGGER.info(MSG.format(str(IETF_SLICE_ALL_URL)))
            return self.get(IETF_SLICE_ALL_URL)
        except requests.exceptions.ConnectionError as e:
            MSG = 'Failed to send GET request to TFS IETF Slice NBI'
            raise Exception(MSG) from e


    def retrieve_slice(self, slice_name : str) -> Dict:
        MSG = '[retrieve_slice] slice_name={:s}'
        LOGGER.debug(MSG.format(str(slice_name)))
        url = IETF_SLICE_ONE_URL.format(slice_name)
        try:
            MSG = '[retrieve_slice] GET {:s}'
            LOGGER.info(MSG.format(str(url)))
            return self.get(url)
        except requests.exceptions.ConnectionError as e:
            MSG = 'Failed to send GET request to TFS IETF Slice NBI'
            raise Exception(MSG) from e


    def update_slice(
        self, slice_name : str, connection_group_id : str,
        updated_connection_group_data : Dict
+268 −0

File added.

Preview size limit exceeded, changes collapsed.

+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ from common.proto.kpi_sample_types_pb2 import KpiSampleType
from common.type_checkers.Checkers import chk_attribute, chk_string, chk_type
from device.service.driver_api._Driver import RESOURCE_ENDPOINTS

from .Constants import SPECIAL_RESOURCE_MAPPINGS
from ..Constants import SPECIAL_RESOURCE_MAPPINGS

LOGGER = logging.getLogger(__name__)