Commit a31cab6b authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Service component - L3NM gNMI OpenConfig Service Handler:

- Added StaticRouteGenerator class
- ConfigRuleComposer: added dump methods to composer classes
- ConfigRuleComposer: extended configure methods to load previous configurations in devices
- ConfigRuleComposer: generalized to a single network instance for all static routing-based services
- ConfigRuleComposer: Updated static route organization with indexes and metrics
- ConfigRuleComposer: Added tracking of connected networks
- ConfigRuleComposer: Corrected config rule composer methods
- Integrated StaticRouteGenerator into Service Handler
- Multiple cosmetic improvements
- Added code for a future unitary test
parent 0ba504e3
Loading
Loading
Loading
Loading
+144 −32
Original line number Diff line number Diff line
@@ -12,33 +12,62 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Dict, List, Optional, Tuple
from common.proto.context_pb2 import Device, EndPoint
import json, 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.object_factory.ConfigRule import json_config_rule_delete, json_config_rule_set

from service.service.service_handler_api.AnyTreeTools import TreeNode

def _interface(if_name, sif_index, ipv4_address, ipv4_prefix, enabled) -> Tuple[str, Dict]:
    str_path = '/interface[{:s}]'.format(if_name)
    str_data = {'name': if_name, 'enabled': enabled, 'sub_if_index': sif_index,
                'sub_if_enabled': enabled, 'sub_if_ipv4_enabled': enabled,
                'sub_if_ipv4_address': ipv4_address, 'sub_if_ipv4_prefix': ipv4_prefix}
    return str_path, str_data

def _network_instance(ni_name, ni_type) -> Tuple[str, Dict]:
    str_path = '/network_instance[{:s}]'.format(ni_name)
    str_data = {'name': ni_name, 'type': ni_type}
    return str_path, str_data

def _network_instance_static_route(ni_name, prefix, next_hop, next_hop_index=0) -> Tuple[str, Dict]:
    str_path = '/network_instance[{:s}]/static_route[{:s}]'.format(ni_name, prefix)
    str_data = {'name': ni_name, 'prefix': prefix, 'next_hop': next_hop, 'next_hop_index': next_hop_index}
    return str_path, str_data
NETWORK_INSTANCE = 'teraflowsdn'

RE_IF = 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
) -> 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

def _network_instance(ni_name : str, ni_type : str) -> Tuple[str, Dict]:
    path = '/network_instance[{:s}]'.format(ni_name)
    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, index : int = 0, metric : Optional[int] = None
) -> Tuple[str, Dict]:
    protocol = 'STATIC'
    path = '/network_instance[{:s}]/protocols[{:s}]/static_route[{:s}:{:d}]'.format(ni_name, protocol, prefix, index)
    data = {
        'name': ni_name, 'identifier': protocol, 'protocol_name': protocol,
        'prefix': prefix, 'index': index, 'next_hop': next_hop
    }
    if metric is not None: data['metric'] = metric
    return path, data

def _network_instance_interface(ni_name, if_name, sif_index) -> Tuple[str, Dict]:
    str_path = '/network_instance[{:s}]/interface[{:s}.{:d}]'.format(ni_name, if_name, sif_index)
    str_data = {'name': ni_name, 'if_name': if_name, 'sif_index': sif_index}
    return str_path, str_data
def _network_instance_interface(ni_name : str, interface : str, sub_interface_index : int) -> Tuple[str, Dict]:
    sub_interface_name = '{:s}.{:d}'.format(interface, sub_interface_index)
    path = '/network_instance[{:s}]/interface[{:s}]'.format(ni_name, sub_interface_name)
    data = {'name': ni_name, 'id': sub_interface_name, 'interface': interface, 'subinterface': sub_interface_index}
    return path, data

class EndpointComposer:
    def __init__(self, endpoint_uuid : str) -> None:
