Commit 9d6230c1 authored by Pablo Armingol's avatar Pablo Armingol
Browse files

Refactor service handling and logging: streamline TAPI LSP processing, enhance...

Refactor service handling and logging: streamline TAPI LSP processing, enhance endpoint handling, and improve logging for better traceability
parent 2674550d
Loading
Loading
Loading
Loading
+12 −10
Original line number Diff line number Diff line
@@ -572,6 +572,7 @@ class MockServicerImpl_Context(ContextServiceServicer):
        for container_name in sorted(container_entry_uuids.keys()):
            entry_uuids = container_entry_uuids[container_name]
            for service in self.obj_db.select_entries(container_name, entry_uuids):
                LOGGER.debug('service={:s}'.format(grpc_message_to_json_string(service)))
                reply_service = Service()
                reply_service.CopyFrom(service)
                if exclude_endpoint_ids: del reply_service.service_endpoint_ids[:] # pylint: disable=no-member
@@ -579,6 +580,7 @@ class MockServicerImpl_Context(ContextServiceServicer):
                if exclude_config_rules: del reply_service.service_config.config_rules[:] # pylint: disable=no-member
                services.append(reply_service)


        reply = ServiceList(services=services)
        LOGGER.debug('[SelectService] reply={:s}'.format(grpc_message_to_json_string(reply)))
        return reply
