Commit d3aaddcb authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Context component:



- Added tracking log messages
- Minor code improvements

Co-authored-by: default avatarCopilot <copilot@github.com>
parent 9ce99ae2
Loading
Loading
Loading
Loading
+16 −11
Original line number Diff line number Diff line
@@ -50,9 +50,12 @@ def select_optical_band(db_engine : Engine, request : OpticalBandId) -> OpticalB
    return OpticalBand(**obj)


def set_optical_band(db_engine : Engine, ob_data):
    def callback(session : Session) -> List[Dict]:
        if len(ob_data) > 0:
def set_optical_band(db_engine : Engine, ob_data : List[Dict]) -> Dict:
    LOGGER.warning('[update_opticalconfig] ob_data={:s}'.format(str(ob_data)))

    def callback(session : Session) -> Optional[str]:
        if len(ob_data) == 0: return None

        stmt = insert(OpticalBandModel).values(ob_data)
        stmt = stmt.on_conflict_do_update(
            index_elements=[OpticalBandModel.ob_uuid],
@@ -62,6 +65,8 @@ def set_optical_band(db_engine : Engine, ob_data):
        )
        stmt = stmt.returning(OpticalBandModel.ob_uuid)
        ob_id = session.execute(stmt).fetchone()
        return ob_id

    ob_id = run_transaction(sessionmaker(bind=db_engine), callback)
    LOGGER.warning('[update_opticalconfig] ob_id={:s}'.format(str(ob_id)))
    return {'ob_id': ob_id}
+26 −10
Original line number Diff line number Diff line
@@ -13,17 +13,18 @@
# limitations under the License.

import json, logging ,datetime
from sqlalchemy.dialects.postgresql import insert
from common.message_broker.MessageBroker import MessageBroker
from common.DeviceTypes import DeviceTypeEnum
from sqlalchemy import inspect
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy_cockroachdb import run_transaction
from common.message_broker.MessageBroker import MessageBroker
from common.proto.context_pb2 import OpticalConfig, OpticalConfigId, Empty, EventTypeEnum
from common.tools.grpc.Tools import grpc_message_to_json_string
from common.DeviceTypes import DeviceTypeEnum
from .models.OpticalConfig.OpticalConfigModel import OpticalConfigModel
from .models.OpticalConfig.TransponderModel import  TransponderTypeModel, OpticalChannelModel, TransponderInterfaceModel
from .models.OpticalConfig.RoadmModel import RoadmTypeModel, ChannelModel, ORInterfaceModel
from .models.OpticalConfig.TransponderModel import  TransponderTypeModel, OpticalChannelModel, TransponderInterfaceModel


from context.service.database.uuids.OpticalConfig import (
@@ -33,8 +34,8 @@ from context.service.database.uuids.OpticalConfig import (
)
from .Events import notify_event_opticalconfig
from .OpticalBand import set_optical_band

LOGGER = logging.getLogger(__name__)
now = datetime.datetime.utcnow()

def get_opticalconfig(db_engine : Engine):
    def callback(session:Session):
@@ -343,6 +344,8 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig):
    return {'opticalconfig_uuid': opticalconfig_id}

def update_opticalconfig(db_engine : Engine, request : OpticalConfig):
    LOGGER.warning('[update_opticalconfig] received request: {:s}'.format(grpc_message_to_json_string(request)))

    opticalconfig_id = OpticalConfigId()
    device_id = request.device_id
    device_uuid = request.device_id.device_uuid.uuid
@@ -357,8 +360,11 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig):
    #is_transpondre = False
    opticalconfig_uuid = opticalconfig_get_uuid(device_id)
    is_optical_band=None

    LOGGER.warning('[update_opticalconfig] request.config={:s}'.format(str(request.config)))
    if request.config :
        config = json.loads(request.config)
        LOGGER.warning('[update_opticalconfig] config={:s}'.format(str(config)))

        if  'new_config' in config:
            if 'type' in config:
@@ -368,7 +374,7 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig):
            if 'channel_namespace' in config['new_config']:
                channel_namespace = config['new_config'] ['channel_namespace']

            if config_type == DeviceTypeEnum.OPTICAL_TRANSPONDER._value_:
            if config_type == DeviceTypeEnum.OPTICAL_TRANSPONDER.value:
                is_transpondre = True
                transceivers = []
                if channel_namespace is None and  'channel_namespace' in config:
@@ -449,10 +455,16 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig):
                    "opticalconfig_uuid": opticalconfig_uuid,
                })

            if config_type == DeviceTypeEnum.OPTICAL_ROADM._value_:
            if config_type == DeviceTypeEnum.OPTICAL_ROADM.value:
                MSG = '[update_opticalconfig] config_type == DeviceTypeEnum.OPTICAL_ROADM.value; config_type={:s}'
                LOGGER.warning(MSG.format(str(config_type)))

                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']:
                    MSG = '[update_opticalconfig] is_opticalband in config and not config[is_opticalband]; config={:s}'
                    LOGGER.warning(MSG.format(str(config)))
                    is_optical_band=config['is_opticalband']
                    bidir = config['new_config']['bidir']
                    #channels = [channel['name']['index'] for channel in config['channels']]
@@ -479,6 +491,8 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig):
                            })
                            if not bidir: break
                if 'is_opticalband' in config and config['is_opticalband']:
                    MSG = '[update_opticalconfig] is_opticalband in config and config[is_opticalband]; config={:s}'
                    LOGGER.warning(MSG.format(str(config)))
                    is_optical_band = config['is_opticalband']
                    #channels = [channel['name']['index'] for channel in config['channels']]
                    
@@ -502,6 +516,7 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig):
                                "type"            : 'optical_band',
                                "channel_index"   : str( channel_index) if channel_index is not None else None
                            })
                            now = datetime.datetime.utcnow()
                            optical_bands.append ({
                                 "channel_uuid"    : channel_get_uuid(f'optical_bands_{channel_index}',device_uuid),
                                 'connection_uuid' : config['connection_uuid'],
@@ -603,8 +618,9 @@ def update_opticalconfig(db_engine : Engine, request : OpticalConfig):
                opticalChannel_id = session.execute(stmt).fetchone()

    opticalconfig_id = run_transaction(sessionmaker(bind=db_engine), callback)
    if is_optical_band: set_optical_band(db_engine,optical_bands)

    LOGGER.warning('[update_opticalconfig] is_optical_band={:s}'.format(str(is_optical_band)))
    if is_optical_band: set_optical_band(db_engine,optical_bands)
    return {'opticalconfig_uuid': opticalconfig_id}

def select_opticalconfig(db_engine : Engine, request : OpticalConfigId):