@@ -46,33 +75,47 @@ class EndpointComposer:
        self.objekt : Optional[EndPoint] = None
        self.sub_interface_index = 0
        self.ipv4_address = None
        self.ipv4_prefix = None
        self.ipv4_prefix_len = None

    def configure(self, endpoint_obj : EndPoint, settings : Optional[TreeNode]) -> None:
    def configure(self, endpoint_obj : Optional[EndPoint], settings : Optional[TreeNode]) -> None:
        if endpoint_obj is not None:
            self.objekt = endpoint_obj
        if settings is None: return
        json_settings : Dict = settings.value
        self.ipv4_address = json_settings['ipv4_address']
        self.ipv4_prefix = json_settings['ipv4_prefix']
        self.ipv4_prefix_len = json_settings['ipv4_prefix_len']
        self.sub_interface_index = json_settings['sub_interface_index']

    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
        return [
            json_config_rule(*_interface(
                self.objekt.name, self.sub_interface_index, self.ipv4_address, self.ipv4_prefix, True
                self.objekt.name, index=self.sub_interface_index, address_ip=self.ipv4_address,
                address_prefix=self.ipv4_prefix_len, enabled=True
            )),
            json_config_rule(*_network_instance_interface(
                network_instance_name, self.objekt.name, self.sub_interface_index
            )),
        ]

    def dump(self) -> Dict:
        return {
            'sub_interface_index' : self.sub_interface_index,
            'ipv4_address'        : self.ipv4_address,
            'ipv4_prefix_len'     : self.ipv4_prefix_len,
        }

class DeviceComposer:
    def __init__(self, device_uuid : str) -> None:
        self.uuid = device_uuid
        self.objekt : Optional[Device] = None
        self.endpoints : Dict[str, EndpointComposer] = dict()
        self.static_routes : Dict[str, str] = dict()
        self.connected : Set[str] = set()
        
        # {prefix => {index => (next_hop, metric)}}
        self.static_routes : Dict[str, Dict[int, Tuple[str, Optional[int]]]] = dict()
    
    def get_endpoint(self, endpoint_uuid : str) -> EndpointComposer:
        if endpoint_uuid not in self.endpoints:
@@ -81,39 +124,108 @@ class DeviceComposer:

    def configure(self, device_obj : Device, settings : Optional[TreeNode]) -> None:
        self.objekt = device_obj
        for endpoint_obj in device_obj.device_endpoints:
            self.get_endpoint(endpoint_obj.name).configure(endpoint_obj, None)

        for config_rule in device_obj.device_config.config_rules:
            if config_rule.action != ConfigActionEnum.CONFIGACTION_SET: continue
            if config_rule.WhichOneof('config_rule') != 'custom': continue
            config_rule_custom = config_rule.custom

            match = RE_IF.match(config_rule_custom.resource_key)
            if match is not None:
                if_name, subif_index = match.groups()
                resource_value = json.loads(config_rule_custom.resource_value)
                ipv4_network    = str(resource_value['address_ip'])
                ipv4_prefix_len = int(resource_value['address_prefix'])
                endpoint = self.get_endpoint(if_name)
                endpoint.ipv4_address = ipv4_network
                endpoint.ipv4_prefix_len = ipv4_prefix_len
                endpoint.sub_interface_index = int(subif_index)
                endpoint_ip_network = netaddr.IPNetwork('{:s}/{:d}'.format(ipv4_network, ipv4_prefix_len))
                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, index = match.groups()
                if ni_name != NETWORK_INSTANCE: continue
                resource_value : Dict = json.loads(config_rule_custom.resource_value)
                next_hop = resource_value['next_hop']
                metric   = resource_value.get('metric')
                self.static_routes.setdefault(prefix, dict())[index] = (next_hop, metric)

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

    def get_config_rules(self, network_instance_name : str, delete : bool = False) -> List[Dict]:
        SELECTED_DEVICES = {DeviceTypeEnum.PACKET_ROUTER.value, DeviceTypeEnum.EMULATED_PACKET_ROUTER.value}
        if self.objekt.device_type not in SELECTED_DEVICES: return []

        json_config_rule = json_config_rule_delete if delete else json_config_rule_set
        config_rules = [
            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))
        for prefix, next_hop in self.static_routes.items():
        if len(self.static_routes) > 0:
            config_rules.append(
                json_config_rule(*_network_instance_protocol_static(network_instance_name))
            )
        for prefix, indexed_static_rule in self.static_routes.items():
            for index, (next_hop, metric) in indexed_static_rule.items():
                config_rules.append(
                json_config_rule(*_network_instance_static_route(network_instance_name, prefix, next_hop))
                    json_config_rule(*_network_instance_protocol_static_route(
                        network_instance_name, prefix, next_hop, index=index, metric=metric
                    ))
                )
        if delete: config_rules = list(reversed(config_rules))
        return config_rules

    def dump(self) -> Dict:
        return {
            'endpoints' : {
                endpoint_uuid : endpoint.dump()
                for endpoint_uuid, endpoint in self.endpoints.items()
            },
            'connected' : list(self.connected),
            'static_routes' : self.static_routes,
        }

class ConfigRuleComposer:
    def __init__(self) -> None:
        self.objekt : Optional[Service] = None
        self.devices : Dict[str, DeviceComposer] = dict()

    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

    def get_device(self, device_uuid : str) -> DeviceComposer:
        if device_uuid not in self.devices:
            self.devices[device_uuid] = DeviceComposer(device_uuid)
        return self.devices[device_uuid]

    def get_config_rules(self, network_instance_name : str, delete : bool = False) -> Dict[str, List[Dict]]:
    def get_config_rules(
        self, network_instance_name : str = NETWORK_INSTANCE, delete : bool = False
    ) -> Dict[str, List[Dict]]:
        return {
            device_uuid : device.get_config_rules(network_instance_name, delete=delete)
            for device_uuid, device in self.devices.items()
        }

    def dump(self) -> Dict:
        return {
            'devices' : {
                device_uuid : device.dump()
                for device_uuid, device in self.devices.items()
            }
        }
+29 −15
Original line number Diff line number Diff line
@@ -18,11 +18,12 @@ from common.method_wrappers.Decorator import MetricsPool, metered_subclass_metho
from common.proto.context_pb2 import ConfigRule, DeviceId, Service
from common.tools.object_factory.Device import json_device_id
from common.type_checkers.Checkers import chk_type
from service.service.service_handler_api.Tools import get_device_endpoint_uuids, get_endpoint_matching
from service.service.service_handler_api._ServiceHandler import _ServiceHandler
from service.service.service_handler_api.SettingsHandler import SettingsHandler
from service.service.service_handler_api.Tools import get_device_endpoint_uuids, get_endpoint_matching
from service.service.task_scheduler.TaskExecutor import TaskExecutor
from .ConfigRuleComposer import ConfigRuleComposer
from .StaticRouteGenerator import StaticRouteGenerator

LOGGER = logging.getLogger(__name__)

@@ -35,16 +36,22 @@ class L3NMGnmiOpenConfigServiceHandler(_ServiceHandler):
        self.__service = service
        self.__task_executor = task_executor
        self.__settings_handler = SettingsHandler(service.service_config, **settings)
        self.__composer = ConfigRuleComposer()
        self.__endpoint_map : Dict[Tuple[str, str], str] = dict()
        self.__config_rule_composer = ConfigRuleComposer()
        self.__static_route_generator = StaticRouteGenerator(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:
        if len(endpoints) % 2 != 0: raise Exception('Number of endpoints should be even')

        service_settings = self.__settings_handler.get_service_settings()
        self.__config_rule_composer.configure(self.__service, service_settings)

        for endpoint in endpoints:
            device_uuid, endpoint_uuid = get_device_endpoint_uuids(endpoint)

            device_obj = self.__task_executor.get_device(DeviceId(**json_device_id(device_uuid)))
            device_settings = self.__settings_handler.get_device_settings(device_obj)
            _device = self.__composer.get_device(device_obj.name)
            _device = self.__config_rule_composer.get_device(device_obj.name)
            _device.configure(device_obj, device_settings)

            endpoint_obj = get_endpoint_matching(device_obj, endpoint_uuid)
@@ -52,7 +59,9 @@ class L3NMGnmiOpenConfigServiceHandler(_ServiceHandler):
            _endpoint = _device.get_endpoint(endpoint_obj.name)
            _endpoint.configure(endpoint_obj, endpoint_settings)

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

        self.__static_route_generator.compose(endpoints)

    def _do_configurations(
        self, config_rules_per_device : Dict[str, List[Dict]], endpoints : List[Tuple[str, str, Optional[str]]],
@@ -62,7 +71,7 @@ class L3NMGnmiOpenConfigServiceHandler(_ServiceHandler):
        results_per_device = dict()
        for device_name,json_config_rules in config_rules_per_device.items():
            try:
                device_obj = self.__composer.get_device(device_name).objekt
                device_obj = self.__config_rule_composer.get_device(device_name).objekt
                if len(json_config_rules) == 0: continue
                del device_obj.device_config.config_rules[:]
                for json_config_rule in json_config_rules:
@@ -78,7 +87,8 @@ class L3NMGnmiOpenConfigServiceHandler(_ServiceHandler):
        results = []
        for endpoint in endpoints:
            device_uuid, endpoint_uuid = get_device_endpoint_uuids(endpoint)
            device_name = self.__endpoint_map[(device_uuid, endpoint_uuid)]
            device_name, _ = self.__endpoint_map[(device_uuid, endpoint_uuid)]
            if device_name not in results_per_device: continue
            results.append(results_per_device[device_name])
        return results

@@ -88,12 +98,14 @@ class L3NMGnmiOpenConfigServiceHandler(_ServiceHandler):
    ) -> List[Union[bool, Exception]]:
        chk_type('endpoints', endpoints, list)
        if len(endpoints) == 0: return []
        service_uuid = self.__service.service_id.service_uuid.uuid
        #settings = self.__settings_handler.get('/settings')
        #service_uuid = self.__service.service_id.service_uuid.uuid
        self._compose_config_rules(endpoints)
        network_instance_name = service_uuid.split('-')[0]
        config_rules_per_device = self.__composer.get_config_rules(network_instance_name, delete=False)
        #network_instance_name = service_uuid.split('-')[0]
        #config_rules_per_device = self.__config_rule_composer.get_config_rules(network_instance_name, delete=False)
        config_rules_per_device = self.__config_rule_composer.get_config_rules(delete=False)
        LOGGER.debug('config_rules_per_device={:s}'.format(str(config_rules_per_device)))
        results = self._do_configurations(config_rules_per_device, endpoints)
        LOGGER.debug('results={:s}'.format(str(results)))
        return results

    @metered_subclass_method(METRICS_POOL)
@@ -102,12 +114,14 @@ class L3NMGnmiOpenConfigServiceHandler(_ServiceHandler):
    ) -> List[Union[bool, Exception]]:
        chk_type('endpoints', endpoints, list)
        if len(endpoints) == 0: return []
        service_uuid = self.__service.service_id.service_uuid.uuid
        #settings = self.__settings_handler.get('/settings')
        #service_uuid = self.__service.service_id.service_uuid.uuid
        self._compose_config_rules(endpoints)
        network_instance_name = service_uuid.split('-')[0]
        config_rules_per_device = self.__composer.get_config_rules(network_instance_name, delete=True)
        #network_instance_name = service_uuid.split('-')[0]
        #config_rules_per_device = self.__config_rule_composer.get_config_rules(network_instance_name, delete=True)
        config_rules_per_device = self.__config_rule_composer.get_config_rules(delete=True)
        LOGGER.debug('config_rules_per_device={:s}'.format(str(config_rules_per_device)))
        results = self._do_configurations(config_rules_per_device, endpoints, delete=True)
        LOGGER.debug('results={:s}'.format(str(results)))
        return results

    @metered_subclass_method(METRICS_POOL)
+183 −0

File added.

Preview size limit exceeded, changes collapsed.

+160 −0

File added.

Preview size limit exceeded, changes collapsed.

+57 −0
Original line number Diff line number Diff line
# Copyright 2022-2024 ETSI OSG/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 logging
from enum import Enum
from typing import Dict, Optional, Union
from common.method_wrappers.ServiceExceptions import NotFoundException
from common.proto.context_pb2 import Connection, Device, DeviceId, Service
from service.service.tools.ObjectKeys import get_device_key

LOGGER = logging.getLogger(__name__)

CacheableObject = Union[Connection, Device, Service]

class CacheableObjectType(Enum):
    CONNECTION = 'connection'
    DEVICE     = 'device'
    SERVICE    = 'service'

class MockTaskExecutor:
    def __init__(self) -> None:
        self._grpc_objects_cache : Dict[str, CacheableObject] = dict()

    # ----- Common methods ---------------------------------------------------------------------------------------------

    def _load_grpc_object(self, object_type : CacheableObjectType, object_key : str) -> Optional[CacheableObject]:
        object_key = '{:s}:{:s}'.format(object_type.value, object_key)
        return self._grpc_objects_cache.get(object_key)

    def _store_grpc_object(self, object_type : CacheableObjectType, object_key : str, grpc_object) -> None:
        object_key = '{:s}:{:s}'.format(object_type.value, object_key)
        self._grpc_objects_cache[object_key] = grpc_object
    
    def _delete_grpc_object(self, object_type : CacheableObjectType, object_key : str) -> None:
        object_key = '{:s}:{:s}'.format(object_type.value, object_key)
        self._grpc_objects_cache.pop(object_key, None)

    def get_device(self, device_id : DeviceId) -> Device:
        device_key = get_device_key(device_id)
        device = self._load_grpc_object(CacheableObjectType.DEVICE, device_key)
        if device is None: raise NotFoundException('Device', device_key)
        return device

    def configure_device(self, device : Device) -> None:
        device_key = get_device_key(device.device_id)
        self._store_grpc_object(CacheableObjectType.DEVICE, device_key, device)
Loading