Skip to content

Commit bd6b6ed

Browse files
committed
Remove inheritence from 'abc' class, all abstract methods now raise NotImplementedError
- We plan to implement a build-time or run-time check for all methods lacking platform-specific implemention and will output a list of them.
1 parent d169853 commit bd6b6ed

File tree

6 files changed

+35
-89
lines changed

6 files changed

+35
-89
lines changed

sonic_platform_base/chassis_base.py

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
#
22
# chassis_base.py
33
#
4-
# Abstract base class for implementing a platform-specific class with which
4+
# Base class for implementing a platform-specific class with which
55
# to interact with a chassis device in SONiC.
66
#
77

8-
try:
9-
import abc
10-
import sys
11-
from . import device_base
12-
except ImportError as e:
13-
raise ImportError(str(e) + " - required module not found")
8+
import sys
9+
from . import device_base
1410

1511

16-
# NOTE: This class inherits the metaclass 'abc.ABCMeta' from DeviceBase
1712
class ChassisBase(device_base.DeviceBase):
1813
"""
19-
Abstract base class for interfacing with a platform chassis
14+
Base class for interfacing with a platform chassis
2015
"""
2116

2217
# Possible reboot causes
@@ -38,7 +33,6 @@ class ChassisBase(device_base.DeviceBase):
3833
# Object derived from WatchdogBase for interacting with hardware watchdog
3934
_watchdog = None
4035

41-
@abc.abstractmethod
4236
def get_base_mac(self):
4337
"""
4438
Retrieves the base MAC address for the chassis
@@ -47,9 +41,8 @@ def get_base_mac(self):
4741
A string containing the MAC address in the format
4842
'XX:XX:XX:XX:XX:XX'
4943
"""
50-
return None
44+
raise NotImplementedError
5145

52-
@abc.abstractmethod
5346
def get_reboot_cause(self):
5447
"""
5548
Retrieves the cause of the previous reboot
@@ -58,7 +51,7 @@ def get_reboot_cause(self):
5851
A string containing the cause of the previous reboot. This string
5952
must be one of the predefined strings in this class.
6053
"""
61-
return REBOOT_CAUSE_SOFTWARE
54+
raise NotImplementedError
6255

6356
##############################################
6457
# Fan module methods

sonic_platform_base/device_base.py

+5-20
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,48 @@
55
# peripheral device in SONiC
66
#
77

8-
try:
9-
import abc
10-
import six
11-
except ImportError as e:
12-
raise ImportError(str(e) + " - required module not found")
13-
14-
15-
# NOTE: Using 'six' module here to ensure consistent abc metaclass behavior
16-
# with both Python 2.x and Python 3.x
17-
@six.add_metaclass(abc.ABCMeta)
188
class DeviceBase(object):
199
"""
2010
Abstract base class for interfacing with a generic type of platform
2111
peripheral device
2212
"""
2313

24-
@abc.abstractmethod
2514
def get_presence(self):
2615
"""
2716
Retrieves the presence of the device
2817
2918
Returns:
3019
bool: True if device is present, False if not
3120
"""
32-
return False
21+
raise NotImplementedError
3322

34-
@abc.abstractmethod
3523
def get_model(self):
3624
"""
3725
Retrieves the model number (or part number) of the device
3826
3927
Returns:
4028
string: Model/part number of device
4129
"""
42-
return None
30+
raise NotImplementedError
4331

44-
@abc.abstractmethod
4532
def get_serial(self):
4633
"""
4734
Retrieves the serial number of the device
4835
4936
Returns:
5037
string: Serial number of device
5138
"""
52-
return None
39+
raise NotImplementedError
5340

54-
@abc.abstractmethod
5541
def get_status(self):
5642
"""
5743
Retrieves the operational status of the device
5844
5945
Returns:
6046
A boolean value, True if device is operating properly, False if not
6147
"""
62-
return False
48+
raise NotImplementedError
6349

64-
@abc.abstractmethod
6550
def get_change_event(self, timeout=0):
6651
"""
6752
Returns a dictionary containing all devices which have experienced a
@@ -78,4 +63,4 @@ def get_change_event(self, timeout=0):
7863
status='1' represents device inserted,
7964
status='0' represents device removed. Ex. {'0': '1', '1': '0'}
8065
"""
81-
return (False, None)
66+
raise NotImplementedError

sonic_platform_base/fan_base.py

+8-19
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@
55
# to interact with a fan module in SONiC
66
#
77

