Skip to content
Snippets Groups Projects
Topology.py 10.6 KiB
Newer Older
# Copyright 2022-2024 ETSI SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
import datetime, logging
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.engine import Engine
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from sqlalchemy.orm import Session, selectinload, sessionmaker
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from sqlalchemy_cockroachdb import run_transaction
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from typing import Dict, List, Optional, Set
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.proto.context_pb2 import (
    ContextId, Empty, EventTypeEnum, Topology, TopologyDetails, TopologyId, TopologyIdList, TopologyList)
from common.message_broker.MessageBroker import MessageBroker
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.method_wrappers.ServiceExceptions import NotFoundException
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.tools.object_factory.Context import json_context_id
from common.tools.object_factory.Topology import json_topology_id
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from context.Config import ALLOW_EXPLICIT_ADD_DEVICE_TO_TOPOLOGY, ALLOW_EXPLICIT_ADD_LINK_TO_TOPOLOGY
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from .models.DeviceModel import DeviceModel
from .models.LinkModel import LinkModel
from .models.OpticalLinkModel import OpticalLinkModel
from .models.TopologyModel import TopologyDeviceModel, TopologyLinkModel, TopologyModel, TopologyOpticalLinkModel
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from .uuids.Context import context_get_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from .uuids.Device import device_get_uuid
from .uuids.Link import link_get_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from .uuids.Topology import topology_get_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from .Events import notify_event_context, notify_event_topology
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
LOGGER = logging.getLogger(__name__)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
def topology_list_ids(db_engine : Engine, request : ContextId) -> TopologyIdList:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    context_uuid = context_get_uuid(request, allow_random=False)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def callback(session : Session) -> List[Dict]:
        obj_list : List[TopologyModel] = session.query(TopologyModel).filter_by(context_uuid=context_uuid).all()
        return [obj.dump_id() for obj in obj_list]
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    topology_ids = run_transaction(sessionmaker(bind=db_engine), callback)
    return TopologyIdList(topology_ids=topology_ids)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
def topology_list_objs(db_engine : Engine, request : ContextId) -> TopologyList:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    context_uuid = context_get_uuid(request, allow_random=False)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def callback(session : Session) -> List[Dict]:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        obj_list : List[TopologyModel] = session.query(TopologyModel)\
            .options(selectinload(TopologyModel.topology_devices))\
            .options(selectinload(TopologyModel.topology_links))\
            .filter_by(context_uuid=context_uuid).all()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return [obj.dump() for obj in obj_list]
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    topologies = run_transaction(sessionmaker(bind=db_engine), callback)
    return TopologyList(topologies=topologies)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
