Skip to content
Snippets Groups Projects
Select Git revision
  • eafc1f9ec92ae1f7515f41f1c3dcdd9e7f58faed
  • master default
  • feat/331-bitnami-kafka-moved-to-bitnamilegacy-kafka
  • feat/330-tid-pcep-component
  • feat/tid-newer-pcep-component
  • feat/policy-refactor
  • feat/314-tid-new-service-for-ipowdm-configuration-fron-orchestrator-to-ipowdm-controller
  • feat/329-ecoc-2025-hack-your-research
  • develop protected
  • feat/116-ubi-updates-in-telemetry-backend-to-support-p4-in-band-network-telemetry
  • feat/320-cttc-ietf-simap-basic-support-with-kafka-yang-push
  • feat/310-cttc-implement-nbi-connector-to-interface-with-osm-client
  • feat/307-update-python-version-service
  • feat/292-cttc-implement-integration-test-for-ryu-openflow
  • cnit_tapi
  • feat/327-tid-new-service-to-ipowdm-controller-to-manage-transceivers-configuration-on-external-agent
  • cnit-p2mp-premerge
  • feat/325-tid-nbi-e2e-to-manage-e2e-path-computation
  • feat/326-tid-external-management-of-devices-telemetry-nbi
  • openroadm-flex-grid
  • CTTC-IMPLEMENT-NBI-CONNECTOR-NOS-ZTP
  • feat/324-tid-nbi-ietf_l3vpn-deploy-fail
  • v5.0.0 protected
  • v4.0.0 protected
  • demo-dpiab-eucnc2024
  • v3.0.0 protected
  • v2.1.0 protected
  • v2.0.0 protected
  • v1.0.0 protected
29 results

PytestGenerateTests.py

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Link.py 4.96 KiB
    # Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
    #
    # 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.
    
    from sqlalchemy.dialects.postgresql import insert
    from sqlalchemy.engine import Engine
    from sqlalchemy.orm import Session, sessionmaker
    from sqlalchemy_cockroachdb import run_transaction
    from typing import Dict, List, Optional, Set, Tuple
    from common.proto.context_pb2 import Link, LinkId, LinkIdList, LinkList
    from common.method_wrappers.ServiceExceptions import NotFoundException
    from common.tools.object_factory.Link import json_link_id
    from .models.LinkModel import LinkModel, LinkEndPointModel
    from .models.TopologyModel import TopologyLinkModel
    from .uuids.EndPoint import endpoint_get_uuid
    from .uuids.Link import link_get_uuid
    
    def link_list_ids(db_engine : Engine) -> LinkIdList:
        def callback(session : Session) -> List[Dict]:
            obj_list : List[LinkModel] = session.query(LinkModel).all()
            #.options(selectinload(LinkModel.topology)).filter_by(context_uuid=context_uuid).one_or_none()
            return [obj.dump_id() for obj in obj_list]
        return LinkIdList(link_ids=run_transaction(sessionmaker(bind=db_engine), callback))
    
    def link_list_objs(db_engine : Engine) -> LinkList:
        def callback(session : Session) -> List[Dict]:
            obj_list : List[LinkModel] = session.query(LinkModel).all()
            #.options(selectinload(LinkModel.topology)).filter_by(context_uuid=context_uuid).one_or_none()
            return [obj.dump() for obj in obj_list]
        return LinkList(links=run_transaction(sessionmaker(bind=db_engine), callback))
    
    def link_get(db_engine : Engine, request : LinkId) -> Link:
        link_uuid = link_get_uuid(request, allow_random=False)
        def callback(session : Session) -> Optional[Dict]:
            obj : Optional[LinkModel] = session.query(LinkModel).filter_by(link_uuid=link_uuid).one_or_none()
            return None if obj is None else obj.dump()
        obj = run_transaction(sessionmaker(bind=db_engine), callback)
        if obj is None:
            raw_link_uuid = request.link_uuid.uuid
            raise NotFoundException('Link', raw_link_uuid, extra_details=[
                'link_uuid generated was: {:s}'.format(link_uuid)
            ])
        return Link(**obj)
    
    def link_set(db_engine : Engine, request : Link) -> Tuple[LinkId, bool]:
        raw_link_uuid = request.link_id.link_uuid.uuid
        raw_link_name = request.name
        link_name = raw_link_uuid if len(raw_link_name) == 0 else raw_link_name
        link_uuid = link_get_uuid(request.link_id, link_name=link_name, allow_random=True)
    
        topology_uuids : Set[str] = set()
        related_topologies : List[Dict] = list()
        link_endpoints_data : List[Dict] = list()
        for endpoint_id in request.link_endpoint_ids:
            endpoint_topology_uuid, _, endpoint_uuid = endpoint_get_uuid(
                endpoint_id, allow_random=False)
    
            link_endpoints_data.append({
                'link_uuid'    : link_uuid,
                'endpoint_uuid': endpoint_uuid,
            })
    
            if endpoint_topology_uuid not in topology_uuids:
                related_topologies.append({
                    'topology_uuid': endpoint_topology_uuid,
                    'link_uuid': link_uuid,
                })
                topology_uuids.add(endpoint_topology_uuid)
    
        link_data = [{
            'link_uuid': link_uuid,
            'link_name': link_name,
        }]
    
        def callback(session : Session) -> None:
            stmt = insert(LinkModel).values(link_data)
            stmt = stmt.on_conflict_do_update(
                index_elements=[LinkModel.link_uuid],
                set_=dict(link_name = stmt.excluded.link_name)
            )
            session.execute(stmt)
    
            stmt = insert(LinkEndPointModel).values(link_endpoints_data)
            stmt = stmt.on_conflict_do_nothing(
                index_elements=[LinkEndPointModel.link_uuid, LinkEndPointModel.endpoint_uuid]
            )
            session.execute(stmt)
    
            session.execute(insert(TopologyLinkModel).values(related_topologies).on_conflict_do_nothing(
                index_elements=[TopologyLinkModel.topology_uuid, TopologyLinkModel.link_uuid]
            ))
    
        run_transaction(sessionmaker(bind=db_engine), callback)
        updated = False # TODO: improve and check if created/updated
        return LinkId(**json_link_id(link_uuid)),updated
    
    def link_delete(db_engine : Engine, request : LinkId) -> bool:
        link_uuid = link_get_uuid(request, allow_random=False)
        def callback(session : Session) -> bool:
            num_deleted = session.query(LinkModel).filter_by(link_uuid=link_uuid).delete()
            return num_deleted > 0
        return run_transaction(sessionmaker(bind=db_engine), callback)