8-
try:
9-
import abc
10-
from . import device_base
11-
except ImportError as e:
12-
raise ImportError(str(e) + " - required module not found")
8+
from . import device_base
139

1410

15-
# NOTE: This class inherits the metaclass 'abc.ABCMeta' from DeviceBase
1611
class FanBase(device_base.DeviceBase):
1712
"""
1813
Abstract base class for interfacing with a fan module
1914
"""
2015

21-
# Possible fan directions
16+
# Possible fan directions (relative to port-side of device)
2217
FAN_DIRECTION_INTAKE = "intake"
2318
FAN_DIRECTION_EXHAUST = "exhaust"
2419

@@ -27,7 +22,6 @@ class FanBase(device_base.DeviceBase):
2722
STATUS_LED_COLOR_RED = "red"
2823
STATUS_LED_COLOR_OFF = "off"
2924

30-
@abc.abstractmethod
3125
def get_direction(self):
3226
"""
3327
Retrieves the direction of fan
@@ -36,9 +30,8 @@ def get_direction(self):
3630
A string, either FAN_DIRECTION_INTAKE or FAN_DIRECTION_EXHAUST
3731
depending on fan direction
3832
"""
39-
return None
33+
raise NotImplementedError
4034

41-
@abc.abstractmethod
4235
def get_speed(self):
4336
"""
4437
Retrieves the speed of fan as a percentage of full speed
@@ -47,9 +40,8 @@ def get_speed(self):
4740
An integer, the percentage of full fan speed, in the range 0 (off)
4841
to 100 (full speed)
4942
"""
50-
return 0
43+
raise NotImplementedError
5144

52-
@abc.abstractmethod
5345
def get_target_speed(self):
5446
"""
5547
Retrieves the target (expected) speed of the fan
@@ -58,9 +50,8 @@ def get_target_speed(self):
5850
An integer, the percentage of full fan speed, in the range 0 (off)
5951
to 100 (full speed)
6052
"""
61-
return 0
53+
raise NotImplementedError
6254

63-
@abc.abstractmethod
6455
def get_speed_tolerance(self):
6556
"""
6657
Retrieves the speed tolerance of the fan
@@ -69,9 +60,8 @@ def get_speed_tolerance(self):
6960
An integer, the percentage of variance from target speed which is
7061
considered tolerable
7162
"""
72-
return 0
63+
raise NotImplementedError
7364

74-
@abc.abstractmethod
7565
def set_speed(self, speed):
7666
"""
7767
Sets the fan speed
@@ -83,9 +73,8 @@ def set_speed(self, speed):
8373
Returns:
8474
A boolean, True if speed is set successfully, False if not
8575
"""
86-
return False
76+
raise NotImplementedError
8777

88-
@abc.abstractmethod
8978
def set_status_led(self, color):
9079
"""
9180
Sets the state of the fan module status LED
@@ -97,4 +86,4 @@ def set_status_led(self, color):
9786
Returns:
9887
bool: True if status LED state is set successfully, False if not
9988
"""
100-
return False
89+
raise NotImplementedError

sonic_platform_base/platform_base.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
#
22
# platform_base.py
33
#
4-
# Abstract base class for implementing platform-specific functionality in
5-
# SONiC. This is the root class of sonic_platform_base hierarchy. A platform
4+
# Base class for implementing platform-specific functionality in SONiC.
5+
# This is the root class of sonic_platform_base hierarchy. A platform
66
# may contain one or more chassis. Classes derived from this class provide
77
# the ability to interact with all available chassis on the platform.
88
#
99

1010
from __future__ import print_function
1111

12-
try:
13-
import sys
14-
except ImportError as e:
15-
raise ImportError(str(e) + " - required module not found")
12+
import sys
1613

1714

1815
class PlatformBase(object):
@@ -57,6 +54,6 @@ def get_chassis(self, index):
5754
chassis = self.chassis_list[index]
5855
except IndexError:
5956
sys.stderr.write("Chassis index {} out of range (0-{})\n".format(
60-
index, len(self.chassis_list)-1))
57+
index, len(self.chassis_list)-1))
6158

6259
return chassis

sonic_platform_base/psu_base.py

+8-18
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
# to interact with a power supply unit (PSU) in SONiC
66
#
77

8-
try:
9-
import abc
10-
from . import device_base
11-
except ImportError as e:
12-
raise ImportError(str(e) + " - required module not found")
8+
from . import device_base
9+
1310

