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

Device - IETF ACTN Driver:

- Completed implementation of GetConfig, SetConfig and DeleteConfig methods, and related helper methods
parent bc3954ad
Loading
Loading
Loading
Loading
+31 −10
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ from device.service.driver_api._Driver import _Driver, RESOURCE_SERVICES
from .handlers.EthtServiceHandler import EthtServiceHandler
from .handlers.OsuTunnelHandler import OsuTunnelHandler
from .handlers.RestApiClient import RestApiClient
from .Tools import get_etht_services, get_osu_tunnels, parse_resource_key

LOGGER = logging.getLogger(__name__)

@@ -73,10 +74,18 @@ class IetfActnDriver(_Driver):
            if len(resource_keys) == 0: resource_keys = ALL_RESOURCE_KEYS
            for i, resource_key in enumerate(resource_keys):
                chk_string('resource_key[#{:d}]'.format(i), resource_key, allow_empty=False)
                etht_service = self._handler_etht_service.get(etht_service_name)
                osu_tunnel = self._handler_osu_tunnel.get(osu_tunnel_name)
                service_data = {}
                results.extend(('/service/service[...]', service_data))

                if resource_key == RESOURCE_SERVICES:
                    get_osu_tunnels(self._handler_osu_tunnel, results)
                    get_etht_services(self._handler_etht_service, results)
                else:
                    # check if resource key is for a specific OSU tunnel or ETHT service, and get them accordingly
                    osu_tunnel_name, etht_service_name = parse_resource_key(resource_key)
                    if osu_tunnel_name is not None:
                        get_osu_tunnels(self._handler_osu_tunnel, results, osu_tunnel_name=osu_tunnel_name)
                    if etht_service_name is not None:
                        get_etht_services(self._handler_etht_service, results, etht_service_name=etht_service_name)

        return results

    @metered_subclass_method(METRICS_POOL)
@@ -87,10 +96,16 @@ class IetfActnDriver(_Driver):
            for resource_key, resource_value in resources:
                LOGGER.info('resource: key({:s}) => value({:s})'.format(str(resource_key), str(resource_value)))
                if isinstance(resource_value, str): resource_value = json.loads(resource_value)
                osu_tunnel_name, etht_service_name = parse_resource_key(resource_key)

                if osu_tunnel_name is not None:
                    succeeded = self._handler_osu_tunnel.update(resource_value)
                if succeeded:
                    results.extend(succeeded)

                if etht_service_name is not None:
                    succeeded = self._handler_etht_service.update(resource_value)
                    results.extend(succeeded)

        return results

    @metered_subclass_method(METRICS_POOL)
@@ -101,10 +116,16 @@ class IetfActnDriver(_Driver):
            for resource_key, resource_value in resources:
                LOGGER.info('resource: key({:s}) => value({:s})'.format(str(resource_key), str(resource_value)))
                if isinstance(resource_value, str): resource_value = json.loads(resource_value)
                succeeded = self._handler_etht_service.delete(etht_service_name)
                if succeeded:
                osu_tunnel_name, etht_service_name = parse_resource_key(resource_key)

                if osu_tunnel_name is not None:
                    succeeded = self._handler_osu_tunnel.delete(osu_tunnel_name)
                    results.extend(succeeded)

                if etht_service_name is not None:
                    succeeded = self._handler_etht_service.delete(etht_service_name)
                    results.extend(succeeded)

        return results

    @metered_subclass_method(METRICS_POOL)
+35 −162
Original line number Diff line number Diff line
@@ -12,168 +12,41 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json, logging, operator, requests
from requests.auth import HTTPBasicAuth
from typing import Dict, Optional
from device.service.driver_api._Driver import RESOURCE_ENDPOINTS, RESOURCE_SERVICES
import logging, re
from typing import Any, List, Optional, Tuple, Union
from .handlers.EthtServiceHandler import EthtServiceHandler
from .handlers.OsuTunnelHandler import OsuTunnelHandler

LOGGER = logging.getLogger(__name__)

HTTP_OK_CODES = {
    200,    # OK
    201,    # Created
    202,    # Accepted
    204,    # No Content
}

def find_key(resource, key):
    return json.loads(resource[1])[key]

