Commit 85ba5e45 authored by Shayan Hajipour's avatar Shayan Hajipour
Browse files

bgp configuration with VRF support added

parent 581c3940
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -54,7 +54,8 @@ def create_If_SubIf(data,vendor, DEL):
                    with tag('enabled'):text('true')    
                with tag('subinterfaces'):
                    with tag('subinterface'):
                        if vendor == 'ADVA':
                        # if vendor == 'ADVA':
                        if True:
                            with tag('index'): text('0')
                        with tag('config'):
                            with tag('index'): text('0')
+43 −30
Original line number Diff line number Diff line
@@ -64,10 +64,14 @@ def create_NI(parameters,vendor,DEL):
                elif "L3VRF" in parameters['type']: 
                    with tag('config'):
                        with tag('name'):text(parameters['name'])
                        if vendor == "ADVA": 
                        if "router_id" in parameters: 
                            with tag('router-id'):text(parameters['router_id'])
                        # if vendor == "ADVA": 
                        if True:
                            with tag('type', 'xmlns:oc-ni-types="http://openconfig.net/yang/network-instance-types"'):text('oc-ni-types:',parameters['type'])
                            with tag('route-distinguisher'):text(parameters['route_distinguisher'])
                    if vendor == "ADVA": 
                    # if vendor == "ADVA": 
                    if True:
                        with tag('encapsulation'):
                            with tag('config'):
                                with tag('encapsulation-type', 'xmlns:oc-ni-types="http://openconfig.net/yang/network-instance-types"')  :text('oc-ni-types:MPLS')
@@ -116,9 +120,6 @@ def add_protocol_NI(parameters,vendor, DEL):
        else:
            with tag('network-instance'):
                with tag('name'):text(parameters['name'])
                with tag('config'):
                    with tag('name'): text(parameters['name'])
                    with tag('type', 'xmlns:oc-ni-types="http://openconfig.net/yang/network-instance-types"'): text('oc-ni-types:DEFAULT_INSTANCE')
                with tag('protocols'):
                    with tag('protocol'):
                        with tag('identifier', 'xmlns:oc-pol-types="http://openconfig.net/yang/policy-types"'):text('oc-pol-types:',parameters['identifier'])
@@ -129,36 +130,25 @@ def add_protocol_NI(parameters,vendor, DEL):
                            with tag('enabled'): text('true')
                        if "BGP" in parameters['identifier']:
                            with tag('bgp'):
                                with tag('name'): text(parameters['as'])
                                with tag('global'):
                                    with tag('afi-safis'):
                                        with tag('afi-safi'):
                                            with tag('afi-safi-name', 'xmlns:oc-bgp-types="http://openconfig.net/yang/bgp-types"'): text('oc-bgp-types:IPV4_UNICAST')
                                            with tag('config'):
                                                with tag('afi-safi-name', 'xmlns:oc-bgp-types="http://openconfig.net/yang/bgp-types"'): text('oc-bgp-types:IPV4_UNICAST')
                                                with tag('enabled'): text('true')
                                    with tag('config'):
                                        with tag('as')       :text(parameters['as'])
                                with tag('peer-groups'):
                                    with tag('peer-group'):
                                        with tag('peer-group-name'): text('IBGP')
                                        with tag('config'):
                                            with tag('peer-group-name'): text('IBGP')
                                            with tag('peer-as'): text(parameters['protocol_name'])
                                        with tag('afi-safis'):
                                            with tag('afi-safi'):
                                                with tag('afi-safi-name', 'xmlns:oc-bgp-types="http://openconfig.net/yang/bgp-types"'): text('oc-bgp-types:IPV4_UNICAST')
                                                with tag('config'):
                                                    with tag('afi-safi-name', 'xmlns:oc-bgp-types="http://openconfig.net/yang/bgp-types"'): text('oc-bgp-types:IPV4_UNICAST')
                                                    with tag('enabled'): text('true')

                                        if "router_id" in parameters: 
                                            with tag('router-id'):text(parameters['router_id'])
                                if 'neighbors' in parameters:
                                    with tag('neighbors'):
                                        for neighbor in parameters['neighbors']:
                                            with tag('neighbor'):
                                                with tag('neighbor-address'): text(neighbor['ip_address'])
                                                with tag('afi-safis'):
                                                    with tag('afi-safi', 'xmlns:oc-bgp-types="http://openconfig.net/yang/bgp-types"'):
                                                        with tag('afi-safi-name'): text('oc-bgp-types:IPV4_UNICAST')
                                                        with tag('enabled'): text('true')
                                                with tag('config'):
                                                    with tag('neighbor-address'): text(neighbor['ip_address'])
                                                  with tag('peer-group'): text('IBGP')
                                                    with tag('enabled'): text('true')
                                                    with tag('peer-as'): text(parameters['as'])
                # if vendor == "ADVA": 
                if True: 
                    with tag('tables'):
@@ -207,6 +197,9 @@ def associate_If_to_NI(parameters, DEL):
        else:
            with tag('network-instance'):
                with tag('name'):text(parameters['name'])
                with tag('config'):
                    with tag('name'):text(parameters['name'])
                    with tag('type', 'xmlns:oc-ni-types="http://openconfig.net/yang/network-instance-types"'):text('oc-ni-types:L3VRF')
                with tag('interfaces'):
                    with tag('interface'):
                        with tag('id'):text(parameters['id'])
@@ -214,6 +207,25 @@ def associate_If_to_NI(parameters, DEL):
                            with tag('id')          :text(parameters['id'])
                            with tag('interface')   :text(parameters['interface'])
                            with tag('subinterface'):text(parameters['subinterface'])
    with tag('interfaces', 'xmlns="http://openconfig.net/yang/interfaces"'):
        with tag('interface'):
            with tag('name'):text(parameters['interface'])
            with tag('config'):
                with tag('name'):text(parameters['interface'])
            with tag('subinterfaces'):
                with tag('subinterface'):
                    with tag('index'): text(parameters['subinterface'])
                    with tag('config'):
                        with tag('index'): text(parameters['subinterface'])
                    with tag('ipv4',  'xmlns="http://openconfig.net/yang/interfaces/ip"'):
                        with tag('config'):
                            with tag('mtu'): text('1500')
                        with tag('addresses'):
                            with tag('address'):
                                with tag('ip'):text(parameters['address_ip'])
                                with tag('config'):
                                    with tag('ip'):text(parameters['address_ip'])
                                    with tag('prefix-length'):text(parameters['address_prefix'])
                            
    result = indent(
        doc.getvalue(),
@@ -356,6 +368,7 @@ def create_table_conns(parameters,DEL):
                        with tag('src-protocol','xmlns:oc-pol-types="http://openconfig.net/yang/policy-types"'):   text('oc-pol-types:',parameters['src_protocol'])
                        with tag('dst-protocol','xmlns:oc-pol-types="http://openconfig.net/yang/policy-types"'):   text('oc-pol-types:',parameters['dst_protocol'])
                        with tag('address-family', 'xmlns:oc-types="http://openconfig.net/yang/openconfig-types"'):text('oc-types:',parameters['address_family'])    
                        with tag('dst-instance', 'xmlns="http://www.ipinfusion.com/yang/ocnos/ipi-oc-ni-augments"'):text('65000')
                        if len(parameters['default_import_policy']) != 0:
                            with tag('default-import-policy'):text(parameters['default_import_policy'])
    result = indent(
+7 −6
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ def create_rp_statement(data, DEL):
    RP_statement_name       = data['statement_name']
    RP_policy_result        = data['policy_result']
    RP_ext_comm_set_name    = data['ext_community_set_name']
    RP_sequence_id          = data['sequence_id']
    

    with tag('routing-policy', xmlns="http://openconfig.net/yang/routing-policy"):
@@ -55,9 +56,9 @@ def create_rp_statement(data, DEL):
                        with tag('name'):text(RP_policy_name)
                    with tag('statements'):
                        with tag('statement'):
                            with tag('name'):text(RP_statement_name)
                            with tag('name'):text(RP_sequence_id)
                            with tag('config'):
                                with tag('name'):text(RP_statement_name)
                                with tag('name'):text(RP_sequence_id)
                            with tag('conditions'):
                                with tag('config'):
                                    with tag('install-protocol-eq', **{'xmlns:openconfig-policy-types': 'http://openconfig.net/yang/policy-types'}):text('openconfig-policy-types:DIRECTLY_CONNECTED')
@@ -133,14 +134,14 @@ data_2 = {'ext_community_member' : '65001:101',
            'ext_community_set_name'    : 'set_srv_101_a'}

print('\nRouting Policy Statement - CREATE\n')
print(rp_statement(data_1, False))
print(create_rp_statement(data_1, False))
print('\nRouting Policy Statement - DELETE\n')
print(rp_statement(data_1, True))
print(create_rp_statement(data_1, True))

print('\nRouting Policy Defined Set - CREATE\n')
print(rp_defined_set(data_2, False))
print(create_rp_def(data_2, False))
print('\nRouting Policy Defined Set - DELETE\n')
print(rp_defined_set(data_2, True))
print(create_rp_def(data_2, True))
'''

'''
+15 −14
Original line number Diff line number Diff line
@@ -38,8 +38,9 @@ ALLOCATION_DIRECTION_DESCRIPTIONS = {
VLAN_TAG = 0
PREFIX_LENGTH = 24
BGP_AS = 65000
policy_AZ = 'srv_{:d}_a'.format(VLAN_TAG)
policy_ZA = 'srv_{:d}_b'.format(VLAN_TAG)
POLICY_AZ = 'srv_{:d}_a'.format(VLAN_TAG)
POLICY_ZA = 'srv_{:d}_b'.format(VLAN_TAG)
BGP_NETWORK_CIDR = '192.168.150.0/24'

def service_2_bwInfo(service: Service) -> dict:
    response = {}
@@ -102,7 +103,6 @@ def bwInfo_2_service(client, bw_info: dict) -> Service:
        devices = client.ListDevices(Empty()).devices
        router_id_counter = 1
        for device in devices:

            device_endpoint_uuids = {ep.name:ep.endpoint_id.endpoint_uuid.uuid for ep in device.device_endpoints}
            for cr in device.device_config.config_rules:
                if cr.WhichOneof('config_rule') != 'custom':
@@ -118,41 +118,42 @@ def bwInfo_2_service(client, bw_info: dict) -> Service:
                ep_id.endpoint_uuid.uuid = device_endpoint_uuids[port_name]
                ep_id.device_id.device_uuid.uuid = device.device_id.device_uuid.uuid
                service.service_endpoint_ids.append(ep_id)

                # add interface config rules
                endpoint_settings_key = ENDPOINT_SETTINGS_KEY.format(device.name, port_name, VLAN_TAG)
                if address_ip == a_ip:
                    field_updates = {
                    'address_ip': (address_ip, True),
                    # 'router_id': ('.'.join([str(router_id_counter)]*4), True),
                    'router_id': ('200.1.1.1', True),
                    'neighbor_address_ip': ('192.168.150.2', True),
                    'self_bgp_interface_name': ('xe5', True),
                    'self_bgp_interface_address_ip': ('192.168.150.1', True),
                    'self_bgp_interface_address_prefix': (PREFIX_LENGTH, True),
                    'self_bgp_sub_interface_index': (0, True),
                    'route_distinguisher': (route_distinguisher, True),
                    'sub_interface_index': (0, True),
                    'vlan_id'            : (VLAN_TAG, True),
                    # 'bgp_as': (BGP_AS+router_id_counter, True),
                    'bgp_as': (BGP_AS, True),
                    'ip_address': (address_ip, True),
                    'prefix_length': (PREFIX_LENGTH, True),
                    'policy_AZ'           : (policy_AZ, True),
                    'policy_ZA'           : (policy_ZA, True),
                    'policy_AZ'           : (POLICY_AZ, True),
                    'policy_ZA'           : (POLICY_ZA, True),
                    'address_prefix'     : (PREFIX_LENGTH, True),
                        }
                elif address_ip == z_ip:
                    field_updates = {
                    'address_ip': (address_ip, True),
                    # 'router_id': ('.'.join([str(router_id_counter)]*4), True),
                    'router_id': ('200.1.1.2', True),
                    'neighbor_address_ip': ('192.168.150.1', True),
                    'self_bgp_interface_name': ('xe5', True),
                    'self_bgp_interface_address_ip': ('192.168.150.2', True),
                    'self_bgp_interface_address_prefix': (PREFIX_LENGTH, True),
                    'self_bgp_sub_interface_index': (0, True),
                    'route_distinguisher': (route_distinguisher, True),
                    'sub_interface_index': (0, True),
                    'vlan_id'            : (VLAN_TAG, True),
                    # 'bgp_as': (BGP_AS+router_id_counter, True),
                    'bgp_as': (BGP_AS, True),
                    'ip_address': (address_ip, True),
                    'prefix_length': (PREFIX_LENGTH, True),
                    'policy_AZ'           : (policy_ZA, True),
                    'policy_ZA'           : (policy_AZ, True),
                    'policy_AZ'           : (POLICY_ZA, True),
                    'policy_ZA'           : (POLICY_AZ, True),
                    'address_prefix'     : (PREFIX_LENGTH, True),
                        }
                router_id_counter += 1
+24 −41
Original line number Diff line number Diff line
@@ -47,11 +47,14 @@ def setup_config_rules(
    network_subinterface_desc = json_endpoint_settings.get('subif_description','')
    #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 = 'default'
    network_instance_name     = json_endpoint_settings.get('ni_name',          service_uuid.split('-')[-1])  #ELAN-AC:1

    '''
    # if_subif_name       = '{:s}.{:d}'.format(endpoint_name, vlan_id)
    bgp_if_name = json_endpoint_settings.get('self_bgp_interface_name','')
    bgp_address_ip = json_endpoint_settings.get('self_bgp_interface_address_ip','')
    bgp_address_prefix = json_endpoint_settings.get('self_bgp_interface_address_prefix','')
    bgp_sub_interface_index = json_endpoint_settings.get('self_bgp_sub_interface_index', 0        )

    # if_subif_name       = '{:s}.{:d}'.format(endpoint_name, 0)
    if_subif_name       = '{:s}'.format(endpoint_name[5:])

    json_config_rules = [
@@ -70,7 +73,7 @@ def setup_config_rules(
                'description': network_interface_desc, 
                'type': 'L3VRF',
                'route_distinguisher': route_distinguisher,
                #'router_id': router_id,
                'router_id': router_id,
                #'address_families': address_families,
        }),

@@ -78,12 +81,12 @@ def setup_config_rules(
        json_config_rule_set(
            '/network_instance[{:s}]/protocols[BGP]'.format(network_instance_name), {
                'name': network_instance_name, 
                # 'protocol_name': 'BGP', 
                'protocol_name': bgp_as, 
                'identifier': 'BGP', 
                # 'identifier': bgp_as, 
                'type': 'L3VRF',
                'as': bgp_as,
                'router_id': router_id, 
                'neighbors': [{'ip_address': neighbor_address_ip, 'remote_as': bgp_as}]
        }),

        #Add DIRECTLY CONNECTED protocol to network instance
@@ -107,7 +110,6 @@ def setup_config_rules(
        json_config_rule_set(
            '/interface[{:s}]/subinterface[{:d}]'.format(if_subif_name, sub_interface_index), {
                'name'          : if_subif_name,
                # 'type'          :'l3ipvlan',
                'type'          :'ethernetCsmacd',
                'mtu'           : mtu,
                'index'         : sub_interface_index,
@@ -124,6 +126,18 @@ def setup_config_rules(
                'id'          : if_subif_name, 
                'interface'   : if_subif_name,
                'subinterface': sub_interface_index,
                'address_ip'    : address_ip, 
                'address_prefix': address_prefix,
        }), 

        json_config_rule_set(
            '/network_instance[{:s}]/interface[{:s}]'.format(network_instance_name, bgp_if_name), {
                'name'        : network_instance_name, 
                'id'          : bgp_if_name, 
                'interface'   : bgp_if_name,
                'subinterface': bgp_sub_interface_index,
                'address_ip'    : bgp_address_ip, 
                'address_prefix': bgp_address_prefix,
        }), 

        #Create routing policy
@@ -139,6 +153,7 @@ def setup_config_rules(
                'statement_name'        : 'stm_{:s}'.format(policy_import),
                'ext_community_set_name': 'set_{:s}'.format(policy_import),
                'policy_result'         : 'ACCEPT_ROUTE',
                'sequence_id'           : '10'
        }),

        #Associate routing policy to network instance
@@ -161,6 +176,7 @@ def setup_config_rules(
                'statement_name'        : 'stm_{:s}'.format(policy_export),
                'ext_community_set_name': 'set_{:s}'.format(policy_export),
                'policy_result'         : 'ACCEPT_ROUTE',
                'sequence_id'           : '10'
        }),

        #Associate routing policy to network instance
@@ -189,39 +205,6 @@ def setup_config_rules(
                'default_import_policy': 'ACCEPT_ROUTE',
        }),

    ]
    '''
    if_subif_name       = '{:s}'.format(endpoint_name[5:])

    json_config_rules = [

        #Add DIRECTLY CONNECTED protocol to network instance
        json_config_rule_set(
            '/network_instance[{:s}]/protocols[DIRECTLY_CONNECTED]'.format(network_instance_name), {
                'name': network_instance_name, 
                'identifier': 'DIRECTLY_CONNECTED', 
                'protocol_name': 'DIRECTLY_CONNECTED',
        }),

        # Add BGP neighbors
        json_config_rule_set(
            '/network_instance[{:s}]/protocols[BGP]'.format(network_instance_name), {
                'name': network_instance_name, 
                'protocol_name': bgp_as, 
                'identifier': 'BGP', 
                'as': bgp_as,
                'router_id': router_id, 
                'neighbors': [{'ip_address': neighbor_address_ip, 'remote_as': bgp_as}]
        }),
        json_config_rule_set(
            '/network_instance[{:s}]/table_connections[DIRECTLY_CONNECTED][BGP][IPV4]'.format(network_instance_name), {
                'name'                 : network_instance_name,
                'src_protocol'         : 'DIRECTLY_CONNECTED',
                'dst_protocol'         : 'BGP',
                'address_family'       : 'IPV4',
                'default_import_policy': 'ACCEPT_ROUTE',
        }),

    ]

    for res_key, res_value in endpoint_acls: