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

Pre-merge code cleanup

parent 2824ac8c
Loading
Loading
Loading
Loading
+14 −26
Original line number Original line Diff line number Diff line
@@ -16,10 +16,10 @@ import datetime, json, logging
from sqlalchemy import delete
from sqlalchemy import delete
#from sqlalchemy.dialects import postgresql
#from sqlalchemy.dialects import postgresql
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.orm import Session
from sqlalchemy.engine import Engine
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session
from typing import Dict, List, Optional, Set
from typing import Dict, List, Optional, Set
from common.proto.context_pb2 import ConfigRule ,ServiceId ,ServiceConfigRule ,Empty
from common.proto.context_pb2 import ConfigRule, ServiceConfigRule, Empty
from common.tools.grpc.Tools import grpc_message_to_json_string
from common.tools.grpc.Tools import grpc_message_to_json_string
from .models.enums.ConfigAction import ORM_ConfigActionEnum, grpc_to_enum__config_action
from .models.enums.ConfigAction import ORM_ConfigActionEnum, grpc_to_enum__config_action
from .models.ConfigRuleModel import (
from .models.ConfigRuleModel import (
@@ -27,7 +27,7 @@ from .models.ConfigRuleModel import (
from .uuids._Builder import get_uuid_from_string
from .uuids._Builder import get_uuid_from_string
from .uuids.EndPoint import endpoint_get_uuid
from .uuids.EndPoint import endpoint_get_uuid
from sqlalchemy_cockroachdb import run_transaction
from sqlalchemy_cockroachdb import run_transaction
from sqlalchemy.orm import Session, selectinload, sessionmaker
from sqlalchemy.orm import Session, sessionmaker


LOGGER = logging.getLogger(__name__)
LOGGER = logging.getLogger(__name__)


@@ -36,10 +36,8 @@ def compose_config_rules_data(
    device_uuid : Optional[str] = None, service_uuid : Optional[str] = None, slice_uuid : Optional[str] = None
    device_uuid : Optional[str] = None, service_uuid : Optional[str] = None, slice_uuid : Optional[str] = None
) -> List[Dict]:
) -> List[Dict]:
    dict_config_rules : List[Dict] = list()
    dict_config_rules : List[Dict] = list()
 
    for position,config_rule in enumerate(config_rules):
    for position,config_rule in enumerate(config_rules):
        str_kind = config_rule.WhichOneof('config_rule')
        str_kind = config_rule.WhichOneof('config_rule')
  
        kind = ConfigRuleKindEnum._member_map_.get(str_kind.upper()) # pylint: disable=no-member
        kind = ConfigRuleKindEnum._member_map_.get(str_kind.upper()) # pylint: disable=no-member
        dict_config_rule = {
        dict_config_rule = {
            'position'  : position,
            'position'  : position,
@@ -68,7 +66,6 @@ def compose_config_rules_data(


        configrule_name = None
        configrule_name = None
        if kind == ConfigRuleKindEnum.CUSTOM:
        if kind == ConfigRuleKindEnum.CUSTOM:
          
            configrule_name = '{:s}:{:s}:{:s}'.format(parent_kind, kind.value, config_rule.custom.resource_key)
            configrule_name = '{:s}:{:s}:{:s}'.format(parent_kind, kind.value, config_rule.custom.resource_key)
        elif kind == ConfigRuleKindEnum.ACL:
        elif kind == ConfigRuleKindEnum.ACL:
            _, _, endpoint_uuid = endpoint_get_uuid(config_rule.acl.endpoint_id, allow_random=False)
            _, _, endpoint_uuid = endpoint_get_uuid(config_rule.acl.endpoint_id, allow_random=False)
@@ -158,22 +155,13 @@ def upsert_config_rules(




def delete_config_rule(db_engine : Engine, request : ServiceConfigRule):
def delete_config_rule(db_engine : Engine, request : ServiceConfigRule):

    config_rule = request.configrule_custom
    config_rule = request.configrule_custom
    service_id  = request.service_id
    service_id  = request.service_id
    parent_uuid = service_id.service_uuid.uuid
    parent_uuid = service_id.service_uuid.uuid
    configrule_name = 'service:custom:{:s}'.format( config_rule.resource_key)
    configrule_name = 'service:custom:{:s}'.format( config_rule.resource_key)
    configrule_uuid = get_uuid_from_string(configrule_name, prefix_for_name=parent_uuid)
    configrule_uuid = get_uuid_from_string(configrule_name, prefix_for_name=parent_uuid)

        
    def callback(session : Session) -> bool:
    def callback(session : Session) -> bool:
          
        num_deleted = session.query(ServiceConfigRuleModel).filter_by(configrule_uuid=configrule_uuid).delete()
        num_deleted = session.query(ServiceConfigRuleModel).filter_by(configrule_uuid=configrule_uuid).delete()
        return num_deleted > 0
        return num_deleted > 0
        
    deleted = run_transaction(sessionmaker(bind=db_engine), callback)
    deleted = run_transaction(sessionmaker(bind=db_engine), callback)

    return Empty()
    return Empty()
            
    
    
 No newline at end of file
+0 −5
Original line number Original line Diff line number Diff line
@@ -40,7 +40,6 @@ from .ConfigRule import compose_config_rules_data, upsert_config_rules
from .Component import compose_components_data
from .Component import compose_components_data
from .Events import notify_event_context, notify_event_device, notify_event_topology
from .Events import notify_event_context, notify_event_device, notify_event_topology



LOGGER = logging.getLogger(__name__)
LOGGER = logging.getLogger(__name__)


def device_list_ids(db_engine : Engine) -> DeviceIdList:
def device_list_ids(db_engine : Engine) -> DeviceIdList:
@@ -151,9 +150,6 @@ def device_set(db_engine : Engine, messagebroker : MessageBroker, request : Devi
        #             'updated_at'       : now,
        #             'updated_at'       : now,
        #     })
        #     })


        
        

        if endpoint_topology_uuid not in topology_uuids:
        if endpoint_topology_uuid not in topology_uuids:
            related_topologies.append({
            related_topologies.append({
                'topology_uuid': endpoint_topology_uuid,
                'topology_uuid': endpoint_topology_uuid,
@@ -323,7 +319,6 @@ def device_delete(db_engine : Engine, messagebroker : MessageBroker, request : D
    return Empty()
    return Empty()


def device_select(db_engine : Engine, request : DeviceFilter) -> DeviceList:
def device_select(db_engine : Engine, request : DeviceFilter) -> DeviceList:
   
    device_uuids = [
    device_uuids = [
        device_get_uuid(device_id, allow_random=False)
        device_get_uuid(device_id, allow_random=False)
        for device_id in request.device_ids.device_ids
        for device_id in request.device_ids.device_ids
+21 −22
Original line number Original line Diff line number Diff line
@@ -17,8 +17,9 @@ from typing import Dict, Iterator, Set
from common.message_broker.Message import Message
from common.message_broker.Message import Message
from common.message_broker.MessageBroker import MessageBroker
from common.message_broker.MessageBroker import MessageBroker
from common.proto.context_pb2 import (
from common.proto.context_pb2 import (
    ConnectionEvent, ContextEvent, DeviceEvent, EventTypeEnum, LinkEvent, ServiceEvent, SliceEvent, TopologyEvent ,
    ConnectionEvent, ContextEvent, DeviceEvent, EventTypeEnum, LinkEvent,
    OpticalConfigEvent)
    ServiceEvent, SliceEvent, TopologyEvent, OpticalConfigEvent
)


class EventTopicEnum(enum.Enum):
class EventTopicEnum(enum.Enum):
    CONNECTION    = 'connection'
    CONNECTION    = 'connection'
@@ -41,8 +42,7 @@ TOPIC_TO_EVENTCLASS = {
    EventTopicEnum.SERVICE.value       : ServiceEvent,
    EventTopicEnum.SERVICE.value       : ServiceEvent,
    EventTopicEnum.SLICE.value         : SliceEvent,
    EventTopicEnum.SLICE.value         : SliceEvent,
    EventTopicEnum.TOPOLOGY.value      : TopologyEvent,
    EventTopicEnum.TOPOLOGY.value      : TopologyEvent,
    EventTopicEnum.OPTICALCONFIG.value : OpticalConfigEvent
    EventTopicEnum.OPTICALCONFIG.value : OpticalConfigEvent,

}
}


CONSUME_TIMEOUT = 0.5 # seconds
CONSUME_TIMEOUT = 0.5 # seconds
@@ -69,7 +69,6 @@ def notify_event_device(messagebroker : MessageBroker, event_type : EventTypeEnu
def notify_event_opticalconfig(messagebroker : MessageBroker, event_type : EventTypeEnum, opticalconfig_id : Dict) -> None:
def notify_event_opticalconfig(messagebroker : MessageBroker, event_type : EventTypeEnum, opticalconfig_id : Dict) -> None:
    notify_event(messagebroker, EventTopicEnum.DEVICE, event_type, {'opticalconfig_id': opticalconfig_id})
    notify_event(messagebroker, EventTopicEnum.DEVICE, event_type, {'opticalconfig_id': opticalconfig_id})



def notify_event_link(messagebroker : MessageBroker, event_type : EventTypeEnum, link_id : Dict) -> None:
def notify_event_link(messagebroker : MessageBroker, event_type : EventTypeEnum, link_id : Dict) -> None:
    notify_event(messagebroker, EventTopicEnum.LINK, event_type, {'link_id': link_id})
    notify_event(messagebroker, EventTopicEnum.LINK, event_type, {'link_id': link_id})


+0 −2
Original line number Original line Diff line number Diff line
@@ -63,7 +63,6 @@ def link_get(db_engine : Engine, request : LinkId) -> Link:
    return Link(**obj)
    return Link(**obj)


def link_set(db_engine : Engine, messagebroker : MessageBroker, request : Link) -> LinkId:
def link_set(db_engine : Engine, messagebroker : MessageBroker, request : Link) -> LinkId:
    
    raw_link_uuid = request.link_id.link_uuid.uuid
    raw_link_uuid = request.link_id.link_uuid.uuid
    raw_link_name = request.name
    raw_link_name = request.name
    link_name = raw_link_uuid if len(raw_link_name) == 0 else raw_link_name
    link_name = raw_link_uuid if len(raw_link_name) == 0 else raw_link_name
@@ -84,7 +83,6 @@ def link_set(db_engine : Engine, messagebroker : MessageBroker, request : Link)


    link_endpoints_data : List[Dict] = list()
    link_endpoints_data : List[Dict] = list()
    for i,endpoint_id in enumerate(request.link_endpoint_ids):
    for i,endpoint_id in enumerate(request.link_endpoint_ids):
   
        endpoint_topology_uuid, _, endpoint_uuid = endpoint_get_uuid(
        endpoint_topology_uuid, _, endpoint_uuid = endpoint_get_uuid(
            endpoint_id, allow_random=False)
            endpoint_id, allow_random=False)


+7 −9
Original line number Original line Diff line number Diff line
@@ -27,6 +27,7 @@ from common.tools.object_factory.Topology import json_topology_id
from context.Config import ALLOW_EXPLICIT_ADD_DEVICE_TO_TOPOLOGY, ALLOW_EXPLICIT_ADD_LINK_TO_TOPOLOGY
from context.Config import ALLOW_EXPLICIT_ADD_DEVICE_TO_TOPOLOGY, ALLOW_EXPLICIT_ADD_LINK_TO_TOPOLOGY
from .models.DeviceModel import DeviceModel
from .models.DeviceModel import DeviceModel
from .models.LinkModel import LinkModel
from .models.LinkModel import LinkModel
from .models.OpticalLinkModel import OpticalLinkModel
from .models.TopologyModel import TopologyDeviceModel, TopologyLinkModel, TopologyModel, TopologyOpticalLinkModel
from .models.TopologyModel import TopologyDeviceModel, TopologyLinkModel, TopologyModel, TopologyOpticalLinkModel
from .uuids.Context import context_get_uuid
from .uuids.Context import context_get_uuid
from .uuids.Device import device_get_uuid
from .uuids.Device import device_get_uuid
@@ -34,8 +35,6 @@ from .uuids.Link import link_get_uuid
from .uuids.Topology import topology_get_uuid
from .uuids.Topology import topology_get_uuid
from .Events import notify_event_context, notify_event_topology
from .Events import notify_event_context, notify_event_topology


from .models.OpticalLinkModel import OpticalLinkModel

LOGGER = logging.getLogger(__name__)
LOGGER = logging.getLogger(__name__)


def topology_list_ids(db_engine : Engine, request : ContextId) -> TopologyIdList:
def topology_list_ids(db_engine : Engine, request : ContextId) -> TopologyIdList:
@@ -84,7 +83,6 @@ def topology_get_details(db_engine : Engine, request : TopologyId) -> TopologyDe
            .options(selectinload(TopologyModel.topology_optical_links, TopologyOpticalLinkModel.optical_link, OpticalLinkModel.opticallink_endpoints))\
            .options(selectinload(TopologyModel.topology_optical_links, TopologyOpticalLinkModel.optical_link, OpticalLinkModel.opticallink_endpoints))\
            .filter_by(topology_uuid=topology_uuid).one_or_none()
            .filter_by(topology_uuid=topology_uuid).one_or_none()
            #.options(selectinload(DeviceModel.components))\
            #.options(selectinload(DeviceModel.components))\
               
        return None if obj is None else obj.dump_details()
        return None if obj is None else obj.dump_details()
    obj = run_transaction(sessionmaker(bind=db_engine), callback)
    obj = run_transaction(sessionmaker(bind=db_engine), callback)
    if obj is None:
    if obj is None:
@@ -92,7 +90,7 @@ def topology_get_details(db_engine : Engine, request : TopologyId) -> TopologyDe
        raw_topology_uuid = '{:s}/{:s}'.format(request.context_id.context_uuid.uuid, request.topology_uuid.uuid)
        raw_topology_uuid = '{:s}/{:s}'.format(request.context_id.context_uuid.uuid, request.topology_uuid.uuid)
        raise NotFoundException('Topology', raw_topology_uuid, extra_details=[
        raise NotFoundException('Topology', raw_topology_uuid, extra_details=[
            'context_uuid generated was: {:s}'.format(context_uuid),
            'context_uuid generated was: {:s}'.format(context_uuid),
            'topology_uuid generated was: {:s}'.format(topology_uuid)
            'topology_uuid generated was: {:s}'.format(topology_uuid),
        ])
        ])
    return TopologyDetails(**obj)
    return TopologyDetails(**obj)