Skip to content

Commit

Permalink
refactor platform API to remove dependency on database
Browse files Browse the repository at this point in the history
  • Loading branch information
keboliu committed Sep 25, 2020
1 parent 13cec4c commit 8dfa8ad
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
41 changes: 17 additions & 24 deletions platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

MLNX_NUM_PSU = 2

GET_HWSKU_CMD = "sonic-cfggen -d -v DEVICE_METADATA.localhost.hwsku"
GET_PLATFORM_CMD = "sonic-cfggen -d -v DEVICE_METADATA.localhost.platform"

EEPROM_CACHE_ROOT = '/var/cache/sonic/decode-syseeprom'
Expand Down Expand Up @@ -61,18 +60,12 @@ class Chassis(ChassisBase):
def __init__(self):
super(Chassis, self).__init__()

# Initialize SKU name and Platform name
self.sku_name = self._get_sku_name()
self.platform_name = self._get_platform_name()

mi = device_info.get_machine_info()
if mi is not None:
self.name = mi['onie_platform']
self.platform_name = device_info.get_platform()
else:
self.name = self.sku_name
self.platform_name = self._get_platform_name()
self.name = "Undefined"
self.model = "Undefined"

# Initialize Platform name
self.platform_name = device_info.get_platform()

# move the initialization of each components to their dedicated initializer
# which will be called from platform
self.sfp_module_initialized = False
Expand Down Expand Up @@ -148,6 +141,9 @@ def initialize_eeprom(self):
from eeprom import Eeprom
# Initialize EEPROM
self._eeprom = Eeprom()
# Get chassis name and model from eeprom
self.name = self._eeprom.get_product_name()
self.model = self._eeprom.get_part_number()


def initialize_components(self):
Expand All @@ -173,6 +169,15 @@ def get_name(self):
return self.name


def get_model(self):
"""
Retrieves the model number (or part number) of the device
Returns:
string: Model/part number of device
"""
return self.model

##############################################
# SFP methods
##############################################
Expand Down Expand Up @@ -244,18 +249,6 @@ def _extract_num_of_fans_and_fan_drawers(self):

return num_of_fan, num_of_drawer


def _get_sku_name(self):
p = subprocess.Popen(GET_HWSKU_CMD, shell=True, stdout=subprocess.PIPE)
out, err = p.communicate()
return out.rstrip('\n')


def _get_platform_name(self):
p = subprocess.Popen(GET_PLATFORM_CMD, shell=True, stdout=subprocess.PIPE)
out, err = p.communicate()
return out.rstrip('\n')

def _get_port_position_tuple_by_platform_name(self):
position_tuple = port_position_tuple_list[platform_dict_port[self.platform_name]]
return position_tuple
Expand Down
34 changes: 32 additions & 2 deletions platform/mellanox/mlnx-platform-api/sonic_platform/eeprom.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,21 @@ def _load_eeprom(self):
pass

self._base_mac = self.mgmtaddrstr(eeprom)
if self._base_mac == None:
if self._base_mac is None:
self._base_mac = "Undefined."

self._serial_str = self.serial_number_str(eeprom)
if self._serial_str == None:
if self._serial_str is None:
self._serial_str = "Undefined."

self._product_name = self.modelstr(eeprom)
if self._product_name is None:
self._product_name = "Undefined."

self._part_number = self.part_number_str(eeprom)
if self._part_number is None:
self._part_number = "Undefined."

original_stdout = sys.stdout
sys.stdout = StringIO()
self.decode_eeprom(eeprom)
Expand Down Expand Up @@ -135,6 +143,28 @@ def get_serial_number(self):
self._load_eeprom()
return self._serial_str

def get_product_name(self):
"""
Retrieves the hardware product name for the chassis
Returns:
A string containing the hardware product name for this chassis.
"""
if not self._eeprom_loaded:
self._load_eeprom()
return self._product_name

def get_part_number(self):
"""
Retrieves the hardware part number for the chassis
Returns:
A string containing the hardware part number for this chassis.
"""
if not self._eeprom_loaded:
self._load_eeprom()
return self._part_number

def get_system_eeprom_info(self):
"""
Retrieves the full content of system EEPROM information for the chassis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(self):
self._chassis = Chassis()
self._chassis.initialize_components()
self._chassis.initizalize_system_led()
self._chassis.initialize_eeprom()
else:
self._chassis = Chassis()
self._chassis.initialize_psu()
Expand Down

0 comments on commit 8dfa8ad

Please sign in to comment.