14-
# NOTE: This class inherits the metaclass 'abc.ABCMeta' from DeviceBase
1511
class PsuBase(device_base.DeviceBase):
1612
"""
1713
Abstract base class for interfacing with a power supply unit
@@ -26,17 +22,15 @@ class PsuBase(device_base.DeviceBase):
2622
STATUS_LED_COLOR_RED = "red"
2723
STATUS_LED_COLOR_OFF = "off"
2824

29-
@abc.abstractmethod
3025
def get_fan_direction(self):
3126
"""
3227
Retrieves the direction of PSU fan
3328
3429
Returns:
3530
A string, either 'intake' or 'exhaust', depending on fan direction
3631
"""
37-
return None
32+
raise NotImplementedError
3833

39-
@abc.abstractmethod
4034
def get_fan_speed(self):
4135
"""
4236
Retrieves the speed of PSU fan as a percentage of full speed
@@ -45,9 +39,8 @@ def get_fan_speed(self):
4539
An integer, the percentage of full fan speed, in the range 0 (off)
4640
to 100 (full speed)
4741
"""
48-
return 0
42+
raise NotImplementedError
4943

50-
@abc.abstractmethod
5144
def get_fan_target_speed(self):
5245
"""
5346
Retrieves the target (expected) speed of the PSU fan
@@ -56,9 +49,8 @@ def get_fan_target_speed(self):
5649
An integer, the percentage of full fan speed, in the range 0 (off)
5750
to 100 (full speed)
5851
"""
59-
return 0
52+
raise NotImplementedError
6053

61-
@abc.abstractmethod
6254
def get_fan_speed_tolerance(self):
6355
"""
6456
Retrieves the speed tolerance of the PSU fan
@@ -67,9 +59,8 @@ def get_fan_speed_tolerance(self):
6759
An integer, the percentage of variance from target speed which is
6860
considered tolerable
6961
"""
70-
return 0
62+
raise NotImplementedError
7163

72-
@abc.abstractmethod
7364
def set_fan_speed(self, speed):
7465
"""
7566
Sets the fan speed
@@ -81,9 +72,8 @@ def set_fan_speed(self, speed):
8172
Returns:
8273
A boolean, True if fan speed is set successfully, False if not
8374
"""
84-
return False
75+
raise NotImplementedError
8576

86-
@abc.abstractmethod
8777
def set_status_led(self, color):
8878
"""
8979
Sets the state of the PSU status LED
@@ -95,4 +85,4 @@ def set_status_led(self, color):
9585
Returns:
9686
bool: True if status LED state is set successfully, False if not
9787
"""
98-
return False
88+
raise NotImplementedError

sonic_platform_base/watchdog_base.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
# to interact with a hardware watchdog module in SONiC
66
#
77

8-
try:
9-
import abc
10-
from . import device_base
11-
except ImportError as e:
12-
raise ImportError(str(e) + " - required module not found")
8+
from . import device_base
139

1410

15-
# NOTE: This class inherits the metaclass 'abc.ABCMeta' from DeviceBase
1611
class WatchdogBase(device_base.DeviceBase):
1712
"""
1813
Abstract base class for interfacing with a hardware watchdog module
@@ -22,27 +17,24 @@ class WatchdogBase(device_base.DeviceBase):
2217
FAN_DIRECTION_INTAKE = "intake"
2318
FAN_DIRECTION_EXHAUST = "exhaust"
2419

25-
@abc.abstractmethod
2620
def arm(self, seconds):
2721
"""
2822
Arm the hardware watchdog with a timeout of <seconds> seconds
2923
3024
Returns:
3125
A boolean, True if watchdog is armed successfully, False if not
3226
"""
33-
return None
27+
raise NotImplementedError
3428

35-
@abc.abstractmethod
3629
def disarm(self):
3730
"""
3831
Disarm the hardware watchdog
3932
4033
Returns:
4134
A boolean, True if watchdog is disarmed successfully, False if not
4235
"""
43-
return 0
36+
raise NotImplementedError
4437

45-
@abc.abstractmethod
4638
def is_armed(self):
4739
"""
4840
Retrieves the armed state of the hardware watchdog and if armed, the
@@ -55,4 +47,4 @@ def is_armed(self):
5547
is disarmed, the first value will be 'false' and the integer
5648
returned as the second value should be ignored.
5749
"""
58-
return (false, 0)
50+
raise NotImplementedError

0 commit comments

Comments
 (0)