diff --git a/src/service/service/service_handlers/l3nm_openconfig/ConfigRules.py b/src/service/service/service_handlers/l3nm_openconfig/ConfigRules.py index 37f8256b1b294724ee017e925c8879777c56008d..99d4bc8e53f73037ebcb66868ef7e96edbb99dcf 100644 --- a/src/service/service/service_handlers/l3nm_openconfig/ConfigRules.py +++ b/src/service/service/service_handlers/l3nm_openconfig/ConfigRules.py @@ -12,52 +12,74 @@ # See the License for the specific language governing permissions and # limitations under the License. +import functools from typing import Any, Dict, List, Optional, Tuple 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 get_value(field_name : str, *containers, default=None) -> Optional[Any]: +def get_settings_containers( + service_settings : TreeNode, device_settings : TreeNode, endpoint_settings : TreeNode +) -> List[Dict]: + settings_containers : List[Dict] = list() + + # highest priority settings container + if endpoint_settings is not None: + json_endpoint_settings : Dict = endpoint_settings.value + settings_containers.append(json_endpoint_settings) + + if device_settings is not None: + json_device_settings : Dict = device_settings.value + settings_containers.append(json_device_settings) + + # lowest priority settings container + if service_settings is not None: + json_service_settings : Dict = service_settings.value + settings_containers.append(json_service_settings) + + return settings_containers + +def get_value(containers : List[Dict], field_name : str, **kwargs) -> Optional[Any]: if len(containers) == 0: raise Exception('No containers specified') + for container in containers: if field_name not in container: continue return container[field_name] - return default + + if 'default' in kwargs: + return kwargs['default'] + + MSG = 'Field({:s}) not found in containers specified({:s}) and no default value specified' + # pylint: disable=broad-exception-raised + raise Exception(MSG.format(str(field_name), str(containers))) def setup_config_rules( service_uuid : str, connection_uuid : str, device_uuid : str, endpoint_uuid : str, endpoint_name : str, service_settings : TreeNode, device_settings : TreeNode, endpoint_settings : TreeNode, endpoint_acls : List [Tuple] ) -> List[Dict]: - if service_settings is None: return [] - if device_settings is None: return [] - if endpoint_settings is None: return [] - - json_settings : Dict = service_settings.value - json_device_settings : Dict = device_settings.value - json_endpoint_settings : Dict = endpoint_settings.value - - settings = (json_settings, json_endpoint_settings, json_device_settings) - - mtu = get_value('mtu', *settings, default=1450) # 1512 - #address_families = json_settings.get('address_families', [] ) # ['IPV4'] - bgp_as = get_value('bgp_as', *settings, default=65000) # 65000 - - router_id = json_endpoint_settings.get('router_id', '0.0.0.0') # '10.95.0.10' - route_distinguisher = json_settings.get('route_distinguisher', '65000:101' ) # '60001:801' - sub_interface_index = json_endpoint_settings.get('sub_interface_index', 0 ) # 1 - vlan_id = json_endpoint_settings.get('vlan_id', 1 ) # 400 - address_ip = json_endpoint_settings.get('address_ip', '0.0.0.0') # '2.2.2.1' - address_prefix = json_endpoint_settings.get('address_prefix', 24 ) # 30 - - policy_import = json_endpoint_settings.get('policy_AZ', '2' ) # 2 - policy_export = json_endpoint_settings.get('policy_ZA', '7' ) # 30 + settings_containers : Tuple[Dict] = get_settings_containers( + service_settings, device_settings, endpoint_settings + ) + get_settings_value = functools.partial(get_value, settings_containers) + + mtu = get_settings_value('mtu', default=1450 ) # 1512 + #address_families = get_settings_value('address_families', default=[] ) # ['IPV4'] + bgp_as = get_settings_value('bgp_as', default=65000 ) # 65000 + router_id = get_settings_value('router_id', default='0.0.0.0' ) # '10.95.0.10' + route_distinguisher = get_settings_value('route_distinguisher', default='65000:101') # '60001:801' + sub_interface_index = get_settings_value('sub_interface_index', default=0 ) # 1 + vlan_id = get_settings_value('vlan_id', default=1 ) # 400 + address_ip = get_settings_value('address_ip', default='0.0.0.0' ) # '2.2.2.1' + address_prefix = get_settings_value('address_prefix', default=24 ) # 30 + policy_import = get_settings_value('policy_AZ', default='2' ) # 2 + policy_export = get_settings_value('policy_ZA', default='7' ) # 30 #network_interface_desc = '{:s}-NetIf'.format(service_uuid) - network_interface_desc = json_endpoint_settings.get('ni_description','') + network_interface_desc = get_settings_value('ni_description', default='' ) #network_subinterface_desc = '{:s}-NetSubIf'.format(service_uuid) - network_subinterface_desc = json_endpoint_settings.get('subif_description','') - #service_short_uuid = service_uuid.split('-')[-1] + network_subinterface_desc = get_settings_value('subif_description', default='' ) + service_short_uuid = service_uuid.split('-')[-1] #network_instance_name = '{:s}-NetInst'.format(service_short_uuid) - network_instance_name = json_endpoint_settings.get('ni_name', service_uuid.split('-')[-1]) #ELAN-AC:1 + network_instance_name = get_settings_value('ni_name', default=service_short_uuid) #ELAN-AC:1 if_subif_name = '{:s}.{:d}'.format(endpoint_name, vlan_id)