Skip to content

Commit

Permalink
Add set/get_lpmode APIs for sff8436 and sff8636 optics (sonic-net#353)
Browse files Browse the repository at this point in the history
* Add lpmode APIs for sff8436 and sff8636

* Update comments

* Simplify set_lpmode logic

* Update docstring

* Update docstring to lpmode on/off
  • Loading branch information
longhuan-cisco authored Mar 21, 2023
1 parent bbaa678 commit d3f9b2f
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
32 changes: 32 additions & 0 deletions sonic_platform_base/sonic_xcvr/api/public/sff8436.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,35 @@ def get_lpmode_support(self):

def get_power_override_support(self):
return not self.is_copper()

def set_lpmode(self, lpmode):
'''
This function sets LPMode for the module.
Args:
lpmode (bool): False means LPMode Off, True means LPMode On
Returns:
bool: True if the provision succeeds, False if it fails
'''
if not self.get_lpmode_support() or not self.get_power_override_support():
return False

return self.set_power_override(True, lpmode)

def get_lpmode(self):
'''
Retrieves low power mode status
Returns:
bool: True if module in low power else returns False.
'''
if not self.get_lpmode_support() or not self.get_power_override_support():
return False

power_set = self.get_power_set()
power_override = self.get_power_override()

# Since typically optics come up by default set to high power, in this case,
# power_override not being set, function will return high power mode.
return power_set and power_override
32 changes: 32 additions & 0 deletions sonic_platform_base/sonic_xcvr/api/public/sff8636.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,35 @@ def get_lpmode_support(self):

def get_power_override_support(self):
return not self.is_copper()

def set_lpmode(self, lpmode):
'''
This function sets LPMode for the module.
Args:
lpmode (bool): False means LPMode Off, True means LPMode On
Returns:
bool: True if the provision succeeds, False if it fails
'''
if not self.get_lpmode_support() or not self.get_power_override_support():
return False

return self.set_power_override(True, lpmode)

def get_lpmode(self):
'''
Retrieves low power mode status
Returns:
bool: True if module in low power else returns False.
'''
if not self.get_lpmode_support() or not self.get_power_override_support():
return False

power_set = self.get_power_set()
power_override = self.get_power_override()

# Since typically optics come up by default set to high power, in this case,
# power_override not being set, function will return high power mode.
return power_set and power_override
31 changes: 31 additions & 0 deletions tests/sonic_xcvr/test_sff8436.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def test_api(self):
self.api.get_transceiver_thresholds_support()
self.api.get_lpmode_support()
self.api.get_power_override_support()
self.api.set_lpmode(True)
self.api.get_lpmode()

@pytest.mark.parametrize("mock_response, expected", [
(bytearray([0x0]), "Power Class 1 Module (1.5W max. Power consumption)"),
Expand Down Expand Up @@ -104,3 +106,32 @@ def test_simulate_copper(self):
assert not self.api.get_temperature_support()
assert not self.api.get_voltage_support()

def test_get_lpmode(self):
self.api.get_lpmode_support = MagicMock()
self.api.get_lpmode_support.return_value = True
self.api.get_power_override_support = MagicMock()
self.api.get_power_override_support.return_value = True
self.api.get_power_set = MagicMock()
self.api.get_power_set.return_value = True
self.api.get_power_override = MagicMock()
self.api.get_power_override.return_value = True
assert self.api.get_lpmode()
self.api.get_power_set.return_value = False
self.api.get_power_override.return_value = True
assert not self.api.get_lpmode()
self.api.get_lpmode_support.return_value = False
self.api.get_power_override_support.return_value = False
assert not self.api.get_lpmode()

def test_set_lpmode(self):
self.api.get_lpmode_support = MagicMock()
self.api.get_lpmode_support.return_value = True
self.api.get_power_override_support = MagicMock()
self.api.get_power_override_support.return_value = True
self.api.set_power_override = MagicMock()
self.api.set_power_override.return_value = True
assert self.api.set_lpmode(True)
assert self.api.set_lpmode(False)
self.api.get_lpmode_support.return_value = False
self.api.get_power_override_support.return_value = False
assert not self.api.set_lpmode(True)
32 changes: 32 additions & 0 deletions tests/sonic_xcvr/test_sff8636.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def test_api(self):
self.api.get_transceiver_thresholds_support()
self.api.get_lpmode_support()
self.api.get_power_override_support()
self.api.set_lpmode(True)
self.api.get_lpmode()

@pytest.mark.parametrize("mock_response, expected", [
(bytearray([0x0]), "Power Class 1 Module (1.5W max.)"),
Expand Down Expand Up @@ -105,3 +107,33 @@ def test_simulate_copper(self):
assert not self.api.get_temperature_support()
assert not self.api.get_voltage_support()

def test_get_lpmode(self):
self.api.get_lpmode_support = MagicMock()
self.api.get_lpmode_support.return_value = True
self.api.get_power_override_support = MagicMock()
self.api.get_power_override_support.return_value = True
self.api.get_power_set = MagicMock()
self.api.get_power_set.return_value = True
self.api.get_power_override = MagicMock()
self.api.get_power_override.return_value = True
assert self.api.get_lpmode()
self.api.get_power_set.return_value = False
self.api.get_power_override.return_value = True
assert not self.api.get_lpmode()
self.api.get_lpmode_support.return_value = False
self.api.get_power_override_support.return_value = False
assert not self.api.get_lpmode()

def test_set_lpmode(self):
self.api.get_lpmode_support = MagicMock()
self.api.get_lpmode_support.return_value = True
self.api.get_power_override_support = MagicMock()
self.api.get_power_override_support.return_value = True
self.api.set_power_override = MagicMock()
self.api.set_power_override.return_value = True
assert self.api.set_lpmode(True)
assert self.api.set_lpmode(False)
self.api.get_lpmode_support.return_value = False
self.api.get_power_override_support.return_value = False
assert not self.api.set_lpmode(True)

0 comments on commit d3f9b2f

Please sign in to comment.