|
| 1 | +############################################################################# |
| 2 | +# Edgecore |
| 3 | +# |
| 4 | +# Module contains an implementation of SONiC Platform Base API and |
| 5 | +# provides the Chassis information which are available in the platform |
| 6 | +# |
| 7 | +############################################################################# |
| 8 | + |
| 9 | +import os |
| 10 | + |
| 11 | +try: |
| 12 | + from sonic_platform_base.chassis_base import ChassisBase |
| 13 | + from .helper import APIHelper |
| 14 | +except ImportError as e: |
| 15 | + raise ImportError(str(e) + "- required module not found") |
| 16 | + |
| 17 | +NUM_FAN_TRAY = 6 |
| 18 | +NUM_FAN = 2 |
| 19 | +NUM_PSU = 2 |
| 20 | +NUM_THERMAL = 4 |
| 21 | +PORT_END = 54 |
| 22 | +NUM_COMPONENT = 4 |
| 23 | +HOST_REBOOT_CAUSE_PATH = "/host/reboot-cause/" |
| 24 | +PMON_REBOOT_CAUSE_PATH = "/usr/share/sonic/platform/api_files/reboot-cause/" |
| 25 | +REBOOT_CAUSE_FILE = "reboot-cause.txt" |
| 26 | +PREV_REBOOT_CAUSE_FILE = "previous-reboot-cause.txt" |
| 27 | +HOST_CHK_CMD = "docker > /dev/null 2>&1" |
| 28 | + |
| 29 | + |
| 30 | +class Chassis(ChassisBase): |
| 31 | + """Platform-specific Chassis class""" |
| 32 | + |
| 33 | + def __init__(self): |
| 34 | + ChassisBase.__init__(self) |
| 35 | + self._api_helper = APIHelper() |
| 36 | + self._api_helper = APIHelper() |
| 37 | + self.is_host = self._api_helper.is_host() |
| 38 | + |
| 39 | + self.config_data = {} |
| 40 | + |
| 41 | + self.__initialize_fan() |
| 42 | + self.__initialize_psu() |
| 43 | + self.__initialize_thermals() |
| 44 | + self.__initialize_components() |
| 45 | + self.__initialize_sfp() |
| 46 | + self.__initialize_eeprom() |
| 47 | + |
| 48 | + def __initialize_sfp(self): |
| 49 | + from sonic_platform.sfp import Sfp |
| 50 | + for index in range(0, PORT_END): |
| 51 | + sfp = Sfp(index) |
| 52 | + self._sfp_list.append(sfp) |
| 53 | + self.sfp_module_initialized = True |
| 54 | + |
| 55 | + def __initialize_fan(self): |
| 56 | + from sonic_platform.fan import Fan |
| 57 | + for fant_index in range(0, NUM_FAN_TRAY): |
| 58 | + for fan_index in range(0, NUM_FAN): |
| 59 | + fan = Fan(fant_index, fan_index) |
| 60 | + self._fan_list.append(fan) |
| 61 | + |
| 62 | + def __initialize_psu(self): |
| 63 | + from sonic_platform.psu import Psu |
| 64 | + for index in range(0, NUM_PSU): |
| 65 | + psu = Psu(index) |
| 66 | + self._psu_list.append(psu) |
| 67 | + |
| 68 | + def __initialize_thermals(self): |
| 69 | + from sonic_platform.thermal import Thermal |
| 70 | + for index in range(0, NUM_THERMAL): |
| 71 | + thermal = Thermal(index) |
| 72 | + self._thermal_list.append(thermal) |
| 73 | + |
| 74 | + def __initialize_eeprom(self): |
| 75 | + from sonic_platform.eeprom import Tlv |
| 76 | + self._eeprom = Tlv() |
| 77 | + |
| 78 | + def __initialize_components(self): |
| 79 | + from sonic_platform.component import Component |
| 80 | + for index in range(0, NUM_COMPONENT): |
| 81 | + component = Component(index) |
| 82 | + self._component_list.append(component) |
| 83 | + |
| 84 | + def __initialize_watchdog(self): |
| 85 | + from sonic_platform.watchdog import Watchdog |
| 86 | + self._watchdog = Watchdog() |
| 87 | + |
| 88 | + |
| 89 | + def __is_host(self): |
| 90 | + return os.system(HOST_CHK_CMD) == 0 |
| 91 | + |
| 92 | + def __read_txt_file(self, file_path): |
| 93 | + try: |
| 94 | + with open(file_path, 'r') as fd: |
| 95 | + data = fd.read() |
| 96 | + return data.strip() |
| 97 | + except IOError: |
| 98 | + pass |
| 99 | + return None |
| 100 | + |
| 101 | + def get_name(self): |
| 102 | + """ |
| 103 | + Retrieves the name of the device |
| 104 | + Returns: |
| 105 | + string: The name of the device |
| 106 | + """ |
| 107 | + |
| 108 | + return self._api_helper.hwsku |
| 109 | + |
| 110 | + def get_presence(self): |
| 111 | + """ |
| 112 | + Retrieves the presence of the Chassis |
| 113 | + Returns: |
| 114 | + bool: True if Chassis is present, False if not |
| 115 | + """ |
| 116 | + return True |
| 117 | + |
| 118 | + def get_status(self): |
| 119 | + """ |
| 120 | + Retrieves the operational status of the device |
| 121 | + Returns: |
| 122 | + A boolean value, True if device is operating properly, False if not |
| 123 | + """ |
| 124 | + return True |
| 125 | + |
| 126 | + def get_base_mac(self): |
| 127 | + """ |
| 128 | + Retrieves the base MAC address for the chassis |
| 129 | + Returns: |
| 130 | + A string containing the MAC address in the format |
| 131 | + 'XX:XX:XX:XX:XX:XX' |
| 132 | + """ |
| 133 | + return self._eeprom.get_mac() |
| 134 | + |
| 135 | + def get_serial_number(self): |
| 136 | + """ |
| 137 | + Retrieves the hardware serial number for the chassis |
| 138 | + Returns: |
| 139 | + A string containing the hardware serial number for this chassis. |
| 140 | + """ |
| 141 | + return self._eeprom.get_serial() |
| 142 | + |
| 143 | + def get_system_eeprom_info(self): |
| 144 | + """ |
| 145 | + Retrieves the full content of system EEPROM information for the chassis |
| 146 | + Returns: |
| 147 | + A dictionary where keys are the type code defined in |
| 148 | + OCP ONIE TlvInfo EEPROM format and values are their corresponding |
| 149 | + values. |
| 150 | + """ |
| 151 | + return self._eeprom.get_eeprom() |
| 152 | + |
| 153 | + def get_reboot_cause(self): |
| 154 | + """ |
| 155 | + Retrieves the cause of the previous reboot |
| 156 | +
|
| 157 | + Returns: |
| 158 | + A tuple (string, string) where the first element is a string |
| 159 | + containing the cause of the previous reboot. This string must be |
| 160 | + one of the predefined strings in this class. If the first string |
| 161 | + is "REBOOT_CAUSE_HARDWARE_OTHER", the second string can be used |
| 162 | + to pass a description of the reboot cause. |
| 163 | + """ |
| 164 | + |
| 165 | + reboot_cause_path = (HOST_REBOOT_CAUSE_PATH + REBOOT_CAUSE_FILE) |
| 166 | + sw_reboot_cause = self._api_helper.read_txt_file( |
| 167 | + reboot_cause_path) or "Unknown" |
| 168 | + |
| 169 | + |
| 170 | + return ('REBOOT_CAUSE_NON_HARDWARE', sw_reboot_cause) |
| 171 | + |
| 172 | + def get_sfp(self, index): |
| 173 | + """ |
| 174 | + Retrieves sfp represented by (1-based) index <index> |
| 175 | + Args: |
| 176 | + index: An integer, the index (1-based) of the sfp to retrieve. |
| 177 | + The index should be the sequence of a physical port in a chassis, |
| 178 | + starting from 1. |
| 179 | + For example, 1 for Ethernet0, 2 for Ethernet4 and so on. |
| 180 | + Returns: |
| 181 | + An object dervied from SfpBase representing the specified sfp |
| 182 | + """ |
| 183 | + sfp = None |
| 184 | + if not self.sfp_module_initialized: |
| 185 | + self.__initialize_sfp() |
| 186 | + |
| 187 | + try: |
| 188 | + # The index will start from 1 |
| 189 | + sfp = self._sfp_list[index-1] |
| 190 | + except IndexError: |
| 191 | + sys.stderr.write("SFP index {} out of range (1-{})\n".format( |
| 192 | + index, len(self._sfp_list))) |
| 193 | + return sfp |
0 commit comments