From 6e8e9aae2629fab3537e05156b611a2bf16ca7ae Mon Sep 17 00:00:00 2001 From: gifrerenom <lluis.gifre@cttc.es> Date: Tue, 20 Feb 2024 16:13:01 +0000 Subject: [PATCH] Device component: - Pre-merge cleanup --- src/device/client/DeviceClient.py | 7 +- src/device/service/DeviceService.py | 1 - .../service/DeviceServiceServicerImpl.py | 3 +- src/device/service/OpenConfigServicer.py | 92 ++------------ src/device/service/Tools.py | 117 +++++++++--------- .../service/driver_api/DriverInstanceCache.py | 7 +- src/device/service/drivers/__init__.py | 6 +- .../service/drivers/oc_driver/OCDriver.py | 2 +- .../service/drivers/oc_driver/__init__.py | 11 +- .../templates/Interfaces/__init__.py | 2 +- .../templates/Interfaces/interfaces.py | 2 +- .../drivers/oc_driver/templates/Tools.py | 1 + .../oc_driver/templates/VPN/physical.py | 2 +- 13 files changed, 92 insertions(+), 161 deletions(-) diff --git a/src/device/client/DeviceClient.py b/src/device/client/DeviceClient.py index 5b94677a9..32fa685b9 100644 --- a/src/device/client/DeviceClient.py +++ b/src/device/client/DeviceClient.py @@ -15,7 +15,7 @@ import grpc, logging from common.Constants import ServiceNameEnum from common.Settings import get_service_host, get_service_port_grpc -from common.proto.context_pb2 import Device, DeviceConfig, DeviceId, Empty,MyConfig,MyConfigId +from common.proto.context_pb2 import Device, DeviceConfig, DeviceId, Empty, MyConfig, MyConfigId from common.proto.device_pb2 import MonitoringSettings from common.proto.device_pb2_grpc import DeviceServiceStub from common.tools.client.RetryDecorator import retry, delay_exponential @@ -82,8 +82,9 @@ class DeviceClient: response = self.stub.MonitorDeviceKpi(request) LOGGER.debug('MonitorDeviceKpi result: {:s}'.format(grpc_message_to_json_string(response))) return response - def ConfigureOpticalDevice(self, request : MyConfig) -> MyConfigId: + def ConfigureOpticalDevice(self, request : MyConfig) -> MyConfigId: + LOGGER.debug('ConfigureOpticalDevice request: {:s}'.format(grpc_message_to_json_string(request))) response = self.openconfig_stub.ConfigureOpticalDevice(request) - + LOGGER.debug('ConfigureOpticalDevice result: {:s}'.format(grpc_message_to_json_string(response))) return response diff --git a/src/device/service/DeviceService.py b/src/device/service/DeviceService.py index 544f250f6..c7868e445 100644 --- a/src/device/service/DeviceService.py +++ b/src/device/service/DeviceService.py @@ -39,7 +39,6 @@ class DeviceService(GenericGrpcService): self.monitoring_loops.start() add_DeviceServiceServicer_to_server(self.device_servicer, self.server) add_OpenConfigServiceServicer_to_server(self.openconfig_device_servicer,self.server) - def stop(self): super().stop() diff --git a/src/device/service/DeviceServiceServicerImpl.py b/src/device/service/DeviceServiceServicerImpl.py index ff39a4e15..7592e5b58 100644 --- a/src/device/service/DeviceServiceServicerImpl.py +++ b/src/device/service/DeviceServiceServicerImpl.py @@ -20,7 +20,8 @@ from common.Settings import ENVVAR_SUFIX_SERVICE_HOST, get_env_var_name from common.method_wrappers.Decorator import MetricTypeEnum, MetricsPool, safe_and_metered_rpc_method from common.method_wrappers.ServiceExceptions import NotFoundException, OperationFailedException from common.proto.context_pb2 import ( - Device, DeviceConfig, DeviceDriverEnum, DeviceId, DeviceOperationalStatusEnum, Empty, Link,MyConfig,MyConfigId) + Device, DeviceConfig, DeviceDriverEnum, DeviceId, DeviceOperationalStatusEnum, Empty, Link, MyConfig, MyConfigId +) from common.proto.device_pb2 import MonitoringSettings from common.proto.device_pb2_grpc import DeviceServiceServicer from common.tools.context_queries.Device import get_device diff --git a/src/device/service/OpenConfigServicer.py b/src/device/service/OpenConfigServicer.py index 5c5c7cb76..5be11cb31 100644 --- a/src/device/service/OpenConfigServicer.py +++ b/src/device/service/OpenConfigServicer.py @@ -20,7 +20,9 @@ from common.Settings import ENVVAR_SUFIX_SERVICE_HOST, get_env_var_name from common.method_wrappers.Decorator import MetricTypeEnum, MetricsPool, safe_and_metered_rpc_method from common.method_wrappers.ServiceExceptions import NotFoundException, OperationFailedException from common.proto.context_pb2 import ( - Device, DeviceConfig, DeviceDriverEnum, DeviceId, DeviceOperationalStatusEnum, Empty, Link,MyConfig,MyConfigId ,MyConfig,MyConfigList) + Device, DeviceConfig, DeviceDriverEnum, DeviceId, DeviceOperationalStatusEnum, Empty, Link, MyConfig, MyConfigId, + MyConfig, MyConfigList +) from common.proto.device_pb2 import MonitoringSettings from common.proto.device_pb2_grpc import DeviceServiceServicer from common.tools.context_queries.Device import get_device @@ -92,99 +94,19 @@ class OpenConfigServicer(DeviceServiceServicer): #results_getconfig = driver.GetConfig(resources_to_get,device_uuid) except Exception as error : LOGGER.debug("error %s",error) - - # @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) - # def ConfigureOpenConfigDevice(self, request : MyConfig, context : grpc.ServicerContext) -> DeviceId: - - # device_id = request.myconfig_id - # device_uuid = device_id.myconfig_uuid - - # self.mutex_queues.wait_my_turn(device_uuid) - # t1 = time.time() - # try: - - # context_client = ContextClient() - # t2 = time.time() - # device = get_device( - # context_client, device_uuid, rw_copy=True, include_endpoints=False, include_components=False, - # include_config_rules=True) - # if device is None: - # raise NotFoundException('Device', device_uuid, extra_details='loading in ConfigureDevice') - - # t3 = time.time() - # device_controller_uuid = get_device_controller_uuid(device) - # if device_controller_uuid is not None: - # device = get_device( - # context_client, device_controller_uuid, rw_copy=True, include_endpoints=False, - # include_components=False, include_config_rules=True) - # if device is None: - # raise NotFoundException( - # 'Device', device_controller_uuid, extra_details='loading in ConfigureDevice') - # device_uuid = device.device_id.device_uuid.uuid - # driver : _Driver = get_driver(self.driver_instance_cache, device) - # resources_to_set, resources_to_delete = compute_rules_to_add_delete(device, request) - # results_setconfig = driver.SetConfig(resources=resources_to_set) - # except Exception as e: - # LOGGER.debug("error in updating %s",e) - - ''' - @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) - def ConfigureOpticalDevice (self, request : MyConfig, context:grpc.ServicerContext) -> Empty: - LOGGER.info('Updating from ConfigureOpticalDevice %s',request) - device_id = request.myconfig_id - device_uuid = device_id.myconfig_uuid - resources=[] - add_proccess=False - update_interface=False - result=None - config =eval(request.config) - - - filter_fields= ["frequency","target-output-power","interface","operational-mode"] - try: - - context_client = ContextClient() - - device = get_device( - context_client, device_uuid, rw_copy=True, include_endpoints=False, include_components=False, - include_config_rules=True) - - if device is None: - LOGGER.debug("device is none") - raise NotFoundException('Device', device_uuid, extra_details='loading in ConfigureDevice') - resources,conditions=extract_resources(config=config,device=device) - driver : _Driver = get_driver(self.driver_instance_cache, device) - LOGGER.info("resource %s conditions %s",resources,conditions) - - result = driver.SetConfig(resources=resources,conditions=conditions) - driver.GetConfig(resource_keys=filter_fields) - - except Exception as e: - LOGGER.info("error in configuring %s",e) - - - LOGGER.info("result %s",result) - return Empty() - ''' - - #modified Andrea @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def ConfigureOpticalDevice (self, request : MyConfig, context:grpc.ServicerContext) -> Empty: LOGGER.info('Setting from ConfigureOpticalDevice with Flows %s',request) #device_id = request.myconfig_id #device_uuid = device_id.myconfig_uuid device_uuid = request.myconfig_id.myconfig_uuid - LOGGER.info("AAAAAAAAAAAAAAAAAAAAAAA device uuid {}".format(device_uuid)) + LOGGER.info("device uuid {}".format(device_uuid)) resources=[] result=None - - config =eval(request.config) - - - filter_fields= ["frequency","target-output-power","interface","operational-mode"] + config = eval(request.config) + filter_fields= ["frequency", "target-output-power", "interface", "operational-mode"] try: - context_client = ContextClient() device = get_device( @@ -192,7 +114,7 @@ class OpenConfigServicer(DeviceServiceServicer): include_config_rules=False) if device is None: - LOGGER.info("AAAAAAAAAAAAAAAAAAAAAAAdevice is none") + LOGGER.info("device is none") raise NotFoundException('Device', device_uuid, extra_details='loading in ConfigureDevice') resources,conditions=extract_resources(config=config,device=device) driver : _Driver = get_driver(self.driver_instance_cache, device) diff --git a/src/device/service/Tools.py b/src/device/service/Tools.py index c4b41020a..8ccb77afa 100644 --- a/src/device/service/Tools.py +++ b/src/device/service/Tools.py @@ -11,6 +11,7 @@ # 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 uuid import UUID, uuid4, uuid5 import json, logging from typing import Any, Dict, List, Optional, Tuple, Union @@ -454,6 +455,7 @@ def update_endpoints(src_device : Device, dst_device : Device) -> None: dst_topology_id = dst_endpoint_id.topology_id if len(src_topology_uuid) > 0: dst_topology_id.topology_uuid.uuid = src_topology_uuid if len(src_context_uuid) > 0: dst_topology_id.context_id.context_uuid.uuid = src_context_uuid + def oc_default_endpoints( device : Device, driver : _Driver, monitoring_loops : MonitoringLoops, new_sub_devices : Dict[str, Device], new_sub_links : Dict[str, Link] @@ -469,62 +471,61 @@ def get_edit_target (device:Device,is_opticalband:bool)-> str: if device.device_type =='optical-roadm': return 'media-channel' else : return 'optical-channel' -def extract_resources (config:dict,device:Device)-> list : - conditions={} - resources=[] - resources.append({"resource_key":"channel_namespace","value":config["channel_namespace"] if "channel_namespace" in config else None}) - resources.append({"resource_key":'add_transceiver',"value":config['add_transceiver'] if 'add_transceiver' in config else None}) - resources.append({"resource_key":"interface","value":config["update_interface"] if 'update_interface' in config else None}) - is_opticalband=config['is_opticalband'] if 'is_opticalband' in config else False - conditions["is_opticalband"]=is_opticalband - conditions["edit_type"]=get_edit_target(device=device,is_opticalband=is_opticalband) - if ('flow' in config): - #for tuple_value in config['flow'][device.name]: - source_vals = [] - dest_vals = [] - for tuple_value in config['flow']: - source_port=None - destination_port=None - #resources.append({"resource_key":"source_port","value":source_port}) - #resources.append({"resource_key":"destination_port","value":destination_port}) - source_port_uuid,destination_port_uuid=tuple_value - if (source_port_uuid !='0'): - src_endpoint_obj = get_endpoint_matching(device, source_port_uuid) - source_port = src_endpoint_obj.name - source_vals.append(source_port) - if (destination_port_uuid !='0'): - dst_endpoint_obj = get_endpoint_matching(device, destination_port_uuid) - destination_port = dst_endpoint_obj.name - dest_vals.append(destination_port) - resources.append({"resource_key":"source_port","value":source_vals}) - resources.append({"resource_key":"destination_port","value":dest_vals}) - if ('new_config' in config): - lower_frequency=None - upper_frequency=None - resources.append({"resource_key":"target-output-power","value":config["new_config"]["target-output-power"] if "target-output-power" in config["new_config"] else None }) - #resources.append({"resource_key":"frequency","value":config["new_config"]["frequency"] if "frequency" in config else config["new_config"]["freqency"]}) - resources.append({"resource_key":"frequency","value":config["new_config"]["frequency"] if "frequency" in config["new_config"] else None}) - resources.append({"resource_key":"operational-mode","value":config["new_config"]["operational-mode"] if "operational-mode" in config["new_config"] else None}) - resources.append({"resource_key":"line-port","value":config["new_config"]["line-port"] if "line-port" in config["new_config"] else None}) - - resources.append({"resource_key":"name","value":config['new_config']['band_type'] if 'band_type' in config['new_config'] else None}) - resources.append({"resource_key":"optical-band-parent","value":config["new_config"]["ob_id"] if "ob_id" in config["new_config"] else None }) - resources.append({"resource_key":"channel_name","value":config["new_config"]["name"] if "name" in config["new_config"] else None}) - - if not is_opticalband : - if 'frequency' in config['new_config'] and 'band' in config['new_config'] and conditions["edit_type"] == 'media-channel': - lower_frequency= int(int(config['new_config']['frequency']) - (int(config['new_config']['band'])/2)) - upper_frequency= int(int(config['new_config']['frequency']) + (int(config['new_config']['band'])/2)) - - #lower_frequency= (config['new_config']['frequency']- config['new_config']['band'])/2 - #upper_frequency=(config['new_config']['frequency']+ config['new_config']['band'])/2 - resources.append({"resource_key":"index","value":config["new_config"]["flow_id"] if "flow_id" in config["new_config"] else None}) - else : - lower_frequency=config['new_config']['low-freq'] if "low-freq" in config['new_config'] else None - upper_frequency=config['new_config']['up-freq'] if 'up-freq' in config['new_config'] else None - resources.append({"resource_key":"index","value":config["new_config"]["ob_id"] if "ob_id" in config["new_config"] else None}) - - resources.append({"resource_key":"lower-frequency","value":lower_frequency}) - resources.append({"resource_key":"upper-frequency","value":upper_frequency}) - return [resources,conditions] +def extract_resources (config : dict, device : Device)-> list: + conditions={} + resources=[] + resources.append({"resource_key":"channel_namespace","value":config["channel_namespace"] if "channel_namespace" in config else None}) + resources.append({"resource_key":'add_transceiver',"value":config['add_transceiver'] if 'add_transceiver' in config else None}) + resources.append({"resource_key":"interface","value":config["update_interface"] if 'update_interface' in config else None}) + is_opticalband=config['is_opticalband'] if 'is_opticalband' in config else False + conditions["is_opticalband"]=is_opticalband + conditions["edit_type"]=get_edit_target(device=device,is_opticalband=is_opticalband) + if ('flow' in config): + #for tuple_value in config['flow'][device.name]: + source_vals = [] + dest_vals = [] + for tuple_value in config['flow']: + source_port=None + destination_port=None + #resources.append({"resource_key":"source_port","value":source_port}) + #resources.append({"resource_key":"destination_port","value":destination_port}) + source_port_uuid,destination_port_uuid=tuple_value + if (source_port_uuid !='0'): + src_endpoint_obj = get_endpoint_matching(device, source_port_uuid) + source_port = src_endpoint_obj.name + source_vals.append(source_port) + if (destination_port_uuid !='0'): + dst_endpoint_obj = get_endpoint_matching(device, destination_port_uuid) + destination_port = dst_endpoint_obj.name + dest_vals.append(destination_port) + resources.append({"resource_key":"source_port","value":source_vals}) + resources.append({"resource_key":"destination_port","value":dest_vals}) + if ('new_config' in config): + lower_frequency=None + upper_frequency=None + resources.append({"resource_key":"target-output-power","value":config["new_config"]["target-output-power"] if "target-output-power" in config["new_config"] else None }) + #resources.append({"resource_key":"frequency","value":config["new_config"]["frequency"] if "frequency" in config else config["new_config"]["freqency"]}) + resources.append({"resource_key":"frequency","value":config["new_config"]["frequency"] if "frequency" in config["new_config"] else None}) + resources.append({"resource_key":"operational-mode","value":config["new_config"]["operational-mode"] if "operational-mode" in config["new_config"] else None}) + resources.append({"resource_key":"line-port","value":config["new_config"]["line-port"] if "line-port" in config["new_config"] else None}) + + resources.append({"resource_key":"name","value":config['new_config']['band_type'] if 'band_type' in config['new_config'] else None}) + resources.append({"resource_key":"optical-band-parent","value":config["new_config"]["ob_id"] if "ob_id" in config["new_config"] else None }) + resources.append({"resource_key":"channel_name","value":config["new_config"]["name"] if "name" in config["new_config"] else None}) + + if not is_opticalband : + if 'frequency' in config['new_config'] and 'band' in config['new_config'] and conditions["edit_type"] == 'media-channel': + lower_frequency= int(int(config['new_config']['frequency']) - (int(config['new_config']['band'])/2)) + upper_frequency= int(int(config['new_config']['frequency']) + (int(config['new_config']['band'])/2)) + + #lower_frequency= (config['new_config']['frequency']- config['new_config']['band'])/2 + #upper_frequency=(config['new_config']['frequency']+ config['new_config']['band'])/2 + resources.append({"resource_key":"index","value":config["new_config"]["flow_id"] if "flow_id" in config["new_config"] else None}) + else : + lower_frequency=config['new_config']['low-freq'] if "low-freq" in config['new_config'] else None + upper_frequency=config['new_config']['up-freq'] if 'up-freq' in config['new_config'] else None + resources.append({"resource_key":"index","value":config["new_config"]["ob_id"] if "ob_id" in config["new_config"] else None}) + resources.append({"resource_key":"lower-frequency","value":lower_frequency}) + resources.append({"resource_key":"upper-frequency","value":upper_frequency}) + return [resources,conditions] diff --git a/src/device/service/driver_api/DriverInstanceCache.py b/src/device/service/driver_api/DriverInstanceCache.py index 5766237bc..26735bc16 100644 --- a/src/device/service/driver_api/DriverInstanceCache.py +++ b/src/device/service/driver_api/DriverInstanceCache.py @@ -52,7 +52,12 @@ class DriverInstanceCache: driver_class = self._driver_factory.get_driver_class(**filter_fields) MSG = 'Driver({:s}) selected for device({:s}) with filter_fields({:s})...' LOGGER.info(MSG.format(str(driver_class.__name__), str(device_uuid), str(filter_fields))) - driver_instance : _Driver =driver_class(address, port,device_uuid=device_uuid, **settings) if (driver_class.__name__ == "OCDriver") else driver_class(address, port, **settings) + + if driver_class.__name__ == "OCDriver": + driver_instance : _Driver = driver_class(address, port, device_uuid=device_uuid, **settings) + else: + driver_instance : _Driver = driver_class(address, port, **settings) + self._device_uuid__to__driver_instance[device_uuid] = driver_instance return driver_instance diff --git a/src/device/service/drivers/__init__.py b/src/device/service/drivers/__init__.py index 9f1431a0c..09c4d33b7 100644 --- a/src/device/service/drivers/__init__.py +++ b/src/device/service/drivers/__init__.py @@ -167,6 +167,7 @@ if LOAD_ALL_DEVICE_DRIVERS: FilterFieldEnum.DRIVER: DeviceDriverEnum.DEVICEDRIVER_FLEXSCALE, } ])) + if LOAD_ALL_DEVICE_DRIVERS: from .oc_driver.OCDriver import OCDriver # pylint: disable=wrong-import-position DRIVERS.append( @@ -174,12 +175,11 @@ if LOAD_ALL_DEVICE_DRIVERS: { # Real Packet Router, specifying OpenConfig Driver => use OpenConfigDriver - FilterFieldEnum.DEVICE_TYPE:[ + FilterFieldEnum.DEVICE_TYPE: [ DeviceTypeEnum.NETCONFIG_AGENT, DeviceTypeEnum.OPTICAL_ROADM, DeviceTypeEnum.OPTICAL_TRANSPONDER - - ], + ], FilterFieldEnum.DRIVER : DeviceDriverEnum.DEVICEDRIVER_OC, } ])) diff --git a/src/device/service/drivers/oc_driver/OCDriver.py b/src/device/service/drivers/oc_driver/OCDriver.py index b76d7118d..512ec49ba 100644 --- a/src/device/service/drivers/oc_driver/OCDriver.py +++ b/src/device/service/drivers/oc_driver/OCDriver.py @@ -1,4 +1,4 @@ -# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (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. diff --git a/src/device/service/drivers/oc_driver/__init__.py b/src/device/service/drivers/oc_driver/__init__.py index a532b7982..1549d9811 100644 --- a/src/device/service/drivers/oc_driver/__init__.py +++ b/src/device/service/drivers/oc_driver/__init__.py @@ -5,9 +5,10 @@ # 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. diff --git a/src/device/service/drivers/oc_driver/templates/Interfaces/__init__.py b/src/device/service/drivers/oc_driver/templates/Interfaces/__init__.py index a5d4f2e0b..355dcdb04 100644 --- a/src/device/service/drivers/oc_driver/templates/Interfaces/__init__.py +++ b/src/device/service/drivers/oc_driver/templates/Interfaces/__init__.py @@ -1,4 +1,4 @@ -# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (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. diff --git a/src/device/service/drivers/oc_driver/templates/Interfaces/interfaces.py b/src/device/service/drivers/oc_driver/templates/Interfaces/interfaces.py index 8d3049cfe..b4df7c2bf 100644 --- a/src/device/service/drivers/oc_driver/templates/Interfaces/interfaces.py +++ b/src/device/service/drivers/oc_driver/templates/Interfaces/interfaces.py @@ -1,4 +1,4 @@ -# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (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. diff --git a/src/device/service/drivers/oc_driver/templates/Tools.py b/src/device/service/drivers/oc_driver/templates/Tools.py index bcfc50826..4370495c9 100644 --- a/src/device/service/drivers/oc_driver/templates/Tools.py +++ b/src/device/service/drivers/oc_driver/templates/Tools.py @@ -11,6 +11,7 @@ # 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. + import re,logging import json import lxml.etree as ET diff --git a/src/device/service/drivers/oc_driver/templates/VPN/physical.py b/src/device/service/drivers/oc_driver/templates/VPN/physical.py index 054e53afd..ba0de7ea6 100644 --- a/src/device/service/drivers/oc_driver/templates/VPN/physical.py +++ b/src/device/service/drivers/oc_driver/templates/VPN/physical.py @@ -1,4 +1,4 @@ -# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (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. -- GitLab