diff --git a/config/main.py b/config/main.py index ebc625cadb8b..ed071c68870d 100755 --- a/config/main.py +++ b/config/main.py @@ -955,10 +955,13 @@ def add(ctx, interface_name, ip_addr): ipaddress.ip_network(unicode(ip_addr), strict=False) if interface_name.startswith("Ethernet"): config_db.set_entry("INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"}) + config_db.set_entry("INTERFACE", interface_name, {"NULL": "NULL"}) elif interface_name.startswith("PortChannel"): config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"}) + config_db.set_entry("PORTCHANNEL_INTERFACE", interface_name, {"NULL": "NULL"}) elif interface_name.startswith("Vlan"): config_db.set_entry("VLAN_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"}) + config_db.set_entry("VLAN_INTERFACE", interface_name, {"NULL": "NULL"}) elif interface_name.startswith("Loopback"): config_db.set_entry("LOOPBACK_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"}) else: @@ -982,20 +985,38 @@ def remove(ctx, interface_name, ip_addr): if interface_name is None: ctx.fail("'interface_name' is None!") + if_table = "" try: ipaddress.ip_network(unicode(ip_addr), strict=False) if interface_name.startswith("Ethernet"): config_db.set_entry("INTERFACE", (interface_name, ip_addr), None) + if_table = "INTERFACE" elif interface_name.startswith("PortChannel"): config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), None) + if_table = "PORTCHANNEL_INTERFACE" elif interface_name.startswith("Vlan"): config_db.set_entry("VLAN_INTERFACE", (interface_name, ip_addr), None) + if_table = "VLAN_INTERFACE" elif interface_name.startswith("Loopback"): config_db.set_entry("LOOPBACK_INTERFACE", (interface_name, ip_addr), None) else: ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan/Loopback]") except ValueError: ctx.fail("'ip_addr' is not valid.") + + exists = False + if if_table: + interfaces = config_db.get_table(if_table) + for key in interfaces.keys(): + if not isinstance(key, tuple): + continue + if interface_name in key: + exists = True + break + + if not exists: + config_db.set_entry(if_table, interface_name, None) + # # 'acl' group ('config acl ...') # diff --git a/scripts/db_migrator.py b/scripts/db_migrator.py index 0eca7c621439..b4dd03c416b8 100755 --- a/scripts/db_migrator.py +++ b/scripts/db_migrator.py @@ -50,12 +50,51 @@ def __init__(self, socket=None): def migrate_pfc_wd_table(self): - # Migrate all data entries from table PFC_WD_TABLE to PFC_WD + ''' + Migrate all data entries from table PFC_WD_TABLE to PFC_WD + ''' data = self.configDB.get_table('PFC_WD_TABLE') for key in data.keys(): self.configDB.set_entry('PFC_WD', key, data[key]) self.configDB.delete_table('PFC_WD_TABLE') + def is_ip_prefix_in_key(self, key): + ''' + Function to check if IP address is present in the key. If it + is present, then the key would be a tuple or else, it shall be + be string + ''' + return (isinstance(key, tuple)) + + def migrate_interface_table(self): + ''' + Migrate all data from existing INTERFACE table with IP Prefix + to have an additional ONE entry without IP Prefix. For. e.g, for an entry + "Vlan1000|192.168.0.1/21": {}", this function shall add an entry without + IP prefix as ""Vlan1000": {}". This is for VRF compatibility. + ''' + if_db = [] + if_tables = { + 'INTERFACE', + 'PORTCHANNEL_INTERFACE', + 'VLAN_INTERFACE' + } + for table in if_tables: + data = self.configDB.get_table(table) + for key in data.keys(): + if not self.is_ip_prefix_in_key(key): + if_db.append(key) + continue + + for table in if_tables: + data = self.configDB.get_table(table) + for key in data.keys(): + if not self.is_ip_prefix_in_key(key) or key[0] in if_db: + continue + log_info('Migrating interface table for ' + key[0]) + self.configDB.set_entry(table, key[0], data[key]) + if_db.append(key[0]) + def version_unknown(self): """ @@ -75,6 +114,7 @@ def version_unknown(self): # If new DB version is added in the future, the incremental # upgrade will take care of the subsequent migrations. self.migrate_pfc_wd_table() + self.migrate_interface_table() self.set_version('version_1_0_1') return 'version_1_0_1' diff --git a/scripts/neighbor_advertiser b/scripts/neighbor_advertiser index 81f8786068d6..550bd60b20ef 100644 --- a/scripts/neighbor_advertiser +++ b/scripts/neighbor_advertiser @@ -110,11 +110,22 @@ def extract_ip_ver_addr(ip_prefix): return (ver, addr) +def is_ip_prefix_in_key(key): + ''' + Function to check if IP address is present in the key. If it + is present, then the key would be a tuple or else, it shall be + be string + ''' + return (isinstance(key, tuple)) + + def get_loopback_addr(ip_ver): loopback_intfs = config_db.get_table('LOOPBACK_INTERFACE') loopback_addr = '' for intf in loopback_intfs.keys(): + if not is_ip_prefix_in_key(intf): + continue if 'Loopback0' in intf: intf_ip_prefix = intf[1] (intf_ip_ver, intf_ip_addr) = extract_ip_ver_addr(intf_ip_prefix) @@ -170,6 +181,8 @@ def get_vlan_addr(vlan_intf_name, ip_ver): vlan_addr = [] for intf in vlan_intfs.keys(): + if not is_ip_prefix_in_key(intf): + continue if vlan_intf_name in intf: intf_ip_prefix = intf[1] (intf_ip_ver, intf_ip_addr) = extract_ip_ver_addr(intf_ip_prefix) diff --git a/scripts/pcmping b/scripts/pcmping index 1f6126327435..f754c2fda466 100755 --- a/scripts/pcmping +++ b/scripts/pcmping @@ -86,13 +86,21 @@ def get_portchannel(interface): sys.stderr.write("Interface %s is not a portchannel member. Please Check!\n" % interface) sys.exit(1) +def is_ip_prefix_in_key(key): + ''' + Function to check if IP address is present in the key. If it + is present, then the key would be a tuple or else, it shall be + be string + ''' + return (isinstance(key, tuple)) + def get_portchannel_ipv4(portchannel_name): configdb = swsssdk.ConfigDBConnector() configdb.connect() config = configdb.get_config() portchannel_interfaces = config["PORTCHANNEL_INTERFACE"] for key in portchannel_interfaces.keys(): - if len(key) == 1: + if not is_ip_prefix_in_key(key): continue pc, ip = key ip = ip.split("/")[0] diff --git a/show/main.py b/show/main.py index ae2b3706952f..9f5631d03806 100755 --- a/show/main.py +++ b/show/main.py @@ -200,6 +200,15 @@ def get_interface_mode(): return mode +def is_ip_prefix_in_key(key): + ''' + Function to check if IP address is present in the key. If it + is present, then the key would be a tuple or else, it shall be + be string + ''' + return (isinstance(key, tuple)) + + # Global class instance for SONiC interface name to alias conversion iface_alias_converter = InterfaceAliasConverter() @@ -1503,7 +1512,7 @@ def brief(verbose): # Parsing VLAN Gateway info for key in natsorted(vlan_ip_data.keys()): - if len(key) == 1: + if not is_ip_prefix_in_key(key): continue interface_key = str(key[0].strip("Vlan")) interface_value = str(key[1])