(CNIT) OpenConfig driver support for P2MP devices

Proposers

  • Marc Lawson (CNIT)
  • Andrea Sgambelluri (CNIT)
  • Nicola Sambo (CNIT)

Description

Driver-side support for point-to-multipoint optical services, plus a device deletion fix.

The OC driver can now configure a transponder with one digital subcarrier group per remote endpoint: device/service/Tools.py derives the per-flow slot information, the transponder template renders the per-remote channels, and tsp_tools holds the shared helpers. Provisioning and discovery templates for OpenROADM devices are added alongside the existing ROADM ones, since a P2MP path traverses the wavelength routers between hub and leaves.

Device deletion no longer fails when the device has already dropped its NETCONF session: close_session raised "Not connected to NETCONF server" and Disconnect propagated it, leaving the device in Context after it had gone. Both are now best effort. Committed separately.

Closes #439 (closed)

Acknowledgements

This work is funded by the European Commission through the HORIZON-JU-SNS-2023 PROTEUS-6G project with Grant Agreement number 101139134.

Edited by Andrea Sgambelluri

Merge request reports

Loading
+14 −1
Changes for src/device/service/driver_api/DriverInstanceCache.py: 14 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -65,7 +65,13 @@ class DriverInstanceCache:
        with self._lock:
            device_driver = self._device_uuid__to__driver_instance.pop(device_uuid, None)
            if device_driver is None: return
            try:
                device_driver.Disconnect()
            except: # pylint: disable=bare-except
                # The driver is already out of the cache; a failed disconnect (e.g. the device
                # dropped the session) must not prevent the device from being deleted.
                msg = 'Error disconnecting Driver({:s}) from device. Removing it anyway...'
                LOGGER.exception(msg.format(device_uuid))

    def terminate(self) -> None:
        self._terminate.set()
@@ -111,4 +117,11 @@ def get_driver(driver_instance_cache : DriverInstanceCache, device : Device) ->
def preload_drivers(driver_instance_cache : DriverInstanceCache) -> None:
    context_client = ContextClient()
    devices = context_client.ListDevices(Empty())
    for device in devices.devices: get_driver(driver_instance_cache, device)
    for device in devices.devices:
        # An unreachable device must not take the whole Device component down at startup;
        # its driver is instantiated again on the next request that needs it.
        try:
            get_driver(driver_instance_cache, device)
        except: # pylint: disable=bare-except
            msg = 'Unable to pre-load driver for Device({:s}); skipping it'
            LOGGER.exception(msg.format(device.device_id.device_uuid.uuid))
+7 −2
Changes for src/device/service/drivers/oc_driver/templates/VPN/common.py: 7 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ def seperate_port_config(resources:list,unwanted_keys=[])->list[list,dict,str]:
    ports={}
    index=None
    for item in resources :
        if 'resource_key' not in item: continue
        if len(unwanted_keys)>0:
            if (item['value'] is not None and (item['resource_key']  not in unwanted_keys)):
                config.append({'resource_key':item['resource_key'], 'value':item['value']} )
@@ -35,7 +36,7 @@ def seperate_port_config(resources:list,unwanted_keys=[])->list[list,dict,str]:
def extract_ports (resources:list):
    if len(resources) ==0 :return 
    ports=[]
    flow=next((i for i in resources if i['resource_key']=='handled_flow'),None)
    flow=next((i for i in resources if i.get('resource_key')=='handled_flow'),None)
    if flow is not None:
        ports = flow['value']
    return ports 
@@ -45,8 +46,12 @@ def filter_config(resources:list,unwanted_keys=[])->list[list,dict,str]:
    ports=()
    index=None
    for item in resources :
        # Entries without a resource_key (e.g. DSCM 'channel_track') are not device config leaves.
        if 'resource_key' not in item: continue
        if len(unwanted_keys)>0:
            if (item['value'] is not None and (item['resource_key']  not in unwanted_keys)):
            # Structured values (e.g. per_flow_slots, slot_info) cannot be rendered as an XML leaf.
            if (item['value'] is not None and (item['resource_key']  not in unwanted_keys)
                    and not isinstance(item['value'], (list, tuple, dict))):
                config.append({'resource_key':item['resource_key'], 'value':item['value']} )
        if (item['resource_key']=='index' and item['value'] is not None)     :
            index=item['value']        
+187 −0
Changes for src/device/service/drivers/oc_driver/templates/VPN/openroadm.py: 187 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Copyright 2022-2026 ETSI SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
#
# 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 yattag import Doc, indent
import logging
from .common  import seperate_port_config ,filter_config
from decimal import Decimal



