From b409e1db108bbc78ea69f9002b954bdb956b10e2 Mon Sep 17 00:00:00 2001 From: keboliu Date: Wed, 5 May 2021 21:17:10 +0800 Subject: [PATCH] fix review comments --- src/sonic_ax_impl/mibs/ietf/rfc3433.py | 39 +++---- tests/mock_tables/global_db/state_db.json | 125 ++++++++++++++++++++++ tests/namespace/test_sensor.py | 125 ++++++++++++++++++++++ 3 files changed, 271 insertions(+), 18 deletions(-) diff --git a/src/sonic_ax_impl/mibs/ietf/rfc3433.py b/src/sonic_ax_impl/mibs/ietf/rfc3433.py index 42d2570d8..6602fca72 100644 --- a/src/sonic_ax_impl/mibs/ietf/rfc3433.py +++ b/src/sonic_ax_impl/mibs/ietf/rfc3433.py @@ -20,6 +20,8 @@ from .sensor_data import ThermalSensorData, FANSensorData, PSUSensorData, TransceiverSensorData NOT_AVAILABLE = 'N/A' +CHASSIS_NAME_SUB_STRING = 'chassis' +PSU_NAME_SUB_STRING = 'PSU' def is_null_empty_str(value): @@ -386,15 +388,17 @@ def reinit_data(self): if transceiver_dom_encoded: self.transceiver_dom = [entry for entry in transceiver_dom_encoded] - fan_sensor_encoded = Namespace.dbs_keys(self.statedb, mibs.STATE_DB, self.FAN_SENSOR_KEY_PATTERN) + # for FAN, PSU and thermal sensors, they are in host namespace DB, to avoid iterating all namespace DBs, + # just get data from host namespace DB, which is self.statedb[0]. + fan_sensor_encoded = self.statedb[0].keys(self.statedb[0].STATE_DB, self.FAN_SENSOR_KEY_PATTERN) if fan_sensor_encoded: self.fan_sensor = [entry for entry in fan_sensor_encoded] - psu_sensor_encoded = Namespace.dbs_keys(self.statedb, mibs.STATE_DB, self.PSU_SENSOR_KEY_PATTERN) + psu_sensor_encoded = self.statedb[0].keys(self.statedb[0].STATE_DB, self.PSU_SENSOR_KEY_PATTERN) if psu_sensor_encoded: self.psu_sensor = [entry for entry in psu_sensor_encoded] - thermal_sensor_encoded = Namespace.dbs_keys(self.statedb, mibs.STATE_DB, self.THERMAL_SENSOR_KEY_PATTERN) + thermal_sensor_encoded = self.statedb[0].keys(self.statedb[0].STATE_DB, self.THERMAL_SENSOR_KEY_PATTERN) if thermal_sensor_encoded: self.thermal_sensor = [entry for entry in thermal_sensor_encoded] @@ -447,15 +451,15 @@ def update_psu_sensor_data(self): for psu_sensor_entry in self.psu_sensor: psu_name = psu_sensor_entry.split(mibs.TABLE_NAME_SEPARATOR_VBAR)[-1] - psu_relation_info = Namespace.dbs_get_all(self.statedb, mibs.STATE_DB, - mibs.physical_entity_info_table(psu_name)) + psu_relation_info = self.statedb[0].get_all(self.statedb[0].STATE_DB, + mibs.physical_entity_info_table(psu_name)) psu_position, psu_parent_name = get_db_data(psu_relation_info, PhysicalRelationInfoDB) if is_null_empty_str(psu_position): continue psu_position = int(psu_position) psu_sub_id = get_psu_sub_id(psu_position) - psu_sensor_entry_data = Namespace.dbs_get_all(self.statedb, mibs.STATE_DB, psu_sensor_entry) + psu_sensor_entry_data = self.statedb[0].get_all(self.statedb[0].STATE_DB, psu_sensor_entry) if not psu_sensor_entry_data: continue @@ -490,27 +494,26 @@ def update_fan_sensor_data(self): fan_parent_sub_id = 0 for fan_sensor_entry in self.fan_sensor: fan_name = fan_sensor_entry.split(mibs.TABLE_NAME_SEPARATOR_VBAR)[-1] - fan_relation_info = Namespace.dbs_get_all(self.statedb, mibs.STATE_DB, - mibs.physical_entity_info_table(fan_name)) - + fan_relation_info = self.statedb[0].get_all(self.statedb[0].STATE_DB, + mibs.physical_entity_info_table(fan_name)) fan_position, fan_parent_name = get_db_data(fan_relation_info, PhysicalRelationInfoDB) if is_null_empty_str(fan_position): continue fan_position = int(fan_position) - if "chassis" in fan_parent_name: + if CHASSIS_NAME_SUB_STRING in fan_parent_name: fan_parent_sub_id = (CHASSIS_SUB_ID,) else: - fan_parent_relation_info = Namespace.dbs_get_all(self.statedb, mibs.STATE_DB, - mibs.physical_entity_info_table(fan_parent_name)) + fan_parent_relation_info = self.statedb[0].get_all(self.statedb[0].STATE_DB, + mibs.physical_entity_info_table(fan_parent_name)) if fan_parent_relation_info: fan_parent_position, fan_grad_parent_name = get_db_data(fan_parent_relation_info, PhysicalRelationInfoDB) fan_parent_position = int(fan_parent_position) - if "PSU" in fan_parent_name: + if PSU_NAME_SUB_STRING in fan_parent_name: fan_parent_sub_id = get_psu_sub_id(fan_parent_position) else: fan_parent_sub_id = get_fan_drawer_sub_id(fan_parent_position) @@ -520,7 +523,7 @@ def update_fan_sensor_data(self): fan_sub_id = get_fan_sub_id(fan_parent_sub_id, fan_position) - fan_sensor_entry_data = Namespace.dbs_get_all(self.statedb, mibs.STATE_DB, fan_sensor_entry) + fan_sensor_entry_data = self.statedb[0].get_all(self.statedb[0].STATE_DB, fan_sensor_entry) if not fan_sensor_entry_data: mibs.logger.error("fan_name = {} get fan_sensor_entry_data failed".format(fan_name)) @@ -555,17 +558,17 @@ def update_thermal_sensor_data(self): for thermal_sensor_entry in self.thermal_sensor: thermal_name = thermal_sensor_entry.split(mibs.TABLE_NAME_SEPARATOR_VBAR)[-1] - thermal_relation_info = Namespace.dbs_get_all(self.statedb, mibs.STATE_DB, - mibs.physical_entity_info_table(thermal_name)) + thermal_relation_info = self.statedb[0].get_all(self.statedb[0].STATE_DB, + mibs.physical_entity_info_table(thermal_name)) thermal_position, thermal_parent_name = get_db_data(thermal_relation_info, PhysicalRelationInfoDB) if is_null_empty_str(thermal_parent_name) or is_null_empty_str(thermal_parent_name) or \ - "chassis" not in thermal_parent_name.lower(): + CHASSIS_NAME_SUB_STRING not in thermal_parent_name.lower(): continue thermal_position = int(thermal_position) - thermal_sensor_entry_data = Namespace.dbs_get_all(self.statedb, mibs.STATE_DB, thermal_sensor_entry) + thermal_sensor_entry_data = self.statedb[0].get_all(self.statedb[0].STATE_DB, thermal_sensor_entry) if not thermal_sensor_entry_data: continue diff --git a/tests/mock_tables/global_db/state_db.json b/tests/mock_tables/global_db/state_db.json index 2c63c0851..0674d83ab 100644 --- a/tests/mock_tables/global_db/state_db.json +++ b/tests/mock_tables/global_db/state_db.json @@ -1,2 +1,127 @@ { + "DEVICE_METADATA|localhost": { + "chassis_serial_number": "SAMPLETESTSN" + }, + "PSU_INFO|PSU 1": { + "presence": "false", + "status": "false", + "current": "15.4", + "power": "302.6", + "voltage": "15.1", + "temp": "30.1" + }, + "PSU_INFO|PSU 2": { + "presence": "true", + "status": "true", + "current": "13.4", + "power": "312.6", + "voltage": "13.1", + "temp": "31.1", + "model": "PSU_MODEL", + "serial": "PSU_SERIAL", + "is_replaceable": "True" + }, + "PSU_INFO|PSU 3": { + "presence": "true", + "status": "false", + "current": "15.5", + "power": "302.8", + "voltage": "15.5", + "temp": "30.8" + }, + "CHASSIS_INFO|chassis 1": { + "psu_num": "3" + }, + "TRANSCEIVER_INFO|Ethernet0": { + "type": "QSFP+", + "hardware_rev": "A1", + "serial": "SERIAL_NUM", + "manufacturer": "VENDOR_NAME", + "model": "MODEL_NAME", + "is_replaceable": "True" + }, + "TRANSCEIVER_INFO|Ethernet1": { + "type": "QSFP-DD", + "hardware_rev": "A1", + "serial": "SERIAL_NUM", + "manufacturer": "VENDOR_NAME", + "model": "MODEL_NAME", + "is_replaceable": "True" + }, + "TRANSCEIVER_DOM_SENSOR|Ethernet0": { + "temperature": 25.39, + "voltage": 3.37, + "tx1bias": "N/A", + "tx2bias": 4.44, + "tx3bias": "inf", + "tx4bias": 4.44, + "rx1power": "-inf", + "rx2power": -0.97, + "rx3power": -0.97, + "rx4power": -0.97, + "tx1power": -5.4, + "tx2power": -5.4, + "tx3power": -5.4, + "tx4power": -5.4 + }, + "MGMT_PORT_TABLE|eth0": { + "oper_status": "down" + }, + "MGMT_PORT_TABLE|eth1": { + "oper_status": "up" + }, + "PHYSICAL_ENTITY_INFO|PSU 1": { + "position_in_parent": 1, + "parent_name": "chassis 1" + }, + "PHYSICAL_ENTITY_INFO|PSU 2": { + "position_in_parent": 2, + "parent_name": "chassis 1" + }, + "PHYSICAL_ENTITY_INFO|PSU 3": { + "position_in_parent": 3, + "parent_name": "chassis 1" + }, + "PHYSICAL_ENTITY_INFO|drawer1": { + "position_in_parent": 1, + "parent_name": "chassis 1" + }, + "PHYSICAL_ENTITY_INFO|fan1": { + "position_in_parent": 1, + "parent_name": "drawer1" + }, + "PHYSICAL_ENTITY_INFO|thermal1": { + "position_in_parent": 1, + "parent_name": "chassis 1" + }, + "PHYSICAL_ENTITY_INFO|psu_2_fan_1": { + "position_in_parent": "1", + "parent_name": "PSU 2" + }, + "FAN_DRAWER_INFO|drawer1": { + "model": "DRAWERMODEL", + "serial": "DRAWERSERIAL", + "presence": "True", + "is_replaceable": "True" + }, + "FAN_INFO|fan1": { + "model": "FANMODEL", + "serial": "FANSERIAL", + "speed": "50", + "presence": "True", + "is_replaceable": "True" + }, + "FAN_INFO|psu_2_fan_1": { + "presence": "True", + "status": "True", + "speed": "57", + "drawer_name": "N/A", + "model": "PSUFANMODEL", + "serial": "PSUFANSERIAL", + "is_replaceable": "False" + }, + "TEMPERATURE_INFO|thermal1": { + "temperature": "20.5", + "is_replaceable": "False" + } } diff --git a/tests/namespace/test_sensor.py b/tests/namespace/test_sensor.py index c5cc9fad7..b5eaf7cae 100644 --- a/tests/namespace/test_sensor.py +++ b/tests/namespace/test_sensor.py @@ -17,6 +17,12 @@ from ax_interface.encodings import ObjectIdentifier from ax_interface.constants import PduTypes from sonic_ax_impl.mibs.ietf.physical_entity_sub_oid_generator import get_transceiver_sub_id, get_transceiver_sensor_sub_id +from sonic_ax_impl.mibs.ietf.physical_entity_sub_oid_generator import get_psu_sub_id +from sonic_ax_impl.mibs.ietf.physical_entity_sub_oid_generator import get_psu_sensor_sub_id +from sonic_ax_impl.mibs.ietf.physical_entity_sub_oid_generator import get_fan_drawer_sub_id +from sonic_ax_impl.mibs.ietf.physical_entity_sub_oid_generator import get_fan_sub_id +from sonic_ax_impl.mibs.ietf.physical_entity_sub_oid_generator import get_fan_tachometers_sub_id +from sonic_ax_impl.mibs.ietf.physical_entity_sub_oid_generator import get_chassis_thermal_sub_id from sonic_ax_impl.mibs.ietf.physical_entity_sub_oid_generator import SENSOR_TYPE_TEMP from sonic_ax_impl.mibs.ietf.physical_entity_sub_oid_generator import SENSOR_TYPE_VOLTAGE from sonic_ax_impl.mibs.ietf.physical_entity_sub_oid_generator import SENSOR_TYPE_PORT_RX_POWER @@ -36,6 +42,11 @@ def setUpClass(cls): cls.XCVR_SUB_ID = get_transceiver_sub_id(cls.IFINDEX) cls.XCVR_SUB_ID_ASIC1 = get_transceiver_sub_id(cls.IFINDEX_ASIC1) cls.XCVR_CHANNELS = (1, 2, 3, 4) + cls.PSU_POSITION = 2 + cls.FAN_DRAWER_POSITION = 1 + cls.FAN_POSITION = 1 + cls.PSU_FAN_POSITION = 1 + cls.THERMAL_POSITION = 1 # Update MIBs for updater in cls.lut.updater_instances: @@ -233,3 +244,117 @@ def test_getpdu_xcvr_tx_bias_sensor(self): for channel in (2, 4): self._test_getpdu_sensor(get_transceiver_sensor_sub_id(self.IFINDEX, SENSOR_TYPE_PORT_TX_BIAS + channel)[0], expected_values) + def test_getpdu_psu_temp_sensor(self): + """ + Test case for correctness of psu temperature sensor MIB values + """ + + expected_values = [ + rfc3433.EntitySensorDataType.CELSIUS, + rfc3433.EntitySensorDataScale.UNITS, + 3, # precision + 31100, # expected sensor value + rfc3433.EntitySensorStatus.OK + ] + + psu_sub_id = get_psu_sub_id(self.PSU_POSITION) + self._test_getpdu_sensor(get_psu_sensor_sub_id(psu_sub_id, "temperature")[0], expected_values) + + def test_getpdu_psu_voltage_sensor(self): + """ + Test case for correctness of psu voltage sensor MIB values + """ + + expected_values = [ + rfc3433.EntitySensorDataType.VOLTS_DC, + rfc3433.EntitySensorDataScale.UNITS, + 3, # precision + 13100, # expected sensor value + rfc3433.EntitySensorStatus.OK + ] + + psu_sub_id = get_psu_sub_id(self.PSU_POSITION) + self._test_getpdu_sensor(get_psu_sensor_sub_id(psu_sub_id, "voltage")[0], expected_values) + + def test_getpdu_psu_power_sensor(self): + """ + Test case for correctness of psu voltage sensor MIB values + """ + + expected_values = [ + rfc3433.EntitySensorDataType.WATTS, + rfc3433.EntitySensorDataScale.UNITS, + 3, # precision + 312600, # expected sensor value + rfc3433.EntitySensorStatus.OK + ] + + psu_sub_id = get_psu_sub_id(self.PSU_POSITION) + self._test_getpdu_sensor(get_psu_sensor_sub_id(psu_sub_id, "power")[0], expected_values) + + def test_getpdu_psu_current_sensor(self): + """ + Test case for correctness of psu current sensor MIB values + """ + + expected_values = [ + rfc3433.EntitySensorDataType.AMPERES, + rfc3433.EntitySensorDataScale.UNITS, + 3, # precision + 13400, # expected sensor value + rfc3433.EntitySensorStatus.OK + ] + + psu_sub_id = get_psu_sub_id(self.PSU_POSITION) + self._test_getpdu_sensor(get_psu_sensor_sub_id(psu_sub_id, "current")[0], expected_values) + + def test_getpdu_chassis_fan_speed_sensor(self): + """ + Test case for correctness of fan speed sensor MIB values + """ + + expected_values = [ + rfc3433.EntitySensorDataType.UNKNOWN, + rfc3433.EntitySensorDataScale.UNITS, + 0, # precision + 50, # expected sensor value + rfc3433.EntitySensorStatus.OK + ] + + fan_parent_sub_id = get_fan_drawer_sub_id(self.FAN_DRAWER_POSITION) + fan_sub_id = get_fan_sub_id(fan_parent_sub_id, self.FAN_POSITION) + + self._test_getpdu_sensor(get_fan_tachometers_sub_id(fan_sub_id)[0], expected_values) + + def test_getpdu_psu_fan_speed_sensor(self): + """ + Test case for correctness of psu fan speed sensor MIB values + """ + + expected_values = [ + rfc3433.EntitySensorDataType.UNKNOWN, + rfc3433.EntitySensorDataScale.UNITS, + 0, # precision + 57, # expected sensor value + rfc3433.EntitySensorStatus.OK + ] + + psu_sub_id = get_psu_sub_id(self.PSU_POSITION) + fan_sub_id = get_fan_sub_id(psu_sub_id, self.PSU_FAN_POSITION) + + self._test_getpdu_sensor(get_fan_tachometers_sub_id(fan_sub_id)[0], expected_values) + + def test_getpdu_chassis_temp_sensor(self): + """ + Test case for correctness of chassis temp sensors MIB values + """ + + expected_values = [ + rfc3433.EntitySensorDataType.CELSIUS, + rfc3433.EntitySensorDataScale.UNITS, + 3, # precision + 20500, # expected sensor value + rfc3433.EntitySensorStatus.OK + ] + + self._test_getpdu_sensor(get_chassis_thermal_sub_id(self.THERMAL_POSITION)[0], expected_values)