From ce88a38185b72c5a5d52cd58e30b141ec592dcc7 Mon Sep 17 00:00:00 2001 From: Hua Liu <58683130+liuh-80@users.noreply.github.com> Date: Tue, 10 Jan 2023 03:37:43 +0800 Subject: [PATCH] Fix code issue when SonicV2Connector.get() return None. (#13250) Fix code issue when SonicV2Connector.get() return None. #### Why I did it When database key does not exist, SonicV2Connector.get() will return None. Code will break if not check return value. #### How I did it Check SonicV2Connector.get() return value before use it. #### How to verify it Pass all E2E test case. --- .../plugins/led_control.py | 8 +++++++- .../plugins/led_control.py | 9 ++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/device/alphanetworks/x86_64-alphanetworks_snh60a0_320fv2-r0/plugins/led_control.py b/device/alphanetworks/x86_64-alphanetworks_snh60a0_320fv2-r0/plugins/led_control.py index 7ce532554f..cd1f77a277 100644 --- a/device/alphanetworks/x86_64-alphanetworks_snh60a0_320fv2-r0/plugins/led_control.py +++ b/device/alphanetworks/x86_64-alphanetworks_snh60a0_320fv2-r0/plugins/led_control.py @@ -187,9 +187,15 @@ class LedControl(LedControlBase): lanes = swss.get( swss.APPL_DB, self.PORT_TABLE_PREFIX + port_name, 'lanes') + # SonicV2Connector.get() will return None when key does not exist. + if lanes: + lanes_len = len(lanes.split(',')) + else: + lanes_len = 0 + # SONiC port nums are 0-based and increment by 4 # Arista QSFP indices are 1-based and increment by 1 - return (((sonic_port_num/4) + 1), sonic_port_num % 4, len(lanes.split(','))) + return (((sonic_port_num/4) + 1), sonic_port_num % 4, lanes_len) # Concrete implementation of port_link_state_change() method diff --git a/device/alphanetworks/x86_64-alphanetworks_snh60b0_640f-r0/plugins/led_control.py b/device/alphanetworks/x86_64-alphanetworks_snh60b0_640f-r0/plugins/led_control.py index e7c8d3bc1e..67d381d973 100644 --- a/device/alphanetworks/x86_64-alphanetworks_snh60b0_640f-r0/plugins/led_control.py +++ b/device/alphanetworks/x86_64-alphanetworks_snh60b0_640f-r0/plugins/led_control.py @@ -154,9 +154,16 @@ class LedControl(LedControlBase): lanes = swss.get( swss.APPL_DB, self.PORT_TABLE_PREFIX + port_name, 'lanes') + # SonicV2Connector.get() will return None when key does not exist. + if lanes: + lanes_len = len(lanes.split(',')) + else: + lanes_len = 0 + + # SONiC port nums are 0-based and increment by 4 # Arista QSFP indices are 1-based and increment by 1 - return (((sonic_port_num/4) + 1), sonic_port_num % 4, len(lanes.split(','))) + return (((sonic_port_num/4) + 1), sonic_port_num % 4, lanes_len) # Concrete implementation of port_link_state_change() method