create_mc_err= '''
 <config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<org-openroadm-device xmlns="http://org/openroadm/device">
  <interface>
    <name>MC-TTP-DEG2-RX-193.3</name>
    <description>Media-Channel-TTP-193.3THz-degree-2-in</description>
    <type>openROADM-if:mediaChannelTrailTerminationPoint</type>
    <administrative-state>inService</administrative-state>
    <supporting-circuit-pack-name>DEG2-AMPRX</supporting-circuit-pack-name>
    <supporting-port>DEG2-AMPRX-IN</supporting-port>
    <supporting-interface-list>OMS-DEG2-TTP-RX</supporting-interface-list>
    <mc-ttp xmlns="http://org/openroadm/media-channel-interfaces">
      <min-freq>193.25</min-freq>
      <max-freq>193.35</max-freq>
    </mc-ttp>
  </interface>
  <interface>
    <name>MC-TTP-DEG2-TX-193.3</name>
    <description>Media-Channel-TTP-193.3THz-degree-2-out</description>
    <type>openROADM-if:mediaChannelTrailTerminationPoint</type>
    <administrative-state>inService</administrative-state>
    <supporting-circuit-pack-name>DEG2-AMPTX</supporting-circuit-pack-name>
    <supporting-port>DEG2-AMPTX-OUT</supporting-port>
    <supporting-interface-list>OMS-DEG2-TTP-TX</supporting-interface-list>
    <mc-ttp xmlns="http://org/openroadm/media-channel-interfaces">
      <min-freq>193.25</min-freq>
      <max-freq>193.35</max-freq>
    </mc-ttp>
  </interface>
</org-openroadm-device>


</config>

'''

def define_interface_name (type:str,interface_list:str,freq:int)->str:
    interface_str = interface_list.split('-')
    port_rank=''
    port_type=''
    if (len(interface_str)==4):
        port_rank=interface_str[1]
        port_type=interface_str[3]
    elif (len(interface_str)==5):
        port_rank=interface_str[2]
        port_type=interface_str[3]
    else :
        port_rank=interface_list
        port_type=interface_list+'type'


    return f'{type.upper()}-{port_rank}-{port_type}-{freq}'



