Scheduled maintenance on Saturday, 27 September 2025, from 07:00 AM to 4:00 PM GMT (09:00 AM to 6:00 PM CEST) - some services may be unavailable -

Skip to content
Snippets Groups Projects
Select Git revision
  • 5fd7570698c26fcb4dda88eaa1e7dbcd1f6dea3c
  • master default
  • feat/320-cttc-ietf-simap-basic-support-with-kafka-yang-push
  • feat/307-update-python-version-service
  • feat/292-cttc-implement-integration-test-for-ryu-openflow
  • cnit_tapi
  • feat/314-tid-new-service-for-ipowdm-configuration-fron-orchestrator-to-ipowdm-controller
  • 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
  • feat/310-cttc-implement-nbi-connector-to-interface-with-osm-client
  • develop protected
  • feat/324-tid-nbi-ietf_l3vpn-deploy-fail
  • feat/321-add-support-for-gnmi-configuration-via-proto
  • feat/322-add-read-support-for-ipinfusion-devices-via-netconf
  • feat/323-add-support-for-restconf-protocol-in-devices
  • feat/policy-refactor
  • feat/192-cttc-implement-telemetry-backend-collector-gnmi-openconfig
  • feat/307-update-python-version
  • feat/telemetry-collector-int
  • 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

report_coverage_l3_centralizedattackdetector.sh

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    test_link.py 10.25 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.
    
    import copy, grpc, pytest, time
    from common.proto.context_pb2 import (
        Context, ContextEvent, ContextId, Device, DeviceEvent, DeviceId, Empty, EventTypeEnum, Link, LinkEvent, LinkId,
        Topology, TopologyEvent, TopologyId)
    from context.client.ContextClient import ContextClient
    from context.client.EventsCollector import EventsCollector
    from context.service.database.uuids.Link import link_get_uuid
    from .Objects import (
        CONTEXT, CONTEXT_ID, DEVICE_R1, DEVICE_R1_ID, DEVICE_R2, DEVICE_R2_ID, LINK_R1_R2, LINK_R1_R2_ID, LINK_R1_R2_NAME,
        TOPOLOGY, TOPOLOGY_ID)
    
    @pytest.mark.depends(on=['context/tests/test_device.py::test_device'])
    def test_link(context_client : ContextClient) -> None:
    
        # ----- Initialize the EventsCollector -----------------------------------------------------------------------------
        events_collector = EventsCollector(
            context_client, log_events_received=True,
            activate_context_collector = True, activate_topology_collector = True, activate_device_collector = True,
            activate_link_collector = True, activate_service_collector = False, activate_slice_collector = False,
            activate_connection_collector = False)
        events_collector.start()
        time.sleep(3)
    
        # ----- Prepare dependencies for the test and capture related events -----------------------------------------------
        response = context_client.SetContext(Context(**CONTEXT))
        context_uuid = response.context_uuid.uuid
    
        response = context_client.SetTopology(Topology(**TOPOLOGY))
        assert response.context_id.context_uuid.uuid == context_uuid
        topology_uuid = response.topology_uuid.uuid
    
        response = context_client.SetDevice(Device(**DEVICE_R1))
        device_r1_uuid = response.device_uuid.uuid
    
        response = context_client.SetDevice(Device(**DEVICE_R2))
        device_r2_uuid = response.device_uuid.uuid
    
        events = events_collector.get_events(block=True, count=4, timeout=1.0)
        assert isinstance(events[0], ContextEvent)
        assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_CREATE
        assert events[0].context_id.context_uuid.uuid == context_uuid
        assert isinstance(events[1], TopologyEvent)
        assert events[1].event.event_type == EventTypeEnum.EVENTTYPE_CREATE
        assert events[1].topology_id.context_id.context_uuid.uuid == context_uuid
        assert events[1].topology_id.topology_uuid.uuid == topology_uuid
        assert isinstance(events[2], DeviceEvent)
        assert events[2].event.event_type == EventTypeEnum.EVENTTYPE_CREATE
        assert events[2].device_id.device_uuid.uuid == device_r1_uuid
        assert isinstance(events[3], DeviceEvent)
        assert events[3].event.event_type == EventTypeEnum.EVENTTYPE_CREATE
        assert events[3].device_id.device_uuid.uuid == device_r2_uuid
    
        # ----- Get when the object does not exist -------------------------------------------------------------------------
        link_id = LinkId(**LINK_R1_R2_ID)
        link_uuid = link_get_uuid(link_id, allow_random=False)
        with pytest.raises(grpc.RpcError) as e:
            context_client.GetLink(link_id)
        assert e.value.code() == grpc.StatusCode.NOT_FOUND
        MSG = 'Link({:s}) not found; link_uuid generated was: {:s}'
        assert e.value.details() == MSG.format(LINK_R1_R2_NAME, link_uuid)
    
        # ----- List when the object does not exist ------------------------------------------------------------------------
        response = context_client.ListLinkIds(Empty())
        assert len(response.link_ids) == 0
    
        response = context_client.ListLinks(Empty())
        assert len(response.links) == 0
    
        # ----- Create the object ------------------------------------------------------------------------------------------
        response = context_client.SetLink(Link(**LINK_R1_R2))
        assert response.link_uuid.uuid == link_uuid
    
        # ----- Check create event -----------------------------------------------------------------------------------------
        event = events_collector.get_event(block=True, timeout=1.0)
        assert isinstance(event, LinkEvent)
        assert event.event.event_type == EventTypeEnum.EVENTTYPE_CREATE
        assert event.link_id.link_uuid.uuid == link_uuid
    
        # ----- Get when the object exists ---------------------------------------------------------------------------------
        response = context_client.GetLink(LinkId(**LINK_R1_R2_ID))
        assert response.link_id.link_uuid.uuid == link_uuid
        assert response.name == LINK_R1_R2_NAME
        assert len(response.link_endpoint_ids) == 2
    
        # ----- List when the object exists --------------------------------------------------------------------------------
        response = context_client.ListLinkIds(Empty())
        assert len(response.link_ids) == 1
        assert response.link_ids[0].link_uuid.uuid == link_uuid
    
        response = context_client.ListLinks(Empty())
        assert len(response.links) == 1
        assert response.links[0].link_id.link_uuid.uuid == link_uuid
        assert response.links[0].name == LINK_R1_R2_NAME
        assert len(response.links[0].link_endpoint_ids) == 2
    
        # ----- Update the object ------------------------------------------------------------------------------------------
        new_link_name = 'new'
        LINK_UPDATED = copy.deepcopy(LINK_R1_R2)
        LINK_UPDATED['name'] = new_link_name
        response = context_client.SetLink(Link(**LINK_UPDATED))
        assert response.link_uuid.uuid == link_uuid
    
        # ----- Check update event -----------------------------------------------------------------------------------------
        event = events_collector.get_event(block=True, timeout=1.0)
        assert isinstance(event, LinkEvent)
        assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE
        assert event.link_id.link_uuid.uuid == link_uuid
    
        # ----- Get when the object is modified ----------------------------------------------------------------------------
        response = context_client.GetLink(LinkId(**LINK_R1_R2_ID))
        assert response.link_id.link_uuid.uuid == link_uuid
        assert response.name == new_link_name
        assert len(response.link_endpoint_ids) == 2
    
        # ----- List when the object is modified ---------------------------------------------------------------------------
        response = context_client.ListLinkIds(Empty())
        assert len(response.link_ids) == 1
        assert response.link_ids[0].link_uuid.uuid == link_uuid
    
        response = context_client.ListLinks(Empty())
        assert len(response.links) == 1
        assert response.links[0].link_id.link_uuid.uuid == link_uuid
        assert response.links[0].name == new_link_name
        assert len(response.links[0].link_endpoint_ids) == 2
    
        # ----- Check relation was created ---------------------------------------------------------------------------------
        response = context_client.GetTopology(TopologyId(**TOPOLOGY_ID))
        assert response.topology_id.context_id.context_uuid.uuid == context_uuid
        assert response.topology_id.topology_uuid.uuid == topology_uuid
        assert len(response.device_ids) == 2
        assert response.device_ids[0].device_uuid.uuid in {device_r1_uuid, device_r2_uuid}
        assert response.device_ids[1].device_uuid.uuid in {device_r1_uuid, device_r2_uuid}
        assert len(response.link_ids) == 1
        assert response.link_ids[0].link_uuid.uuid == link_uuid
    
        # ----- Remove the object ------------------------------------------------------------------------------------------
        context_client.RemoveLink(LinkId(**LINK_R1_R2_ID))
    
        # ----- Check remove event -----------------------------------------------------------------------------------------
        event = events_collector.get_event(block=True, timeout=1.0)
        assert isinstance(event, LinkEvent)
        assert event.event.event_type == EventTypeEnum.EVENTTYPE_REMOVE
        assert event.link_id.link_uuid.uuid == link_uuid
    
        # ----- List after deleting the object -----------------------------------------------------------------------------
        response = context_client.ListLinkIds(Empty())
        assert len(response.link_ids) == 0
    
        response = context_client.ListLinks(Empty())
        assert len(response.links) == 0
    
        response = context_client.GetTopology(TopologyId(**TOPOLOGY_ID))
        assert response.topology_id.context_id.context_uuid.uuid == context_uuid
        assert response.topology_id.topology_uuid.uuid == topology_uuid
        assert len(response.device_ids) == 2
        assert response.device_ids[0].device_uuid.uuid in {device_r1_uuid, device_r2_uuid}
        assert response.device_ids[1].device_uuid.uuid in {device_r1_uuid, device_r2_uuid}
        assert len(response.link_ids) == 0
    
        # ----- Clean dependencies used in the test and capture related events ---------------------------------------------
        context_client.RemoveDevice(DeviceId(**DEVICE_R1_ID))
        context_client.RemoveDevice(DeviceId(**DEVICE_R2_ID))
        context_client.RemoveTopology(TopologyId(**TOPOLOGY_ID))
        context_client.RemoveContext(ContextId(**CONTEXT_ID))
    
        events = events_collector.get_events(block=True, count=4, timeout=1.0)
        assert isinstance(events[0], DeviceEvent)
        assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE
        assert events[0].device_id.device_uuid.uuid == device_r1_uuid
        assert isinstance(events[1], DeviceEvent)
        assert events[1].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE
        assert events[1].device_id.device_uuid.uuid == device_r2_uuid
        assert isinstance(events[2], TopologyEvent)
        assert events[2].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE
        assert events[2].topology_id.context_id.context_uuid.uuid == context_uuid
        assert events[2].topology_id.topology_uuid.uuid == topology_uuid
        assert isinstance(events[3], ContextEvent)
        assert events[3].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE
        assert events[3].context_id.context_uuid.uuid == context_uuid
    
        # ----- Stop the EventsCollector -----------------------------------------------------------------------------------
        events_collector.stop()