def get_resource(
    base_url : str, resource_key : str,
    auth : Optional[HTTPBasicAuth] = None, timeout : Optional[int] = None
):
    url = '{:s}/restconf/data/tapi-common:context'.format(base_url)
    result = []
    try:
        response = requests.get(url, timeout=timeout, verify=False, auth=auth)
    except requests.exceptions.Timeout:
        LOGGER.exception('Timeout connecting {:s}'.format(url))
        return result
    except Exception as e:  # pylint: disable=broad-except
        LOGGER.exception('Exception retrieving {:s}'.format(resource_key))
        result.append((resource_key, e))
        return result

    try:
        context = json.loads(response.content)
    except Exception as e:  # pylint: disable=broad-except
        LOGGER.warning('Unable to decode reply: {:s}'.format(str(response.content)))
        result.append((resource_key, e))
        return result

    if resource_key == RESOURCE_ENDPOINTS:
        if 'tapi-common:context' in context:
            context = context['tapi-common:context']
        elif 'context' in context:
            context = context['context']

        for sip in context['service-interface-point']:
            layer_protocol_name = sip.get('layer-protocol-name', '?')
            supportable_spectrum = sip.get('tapi-photonic-media:media-channel-service-interface-point-spec', {})
            supportable_spectrum = supportable_spectrum.get('mc-pool', {})
            supportable_spectrum = supportable_spectrum.get('supportable-spectrum', [])
            supportable_spectrum = supportable_spectrum[0] if len(supportable_spectrum) == 1 else {}
            grid_type = supportable_spectrum.get('frequency-constraint', {}).get('grid-type')
            granularity = supportable_spectrum.get('frequency-constraint', {}).get('adjustment-granularity')
            direction = sip.get('direction', '?')

            endpoint_type = [layer_protocol_name, grid_type, granularity, direction]
            str_endpoint_type = ':'.join(filter(lambda i: operator.is_not(i, None), endpoint_type))
            sip_uuid = sip['uuid']

            sip_names = sip.get('name', [])
            sip_name = next(iter([
                sip_name['value']
                for sip_name in sip_names
                if sip_name['value-name'] == 'local-name'
            ]), sip_uuid)

            endpoint_url = '/endpoints/endpoint[{:s}]'.format(sip_uuid)
            endpoint_data = {'uuid': sip_uuid, 'name': sip_name, 'type': str_endpoint_type}
            result.append((endpoint_url, endpoint_data))

    elif resource_key == RESOURCE_SERVICES:
        if 'tapi-common:context' in context:
            context = context['tapi-common:context']
        elif 'context' in context:
            context = context['context']

        if 'tapi-connectivity:connectivity-context' in context:
            context = context['tapi-connectivity:connectivity-context']
        elif 'connectivity-context' in context:
            context = context['connectivity-context']

        for conn_svc in context['connectivity-service']:
            service_uuid = conn_svc['uuid']
            constraints = conn_svc.get('connectivity-constraint', {})
            total_req_cap = constraints.get('requested-capacity', {}).get('total-size', {})

            service_url = '/services/service[{:s}]'.format(service_uuid)
            service_data = {
                'uuid': service_uuid,
                'direction': constraints.get('connectivity-direction', 'UNIDIRECTIONAL'),
                'capacity_unit': total_req_cap.get('unit', '<UNDEFINED>'),
                'capacity_value': total_req_cap.get('value', '<UNDEFINED>'),
            }

            for i,endpoint in enumerate(conn_svc.get('end-point', [])):
                layer_protocol_name = endpoint.get('layer-protocol-name')
                if layer_protocol_name is not None:
                    service_data['layer_protocol_name'] = layer_protocol_name

                layer_protocol_qualifier = endpoint.get('layer-protocol-qualifier')
                if layer_protocol_qualifier is not None:
                    service_data['layer_protocol_qualifier'] = layer_protocol_qualifier

                sip = endpoint['service-interface-point']['service-interface-point-uuid']
                service_data['input_sip' if i == 0 else 'output_sip'] = sip

            result.append((service_url, service_data))

    return result

