Skip to content

Commit

Permalink
power_status_handler handles missing PSU and all exceptions
Browse files Browse the repository at this point in the history
Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>
  • Loading branch information
qiluo-msft committed May 22, 2018
1 parent f283152 commit bf1b165
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
42 changes: 36 additions & 6 deletions src/sonic_ax_impl/mibs/vendor/cisco/ciscoEntityFruControlMIB.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ def _getPsuIndex(self, sub_id):

psu_index = int(sub_id[0])

if psu_index < 1 or psu_index > self.psuutil.get_num_psus():
try:
num_psus = self.psuutil.get_num_psus()
except Exception:
# Any unexpected exception or error, log it and keep running
logger.exception("PowerStatusHandler._getPsuIndex() caught an unexpected exception during get_num_psus()")
return None

if psu_index < 1 or psu_index > num_psus:
return None

return psu_index
Expand All @@ -62,8 +69,15 @@ def get_next(self, sub_id):
return (1,)

psu_index = self._getPsuIndex(sub_id)
try:
num_psus = self.psuutil.get_num_psus()
except Exception:
# Any unexpected exception or error, log it and keep running
logger.exception("PowerStatusHandler.get_next() caught an unexpected exception during get_num_psus()")
return None

if psu_index and psu_index + 1 <= self.psuutil.get_num_psus():

if psu_index and psu_index + 1 <= num_psus:
return (psu_index + 1,)

return None
Expand All @@ -74,19 +88,35 @@ def getPsuStatus(self, sub_id):
:return: the status of requested PSU according to cefcModuleOperStatus ModuleOperType
2 - PSU has correct functionalling - ok
7 - PSU has a problem with functionalling - failed
8 - the module is provisioned, but it is missing. This is a failure state.
:ref: https://www.cisco.com/c/en/us/td/docs/switches/wan/mgx/mgx_8850/software/mgx_r2-0-10/pxm/reference/guide/pxm/cscoent.html
"""
psu_index = self._getPsuIndex(sub_id)

if not psu_index:
return None

psu_status = self.psuutil.get_psu_status(psu_index)
try:
psu_presence = self.psuutil.get_psu_presence(psu_index)
except Exception:
# Any unexpected exception or error, log it and keep running
logger.exception("PowerStatusHandler.getPsuStatus() caught an unexpected exception during get_psu_presence()")
return None

if psu_presence:
try:
psu_status = self.psuutil.get_psu_status(psu_index)
except Exception:
# Any unexpected exception or error, log it and keep running
logger.exception("PowerStatusHandler.getPsuStatus() caught an unexpected exception during get_psu_status()")
return None

if psu_status:
return 2
if psu_status:
return 2

return 7
return 7
else:
return 8


class cefcFruPowerStatusTable(metaclass=MIBMeta, prefix='.1.3.6.1.4.1.9.9.117.1.1.2'):
Expand Down
12 changes: 8 additions & 4 deletions tests/plugins/psuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class PsuUtil():

def __init__(self):
""" For testing purpose only """
self.num_of_psus = 2
self.psu_status = { 1 : True, 2 : False }
self.num_of_psus = 3
self.psu_status = { 1: False, 2: True, 3: False }
self.psu_presence = { 1: False, 2: True, 3: True }

def get_num_psus(self):
"""
Expand All @@ -37,7 +38,10 @@ def get_psu_status(self, index):
if not isinstance(index, int):
return False
elif index > 0 and index <= self.num_of_psus:
return self.psu_status[index]
if self.psu_presence[index]:
return self.psu_status[index]
else:
raise ValueError(index)
else:
return False

Expand All @@ -53,6 +57,6 @@ def get_psu_presence(self, index):
if not isinstance(index, int):
return False
elif index > 0 and index <= self.num_of_psus:
return self.psu_status[index]
return self.psu_presence[index]
else:
return False
26 changes: 21 additions & 5 deletions tests/test_psu.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ class TestPsuStatus(TestCase):
def setUpClass(cls):
cls.lut = MIBTable(ciscoEntityFruControlMIB.cefcFruPowerStatusTable)

def test_getNextPsu0(self):
oid = ObjectIdentifier(2, 0, 0, 0, (1, 3, 6, 1, 4, 1, 9, 9, 117, 1, 1, 2, 1, 2))
expected_oid = ObjectIdentifier(2, 0, 0, 0, (1, 3, 6, 1, 4, 1, 9, 9, 117, 1, 1, 2, 1, 2, 1))
get_pdu = GetNextPDU(
header=PDUHeader(1, PduTypes.GET_NEXT, 16, 0, 42, 0, 0, 0),
oids=[oid]
)

encoded = get_pdu.encode()
response = get_pdu.make_response(self.lut)

value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.INTEGER)
self.assertEqual(str(value0.name), str(expected_oid))
self.assertEqual(value0.data, 8)

def test_getPsu1Status(self):
oid = ObjectIdentifier(2, 0, 0, 0, (1, 3, 6, 1, 4, 1, 9, 9, 117, 1, 1, 2, 1, 2, 1))
get_pdu = GetPDU(
Expand All @@ -35,7 +51,7 @@ def test_getPsu1Status(self):
value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.INTEGER)
self.assertEqual(str(value0.name), str(oid))
self.assertEqual(value0.data, 2)
self.assertEqual(value0.data, 8)

def test_getNextPsu1(self):
oid = ObjectIdentifier(2, 0, 0, 0, (1, 3, 6, 1, 4, 1, 9, 9, 117, 1, 1, 2, 1, 2, 1))
Expand All @@ -51,7 +67,7 @@ def test_getNextPsu1(self):
value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.INTEGER)
self.assertEqual(str(value0.name), str(expected_oid))
self.assertEqual(value0.data, 7)
self.assertEqual(value0.data, 2)

def test_getPsu2Status(self):
oid = ObjectIdentifier(2, 0, 0, 0, (1, 3, 6, 1, 4, 1, 9, 9, 117, 1, 1, 2, 1, 2, 2))
Expand All @@ -66,10 +82,10 @@ def test_getPsu2Status(self):
value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.INTEGER)
self.assertEqual(str(value0.name), str(oid))
self.assertEqual(value0.data, 7)
self.assertEqual(value0.data, 2)

def test_getNextPsu2(self):
oid = ObjectIdentifier(2, 0, 0, 0, (1, 3, 6, 1, 4, 1, 9, 9, 117, 1, 1, 2, 1, 2, 2))
def test_getNextPsu3(self):
oid = ObjectIdentifier(2, 0, 0, 0, (1, 3, 6, 1, 4, 1, 9, 9, 117, 1, 1, 2, 1, 2, 3))
expected_oid = None
get_pdu = GetNextPDU(
header=PDUHeader(1, PduTypes.GET_NEXT, 16, 0, 42, 0, 0, 0),
Expand Down

0 comments on commit bf1b165

Please sign in to comment.