Commit 26d23b3a authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Service component - L2NM gNMI OpenConfig Service Handler:

- Removed unused Static Route Generator
- Implemented VlanIdPropagator
- Updated ConfigRuleComposer
- Integrated VlanIdPropagator in Service Handler
parent 1b8a0a92
Loading
Loading
Loading
Loading
+35 −94
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ import json, logging, netaddr, re
from typing import Dict, List, Optional, Set, Tuple
from common.DeviceTypes import DeviceTypeEnum
from common.proto.context_pb2 import ConfigActionEnum, Device, EndPoint, Service
from common.tools.grpc.Tools import grpc_message_to_json_string
from common.tools.object_factory.ConfigRule import json_config_rule_delete, json_config_rule_set
from service.service.service_handler_api.AnyTreeTools import TreeNode

@@ -27,19 +28,15 @@ DEFAULT_NETWORK_INSTANCE = 'default'

RE_IF    = re.compile(r'^\/interface\[([^\]]+)\]$')
RE_SUBIF = re.compile(r'^\/interface\[([^\]]+)\]\/subinterface\[([^\]]+)\]$')
RE_SR    = re.compile(r'^\/network_instance\[([^\]]+)\]\/protocols\[STATIC\]/route\[([^\:]+)\:([^\]]+)\]$')

def _interface(
    interface : str, if_type : Optional[str] = 'l3ipvlan', index : int = 0, vlan_id : Optional[int] = None,
    address_ip : Optional[str] = None, address_prefix : Optional[int] = None, mtu : Optional[int] = None,
    enabled : bool = True
    mtu : Optional[int] = None, enabled : bool = True
) -> Tuple[str, Dict]:
    path = '/interface[{:s}]/subinterface[{:d}]'.format(interface, index)
    data = {'name': interface, 'type': if_type, 'index': index, 'enabled': enabled}
    if if_type is not None: data['type'] = if_type
    if vlan_id is not None: data['vlan_id'] = vlan_id
    if address_ip is not None: data['address_ip'] = address_ip
    if address_prefix is not None: data['address_prefix'] = address_prefix
    if mtu is not None: data['mtu'] = mtu
    return path, data

@@ -48,24 +45,9 @@ def _network_instance(ni_name : str, ni_type : str) -> Tuple[str, Dict]:
    data = {'name': ni_name, 'type': ni_type}
    return path, data

def _network_instance_protocol(ni_name : str, protocol : str) -> Tuple[str, Dict]:
    path = '/network_instance[{:s}]/protocols[{:s}]'.format(ni_name, protocol)
    data = {'name': ni_name, 'identifier': protocol, 'protocol_name': protocol}
    return path, data

def _network_instance_protocol_static(ni_name : str) -> Tuple[str, Dict]:
    return _network_instance_protocol(ni_name, 'STATIC')

def _network_instance_protocol_static_route(
    ni_name : str, prefix : str, next_hop : str, metric : int
) -> Tuple[str, Dict]:
    protocol = 'STATIC'
    path = '/network_instance[{:s}]/protocols[{:s}]/static_route[{:s}:{:d}]'.format(ni_name, protocol, prefix, metric)
    index = 'AUTO_{:d}_{:s}'.format(metric, next_hop.replace('.', '-'))
    data = {
        'name': ni_name, 'identifier': protocol, 'protocol_name': protocol,
        'prefix': prefix, 'index': index, 'next_hop': next_hop, 'metric': metric
    }
def _network_instance_vlan(ni_name : str, vlan_id : int, vlan_name : str = None) -> Tuple[str, Dict]:
    path = '/network_instance[{:s}]/vlan[{:s}]'.format(ni_name, str(vlan_id))
    data = {'name': ni_name, 'vlan_id': vlan_id, 'vlan_name': vlan_name}
    return path, data

