From 69783027ec23e160602483b45872a08e28745f40 Mon Sep 17 00:00:00 2001 From: gifrerenom <lluis.gifre@cttc.es> Date: Mon, 28 Oct 2024 12:07:00 +0000 Subject: [PATCH] Pre-merge code cleanup --- src/context/service/database/OpticalConfig.py | 117 +++++++++--------- .../OpticalConfig/OpticalConfigModel.py | 31 +++-- .../models/OpticalConfig/RoadmModel.py | 59 ++++----- src/device/service/Tools.py | 35 +++--- .../service/drivers/oc_driver/OCDriver.py | 17 ++- src/tests/ofc24/deploy_specs.sh | 2 +- 6 files changed, 126 insertions(+), 135 deletions(-) diff --git a/src/context/service/database/OpticalConfig.py b/src/context/service/database/OpticalConfig.py index 09e0f9594..c0236a0fe 100644 --- a/src/context/service/database/OpticalConfig.py +++ b/src/context/service/database/OpticalConfig.py @@ -51,13 +51,13 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig): device_id = request.device_id device_uuid = request.device_id.device_uuid.uuid channels = [] - interfaces=[] - transponder=[] - roadms=[] - channel_namespace= None + interfaces = [] + transponder = [] + roadms = [] + channel_namespace = None OpticalConfig_data = [] - config_type=None - #is_transpondre=False + config_type = None + #is_transpondre = False opticalconfig_uuid = opticalconfig_get_uuid(device_id) if request.config: @@ -140,12 +140,12 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig): "roadm_uuid" : roadm_get_uuid(device_id), "opticalconfig_uuid" : opticalconfig_uuid, }) - + if config_type == DeviceTypeEnum.OPEN_ROADM._value_: if 'interfaces' in config: for interface in config['interfaces']: interfaces.append({ - "interface_uuid" :interface_get_uuid(interface['name']), + "interface_uuid" : interface_get_uuid(interface['name']), 'name' : interface["name"], "type" : interface["type"], "administrative_state": interface["administrative_state"], @@ -157,21 +157,21 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig): "roadm_uuid" : roadm_get_uuid(device_id), }) roadms.append({ - "roadm_uuid":roadm_get_uuid(device_id), - "opticalconfig_uuid":opticalconfig_uuid, + "roadm_uuid" : roadm_get_uuid(device_id), + "opticalconfig_uuid": opticalconfig_uuid, }) LOGGER.info(f"open_roadm") OpticalConfig_data.append({ - "opticalconfig_uuid":opticalconfig_uuid, + "opticalconfig_uuid" : opticalconfig_uuid, # "transcievers" : transceivers, # "interfaces" :"", "channel_namespace" : channel_namespace , "endpoints" : [json.dumps(endpoint) for endpoint in config.get("endpoints",[])], - "device_uuid": device_uuid, - "type":config_type + "device_uuid" : device_uuid, + "type" : config_type }) - + LOGGER.info(f"added OpticalConfig_data {OpticalConfig_data}") LOGGER.info(f"added interfaces {interfaces}") @@ -180,7 +180,7 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig): stmt = stmt.on_conflict_do_update( index_elements=[OpticalConfigModel.opticalconfig_uuid], set_=dict( - channel_namespace=stmt.excluded.channel_namespace + channel_namespace = stmt.excluded.channel_namespace ) ) stmt = stmt.returning(OpticalConfigModel.opticalconfig_uuid) @@ -189,12 +189,11 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig): if (len(transponder)>0): stmt = insert(TransponderTypeModel).values(transponder) stmt = stmt.on_conflict_do_update( - index_elements=[TransponderTypeModel.transponder_uuid], - set_=dict( - transcievers= stmt.excluded.transcievers , - ) - + index_elements=[TransponderTypeModel.transponder_uuid], + set_=dict( + transcievers = stmt.excluded.transcievers, ) + ) stmt = stmt.returning(TransponderTypeModel.transponder_uuid) transponder_id = session.execute(stmt).fetchone() @@ -218,7 +217,7 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig): stmt = stmt.on_conflict_do_update( index_elements=[RoadmTypeModel.roadm_uuid], set_=dict( - opticalconfig_uuid=stmt.excluded.opticalconfig_uuid + opticalconfig_uuid = stmt.excluded.opticalconfig_uuid ) ) stmt = stmt.returning(RoadmTypeModel.roadm_uuid) @@ -228,15 +227,15 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig): stmt = stmt.on_conflict_do_update( index_elements=[ChannelModel.channel_uuid ], set_=dict( - band_name= stmt.excluded.band_name , - lower_frequency = stmt.excluded.lower_frequency, - upper_frequency = stmt.excluded.upper_frequency, - type=stmt.excluded.type, - status=stmt.excluded.status, - dest_port=stmt.excluded.dest_port, - src_port=stmt.excluded.src_port, - channel_index=stmt.excluded.channel_index, - optical_band_parent = stmt.excluded.optical_band_parent + band_name = stmt.excluded.band_name, + lower_frequency = stmt.excluded.lower_frequency, + upper_frequency = stmt.excluded.upper_frequency, + type = stmt.excluded.type, + status = stmt.excluded.status, + dest_port = stmt.excluded.dest_port, + src_port = stmt.excluded.src_port, + channel_index = stmt.excluded.channel_index, + optical_band_parent = stmt.excluded.optical_band_parent, ) ) stmt = stmt.returning(ChannelModel.channel_uuid) @@ -248,7 +247,7 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig): stmt = stmt.on_conflict_do_update( index_elements=[RoadmTypeModel.roadm_uuid], set_=dict( - opticalconfig_uuid=stmt.excluded.opticalconfig_uuid + opticalconfig_uuid = stmt.excluded.opticalconfig_uuid ) ) stmt = stmt.returning(RoadmTypeModel.roadm_uuid) @@ -257,16 +256,16 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig): if len(interfaces) >0 : stmt = insert(ORInterfaceModel).values(interfaces) stmt = stmt.on_conflict_do_update( - index_elements=[ORInterfaceModel.interface_uuid ], + index_elements=[ORInterfaceModel.interface_uuid], set_=dict( - name= stmt.excluded.name , - frequency = stmt.excluded.frequency, + name = stmt.excluded.name, + frequency = stmt.excluded.frequency, administrative_state = stmt.excluded.administrative_state, - type=stmt.excluded.type, - circuit_pack_name=stmt.excluded.circuit_pack_name, - port=stmt.excluded.port, - interface_list=stmt.excluded.interface_list, - width=stmt.excluded.width, + type = stmt.excluded.type, + circuit_pack_name = stmt.excluded.circuit_pack_name, + port = stmt.excluded.port, + interface_list = stmt.excluded.interface_list, + width = stmt.excluded.width, ) ) stmt = stmt.returning(ORInterfaceModel.interface_uuid) @@ -278,15 +277,15 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig): def update_opticalconfig(db_engine : Engine, request : OpticalConfig): opticalconfig_id = OpticalConfigId() device_id = request.device_id - device_uuid = request.device_id.device_uuid.uuid + device_uuid = request.device_id.device_uuid.uuid channels = [] - transponder=[] - roadms=[] - channel_namespace= None + transponder = [] + roadms = [] + channel_namespace = None OpticalConfig_data = [] - config_type=None - #is_transpondre=False - opticalconfig_uuid =opticalconfig_get_uuid(device_id) + config_type = None + #is_transpondre = False + opticalconfig_uuid = opticalconfig_get_uuid(device_id) if request.config : config = json.loads(request.config) @@ -433,7 +432,7 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig): stmt = stmt.on_conflict_do_update( index_elements=[TransponderTypeModel.transponder_uuid], set_=dict( - transcievers= stmt.excluded.transcievers, + transcievers = stmt.excluded.transcievers, ) ) stmt = stmt.returning(TransponderTypeModel.transponder_uuid) @@ -442,13 +441,13 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig): if len(channels) > 0: stmt = insert(OpticalChannelModel).values(channels) stmt = stmt.on_conflict_do_update( - index_elements=[OpticalChannelModel.channel_uuid ], + index_elements=[OpticalChannelModel.channel_uuid], set_=dict( - channel_name= stmt.excluded.channel_name , - frequency = stmt.excluded.frequency, - operational_mode=stmt.excluded.operational_mode, - target_output_power=stmt.excluded.target_output_power, - status = stmt.excluded.status, + channel_name = stmt.excluded.channel_name, + frequency = stmt.excluded.frequency, + operational_mode = stmt.excluded.operational_mode, + target_output_power = stmt.excluded.target_output_power, + status = stmt.excluded.status, ) ) stmt = stmt.returning(OpticalChannelModel.channel_uuid) @@ -459,7 +458,7 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig): stmt = stmt.on_conflict_do_update( index_elements=[RoadmTypeModel.roadm_uuid], set_=dict( - circuits=stmt.excluded.circuits + circuits = stmt.excluded.circuits ) ) stmt = stmt.returning(RoadmTypeModel.roadm_uuid) @@ -470,13 +469,13 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig): stmt = stmt.on_conflict_do_update( index_elements=[ChannelModel.channel_uuid ], set_=dict( - band_name= stmt.excluded.band_name , + band_name = stmt.excluded.band_name, lower_frequency = stmt.excluded.lower_frequency, upper_frequency = stmt.excluded.upper_frequency, - type=stmt.excluded.type, - status=stmt.excluded.status, - dest_port=stmt.excluded.dest_port, - src_port=stmt.excluded.src_port, + type = stmt.excluded.type, + status = stmt.excluded.status, + dest_port = stmt.excluded.dest_port, + src_port = stmt.excluded.src_port, ) ) stmt = stmt.returning(ChannelModel.channel_uuid) diff --git a/src/context/service/database/models/OpticalConfig/OpticalConfigModel.py b/src/context/service/database/models/OpticalConfig/OpticalConfigModel.py index 9545c6a0d..f9f6655e3 100644 --- a/src/context/service/database/models/OpticalConfig/OpticalConfigModel.py +++ b/src/context/service/database/models/OpticalConfig/OpticalConfigModel.py @@ -20,10 +20,10 @@ from context.service.database.models._Base import _Base class OpticalConfigModel(_Base): __tablename__ = 'optical_config' - opticalconfig_uuid = Column(String, primary_key=True) - channel_namespace = Column(String, nullable=True) - endpoints = Column(ARRAY(String), nullable=True) - type = Column(String,nullable=False) + opticalconfig_uuid = Column(String, primary_key = True) + channel_namespace = Column(String, nullable = True) + endpoints = Column(ARRAY(String), nullable = True) + type = Column(String, nullable = False) # transcievers = Column(ARRAY(String), nullable=True) # interfaces = Column(String, nullable=True) @@ -51,21 +51,20 @@ class OpticalConfigModel(_Base): "type" : self.type } if self.type =="optical-transponder" : - channels= [transponer.dump() for transponer in self.transponders ][0] - obj['channels']=channels['channels'] if 'channels' in channels else None - obj['transceivers']=channels['transceivers'] if 'transceivers' in channels else None - obj['interfaces']=channels['interfaces'] if 'interfaces' in channels else None - obj['trasponder_uuid']=channels['trasponder_uuid'] if 'trasponder_uuid' in channels else None + channels = [transponer.dump() for transponer in self.transponders][0] + obj['channels' ] = channels.get('channels', None) + obj['transceivers' ] = channels.get('transceivers', None) + obj['interfaces' ] = channels.get('interfaces', None) + obj['trasponder_uuid'] = channels.get('trasponder_uuid', None) if self.type =="optical-roadm" : - dev = [roadms.dump() for roadms in self.roadms ][0] - obj['channels']=dev['channels'] if 'channels' in dev else None - obj['roadm_uuid']=dev['roadm_uuid'] if 'roadm_uuid' in dev else None + dev = [roadms.dump() for roadms in self.roadms][0] + obj['channels' ] = dev.get('channels', None) + obj['roadm_uuid'] = dev.get('roadm_uuid', None) if self.type =="openroadm" : - dev = [roadms.dump() for roadms in self.roadms ][0] - obj['interfaces']=dev['interfaces'] if 'interfaces' in dev else None - obj['roadm_uuid']=dev['roadm_uuid'] if 'roadm_uuid' in dev else None - logging.info(f"optical_config_model {obj}") + dev = [roadms.dump() for roadms in self.roadms][0] + obj['interfaces'] = dev.get('interfaces', None) + obj['roadm_uuid'] = dev.get('roadm_uuid', None) return obj diff --git a/src/context/service/database/models/OpticalConfig/RoadmModel.py b/src/context/service/database/models/OpticalConfig/RoadmModel.py index fbdf00f1a..48a921b47 100644 --- a/src/context/service/database/models/OpticalConfig/RoadmModel.py +++ b/src/context/service/database/models/OpticalConfig/RoadmModel.py @@ -12,13 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json , logging -from sqlalchemy import Column, String, Integer , ForeignKey, Boolean , Float -from sqlalchemy.dialects.postgresql import ARRAY +import logging +from sqlalchemy import Column, String, Integer, ForeignKey, Float from sqlalchemy.orm import relationship from context.service.database.models._Base import _Base -class RoadmTypeModel (_Base): +class RoadmTypeModel(_Base): __tablename__ = 'roadm_type' roadm_uuid = Column(String, primary_key=True) circuits = Column(String, nullable=True) @@ -33,8 +32,6 @@ class RoadmTypeModel (_Base): } def dump (self): - i=[interface.dump() for interface in self.interfaces] - logging.info(f"roamd_type_model {i}") return { "channels" : [channel.dump() for channel in self.channels], "roadm_uuid" : self.dump_id(), @@ -55,8 +52,7 @@ class ChannelModel(_Base): type = Column(String, nullable=False) optical_band_parent = Column(String, nullable=True) roadm_uuid = Column(ForeignKey('roadm_type.roadm_uuid', ondelete='CASCADE'), nullable=False) - - roadm = relationship('RoadmTypeModel',back_populates='channels') + roadm = relationship('RoadmTypeModel',back_populates='channels') # opticalconfig_uuid = Column(ForeignKey('optical_config.opticalconfig_uuid', ondelete='CASCADE'), primary_key=True) # opticalconfig = relationship('OpticalConfigModel', back_populates='channels') @@ -78,38 +74,35 @@ class ChannelModel(_Base): "optical_band_parent" : self.optical_band_parent, "channel_index" : self.channel_index, } - + class ORInterfaceModel (_Base): __tablename__ = 'open_roadm_interface' - interface_uuid = Column(String, primary_key=True) - name = Column(String, nullable=False ,unique=True ) - type = Column(String , nullable=True) - administrative_state = Column(String,nullable=True) - circuit_pack_name = Column(String,nullable=True) - port = Column(String,nullable=True) - interface_list = Column(String ,nullable=True) - frequency = Column(Float ,nullable=True) - width = Column(Integer ,nullable=True) - - roadm_uuid = Column(ForeignKey('roadm_type.roadm_uuid', ondelete='CASCADE' ),nullable=False) - roadm = relationship('RoadmTypeModel',back_populates='interfaces') - + interface_uuid = Column(String, primary_key = True) + name = Column(String, nullable = False, unique = True) + type = Column(String, nullable = True) + administrative_state = Column(String, nullable = True) + circuit_pack_name = Column(String, nullable = True) + port = Column(String, nullable = True) + interface_list = Column(String, nullable = True) + frequency = Column(Float, nullable = True) + width = Column(Integer, nullable = True) + roadm_uuid = Column(ForeignKey('roadm_type.roadm_uuid', ondelete='CASCADE'), nullable=False) + roadm = relationship('RoadmTypeModel', back_populates='interfaces') + def dump_id (self ): return { - "interface_uuid":self.interface_uuid + "interface_uuid": self.interface_uuid } def dump(self): return { - "name" : self.name, - "type" : self.type, - "administrative_state" : self.administrative_state, - "circuit_pack_name" : self.circuit_pack_name, - "port" : self.port, - "interface_list" : self.interface_list, - - "frequency" : self.frequency, - "width" : self.width - + "name" : self.name, + "type" : self.type, + "administrative_state": self.administrative_state, + "circuit_pack_name" : self.circuit_pack_name, + "port" : self.port, + "interface_list" : self.interface_list, + "frequency" : self.frequency, + "width" : self.width } diff --git a/src/device/service/Tools.py b/src/device/service/Tools.py index f68f9bffa..87a9b535c 100644 --- a/src/device/service/Tools.py +++ b/src/device/service/Tools.py @@ -478,26 +478,27 @@ def is_key_existed(key : str, keys_dic = dict, key_name_to_use = None) -> dict: def extract_resources(config : dict, device : Device) -> list[list[dict], dict]: conditions = {} - resources:list[dict] = [] + resources : list[dict] = [] is_opticalband = config.get('is_opticalband', False) conditions['edit_type'] = get_edit_target(device, is_opticalband) if device.device_type == DeviceTypeEnum.OPEN_ROADM._value_ : - ports_dic=is_key_existed('ports',keys_dic=config['new_config']) - interfaces_list=[] - config_type= is_key_existed('config_type', keys_dic=config['new_config']) + ports_dic = is_key_existed('ports',keys_dic=config['new_config']) + interfaces_list = [] + config_type = is_key_existed('config_type', keys_dic=config['new_config']) resources.append(config_type) resources.append(is_key_existed('administrative-state', keys_dic=config['new_config'])) resources.append(is_key_existed('frequency', keys_dic=config['new_config'])) resources.append(is_key_existed('width', keys_dic=config['new_config'])) for port in ports_dic["value"]: - circuit_pack_dic=is_key_existed('supporting-circuit-pack-name', keys_dic=port) - interface_list=is_key_existed('supporting-interface-list', keys_dic=port) - supporting_port=is_key_existed('supporting-port', keys_dic=port) + circuit_pack_dic = is_key_existed('supporting-circuit-pack-name', keys_dic=port) + interface_list = is_key_existed('supporting-interface-list', keys_dic=port) + supporting_port = is_key_existed('supporting-port', keys_dic=port) interfaces_list.append([ - circuit_pack_dic, - interface_list, - supporting_port]) + circuit_pack_dic, + interface_list, + supporting_port + ]) resources.append({'resource_key':'interfaces','value':interfaces_list}) else : resources.append(is_key_existed('channel_namespace', config)) @@ -508,7 +509,7 @@ def extract_resources(config : dict, device : Device) -> list[list[dict], dict]: #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 @@ -521,10 +522,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 @@ -532,12 +533,12 @@ def extract_resources(config : dict, device : Device) -> list[list[dict], dict]: resources.append(is_key_existed('frequency', keys_dic=config['new_config'])) resources.append(is_key_existed('operational-mode', keys_dic=config['new_config'])) resources.append(is_key_existed('line-port', keys_dic=config['new_config'])) - resources.append(is_key_existed('status', keys_dic=config['new_config'] , key_name_to_use="admin-state")) + resources.append(is_key_existed('status', keys_dic=config['new_config'], key_name_to_use="admin-state")) resources.append(is_key_existed('band_type', keys_dic=config['new_config'], key_name_to_use='name')) 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) diff --git a/src/device/service/drivers/oc_driver/OCDriver.py b/src/device/service/drivers/oc_driver/OCDriver.py index b75d627b4..e93db2812 100644 --- a/src/device/service/drivers/oc_driver/OCDriver.py +++ b/src/device/service/drivers/oc_driver/OCDriver.py @@ -168,8 +168,7 @@ def edit_config( str_config_messages = edit_optical_channel(resources) elif (conditions['edit_type']=='optical-band'): #roadm optical-band - str_config_messages = create_optical_band(resources) - + str_config_messages = create_optical_band(resources) elif (conditions['edit_type']=='network-media-channel'): commit_per_rule=True #openroadm network media channel @@ -265,20 +264,20 @@ class OCDriver(_Driver): data_xml=xml_data, resource_keys=transponder_filter_fields, dic=config ) transceivers, optical_channels_params, channel_namespace, endpoints, ports_result = extracted_values - oc_values["channels"] = optical_channels_params - oc_values["transceivers"] = transceivers + oc_values["channels" ] = optical_channels_params + oc_values["transceivers" ] = transceivers oc_values["channel_namespace"] = channel_namespace - oc_values["endpoints"] = endpoints - oc_values["ports"] = ports_result + oc_values["endpoints" ] = endpoints + oc_values["ports" ] = ports_result elif (self.__type =='openroadm'): extracted_values=openroadm_values_extractor(data_xml=xml_data, resource_keys=[], dic=oc_values) ports_result = extracted_values[1] - oc_values['interfaces']=extracted_values[0]['interfaces'] + oc_values['interfaces'] = extracted_values[0]['interfaces'] else: extracted_values = roadm_values_extractor(data_xml=xml_data, resource_keys=[], dic=config) ports_result = extracted_values[0] - oc_values['optical_bands']=extracted_values[1] - oc_values['media_channels']=extracted_values[2] + oc_values['optical_bands' ] = extracted_values[1] + oc_values['media_channels'] = extracted_values[2] #results.append((resource_key, e)) # if validation fails, store the exception diff --git a/src/tests/ofc24/deploy_specs.sh b/src/tests/ofc24/deploy_specs.sh index a303537bf..4cd0e7fa0 100755 --- a/src/tests/ofc24/deploy_specs.sh +++ b/src/tests/ofc24/deploy_specs.sh @@ -110,7 +110,7 @@ export CRDB_DEPLOY_MODE="single" export CRDB_DROP_DATABASE_IF_EXISTS="YES" # Disable flag for re-deploying CockroachDB from scratch. -export CRDB_REDEPLOY="YES" +export CRDB_REDEPLOY="" # ----- NATS ------------------------------------------------------------------- -- GitLab