def create_media_channel (resources):

    frequency_dict=next((r for r in resources if r['resource_key']== 'frequency'),None)
    width_dict=next((r for r in resources if r['resource_key']== 'width'),None)
    interfaces_lists =next((r for r in resources if r['resource_key']== 'interfaces'),None)
    administrative_state= next((r for r in resources if r['resource_key']== 'administrative-state'),None)
    min_freq= int(Decimal(frequency_dict["value"])*1000) - (int(width_dict["value"])/2)
    max_freq = int(Decimal(frequency_dict["value"])*1000) + (int(width_dict["value"])/2)
    #config,_,_ = filter_config(resources=resources,unwanted_keys=unwanted_keys)

    or_device_ns="http://org/openroadm/device"
    or_interface_ns="http://org/openroadm/interfaces"

    results=[]
    logging.info(f"from openroadm mc {resources}")
    doc, tag, text = Doc().tagtext()
    with tag('config',xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
        with tag('org-openroadm-device', ('xmlns',or_device_ns)):

            for interface in interfaces_lists["value"]:
                port = next((r for r in interface if r['resource_key']=='supporting-port'),None)
                circuit_pack =next((r for r in interface if r['resource_key']=='supporting-circuit-pack-name'),None)
                interface_list = next((r for r in interface if r['resource_key']=='supporting-interface-list'),None)
                mc_name = define_interface_name('mc-ttp',interface_list['value'],frequency_dict['value'])
                interface.append({'resource_key':'mc_name','value':mc_name})
                with tag('interface'):

                        with tag('name'):text(mc_name)
                        with tag('description'):text(f'Media-channel-{frequency_dict["value"]}THz')
                        with tag('type'):text("openROADM-if:mediaChannelTrailTerminationPoint")
                        with tag('administrative-state'):text(administrative_state["value"])
                        with tag('supporting-circuit-pack-name'):text(circuit_pack["value"])
                        with tag('supporting-port'):text(port["value"])
                        with tag('supporting-interface-list'):text(interface_list["value"])
                        with tag('mc-ttp',xmlns="http://org/openroadm/media-channel-interfaces"):
                            with tag('max-freq'):text(max_freq)
                            with tag('min-freq'):text(min_freq)



    result = indent(
                    doc.getvalue(),
                    indentation = ' '*2,
                    newline = ''
                )
    results.append(result)
    logging.info(f"from openroadm mc results {results}")
    return [results,resources]




def create_network_media_channel (resources):

    logging.info(f"nmc resources {resources}")

    unwanted_keys= ['max-freq','min-freq']
    #config,_,_ = filter_config(resources=resources,unwanted_keys=unwanted_keys)

    or_device_ns="http://org/openroadm/device"
    frequency_dict=next((r for r in resources if r['resource_key']== 'frequency'),None)
    width_dict=next((r for r in resources if r['resource_key']== 'width'),None)
    interfaces_lists =next((r for r in resources if r['resource_key']== 'interfaces'),None)
    administrative_state= next((r for r in resources if r['resource_key']== 'administrative-state'),None)


    results=[]
    doc, tag, text = Doc().tagtext()
    with tag('config',xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
        with tag('org-openroadm-device', ('xmlns',or_device_ns)):
              for interface in interfaces_lists["value"]:
                port = next((r for r in interface if r['resource_key']=='supporting-port'),None)
                circuit_pack =next((r for r in interface if r['resource_key']=='supporting-circuit-pack-name'),None)
                interface_list = next((r for r in interface if r['resource_key']=='mc_name'),None)
                nmc_name = define_interface_name('nmc-ctp',interface_list['value'],frequency_dict['value'])

                with tag('interface'):

                        with tag('name'):text(nmc_name)
                        with tag('description'):text(f'Media-channel-{frequency_dict["value"]}THz')
                        with tag('type'):text("openROADM-if:networkMediaChannelConnectionTerminationPoint")
                        with tag('administrative-state'):text(administrative_state["value"])
                        with tag('supporting-circuit-pack-name'):text(circuit_pack["value"])
                        with tag('supporting-port'):text(port["value"])
                        with tag('supporting-interface-list'):text(interface_list["value"])
                        with tag('nmc-ctp',xmlns="http://org/openroadm/network-media-channel-interfaces"):
                            with tag('frequency'):text(frequency_dict['value'])
                            with tag('width'):text(width_dict['value'])



    result = indent(
                    doc.getvalue(),
                    indentation = ' '*2,
                    newline = ''
                )
    results.append(result)
    logging.info(f"nmc message {results}")
    return results


def network_media_channel_handler (resources):
     unwanted_keys=["config_type"]
     config,_,_ = filter_config(resources=resources,unwanted_keys=unwanted_keys)
     mc_list,resources_updated= create_media_channel(resources=config)
     nmc_list= create_network_media_channel(resources=resources_updated)
     mc_list.extend(nmc_list)

     return mc_list
+246 −13
Changes for src/device/service/drivers/oc_driver/templates/VPN/roadms.py: 246 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -20,19 +20,18 @@ from .common import seperate_port_config ,filter_config


         


 

         
def create_media_channel (resources):
    optical_band_namespaces="http://flex-scale-project.eu/yang/flex-scale-mg-on"
    # Validate resources structure
    if not all(isinstance(r, dict) and 'resource_key' in r for r in resources):
        logging.error("Invalid resources structure for media channel")
    results=[]
    unwanted_keys=['destination_port','source_port','channel_namespace'
                ,'frequency','operational-mode','target-output-power',
               "handled_flow","channel_num",'bidir']
               "handled_flow","channel_num",'bidir',
               'Band']   # requested optical-band width (P2MP settings), not a media-channel leaf

    bidir = next(i['value'] for i in resources if i['resource_key'] == 'bidir')
    bidir = next((i['value'] for i in resources if 'resource_key' in i and i['resource_key'] == 'bidir'), 1)
    config,ports,index=filter_config(resources,unwanted_keys)
    logging.info(f'bidir {bidir}')

@@ -43,6 +42,8 @@ def create_media_channel (resources):
        with tag('config',xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
            with tag('wavelength-router', xmlns="http://openconfig.net/yang/wavelength-router"):
                    with tag('media-channels'):


                            src,dest=flow
                            with tag('channel', operation="create"):
                            #with tag('channel'):
@@ -60,6 +61,7 @@ def create_media_channel (resources):
                                        else:
                                            with tag(resource['resource_key']):text(resource['value'])


                                if src is not None and src != '0':                    
                                    with tag('source'):
                                            with tag('config'):  
@@ -69,7 +71,10 @@ def create_media_channel (resources):
                                            with tag('config'):  
                                                with tag('port-name'):text(dest)  

                                                       
        n+=1


        result = indent(
                    doc.getvalue(),
                    indentation = ' '*2,
@@ -77,6 +82,8 @@ def create_media_channel (resources):
                )
        results.append(result)
        if not bidir  : break

    logging.info(f"roadm create_media_channel {results}")
    return results
        

@@ -85,19 +92,27 @@ def create_optical_band (resources) :
    results =[]
    unwanted_keys=['destination_port','source_port','channel_namespace'
                   ,'frequency','optical-band-parent'
                   ,'handled_flow','bidir']
                   ,'handled_flow','bidir','Band']
    config,ports,index= filter_config(resources,unwanted_keys=unwanted_keys)

    #with tag('config'):
    n = 0
    for flow in ports:
        doc, tag, text = Doc().tagtext()
        with tag('config',xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
            with tag('wavelength-router', xmlns="http://openconfig.net/yang/wavelength-router"):
                with tag('optical-bands',xmlns="http://flex-scale-project.eu/yang/flex-scale-mg-on"):


                        #with tag('optical-band', operation="create"):
                        src,dest=flow

                        with tag('optical-band'):
                            if index is not None:
                                with tag('index'):text(str(int(index)+n))
                            with tag('config'):
                                #if index is not None:
                                #    with tag('index'):text(str(int(index)+i))
                                for resource in config:       
                                    if resource['resource_key'] == "index":
                                        with tag('index'):text(str(int(index)+n))
@@ -114,28 +129,34 @@ def create_optical_band (resources) :
                                    with tag('config'):  
                                        with tag('port-name'):text(src)   
        n +=1                


        result = indent(
                    doc.getvalue(),
                    indentation = ' '*2,
                    newline = ''
                )
        results.append(result)
    logging.info(f"roadm create_optical_band {results}")
    return results


def disable_media_channel (resources):
    
    results=[]
    bidir = next(i['value'] for i in resources if i['resource_key'] == 'bidir')
    bidir = next((i['value'] for i in resources if 'resource_key' in i and i['resource_key'] == 'bidir'), 1)
    unwanted_keys=['destination_port','source_port'
                   ,'channel_namespace','frequency'
                   ,'operational-mode', 'optical-band-parent'
                   ,'bidir'
                   ]
    config,ports,index= seperate_port_config(resources,unwanted_keys=unwanted_keys)
    # one media channel per flow (index, index+1, ...), as created by create_media_channel();
    # ports holds the per-flow source/destination lists, so iterating it directly only covers 2
    num_flows = max(len(ports.get('source_port') or []), len(ports.get('destination_port') or []), 1)

    n = 0
    for flow in ports:
    for flow in range(num_flows):
        doc, tag, text = Doc().tagtext()
        with tag('config',xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
            with tag('wavelength-router', xmlns="http://openconfig.net/yang/wavelength-router"):
@@ -146,6 +167,21 @@ def disable_media_channel (resources):
                            with tag('index'):text(str(int(index) + n))

        n +=1




        '''
        doc, tag, text = Doc().tagtext()
        #with tag('config'):
        with tag('config',xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
        with tag('wavelength-router', xmlns="http://openconfig.net/yang/wavelength-router"):
            with tag('media-channels'):
                with tag("channel",operation="delete"):
                    with tag('index'):text(str(int(index)))
                    with tag('config'):
                        with tag('index'):text(str(int(index)))
        '''
        result = indent(
                    doc.getvalue(),
                    indentation = ' '*2,
@@ -161,7 +197,7 @@ def disable_optical_band (resources:list,state:str):
                   ,'channel_namespace','frequency'
                   ,'operational-mode', 'optical-band-parent'
                   ,"bidir"]
    _,_,index= seperate_port_config(resources,unwanted_keys=unwanted_keys)
    config,ports,index= seperate_port_config(resources,unwanted_keys=unwanted_keys)
    doc, tag, text = Doc().tagtext()
    #with tag('config'):
    with tag('config',xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
@@ -187,7 +223,7 @@ def disable_optical_band (resources:list,state:str):
def disable_optical_band_v2 (resources:list,state:str):
    results=[]
    unwanted_keys=['destination_port','source_port','channel_namespace','frequency','operational-mode', 'optical-band-parent']
    _,ports,index= seperate_port_config(resources,unwanted_keys=unwanted_keys)
    config,ports,index= seperate_port_config(resources,unwanted_keys=unwanted_keys)
    n = 0
    for flow in ports:
        doc, tag, text = Doc().tagtext()
@@ -201,6 +237,21 @@ def disable_optical_band_v2 (resources:list,state:str):
                            with tag('index'):text(str(int(index) + n))
    
        n +=1                


        '''
        doc, tag, text = Doc().tagtext()
        #with tag('config'):
        with tag('config',xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
        with tag('wavelength-router', xmlns="http://openconfig.net/yang/wavelength-router"):
            with tag('optical-bands',xmlns="http://flex-scale-project.eu/yang/flex-scale-mg-on"):
                with tag('optical-band',operation="delete"):
                    if index is not None:
                        with tag('index'):text(index)
                    with tag('config'):
                        with tag('index'):text(index)

        '''
        result = indent(
                    doc.getvalue(),
                    indentation = ' '*2,
@@ -214,7 +265,26 @@ def disable_optical_band_v2 (resources:list,state:str):
def delete_optical_band (resources:list):
    results=[]
    unwanted_keys=['destination_port','source_port','channel_namespace','frequency','operational-mode', 'optical-band-parent']
    _,ports,index= seperate_port_config(resources,unwanted_keys=unwanted_keys)
    config,ports,index= seperate_port_config(resources,unwanted_keys=unwanted_keys)
    # doc, tag, text = Doc().tagtext()
    # #with tag('config'):
    # with tag('config',xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
    #   with tag('wavelength-router', xmlns="http://openconfig.net/yang/wavelength-router"):
    #     with tag('optical-bands',xmlns="http://flex-scale-project.eu/yang/flex-scale-mg-on"):
    #         with tag('optical-band',operation="delete"):
    #             if index is not None:
    #                 with tag('index'):text(index)
    #             with tag('config'):
    #                 with tag('index'):text(index)

    # result = indent(
    #             doc.getvalue(),
    #             indentation = ' '*2,
    #             newline = ''
    #         )
    # results.append(result)
    # return results

    n = 0
    for key,v in ports.items():
        if isinstance(v,list): 
@@ -232,6 +302,21 @@ def delete_optical_band (resources:list):
                            with tag('index'):text(str(int(index) + n))
    
        n +=1                


        '''
        doc, tag, text = Doc().tagtext()
        #with tag('config'):
        with tag('config',xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
        with tag('wavelength-router', xmlns="http://openconfig.net/yang/wavelength-router"):
            with tag('optical-bands',xmlns="http://flex-scale-project.eu/yang/flex-scale-mg-on"):
                with tag('optical-band',operation="delete"):
                    if index is not None:
                        with tag('index'):text(index)
                    with tag('config'):
                        with tag('index'):text(index)

        '''
        result = indent(
                    doc.getvalue(),
                    indentation = ' '*2,
@@ -239,3 +324,151 @@ def delete_optical_band (resources:list):
                )
        results.append(result)
    return results                          


def create_media_channel_v2(resources):
    optical_band_namespaces = "http://flex-scale-project.eu/yang/flex-scale-mg-on"
    results = []

    # Configuration filtering
    unwanted_keys = [
        'destination_port', 'source_port', 'channel_namespace',
        'frequency', 'operational-mode', 'target-output-power',
        'Band', 'band', 'slot_info', 'handled_flow',
        'channel_num', 'name', 'line-port'
    ]

    # Extract channel track information into a dictionary for easy lookup
    frequency_map = {}
    for r in resources:
        if isinstance(r, dict) and 'channel_track' in r:
            try:
                flow_tracker = r['channel_track']['flow_tracker']
                frequency_map[flow_tracker] = {
                    'lower-frequency': r['channel_track'].get('lower_frequency'),
                    'upper-frequency': r['channel_track'].get('upper_frequency')
                }
            except (KeyError, TypeError):
                continue

    # More robust admin-state handling
    admin_state = 'ENABLED'  # Default value
    for r in resources:
        if isinstance(r, dict) and r.get('resource_key') == 'admin-state':
            if r.get('value') in ['ENABLED', 'DISABLED']:
                admin_state = r['value']
                break

    try:
        # Get filtered configuration
        config = []
        for r in resources:
            if not isinstance(r, dict):
                continue
            key = r.get('resource_key')
            value = r.get('value')

            if key in unwanted_keys:
                continue
            if value is None and key in ['admin-state', 'optical-band-parent']:
                continue
            if key and key not in ['channel_track', 'lower-frequency', 'upper-frequency']:
                config.append(r)

        # Get ports
        ports = []
        for r in resources:
            if isinstance(r, dict) and r.get('resource_key') == 'handled_flow':
                ports_value = r.get('value', [])
                if isinstance(ports_value, list):
                    ports.extend(ports_value)

        # Get index
        index = 1
        index_res = next((r for r in resources if isinstance(r, dict) and
                            r.get('resource_key') == 'index'), None)
        if index_res and index_res.get('value') is not None:
            try:
                index = int(index_res['value'])
            except (ValueError, TypeError):
                index = 1

        # Process each flow and generate XML
        for i, flow in enumerate(ports):
            try:
                src, dest = parse_flow_ports(flow)
                current_index = index + i
                flow_tracker_to_use = current_index  # Use the current index as the flow tracker

                freq_params = frequency_map.get(flow_tracker_to_use, {})
                lower_frequency = 195000000000000 #freq_params.get('lower-frequency')
                upper_frequency = 196000000000000  # freq_params.get('upper-frequency')

                doc, tag, text = Doc().tagtext()
                with tag('config', xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
                    with tag('wavelength-router', xmlns="http://openconfig.net/yang/wavelength-router"):
                        with tag('media-channels'):
                            with tag('channel', operation="replace"):
                                with tag('index'): text(str(current_index))
                                with tag('config'):
                                    with tag('index'): text(str(current_index))
                                    with tag('admin-status'): text(admin_state)
                                    if lower_frequency is not None:
                                        with tag('lower-frequency'): text(str(lower_frequency))
                                    if upper_frequency is not None:
                                        with tag('upper-frequency'): text(str(upper_frequency))

                                    for resource in config:
                                        key = resource.get('resource_key')
                                        value = resource.get('value')
                                        if key == 'optical-band-parent':
                                            band_value = str(i)
                                            if value is not None:
                                                try:
                                                    band_value = str(int(value) + i)
                                                except (ValueError, TypeError):
                                                    band_value = str(i)
                                            with tag('optical-band-parent', xmlns=optical_band_namespaces):
                                                text(band_value)
                                        elif key not in ['admin-state', 'index', 'lower-frequency', 'upper-frequency', 'frequency', 'band'] and value is not None:
                                            with tag(key): text(str(value))

                                if src and src != '0':
                                    with tag('source'):
                                        with tag('config'):
                                            with tag('port-name'): text(src)
                                if dest and dest != '0':
                                    with tag('dest'):
                                        with tag('config'):
                                            with tag('port-name'): text(dest)

                results.append(indent(doc.getvalue(), indentation='  ', newline=''))

            except Exception as flow_error:
                logging.error(f"Error processing flow {flow}: {str(flow_error)}")
                continue

    except Exception as e:
        logging.error(f"Error in create_media_channel_v2: {str(e)}", exc_info=True)
        raise

    if not results:
        raise ValueError("No valid configuration messages generated")

    logging.info(f"Generated configuration messages for roadm: {results}")
    return results

def parse_flow_ports(flow):

    if isinstance(flow, dict):
        src = str(flow.get('src') or flow.get('in') or '0')
        dest = str(flow.get('dest') or flow.get('out') or '0')
    else:
        try:
            # Handle tuple/list format
            src = str(flow[0]) if len(flow) > 0 else '0'
            dest = str(flow[1]) if len(flow) > 1 else '0'
        except (TypeError, IndexError):
            src, dest = '0', '0'

    return src, dest
+226 −92
Changes for src/device/service/drivers/oc_driver/templates/VPN/transponder.py: 226 added lines, 92 removed lines.
Original line number Diff line number Diff line
@@ -15,11 +15,11 @@

from yattag import Doc, indent
import logging
from device.service.variables import Fc, Fs, Fl
from .common import seperate_port_config, extract_ports, filter_config

from .common  import seperate_port_config

def add_transceiver(transceiver_name: str):
 
    doc, tag, text = Doc().tagtext()
    with tag('config', xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
        with tag('components', xmlns="http://openconfig.net/yang/platform"):
@@ -29,20 +29,29 @@ def add_transceiver (transceiver_name:str):
                    with tag('name'): text(transceiver_name)
                with tag("state"):
                    with tag('name'): text(transceiver_name)
                    with tag("type",('xmlns:oc-platform-types',"http://openconfig.net/yang/platform-types")):text("oc-platform-types:TRANSCEIVER")
                    with tag("type", ('xmlns:oc-platform-types', "http://openconfig.net/yang/platform-types")):
                        text("oc-platform-types:TRANSCEIVER")
                with tag("transceiver", xmlns="http://openconfig.net/yang/platform/transceiver"):
                    with tag("config"):
                        with tag("enabled"): text("true")
                        with tag("form-factor-preconf",("xmlns:oc-opt-types","http://openconfig.net/yang/transport-types")):text("oc-opt-types:QSFP56_DD_TYPE1")
                        with tag("ethernet-pmd-preconf",("xmlns:oc-opt-types","http://openconfig.net/yang/transport-types")):text("oc-opt-types:ETH_400GBASE_ZR")
                        with tag("fec-mode",("xmlns:oc-platform-types","http://openconfig.net/yang/platform-types")):text("oc-platform-types:FEC_AUTO")
                        with tag("module-functional-type",("xmlns:oc-opt-types","http://openconfig.net/yang/transport-types")):text("oc-opt-types:TYPE_DIGITAL_COHERENT_OPTIC")
                        with tag("form-factor-preconf", ("xmlns:oc-opt-types", "http://openconfig.net/yang/transport-types")):
                            text("oc-opt-types:QSFP56_DD_TYPE1")
                        with tag("ethernet-pmd-preconf", ("xmlns:oc-opt-types", "http://openconfig.net/yang/transport-types")):
                            text("oc-opt-types:ETH_400GBASE_ZR")
                        with tag("fec-mode", ("xmlns:oc-platform-types", "http://openconfig.net/yang/platform-types")):
                            text("oc-platform-types:FEC_AUTO")
                        with tag("module-functional-type", ("xmlns:oc-opt-types", "http://openconfig.net/yang/transport-types")):
                            text("oc-opt-types:TYPE_DIGITAL_COHERENT_OPTIC")
                    with tag("state"):
                        with tag("enabled"): text("true")
                        with tag("form-factor-preconf",("xmlns:oc-opt-types","http://openconfig.net/yang/transport-types")):text("oc-opt-types:QSFP56_DD_TYPE1")
                        with tag("ethernet-pmd-preconf",("xmlns:oc-opt-types","http://openconfig.net/yang/transport-types")):text("oc-opt-types:ETH_400GBASE_ZR")
                        with tag("fec-mode",("xmlns:oc-platform-types","http://openconfig.net/yang/platform-types")):text("oc-platform-types:FEC_AUTO")
                        with tag("module-functional-type",("xmlns:oc-opt-types","http://openconfig.net/yang/transport-types")):text("oc-opt-types:TYPE_DIGITAL_COHERENT_OPTIC")
                        with tag("form-factor-preconf", ("xmlns:oc-opt-types", "http://openconfig.net/yang/transport-types")):
                            text("oc-opt-types:QSFP56_DD_TYPE1")
                        with tag("ethernet-pmd-preconf", ("xmlns:oc-opt-types", "http://openconfig.net/yang/transport-types")):
                            text("oc-opt-types:ETH_400GBASE_ZR")
                        with tag("fec-mode", ("xmlns:oc-platform-types", "http://openconfig.net/yang/platform-types")):
                            text("oc-platform-types:FEC_AUTO")
                        with tag("module-functional-type", ("xmlns:oc-opt-types", "http://openconfig.net/yang/transport-types")):
                            text("oc-opt-types:TYPE_DIGITAL_COHERENT_OPTIC")
                        with tag("vendor"): text("Cisco")
                        with tag("vendor-part"): text("400zr-QSFP-DD")
                        with tag("vendor-rev"): text("01")
@@ -53,75 +62,183 @@ def add_transceiver (transceiver_name:str):
                            with tag("config"):
                                with tag("index"): text("1")
                                with tag("associated-optical-channel"): text("channel-4")
    result = indent(
                doc.getvalue(),
                indentation = ' '*2,
                newline = ''
            )
         
   
    result = indent(doc.getvalue(), indentation=' ' * 2, newline='')
    return result


def create_optical_channel(resources:list[dict],ports:list[dict],config:list[dict] ):
def slots_to_center_frequency_mhz(slots: list, band_type: str):
    """Deterministically compute center frequency in MHz from slot indices.

    # unwanted_keys=['bidir','source_port','destination_port','handled_flow','name','channel_namespace','admin-state'
    #                ,'optical-band-parent']
    results =[]
    data ={}
    data["channel_namespace"]=next((i["value"] for i in resources if i["resource_key"] == "channel_namespace"), None)
    #config,ports,index=seperate_port_config(resources,unwanted_keys=unwanted_keys)
    Each slot is 12.5 GHz wide. Slot 0 starts at the band base frequency.
    Bands: c_slots → Fc (195000 GHz), s_slots → Fs, l_slots → Fl.
    Returns int MHz, or None if inputs are invalid.
    """
    band_bases = {'c_slots': Fc, 's_slots': Fs, 'l_slots': Fl}
    base_ghz = band_bases.get(band_type)
    if base_ghz is None or not slots:
        return None
    slot_width_ghz = 12.5
    center_ghz = base_ghz + (min(slots) + len(slots) / 2.0) * slot_width_ghz
    return int(center_ghz * 1000)  # GHz → MHz (1 GHz = 1000 MHz)

    port_val = ""
    if 'destination_port' in ports and ports['destination_port'][0] is not None:
        port_val = ports['destination_port'][0]
    else:
        port_val = ports['source_port'][0]

def create_optical_channel(resources: list, ports: list, config: list):
    """Generate NETCONF XML for an optical channel component.

    All parameters are derived from the resources list produced by
    extract_resources() in device/service/Tools.py.  No hardcoded
    channel-specific frequencies or operational modes.
    """
    results = []
    OC_OPT_TERM_NS = "http://openconfig.net/yang/terminal-device-digital-subcarriers"

    # --- Read every value from resources ---

    def _get(key):
        for r in resources:
            if isinstance(r, dict) and r.get('resource_key') == key:
                return r.get('value')
        return None

    channel_namespace = _get('channel_namespace') or OC_OPT_TERM_NS

    # Flows: list of (src_port_name, dst_port_name)
    flow_ports = _get('handled_flow') or []

    # Per-flow slot lists: [[slots_flow_0], [slots_flow_1], ...]
    per_flow_slots = _get('per_flow_slots') or []

    # Union of all flows' slots (for single-group devices or frequency computation)
    slot_info = _get('slot_info') or []

    # Band type (e.g. 'c_slots') from the 'name' resource in new_config
    band_type = _get('name')

    # Explicit frequency (MHz) — may be None if not set by optical controller
    frequency_mhz = None
    raw_freq = _get('frequency')
    if raw_freq is not None:
        try:
            frequency_mhz = int(float(raw_freq))
        except (ValueError, TypeError):
            logging.warning(f"Invalid frequency in resources: {raw_freq}")

    # Compute frequency from slots if not explicitly provided
    if frequency_mhz is None and slot_info and band_type:
        frequency_mhz = slots_to_center_frequency_mhz(slot_info, band_type)

    # Last resort: band center
    if frequency_mhz is None:
        frequency_mhz = int(Fc * 1000)
        logging.warning(f"No slot/frequency info — using band center: {frequency_mhz} MHz")

    # Derive op_mode from this device's actual slot count (slot_info reflects how
    # many subcarriers this specific device handles — 4→mode 4, 12→mode 9).
    # This is authoritative: T1.x carry 4 slots (mode 4), T2.1 carries 12 (mode 9).
    # Do NOT use new_config's service-level operational-mode, which is the hub value.
    op_mode = determine_op_mode(slot_info)
    if op_mode is None and per_flow_slots:
        candidate_modes = [
            m for flow_slots in per_flow_slots
            for m in [determine_op_mode(flow_slots)] if m is not None
        ]
        if candidate_modes:
            op_mode = max(candidate_modes)

    # Other parameters from new_config
    target_power = None
    raw_pwr = _get('target-output-power')
    if raw_pwr is not None:
        try:
            target_power = float(raw_pwr)
        except (ValueError, TypeError):
            pass

    line_port = _get('line-port')

    # --- Determine channel name from port ---
    port_val = '0'
    if flow_ports:
        fp = flow_ports[0]
        src = str(fp[0]) if len(fp) > 0 else '0'
        dst = str(fp[1]) if len(fp) > 1 else '0'
        port_val = dst if dst != '0' else src

    channel_name = f"channel-{port_val}"
    if line_port is None:
        line_port = f"port-{port_val}"

    # --- Build digital subcarrier groups ---
    # Use per-flow slots for multiple DSGs (one per remote in PMP).
    # Fall back to a single group with the combined slot_info.
    dsg_list = []
    if per_flow_slots:
        for group_idx, flow_slots in enumerate(per_flow_slots):
            if flow_slots:
                dsg_list.append({'group_id': group_idx + 1, 'slots': flow_slots})
    if not dsg_list and slot_info:
        dsg_list.append({'group_id': 1, 'slots': slot_info})

    # --- Generate NETCONF XML ---
    doc, tag, text = Doc().tagtext()
    #with tag('config'):
    try:
        with tag('config', xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
            with tag('components', xmlns="http://openconfig.net/yang/platform"):
                with tag('component'):
                with tag('name'):text("channel-{}".format(port_val))
                    with tag('name'): text(channel_name)
                    with tag('config'):
                    with tag('name'):text("channel-{}".format(port_val))
                with tag('optical-channel',xmlns=data["channel_namespace"]):
                        with tag('name'): text(channel_name)
                    with tag('optical-channel', xmlns=channel_namespace):
                        with tag('config'):
                        for resource in config:
                            with tag(resource['resource_key']):text(resource['value'])
        with tag('terminal-device', xmlns="http://openconfig.net/yang/terminal-device"):
            with tag('logical-channels'):
                with tag('channel'):
                    with tag('index'):text("{}".format(port_val))
                    with tag('config'):
                        with tag('index'):text("{}".format(port_val))
                        with tag('admin-state'):text("ENABLED")
    result = indent(
        doc.getvalue(),
        indentation = ' '*2,
        newline = ''
    )
                            with tag('frequency'): text(str(frequency_mhz))
                            if target_power is not None:
                                with tag('target-output-power'): text(str(target_power))
                            if op_mode is not None:
                                with tag('operational-mode'): text(str(op_mode))
                            with tag('line-port'): text(str(line_port))
                            # Only DSCM-capable transceivers model subcarriers; plain OpenConfig
                            # terminal devices reject this leaf as an unknown element.
                            if dsg_list:
                                with tag('digital-subcarrier-spacing'): text("50.0")

                            for dsg in dsg_list:
                                with tag('digital-subcarriers-group'):
                                    with tag('digital-subcarriers-group-id'): text(str(dsg['group_id']))
                                    with tag('number-of-digital-subcarriers'): text(str(len(dsg['slots'])))
                                    with tag('digital-subcarrier-group-size'): text("100")
                                    for slot in sorted(dsg['slots']):
                                        with tag('digital-subcarrier-id'):
                                            with tag('subcarrier-id'): text(str(slot + 1))
                                            with tag('active'): text("true")

        result = indent(doc.getvalue(), indentation='  ', newline='')
        results.append(result)

    except Exception as e:
        logging.error(f"Error generating NETCONF config for {channel_name}: {str(e)}")
        raise

    logging.info(
        f"Transponder {channel_name}: freq={frequency_mhz} MHz, "
        f"op_mode={op_mode}, slots={slot_info}, dsg_groups={len(dsg_list)}"
    )
    return results


def disable_optical_channel(ports:list[dict] ):
def _flow_port(ports: dict):
    """Transponder port of a flow: its destination when this device is the source, else its source.
    '0' (and None) is the placeholder for "no port" on that side of the flow."""
    dst = (ports.get('destination_port') or [None])[0]
    if dst is not None and str(dst) != '0':
        return dst
    return (ports.get('source_port') or [None])[0]


    # unwanted_keys=['bidir','source_port','destination_port','handled_flow','name','channel_namespace','admin-state'
    #                ,'optical-band-parent']
def disable_optical_channel(ports: list):
    results = []
    port_val = ""
    if 'destination_port' in ports and ports['destination_port'][0] is not None:
        port_val = ports['destination_port'][0]
    else:
        port_val = ports['source_port'][0]
    port_val = _flow_port(ports)

    doc, tag, text = Doc().tagtext()
    #with tag('config'):
    with tag('config', xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
        with tag('components', xmlns="http://openconfig.net/yang/platform"):
            with tag('component'):
@@ -140,63 +257,80 @@ def disable_optical_channel(ports:list[dict] ):
                    with tag('config'):
                        with tag('index'): text("{}".format(port_val))
                        with tag('admin-state'): text("DISABLED")
    result = indent(
        doc.getvalue(),
        indentation = ' '*2,
        newline = ''
    )
    result = indent(doc.getvalue(), indentation=' ' * 2, newline='')
    results.append(result)


    return results


def clear_frequency_tracking():
    """No-op kept for API compatibility — frequency is now computed deterministically."""
    logging.info("clear_frequency_tracking called (no stateful tracking to reset)")

def change_optical_channel_status (state:str,ports:list[dict]) :
    port_val=""
    if 'destination_port' in ports and ports['destination_port'][0] is not None:
        port_val = ports['destination_port'][0]
    else:
        port_val = ports['source_port'][0]

def change_optical_channel_status(state: str, ports: list):
    port_val = _flow_port(ports)

    results = []
    doc, tag, text = Doc().tagtext()
    #with tag('config'):
    with tag('config', xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"):
      
        with tag('terminal-device', xmlns="http://openconfig.net/yang/terminal-device"):
            with tag("logical-channels"):
                with tag('channel'):
                    with tag('index'): text("{}".format(port_val))
                    with tag('config'):
                        with tag('admin-state'): text("{}".format(state))
                       
    result = indent(
        doc.getvalue(),
        indentation = ' '*2,
        newline = ''
    )
    result = indent(doc.getvalue(), indentation=' ' * 2, newline='')
    results.append(result)


    return results



def edit_optical_channel (resources:list[dict]):

    unwanted_keys=['destination_port','source_port','channel_namespace'
                   ,'optical-band-parent','index', 'name','admin-state','handled_flow','bidir']
def edit_optical_channel(resources: list):
    unwanted_keys = [
        'destination_port', 'source_port', 'channel_namespace',
        'optical-band-parent', 'index', 'name', 'admin-state', 'handled_flow', 'bidir',
    ]
    config, ports, index = seperate_port_config(resources, unwanted_keys=unwanted_keys)
    results = []
    # channel_name=next((i["value"] for i in resources if i["resource_key"]=="channel_name" and i["value"] != None),None)
    # admin_state= next((i["value"] for i in resources if i["resource_key"]== "admin-state" and i["value"] != None) , None)
    
    results.extend(create_optical_channel(resources=resources, ports=ports, config=config))
    return results


    # results.extend(change_optical_channel_status(state=admin_state,ports=ports))
    # else :    
def clear_frequency_tracking_transponder():
    """No-op kept for API compatibility — frequency is now computed deterministically."""
    logging.info("clear_frequency_tracking_transponder called (no stateful tracking to reset)")


def extract_config_values(resources: list) -> dict:
    """Extract band_type and slots from resources (kept for backward compatibility)."""
    result = {'band_type': None, 'slots': None}
    processed_keys = set()
    for item in resources:
        if not isinstance(item, dict):
            continue
        key = item.get('resource_key')
        value = item.get('value')
        if key in processed_keys or value is None:
            continue
        key_lower = key.lower()
        if key_lower == 'name' and result['band_type'] is None:
            result['band_type'] = str(value)
            processed_keys.add(key)
        elif key_lower == 'slot_info':
            if isinstance(value, list):
                result['slots'] = [int(s) for s in (value[0] if value and isinstance(value[0], list) else value)]
            processed_keys.add(key)
    return result

    results.extend(create_optical_channel(resources=resources,ports=ports,config=config)  )

    return results
def determine_op_mode(slots):
    """Derive operational mode from slot count."""
    if not isinstance(slots, list):
        return None
    n = len(slots)
    if n == 4:
        return 4
    if n == 8:
        return 8
    if n > 8:
        return 9
    return None
Loading
Loading