|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# led_control.py |
| 4 | +# |
| 5 | +# Platform-specific LED control functionality for SONiC |
| 6 | +# |
| 7 | + |
| 8 | +try: |
| 9 | + from sonic_led.led_control_base import LedControlBase |
| 10 | +except ImportError, e: |
| 11 | + raise ImportError (str(e) + " - required module not found") |
| 12 | + |
| 13 | + |
| 14 | +class LedControl(LedControlBase): |
| 15 | + """Platform specific LED control class""" |
| 16 | + |
| 17 | + SONIC_PORT_NAME_PREFIX = "Ethernet" |
| 18 | + |
| 19 | + LED_SYSFS_PATH_BREAKOUT_CAPABLE = "/sys/class/leds/qsfp{0}_{1}/brightness" |
| 20 | + LED_SYSFS_PATH_NO_BREAKOUT = "/sys/class/leds/qsfp{0}/brightness" |
| 21 | + |
| 22 | + QSFP_BREAKOUT_START_IDX = 1 |
| 23 | + QSFP_BREAKOUT_END_IDX = 24 |
| 24 | + QSFP_NO_BREAKOUT_START_IDX = 25 |
| 25 | + QSFP_NO_BREAKOUT_END_IDX = 32 |
| 26 | + |
| 27 | + LED_COLOR_OFF = 0 |
| 28 | + LED_COLOR_GREEN = 1 |
| 29 | + LED_COLOR_YELLOW = 2 |
| 30 | + |
| 31 | + # Helper method to map SONiC port name to Arista QSFP index |
| 32 | + def _port_name_to_qsfp_index(self, port_name): |
| 33 | + # Strip "Ethernet" off port name |
| 34 | + if not port_name.startswith(self.SONIC_PORT_NAME_PREFIX): |
| 35 | + return -1 |
| 36 | + |
| 37 | + sonic_port_num = int(port_name[len(self.SONIC_PORT_NAME_PREFIX):]) |
| 38 | + |
| 39 | + # SONiC port nums are 0-based and increment by 4 |
| 40 | + # Arista QSFP indices are 1-based and increment by 1 |
| 41 | + return ((sonic_port_num/4) + 1) |
| 42 | + |
| 43 | + # Concrete implementation of port_link_state_change() method |
| 44 | + def port_link_state_change(self, port, state): |
| 45 | + qsfp_index = self._port_name_to_qsfp_index(port) |
| 46 | + |
| 47 | + # Ignore invalid QSFP indices |
| 48 | + if qsfp_index <= 0: |
| 49 | + return |
| 50 | + |
| 51 | + # QSFP indices 1-24 are breakout-capable and have four LEDs, |
| 52 | + # whereas indices 25-32 are not breakout-capable, and only have one |
| 53 | + if qsfp_index <= self.QSFP_BREAKOUT_END_IDX: |
| 54 | + led_sysfs_path = self.LED_SYSFS_PATH_BREAKOUT_CAPABLE.format(qsfp_index, 1) |
| 55 | + else: |
| 56 | + led_sysfs_path = self.LED_SYSFS_PATH_NO_BREAKOUT.format(qsfp_index) |
| 57 | + |
| 58 | + led_file = open(led_sysfs_path, "w") |
| 59 | + |
| 60 | + if state == "up": |
| 61 | + led_file.write("%d" % self.LED_COLOR_GREEN) |
| 62 | + else: |
| 63 | + led_file.write("%d" % self.LED_COLOR_OFF) |
| 64 | + |
| 65 | + led_file.close() |
| 66 | + |
| 67 | + # Constructor |
| 68 | + def __init__(self): |
| 69 | + # Initialize: Turn all front panel QSFP LEDs off |
| 70 | + for qsfp_index in range(self.QSFP_BREAKOUT_START_IDX, self.QSFP_BREAKOUT_END_IDX + 1): |
| 71 | + for lane in range(1, 5): |
| 72 | + led_sysfs_path = self.LED_SYSFS_PATH_BREAKOUT_CAPABLE.format(qsfp_index, lane) |
| 73 | + with open(led_sysfs_path, 'w') as led_file: |
| 74 | + led_file.write("%d" % self.LED_COLOR_OFF) |
| 75 | + |
| 76 | + for qsfp_index in range(self.QSFP_NO_BREAKOUT_START_IDX, self.QSFP_NO_BREAKOUT_END_IDX + 1): |
| 77 | + led_sysfs_path = self.LED_SYSFS_PATH_NO_BREAKOUT.format(qsfp_index) |
| 78 | + with open(led_sysfs_path, 'w') as led_file: |
| 79 | + led_file.write("%d" % self.LED_COLOR_OFF) |
| 80 | + |
0 commit comments