Skip to content

Commit

Permalink
[Mellanox] PSU led platform API fixes (#6214)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
shlomibitton committed Dec 24, 2020
1 parent 7d4eade commit 6d38654
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
19 changes: 15 additions & 4 deletions platform/mellanox/mlnx-platform-api/sonic_platform/led.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -24,9 +28,11 @@ def add_virtual_leds(self, led):
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

Expand All @@ -41,8 +47,13 @@ def __init__(self, shared_led):
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
Expand Down
59 changes: 24 additions & 35 deletions platform/mellanox/mlnx-platform-api/sonic_platform/psu.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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")

Expand Down Expand Up @@ -188,7 +189,7 @@ def get_power(self):
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):
Expand Down Expand Up @@ -217,33 +218,21 @@ def set_status_led(self, color):

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):
Expand All @@ -261,24 +250,24 @@ def get_status_led(self):
"""
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):
Expand Down

0 comments on commit 6d38654

Please sign in to comment.