diff --git a/src/context/service/ContextServiceServicerImpl.py b/src/context/service/ContextServiceServicerImpl.py index 8b14723de6e2259885361a131e21d77a07fc4179..09d16b0f8a80f956a32f7e074e3bbe5582b3b26c 100644 --- a/src/context/service/ContextServiceServicerImpl.py +++ b/src/context/service/ContextServiceServicerImpl.py @@ -24,7 +24,7 @@ from common.proto.context_pb2 import ( Service, ServiceEvent, ServiceFilter, ServiceId, ServiceIdList, ServiceList, Slice, SliceEvent, SliceFilter, SliceId, SliceIdList, SliceList, Topology, TopologyDetails, TopologyEvent, TopologyId, TopologyIdList, TopologyList, - OpticalConfigList, OpticalConfigId, OpticalConfig ,OpticalLink , OpticalLinkList, + OpticalConfigList, OpticalConfigId, OpticalConfig, OpticalLink, OpticalLinkList, ServiceConfigRule ) from common.proto.policy_pb2 import PolicyRuleIdList, PolicyRuleId, PolicyRuleList, PolicyRule @@ -32,24 +32,42 @@ from common.proto.context_pb2_grpc import ContextServiceServicer from common.proto.context_policy_pb2_grpc import ContextPolicyServiceServicer from common.method_wrappers.Decorator import MetricsPool, safe_and_metered_rpc_method from .database.Connection import ( - connection_delete, connection_get, connection_list_ids, connection_list_objs, connection_set) -from .database.Context import context_delete, context_get, context_list_ids, context_list_objs, context_set -from .database.Device import device_delete, device_get, device_list_ids, device_list_objs, device_select, device_set + connection_delete, connection_get, connection_list_ids, connection_list_objs, connection_set +) +from .database.Context import ( + context_delete, context_get, context_list_ids, context_list_objs, context_set +) +from .database.Device import ( + device_delete, device_get, device_list_ids, device_list_objs, device_select, device_set +) from .database.EndPoint import endpoint_list_names from .database.Events import EventTopicEnum, consume_events -from .database.Link import link_delete, link_get, link_list_ids, link_list_objs, link_set +from .database.Link import ( + link_delete, link_get, link_list_ids, link_list_objs, link_set +) from .database.PolicyRule import ( - policyrule_delete, policyrule_get, policyrule_list_ids, policyrule_list_objs, policyrule_set) + policyrule_delete, policyrule_get, policyrule_list_ids, policyrule_list_objs, + policyrule_set +) from .database.Service import ( - service_delete, service_get, service_list_ids, service_list_objs, service_select, service_set, service_unset) + service_delete, service_get, service_list_ids, service_list_objs, service_select, + service_set, service_unset +) from .database.Slice import ( - slice_delete, slice_get, slice_list_ids, slice_list_objs, slice_select, slice_set, slice_unset) + slice_delete, slice_get, slice_list_ids, slice_list_objs, slice_select, + slice_set, slice_unset +) from .database.Topology import ( - topology_delete, topology_get, topology_get_details, topology_list_ids, topology_list_objs, topology_set) -from .database.OpticalConfig import (set_opticalconfig, select_opticalconfig, get_opticalconfig - ,delete_opticalconfig ,update_opticalconfig ,delete_opticalchannel + topology_delete, topology_get, topology_get_details, topology_list_ids, + topology_list_objs, topology_set +) +from .database.OpticalConfig import ( + set_opticalconfig, select_opticalconfig, get_opticalconfig, delete_opticalconfig, + update_opticalconfig, delete_opticalchannel +) +from .database.OpticalLink import ( + optical_link_delete, optical_link_get, optical_link_list_objs, optical_link_set ) -from .database.OpticalLink import optical_link_delete,optical_link_get,optical_link_list_objs,optical_link_set from .database.ConfigRule import delete_config_rule LOGGER = logging.getLogger(__name__) @@ -315,7 +333,7 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer def SetOpticalConfig(self, request : OpticalConfig, context : grpc.ServicerContext) -> OpticalConfigId: result = set_opticalconfig(self.db_engine, request) return OpticalConfigId(**result) - + @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def UpdateOpticalConfig(self, request : OpticalConfig, context : grpc.ServicerContext) -> OpticalConfigId: result = update_opticalconfig(self.db_engine, request) @@ -329,21 +347,19 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer optical_config_id.CopyFrom(result.opticalconfig_id) device_id.CopyFrom(result.device_id) return OpticalConfig(config=result.config, opticalconfig_id=optical_config_id , device_id=device_id) - + @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) - def DeleteOpticalConfig (self, request : OpticalConfigId, context : grpc.ServicerContext) -> Empty: - delete_opticalconfig(self.db_engine,self.messagebroker, request) - + def DeleteOpticalConfig(self, request : OpticalConfigId, context : grpc.ServicerContext) -> Empty: + delete_opticalconfig(self.db_engine, self.messagebroker, request) return Empty() + @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) - def DeleteOpticalChannel (self, request : OpticalConfig, context : grpc.ServicerContext) -> Empty: - delete_opticalchannel(self.db_engine,self.messagebroker, request) - + def DeleteOpticalChannel(self, request : OpticalConfig, context : grpc.ServicerContext) -> Empty: + delete_opticalchannel(self.db_engine, self.messagebroker, request) return Empty() - #--------------------- Experimental Optical Link ------------------- - + @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def GetOpticalLinkList(self, request : Empty, context : grpc.ServicerContext) -> OpticalLinkList: return optical_link_list_objs(self.db_engine) @@ -360,8 +376,6 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer def DeleteOpticalLink(self, request : LinkId, context : grpc.ServicerContext) -> Empty: return optical_link_delete(self.db_engine, self.messagebroker, request) - @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def DeleteServiceConfigRule(self, request : ServiceConfigRule, context : grpc.ServicerContext) -> Empty: - return delete_config_rule(self.db_engine, request) diff --git a/src/context/service/database/OpticalConfig.py b/src/context/service/database/OpticalConfig.py index 5824d858aa958cea627623242d61265f0b0450ad..a5791fc0eea166afd8a41a7bef2b035977ffe8ce 100644 --- a/src/context/service/database/OpticalConfig.py +++ b/src/context/service/database/OpticalConfig.py @@ -19,13 +19,13 @@ from common.DeviceTypes import DeviceTypeEnum from sqlalchemy.engine import Engine from sqlalchemy.orm import Session, sessionmaker from sqlalchemy_cockroachdb import run_transaction -from common.proto.context_pb2 import OpticalConfig, OpticalConfigId , Empty , EventTypeEnum -from .models.OpticalConfig.OpticalConfigModel import OpticalConfigModel -from .models.OpticalConfig.TransponderModel import TransponderTypeModel ,OpticalChannelModel +from common.proto.context_pb2 import OpticalConfig, OpticalConfigId, Empty, EventTypeEnum +from .models.OpticalConfig.OpticalConfigModel import OpticalConfigModel +from .models.OpticalConfig.TransponderModel import TransponderTypeModel, OpticalChannelModel from .models.OpticalConfig.RoadmModel import RoadmTypeModel, ChannelModel from context.service.database.uuids.OpticalConfig import ( - channel_get_uuid , opticalconfig_get_uuid ,transponder_get_uuid,roadm_get_uuid - ) + channel_get_uuid, opticalconfig_get_uuid, transponder_get_uuid, roadm_get_uuid +) from .Events import notify_event_opticalconfig LOGGER = logging.getLogger(__name__) @@ -34,14 +34,11 @@ def get_opticalconfig(db_engine : Engine): def callback(session:Session): optical_configs = list() results = session.query(OpticalConfigModel).all() - for obj in results: - - optical_config = OpticalConfig() optical_config.config = json.dumps(obj.dump()) ids_obj = obj.dump_id() - + optical_config.opticalconfig_id.opticalconfig_uuid = ids_obj["opticalconfig_uuid"] optical_config.device_id.device_uuid.uuid=ids_obj["device_uuid"] optical_configs.append(optical_config) @@ -50,139 +47,111 @@ def get_opticalconfig(db_engine : Engine): return obj def set_opticalconfig(db_engine : Engine, request : OpticalConfig): - opticalconfig_id = OpticalConfigId() device_id = request.device_id 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) - + if request.config: config = json.loads(request.config) if 'channel_namespace' in config: - channel_namespace=config['channel_namespace'] + channel_namespace=config['channel_namespace'] if "type" in config: - config_type= config["type"] if config_type == DeviceTypeEnum.OPTICAL_TRANSPONDER._value_: - is_transpondre=True + is_transpondre = True transceivers = [] if channel_namespace is None and 'channel_namespace' in config: channel_namespace=config['channel_namespace'] - - if 'transceivers' in config and len(config['transceivers']['transceiver']) > 0: transceivers = [transceiver for transceiver in config ['transceivers']['transceiver']] - if 'channels' in config and len(config['channels']) > 0: #channels = [channel['name']['index'] for channel in config['channels']] - for channel_params in config['channels']: - channels.append( - { - # "opticalconfig_uuid":opticalconfig_uuid, - "transponder_uuid" : transponder_get_uuid(device_id), - "channel_uuid" : channel_get_uuid(channel_params['name']['index'],device_uuid), - "channel_name" : channel_params['name']['index'], - "frequency" : int(channel_params["frequency"]) if "frequency" in channel_params else 0, - "operational_mode" : int(channel_params["operational-mode"]) if "operational-mode" in channel_params else 0, - "target_output_power" : channel_params["target-output-power"] if "target-output-power" in channel_params else '', - "status" : channel_params["status"] if "status" in channel_params else "" - } - ) - - transponder.append({ - "transponder_uuid":transponder_get_uuid(device_id), - "transcievers":transceivers, - "interfaces":None, - "opticalconfig_uuid":opticalconfig_uuid, - - + channels.append({ + # "opticalconfig_uuid":opticalconfig_uuid, + "transponder_uuid" : transponder_get_uuid(device_id), + "channel_uuid" : channel_get_uuid(channel_params['name']['index'],device_uuid), + "channel_name" : channel_params['name']['index'], + "frequency" : int(channel_params["frequency"]) if "frequency" in channel_params else 0, + "operational_mode" : int(channel_params["operational-mode"]) if "operational-mode" in channel_params else 0, + "target_output_power": channel_params["target-output-power"] if "target-output-power" in channel_params else '', + "status" : channel_params["status"] if "status" in channel_params else "" + }) + transponder.append({ + "transponder_uuid" : transponder_get_uuid(device_id), + "transcievers" : transceivers, + "interfaces" : None, + "opticalconfig_uuid": opticalconfig_uuid, }) - + if config_type == DeviceTypeEnum.OPTICAL_ROADM._value_: - - if channel_namespace is None and 'channel_namespace' in config: channel_namespace=config['channel_namespace'] - - - - if 'media_channels' in config and len(config['media_channels']) > 0: #channels = [channel['name']['index'] for channel in config['channels']] - channel_num=0 + channel_num = 0 for channel_params in config['media_channels']: - channel_index=channel_params['channel_index'] if channel_params['channel_index'] is not None else None - channels.append( - { - # "opticalconfig_uuid":opticalconfig_uuid, - "roadm_uuid" : roadm_get_uuid(device_id), - "channel_uuid" : channel_get_uuid(f'media_channel_{channel_index}',device_uuid), - "band_name" : channel_params['band_name'], - "lower_frequency" : int(channel_params["lower_frequency"]) if "lower_frequency" in channel_params else 0, - "upper_frequency" : int(channel_params["upper_frequency"]) if "upper_frequency" in channel_params else 0, - "dest_port" : channel_params["dest_port"] if "dest_port" in channel_params else '', - "src_port" : channel_params["src_port"] if "src_port" in channel_params else '', - "status" : channel_params["status"] if "status" in channel_params else "", - "type" : 'media_channel', - "optical_band_parent":str(channel_params['optical_band_parent']) if 'optical_band_parent' in channel_params else None, - "channel_index" : channel_index if channel_index is not None else None, - } - ) + channel_index = channel_params['channel_index'] if channel_params['channel_index'] is not None else None + channels.append({ + # "opticalconfig_uuid":opticalconfig_uuid, + "roadm_uuid" : roadm_get_uuid(device_id), + "channel_uuid" : channel_get_uuid(f'media_channel_{channel_index}',device_uuid), + "band_name" : channel_params['band_name'], + "lower_frequency" : int(channel_params["lower_frequency"]) if "lower_frequency" in channel_params else 0, + "upper_frequency" : int(channel_params["upper_frequency"]) if "upper_frequency" in channel_params else 0, + "dest_port" : channel_params["dest_port"] if "dest_port" in channel_params else '', + "src_port" : channel_params["src_port"] if "src_port" in channel_params else '', + "status" : channel_params["status"] if "status" in channel_params else "", + "type" : 'media_channel', + "optical_band_parent" : str(channel_params['optical_band_parent']) if 'optical_band_parent' in channel_params else None, + "channel_index" : channel_index if channel_index is not None else None, + }) if 'optical_bands' in config and len(config['optical_bands']) > 0: #channels = [channel['name']['index'] for channel in config['channels']] - channel_num=0 + channel_num = 0 for channel_params in config['optical_bands']: - channel_num+=1 - channels.append( - { - # "opticalconfig_uuid":opticalconfig_uuid, - "roadm_uuid" : roadm_get_uuid(device_id), - "channel_uuid" : channel_get_uuid(f'optical_bands_{channel_num}',device_uuid), - "band_name" : channel_params['band_name'], - "lower_frequency" : int(channel_params["lower_frequency"]) if "lower_frequency" in channel_params else 0, - "upper_frequency" : int(channel_params["upper_frequency"]) if "upper_frequency" in channel_params else 0, - "dest_port" : channel_params["dest_port"] if "dest_port" in channel_params else '', - "src_port" : channel_params["src_port"] if "src_port" in channel_params else '', - "status" : channel_params["status"] if "status" in channel_params else "", - "type" : 'optical_band', - "channel_index" : channel_params['channel_index'] if 'channel_index' in channel_params else None, - "optical_band_parent":None - - } - ) - + channel_num += 1 + channels.append({ + # "opticalconfig_uuid":opticalconfig_uuid, + "roadm_uuid" : roadm_get_uuid(device_id), + "channel_uuid" : channel_get_uuid(f'optical_bands_{channel_num}',device_uuid), + "band_name" : channel_params['band_name'], + "lower_frequency" : int(channel_params["lower_frequency"]) if "lower_frequency" in channel_params else 0, + "upper_frequency" : int(channel_params["upper_frequency"]) if "upper_frequency" in channel_params else 0, + "dest_port" : channel_params["dest_port"] if "dest_port" in channel_params else '', + "src_port" : channel_params["src_port"] if "src_port" in channel_params else '', + "status" : channel_params["status"] if "status" in channel_params else "", + "type" : 'optical_band', + "channel_index" : channel_params['channel_index'] if 'channel_index' in channel_params else None, + "optical_band_parent": None + }) + roadms.append({ - "roadm_uuid":roadm_get_uuid(device_id), - "opticalconfig_uuid":opticalconfig_uuid, + "roadm_uuid" : roadm_get_uuid(device_id), + "opticalconfig_uuid" : opticalconfig_uuid, }) - - - OpticalConfig_data.append( - { - "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 - } - ) - + + OpticalConfig_data.append({ + "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 + }) def callback(session:Session)->bool: stmt = insert(OpticalConfigModel).values(OpticalConfig_data) - stmt = stmt.on_conflict_do_update( index_elements=[OpticalConfigModel.opticalconfig_uuid], set_=dict( @@ -194,7 +163,6 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig): if config_type == DeviceTypeEnum.OPTICAL_TRANSPONDER._value_: if (len(transponder)>0): stmt = insert(TransponderTypeModel).values(transponder) - stmt = stmt.on_conflict_do_update( index_elements=[TransponderTypeModel.transponder_uuid], set_=dict( @@ -204,30 +172,24 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig): ) stmt = stmt.returning(TransponderTypeModel.transponder_uuid) transponder_id = session.execute(stmt).fetchone() - - if (len(channels)>0) : - stmt = insert(OpticalChannelModel).values(channels) - - stmt = stmt.on_conflict_do_update( - index_elements=[OpticalChannelModel.channel_uuid ], - set_=dict( - channel_name= stmt.excluded.channel_name , - frequency = stmt.excluded.frequency, - operational_mode=stmt.excluded.operational_mode, + if (len(channels) > 0): + stmt = insert(OpticalChannelModel).values(channels) + stmt = stmt.on_conflict_do_update( + 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, - - ) - ) - stmt = stmt.returning(OpticalChannelModel.channel_uuid) - opticalChannel_id = session.execute(stmt).fetchone() - - + ) + stmt = stmt.returning(OpticalChannelModel.channel_uuid) + opticalChannel_id = session.execute(stmt).fetchone() + if config_type == DeviceTypeEnum.OPTICAL_ROADM._value_: - if (len(roadms)>0): + if len(roadms) > 0: stmt = insert(RoadmTypeModel).values(roadms) - stmt = stmt.on_conflict_do_update( index_elements=[RoadmTypeModel.roadm_uuid], set_=dict( @@ -236,38 +198,30 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig): ) stmt = stmt.returning(RoadmTypeModel.roadm_uuid) roadm_id = session.execute(stmt).fetchone() - - if (channels is not None and len(channels)>0) : - stmt = insert(ChannelModel).values(channels) - - 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 - - ) - + if (channels is not None and len(channels) > 0): + stmt = insert(ChannelModel).values(channels) + 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 ) - stmt = stmt.returning(ChannelModel.channel_uuid) - opticalChannel_id = session.execute(stmt).fetchone() - - - + ) + stmt = stmt.returning(ChannelModel.channel_uuid) + opticalChannel_id = session.execute(stmt).fetchone() + opticalconfig_id = run_transaction(sessionmaker(bind=db_engine), callback) return {'opticalconfig_uuid': opticalconfig_id} - def update_opticalconfig(db_engine : Engine, request : OpticalConfig): - opticalconfig_id = OpticalConfigId() device_id = request.device_id device_uuid = request.device_id.device_uuid.uuid @@ -279,22 +233,20 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig): config_type=None #is_transpondre=False opticalconfig_uuid =opticalconfig_get_uuid(device_id) - + if request.config : config = json.loads(request.config) - + if 'new_config' in config: if 'type' in config: - config_type=config['type'] + config_type = config['type'] if "type" in config['new_config'] and config_type is None: - - config_type= config['new_config']["type"] + config_type = config['new_config']["type"] if 'channel_namespace' in config['new_config']: - channel_namespace=config['new_config'] ['channel_namespace'] - - + channel_namespace = config['new_config'] ['channel_namespace'] + if config_type == DeviceTypeEnum.OPTICAL_TRANSPONDER._value_: - is_transpondre=True + is_transpondre = True transceivers = [] if channel_namespace is None and 'channel_namespace' in config: channel_namespace=config['channel_namespace'] @@ -306,135 +258,113 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig): #channels = [channel['name']['index'] for channel in config['channels']] for channel_params in config['new_config']['channels']: - channels.append( - { - # "opticalconfig_uuid":opticalconfig_uuid, - "transponder_uuid" : transponder_get_uuid(device_id), - "channel_uuid" : channel_get_uuid(channel_params['name']['index'],device_uuid), - "channel_name" : channel_params['name']['index'], - "frequency" : int(channel_params["frequency"]) if "frequency" in channel_params else 0, - "operational_mode" : int(channel_params["operational-mode"]) if "operational-mode" in channel_params else 0, - "target_output_power" : channel_params["target-output-power"] if "target-output-power" in channel_params else '', - "status" : channel_params["status"] if "status" in channel_params else "" - } - ) + channels.append({ + # "opticalconfig_uuid":opticalconfig_uuid, + "transponder_uuid" : transponder_get_uuid(device_id), + "channel_uuid" : channel_get_uuid(channel_params['name']['index'],device_uuid), + "channel_name" : channel_params['name']['index'], + "frequency" : int(channel_params["frequency"]) if "frequency" in channel_params else 0, + "operational_mode" : int(channel_params["operational-mode"]) if "operational-mode" in channel_params else 0, + "target_output_power" : channel_params["target-output-power"] if "target-output-power" in channel_params else '', + "status" : channel_params["status"] if "status" in channel_params else "" + }) elif 'flow_handled' in config and 'new_config' in config : - - target_config = config['new_config'] - dest_pair=None - src=None - dst=None - src_pair=config['flow_handled'][0] - - src,dst=src_pair - if src_pair is None and len(config['flow_handled'])>1 : - dest_pair=config['flow_handled'][1] - src,dst=dest_pair - channel_index=src if src is not None and src !='0' else dst - channel_name=f"channel-{channel_index}" - channels.append( - { - # "opticalconfig_uuid":opticalconfig_uuid, - "transponder_uuid" : transponder_get_uuid(device_id), - "channel_uuid" : channel_get_uuid(channel_name,device_uuid), - "channel_name" :channel_name, - "frequency" : int(target_config["frequency"]) if "frequency" in target_config else 0, - "operational_mode" : int(target_config["operational-mode"]) if "operational-mode" in target_config else 0, - "target_output_power" : target_config["target-output-power"] if "target-output-power" in target_config else '', - "status" : target_config["status"] if "status" in target_config else "" - } - ) - + target_config = config['new_config'] + dest_pair = None + src = None + dst = None + src_pair = config['flow_handled'][0] + + src, dst = src_pair + if src_pair is None and len(config['flow_handled'])>1 : + dest_pair = config['flow_handled'][1] + src, dst = dest_pair + channel_index = src if src is not None and src !='0' else dst + channel_name = f"channel-{channel_index}" + channels.append({ + # "opticalconfig_uuid":opticalconfig_uuid, + "transponder_uuid" : transponder_get_uuid(device_id), + "channel_uuid" : channel_get_uuid(channel_name,device_uuid), + "channel_name" : channel_name, + "frequency" : int(target_config["frequency"]) if "frequency" in target_config else 0, + "operational_mode" : int(target_config["operational-mode"]) if "operational-mode" in target_config else 0, + "target_output_power" : target_config["target-output-power"] if "target-output-power" in target_config else '', + "status" : target_config["status"] if "status" in target_config else "" + }) + transponder.append({ - "transponder_uuid":transponder_get_uuid(device_id), - "transcievers":transceivers, - "interfaces":None, - "opticalconfig_uuid":opticalconfig_uuid, + "transponder_uuid" : transponder_get_uuid(device_id), + "transcievers" : transceivers, + "interfaces" : None, + "opticalconfig_uuid": opticalconfig_uuid, }) - - + if config_type == DeviceTypeEnum.OPTICAL_ROADM._value_: - if channel_namespace is None and 'channel_namespace' in config['new_config']: channel_namespace=config['new_config']['channel_namespace'] - if 'is_opticalband' in config and not config['is_opticalband']: #channels = [channel['name']['index'] for channel in config['channels']] if 'flow_handled' in config and len(config['flow_handled'])>0 : - num=0 - flow_id=config["new_config"]["flow_id"] if 'flow_id' in config['new_config'] else None + num = 0 + flow_id = config["new_config"]["flow_id"] if 'flow_id' in config['new_config'] else None for flow in config['flow_handled']: - src_port,dest_port=flow - channel_index=flow_id+num - num+=1 - channels.append( - { - # "opticalconfig_uuid":opticalconfig_uuid, - "roadm_uuid" : roadm_get_uuid(device_id), - "channel_uuid" : channel_get_uuid(f'media_channel_{channel_index}',device_uuid), - "band_name" : config['new_config']['band_type'], - "lower_frequency" : int(int(config['new_config']['frequency']) - (int(config['new_config']['band'])/2)) if "frequency" in config['new_config'] else 0, - "upper_frequency" :int(int(config['new_config']['frequency']) + (int(config['new_config']['band'])/2)) if "frequency" in config['new_config'] else 0, - "dest_port" :dest_port, - "src_port" : src_port, - "status" : config['new_config']["status"] if "status" in config['new_config'] else "", - "type" : 'media_channel', - "optical_band_parent":str( config['new_config']['ob_id']) if 'ob_id' in config['new_config'] else None, - "channel_index":str(channel_index) if channel_index is not None else None - } - ) + src_port, dest_port = flow + channel_index = flow_id + num + num += 1 + channels.append({ + # "opticalconfig_uuid":opticalconfig_uuid, + "roadm_uuid" : roadm_get_uuid(device_id), + "channel_uuid" : channel_get_uuid(f'media_channel_{channel_index}',device_uuid), + "band_name" : config['new_config']['band_type'], + "lower_frequency" : int(int(config['new_config']['frequency']) - (int(config['new_config']['band'])/2)) if "frequency" in config['new_config'] else 0, + "upper_frequency" : int(int(config['new_config']['frequency']) + (int(config['new_config']['band'])/2)) if "frequency" in config['new_config'] else 0, + "dest_port" : dest_port, + "src_port" : src_port, + "status" : config['new_config']["status"] if "status" in config['new_config'] else "", + "type" : 'media_channel', + "optical_band_parent" : str( config['new_config']['ob_id']) if 'ob_id' in config['new_config'] else None, + "channel_index" : str(channel_index) if channel_index is not None else None + }) if 'is_opticalband' in config and config['is_opticalband']: #channels = [channel['name']['index'] for channel in config['channels']] - - - if 'flow_handled' in config and len(config['flow_handled'])>0 : - ob_id =config['new_config']['ob_id'] if 'ob_id' in config['new_config'] else None - num=0 - for flow in config['flow_handled']: - - src_port,dest_port=flow - channel_index=ob_id+num - num+=1 - channels.append( - - { - # "opticalconfig_uuid":opticalconfig_uuid, - "roadm_uuid" : roadm_get_uuid(device_id), - "channel_uuid" : channel_get_uuid(f'optical_bands_{channel_index}',device_uuid), - "band_name" : config['new_config']['band_type'], - "lower_frequency" : int(config['new_config']["low-freq"]) if "low-freq" in config['new_config'] else 0, - "upper_frequency" : int(config['new_config']["up-freq"]) if "up-freq" in config['new_config'] else 0, - "dest_port" :dest_port, - "src_port" : src_port, - "status" : config['new_config']["status"] if "status" in config['new_config'] else "", - "type" : 'optical_band', - "channel_index" :str( channel_index) if channel_index is not None else None - } - ) - + if 'flow_handled' in config and len(config['flow_handled']) > 0: + ob_id = config['new_config']['ob_id'] if 'ob_id' in config['new_config'] else None + num = 0 + for flow in config['flow_handled']: + src_port, dest_port = flow + channel_index = ob_id + num + num += 1 + channels.append({ + # "opticalconfig_uuid":opticalconfig_uuid, + "roadm_uuid" : roadm_get_uuid(device_id), + "channel_uuid" : channel_get_uuid(f'optical_bands_{channel_index}',device_uuid), + "band_name" : config['new_config']['band_type'], + "lower_frequency" : int(config['new_config']["low-freq"]) if "low-freq" in config['new_config'] else 0, + "upper_frequency" : int(config['new_config']["up-freq"]) if "up-freq" in config['new_config'] else 0, + "dest_port" : dest_port, + "src_port" : src_port, + "status" : config['new_config']["status"] if "status" in config['new_config'] else "", + "type" : 'optical_band', + "channel_index" : str( channel_index) if channel_index is not None else None + }) + roadms.append({ - "roadm_uuid":roadm_get_uuid(device_id), - "opticalconfig_uuid":opticalconfig_uuid, - }) - - - OpticalConfig_data.append( - { - "opticalconfig_uuid":opticalconfig_uuid, - # "transcievers" : transceivers, - # "interfaces" :"", - "channel_namespace" : channel_namespace , - "endpoints" : [json.dumps(endpoint) for endpoint in config['new_config'].get("endpoints",[])], - "device_uuid": device_uuid, - "type":config_type - } - ) - + "roadm_uuid" : roadm_get_uuid(device_id), + "opticalconfig_uuid": opticalconfig_uuid, + }) + OpticalConfig_data.append({ + "opticalconfig_uuid": opticalconfig_uuid, + # "transcievers" : transceivers, + # "interfaces" :"", + "channel_namespace" : channel_namespace , + "endpoints" : [json.dumps(endpoint) for endpoint in config['new_config'].get("endpoints",[])], + "device_uuid" : device_uuid, + "type" : config_type + }) def callback(session:Session)->bool: stmt = insert(OpticalConfigModel).values(OpticalConfig_data) - stmt = stmt.on_conflict_do_update( index_elements=[OpticalConfigModel.opticalconfig_uuid], set_=dict( @@ -446,76 +376,62 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig): if config_type == DeviceTypeEnum.OPTICAL_TRANSPONDER._value_: 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() - - if (len(channels)>0) : - stmt = insert(OpticalChannelModel).values(channels) - - stmt = stmt.on_conflict_do_update( - 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, - - ) - + if len(channels) > 0: + stmt = insert(OpticalChannelModel).values(channels) + stmt = stmt.on_conflict_do_update( + 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, ) - stmt = stmt.returning(OpticalChannelModel.channel_uuid) - opticalChannel_id = session.execute(stmt).fetchone() + ) + stmt = stmt.returning(OpticalChannelModel.channel_uuid) + opticalChannel_id = session.execute(stmt).fetchone() if config_type == DeviceTypeEnum.OPTICAL_ROADM._value_: - if (len(roadms)>0): + if len(roadms) > 0: stmt = insert(RoadmTypeModel).values(roadms) - stmt = stmt.on_conflict_do_update( - index_elements=[RoadmTypeModel.roadm_uuid], - set_=dict( - circuits=stmt.excluded.circuits - ) + index_elements=[RoadmTypeModel.roadm_uuid], + set_=dict( + circuits=stmt.excluded.circuits ) + ) stmt = stmt.returning(RoadmTypeModel.roadm_uuid) roadm_id = session.execute(stmt).fetchone() - if (channels is not None and len(channels)>0) : - - stmt = insert(ChannelModel).values(channels) - - 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, - - ) - + if (channels is not None and len(channels) > 0): + stmt = insert(ChannelModel).values(channels) + 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, ) - stmt = stmt.returning(ChannelModel.channel_uuid) - opticalChannel_id = session.execute(stmt).fetchone() - - - + ) + stmt = stmt.returning(ChannelModel.channel_uuid) + opticalChannel_id = session.execute(stmt).fetchone() + opticalconfig_id = run_transaction(sessionmaker(bind=db_engine), callback) return {'opticalconfig_uuid': opticalconfig_id} - -def select_opticalconfig(db_engine:Engine,request:OpticalConfigId): +def select_opticalconfig(db_engine : Engine, request : OpticalConfigId): def callback(session : Session) -> OpticalConfig: result = OpticalConfig() stmt = session.query(OpticalConfigModel) @@ -524,134 +440,101 @@ def select_opticalconfig(db_engine:Engine,request:OpticalConfigId): if obj is not None: result.config = json.dumps(obj.dump()) ids_obj = obj.dump_id() - result.opticalconfig_id.opticalconfig_uuid = ids_obj["opticalconfig_uuid"] result.device_id.device_uuid.uuid=ids_obj["device_uuid"] - return result return run_transaction(sessionmaker(bind=db_engine, expire_on_commit=False), callback) - -def delete_opticalconfig(db_engine : Engine ,messagebroker : MessageBroker, request : OpticalConfigId): +def delete_opticalconfig(db_engine : Engine, messagebroker : MessageBroker, request : OpticalConfigId): opticalconfig_uuid = request.opticalconfig_uuid def callback(session : Session): - query = session.query(OpticalConfigModel) - + #query = session.query(OpticalConfigModel) num_deleted = session.query(OpticalConfigModel).filter_by(opticalconfig_uuid=opticalconfig_uuid).delete() return num_deleted > 0 deleted = run_transaction(sessionmaker(bind=db_engine), callback) - if deleted: - notify_event_opticalconfig(messagebroker, EventTypeEnum.EVENTTYPE_REMOVE, opticalconfig_uuid) - + notify_event_opticalconfig(messagebroker, EventTypeEnum.EVENTTYPE_REMOVE, opticalconfig_uuid) return Empty() -def delete_opticalchannel(db_engine : Engine ,messagebroker : MessageBroker, request : OpticalConfig): +def delete_opticalchannel(db_engine : Engine, messagebroker : MessageBroker, request : OpticalConfig): config = json.loads(request.config) device_id = request.device_id device_uuid = request.device_id.device_uuid.uuid opticalconfig_uuid = request.opticalconfig_id.opticalconfig_uuid - channels=[] - config_type=None + channels = [] + config_type = None if "type" in config : - config_type= config["type"] + config_type= config["type"] if 'new_config' in config: - - if config_type == DeviceTypeEnum.OPTICAL_TRANSPONDER._value_: for flow in config['flow']: - src,dest = flow channel_index= src if src is not None and src!='0' else dest channel_name= f"channel-{channel_index}" - channels.append( - { - # "opticalconfig_uuid":opticalconfig_uuid, - "transponder_uuid" : transponder_get_uuid(device_id), - "channel_uuid" : channel_get_uuid(channel_name ,device_uuid), - "channel_name" :channel_name , - "frequency" : 0, - "operational_mode" : None, - "target_output_power" : None, - "status" :"DISABLED" - } - ) + channels.append({ + # "opticalconfig_uuid":opticalconfig_uuid, + "transponder_uuid" : transponder_get_uuid(device_id), + "channel_uuid" : channel_get_uuid(channel_name ,device_uuid), + "channel_name" : channel_name , + "frequency" : 0, + "operational_mode" : None, + "target_output_power": None, + "status" : "DISABLED" + }) elif config_type == DeviceTypeEnum.OPTICAL_ROADM._value_: - - channel_num=0 + channel_num = 0 if 'flow' in config : if 'is_opticalband' in config: if config['is_opticalband']: - ob_id =config['new_config']['ob_id'] if 'ob_id' in config['new_config'] else None - if len(config['flow']) ==0: - channel_index=ob_id+channel_num - - channel_name=f'optical_bands_{channel_index}' - channels.append( channel_get_uuid(channel_name,device_uuid)) - else : + ob_id = config['new_config']['ob_id'] if 'ob_id' in config['new_config'] else None + if len(config['flow']) == 0: + channel_index = ob_id + channel_num + channel_name = f'optical_bands_{channel_index}' + channels.append(channel_get_uuid(channel_name, device_uuid)) + else: for flow in config['flow']: - channel_index=ob_id+channel_num + channel_index = ob_id+channel_num channel_num +=1 - channel_name=f'optical_bands_{channel_index}' - channels.append( channel_get_uuid(channel_name,device_uuid)) - else : - if config['flow'] ==0: - channel_num=1 - channel_name=f'media_channel_{channel_num}' - channels.append( channel_get_uuid(channel_name,device_uuid)) - else : + channel_name = f'optical_bands_{channel_index}' + channels.append(channel_get_uuid(channel_name, device_uuid)) + else: + if config['flow'] == 0: + channel_num = 1 + channel_name = f'media_channel_{channel_num}' + channels.append(channel_get_uuid(channel_name, device_uuid)) + else: for flow in config['flow']: - channel_num +=1 - channel_name=f'media_channel_{channel_num}' - channels.append( channel_get_uuid(channel_name,device_uuid)) - - else : - return - - - - + channel_num += 1 + channel_name = f'media_channel_{channel_num}' + channels.append(channel_get_uuid(channel_name, device_uuid)) + def callback(session : Session): - all_suceed=[] - if config_type ==DeviceTypeEnum.OPTICAL_ROADM._value_: - query = session.query(ChannelModel) + all_suceed = [] + if config_type == DeviceTypeEnum.OPTICAL_ROADM._value_: for channel_uuid in channels: num_deleted = session.query(ChannelModel).filter_by(channel_uuid=channel_uuid).delete() all_suceed.append(num_deleted > 0) - return all_suceed - elif config_type == DeviceTypeEnum.OPTICAL_TRANSPONDER._value_ : - if (len(channels)>0) : - - stmt = insert(OpticalChannelModel).values(channels) - - stmt = stmt.on_conflict_do_update( - 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 - - ) - + elif config_type == DeviceTypeEnum.OPTICAL_TRANSPONDER._value_: + if len(channels) > 0: + stmt = insert(OpticalChannelModel).values(channels) + stmt = stmt.on_conflict_do_update( + 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 ) - stmt = stmt.returning(OpticalChannelModel.channel_uuid) - opticalChannel_id = session.execute(stmt).fetchone() - all_suceed.append(True) - return all_suceed - else: - return all_suceed - - - all_deleted = run_transaction(sessionmaker(bind=db_engine), callback) - - for stat in all_deleted: - if not stat: - return - notify_event_opticalconfig(messagebroker, EventTypeEnum.EVENTTYPE_REMOVE, opticalconfig_uuid) + ) + stmt = stmt.returning(OpticalChannelModel.channel_uuid) + opticalChannel_id = session.execute(stmt).fetchone() + all_suceed.append(True) + return all_suceed + all_deleted = run_transaction(sessionmaker(bind=db_engine), callback) + for stat in all_deleted: + if not stat: return + notify_event_opticalconfig(messagebroker, EventTypeEnum.EVENTTYPE_REMOVE, opticalconfig_uuid) return Empty() - - \ No newline at end of file diff --git a/src/context/service/database/OpticalLink.py b/src/context/service/database/OpticalLink.py index 1c897b67f92d06e5173a4db347952531b2ac54bf..93f43ef34497cb353f50a9b001da8ec4a6f5f92f 100644 --- a/src/context/service/database/OpticalLink.py +++ b/src/context/service/database/OpticalLink.py @@ -15,28 +15,25 @@ import datetime, logging from sqlalchemy.dialects.postgresql import insert from sqlalchemy.engine import Engine -from sqlalchemy import inspect from sqlalchemy.orm import Session, selectinload, sessionmaker from sqlalchemy_cockroachdb import run_transaction from typing import Dict, List, Optional, Set, Tuple -from common.proto.context_pb2 import Empty, EventTypeEnum, OpticalLink, LinkId, OpticalLinkList, TopologyId +from common.proto.context_pb2 import ( + Empty, EventTypeEnum, OpticalLink, LinkId, OpticalLinkList, TopologyId +) from common.message_broker.MessageBroker import MessageBroker from common.method_wrappers.ServiceExceptions import NotFoundException from common.tools.object_factory.Link import json_link_id -from context.service.database.uuids.Topology import topology_get_uuid from .models.OpticalLinkModel import OpticalLinkModel,OpticalLinkEndPointModel - from .models.TopologyModel import TopologyOpticalLinkModel, TopologyModel -from .uuids.OpticalEndPoint import optical_endpoint_get_uuid -from .uuids.Link import link_get_uuid -from .uuids.OpticalLink import opticaldetail_get_uuid -from .Events import notify_event_context, notify_event_link, notify_event_topology from .uuids.EndPoint import endpoint_get_uuid +from .uuids.Link import link_get_uuid +from .uuids.Topology import topology_get_uuid +from .Events import notify_event_link LOGGER = logging.getLogger(__name__) - def optical_link_list_objs(db_engine : Engine) -> OpticalLinkList: def callback(session : Session) -> List[Dict]: obj_list : List[OpticalLinkModel] = session.query(OpticalLinkModel)\ @@ -70,8 +67,6 @@ def optical_link_set(db_engine : Engine, messagebroker : MessageBroker, request now = datetime.datetime.utcnow() # By default, always add link to default Context/Topology - - topology_uuids : Set[str] = set() related_topologies : List[Dict] = list() _,topology_uuid = topology_get_uuid(TopologyId(), allow_random=False, allow_default=True) @@ -80,9 +75,9 @@ def optical_link_set(db_engine : Engine, messagebroker : MessageBroker, request 'optical_link_uuid' : link_uuid, }) topology_uuids.add(topology_uuid) - + link_endpoints_data : List[Dict] = list() - + for i,endpoint_id in enumerate(request.link_endpoint_ids): endpoint_topology_uuid, endpoint_device_uuid, endpoint_uuid = endpoint_get_uuid( endpoint_id, endpoint_name="", allow_random=True) @@ -92,7 +87,7 @@ def optical_link_set(db_engine : Engine, messagebroker : MessageBroker, request 'endpoint_uuid': endpoint_uuid, }) - + if endpoint_topology_uuid not in topology_uuids: related_topologies.append({ 'topology_uuid': endpoint_topology_uuid, @@ -100,10 +95,6 @@ def optical_link_set(db_engine : Engine, messagebroker : MessageBroker, request }) topology_uuids.add(endpoint_topology_uuid) - - - - optical_link_data = [{ 'opticallink_uuid' : link_uuid, 'name' : link_name, @@ -118,10 +109,8 @@ def optical_link_set(db_engine : Engine, messagebroker : MessageBroker, request "c_slots" : request.optical_details.c_slots , "l_slots" : request.optical_details.l_slots, "s_slots" : request.optical_details.s_slots, - }] - - + def callback(session : Session) -> Tuple[bool, List[Dict]]: stmt = insert(OpticalLinkModel).values(optical_link_data) stmt = stmt.on_conflict_do_update( @@ -143,11 +132,7 @@ def optical_link_set(db_engine : Engine, messagebroker : MessageBroker, request updated = updated_at > created_at updated_endpoints = False - - - if len(link_endpoints_data) > 0: - stmt = insert(OpticalLinkEndPointModel).values(link_endpoints_data) stmt = stmt.on_conflict_do_nothing( index_elements=[OpticalLinkEndPointModel.link_uuid, OpticalLinkEndPointModel.endpoint_uuid] @@ -174,8 +159,6 @@ def optical_link_set(db_engine : Engine, messagebroker : MessageBroker, request link_topologies : List[TopologyModel] = query.all() link_topology_ids = [obj.dump_id() for obj in link_topologies] #LOGGER.warning('link_topology_ids={:s}'.format(str(link_topology_ids))) - - return updated or updated_endpoints updated = run_transaction(sessionmaker(bind=db_engine), callback ) @@ -187,10 +170,8 @@ def optical_link_set(db_engine : Engine, messagebroker : MessageBroker, request def optical_link_delete(db_engine : Engine, messagebroker : MessageBroker, request : LinkId) -> Empty: link_uuid = link_get_uuid(request, allow_random=False) - + def callback(session : Session) -> bool: - - num_deleted = session.query(OpticalLinkModel).filter_by(opticallink_uuid=link_uuid).delete() return num_deleted > 0 deleted = run_transaction(sessionmaker(bind=db_engine), callback) diff --git a/src/device/service/DeviceServiceServicerImpl.py b/src/device/service/DeviceServiceServicerImpl.py index 347bbcdbb8d4a5cf95e73a845d9a6c9f697d8a29..6e6fa6a9311751888d51e053e323a63d810dad4e 100644 --- a/src/device/service/DeviceServiceServicerImpl.py +++ b/src/device/service/DeviceServiceServicerImpl.py @@ -33,9 +33,11 @@ from .driver_api.DriverInstanceCache import DriverInstanceCache, get_driver from .monitoring.MonitoringLoops import MonitoringLoops from .ErrorMessages import ERROR_MISSING_DRIVER, ERROR_MISSING_KPI from .Tools import ( - check_connect_rules, check_no_endpoints, compute_rules_to_add_delete, configure_rules, deconfigure_rules, - get_device_controller_uuid, populate_config_rules, populate_endpoint_monitoring_resources, populate_endpoints, - populate_initial_config_rules, subscribe_kpi, unsubscribe_kpi, update_endpoints) + check_connect_rules, check_no_endpoints, compute_rules_to_add_delete, configure_rules, + deconfigure_rules, get_device_controller_uuid, populate_config_rules, + populate_endpoint_monitoring_resources, populate_endpoints, populate_initial_config_rules, + subscribe_kpi, unsubscribe_kpi, update_endpoints +) LOGGER = logging.getLogger(__name__) @@ -56,13 +58,10 @@ class DeviceServiceServicerImpl(DeviceServiceServicer): @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def AddDevice(self, request : Device, context : grpc.ServicerContext) -> DeviceId: t0 = time.time() - is_optical_device = request.device_drivers[0] == DeviceDriverEnum.DEVICEDRIVER_OC + device_uuid = request.device_id.device_uuid.uuid connection_config_rules = check_connect_rules(request.device_config) - - - check_no_endpoints(request.device_endpoints) t1 = time.time() @@ -111,22 +110,22 @@ class DeviceServiceServicerImpl(DeviceServiceServicer): new_sub_links : Dict[str, Link] = dict() #----- Experimental ------------ - new_optial_configs: Dict[str , OpticalConfig]= dict() + new_optical_configs : Dict[str, OpticalConfig] = dict() if len(device.device_endpoints) == 0: t5 = time.time() # created from request, populate endpoints using driver errors.extend(populate_endpoints( - device, driver, self.monitoring_loops, new_sub_devices, new_sub_links,new_optial_configs)) + device, driver, self.monitoring_loops, new_sub_devices, new_sub_links, + new_optical_configs + )) t6 = time.time() t_pop_endpoints = t6 - t5 else: t_pop_endpoints = None - - - - if len(device.device_config.config_rules) == len(connection_config_rules) and not is_optical_device : - + + is_optical_device = request.device_drivers[0] == DeviceDriverEnum.DEVICEDRIVER_OC + if len(device.device_config.config_rules) == len(connection_config_rules) and not is_optical_device: # created from request, populate config rules using driver t7 = time.time() errors.extend(populate_config_rules(device, driver)) @@ -151,16 +150,15 @@ class DeviceServiceServicerImpl(DeviceServiceServicer): else: # ZTP is not deployed; assume the device is ready while onboarding and set them as enabled. device.device_operational_status = DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_ENABLED - - # temporary line - if is_optical_device : + + # temporary line + if is_optical_device: #for endpoint in request.device_endpoints: # #endpoint.endpoint_id.device_id.CopyFrom(device.device_id) # pass - - if ('new_optical_config' in new_optial_configs and 'opticalconfig' in new_optial_configs["new_optical_config"]): - - context_client.SetOpticalConfig(new_optial_configs["new_optical_config"]['opticalconfig']) + + if 'new_optical_config' in new_optical_configs and 'opticalconfig' in new_optical_configs["new_optical_config"]: + context_client.SetOpticalConfig(new_optical_configs["new_optical_config"]['opticalconfig']) device_id = context_client.SetDevice(device) diff --git a/src/service/service/task_scheduler/tasks/Task_OpticalConnectionDeconfigure.py b/src/service/service/task_scheduler/tasks/Task_OpticalConnectionDeconfigure.py index 78b4378cde700960552b4a7ebf1d727f4baf968f..b1afdeef19d6526d6f52b05924b5b77a8049da8f 100644 --- a/src/service/service/task_scheduler/tasks/Task_OpticalConnectionDeconfigure.py +++ b/src/service/service/task_scheduler/tasks/Task_OpticalConnectionDeconfigure.py @@ -24,10 +24,13 @@ from ._Task import _Task KEY_TEMPLATE = 'optical_connection({connection_id:s}):deconfigure' class Task_OpticalConnectionDeconfigure(_Task): - def __init__(self, task_executor : TaskExecutor, connection_id : ConnectionId,has_media_channel:bool) -> None: + def __init__( + self, task_executor : TaskExecutor, connection_id : ConnectionId, + has_media_channel : bool + ) -> None: super().__init__(task_executor) self._connection_id = connection_id - self._has_media_channel=has_media_channel + self._has_media_channel = has_media_channel @property def connection_id(self) -> ConnectionId: return self._connection_id @@ -43,14 +46,14 @@ class Task_OpticalConnectionDeconfigure(_Task): def execute(self) -> None: connection = self._task_executor.get_connection(self._connection_id) service = self._task_executor.get_service(connection.service_id) - errors=[] + errors = [] service_handler_settings = {} service_handler = self._task_executor.get_service_handler(connection, service, **service_handler_settings) endpointids_to_delete = endpointids_to_raw(connection.path_hops_endpoint_ids) connection_uuid = connection.connection_id.connection_uuid.uuid if self._has_media_channel: - is_media_channel= service_handler.check_media_channel(connection_uuid) + is_media_channel = service_handler.check_media_channel(connection_uuid) if is_media_channel: results_deleteendpoint = service_handler.DeleteEndpoint(endpointids_to_delete, connection_uuid=connection_uuid) errors = check_errors_deleteendpoint(endpointids_to_delete, results_deleteendpoint) @@ -61,8 +64,7 @@ class Task_OpticalConnectionDeconfigure(_Task): raise OperationFailedException(MSG.format(str_connection, str_service), extra_details=errors) if is_media_channel: self._task_executor.delete_connection(self._connection_id) - else : - + else: results_deleteendpoint = service_handler.DeleteEndpoint(endpointids_to_delete, connection_uuid=connection_uuid) errors = check_errors_deleteendpoint(endpointids_to_delete, results_deleteendpoint) if len(errors) > 0: @@ -70,4 +72,4 @@ class Task_OpticalConnectionDeconfigure(_Task): str_connection = grpc_message_to_json_string(connection) str_service = grpc_message_to_json_string(service) raise OperationFailedException(MSG.format(str_connection, str_service), extra_details=errors) - self._task_executor.delete_connection(self._connection_id) \ No newline at end of file + self._task_executor.delete_connection(self._connection_id) diff --git a/src/service/service/task_scheduler/tasks/Task_OpticalServiceConfigDelete.py b/src/service/service/task_scheduler/tasks/Task_OpticalServiceConfigDelete.py index 83f59e48e263fb7d589290ccab03b74c6f818e40..4a21d246e8dfcce4f828553df68169a2bbada160 100644 --- a/src/service/service/task_scheduler/tasks/Task_OpticalServiceConfigDelete.py +++ b/src/service/service/task_scheduler/tasks/Task_OpticalServiceConfigDelete.py @@ -16,17 +16,16 @@ from common.proto.context_pb2 import ServiceId , ConnectionId from service.service.task_scheduler.TaskExecutor import TaskExecutor from service.service.tools.ObjectKeys import get_service_key from ._Task import _Task -import logging KEY_TEMPLATE = 'optical_service({service_id:s})_Config:delete' class Task_OpticalServiceConfigDelete(_Task): - def __init__(self, task_executor : TaskExecutor - ,connection_id:ConnectionId - , service_id : ServiceId - ) -> None: + def __init__( + self, task_executor : TaskExecutor, connection_id : ConnectionId, + service_id : ServiceId + ) -> None: super().__init__(task_executor) - self._connection_id=connection_id + self._connection_id = connection_id self._service_id = service_id @property @@ -41,7 +40,4 @@ class Task_OpticalServiceConfigDelete(_Task): def key(self) -> str: return self.build_key(self._service_id) def execute(self) -> None: - - - - self._task_executor.delete_setting(self._service_id,'/settings','value') + self._task_executor.delete_setting(self._service_id, '/settings', 'value') diff --git a/src/service/service/task_scheduler/tasks/Task_OpticalServiceDelete.py b/src/service/service/task_scheduler/tasks/Task_OpticalServiceDelete.py index cdcdb84235d5d5605f5570f7c50165d126b348da..536d954957ebf1f2d294b9bc435cae7ded7c7531 100644 --- a/src/service/service/task_scheduler/tasks/Task_OpticalServiceDelete.py +++ b/src/service/service/task_scheduler/tasks/Task_OpticalServiceDelete.py @@ -20,11 +20,14 @@ from ._Task import _Task KEY_TEMPLATE = 'optical_service({service_id:s}):delete' class Task_OpticalServiceDelete(_Task): - def __init__(self, task_executor : TaskExecutor, service_id : ServiceId,has_media_channel:bool,has_optical_band:bool) -> None: + def __init__( + self, task_executor : TaskExecutor, service_id : ServiceId, + has_media_channel : bool, has_optical_band : bool + ) -> None: super().__init__(task_executor) self._service_id = service_id - self._has_media_channel=has_media_channel - self._has_optical_band=has_optical_band + self._has_media_channel = has_media_channel + self._has_optical_band = has_optical_band @property def service_id(self) -> ServiceId: return self._service_id @@ -39,4 +42,4 @@ class Task_OpticalServiceDelete(_Task): def execute(self) -> None: if not self._has_media_channel or not self._has_optical_band: - self._task_executor.delete_service(self._service_id) + self._task_executor.delete_service(self._service_id) diff --git a/src/service/service/task_scheduler/tasks/Task_ServiceDelete.py b/src/service/service/task_scheduler/tasks/Task_ServiceDelete.py index dc2ea15928df0081c0655b67fe93c939781c423b..20928f1771d26f810f59ef84a9d1e5471512542b 100644 --- a/src/service/service/task_scheduler/tasks/Task_ServiceDelete.py +++ b/src/service/service/task_scheduler/tasks/Task_ServiceDelete.py @@ -23,7 +23,6 @@ class Task_ServiceDelete(_Task): def __init__(self, task_executor : TaskExecutor, service_id : ServiceId) -> None: super().__init__(task_executor) self._service_id = service_id - @property def service_id(self) -> ServiceId: return self._service_id @@ -37,5 +36,4 @@ class Task_ServiceDelete(_Task): def key(self) -> str: return self.build_key(self._service_id) def execute(self) -> None: - self._task_executor.delete_service(self._service_id) diff --git a/src/service/service/tools/ObjectKeys.py b/src/service/service/tools/ObjectKeys.py index f69f781ee9936ecc496451c0c589e78e227c1ef3..f67cb02e143b78127484d6644a7fdd8c9c71e29c 100644 --- a/src/service/service/tools/ObjectKeys.py +++ b/src/service/service/tools/ObjectKeys.py @@ -22,7 +22,6 @@ def get_device_key(device_id : DeviceId) -> str: return device_id.device_uuid.uuid def get_service_key(service_id : ServiceId) -> str: - context_uuid = service_id.context_id.context_uuid.uuid service_uuid = service_id.service_uuid.uuid return '{:s}/{:s}'.format(context_uuid, service_uuid)