diff --git a/sonic_platform_base/__init__.py b/sonic_platform_base/__init__.py index 3d7f64b24..45ece3cfe 100644 --- a/sonic_platform_base/__init__.py +++ b/sonic_platform_base/__init__.py @@ -8,4 +8,5 @@ from . import psu_base from . import sfp_base from . import thermal_base +from . import sensor_base from . import watchdog_base diff --git a/sonic_platform_base/chassis_base.py b/sonic_platform_base/chassis_base.py index 877d9c849..6cd4403c6 100644 --- a/sonic_platform_base/chassis_base.py +++ b/sonic_platform_base/chassis_base.py @@ -54,6 +54,8 @@ def __init__(self): # List of ThermalBase-derived objects representing all thermals # available on the chassis self._thermal_list = [] + self._voltage_sensor_list = [] + self._current_sensor_list = [] # List of SfpBase-derived objects representing all sfps # available on the chassis @@ -451,6 +453,95 @@ def get_thermal_manager(self): """ raise NotImplementedError + ############################################## + # Voltage Sensor Methods + ############################################## + + def get_num_voltage_sensors(self): + """ + Retrieves the number of voltage sensors available on this chassis + + Returns: + An integer, the number of voltage sensors available on this chassis + """ + return len(self._voltage_sensor_list) + + def get_all_voltage_sensors(self): + """ + Retrieves all voltage sensors available on this chassis + + Returns: + A list of objects derived from VoltageSensorBase representing all voltage + sensors available on this chassis + """ + return self._voltage_sensor_list + + def get_voltage_sensor(self, index): + """ + Retrieves voltage sensor unit represented by (0-based) index + + Args: + index: An integer, the index (0-based) of the voltage sensor to + retrieve + + Returns: + An object derived from VoltageSensorBase representing the specified voltage sensor + """ + voltage_sensor = None + + try: + voltage_sensor = self._voltage_sensor_list[index] + except IndexError: + sys.stderr.write("Voltage sensor index {} out of range (0-{})\n".format( + index, len(self._voltage_sensor_list)-1)) + + return voltage_sensor + + ############################################## + # Current Sensor Methods + ############################################## + + def get_num_current_sensors(self): + """ + Retrieves the number of current sensors available on this chassis + + Returns: + An integer, the number of current sensors available on this chassis + """ + return len(self._current_sensor_list) + + def get_all_current_sensors(self): + """ + Retrieves all current sensors available on this chassis + + Returns: + A list of objects derived from CurrentSensorBase representing all current + sensors available on this chassis + """ + return self._current_sensor_list + + def get_current_sensor(self, index): + """ + Retrieves current sensor object represented by (0-based) index + + Args: + index: An integer, the index (0-based) of the current sensor to + retrieve + + Returns: + An object derived from CurrentSensorBase representing the specified current + sensor + """ + current_sensor = None + + try: + current_sensor = self._current_sensor_list[index] + except IndexError: + sys.stderr.write("Current sensor index {} out of range (0-{})\n".format( + index, len(self._current_sensor_list)-1)) + + return current_sensor + ############################################## # SFP methods ############################################## diff --git a/sonic_platform_base/module_base.py b/sonic_platform_base/module_base.py index 3e96f8b24..48e718184 100644 --- a/sonic_platform_base/module_base.py +++ b/sonic_platform_base/module_base.py @@ -66,6 +66,8 @@ def __init__(self): # List of ThermalBase-derived objects representing all thermals # available on the module self._thermal_list = [] + self._voltage_sensor_list = [] + self._current_sensor_list = [] # List of SfpBase-derived objects representing all sfps # available on the module @@ -372,6 +374,95 @@ def get_thermal(self, index): return thermal + ############################################## + # Voltage Sensor methods + ############################################## + + def get_num_voltage_sensors(self): + """ + Retrieves the number of voltage sensors available on this module + + Returns: + An integer, the number of voltage sensors available on this module + """ + return len(self._voltage_sensor_list) + + def get_all_voltage_sensors(self): + """ + Retrieves all voltage sensors available on this module + + Returns: + A list of objects derived from VoltageSensorBase representing all voltage + sensors available on this module + """ + return self._voltage_sensor_list + + def get_voltage_sensor(self, index): + """ + Retrieves voltage sensor unit represented by (0-based) index + + Args: + index: An integer, the index (0-based) of the voltage sensor to + retrieve + + Returns: + An object derived from VoltageSensorBase representing the specified voltage + sensor + """ + voltage_sensor = None + + try: + voltage_sensor = self._voltage_sensor_list[index] + except IndexError: + sys.stderr.write("Voltage sensor index {} out of range (0-{})\n".format( + index, len(self._voltage_sensor_list)-1)) + + return voltage_sensor + + ############################################## + # Current sensor methods + ############################################## + + def get_num_CurrentSensors(self): + """ + Retrieves the number of current sensors available on this module + + Returns: + An integer, the number of current sensors available on this module + """ + return len(self._current_sensor_list) + + def get_all_current_sensors(self): + """ + Retrieves all current sensors available on this module + + Returns: + A list of objects derived from CurrentSensorBase representing all current + sensors available on this module + """ + return self._current_sensor_list + + def get_current_sensor(self, index): + """ + Retrieves current sensor object represented by (0-based) index + + Args: + index: An integer, the index (0-based) of the current sensor to + retrieve + + Returns: + An object derived from CurrentSensorBase representing the specified current_sensor + """ + current_sensor = None + + try: + current_sensor = self._current_sensor_list[index] + except IndexError: + sys.stderr.write("Current sensor index {} out of range (0-{})\n".format( + index, len(self._current_sensor_list)-1)) + + return current_sensor + ############################################## # SFP methods ############################################## diff --git a/sonic_platform_base/sensor_base.py b/sonic_platform_base/sensor_base.py new file mode 100644 index 000000000..742a20f43 --- /dev/null +++ b/sonic_platform_base/sensor_base.py @@ -0,0 +1,172 @@ +""" + sensor_base.py + + Abstract base class for implementing a platform-specific class with which + to interact with a sensor module in SONiC +""" + +from . import device_base + +SENSOR_TYPE_VOLTAGE = 1 +SENSOR_TYPE_CURRENT = 2 + +class SensorBase(device_base.DeviceBase): + """ + Abstract base class for interfacing with a sensor module + """ + + def get_type(self): + """ + Specifies the type of the sensor. + + Returns: + Sensor type + """ + raise NotImplementedError + + def get_value(self): + """ + Retrieves measurement reported by sensor + + Returns: + Sensor measurement + """ + raise NotImplementedError + + def get_unit(self): + """ + Retrieves unit of measurement reported by sensor + + Returns: + Sensor measurement unit + """ + raise NotImplementedError + + def get_high_threshold(self): + """ + Retrieves the high threshold of sensor + + Returns: + High threshold + """ + raise NotImplementedError + + def get_low_threshold(self): + """ + Retrieves the low threshold + + Returns: + Low threshold + """ + raise NotImplementedError + + def set_high_threshold(self, value): + """ + Sets the high threshold value of sensor + + Args: + value: High threshold value to set + + Returns: + A boolean, True if threshold is set successfully, False if not + """ + raise NotImplementedError + + def set_low_threshold(self, value): + """ + Sets the low threshold value of sensor + + Args: + value: Value + + Returns: + A boolean, True if threshold is set successfully, False if not + """ + raise NotImplementedError + + def get_high_critical_threshold(self): + """ + Retrieves the high critical threshold value of sensor + + Returns: + The high critical threshold value of sensor + """ + raise NotImplementedError + + def get_low_critical_threshold(self): + """ + Retrieves the low critical threshold value of sensor + + Returns: + The low critical threshold value of sensor + """ + raise NotImplementedError + + def set_high_critical_threshold(self, value): + """ + Sets the critical high threshold value of sensor + + Args: + value: Critical high threshold Value + + Returns: + A boolean, True if threshold is set successfully, False if not + """ + raise NotImplementedError + + def set_low_critical_threshold(self, value): + """ + Sets the critical low threshold value of sensor + + Args: + value: Critial low threshold Value + + Returns: + A boolean, True if threshold is set successfully, False if not + """ + raise NotImplementedError + + def get_minimum_recorded(self): + """ + Retrieves the minimum recorded value of sensor + + Returns: + The minimum recorded value of sensor + """ + raise NotImplementedError + + def get_maximum_recorded(self): + """ + Retrieves the maximum recorded value of sensor + + Returns: + The maximum recorded value of sensor + """ + raise NotImplementedError + + + +class VoltageSensorBase(SensorBase): + """ + Abstract base class for interfacing with a voltage sensor module + """ + MILLI_VOLTS = "mV" + + def get_type(self): + return SENSOR_TYPE_VOLTAGE + + def get_unit(self): + return MILLI_VOLTS + + +class CurrentSensorBase(SensorBase): + """ + Abstract base class for interfacing with a current sensor module + """ + MILLI_AMPS = "mA" + + def get_type(self): + return SENSOR_TYPE_CURRENT + + def get_unit(self): + return MILLI_AMPS