def _network_instance_interface(ni_name : str, interface : str, sub_interface_index : int) -> Tuple[str, Dict]:
@@ -79,8 +61,7 @@ class EndpointComposer:
        self.uuid = endpoint_uuid
        self.objekt : Optional[EndPoint] = None
        self.sub_interface_index = 0
        self.ipv4_address = None
        self.ipv4_prefix_len = None
        self.vlan_id = None

    def configure(self, endpoint_obj : Optional[EndPoint], settings : Optional[TreeNode]) -> None:
        if endpoint_obj is not None:
@@ -88,27 +69,12 @@ class EndpointComposer:
        if settings is None: return
        json_settings : Dict = settings.value

        if 'address_ip' in json_settings:
            self.ipv4_address = json_settings['address_ip']
        elif 'ip_address' in json_settings:
            self.ipv4_address = json_settings['ip_address']
        else:
            MSG = 'IP Address not found. Tried: address_ip and ip_address. endpoint_obj={:s} settings={:s}'
            LOGGER.warning(MSG.format(str(endpoint_obj), str(settings)))

        if 'address_prefix' in json_settings:
            self.ipv4_prefix_len = json_settings['address_prefix']
        elif 'prefix_length' in json_settings:
            self.ipv4_prefix_len = json_settings['prefix_length']
        else:
            MSG = 'IP Address Prefix not found. Tried: address_prefix and prefix_length. endpoint_obj={:s} settings={:s}'
            LOGGER.warning(MSG.format(str(endpoint_obj), str(settings)))
        if 'vlan_id' in json_settings:
            self.vlan_id = json_settings['vlan_id']

        self.sub_interface_index = json_settings.get('index', 0)

    def get_config_rules(self, network_instance_name : str, delete : bool = False) -> List[Dict]:
        if self.ipv4_address is None: return []
        if self.ipv4_prefix_len is None: return []
        json_config_rule = json_config_rule_delete if delete else json_config_rule_set

        config_rules : List[Dict] = list()
@@ -120,15 +86,13 @@ class EndpointComposer:
        if delete:
            config_rules.extend([
                json_config_rule(*_interface(
                    self.objekt.name, index=self.sub_interface_index, address_ip=None,
                    address_prefix=None, enabled=False
                    self.objekt.name, index=self.sub_interface_index, vlan_id=self.vlan_id, enabled=False
                )),
            ])
        else:
            config_rules.extend([
                json_config_rule(*_interface(
                    self.objekt.name, index=self.sub_interface_index, address_ip=self.ipv4_address,
                    address_prefix=self.ipv4_prefix_len, enabled=True
                    self.objekt.name, index=self.sub_interface_index, vlan_id=self.vlan_id, enabled=True
                )),
            ])
        return config_rules
@@ -136,8 +100,7 @@ class EndpointComposer:
    def dump(self) -> Dict:
        return {
            'index'   : self.sub_interface_index,
            'address_ip'    : self.ipv4_address,
            'address_prefix': self.ipv4_prefix_len,
            'vlan_id' : self.vlan_id,
        }
    
    def __str__(self):
@@ -152,8 +115,7 @@ class DeviceComposer:
        self.objekt : Optional[Device] = None
        self.aliases : Dict[str, str] = dict() # endpoint_name => endpoint_uuid
        self.endpoints : Dict[str, EndpointComposer] = dict() # endpoint_uuid => EndpointComposer
        self.connected : Set[str] = set()
        self.static_routes : Dict[str, Dict[int, str]] = dict() # {prefix => {metric => next_hop}}
        self.vlan_ids : Set[int] = set()

    def set_endpoint_alias(self, endpoint_name : str, endpoint_uuid : str) -> None:
        self.aliases[endpoint_name] = endpoint_uuid
