Skip to content

Commit 086a88c

Browse files
authored
Sensor Monitor support (sonic-net#393)
New base class is introduced for defining voltage and current sensors. Chassis and Module base classes are enhanced with APIs to retrieve voltage and current sensor data from platform.
1 parent 1988b37 commit 086a88c

7 files changed

+442
-1
lines changed

sonic_platform_base/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
from . import psu_base
99
from . import sfp_base
1010
from . import thermal_base
11+
from . import sensor_base
1112
from . import watchdog_base

sonic_platform_base/chassis_base.py

+91
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def __init__(self):
5454
# List of ThermalBase-derived objects representing all thermals
5555
# available on the chassis
5656
self._thermal_list = []
57+
self._voltage_sensor_list = []
58+
self._current_sensor_list = []
5759

5860
# List of SfpBase-derived objects representing all sfps
5961
# available on the chassis
@@ -451,6 +453,95 @@ def get_thermal_manager(self):
451453
"""
452454
raise NotImplementedError
453455

456+
##############################################
457+
# Voltage Sensor Methods
458+
##############################################
459+
460+
def get_num_voltage_sensors(self):
461+
"""
462+
Retrieves the number of voltage sensors available on this chassis
463+
464+
Returns:
465+
An integer, the number of voltage sensors available on this chassis
466+
"""
467+
return len(self._voltage_sensor_list)
468+
469+
def get_all_voltage_sensors(self):
470+
"""
471+
Retrieves all voltage sensors available on this chassis
472+
473+
Returns:
474+
A list of objects derived from VoltageSensorBase representing all voltage
475+
sensors available on this chassis
476+
"""
477+
return self._voltage_sensor_list
478+
479+
def get_voltage_sensor(self, index):
480+
"""
481+
Retrieves voltage sensor unit represented by (0-based) index <index>
482+
483+
Args:
484+
index: An integer, the index (0-based) of the voltage sensor to
485+
retrieve
486+
487+
Returns:
488+
An object derived from VoltageSensorBase representing the specified voltage sensor
489+
"""
490+
voltage_sensor = None
491+
492+
try:
493+
voltage_sensor = self._voltage_sensor_list[index]
494+
except IndexError:
495+
sys.stderr.write("Voltage sensor index {} out of range (0-{})\n".format(
496+
index, len(self._voltage_sensor_list)-1))
497+
498+
return voltage_sensor
499+
500+
##############################################
501+
# Current Sensor Methods
502+
##############################################
503+
504+
def get_num_current_sensors(self):
505+
"""
506+
Retrieves the number of current sensors available on this chassis
507+
508+
Returns:
509+
An integer, the number of current sensors available on this chassis
510+
"""
511+
return len(self._current_sensor_list)
512+
513+
def get_all_current_sensors(self):
514+
"""
515+
Retrieves all current sensors available on this chassis
516+
517+
Returns:
518+
A list of objects derived from CurrentSensorBase representing all current
519+
sensors available on this chassis
520+
"""
521+
return self._current_sensor_list
522+
523+
def get_current_sensor(self, index):
524+
"""
525+
Retrieves current sensor object represented by (0-based) index <index>
526+
527+
Args:
528+
index: An integer, the index (0-based) of the current sensor to
529+
retrieve
530+
531+
Returns:
532+
An object derived from CurrentSensorBase representing the specified current
533+
sensor
534+
"""
535+
current_sensor = None
536+
537+
try:
538+
current_sensor = self._current_sensor_list[index]
539+
except IndexError:
540+
sys.stderr.write("Current sensor index {} out of range (0-{})\n".format(
541+
index, len(self._current_sensor_list)-1))
542+
543+
return current_sensor
544+
454545
##############################################
455546
# SFP methods
456547
##############################################

sonic_platform_base/module_base.py

+91
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ def __init__(self):
6666
# List of ThermalBase-derived objects representing all thermals
6767
# available on the module
6868
self._thermal_list = []
69+
self._voltage_sensor_list = []
70+
self._current_sensor_list = []
6971

7072
# List of SfpBase-derived objects representing all sfps
7173
# available on the module
@@ -372,6 +374,95 @@ def get_thermal(self, index):
372374

373375
return thermal
374376

377+
##############################################
378+
# Voltage Sensor methods
379+
##############################################
380+
381+
def get_num_voltage_sensors(self):
382+
"""
383+
Retrieves the number of voltage sensors available on this module
384+
385+
Returns:
386+
An integer, the number of voltage sensors available on this module
387+
"""
388+
return len(self._voltage_sensor_list)
389+
390+
def get_all_voltage_sensors(self):
391+
"""
392+
Retrieves all voltage sensors available on this module
393+
394+
Returns:
395+
A list of objects derived from VoltageSensorBase representing all voltage
396+
sensors available on this module
397+
"""
398+
return self._voltage_sensor_list
399+
400+
def get_voltage_sensor(self, index):
401+
"""
402+
Retrieves voltage sensor unit represented by (0-based) index <index>
403+
404+
Args:
405+
index: An integer, the index (0-based) of the voltage sensor to
406+
retrieve
407+
408+
Returns:
409+
An object derived from VoltageSensorBase representing the specified voltage
410+
sensor
411+
"""
412+
voltage_sensor = None
413+
414+
try:
415+
voltage_sensor = self._voltage_sensor_list[index]
416+
except IndexError:
417+
sys.stderr.write("Voltage sensor index {} out of range (0-{})\n".format(
418+
index, len(self._voltage_sensor_list)-1))
419+
420+
return voltage_sensor
421+
422+
##############################################
423+
# Current sensor methods
424+
##############################################
425+
426+
def get_num_current_sensors(self):
427+
"""
428+
Retrieves the number of current sensors available on this module
429+
430+
Returns:
431+
An integer, the number of current sensors available on this module
432+
"""
433+
return len(self._current_sensor_list)
434+
435+
def get_all_current_sensors(self):
436+
"""
437+
Retrieves all current sensors available on this module
438+
439+
Returns:
440+
A list of objects derived from CurrentSensorBase representing all current
441+
sensors available on this module
442+
"""
443+
return self._current_sensor_list
444+
445+
def get_current_sensor(self, index):
446+
"""
447+
Retrieves current sensor object represented by (0-based) index <index>
448+
449+
Args:
450+
index: An integer, the index (0-based) of the current sensor to
451+
retrieve
452+
453+
Returns:
454+
An object derived from CurrentSensorBase representing the specified current_sensor
455+
"""
456+
current_sensor = None
457+
458+
try:
459+
current_sensor = self._current_sensor_list[index]
460+
except IndexError:
461+
sys.stderr.write("Current sensor index {} out of range (0-{})\n".format(
462+
index, len(self._current_sensor_list)-1))
463+
464+
return current_sensor
465+
375466
##############################################
376467
# SFP methods
377468
##############################################

sonic_platform_base/sensor_base.py

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
"""
2+
sensor_base.py
3+
4+
Abstract base class for implementing a platform-specific class with which
5+
to interact with a sensor module in SONiC
6+
"""
7+
8+
from . import device_base
9+
10+
class SensorBase(device_base.DeviceBase):
11+
"""
12+
Abstract base class for interfacing with a sensor module
13+
"""
14+
15+
@classmethod
16+
def get_type(cls):
17+
"""
18+
Specifies the type of the sensor such as current/voltage etc.
19+
20+
Returns:
21+
Sensor type
22+
"""
23+
raise NotImplementedError
24+
25+
def get_value(self):
26+
"""
27+
Retrieves measurement reported by sensor
28+
29+
Returns:
30+
Sensor measurement
31+
"""
32+
raise NotImplementedError
33+
34+
@classmethod
35+
def get_unit(cls):
36+
"""
37+
Retrieves unit of measurement reported by sensor
38+
39+
Returns:
40+
Sensor measurement unit
41+
"""
42+
raise NotImplementedError
43+
44+
def get_high_threshold(self):
45+
"""
46+
Retrieves the high threshold of sensor
47+
48+
Returns:
49+
High threshold
50+
"""
51+
raise NotImplementedError
52+
53+
def get_low_threshold(self):
54+
"""
55+
Retrieves the low threshold
56+
57+
Returns:
58+
Low threshold
59+
"""
60+
raise NotImplementedError
61+
62+
def set_high_threshold(self, value):
63+
"""
64+
Sets the high threshold value of sensor
65+
66+
Args:
67+
value: High threshold value to set
68+
69+
Returns:
70+
A boolean, True if threshold is set successfully, False if not
71+
"""
72+
raise NotImplementedError
73+
74+
def set_low_threshold(self, value):
75+
"""
76+
Sets the low threshold value of sensor
77+
78+
Args:
79+
value: Value
80+
81+
Returns:
82+
A boolean, True if threshold is set successfully, False if not
83+
"""
84+
raise NotImplementedError
85+
86+
def get_high_critical_threshold(self):
87+
"""
88+
Retrieves the high critical threshold value of sensor
89+
90+
Returns:
91+
The high critical threshold value of sensor
92+
"""
93+
raise NotImplementedError
94+
95+
def get_low_critical_threshold(self):
96+
"""
97+
Retrieves the low critical threshold value of sensor
98+
99+
Returns:
100+
The low critical threshold value of sensor
101+
"""
102+
raise NotImplementedError
103+
104+
def set_high_critical_threshold(self, value):
105+
"""
106+
Sets the critical high threshold value of sensor
107+
108+
Args:
109+
value: Critical high threshold Value
110+
111+
Returns:
112+
A boolean, True if threshold is set successfully, False if not
113+
"""
114+
raise NotImplementedError
115+
116+
def set_low_critical_threshold(self, value):
117+
"""
118+
Sets the critical low threshold value of sensor
119+
120+
Args:
121+
value: Critial low threshold Value
122+
123+
Returns:
124+
A boolean, True if threshold is set successfully, False if not
125+
"""
126+
raise NotImplementedError
127+
128+
def get_minimum_recorded(self):
129+
"""
130+
Retrieves the minimum recorded value of sensor
131+
132+
Returns:
133+
The minimum recorded value of sensor
134+
"""
135+
raise NotImplementedError
136+
137+
def get_maximum_recorded(self):
138+
"""
139+
Retrieves the maximum recorded value of sensor
140+
141+
Returns:
142+
The maximum recorded value of sensor
143+
"""
144+
raise NotImplementedError
145+
146+
147+
148+
class VoltageSensorBase(SensorBase):
149+
"""
150+
Abstract base class for interfacing with a voltage sensor module
151+
"""
152+
153+
@classmethod
154+
def get_type(cls):
155+
return "SENSOR_TYPE_VOLTAGE"
156+
157+
@classmethod
158+
def get_unit(cls):
159+
return "mV"
160+
161+
162+
class CurrentSensorBase(SensorBase):
163+
"""
164+
Abstract base class for interfacing with a current sensor module
165+
"""
166+
167+
@classmethod
168+
def get_type(cls):
169+
return "SENSOR_TYPE_CURRENT"
170+
171+
@classmethod
172+
def get_unit(cls):
173+
return "mA"

0 commit comments

Comments
 (0)