diff --git a/src/device/service/OpenConfigServicer.py b/src/device/service/OpenConfigServicer.py index 2a7e94a99262d8ae4add2896aa2788efc254dee2..bee2d4d5ccf6a5757b854f2d8b2275c4da64758f 100644 --- a/src/device/service/OpenConfigServicer.py +++ b/src/device/service/OpenConfigServicer.py @@ -16,7 +16,8 @@ import grpc, logging, json from common.method_wrappers.Decorator import MetricsPool, safe_and_metered_rpc_method from common.method_wrappers.ServiceExceptions import NotFoundException from common.proto.context_pb2 import ( - Device, DeviceId, DeviceOperationalStatusEnum, Empty, OpticalConfig, OpticalConfig,OpticalConfigList,EndPoint + Device, DeviceId, DeviceOperationalStatusEnum, Empty, OpticalConfig, + OpticalConfig, OpticalConfigList ) from common.proto.device_pb2_grpc import DeviceServiceServicer from common.tools.context_queries.Device import get_device @@ -25,7 +26,7 @@ from context.client.ContextClient import ContextClient from .driver_api._Driver import _Driver from .driver_api.DriverInstanceCache import DriverInstanceCache, get_driver from .monitoring.MonitoringLoops import MonitoringLoops -from .Tools import extract_resources , get_endpoint_matching +from .Tools import extract_resources from .Tools import check_no_endpoints LOGGER = logging.getLogger(__name__) @@ -76,104 +77,84 @@ class OpenConfigServicer(DeviceServiceServicer): @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def ConfigureOpticalDevice (self, request : OpticalConfig, context : grpc.ServicerContext) -> Empty: device_uuid = request.device_id.device_uuid.uuid - resources:list[dict]=[] - is_all_good=True - config =json.loads(request.config) - results=None + resources : list[dict] = [] + is_all_good = True + config = json.loads(request.config) + results = None try: context_client = ContextClient() device = get_device( context_client, device_uuid, rw_copy=True, include_endpoints=True, include_components=False, include_config_rules=False) - if device is None: raise NotFoundException('Device', device_uuid, extra_details='loading in ConfigureDevice') - - resources,conditions=extract_resources(config=config,device=device) - + resources, conditions = extract_resources(config=config, device=device) + driver : _Driver = get_driver(self.driver_instance_cache, device) results = driver.SetConfig(resources=resources,conditions=conditions) for result in results: if not result : - is_all_good=False - - + is_all_good = False + if is_all_good: #driver.GetConfig(resource_keys=[]) config = json.loads(request.config) - handled_flow=next((i for i in resources if i['resource_key']=='handled_flow'),None) - flow_handled=[ ] - - - if handled_flow is not None and len(handled_flow)>0: - config['flow_handled']=handled_flow['value'] - + handled_flow = next((i for i in resources if i['resource_key'] == 'handled_flow'), None) + if handled_flow is not None and len(handled_flow) > 0: + config['flow_handled'] = handled_flow['value'] request.config=json.dumps(config) - context_client.UpdateOpticalConfig(request) context_client.close() except Exception as e: - LOGGER.info("error in configuring %s",e) + LOGGER.info("error in configuring %s",e) return Empty() - - + @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def GetDeviceConfiguration (self, request : OpticalConfigList, context : grpc.ServicerContext) -> Empty: context_client = ContextClient() - - for configs in request.opticalconfigs: + for configs in request.opticalconfigs: device_uuid = configs.device_id.device_uuid.uuid - try: - device = get_device( context_client, device_uuid, rw_copy=True, include_endpoints=True, include_components=False, include_config_rules=False) - if device is None: raise NotFoundException('Device', device_uuid, extra_details='loading in ConfigureDevice') - driver : _Driver = get_driver(self.driver_instance_cache, device) - results = driver.GetConfig(resource_keys=[]) for resource_data in results : resource_key, resource_value = resource_data - if resource_key.startswith('/opticalconfigs/opticalconfig/'): if 'opticalconfig' in resource_value: - context_client.SetOpticalConfig(resource_value['opticalconfig']) + context_client.SetOpticalConfig(resource_value['opticalconfig']) #TODO: add a control with the NETCONF get #driver.GetConfig(resource_keys=filter_fields) except Exception as e: - LOGGER.info("error in configuring %s",e) + LOGGER.info("error in configuring %s",e) context_client.close() return Empty() - - - - + @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def DisableOpticalDevice (self, request : OpticalConfig, context : grpc.ServicerContext) -> Empty: - - roadm_configuration=None + roadm_configuration = None device_uuid = request.device_id.device_uuid.uuid - resources:list[dict]=[] - is_all_good=True - config =json.loads(request.config) + resources : list[dict] = [] + is_all_good = True + config = json.loads(request.config) try: context_client = ContextClient() device = get_device( context_client, device_uuid, rw_copy=True, include_endpoints=True, include_components=False, include_config_rules=False) - + if device is None: raise NotFoundException('Device', device_uuid, extra_details='loading in ConfigureDevice') - - resources,conditions=extract_resources(config=config,device=device) + + resources, conditions = extract_resources(config=config, device=device) driver : _Driver = get_driver(self.driver_instance_cache, device) if 'edit_type' in conditions and conditions['edit_type'] == 'optical-band': @@ -181,22 +162,17 @@ class OpenConfigServicer(DeviceServiceServicer): for resource_data in roadm_configuration: resource_key, resource_value = resource_data if resource_key.startswith('/opticalconfigs/opticalconfig/'): - roadm_configuration=resource_value["opticalconfig"] + roadm_configuration=resource_value["opticalconfig"] results = driver.DeleteConfig(resources=resources,conditions=conditions,optical_device_configuration=roadm_configuration) for result in results: if not result : - is_all_good=False + is_all_good = False if is_all_good: config = json.loads(request.config) - flow_handled=[ ] if "new_config" in config : - - - context_client.DeleteOpticalChannel(request) - context_client.close() + context_client.DeleteOpticalChannel(request) + context_client.close() except Exception as e: - LOGGER.info("error in Disable configuring %s",e) + LOGGER.info("error in Disable configuring %s",e) return Empty() - - \ No newline at end of file diff --git a/src/device/service/Tools.py b/src/device/service/Tools.py index 367188650c72c1d2990231bd27dc45e9e162cc50..967a6a8c19ca4da763f29bfa164f50ad88d27363 100644 --- a/src/device/service/Tools.py +++ b/src/device/service/Tools.py @@ -17,7 +17,9 @@ from typing import Any, Dict, List, Optional, Tuple, Union from common.Constants import DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME from common.DeviceTypes import DeviceTypeEnum from common.method_wrappers.ServiceExceptions import InvalidArgumentException, NotFoundException -from common.proto.context_pb2 import ConfigActionEnum, ConfigRule_ACL, Device, DeviceConfig, EndPoint, Link, Location,OpticalConfig +from common.proto.context_pb2 import ( + ConfigActionEnum, ConfigRule_ACL, Device, DeviceConfig, EndPoint, Link, Location, OpticalConfig +) from common.proto.device_pb2 import MonitoringSettings from common.proto.kpi_sample_types_pb2 import KpiSampleType from common.tools.grpc.ConfigRules import update_config_rule_custom @@ -26,7 +28,8 @@ from common.type_checkers.Checkers import chk_length, chk_type from .driver_api._Driver import _Driver, RESOURCE_ENDPOINTS from .monitoring.MonitoringLoops import MonitoringLoops from .ErrorMessages import ( - ERROR_BAD_RESOURCE, ERROR_DELETE, ERROR_GET, ERROR_GET_INIT, ERROR_MISSING_KPI, ERROR_SAMPLETYPE, ERROR_SET, + ERROR_BAD_RESOURCE, ERROR_DELETE, ERROR_GET, ERROR_GET_INIT, + ERROR_MISSING_KPI, ERROR_SAMPLETYPE, ERROR_SET, ERROR_SUBSCRIBE, ERROR_UNSUBSCRIBE, ERROR_UNSUP_RESOURCE ) @@ -106,8 +109,8 @@ def get_device_controller_uuid(device : Device) -> Optional[str]: def populate_endpoints( device : Device, driver : _Driver, monitoring_loops : MonitoringLoops, - new_sub_devices : Dict[str, Device], new_sub_links : Dict[str, Link] - ,new_optical_configs:Dict[str,OpticalConfig] + new_sub_devices : Dict[str, Device], new_sub_links : Dict[str, Link], + new_optical_configs : Dict[str, OpticalConfig] ) -> List[str]: device_uuid = device.device_id.device_uuid.uuid device_name = device.name @@ -116,7 +119,6 @@ def populate_endpoints( results_getconfig = driver.GetConfig(resources_to_get) LOGGER.debug('results_getconfig = {:s}'.format(str(results_getconfig))) - # first quick pass to identify need of mgmt endpoints and links add_mgmt_port = False for resource_data in results_getconfig: @@ -238,12 +240,10 @@ def populate_endpoints( _sub_link_endpoint_id.topology_id.topology_uuid.uuid = DEFAULT_TOPOLOGY_NAME _sub_link_endpoint_id.device_id.device_uuid.uuid = device_uuid _sub_link_endpoint_id.endpoint_uuid.uuid = endpoint_uuid - + # ----------Experimental -------------- elif resource_key.startswith('/opticalconfigs/opticalconfig/'): new_optical_configs["new_optical_config"]=resource_value - - else: errors.append(ERROR_UNSUP_RESOURCE.format(device_uuid=device_uuid, resource_data=str(resource_data))) continue @@ -277,8 +277,7 @@ def _raw_config_rules_to_grpc( device_uuid=device_uuid, resource_key=str(resource_key), resource_value=str(resource_value), error=str(resource_value))) continue - - + if resource_value is None: continue resource_value = json.loads(resource_value) if isinstance(resource_value, str) else resource_value if isinstance(resource_value, ConfigRule_ACL): resource_value = grpc_message_to_json(resource_value) @@ -288,7 +287,6 @@ def _raw_config_rules_to_grpc( return errors def populate_config_rules(device : Device, driver : _Driver) -> List[str]: - device_uuid = device.device_id.device_uuid.uuid results_getconfig = driver.GetConfig() return _raw_config_rules_to_grpc( @@ -462,7 +460,6 @@ def update_endpoints(src_device : Device, dst_device : Device) -> None: if len(src_context_uuid) > 0: dst_topology_id.context_id.context_uuid.uuid = src_context_uuid def get_edit_target(device : Device, is_opticalband : bool) -> str: - if is_opticalband: return 'optical-band' if device.device_type == DeviceTypeEnum.OPTICAL_ROADM._value_: return 'media-channel' return 'optical-channel' @@ -478,20 +475,20 @@ def is_key_existed(key : str, keys_dic = dict, key_name_to_use = None) -> dict: dic['value'] = None return dic -def extract_resources(config : dict, device : Device) -> list[list[dict],dict]: +def extract_resources(config : dict, device : Device) -> list[list[dict], dict]: conditions = {} - resources:list[dict] = [] + resources : list[dict] = [] resources.append(is_key_existed('channel_namespace', config)) resources.append(is_key_existed('add_transceiver', config)) is_opticalband = config.get('is_opticalband', False) conditions['is_opticalband'] = is_opticalband conditions['edit_type'] = get_edit_target(device, is_opticalband) - + if 'flow' in config: #for tuple_value in config['flow'][device.name]: source_vals = [] dest_vals = [] - handled_flow=[] + handled_flow = [] for tuple_value in config['flow']: source_port = None destination_port = None @@ -504,10 +501,10 @@ def extract_resources(config : dict, device : Device) -> list[list[dict],dict]: dst_endpoint_obj = get_endpoint_matching(device, destination_port_uuid) destination_port = dst_endpoint_obj.name dest_vals.append(destination_port) - handled_flow.append((source_port,destination_port)) - resources.append({'resource_key': 'source_port', 'value': source_vals}) - resources.append({'resource_key': 'destination_port', 'value': dest_vals }) - resources.append({'resource_key':'handled_flow','value':handled_flow}) + handled_flow.append((source_port, destination_port)) + resources.append({'resource_key': 'source_port', 'value': source_vals }) + resources.append({'resource_key': 'destination_port', 'value': dest_vals }) + resources.append({'resource_key': 'handled_flow', 'value': handled_flow}) if 'new_config' in config: lower_frequency = None upper_frequency = None @@ -520,21 +517,15 @@ def extract_resources(config : dict, device : Device) -> list[list[dict],dict]: resources.append(is_key_existed('ob_id', keys_dic=config['new_config'], key_name_to_use='optical-band-parent')) #resources.append(is_key_existed('name', keys_dic=config['new_config'], key_name_to_use='channel_name')) if not is_opticalband: - if 'frequency' in config['new_config'] and 'band' in config['new_config'] and conditions['edit_type'] == 'media-channel': if config['new_config']['frequency'] is not None and config['new_config']['band'] is not None: lower_frequency = int(int(config['new_config']['frequency']) - (int(config['new_config']['band'])/2)+1) upper_frequency = int(int(config['new_config']['frequency']) + (int(config['new_config']['band'])/2)) - - resources.append(is_key_existed('flow_id', keys_dic=config['new_config'], key_name_to_use='index')) #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(is_key_existed('ob_id', keys_dic=config['new_config'], key_name_to_use='index')) #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})