Commit 7503e215 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Pre-merge code cleanup

parent babe245f
Loading
Loading
Loading
Loading
+38 −24
Original line number Diff line number Diff line
@@ -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__)

@@ -333,15 +351,13 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
    @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)
       
        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)
       
        return Empty()


    #--------------------- Experimental Optical Link -------------------

    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
@@ -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)
+299 −416

File changed.

Preview size limit exceeded, changes collapsed.

+11 −30
Original line number Diff line number Diff line
@@ -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)
@@ -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 )
@@ -189,8 +172,6 @@ def optical_link_delete(db_engine : Engine, messagebroker : MessageBroker, reque
    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)
+19 −21
Original line number Diff line number Diff line
@@ -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

                
           
            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))
@@ -158,9 +157,8 @@ class DeviceServiceServicerImpl(DeviceServiceServicer):
                #    #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)

+9 −7
Original line number Diff line number Diff line
@@ -24,7 +24,10 @@ 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
@@ -62,7 +65,6 @@ class Task_OpticalConnectionDeconfigure(_Task):
            if is_media_channel:
                self._task_executor.delete_connection(self._connection_id)
        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:
Loading