Commit 27b2104d authored by Shayan Hajipour's avatar Shayan Hajipour
Browse files

refactoring: port lookup done sequentially in device protobuf instead of global regex search

parent 6958674b
Loading
Loading
Loading
Loading
+15 −16
Original line number Diff line number Diff line
@@ -27,9 +27,8 @@ from common.tools.object_factory.Service import json_service_id

LOGGER = logging.getLogger(__name__)

RE_PORT_AND_IP_MATCH = r'resource_key: \"\/interface\[(.*)\]\/subinterface\[.*\]\"[.|\n]*.*\\\"address_ip\\\": \\\"{}\\\"'
RE_ENDPOINT_UUID_PORT_MATCH = r'\s*\n?\s*endpoint_uuid .\s*\n?\s*uuid: \"(.*)\"\n?\s*.\n?\s*.\n?\s*name: \"{}\"'
RE_DEVICE_UUID_MATCH = r'\n?\s*device_uuid \{\s*\n?\s*uuid: \"(.*)\"\n?\s*\}\n?\s*\}'
RE_CONFIG_RULE_IF_SUBIF = re.compile(r'^\/interface\[([^\]]+)\]\/subinterface\[([^\]]+)\]$')
RE_CONFIG_RULE_ADDRESS_IP = re.compile(r'\\\"address_ip\\\": \\\"((?:[0-9]{1,3}\.){3}[0-9]{1,3})\\\"')

def service_2_bwInfo(service: Service) -> dict:
    response = {}
@@ -79,20 +78,20 @@ def bwInfo_2_service(client, bwInfo: dict) -> Service:
        a_ip = bwInfo['sessionFilter'][0]['sourceIp']
        z_ip = bwInfo['sessionFilter'][0]['dstAddress']

        devices = (str(device) for device in client.ListDevices(Empty()).devices)
        devices = client.ListDevices(Empty()).devices
        for device in devices:
            for ep_id in (a_ip, z_ip):
                if look_up := re.search(RE_PORT_AND_IP_MATCH.format(ep_id), device):
                    physical_port = look_up.groups(0)[0]
                    # 'PORT-' is added as a prefix
                    port = 'PORT-' + physical_port
                    port_uuid = re.search(RE_ENDPOINT_UUID_PORT_MATCH.format(port), device).group(0)[0]
                    device_uuid = re.search(RE_DEVICE_UUID_MATCH, device).group(0)[0]
            device_endpoint_uuids = {ep.name:ep.endpoint_id.endpoint_uuid for ep in device.device_endpoints}
            for cr in device.device_config.config_rules:
                if cr.WhichOneof('config_rule') == 'custom':
                    match_subif = RE_CONFIG_RULE_IF_SUBIF.match(cr.custom.resource_key)
                    match_ip = RE_CONFIG_RULE_ADDRESS_IP.match(cr.custom.resource_value)
                    if match_subif and match_ip and match_ip.groups(0)[0] in [a_ip, z_ip]:
                        # `PORT-` added as prefix
                        port_name = 'PORT-' + match_subif.groups(0)[0]
                        ep_id = EndPointId()
                    ep_id.endpoint_uuid.uuid = port_uuid
                    ep_id.device_id.device_uuid.uuid = device_uuid
                        ep_id.endpoint_uuid.uuid = device_endpoint_uuids[port_name]
                        ep_id.device_id.device_uuid.uuid = device.device_id.device_uuid
                        service.service_endpoint_ids.append(ep_id)
            
    service.service_type = ServiceTypeEnum.SERVICETYPE_L3NM

    if 'appInsId' in bwInfo: