From be974bf33604356015c1fc39f563677256ff92ef Mon Sep 17 00:00:00 2001 From: Sumukha Tumkur Vani Date: Wed, 5 May 2021 09:16:22 -0700 Subject: [PATCH] [neighbor_advertiser] Use existing tunnel if present for creating tunnel mappings (#1589) --- scripts/neighbor_advertiser | 18 ++++++++++++++---- tests/neighbor_advertiser_test.py | 9 +++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/scripts/neighbor_advertiser b/scripts/neighbor_advertiser index dc38cf26c34d..5cc09d32c903 100644 --- a/scripts/neighbor_advertiser +++ b/scripts/neighbor_advertiser @@ -169,9 +169,11 @@ def get_loopback_addr(ip_ver): def get_vlan_interfaces(): vlan_info = config_db.get_table('VLAN') vlan_interfaces = [] - + vlan_intfs = config_db.get_table('VLAN_INTERFACE') + # Skip L2 VLANs for vlan_name in vlan_info: - vlan_interfaces.append(vlan_name) + if vlan_name in vlan_intfs: + vlan_interfaces.append(vlan_name) return vlan_interfaces @@ -502,6 +504,14 @@ def reset_mirror_tunnel(): # Set vxlan tunnel # +def check_existing_tunnel(): + vxlan_tunnel = config_db.get_table('VXLAN_TUNNEL') + if len(vxlan_tunnel): + global VXLAN_TUNNEL_NAME + VXLAN_TUNNEL_NAME = list(vxlan_tunnel.keys())[0] + return True + return False + def add_vxlan_tunnel(dst_ipv4_addr): vxlan_tunnel_info = { 'src_ip': get_loopback_addr(4), @@ -517,12 +527,12 @@ def add_vxlan_tunnel_map(): 'vni': get_vlan_interface_vxlan_id(vlan_intf_name), 'vlan': vlan_intf_name } - config_db.set_entry('VXLAN_TUNNEL_MAP', (VXLAN_TUNNEL_NAME, VXLAN_TUNNEL_MAP_PREFIX + str(index)), vxlan_tunnel_map_info) def set_vxlan_tunnel(ferret_server_ip): - add_vxlan_tunnel(ferret_server_ip) + if not check_existing_tunnel(): + add_vxlan_tunnel(ferret_server_ip) add_vxlan_tunnel_map() log.log_info('Finish setting vxlan tunnel; Ferret: {}'.format(ferret_server_ip)) diff --git a/tests/neighbor_advertiser_test.py b/tests/neighbor_advertiser_test.py index 4a7ab41863a1..3ad575c98341 100644 --- a/tests/neighbor_advertiser_test.py +++ b/tests/neighbor_advertiser_test.py @@ -57,3 +57,12 @@ def test_neighbor_advertiser_slice(self, set_up): } ) assert output == expected_output + + def test_set_vxlan(self, set_up): + assert(neighbor_advertiser.check_existing_tunnel()) + neighbor_advertiser.add_vxlan_tunnel_map() + tunnel_mapping = neighbor_advertiser.config_db.get_table('VXLAN_TUNNEL_MAP') + expected_mapping = {("vtep1", "map_1"): {"vni": "1000", "vlan": "Vlan1000"}, ("vtep1", "map_2"): {"vni": "2000", "vlan": "Vlan2000"}} + for key in expected_mapping.keys(): + assert(key in tunnel_mapping.keys()) + assert(expected_mapping[key] == tunnel_mapping[key])