Commit 592b5640 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Pre-merge code cleanup

parent f63f6b31
Loading
Loading
Loading
Loading
+32 −56
Original line number Diff line number Diff line
@@ -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__)
@@ -86,10 +87,8 @@ class OpenConfigServicer(DeviceServiceServicer):
            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)

            driver : _Driver = get_driver(self.driver_instance_cache, device)
@@ -98,49 +97,35 @@ class OpenConfigServicer(DeviceServiceServicer):
                if not result :
                    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']
                    
                    request.config=json.dumps(config) 
                    
                context_client.UpdateOpticalConfig(request) 
                context_client.close()    
        except Exception as 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:

            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'])
@@ -152,12 +137,8 @@ class OpenConfigServicer(DeviceServiceServicer):
        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
        device_uuid = request.device_id.device_uuid.uuid
        resources : list[dict] = []
@@ -189,14 +170,9 @@ class OpenConfigServicer(DeviceServiceServicer):

            if is_all_good:
                config = json.loads(request.config)
                flow_handled=[ ]
                if "new_config" in config :
                    
                    
                    context_client.DeleteOpticalChannel(request) 
                    context_client.close()    
        except Exception as e:
            LOGGER.info("error in Disable configuring %s",e)    
        return Empty()
    
      
 No newline at end of file
+17 −26
Original line number Diff line number Diff line
@@ -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:
@@ -242,8 +244,6 @@ def populate_endpoints(
        # ----------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
@@ -278,7 +278,6 @@ def _raw_config_rules_to_grpc(
                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'
@@ -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})