Skip to content

Commit

Permalink
[lldp-syncd] Fix unexpected exception in snmp-subagent (#64)
Browse files Browse the repository at this point in the history
Separate deleted and changed scearios
deleted interfaces, delete their table key
changed interfaces
if lldp_rem_time_mark is only changed key, just use hset its new value. Don't delete interface table key.
If other keys are changed as well, delete interface key and use hmset to set the whole dict into DB, instead of using hset each item one by one. This will avoid interface exists but some of its keys are not added.

Signed-off-by: Zhaohui Sun <[email protected]>
  • Loading branch information
ZhaohuiS authored Nov 29, 2023
1 parent e294eb0 commit 4d627ee
Show file tree
Hide file tree
Showing 4 changed files with 499 additions and 376 deletions.
48 changes: 41 additions & 7 deletions src/lldp_syncd/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,26 @@ def cache_diff(self, cache, update):

return new_keys, changed_keys, deleted_keys

def is_only_time_mark_modified(self, cached_interface, updated_interface):
"""
Check if only lldp_rem_time_mark is modified in the update
:param cached_interface: Local cached interface dict
:param updated_interface: Updated interface dict
:return: True if only lldp_rem_time_mark is modified, False otherwise
"""
if len(cached_interface) != len(updated_interface):
return False

changed_keys = 0

for key in cached_interface.keys():
if 'lldp_rem_time_mark' == key and cached_interface[key] != updated_interface[key]:
changed_keys += 1
elif key not in updated_interface or cached_interface[key] != updated_interface[key]:
return False

return True if changed_keys == 1 else False

def sync(self, parsed_update):
"""
Sync LLDP information to redis DB.
Expand All @@ -367,18 +387,32 @@ def sync(self, parsed_update):
logger.debug("sync'd: {}".format(json.dumps(chassis_update, indent=3)))

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:
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]))
self.interfaces_cache = parsed_update
# Delete LLDP_ENTRIES which were modified or are missing
for interface in changed + deleted:
# 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)
# Repopulate LLDP_ENTRY_TABLE by adding all changed elements
for interface in changed + new:
logger.debug("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:
logger.warning("Ignoring interface '{}'".format(interface))
continue
# port_table_key = LLDP_ENTRY_TABLE:INTERFACE_NAME;
table_key = ':'.join([LldpSyncDaemon.LLDP_ENTRY_TABLE, interface])
for k, v in parsed_update[interface].items():
self.db_connector.set(self.db_connector.APPL_DB, table_key, k, v, blocking=True)
logger.debug("sync'd: \n{}".format(json.dumps(parsed_update[interface], indent=3)))
self.db_connector.hmset(self.db_connector.APPL_DB, table_key, parsed_update[interface])
logger.debug("Add new interface {} : {}".format(interface, parsed_update[interface]))
Loading

0 comments on commit 4d627ee

Please sign in to comment.