@@ -195,34 +157,12 @@ class DeviceComposer:
                if_name, subif_index = match.groups()
                if if_name in mgmt_ifaces: continue
                resource_value = json.loads(config_rule_custom.resource_value)
                if 'address_ip' not in resource_value: continue
                if 'address_prefix' not in resource_value: continue
                ipv4_network    = str(resource_value['address_ip'])
                ipv4_prefix_len = int(resource_value['address_prefix'])
                if 'vlan_id' not in resource_value: continue
                vlan_id = int(resource_value['vlan_id'])
                self.vlan_ids.add(vlan_id)
                endpoint = self.get_endpoint(if_name)
                endpoint.ipv4_address = ipv4_network
                endpoint.ipv4_prefix_len = ipv4_prefix_len
                endpoint.vlan_id = vlan_id
                endpoint.sub_interface_index = int(subif_index)
                endpoint_ip_network = netaddr.IPNetwork('{:s}/{:d}'.format(ipv4_network, ipv4_prefix_len))
                if '0.0.0.0/' not in str(endpoint_ip_network.cidr):
                    self.connected.add(str(endpoint_ip_network.cidr))

            match = RE_SR.match(config_rule_custom.resource_key)
            if match is not None:
                ni_name, prefix, metric = match.groups()
                if ni_name != NETWORK_INSTANCE: continue
                resource_value : Dict = json.loads(config_rule_custom.resource_value)
                next_hop = resource_value['next_hop']
                self.static_routes.setdefault(prefix, dict())[metric] = next_hop

        if settings is None: return
        json_settings : Dict = settings.value
        static_routes : List[Dict] = json_settings.get('static_routes', [])
        for static_route in static_routes:
            prefix   = static_route['prefix']
            next_hop = static_route['next_hop']
            metric   = static_route.get('metric', 0)
            self.static_routes.setdefault(prefix, dict())[metric] = next_hop

    def get_config_rules(self, network_instance_name : str, delete : bool = False) -> List[Dict]:
        SELECTED_DEVICES = {
@@ -238,17 +178,11 @@ class DeviceComposer:
            json_config_rule(*_network_instance(network_instance_name, 'L3VRF'))
        for endpoint in self.endpoints.values():
            config_rules.extend(endpoint.get_config_rules(network_instance_name, delete=delete))
        if len(self.static_routes) > 0:
            config_rules.append(
                json_config_rule(*_network_instance_protocol_static(network_instance_name))
            )
        for prefix, metric_next_hop in self.static_routes.items():
            for metric, next_hop in metric_next_hop.items():
                config_rules.append(
                    json_config_rule(*_network_instance_protocol_static_route(
                        network_instance_name, prefix, next_hop, metric
                    ))
                )
        for vlan_id in self.vlan_ids:
            vlan_name = 'tfs-vlan-{:s}'.format(str(vlan_id))
            config_rules.append(json_config_rule(*_network_instance_vlan(
                network_instance_name, vlan_id, vlan_name=vlan_name
            )))
        if delete: config_rules = list(reversed(config_rules))
        return config_rules

@@ -257,9 +191,7 @@ class DeviceComposer:
            'endpoints' : {
                endpoint_uuid : endpoint.dump()
                for endpoint_uuid, endpoint in self.endpoints.items()
            },
            'connected' : list(self.connected),
            'static_routes' : self.static_routes,
            }
        }

    def __str__(self):
@@ -273,6 +205,7 @@ class ConfigRuleComposer:
        self.objekt : Optional[Service] = None
        self.aliases : Dict[str, str] = dict() # device_name => device_uuid
        self.devices : Dict[str, DeviceComposer] = dict() # device_uuid => DeviceComposer
        self.vlan_id = None

    def set_device_alias(self, device_name : str, device_uuid : str) -> None:
        self.aliases[device_name] = device_uuid
