Commit 14765779 authored by Waleed Akbar's avatar Waleed Akbar
Browse files

feat: Refactor extract_network_data to use get_device for improved device retrieval

parent 8d8e0d54
Loading
Loading
Loading
Loading
+9 −38
Original line number Diff line number Diff line
@@ -16,31 +16,14 @@
import logging
from typing import Dict, List, Tuple
from context.client.ContextClient import ContextClient
from common.proto.context_pb2 import DeviceId, DeviceIdList
from common.tools.context_queries.Device import get_device
from .SimapClient import SimapClient


LOGGER = logging.getLogger(__name__)




# Use connection event log at IP: [2026-02-20 21:20:21,224] INFO:simap_connector.service.simap_updater.SimapUpdater:Processing Connection: {"connection_id": {"connection_uuid": {"uuid": "fdb61970-1a08-4f0e-acec-95d82a4b0d32"}}, "path_hops_endpoint_ids": [{"device_id": {"device_uuid": {"uuid": "c4b22f0f-d958-5895-a452-cac82e11ef90"}}, "endpoint_uuid": {"uuid": "97f60155-b852-5607-9ba5-1b41e228f04d"}, "topology_id": {"context_id": {"context_uuid": {"uuid": "43813baf-195e-5da6-af20-b3d0922e71a7"}}, "topology_uuid": {"uuid": "c76135e3-24a8-5e92-9bed-c3c9139359c8"}}}, {"device_id": {"device_uuid": {"uuid": "c4b22f0f-d958-5895-a452-cac82e11ef90"}}, "endpoint_uuid": {"uuid": "f9cea78e-0de3-5c8a-93f7-b99d207ae709"}, "topology_id": {"context_id": {"context_uuid": {"uuid": "43813baf-195e-5da6-af20-b3d0922e71a7"}}, "topology_uuid": {"uuid": "c76135e3-24a8-5e92-9bed-c3c9139359c8"}}}, {"device_id": {"device_uuid": {"uuid": "706e20a6-1f43-522d-9500-0bafd8131899"}}, "endpoint_uuid": {"uuid": "7dfc3453-a6df-584d-9e74-8ad4bfa301da"}, "topology_id": {"context_id": {"context_uuid": {"uuid": "43813baf-195e-5da6-af20-b3d0922e71a7"}}, "topology_uuid": {"uuid": "c76135e3-24a8-5e92-9bed-c3c9139359c8"}}}, {"device_id": {"device_uuid": {"uuid": "706e20a6-1f43-522d-9500-0bafd8131899"}}, "endpoint_uuid": {"uuid": "559070fb-857e-56b4-8453-dd427c489f59"}, "topology_id": {"context_id": {"context_uuid": {"uuid": "43813baf-195e-5da6-af20-b3d0922e71a7"}}, "topology_uuid": {"uuid": "c76135e3-24a8-5e92-9bed-c3c9139359c8"}}}, {"device_id": {"device_uuid": {"uuid": "7ae7e7bc-c4db-50a2-ad59-b5b7d9bcdc47"}}, "endpoint_uuid": {"uuid": "842aa058-7ac6-57f4-bca7-496070518b11"}, "topology_id": {"context_id": {"context_uuid": {"uuid": "43813baf-195e-5da6-af20-b3d0922e71a7"}}, "topology_uuid": {"uuid": "c76135e3-24a8-5e92-9bed-c3c9139359c8"}}}, {"device_id": {"device_uuid": {"uuid": "7ae7e7bc-c4db-50a2-ad59-b5b7d9bcdc47"}}, "endpoint_uuid": {"uuid": "dc6ac139-e4c9-5b27-a693-17e4a06aaf37"}, "topology_id": {"context_id": {"context_uuid": {"uuid": "43813baf-195e-5da6-af20-b3d0922e71a7"}}, "topology_uuid": {"uuid": "c76135e3-24a8-5e92-9bed-c3c9139359c8"}}}], "service_id": {"context_id": {"context_uuid": {"uuid": "43813baf-195e-5da6-af20-b3d0922e71a7"}}, "service_uuid": {"uuid": "e1ed09d2-dc76-5183-a245-855e5a596af2"}}, "settings": {}, "sub_service_ids: []}
# We need to extract the connection's path hops and identify which links are involved, then determine the domain (topology) to which this connection belongs, and finally filter links based on allowed links per controller. 
def extract_network_data(context_client: ContextClient, network_id: str, network_connection: dict) -> list[tuple[str, dict]]:
    """
    Extract network data from a connection object for SIMAP hierarchical network configuration.
    Extracts only the first and last devices (Service Demarcation Points) with their service-facing endpoints.
    
    Args:
        network_id: The network identifier (e.g., 'e2e', 'agg', 'trans-pkt')
        network_connection: Dictionary representation of a Connection protobuf message containing path_hops_endpoint_ids
    
    Returns:
        List of exactly 2 tuples: [(first_device_name, {'termination_points': [endpoint]}), 
                                     (last_device_name, {'termination_points': [endpoint]})]
        For example: [('P-PE1', {'termination_points': ['200']}), ('P-PE2', {'termination_points': ['200']})]
    """
    try:
        # Extract path_hops_endpoint_ids from network_connection dict
        path_hops = network_connection.get('path_hops_endpoint_ids', [])
@@ -72,21 +55,15 @@ def extract_network_data(context_client: ContextClient, network_id: str, network
        network_data: List[Tuple[str, Dict[str, List[str]]]] = []
        
        # Process first device (sdp1)
        first_device_id = DeviceId()
        first_device_id.device_uuid.uuid = first_device_uuid
        
        try:
            device_list = context_client.SelectDevice(
                DeviceIdList(device_ids=[first_device_id]),
                include_endpoints=True,
                include_config_rules=False,
                include_components=False
            first_device = get_device(
                context_client, first_device_uuid, rw_copy=False,
                include_endpoints=True, include_config_rules=False, include_components=False
            )
            if not device_list.devices:
            if first_device is None:
                LOGGER.warning(f"First device with UUID {first_device_uuid} not found in context")
                return []
            
            first_device = device_list.devices[0]
            first_device_name = first_device.name
            
            # Find the service-facing endpoint name
@@ -107,21 +84,15 @@ def extract_network_data(context_client: ContextClient, network_id: str, network
            return []
        
        # Process last device (sdp2)
        last_device_id = DeviceId()
        last_device_id.device_uuid.uuid = last_device_uuid
        
        try:
            device_list = context_client.SelectDevice(
                DeviceIdList(device_ids=[last_device_id]),
                include_endpoints=True,
                include_config_rules=False,
                include_components=False
            last_device = get_device(
                context_client, last_device_uuid, rw_copy=False,
                include_endpoints=True, include_config_rules=False, include_components=False
            )
            if not device_list.devices:
            if last_device is None:
                LOGGER.warning(f"Last device with UUID {last_device_uuid} not found in context")
                return []
            
            last_device = device_list.devices[0]
            last_device_name = last_device.name
            
            # Find the service-facing endpoint name