From 6d386540343c42b385a86e67e33193f72c3747de Mon Sep 17 00:00:00 2001 From: shlomibitton <60430976+shlomibitton@users.noreply.github.com> Date: Thu, 24 Dec 2020 11:11:48 +0200 Subject: [PATCH] [Mellanox] PSU led platform API fixes (#6214) - Why I did it Fix setting PSU led to 'green' or 'red' states. Fix return False if unsupported color request. Remove 'off' option for PSU led API since it is not supported in Mellanox. - How I did it Fix import missing information. Return 'False' when unsupported led color is requested, preventing an exception. - How to verify it Try to set PSU LED to different status with Mellanox platform device. Try to set PSU LED color to unsupported color with Mellanox platform device. --- .../mlnx-platform-api/sonic_platform/led.py | 19 ++++-- .../mlnx-platform-api/sonic_platform/psu.py | 59 ++++++++----------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/led.py b/platform/mellanox/mlnx-platform-api/sonic_platform/led.py index ef39dfcfb4..2a08ec885c 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/led.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/led.py @@ -1,4 +1,8 @@ class Led(object): + LED_PATH = "/var/run/hw-management/led/" + LED_ON = '1' + LED_OFF = '0' + LED_BLINK = '50' STATUS_LED_COLOR_GREEN = 'green' STATUS_LED_COLOR_GREEN_BLINK = 'green_blink' STATUS_LED_COLOR_RED = 'red' @@ -24,9 +28,11 @@ class SharedLed(object): def update_status_led(self): target_color = Led.STATUS_LED_COLOR_GREEN for virtual_led in self._virtual_leds: - if SharedLed.LED_PRIORITY[virtual_led.get_led_color()] < SharedLed.LED_PRIORITY[target_color]: - target_color = virtual_led.get_led_color() - + try: + if SharedLed.LED_PRIORITY[virtual_led.get_led_color()] < SharedLed.LED_PRIORITY[target_color]: + target_color = virtual_led.get_led_color() + except KeyError: + return False self._target_color = target_color return True @@ -41,8 +47,13 @@ class ComponentFaultyIndicator(object): self._shared_led.add_virtual_leds(self) def set_status(self, color): + current_color = self._color self._color = color - return self._shared_led.update_status_led() + if self._shared_led.update_status_led(): + return True + else: + self._color = current_color + return False def get_led_color(self): return self._color diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/psu.py b/platform/mellanox/mlnx-platform-api/sonic_platform/psu.py index bce1773122..c1dd1e2f95 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/psu.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/psu.py @@ -13,6 +13,7 @@ try: from sonic_platform_base.psu_base import PsuBase from sonic_py_common.logger import Logger from sonic_platform.fan import Fan + from sonic_platform.led import Led except ImportError as e: raise ImportError (str(e) + "- required module not found") @@ -188,7 +189,7 @@ class Psu(PsuBase): def _get_led_capability(self): cap_list = None try: - with open(os.path.join(LED_PATH, self.psu_led_cap_path), 'r') as psu_led_cap: + with open(os.path.join(Led.LED_PATH, self.psu_led_cap_path), 'r') as psu_led_cap: caps = psu_led_cap.read() cap_list = caps.split() except (ValueError, IOError): @@ -217,33 +218,21 @@ class Psu(PsuBase): status = False try: - if color == self.STATUS_LED_COLOR_GREEN: - with open(os.path.join(LED_PATH, self.psu_green_led_path), 'w') as psu_led: - psu_led.write(LED_ON) + if color == Led.STATUS_LED_COLOR_GREEN: + with open(os.path.join(Led.LED_PATH, self.psu_green_led_path), 'w') as psu_led: + psu_led.write(Led.LED_ON) status = True - elif color == self.STATUS_LED_COLOR_RED: + elif color == Led.STATUS_LED_COLOR_RED: # Some fan don't support red led but support orange led, in this case we set led to orange - if self.STATUS_LED_COLOR_RED in led_cap_list: - led_path = os.path.join(LED_PATH, self.psu_red_led_path) - elif self.STATUS_LED_COLOR_ORANGE in led_cap_list: - led_path = os.path.join(LED_PATH, self.psu_orange_led_path) + if Led.STATUS_LED_COLOR_RED in led_cap_list: + led_path = os.path.join(Led.LED_PATH, self.psu_red_led_path) + elif Led.STATUS_LED_COLOR_ORANGE in led_cap_list: + led_path = os.path.join(Led.LED_PATH, self.psu_orange_led_path) else: return False with open(led_path, 'w') as psu_led: - psu_led.write(LED_ON) + psu_led.write(Led.LED_ON) status = True - elif color == self.STATUS_LED_COLOR_OFF: - if self.STATUS_LED_COLOR_GREEN in led_cap_list: - with open(os.path.join(LED_PATH, self.psu_green_led_path), 'w') as psu_led: - psu_led.write(str(LED_OFF)) - if self.STATUS_LED_COLOR_RED in led_cap_list: - with open(os.path.join(LED_PATH, self.psu_red_led_path), 'w') as psu_led: - psu_led.write(str(LED_OFF)) - if self.STATUS_LED_COLOR_ORANGE in led_cap_list: - with open(os.path.join(LED_PATH, self.psu_orange_led_path), 'w') as psu_led: - psu_led.write(str(LED_OFF)) - - status = True else: status = False except (ValueError, IOError): @@ -261,24 +250,24 @@ class Psu(PsuBase): """ led_cap_list = self._get_led_capability() if led_cap_list is None: - return self.STATUS_LED_COLOR_OFF + return Led.STATUS_LED_COLOR_OFF try: - with open(os.path.join(LED_PATH, self.psu_green_led_path), 'r') as psu_led: - if LED_OFF != psu_led.read().rstrip('\n'): - return self.STATUS_LED_COLOR_GREEN - if self.STATUS_LED_COLOR_RED in led_cap_list: - with open(os.path.join(LED_PATH, self.psu_red_led_path), 'r') as psu_led: - if LED_OFF != psu_led.read().rstrip('\n'): - return self.STATUS_LED_COLOR_RED - if self.STATUS_LED_COLOR_ORANGE in led_cap_list: - with open(os.path.join(LED_PATH, self.psu_orange_led_path), 'r') as psu_led: - if LED_OFF != psu_led.read().rstrip('\n'): - return self.STATUS_LED_COLOR_RED + with open(os.path.join(Led.LED_PATH, self.psu_green_led_path), 'r') as psu_led: + if Led.LED_OFF != psu_led.read().rstrip('\n'): + return Led.STATUS_LED_COLOR_GREEN + if Led.STATUS_LED_COLOR_RED in led_cap_list: + with open(os.path.join(Led.LED_PATH, self.psu_red_led_path), 'r') as psu_led: + if Led.LED_OFF != psu_led.read().rstrip('\n'): + return Led.STATUS_LED_COLOR_RED + if Led.STATUS_LED_COLOR_ORANGE in led_cap_list: + with open(os.path.join(Led.LED_PATH, self.psu_orange_led_path), 'r') as psu_led: + if Led.LED_OFF != psu_led.read().rstrip('\n'): + return Led.STATUS_LED_COLOR_RED except (ValueError, IOError) as e: raise RuntimeError("Failed to read led status for psu due to {}".format(repr(e))) - return self.STATUS_LED_COLOR_OFF + return Led.STATUS_LED_COLOR_OFF def get_power_available_status(self):