def topology_get(db_engine : Engine, request : TopologyId) -> Topology:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    _,topology_uuid = topology_get_uuid(request, allow_random=False)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def callback(session : Session) -> Optional[Dict]:
        obj : Optional[TopologyModel] = session.query(TopologyModel)\
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            .options(selectinload(TopologyModel.topology_devices))\
            .options(selectinload(TopologyModel.topology_links))\
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            .filter_by(topology_uuid=topology_uuid).one_or_none()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return None if obj is None else obj.dump()
    obj = run_transaction(sessionmaker(bind=db_engine), callback)
    if obj is None:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        context_uuid = context_get_uuid(request.context_id, allow_random=False)
        raw_topology_uuid = '{:s}/{:s}'.format(request.context_id.context_uuid.uuid, request.topology_uuid.uuid)
        raise NotFoundException('Topology', raw_topology_uuid, extra_details=[
            'context_uuid generated was: {:s}'.format(context_uuid),
            'topology_uuid generated was: {:s}'.format(topology_uuid),
        ])
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    return Topology(**obj)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
def topology_get_details(db_engine : Engine, request : TopologyId) -> TopologyDetails:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    _,topology_uuid = topology_get_uuid(request, allow_random=False)
    def callback(session : Session) -> Optional[Dict]:
        obj : Optional[TopologyModel] = session.query(TopologyModel)\
            .options(selectinload(TopologyModel.topology_devices, TopologyDeviceModel.device, DeviceModel.endpoints))\
            .options(selectinload(TopologyModel.topology_links, TopologyLinkModel.link, LinkModel.link_endpoints))\
            .options(selectinload(TopologyModel.topology_optical_links, TopologyOpticalLinkModel.optical_link, OpticalLinkModel.opticallink_endpoints))\
            .filter_by(topology_uuid=topology_uuid).one_or_none()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            #.options(selectinload(DeviceModel.components))\
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return None if obj is None else obj.dump_details()
    obj = run_transaction(sessionmaker(bind=db_engine), callback)
    if obj is None:
        context_uuid = context_get_uuid(request.context_id, allow_random=False)
        raw_topology_uuid = '{:s}/{:s}'.format(request.context_id.context_uuid.uuid, request.topology_uuid.uuid)
        raise NotFoundException('Topology', raw_topology_uuid, extra_details=[
            'context_uuid generated was: {:s}'.format(context_uuid),
            'topology_uuid generated was: {:s}'.format(topology_uuid),
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        ])
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    return TopologyDetails(**obj)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
def topology_set(db_engine : Engine, messagebroker : MessageBroker, request : Topology) -> TopologyId:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    topology_name = request.name
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    if len(topology_name) == 0: topology_name = request.topology_id.topology_uuid.uuid
    context_uuid,topology_uuid = topology_get_uuid(request.topology_id, topology_name=topology_name, allow_random=True)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    # By default, ignore request.device_ids and request.link_ids. They are used for retrieving
    # devices and links added into the topology. Explicit addition into the topology is done
    # automatically when creating the devices and links, based on the topologies specified in
    # the endpoints associated with the devices and links.
    # In some cases, it might be needed to add them explicitly; to allow that, activate flags
    # ALLOW_EXPLICIT_ADD_DEVICE_TO_TOPOLOGY and/or ALLOW_EXPLICIT_ADD_LINK_TO_TOPOLOGY.
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    related_devices : List[Dict] = list()
    if ALLOW_EXPLICIT_ADD_DEVICE_TO_TOPOLOGY:
        device_uuids : Set[str] = set()
        for device_id in request.device_ids:
            device_uuid = device_get_uuid(device_id)
            if device_uuid not in device_uuids: continue
            related_devices.append({'topology_uuid': topology_uuid, 'device_uuid': device_uuid})
            device_uuids.add(device_uuid)
    else:
        if len(request.device_ids) > 0: # pragma: no cover
            MSG = 'ALLOW_EXPLICIT_ADD_DEVICE_TO_TOPOLOGY={:s}; '.format(str(ALLOW_EXPLICIT_ADD_DEVICE_TO_TOPOLOGY))
            MSG += 'Items in field "device_ids" ignored. This field is used for retrieval purposes only.'
            LOGGER.warning(MSG)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    related_links : List[Dict] = list()
    if ALLOW_EXPLICIT_ADD_LINK_TO_TOPOLOGY:
        link_uuids : Set[str] = set()
        for link_id in request.link_ids:
            link_uuid = link_get_uuid(link_id)
            if link_uuid not in link_uuids: continue
            related_links.append({'topology_uuid': topology_uuid, 'link_uuid': link_uuid})
            link_uuids.add(link_uuid)
    else:
        if len(request.link_ids) > 0:   # pragma: no cover
            MSG = 'ALLOW_EXPLICIT_ADD_LINK_TO_TOPOLOGY={:s}; '.format(str(ALLOW_EXPLICIT_ADD_LINK_TO_TOPOLOGY))
            MSG += 'Items in field "link_ids" ignored. This field is used for retrieval purposes only.'
            LOGGER.warning(MSG)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    now = datetime.datetime.now(datetime.timezone.utc)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    topology_data = [{
        'context_uuid' : context_uuid,
        'topology_uuid': topology_uuid,
        'topology_name': topology_name,
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        'created_at'   : now,
        'updated_at'   : now,
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    }]
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def callback(session : Session) -> bool:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        stmt = insert(TopologyModel).values(topology_data)
        stmt = stmt.on_conflict_do_update(
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            index_elements=[TopologyModel.topology_uuid],
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            set_=dict(
                topology_name = stmt.excluded.topology_name,
                updated_at    = stmt.excluded.updated_at,
            )
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        stmt = stmt.returning(TopologyModel.created_at, TopologyModel.updated_at)
        created_at,updated_at = session.execute(stmt).fetchone()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

        updated = updated_at > created_at

        updated_topology_device = False
        if len(related_devices) > 0:
            stmt = insert(TopologyDeviceModel).values(related_devices)
            stmt = stmt.on_conflict_do_nothing(
                index_elements=[TopologyDeviceModel.topology_uuid, TopologyDeviceModel.device_uuid]
            )
            topology_device_inserts = session.execute(stmt)
            updated_topology_device = int(topology_device_inserts.rowcount) > 0

        updated_topology_link = False
        if len(related_links) > 0:
            stmt = insert(TopologyLinkModel).values(related_links)
            stmt = stmt.on_conflict_do_nothing(
                index_elements=[TopologyLinkModel.topology_uuid, TopologyLinkModel.link_uuid]
            )
            topology_link_inserts = session.execute(stmt)
            updated_topology_link = int(topology_link_inserts.rowcount) > 0

        return updated or updated_topology_device or updated_topology_link
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    updated = run_transaction(sessionmaker(bind=db_engine), callback)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    context_id = json_context_id(context_uuid)
    topology_id = json_topology_id(topology_uuid, context_id=context_id)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    event_type = EventTypeEnum.EVENTTYPE_UPDATE if updated else EventTypeEnum.EVENTTYPE_CREATE
    notify_event_topology(messagebroker, event_type, topology_id)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    notify_event_context(messagebroker, EventTypeEnum.EVENTTYPE_UPDATE, context_id)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    return TopologyId(**topology_id)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
def topology_delete(db_engine : Engine, messagebroker : MessageBroker, request : TopologyId) -> Empty:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    context_uuid,topology_uuid = topology_get_uuid(request, allow_random=False)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def callback(session : Session) -> bool:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        num_deleted = session.query(TopologyModel).filter_by(topology_uuid=topology_uuid).delete()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return num_deleted > 0
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    deleted = run_transaction(sessionmaker(bind=db_engine), callback)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    context_id = json_context_id(context_uuid)
    topology_id = json_topology_id(topology_uuid, context_id=context_id)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    if deleted:
        notify_event_topology(messagebroker, EventTypeEnum.EVENTTYPE_REMOVE, topology_id)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        notify_event_context(messagebroker, EventTypeEnum.EVENTTYPE_UPDATE, context_id)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    return Empty()