def create_resource(
    base_url : str, resource_key : str, resource_value : Dict,
    auth : Optional[HTTPBasicAuth] = None, timeout : Optional[int] = None
):

    uuid = find_key(resource, 'uuid')
    input_sip = find_key(resource, 'input_sip_uuid')
    output_sip = find_key(resource, 'output_sip_uuid')
    capacity_value = find_key(resource, 'capacity_value')
    capacity_unit = find_key(resource, 'capacity_unit')
    layer_protocol_name = find_key(resource, 'layer_protocol_name')
    layer_protocol_qualifier = find_key(resource, 'layer_protocol_qualifier')
    direction = find_key(resource, 'direction')


    url = '{:s}/restconf/data/tapi-common:context/tapi-connectivity:connectivity-context'.format(base_url)
    headers = {'content-type': 'application/json'}
    data = compose_...
    results = []
    try:
        LOGGER.info('Connectivity service {:s}: {:s}'.format(str(uuid), str(data)))
        response = requests.post(
            url=url, data=json.dumps(data), timeout=timeout, headers=headers, verify=False, auth=auth)
        LOGGER.info('TAPI response: {:s}'.format(str(response)))
    except Exception as e:  # pylint: disable=broad-except
        LOGGER.exception('Exception creating ConnectivityService(uuid={:s}, data={:s})'.format(str(uuid), str(data)))
        results.append(e)
    else:
        if response.status_code not in HTTP_OK_CODES:
            msg = 'Could not create ConnectivityService(uuid={:s}, data={:s}). status_code={:s} reply={:s}'
            LOGGER.error(msg.format(str(uuid), str(data), str(response.status_code), str(response)))
        results.append(response.status_code in HTTP_OK_CODES)
    return results

def delete_resource(
    base_url : str, resource_key : str, resource_value : Dict,
    auth : Optional[HTTPBasicAuth] = None, timeout : Optional[int] = None
):
    uuid = find_key(resource_value, 'uuid')

    url = '{:s}/tapi-common:context/tapi-connectivity:connectivity-context/connectivity-service={:s}'
    url = url.format(base_url, uuid)
    results = []
    try:
        response = requests.delete(url=url, timeout=timeout, verify=False, auth=auth)
    except Exception as e:  # pylint: disable=broad-except
        LOGGER.exception('Exception deleting ConnectivityService(uuid={:s})'.format(str(uuid)))
        results.append(e)
    else:
        if response.status_code not in HTTP_OK_CODES:
            msg = 'Could not delete ConnectivityService(uuid={:s}). status_code={:s} reply={:s}'
            LOGGER.error(msg.format(str(uuid), str(response.status_code), str(response)))
        results.append(response.status_code in HTTP_OK_CODES)
    return results
RE_OSU_TUNNEL   = re.compile(r'^\/osu\_tunnels\/osu\_tunnel\[([^\]]+)\]$')
RE_ETHT_SERVICE = re.compile(r'^\/etht\_services\/etht\_service\[([^\]]+)\]$')

def parse_resource_key(resource_key : str) -> Tuple[Optional[str], Optional[str]]:
    re_match_osu_tunnel   = RE_OSU_TUNNEL.match(resource_key)
    osu_tunnel_name = None if re_match_osu_tunnel is None else re_match_osu_tunnel.group(1)

    re_match_etht_service = RE_ETHT_SERVICE.match(resource_key)
    etht_service_name = None if re_match_etht_service is None else re_match_etht_service.group(1)

    return osu_tunnel_name, etht_service_name

def get_osu_tunnels(
    handler_osu_tunnel : OsuTunnelHandler, results : List[Tuple[str, Union[Any, None, Exception]]],
    osu_tunnel_name : Optional[str] = None
) -> None:
    osu_tunnels = handler_osu_tunnel.get(osu_tunnel_name=osu_tunnel_name)
    for osu_tunnel in osu_tunnels:
        osu_tunnel_name = osu_tunnel['name']
        resource_key = '/osu_tunnels/osu_tunnel[{:s}]'.format(osu_tunnel_name)
        results.extend((resource_key, osu_tunnel))

def get_etht_services(
    handler_etht_service : EthtServiceHandler, results : List[Tuple[str, Union[Any, None, Exception]]],
    etht_service_name : Optional[str] = None
) -> None:
    etht_services = handler_etht_service.get(etht_service_name=etht_service_name)
    for etht_service in etht_services:
        etht_service_name = etht_service['name']
        resource_key = '/etht_services/etht_service[{:s}]'.format(etht_service_name)
        results.extend((resource_key, etht_service))