+5 −2
Original line number Diff line number Diff line
@@ -47,6 +47,9 @@ def compose_resource_endpoint(endpoint_data : Dict[str, Any]) -> Optional[Tuple[
        process_optional_string_field(endpoint_data, 'context_uuid', endpoint_resource_value)
        process_optional_string_field(endpoint_data, 'topology_uuid', endpoint_resource_value)

        if 'name' not in endpoint_resource_value:
            endpoint_resource_value['name'] = endpoint_uuid

        # Check endpoint sample types (optional)
        endpoint_sample_types = chk_attribute('sample_types', endpoint_data, 'endpoint_data', default=[])
        chk_type('endpoint_data.sample_types', endpoint_sample_types, list)
+1 −1
Original line number Diff line number Diff line
@@ -338,7 +338,7 @@ def tapi_tequest(resource_value):
        executor.submit(process_uuid, service, url)

def build_tapi_connectivity(service_data):
    logging.info(f"Building TAPI connectivity for services data: {service_data}")
    LOGGER.info(f"Building TAPI connectivity for services data: {service_data}")
    outputs = []
    urls = []
    rules_set = service_data[1]["rule_set"]
+96 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@ import json, logging, operator, requests
from requests.auth import HTTPBasicAuth
from typing import Optional
from device.service.driver_api._Driver import RESOURCE_ENDPOINTS, RESOURCE_SERVICES
from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor()

LOGGER = logging.getLogger(__name__)

@@ -196,3 +198,97 @@ def delete_connectivity_service(root_url, uuid, auth : Optional[HTTPBasicAuth] =
            LOGGER.error(msg.format(str(uuid), str(response.status_code), str(response)))
        results.append(response.status_code in HTTP_OK_CODES)
    return results

def tapi_tequest(resource_value):

    services, urls  =  build_tapi_connectivity(resource_value)
    for service, url in zip(services, urls):
        LOGGER.info(f"Services to be processed: {json.dumps(service, indent=2)}")
        LOGGER.info(f"URL to be used: {url}")

        executor.submit(process_uuid, service, url)

def process_uuid(service, url):
    headers = {
    "Content-Type": "application/json",
    "Accept": "application/json"
    }

    LOGGER.info(f"Processing service {service} to URL: {url}")
    try:
        response = 200
        # requests.post(url = url, json=service, headers=headers, timeout=300)
        LOGGER.info("Response Status code: %s", response)
    except requests.exceptions.RequestException as e:
        LOGGER.info(f"ERROR equest to {service} failed: {e}")

def build_tapi_connectivity(service_data):
    LOGGER.info(f"Building TAPI connectivity for services data: {service_data}")
    outputs = []
    urls = []
    rules_set = service_data[1]["rule_set"]
    for rule_set in rules_set:
        direction = rule_set["direction"]
        layer_name = rule_set["layer_protocol_name"]
        layer_qualifier = rule_set["layer_protocol_qualifier"]
        urls.append(rule_set["url"])
        cs = {
            "uuid": rule_set["uuid"],
            "connectivity-direction": direction,
            "layer-protocol-name": layer_name,
            "layer-protocol-qualifier": layer_qualifier,
            "end-point": []
        }

        endpoints = []
        if direction == "UNIDIRECTIONAL":
            endpoints.append({
                "layer-protocol-name": layer_name,
                "layer-protocol-qualifier": layer_qualifier,
                "service-interface-point": {"service-interface-point-uuid": rule_set["input_sip"]},
                "direction:": "INPUT",
                "local-id": rule_set["input_sip"]
            })
            endpoints.append({
                "layer-protocol-name": layer_name,
                "layer-protocol-qualifier": layer_qualifier,
                "service-interface-point": {"service-interface-point-uuid": rule_set["output_sip"]},
                "direction:": "OUTPUT",
                "local-id": rule_set["output_sip"]
            })
        else:
            for point in [rule_set["input_sip"], rule_set["output_sip"]]:
                ep = {
                    "layer-protocol-name": layer_name,
                    "layer-protocol-qualifier": layer_qualifier,
                    "service-interface-point": {"service-interface-point-uuid": point},
                    "direction:": "BIDIRECTIONAL",
                    "local-id": point
                }
                if rule_set.get("lower_frequency_mhz") != "NONE":
                    ep["tapi-photonic-media:media-channel-connectivity-service-end-point-spec"] = {
                        "mc-config": {
                            "spectrum": {
                                "frequency-constraint": {
                                    "adjustment-granularity": rule_set.get("granularity", "NONE"),
                                    "grid-type": rule_set.get("grid_type", "NONE")
                                },
                                "lower-frequency": rule_set["lower_frequency_mhz"],
                                "upper-frequency": rule_set["upper_frequency_mhz"]
                            }
                        }
                    }
                endpoints.append(ep)
        cs["end-point"] = endpoints

        route_obj = rule_set.get("route_objective_function", "UNSPECIFIED")
        cs["route-objective-function"] = route_obj if route_obj != "NONE" else "10000"
        capacity_value = rule_set.get("capacity_value", "NONE")
        capacity_unit  = rule_set.get("capacity_unit" , "NONE")

        if capacity_value != "NONE":
            cs["requested-capacity"] = {
                "total-size": {"value": capacity_value, "unit": capacity_unit}
            }
        outputs.append({"tapi-connectivity:connectivity-service": [cs]})
    return outputs, urls
+24 −15
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ from common.method_wrappers.Decorator import MetricsPool, metered_subclass_metho
from common.type_checkers.Checkers import chk_string, chk_type
from device.service.driver_api._Driver import _Driver
from . import ALL_RESOURCE_KEYS
from .Tools import create_connectivity_service, find_key, config_getter, delete_connectivity_service
from .Tools import create_connectivity_service, find_key, config_getter, delete_connectivity_service, tapi_tequest

LOGGER = logging.getLogger(__name__)

@@ -84,6 +84,15 @@ class TransportApiDriver(_Driver):
        if len(resources) == 0:
            return results
        with self.__lock:
            if "tapi_lsp" in str(resources):
                for resource in resources:
                    try:
                        tapi_tequest(resource)
                        results.append((resource, True))
                    except Exception as e:
                        MSG = "Invalid resource_value type: expected dict, got {:s}"
                        results.append((resource, e))
            else:
                for resource in resources:
                    LOGGER.info('resource = {:s}'.format(str(resource)))

Loading