2021-10-17 11:03:02 -05:00
|
|
|
#
|
|
|
|
# Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES.
|
|
|
|
# Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
2020-05-13 12:01:32 -05:00
|
|
|
import os
|
2021-10-24 23:59:06 -05:00
|
|
|
from sonic_py_common.logger import Logger
|
2020-05-13 12:01:32 -05:00
|
|
|
|
2021-10-24 23:59:06 -05:00
|
|
|
from . import utils
|
|
|
|
|
|
|
|
logger = Logger()
|
2020-05-13 12:01:32 -05:00
|
|
|
|
|
|
|
class Led(object):
|
|
|
|
STATUS_LED_COLOR_GREEN = 'green'
|
2020-07-13 12:22:39 -05:00
|
|
|
STATUS_LED_COLOR_GREEN_BLINK = 'green_blink'
|
2020-05-13 12:01:32 -05:00
|
|
|
STATUS_LED_COLOR_RED = 'red'
|
2020-07-13 12:22:39 -05:00
|
|
|
STATUS_LED_COLOR_RED_BLINK = 'red_blink'
|
2020-05-13 12:01:32 -05:00
|
|
|
STATUS_LED_COLOR_ORANGE = 'orange'
|
2020-07-13 12:22:39 -05:00
|
|
|
STATUS_LED_COLOR_ORANGE_BLINK = 'orange_blink'
|
2020-05-13 12:01:32 -05:00
|
|
|
STATUS_LED_COLOR_OFF = 'off'
|
|
|
|
|
2021-10-24 23:59:06 -05:00
|
|
|
LED_ON = '255'
|
2020-05-13 12:01:32 -05:00
|
|
|
LED_OFF = '0'
|
2020-07-13 12:22:39 -05:00
|
|
|
LED_BLINK = '50'
|
2020-05-13 12:01:32 -05:00
|
|
|
|
|
|
|
LED_PATH = "/var/run/hw-management/led/"
|
|
|
|
|
|
|
|
def set_status(self, color):
|
|
|
|
led_cap_list = self.get_capability()
|
|
|
|
if led_cap_list is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
status = False
|
|
|
|
try:
|
2020-07-13 12:22:39 -05:00
|
|
|
self._stop_blink(led_cap_list)
|
|
|
|
blink_pos = color.find('blink')
|
|
|
|
if blink_pos != -1:
|
2021-10-24 23:59:06 -05:00
|
|
|
return self._set_status_blink(color, led_cap_list)
|
2020-07-13 12:22:39 -05:00
|
|
|
|
2020-05-13 12:01:32 -05:00
|
|
|
if color == Led.STATUS_LED_COLOR_GREEN:
|
2021-10-24 23:59:06 -05:00
|
|
|
utils.write_file(self.get_green_led_path(), Led.LED_ON)
|
|
|
|
status = True
|
2020-05-13 12:01:32 -05:00
|
|
|
elif color == Led.STATUS_LED_COLOR_RED:
|
|
|
|
# Some led don't support red led but support orange led, in this case we set led to orange
|
|
|
|
if Led.STATUS_LED_COLOR_RED in led_cap_list:
|
|
|
|
led_path = self.get_red_led_path()
|
|
|
|
elif Led.STATUS_LED_COLOR_ORANGE in led_cap_list:
|
|
|
|
led_path = self.get_orange_led_path()
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2021-10-24 23:59:06 -05:00
|
|
|
utils.write_file(led_path, Led.LED_ON)
|
|
|
|
status = True
|
2020-05-13 12:01:32 -05:00
|
|
|
elif color == Led.STATUS_LED_COLOR_OFF:
|
|
|
|
if Led.STATUS_LED_COLOR_GREEN in led_cap_list:
|
2021-10-24 23:59:06 -05:00
|
|
|
utils.write_file(self.get_green_led_path(), Led.LED_OFF)
|
2020-05-13 12:01:32 -05:00
|
|
|
if Led.STATUS_LED_COLOR_RED in led_cap_list:
|
2021-10-24 23:59:06 -05:00
|
|
|
utils.write_file(self.get_red_led_path(), Led.LED_OFF)
|
2020-05-13 12:01:32 -05:00
|
|
|
if Led.STATUS_LED_COLOR_ORANGE in led_cap_list:
|
2021-10-24 23:59:06 -05:00
|
|
|
utils.write_file(self.get_orange_led_path(), Led.LED_OFF)
|
2020-05-13 12:01:32 -05:00
|
|
|
|
|
|
|
status = True
|
|
|
|
else:
|
|
|
|
status = False
|
|
|
|
except (ValueError, IOError):
|
|
|
|
status = False
|
|
|
|
|
|
|
|
return status
|
|
|
|
|
2021-10-24 23:59:06 -05:00
|
|
|
def _set_status_blink(self, color, led_cap_list):
|
2020-07-13 12:22:39 -05:00
|
|
|
if color not in led_cap_list:
|
|
|
|
if color == Led.STATUS_LED_COLOR_RED_BLINK and Led.STATUS_LED_COLOR_ORANGE_BLINK in led_cap_list:
|
|
|
|
color = Led.STATUS_LED_COLOR_ORANGE_BLINK
|
|
|
|
elif color == Led.STATUS_LED_COLOR_ORANGE_BLINK and Led.STATUS_LED_COLOR_RED_BLINK in led_cap_list:
|
|
|
|
color = Led.STATUS_LED_COLOR_RED_BLINK
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if Led.STATUS_LED_COLOR_GREEN_BLINK == color:
|
2021-10-24 23:59:06 -05:00
|
|
|
return self._set_led_blink_status(self.get_green_led_delay_on_path(), self.get_green_led_delay_off_path(), Led.LED_BLINK)
|
2020-07-13 12:22:39 -05:00
|
|
|
elif Led.STATUS_LED_COLOR_RED_BLINK == color:
|
2021-10-24 23:59:06 -05:00
|
|
|
return self._set_led_blink_status(self.get_red_led_delay_on_path(), self.get_red_led_delay_off_path(), Led.LED_BLINK)
|
2020-07-13 12:22:39 -05:00
|
|
|
elif Led.STATUS_LED_COLOR_ORANGE_BLINK == color:
|
2021-10-24 23:59:06 -05:00
|
|
|
return self._set_led_blink_status(self.get_orange_led_delay_on_path(), self.get_orange_led_delay_off_path(), Led.LED_BLINK)
|
2020-07-13 12:22:39 -05:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _stop_blink(self, led_cap_list):
|
|
|
|
try:
|
|
|
|
if Led.STATUS_LED_COLOR_GREEN_BLINK in led_cap_list:
|
|
|
|
self._set_led_blink_status(self.get_green_led_delay_on_path(), self.get_green_led_delay_off_path(), Led.LED_OFF)
|
|
|
|
if Led.STATUS_LED_COLOR_RED_BLINK in led_cap_list:
|
|
|
|
self._set_led_blink_status(self.get_red_led_delay_on_path(), self.get_red_led_delay_off_path(), Led.LED_OFF)
|
|
|
|
if Led.STATUS_LED_COLOR_ORANGE_BLINK in led_cap_list:
|
|
|
|
self._set_led_blink_status(self.get_orange_led_delay_on_path(), self.get_orange_led_delay_off_path(), Led.LED_OFF)
|
|
|
|
except Exception as e:
|
|
|
|
return
|
|
|
|
|
|
|
|
def _set_led_blink_status(self, delay_on_file, delay_off_file, value):
|
2021-10-24 23:59:06 -05:00
|
|
|
utils.write_file(delay_on_file, value)
|
|
|
|
utils.write_file(delay_off_file, value)
|
|
|
|
return True
|
2020-07-13 12:22:39 -05:00
|
|
|
|
2020-05-13 12:01:32 -05:00
|
|
|
def get_status(self):
|
|
|
|
led_cap_list = self.get_capability()
|
|
|
|
if led_cap_list is None:
|
|
|
|
return Led.STATUS_LED_COLOR_OFF
|
|
|
|
|
|
|
|
try:
|
2020-07-13 12:22:39 -05:00
|
|
|
blink_status = self._get_blink_status(led_cap_list)
|
|
|
|
if blink_status is not None:
|
|
|
|
return blink_status
|
|
|
|
|
2021-10-24 23:59:06 -05:00
|
|
|
if utils.read_str_from_file(self.get_green_led_path()) != Led.LED_OFF:
|
|
|
|
return Led.STATUS_LED_COLOR_GREEN
|
2020-05-13 12:01:32 -05:00
|
|
|
|
|
|
|
if Led.STATUS_LED_COLOR_RED in led_cap_list:
|
2021-10-24 23:59:06 -05:00
|
|
|
if utils.read_str_from_file(self.get_red_led_path()) != Led.LED_OFF:
|
|
|
|
return Led.STATUS_LED_COLOR_RED
|
2020-05-13 12:01:32 -05:00
|
|
|
if Led.STATUS_LED_COLOR_ORANGE in led_cap_list:
|
2021-10-24 23:59:06 -05:00
|
|
|
if utils.read_str_from_file(self.get_orange_led_path()) != Led.LED_OFF:
|
|
|
|
return Led.STATUS_LED_COLOR_RED
|
2020-05-13 12:01:32 -05:00
|
|
|
except (ValueError, IOError) as e:
|
|
|
|
raise RuntimeError("Failed to read led status due to {}".format(repr(e)))
|
|
|
|
|
|
|
|
return Led.STATUS_LED_COLOR_OFF
|
|
|
|
|
2020-07-13 12:22:39 -05:00
|
|
|
def _get_blink_status(self, led_cap_list):
|
|
|
|
try:
|
|
|
|
if Led.STATUS_LED_COLOR_GREEN_BLINK in led_cap_list:
|
|
|
|
if self._is_led_blinking(self.get_green_led_delay_on_path(), self.get_green_led_delay_off_path()):
|
|
|
|
return Led.STATUS_LED_COLOR_GREEN_BLINK
|
2021-10-24 23:59:06 -05:00
|
|
|
|
2020-07-13 12:22:39 -05:00
|
|
|
if Led.STATUS_LED_COLOR_RED_BLINK in led_cap_list:
|
|
|
|
if self._is_led_blinking(self.get_red_led_delay_on_path(), self.get_red_led_delay_off_path()):
|
|
|
|
return Led.STATUS_LED_COLOR_RED_BLINK
|
|
|
|
if Led.STATUS_LED_COLOR_ORANGE_BLINK in led_cap_list:
|
|
|
|
if self._is_led_blinking(self.get_orange_led_delay_on_path(), self.get_orange_led_delay_off_path()):
|
|
|
|
return Led.STATUS_LED_COLOR_ORANGE_BLINK
|
|
|
|
except Exception as e:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _is_led_blinking(self, delay_on_file, delay_off_file):
|
2021-10-24 23:59:06 -05:00
|
|
|
delay_on = utils.read_str_from_file(delay_on_file, default=Led.LED_OFF, log_func=None)
|
|
|
|
delay_off = utils.read_str_from_file(delay_off_file, default=Led.LED_OFF, log_func=None)
|
2020-07-13 12:22:39 -05:00
|
|
|
return delay_on != Led.LED_OFF and delay_off != Led.LED_OFF
|
|
|
|
|
2020-05-13 12:01:32 -05:00
|
|
|
def get_capability(self):
|
2021-10-24 23:59:06 -05:00
|
|
|
caps = utils.read_str_from_file(self.get_led_cap_path())
|
|
|
|
return set(caps.split())
|
2020-05-13 12:01:32 -05:00
|
|
|
|
|
|
|
def get_green_led_path(self):
|
2021-10-24 23:59:06 -05:00
|
|
|
return os.path.join(Led.LED_PATH, 'led_{}_green'.format(self._led_id))
|
2020-05-13 12:01:32 -05:00
|
|
|
|
2020-07-13 12:22:39 -05:00
|
|
|
def get_green_led_delay_off_path(self):
|
2021-10-24 23:59:06 -05:00
|
|
|
return os.path.join(Led.LED_PATH, 'led_{}_green_delay_off'.format(self._led_id))
|
2020-07-13 12:22:39 -05:00
|
|
|
|
|
|
|
def get_green_led_delay_on_path(self):
|
2021-10-24 23:59:06 -05:00
|
|
|
return os.path.join(Led.LED_PATH, 'led_{}_green_delay_on'.format(self._led_id))
|
|
|
|
|
|
|
|
def get_green_led_trigger(self):
|
|
|
|
return os.path.join(Led.LED_PATH, 'led_{}_green_trigger'.format(self._led_id))
|
2020-07-13 12:22:39 -05:00
|
|
|
|
2020-05-13 12:01:32 -05:00
|
|
|
def get_red_led_path(self):
|
2021-10-24 23:59:06 -05:00
|
|
|
return os.path.join(Led.LED_PATH, 'led_{}_red'.format(self._led_id))
|
2020-05-13 12:01:32 -05:00
|
|
|
|
2020-07-13 12:22:39 -05:00
|
|
|
def get_red_led_delay_off_path(self):
|
2021-10-24 23:59:06 -05:00
|
|
|
return os.path.join(Led.LED_PATH, 'led_{}_red_delay_off'.format(self._led_id))
|
2020-07-13 12:22:39 -05:00
|
|
|
|
|
|
|
def get_red_led_delay_on_path(self):
|
2021-10-24 23:59:06 -05:00
|
|
|
return os.path.join(Led.LED_PATH, 'led_{}_red_delay_on'.format(self._led_id))
|
|
|
|
|
|
|
|
def get_red_led_trigger(self):
|
|
|
|
return os.path.join(Led.LED_PATH, 'led_{}_red_trigger'.format(self._led_id))
|
2020-07-13 12:22:39 -05:00
|
|
|
|
2020-05-13 12:01:32 -05:00
|
|
|
def get_orange_led_path(self):
|
2021-10-24 23:59:06 -05:00
|
|
|
return os.path.join(Led.LED_PATH, 'led_{}_orange'.format(self._led_id))
|
2020-05-13 12:01:32 -05:00
|
|
|
|
2020-07-13 12:22:39 -05:00
|
|
|
def get_orange_led_delay_off_path(self):
|
2021-10-24 23:59:06 -05:00
|
|
|
return os.path.join(Led.LED_PATH, 'led_{}_orange_delay_off'.format(self._led_id))
|
2020-07-13 12:22:39 -05:00
|
|
|
|
|
|
|
def get_orange_led_delay_on_path(self):
|
2021-10-24 23:59:06 -05:00
|
|
|
return os.path.join(Led.LED_PATH, 'led_{}_orange_delay_on'.format(self._led_id))
|
|
|
|
|
|
|
|
def get_orange_led_trigger(self):
|
|
|
|
return os.path.join(Led.LED_PATH, 'led_{}_orange_trigger'.format(self._led_id))
|
2020-07-13 12:22:39 -05:00
|
|
|
|
2020-05-13 12:01:32 -05:00
|
|
|
def get_led_cap_path(self):
|
2021-10-24 23:59:06 -05:00
|
|
|
return os.path.join(Led.LED_PATH, 'led_{}_capability'.format(self._led_id))
|
2020-05-13 12:01:32 -05:00
|
|
|
|
|
|
|
|
2021-10-24 23:59:06 -05:00
|
|
|
class FanLed(Led):
|
2020-05-13 12:01:32 -05:00
|
|
|
def __init__(self, index):
|
|
|
|
if index is not None:
|
2021-10-24 23:59:06 -05:00
|
|
|
self._led_id = 'fan{}'.format(index)
|
2020-05-13 12:01:32 -05:00
|
|
|
else:
|
2021-10-24 23:59:06 -05:00
|
|
|
self._led_id = 'fan'
|
2020-05-13 12:01:32 -05:00
|
|
|
|
|
|
|
|
|
|
|
class PsuLed(Led):
|
|
|
|
def __init__(self, index):
|
|
|
|
if index is not None:
|
2021-10-24 23:59:06 -05:00
|
|
|
self._led_id = 'psu{}'.format(index)
|
2020-05-13 12:01:32 -05:00
|
|
|
else:
|
2021-10-24 23:59:06 -05:00
|
|
|
self._led_id = 'psu'
|
2020-05-13 12:01:32 -05:00
|
|
|
|
|
|
|
|
2020-07-13 12:22:39 -05:00
|
|
|
class SystemLed(Led):
|
|
|
|
def __init__(self):
|
2021-10-24 23:59:06 -05:00
|
|
|
self._led_id = 'status'
|
2020-07-13 12:22:39 -05:00
|
|
|
|
|
|
|
|
2020-05-13 12:01:32 -05:00
|
|
|
class SharedLed(object):
|
|
|
|
LED_PRIORITY = {
|
|
|
|
Led.STATUS_LED_COLOR_RED: 0,
|
|
|
|
Led.STATUS_LED_COLOR_GREEN: 1
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, led):
|
|
|
|
self._led = led
|
|
|
|
self._virtual_leds = []
|
|
|
|
|
|
|
|
def add_virtual_leds(self, led):
|
|
|
|
self._virtual_leds.append(led)
|
|
|
|
|
|
|
|
def update_status_led(self):
|
|
|
|
target_color = Led.STATUS_LED_COLOR_GREEN
|
|
|
|
for virtual_led in self._virtual_leds:
|
2020-12-22 16:54:40 -06:00
|
|
|
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
|
2020-05-13 12:01:32 -05:00
|
|
|
return self._led.set_status(target_color)
|
|
|
|
|
|
|
|
def get_status(self):
|
|
|
|
return self._led.get_status()
|
|
|
|
|
|
|
|
|
|
|
|
class ComponentFaultyIndicator(object):
|
|
|
|
def __init__(self, shared_led):
|
|
|
|
self._color = Led.STATUS_LED_COLOR_GREEN
|
|
|
|
self._shared_led = shared_led
|
|
|
|
self._shared_led.add_virtual_leds(self)
|
|
|
|
|
|
|
|
def set_status(self, color):
|
2020-12-22 16:54:40 -06:00
|
|
|
current_color = self._color
|
2020-05-13 12:01:32 -05:00
|
|
|
self._color = color
|
2020-12-22 16:54:40 -06:00
|
|
|
if self._shared_led.update_status_led():
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self._color = current_color
|
|
|
|
return False
|
2020-05-13 12:01:32 -05:00
|
|
|
|
|
|
|
def get_led_color(self):
|
|
|
|
return self._color
|
|
|
|
|
|
|
|
def get_status(self):
|
|
|
|
return self._shared_led.get_status()
|