Commit c15cd6fc authored by Pablo Armingol's avatar Pablo Armingol
Browse files

fix: improve template path resolution and add error handling for resource parsing in L3VPN tools

parent 2cb1883d
Loading
Loading
Loading
Loading
+53 −25
Original line number Diff line number Diff line
@@ -28,11 +28,13 @@ HEADERS = {
executor = ThreadPoolExecutor()

def generate_l3vpn_template_pair( vpn_id, svc_mtu=1500, svc_input_bandwidth=1000000000, svc_output_bandwidth=1000000000, latency_boundary=10, guaranteed_bw_percent=100):
    with open("templates/l3vpn.json", "r") as f:
    template_path = os.path.join(os.path.dirname(__file__), "l3vpn.json")
    with open(template_path, "r") as f:
        template = json.load(f)
    LOGGER.info(f"Loaded template: {json.dumps(template, indent=2)}")

    template['ietf-l3vpn-svc:l3vpn-svc']['vpn-services']['vpn-service'][0]['vpn-id'] = vpn_id
    LOGGER.info(f"VPN ID: {vpn_id}")

    for site in template['ietf-l3vpn-svc:l3vpn-svc']['sites']['site']:
        for access in site['site-network-accesses']['site-network-access']:
@@ -67,15 +69,28 @@ def create_request(resource_value):

    LOGGER.info("Creating request for resource_value: %s", resource_value)

    try:
        val = resource_value[1]
        if isinstance(val, str):
            val = json.loads(val)
    except Exception as e:
        LOGGER.error(f"Error parsing resource_value[1] JSON: {e}")
        return None

    node_src = resource_value[1]['rule_set']['src'][0]
    try:
        node_src = val['rule_set']['src'][0]
        src = [{
            'uuid': node_src["uuid"],
            'ip_address': node_src["ip_address"],
            'ip_mask': node_src["ip_mask"],
            'vlan_id': node_src["vlan_id"]
        }]
    dst_list = resource_value[1]['rule_set']['dst']
    except Exception as e:
        LOGGER.info(f"Error parsing src: {e}")

    try:
        dst_list = val['rule_set']['dst']
    
        dsts = []
        for node in dst_list:
            dsts.append({
@@ -84,22 +99,35 @@ def create_request(resource_value):
                'ip_mask': node["ip_mask"],
                'vlan_id': node["vlan_id"]
            })
    except Exception as e:
        LOGGER.info(f"Error parsing dst: {e}")

    sites_input = src + dsts

    dests_uuids = [dst['uuid'] for dst in dsts]
    src_uuid = src[0]['uuid']
    vpn_id = src_uuid + "-" + "-".join(dests_uuids)
    LOGGER.info(f"VPN ID: {vpn_id}")

    components = resource_value[1]['rule_set']['transceiver']['components']
    try:
        components = val['rule_set']['transceiver']['components']
        for i, device in enumerate(components):
            name = sites_input[i]['uuid']
            LOGGER.info(f"NODE TO CONFIGURE: \n{name}: {json.dumps(device, indent=2)}")
            # try:
            #     response = patch_optical_channel_frequency(device, name)
            #     LOGGER.debug(f"RESPONSE :\n {response}")
            # except Exception as e:
            #     LOGGER.error(f"Error configuring node: {e}")
            #     return
    except Exception as e:
        LOGGER.error(f"Error parsing components: {e}")
        return

    templates = []
    for dst in dsts:
        LOGGER.info(f"dst: {dst}")
        vpn = "L3VPN_"+src[0]['uuid']+"_"+dst['uuid']
        LOGGER.info(f"VPN ID: {vpn}")
        templates.append(generate_l3vpn_template_pair(vpn_id=vpn_id, svc_mtu="1500", svc_input_bandwidth=1000000000, svc_output_bandwidth=1000000000, latency_boundary=10, guaranteed_bw_percent=100))
    LOGGER.info(f"Generated L3VPN P2MP service JSONs:\n{json.dumps(templates, indent=2)}")