Commit 4541fc62 authored by Andrea Sgambelluri's avatar Andrea Sgambelluri
Browse files

Optical controller clean up

parent 916e2b32
Loading
Loading
Loading
Loading
+10 −5
Changes for src/device/service/drivers/oc_driver/OCDriver.py: 10 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -381,12 +381,17 @@ class OCDriver(_Driver):
                                if self.__device_uuid not in OCDriver._processed_transponders:
                                    OCDriver._processed_transponders.add(self.__device_uuid)
                                    
                                    # Store transponder data with its device ID
                                    # Store every channel the device reports, keyed by its real name, so
                                    # consumers (e.g. the optical controller) can resolve the channel/port
                                    # in use from the device itself instead of hardcoded per-device maps.
                                    OCDriver._subcarriergroupdb[self.__device_uuid] = [{
                                        'frequency': optical_channels_params[0].get('frequency'),
                                        'op-mode': optical_channels_params[0].get('operational-mode'),
                                        'target-output-power': optical_channels_params[0].get('target-output-power')
                                    }]
                                        'channel-name': get_channel_name(channel),
                                        'frequency': channel.get('frequency'),
                                        'op-mode': channel.get('operational-mode'),
                                        'target-output-power': channel.get('target-output-power'),
                                        'line-port': channel.get('line-port'),
                                        'status': channel.get('status'),
                                    } for channel in optical_channels_params]
                                    
                                    current_count = len(OCDriver._processed_transponders)
                                    self.__logger.info(f"Processed transponder {self.__device_uuid}. Total processed: {current_count}")
+12 −2
Changes for src/device/service/drivers/oc_driver/Tools.py: 12 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -127,6 +127,14 @@ def extract_optical_channel_data(xml_data):



def get_channel_name(channel : dict):
    # The transponder extractor reports the channel name either as a plain
    # string or wrapped as {'index': 'channel-N'}.
    name = channel.get('name')
    if isinstance(name, dict):
        name = name.get('index')
    return name

def extract_subcarrier_groups(oc_values, device_id):
    # Extract all possible values for each parameter
    subcarrier_groups = {}
@@ -177,10 +185,12 @@ def send_subcarrier_groups(subcarrier_groups, device_id):
        
        for device_id, channels in subcarrier_groups.items():
            for channel in channels:
                formatted_groups[f"DSG{group_counter}--{device_id}--{channel.get('channel-name', 'unknown')}"] = {
                formatted_groups[f"DSG{group_counter}--{device_id}--{channel.get('channel-name') or 'unknown'}"] = {
                    'frequency': channel['frequency'],
                    'op-mode': channel['op-mode'],
                    'target-output-power': channel['target-output-power']
                    'target-output-power': channel['target-output-power'],
                    'line-port': channel.get('line-port'),
                    'status': channel.get('status'),
                }
                group_counter += 1
        
+3 −1
Changes for src/device/service/drivers/oc_driver/readSupport.py: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -45,7 +45,9 @@ def clear_cache():
subcarrier_group_model = api.model('SubcarrierGroup', {
    'frequency': fields.String(description='Center frequency of the subcarrier group'),
    'op-mode': fields.String(description='Operational mode'),
    'target-output-power': fields.String(description='Target output power in dBm')
    'target-output-power': fields.String(description='Target output power in dBm'),
    'line-port': fields.String(description='Line port the channel is attached to, as reported by the device'),
    'status': fields.String(description='Channel admin status, as reported by the device')
})

# Create a dynamic model for the response
+174 −73
Changes for src/opticalcontroller/OpticalController.py: 174 added lines, 73 removed lines.
Original line number Diff line number Diff line
@@ -1735,6 +1735,95 @@ def _hub_received_power_is_out_of_range(device_name: str) -> bool:
_alarm_subscriptions: dict = {}
# ──────────────────────────────────────────────────────────────────────────────

def _load_channel_overrides() -> dict:
    """Optional device-name -> channel pinning (e.g. '{"T1.1": "channel-5"}')
    read from OPTICAL_DEVICE_CHANNEL_MAP. Empty by default: channels/ports
    are derived from what each device reports (see _select_device_channel)."""
    raw = os.environ.get('OPTICAL_DEVICE_CHANNEL_MAP', '').strip()
    if not raw:
        return {}
    try:
        overrides = json.loads(raw)
        if isinstance(overrides, dict):
            return {str(k): str(v) for k, v in overrides.items()}
        LOGGER.warning("OPTICAL_DEVICE_CHANNEL_MAP is not a JSON object, ignoring it: %s", raw)
    except ValueError as e:
        LOGGER.warning("OPTICAL_DEVICE_CHANNEL_MAP is not valid JSON, ignoring it (%s): %s", e, raw)
    return {}

_DEVICE_CHANNEL_OVERRIDES = _load_channel_overrides()


def _channel_index(channel_name):
    """'channel-5' -> '5'; returns None when the name carries no index."""
    if not channel_name:
        return None
    index = str(channel_name).rsplit('-', 1)[-1]
    return index if index.isdigit() else None


def _port_for_channel(channel_name, endpoints=None):
    """Port name for a device channel, matched against the device's own
    endpoints (named either 'N' or 'port-N', possibly typed 'port-N').
    Falls back to the channel-N <-> port-N convention the OC driver uses
    when building its NETCONF messages."""
    index = _channel_index(channel_name)
    if index is None:
        return None
    candidates = {index, f"port-{index}"}
    for ep in endpoints or []:
        ep_name = str(ep.get('name', ''))
        ep_type = str(ep.get('type', '') or ep.get('endpoint_type', ''))
        if ep_name in candidates or ep_type in candidates:
            return ep_type if ep_type.startswith('port-') else f"port-{index}"
    return f"port-{index}"


def _select_device_channel(device_name, dsc_channels, endpoints=None):
    """Pick the channel a transponder is using, from the channels the device
    itself reported (dsc_channels: {channel_name: {...}}). Order of
    preference: an OPTICAL_DEVICE_CHANNEL_MAP override, then channels backed
    by one of the device's endpoints, then ENABLED ones, then the lowest
    channel index. Returns (channel_name, port_name, channel_data), or
    (None, None, None) when the device reported no channels."""
    channels = {
        name: data for name, data in (dsc_channels or {}).items()
        if name and name != 'unknown'
    }

    override = _DEVICE_CHANNEL_OVERRIDES.get(device_name)
    if override:
        if override in channels:
            return override, _port_for_channel(override, endpoints), channels[override]
        LOGGER.warning(
            "OPTICAL_DEVICE_CHANNEL_MAP pins %s to %s, but the device does not report it (has: %s)",
            device_name, override, sorted(channels),
        )

    if not channels:
        # Device data without channel names (older driver): still usable for
        # frequency/power, but no channel/port can be derived from it.
        if dsc_channels:
            return None, None, next(iter(dsc_channels.values()))
        return None, None, None

    endpoint_ids = set()
    for ep in endpoints or []:
        for value in (ep.get('name'), ep.get('type'), ep.get('endpoint_type')):
            if value:
                endpoint_ids.add(str(value).rsplit('-', 1)[-1])

    def _rank(name):
        data = channels[name] or {}
        index = _channel_index(name)
        has_endpoint = index is not None and index in endpoint_ids
        is_enabled = str(data.get('status') or '').upper() == 'ENABLED'
        return (not has_endpoint, not is_enabled, int(index) if index is not None else float('inf'), name)

    channel_name = min(channels, key=_rank)
    return channel_name, _port_for_channel(channel_name, endpoints), channels[channel_name]


class TapiClient:
    """Builds the TAPI-formatted topology response for GET
    /restconf/operations/tapi-topology:get-topology (and is reused as a
@@ -1815,41 +1904,19 @@ class TapiClient:
                    "name": device_name,
                }

                # Map device to specific channel based on device name
                # Channel/port in use are taken from what the device reports
                # (its channels + endpoints), not from its name.
                if device_uuid in dsc_data:
                    dsc_channels = dsc_data[device_uuid]
                    
                    # Determine which channel to use based on device name
                    channel_to_use = None
                    Port_to_use = None
                    
                    if device_name in ['T1.1', 'T2.1']:
                        channel_to_use = 'channel-5'
                        Port_to_use = 'port-5'
                    elif device_name in ['T1.2']:
                        channel_to_use = 'channel-7'
                        Port_to_use = 'port-7'
                    elif device_name in ['T1.3']:
                        channel_to_use = 'channel-3'
                        Port_to_use = 'port-3'
                    # Use the specified channel if available
                    if channel_to_use and channel_to_use in dsc_channels:
                        dsc_entry = dsc_channels[channel_to_use]
                        node.update({
                            "frequency_hz": int(dsc_entry.get('frequency', 0)),
                            "op_mode": int(dsc_entry.get('op-mode', 0)),
                            "Target-output-power": float(dsc_entry.get('target-output-power', 0.0)),
                            "Ports-in-use": Port_to_use,
                            "chan-in-use": channel_to_use  # Optional: include which channel was used
                        })
                    elif dsc_channels:  # Fallback: use first available channel
                        first_channel = next(iter(dsc_channels.values()))
                    channel_to_use, port_to_use, dsc_entry = _select_device_channel(
                        device_name, dsc_data[device_uuid], owned_node_edge_points
                    )
                    if dsc_entry:
                        node.update({
                            "frequency_hz": int(first_channel.get('frequency', 0)),
                            "op_mode": int(first_channel.get('op-mode', 0)),
                            "Target-output-power": float(first_channel.get('target-output-power', 0.0)),
                            "Ports-in-use": Port_to_use,
                            "channel_used": channel_to_use
                            "frequency_hz": int(float(dsc_entry.get('frequency') or 0)),
                            "op_mode": int(float(dsc_entry.get('op-mode') or 0)),
                            "Target-output-power": float(dsc_entry.get('target-output-power') or 0.0),
                            "Ports-in-use": port_to_use,
                            "chan-in-use": channel_to_use
                        })
                
                node["ownedNodeEdgePoint"] = owned_node_edge_points
@@ -2150,22 +2217,20 @@ class ComputeP2MP(Resource):
                }
            }, 500
    
    # Custom group-id -> port mapping for the digital-subcarrier-groups,
    # shared between endpoint local-id/subcarrier reporting and the
    # optical-connection-attributes so both stay consistent.
    _GROUP_PORT_MAP = {1: "port-1", 2: "port-3", 3: "port-5"}

    # Fixed device-name -> port mapping, matching the physical channel
    # assignment used elsewhere (see TapiClient.topology_extract()).
    # Takes priority over the group-id mapping above; if a device isn't
    # listed here, the group-id based mapping is used as before.
    _DEVICE_CHANNEL_MAP = {"T1.1": "port-5", "T1.2": "port-7", "T1.3": "port-3", "T2.1": "port-5"}

    def _channel_for_group(self, group_id, device_name=None):
        port = self._DEVICE_CHANNEL_MAP.get(device_name)
        if port is None:
            port = self._GROUP_PORT_MAP.get(group_id, f"port-{group_id}")
        return f"channel-{port.split('-')[1]}", port
    @staticmethod
    def _channel_for_group(group_id, device_name=None, device_channels=None):
        """(channel, port) for a subcarrier group's source device: the
        channel/port that device reports as in use (see
        TapiClient.topology_extract), falling back to channel-<group_id> /
        port-<group_id> when the device reported nothing."""
        channel, port = (device_channels or {}).get(device_name, (None, None))
        if channel and port:
            return channel, port
        if port:
            return f"channel-{port.rsplit('-', 1)[-1]}", port
        if channel:
            return channel, _port_for_channel(channel)
        return f"channel-{group_id}", f"port-{group_id}"

    # Fixed device-name -> center-frequency (Hz) mapping, for the sake of a
    # demo. Takes priority over the group's own reported/derived frequency;
@@ -2204,11 +2269,9 @@ class ComputeP2MP(Resource):
        caller set it first) plus per-endpoint spectrum config, and one
        top-level optical-connection-attributes block
        (_create_optical_attributes) describing modulation/power/subcarrier
        layout. Device -> physical-port/channel/frequency mappings are
        governed by the fixed lookup tables above
        (_DEVICE_CHANNEL_MAP/_GROUP_PORT_MAP/_DEVICE_FREQUENCY_MAP) rather
        than derived purely from the topology, since this is a fixed demo
        rig rather than a general deployment."""
        layout. Each device's channel/port comes from the topology nodes,
        i.e. from what the device itself reports (chan-in-use/Ports-in-use);
        only the per-device frequency still uses _DEVICE_FREQUENCY_MAP."""

        # Main frequency (no unit conversion here if your dscm gives Hz already)
        freq_raw = dscm_flow.get('frequency') or dscm_flow.get('central-frequency') or dscm_flow.get('central_frequency')
@@ -2234,6 +2297,7 @@ class ComputeP2MP(Resource):
        device_uuid_map = {}       # device_name -> node_uuid
        device_port_map = {}       # device_name -> { port_name : endpoint_uuid }
        devices_by_role = {}       # role -> [node_name,...]
        device_channels = {}       # device_name -> (channel_in_use, port_in_use), as reported by the device

        for node in nodes:
            name = node.get('name')
@@ -2244,6 +2308,8 @@ class ComputeP2MP(Resource):
                device_uuid_map[name] = n_uuid
            if role:
                devices_by_role.setdefault(role, []).append(name)
            if name and (node.get('chan-in-use') or node.get('Ports-in-use')):
                device_channels[name] = (node.get('chan-in-use'), node.get('Ports-in-use'))

            port_map = {}
            for ep in node.get('ownedNodeEdgePoint', []) or []:
@@ -2260,6 +2326,11 @@ class ComputeP2MP(Resource):
            if name:
                device_port_map[name] = port_map

        def _in_use_port_index(node_name):
            # Index of the port the device reports as in use ('port-5' -> '5'), if any.
            _channel, port = device_channels.get(node_name, (None, None))
            return port.rsplit('-', 1)[-1] if port else None

        # --- build endpoints: destination first (only one) ---
        endpoints = []

@@ -2272,7 +2343,10 @@ class ComputeP2MP(Resource):
        dest_sip = None
        if dest_node_name:
            ports = device_port_map.get(dest_node_name, {})
            if "1" in ports and ports["1"]:
            dest_port_index = _in_use_port_index(dest_node_name)
            if dest_port_index and ports.get(dest_port_index):
                dest_sip = ports[dest_port_index]
            elif "1" in ports and ports["1"]:
                dest_sip = ports["1"]
            elif "port-1" in ports and ports["port-1"]:
                dest_sip = ports["port-1"]
@@ -2284,11 +2358,13 @@ class ComputeP2MP(Resource):
            if not dest_sip:
                dest_sip = device_uuid_map.get(dest_node_name, dest_node_name)

            # If the destination device has a fixed channel assignment, use it
            # as the local-id; otherwise fall back to what it currently has
            # (the SIP uuid).
            dest_port = self._DEVICE_CHANNEL_MAP.get(dest_node_name)
            dest_local_id = f"channel-{dest_port.split('-')[1]}" if dest_port else dest_sip
            # Use the channel the destination device reports as in use as the
            # local-id; fall back to the SIP uuid if it reported none.
            dest_local_id = dest_sip
            if dest_node_name in device_channels:
                dest_local_id, _dest_port = self._channel_for_group(
                    1, dest_node_name, device_channels
                )

            endpoints.append({
                "direction": "BIDIRECTIONAL",
@@ -2335,7 +2411,12 @@ class ComputeP2MP(Resource):
        n_source_nodes = max(1, len(source_nodes))
        for idx, group in enumerate(subcarrier_groups):
            src_node = source_nodes[idx % n_source_nodes]
            # The device's in-use port only for the first group a node carries;
            # further groups on the same node keep their own per-group port so
            # each subcarrier group still lands on a distinct SIP.
            preferred_port = str(idx + 1)
            if idx < n_source_nodes:
                preferred_port = _in_use_port_index(src_node) or preferred_port
            ports = device_port_map.get(src_node, {})

            src_sip = None
@@ -2377,7 +2458,7 @@ class ComputeP2MP(Resource):

            raw_gid = group.get('digital-subcarrier-group-id') or group.get('group-id')
            gid = int(raw_gid) if raw_gid is not None else (idx + 1)
            channel_name, _channel_port = self._channel_for_group(gid, src_node)
            channel_name, _channel_port = self._channel_for_group(gid, src_node, device_channels)

            endpoints.append({
                "direction": "BIDIRECTIONAL",
@@ -2401,7 +2482,10 @@ class ComputeP2MP(Resource):


        # optical attributes
        optical_attrs = self._create_optical_attributes(main_freq, Tx_power, op_mode, subcarrier_groups, source_nodes)
        optical_attrs = self._create_optical_attributes(
            main_freq, Tx_power, op_mode, subcarrier_groups, source_nodes,
            device_channels=device_channels, dest_node_name=dest_node_name
        )

        tapi_response = {
            "tapi-connectivity:connectivity-service": {
@@ -2417,10 +2501,14 @@ class ComputeP2MP(Resource):



    def _create_optical_attributes(self, frequency_hz, Tx_power, op_mode, subcarrier_groups, source_nodes=None):
    def _create_optical_attributes(
        self, frequency_hz, Tx_power, op_mode, subcarrier_groups, source_nodes=None,
        device_channels=None, dest_node_name=None
    ):
        modulation_map = {1: "DP-QPSK", 4: "DP-QPSK", 9: "DP-16QAM"}
        # top-level port is port-1
        top_port = "port-1"
        # top-level port: the one the destination device reports as in use
        # (port-1 when it reported none)
        top_port = (device_channels or {}).get(dest_node_name, (None, None))[1] or "port-1"

        optical_attrs = {
            "central-frequency": int(frequency_hz),
@@ -2453,7 +2541,7 @@ class ComputeP2MP(Resource):
                ) or int(frequency_hz)
                g_freq = self._frequency_for_device(src_node, g_freq)

                _channel_name, port = self._channel_for_group(gid, src_node)
                _channel_name, port = self._channel_for_group(gid, src_node, device_channels)

                groups.append({
                    "group-id": gid,
@@ -2462,7 +2550,7 @@ class ComputeP2MP(Resource):
                    "operational-mode": opm,
                    "Tx-power": g.get('Tx-power', 0),
                    "group-size": g.get('digital-subcarrier-group-size'),
                    # use shared group-id -> port mapping (fallback to sequential if not defined)
                    # port the source device reports as in use (port-<group-id> if none)
                    "port": port,
                    "subcarrier-id": g.get('digital-subcarrier-id', [])
                })
@@ -3134,11 +3222,24 @@ def _subcarriers_for_source(record: dict, source: str):
        return subcarriers_list[0] if subcarriers_list else None


# Channel each source device's pluggable KPIs are actually read from --
# mirrors _DEVICE_CHANNEL_MAP's chan-in-use values above (T1.1 -> channel-5,
# T1.2 -> channel-7, T1.3 -> channel-3) since KPI descriptors need the same
# physical channel as the topology/path-computation code already uses.
_DEVICE_KPI_CHANNEL_MAP = {"T1.1": "channel-5", "T1.2": "channel-7", "T1.3": "channel-3", "T2.1": "channel-5"}
def _kpi_channel_for_device(device) -> str:
    """Channel a device's pluggable KPIs are read from: the same channel the
    topology/path-computation code uses for it (TapiClient.topology_extract),
    derived from what the device reports rather than from its name."""
    endpoints = [{'name': ep.name, 'type': ep.endpoint_type} for ep in device.device_endpoints]
    dsc_channels = tapi_client.read_DSC_only().get(device.device_id.device_uuid.uuid, {})
    channel, _port, _data = _select_device_channel(device.name, dsc_channels, endpoints)
    if channel:
        return channel
    # Device published no channel data yet: use its first port endpoint.
    indexes = sorted(
        int(i) for i in (_channel_index(ep['name']) or _channel_index(ep['type']) for ep in endpoints)
        if i is not None
    )
    if indexes:
        return f"channel-{indexes[0]}"
    LOGGER.warning("No channel/port data reported by %s, monitoring channel-1", device.name)
    return "channel-1"

# Every KPI sample type this controller evaluates for a leaf device (see
# RECEIVED_POWER_RANGE_DBM / PRE_FEC_BER_RANGE above) -- both are started for
@@ -3268,7 +3369,7 @@ def _start_leaf_monitoring(device_name: str) -> None:
            )
            return
        device_uuid = device.device_id.device_uuid.uuid
        channel = _DEVICE_KPI_CHANNEL_MAP.get(device_name, "channel-5")
        channel = _kpi_channel_for_device(device)

        kpi_manager_client = KpiManagerClient()
        telemetry_client   = TelemetryFrontendClient()
+71 −8

File changed.

Preview size limit exceeded, changes collapsed.

Loading