Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lldp]Fix the issue of only one field lldp_rem_time_mark in APPL_DB #71

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions src/lldp_syncd/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,25 +390,36 @@ def sync(self, parsed_update):

new, changed, deleted = self.cache_diff(self.interfaces_cache, parsed_update)

# For changed elements, if only lldp_rem_time_mark changed, update its value, otherwise delete and repopulate
for interface in changed:
if re.match(SONIC_ETHERNET_RE_PATTERN, interface) is None:
logger.warning("Ignoring interface '{}'".format(interface))
continue
table_key = ':'.join([LldpSyncDaemon.LLDP_ENTRY_TABLE, interface])
if self.is_only_time_mark_modified(self.interfaces_cache[interface], parsed_update[interface]):
self.db_connector.set(self.db_connector.APPL_DB, table_key, 'lldp_rem_time_mark', parsed_update[interface]['lldp_rem_time_mark'], blocking=True)
logger.debug("Only sync'd interface {} lldp_rem_time_mark: {}".format(interface, parsed_update[interface]['lldp_rem_time_mark']))
else:
if new or deleted:
# If detects any new or deleted interfaces, repopulate for changed interfaces
for interface in changed:
if re.match(SONIC_ETHERNET_RE_PATTERN, interface) is None:
logger.warning("Ignoring interface '{}'".format(interface))
continue
table_key = ':'.join([LldpSyncDaemon.LLDP_ENTRY_TABLE, interface])
self.db_connector.delete(self.db_connector.APPL_DB, table_key)
self.db_connector.hmset(self.db_connector.APPL_DB, table_key, parsed_update[interface])
logger.debug("Sync'd changed interface {} : {}".format(interface, parsed_update[interface]))
logger.info("Force repopulate the changed interface {} : {}".format(interface, parsed_update[interface]))
else:
# For changed elements, if only lldp_rem_time_mark changed, update its value, otherwise delete and repopulate
for interface in changed:
if re.match(SONIC_ETHERNET_RE_PATTERN, interface) is None:
logger.warning("Ignoring interface '{}'".format(interface))
continue
table_key = ':'.join([LldpSyncDaemon.LLDP_ENTRY_TABLE, interface])
if self.is_only_time_mark_modified(self.interfaces_cache[interface], parsed_update[interface]):
self.db_connector.set(self.db_connector.APPL_DB, table_key, 'lldp_rem_time_mark', parsed_update[interface]['lldp_rem_time_mark'], blocking=True)
logger.debug("Only sync'd interface {} lldp_rem_time_mark: {}".format(interface, parsed_update[interface]['lldp_rem_time_mark']))
else:
self.db_connector.delete(self.db_connector.APPL_DB, table_key)
self.db_connector.hmset(self.db_connector.APPL_DB, table_key, parsed_update[interface])
logger.info("Repopulate for changed interface {} : {}".format(interface, parsed_update[interface]))
self.interfaces_cache = parsed_update
# Delete LLDP_ENTRIES which are missing
for interface in deleted:
table_key = ':'.join([LldpSyncDaemon.LLDP_ENTRY_TABLE, interface])
self.db_connector.delete(self.db_connector.APPL_DB, table_key)
logger.debug("Delete table_key: {}".format(table_key))
logger.info("Delete table_key: {}".format(table_key))
# Repopulate LLDP_ENTRY_TABLE by adding new elements
for interface in new:
if re.match(SONIC_ETHERNET_RE_PATTERN, interface) is None:
Expand All @@ -417,4 +428,4 @@ def sync(self, parsed_update):
# port_table_key = LLDP_ENTRY_TABLE:INTERFACE_NAME;
table_key = ':'.join([LldpSyncDaemon.LLDP_ENTRY_TABLE, interface])
self.db_connector.hmset(self.db_connector.APPL_DB, table_key, parsed_update[interface])
logger.debug("Add new interface {} : {}".format(interface, parsed_update[interface]))
logger.info("Add new interface {} : {}".format(interface, parsed_update[interface]))
55 changes: 54 additions & 1 deletion tests/test_lldpSyncDaemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from swsscommon.swsscommon import SonicV2Connector

INPUT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'subproc_outputs')

TABLE_PREFIX = "LLDP_ENTRY_TABLE:"

def create_dbconnector():
db = SonicV2Connector()
Expand Down Expand Up @@ -218,3 +218,56 @@ def test_invalid_chassis_name(self, mock_check_output):
'''
result = self.daemon.source_update()
self.assertIsNone(result)


def test_changed_interface(self):
parsed_update = self.daemon.parse_update(self._json)
self.daemon.sync(parsed_update)
db = create_dbconnector()
keys = db.keys(db.APPL_DB)
# Check if each lldp_rem_time_mark is changed
dump = {}
for k in keys:
if k != 'LLDP_LOC_CHASSIS':
if TABLE_PREFIX + 'eth0' == k or TABLE_PREFIX + 'Ethernet0' == k:
dump[k] = db.get(db.APPL_DB, k, 'lldp_rem_time_mark')

time.sleep(1)
# simulate lldp_rem_time_mark was changed or port description was changed or interface was removed
changed_json = self._json.copy()
changed_json['lldp']['interface'][0]['eth0']['age'] = '0 day, 05:09:12'
changed_json['lldp']['interface'][1]['Ethernet0']['age'] = '0 day, 05:09:15'

parsed_update = self.daemon.parse_update(changed_json)
self.daemon.sync(parsed_update)
keys = db.keys(db.APPL_DB)

jo = {}
for k in keys:
if k != 'LLDP_LOC_CHASSIS':
if TABLE_PREFIX + 'eth0' == k or TABLE_PREFIX + 'Ethernet0' == k:
jo[k] = db.get(db.APPL_DB, k, 'lldp_rem_time_mark')
self.assertEqual(int(jo[k]), int(dump[k])+10)
else:
jo[k] = db.get_all(db.APPL_DB, k)
time.sleep(1)
# simulate lldp_rem_time_mark was changed or port description was changed or interface was removed
changed_json = self._json.copy()
changed_json['lldp']['interface'][0]['eth0']['age'] = '0 day, 05:09:12'
changed_json['lldp']['interface'][1]['Ethernet0']['port']['descr'] = 'Ethernet1'

parsed_update = self.daemon.parse_update(changed_json)
self.daemon.sync(parsed_update)
keys = db.keys(db.APPL_DB)

jo = {}
for k in keys:
if k != 'LLDP_LOC_CHASSIS':
if TABLE_PREFIX + 'eth0' == k:
jo[k] = db.get(db.APPL_DB, k, 'lldp_rem_time_mark')
self.assertEqual(int(jo[k]), int(dump[k])+10)
elif TABLE_PREFIX + 'Ethernet0' == k:
jo[k] = db.get(db.APPL_DB, k, 'lldp_rem_port_desc')
self.assertEqual(jo[k], 'Ethernet1')
else:
jo[k] = db.get_all(db.APPL_DB, k)
Loading