Commit 538582a7 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

PathComp component - Front-end:

- Extended ComposeConfigRules to propagate static routes in L3 services
- Extended ComposeConfigRules to recognize custom config rules with the form /device[]/endpoint[]/vlan[]/settings
parent 1ddd68dd
Loading
Loading
Loading
Loading
+53 −23
Original line number Diff line number Diff line
@@ -21,19 +21,21 @@ from common.tools.object_factory.ConfigRule import json_config_rule_set
LOGGER = logging.getLogger(__name__)

SETTINGS_RULE_NAME = '/settings'
STATIC_ROUTING_RULE_NAME = '/static_routing'

DEVICE_SETTINGS = re.compile(r'\/device\[([^\]]+)\]\/settings')
ENDPOINT_SETTINGS = re.compile(r'\/device\[([^\]]+)\]\/endpoint\[([^\]]+)\]\/settings')
RE_DEVICE_SETTINGS        = re.compile(r'\/device\[([^\]]+)\]\/settings')
RE_ENDPOINT_SETTINGS      = re.compile(r'\/device\[([^\]]+)\]\/endpoint\[([^\]]+)\]\/settings')
RE_ENDPOINT_VLAN_SETTINGS = re.compile(r'\/device\[([^\]]+)\]\/endpoint\[([^\]]+)\]\/vlan\[([^\]]+)\]\/settings')

L2NM_SETTINGS_FIELD_DEFAULTS = {
    'encapsulation_type': 'dot1q',
    'vlan_id'           : 100,
    #'encapsulation_type': 'dot1q',
    #'vlan_id'           : 100,
    'mtu'               : 1450,
}

L3NM_SETTINGS_FIELD_DEFAULTS = {
    'encapsulation_type': 'dot1q',
    'vlan_id'           : 100,
    #'encapsulation_type': 'dot1q',
    #'vlan_id'           : 100,
    'mtu'               : 1450,
}

@@ -54,26 +56,48 @@ def find_custom_config_rule(config_rules : List, resource_name : str) -> Optiona
    return resource_value

def compose_config_rules(
    main_service_config_rules : List, subservice_config_rules : List, field_defaults : Dict
    main_service_config_rules : List, subservice_config_rules : List, settings_rule_name : str, field_defaults : Dict
) -> None:
    settings = find_custom_config_rule(main_service_config_rules, SETTINGS_RULE_NAME)
    settings = find_custom_config_rule(main_service_config_rules, settings_rule_name)
    if settings is None: return

    json_settings = {}

    if len(field_defaults) == 0:
        for field_name,field_value in settings.items():
            json_settings[field_name] = field_value
    else:
        for field_name,default_value in field_defaults.items():
        json_settings[field_name] = settings.get(field_name, default_value)
            field_value = settings.get(field_name, default_value)
            if field_value is None: continue
            json_settings[field_name] = field_value

    config_rule = ConfigRule(**json_config_rule_set('/settings', json_settings))
    if len(json_settings) == 0: return

    config_rule = ConfigRule(**json_config_rule_set(settings_rule_name, json_settings))
    subservice_config_rules.append(config_rule)

def compose_l2nm_config_rules(main_service_config_rules : List, subservice_config_rules : List) -> None:
    compose_config_rules(main_service_config_rules, subservice_config_rules, L2NM_SETTINGS_FIELD_DEFAULTS)
    CONFIG_RULES = [
        (SETTINGS_RULE_NAME, L2NM_SETTINGS_FIELD_DEFAULTS),
    ]
    for rule_name, defaults in CONFIG_RULES:
        compose_config_rules(main_service_config_rules, subservice_config_rules, rule_name, defaults)

def compose_l3nm_config_rules(main_service_config_rules : List, subservice_config_rules : List) -> None:
    compose_config_rules(main_service_config_rules, subservice_config_rules, L3NM_SETTINGS_FIELD_DEFAULTS)
    CONFIG_RULES = [
        (SETTINGS_RULE_NAME, L3NM_SETTINGS_FIELD_DEFAULTS),
        (STATIC_ROUTING_RULE_NAME, {}),
    ]
    for rule_name, defaults in CONFIG_RULES:
        compose_config_rules(main_service_config_rules, subservice_config_rules, rule_name, defaults)

def compose_tapi_config_rules(main_service_config_rules : List, subservice_config_rules : List) -> None:
    compose_config_rules(main_service_config_rules, subservice_config_rules, TAPI_SETTINGS_FIELD_DEFAULTS)
    CONFIG_RULES = [
        (SETTINGS_RULE_NAME, TAPI_SETTINGS_FIELD_DEFAULTS),
    ]
    for rule_name, defaults in CONFIG_RULES:
        compose_config_rules(main_service_config_rules, subservice_config_rules, rule_name, defaults)

def compose_device_config_rules(
    config_rules : List, subservice_config_rules : List, path_hops : List,
@@ -127,25 +151,31 @@ def compose_device_config_rules(
        elif config_rule.WhichOneof('config_rule') == 'custom':
            LOGGER.debug('[compose_device_config_rules]   is custom')

            match = DEVICE_SETTINGS.match(config_rule.custom.resource_key)
            match = RE_DEVICE_SETTINGS.match(config_rule.custom.resource_key)
            if match is not None:
                device_uuid_or_name = match.group(1)
                device_name_or_uuid = device_name_mapping[device_uuid_or_name]
                device_keys = {device_uuid_or_name, device_name_or_uuid}
                device_keys = {'?', device_uuid_or_name}
                device_name_or_uuid = device_name_mapping.get(device_uuid_or_name)
                if device_name_or_uuid is not None: device_keys.add(device_name_or_uuid)

                if len(device_keys.intersection(devices_traversed)) == 0: continue
                subservice_config_rules.append(config_rule)

            match = ENDPOINT_SETTINGS.match(config_rule.custom.resource_key)
            match = RE_ENDPOINT_SETTINGS.match(config_rule.custom.resource_key)
            if match is None:
                match = RE_ENDPOINT_VLAN_SETTINGS.match(config_rule.custom.resource_key)
            if match is not None:
                device_uuid_or_name = match.group(1)
                device_name_or_uuid = device_name_mapping[device_uuid_or_name]
                device_keys = {device_uuid_or_name, device_name_or_uuid}
                device_keys = {'?', device_uuid_or_name}
                device_name_or_uuid = device_name_mapping.get(device_uuid_or_name)
                if device_name_or_uuid is not None: device_keys.add(device_name_or_uuid)

                endpoint_uuid_or_name = match.group(2)
                endpoint_name_or_uuid_1 = endpoint_name_mapping[(device_uuid_or_name, endpoint_uuid_or_name)]
                endpoint_name_or_uuid_2 = endpoint_name_mapping[(device_name_or_uuid, endpoint_uuid_or_name)]
                endpoint_keys = {endpoint_uuid_or_name, endpoint_name_or_uuid_1, endpoint_name_or_uuid_2}
                endpoint_keys = {'?', endpoint_uuid_or_name}
                endpoint_name_or_uuid_1 = endpoint_name_mapping.get((device_uuid_or_name, endpoint_uuid_or_name))
                if endpoint_name_or_uuid_1 is not None: endpoint_keys.add(endpoint_name_or_uuid_1)
                endpoint_name_or_uuid_2 = endpoint_name_mapping.get((device_name_or_uuid, endpoint_uuid_or_name))
                if endpoint_name_or_uuid_2 is not None: endpoint_keys.add(endpoint_name_or_uuid_2)

                device_endpoint_keys = set(itertools.product(device_keys, endpoint_keys))
                if len(device_endpoint_keys.intersection(endpoints_traversed)) == 0: continue