@@ -286,8 +219,15 @@ class ConfigRuleComposer:
    def configure(self, service_obj : Service, settings : Optional[TreeNode]) -> None:
        self.objekt = service_obj
        if settings is None: return
        #json_settings : Dict = settings.value
        # For future use
        json_settings : Dict = settings.value

        if 'vlan_id' in json_settings:
            self.vlan_id = json_settings['vlan_id']
        elif 'vlan-id' in json_settings:
            self.vlan_id = json_settings['vlan-id']
        else:
            MSG = 'VLAN ID not found. Tried: vlan_id and vlan-id. service_obj={:s} settings={:s}'
            LOGGER.warning(MSG.format(grpc_message_to_json_string(service_obj), str(settings)))

    def get_config_rules(
        self, network_instance_name : str = NETWORK_INSTANCE, delete : bool = False
@@ -302,5 +242,6 @@ class ConfigRuleComposer:
            'devices' : {
                device_uuid : device.dump()
                for device_uuid, device in self.devices.items()
            }
            },
            'vlan_id': self.vlan_id,
        }
+9 −5
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ from service.service.service_handler_api.Tools import get_device_endpoint_uuids,
from service.service.task_scheduler.TaskExecutor import TaskExecutor
from service.service.tools.EndpointIdFormatters import endpointids_to_raw
from .ConfigRuleComposer import ConfigRuleComposer
from .StaticRouteGenerator import StaticRouteGenerator
from .VlanIdPropagator import VlanIdPropagator

LOGGER = logging.getLogger(__name__)

@@ -39,7 +39,7 @@ class L2NMGnmiOpenConfigServiceHandler(_ServiceHandler):
        self.__task_executor = task_executor
        self.__settings_handler = SettingsHandler(service.service_config, **settings)
        self.__config_rule_composer = ConfigRuleComposer()
        self.__static_route_generator = StaticRouteGenerator(self.__config_rule_composer)
        self.__vlan_id_propagator = VlanIdPropagator(self.__config_rule_composer)
        self.__endpoint_map : Dict[Tuple[str, str], Tuple[str, str]] = dict()

    def _compose_config_rules(self, endpoints : List[Tuple[str, str, Optional[str]]]) -> None:
@@ -65,9 +65,13 @@ class L2NMGnmiOpenConfigServiceHandler(_ServiceHandler):

            self.__endpoint_map[(device_uuid, endpoint_uuid)] = (device_obj.name, endpoint_obj.name)

        LOGGER.debug('[pre] config_rule_composer = {:s}'.format(json.dumps(self.__config_rule_composer.dump())))
        self.__static_route_generator.compose(endpoints)
        LOGGER.debug('[post] config_rule_composer = {:s}'.format(json.dumps(self.__config_rule_composer.dump())))
        MSG = '[pre] config_rule_composer = {:s}'
        LOGGER.debug(MSG.format(json.dumps(self.__config_rule_composer.dump())))

        self.__vlan_id_propagator.compose(endpoints)

        MSG = '[post] config_rule_composer = {:s}'
        LOGGER.debug(MSG.format(json.dumps(self.__config_rule_composer.dump())))

    def _do_configurations(
        self, config_rules_per_device : Dict[str, List[Dict]], endpoints : List[Tuple[str, str, Optional[str]]],
+0 −215

File deleted.

Preview size limit exceeded, changes collapsed.

+92 −0
Original line number Diff line number Diff line
# Copyright 2022-2025 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.

import json, logging
from typing import List, Optional, Tuple
from .ConfigRuleComposer import ConfigRuleComposer

LOGGER = logging.getLogger(__name__)

class VlanIdPropagator:
    def __init__(self, config_rule_composer : ConfigRuleComposer) -> None:
        self._config_rule_composer = config_rule_composer

    def compose(self, connection_hop_list : List[Tuple[str, str, Optional[str]]]) -> None:
        link_endpoints = self._compute_link_endpoints(connection_hop_list)
        LOGGER.debug('link_endpoints = {:s}'.format(str(link_endpoints)))

        self._propagate_vlan_id(link_endpoints)
        LOGGER.debug('config_rule_composer = {:s}'.format(json.dumps(self._config_rule_composer.dump())))

    def _compute_link_endpoints(
        self, connection_hop_list : List[Tuple[str, str, Optional[str]]]
    ) -> List[Tuple[Tuple[str, str, Optional[str]], Tuple[str, str, Optional[str]]]]:
        # In some cases connection_hop_list might contain repeated endpoints, remove them here.
        added_connection_hops = set()
        filtered_connection_hop_list = list()
        for connection_hop in connection_hop_list:
            if connection_hop in added_connection_hops: continue
            filtered_connection_hop_list.append(connection_hop)
            added_connection_hops.add(connection_hop)
        connection_hop_list = filtered_connection_hop_list

        # In some cases connection_hop_list first and last items might be internal endpoints of
        # devices instead of link endpoints. Filter those endpoints not reaching a new device.
        if len(connection_hop_list) > 2 and connection_hop_list[0][0] == connection_hop_list[1][0]:
            # same device on first 2 endpoints
            connection_hop_list = connection_hop_list[1:]
        if len(connection_hop_list) > 2 and connection_hop_list[-1][0] == connection_hop_list[-2][0]:
            # same device on last 2 endpoints
            connection_hop_list = connection_hop_list[:-1]

        num_connection_hops = len(connection_hop_list)
        if num_connection_hops % 2 != 0: raise Exception('Number of connection hops must be even')
        if num_connection_hops < 4: raise Exception('Number of connection hops must be >= 4')

        it_connection_hops = iter(connection_hop_list)
        return list(zip(it_connection_hops, it_connection_hops))

    def _propagate_vlan_id(
        self, link_endpoints_list : List[Tuple[Tuple[str, str, Optional[str]], Tuple[str, str, Optional[str]]]]
    ) -> None:
        for link_endpoints in link_endpoints_list:
            device_endpoint_a, device_endpoint_b = link_endpoints

            device_uuid_a, endpoint_uuid_a = device_endpoint_a[0:2]
            endpoint_a = self._config_rule_composer.get_device(device_uuid_a).get_endpoint(endpoint_uuid_a)

            device_uuid_b, endpoint_uuid_b = device_endpoint_b[0:2]
            endpoint_b = self._config_rule_composer.get_device(device_uuid_b).get_endpoint(endpoint_uuid_b)

            svc_vlan_id  = self._config_rule_composer.vlan_id
            ep_a_vlan_id = endpoint_a.vlan_id
            ep_b_vlan_id = endpoint_b.vlan_id

            if ep_a_vlan_id is None and ep_b_vlan_id is None:
                endpoint_a.vlan_id = svc_vlan_id
                endpoint_b.vlan_id = svc_vlan_id
            elif ep_a_vlan_id is not None and ep_b_vlan_id is None:
                if ep_a_vlan_id != svc_vlan_id:
                    MSG = 'Incompatible VLAN-IDs: endpoint_a({:s}), service({:s})'
                    raise Exception(MSG.format(str(endpoint_a), str(svc_vlan_id)))
                endpoint_b.vlan_id = svc_vlan_id
            elif ep_a_vlan_id is None and ep_b_vlan_id is not None:
                if ep_b_vlan_id != svc_vlan_id:
                    MSG = 'Incompatible VLAN-IDs: endpoint_b({:s}), service({:s})'
                    raise Exception(MSG.format(str(endpoint_b), str(svc_vlan_id)))
                endpoint_a.vlan_id = svc_vlan_id
            elif ep_a_vlan_id is not None and ep_b_vlan_id is not None:
                if ep_a_vlan_id != svc_vlan_id or ep_b_vlan_id != svc_vlan_id:
                    MSG = 'Incompatible VLAN-IDs: endpoint_a({:s}), endpoint_a({:s}), service({:s})'
                    raise Exception(MSG.format(str(endpoint_a), str(endpoint_b), str(svc_vlan_id)))