[barefoot][sonic-platform] Fix get_system_eeprom_info and refactor eeprom.py (#6739)
Signed-off-by: Volodymyr Boyko <volodymyrx.boiko@intel.com>
This commit is contained in:
parent
23535b13f4
commit
32c497f5e3
@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
try:
|
||||
import sys
|
||||
from sonic_platform_base.chassis_base import ChassisBase
|
||||
from sonic_platform.sfp import Sfp
|
||||
from sonic_platform.psu import Psu
|
||||
@ -55,7 +56,7 @@ class Chassis(ChassisBase):
|
||||
Returns:
|
||||
string: Serial number of chassis
|
||||
"""
|
||||
return self._eeprom.serial_str()
|
||||
return self._eeprom.serial_number_str()
|
||||
|
||||
def get_sfp(self, index):
|
||||
"""
|
||||
|
@ -6,6 +6,7 @@ try:
|
||||
import logging
|
||||
import logging.config
|
||||
import yaml
|
||||
import re
|
||||
|
||||
sys.path.append(os.path.dirname(__file__))
|
||||
|
||||
@ -14,15 +15,15 @@ try:
|
||||
else:
|
||||
from cStringIO import StringIO
|
||||
|
||||
from sonic_eeprom import eeprom_base
|
||||
from sonic_eeprom import eeprom_tlvinfo
|
||||
from sonic_platform_base.sonic_eeprom import eeprom_base
|
||||
from sonic_platform_base.sonic_eeprom import eeprom_tlvinfo
|
||||
|
||||
from platform_thrift_client import thrift_try
|
||||
except ImportError as e:
|
||||
raise ImportError (str(e) + "- required module not found")
|
||||
|
||||
|
||||
eeprom_default_dict = {
|
||||
_platform_eeprom_map = {
|
||||
"prod_name" : ("Product Name", "0x21", 12),
|
||||
"odm_pcba_part_num" : ("Part Number", "0x22", 13),
|
||||
"prod_ser_num" : ("Serial Number", "0x23", 12),
|
||||
@ -33,36 +34,13 @@ eeprom_default_dict = {
|
||||
"sys_mfger" : ("Manufacturer", "0x2B", 8)
|
||||
}
|
||||
|
||||
eeprom_dict = { "version" : ("Version", None, 0),
|
||||
"pcb_mfger" : ("PCB Manufacturer", "0x01", 8),
|
||||
"prod_ser_num" : ("Serial Number", "0x23", 12),
|
||||
"bfn_pcba_part_num" : ("Switch PCBA Part Number", "0x02", 12),
|
||||
"odm_pcba_part_num" : ("Part Number", "0x22", 13),
|
||||
"bfn_pcbb_part_num" : ("Switch PCBB Part Number", "0x04", 12),
|
||||
"sys_asm_part_num" : ("System Assembly Part Number", "0x05", 12),
|
||||
"prod_state" : ("Product Production State", "0x06", 1),
|
||||
"location" : ("EEPROM Location of Fabric", "0x07", 8),
|
||||
"ext_mac_addr_size" : ("Extende MAC Address Size", "0x08", 2),
|
||||
"sys_mfg_date" : ("System Manufacturing Date", "0x25", 4),
|
||||
"prod_name" : ("Product Name", "0x21", 12),
|
||||
"prod_ver" : ("Product Version", "0x26", 1),
|
||||
"prod_part_num" : ("Product Part Number", "0x09", 8),
|
||||
"sys_mfger" : ("Manufacturer", "0x2B", 8),
|
||||
"assembled_at" : ("Assembled at", "0x08", 8),
|
||||
"prod_ast_tag" : ("Product Asset Tag", "0x09", 12),
|
||||
"loc_mac_addr" : ("Local MAC address", "0x0A", 12),
|
||||
"odm_pcba_ser_num" : ("ODM PBCA Serial Number", "0x0B", 12),
|
||||
"ext_mac_addr" : ("Extended MAC Address Base", "0x0C", 12),
|
||||
"prod_sub_ver" : ("Product Sub Version", "0x0D", 1)
|
||||
}
|
||||
|
||||
product_dict = { "Montara" : "Wedge100BF-32X-O-AC-F-BF",
|
||||
_product_dict = { "Montara" : "Wedge100BF-32X-O-AC-F-BF",
|
||||
"Lower MAV" : "Wedge100BF-65X-O-AC-F-BF",
|
||||
"Upper MAV" : "Wedge100BF-65X-O-AC-F-BF"
|
||||
}
|
||||
|
||||
EEPROM_SYMLINK = "/var/run/platform/eeprom/syseeprom"
|
||||
EEPROM_STATUS = "/var/run/platform/eeprom/status"
|
||||
_EEPROM_SYMLINK = "/var/run/platform/eeprom/syseeprom"
|
||||
_EEPROM_STATUS = "/var/run/platform/eeprom/status"
|
||||
|
||||
try:
|
||||
_str_type = basestring
|
||||
@ -71,46 +49,43 @@ except NameError:
|
||||
|
||||
class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
|
||||
def __init__(self):
|
||||
|
||||
with open(os.path.dirname(__file__) + "/logging.conf", 'r') as f:
|
||||
config_dict = yaml.load(f, yaml.SafeLoader)
|
||||
logging.config.dictConfig(config_dict)
|
||||
|
||||
if not os.path.exists(os.path.dirname(EEPROM_SYMLINK)):
|
||||
if not os.path.exists(os.path.dirname(_EEPROM_SYMLINK)):
|
||||
try:
|
||||
os.makedirs(os.path.dirname(EEPROM_SYMLINK))
|
||||
os.makedirs(os.path.dirname(_EEPROM_SYMLINK))
|
||||
except OSError as e:
|
||||
if e.errno != errno.EEXIST:
|
||||
raise
|
||||
|
||||
open(EEPROM_SYMLINK, 'a').close()
|
||||
f = open(EEPROM_STATUS, 'w')
|
||||
f.write("initializing..")
|
||||
f.close()
|
||||
open(_EEPROM_SYMLINK, 'a').close()
|
||||
with open(_EEPROM_STATUS, 'w') as f:
|
||||
f.write("initializing..")
|
||||
|
||||
self.eeprom_path = EEPROM_SYMLINK
|
||||
super(Eeprom, self).__init__(self.eeprom_path, 0, EEPROM_STATUS, True)
|
||||
self.eeprom_path = _EEPROM_SYMLINK
|
||||
super(Eeprom, self).__init__(self.eeprom_path, 0, _EEPROM_STATUS, True)
|
||||
|
||||
def sys_eeprom_get(client):
|
||||
return client.pltfm_mgr.pltfm_mgr_sys_eeprom_get()
|
||||
try:
|
||||
self.eeprom = thrift_try(sys_eeprom_get)
|
||||
platform_eeprom = thrift_try(sys_eeprom_get)
|
||||
except Exception:
|
||||
raise RuntimeError("eeprom.py: Initialization failed")
|
||||
|
||||
self.eeprom_parse()
|
||||
self.__eeprom_init(platform_eeprom)
|
||||
|
||||
def eeprom_parse(self):
|
||||
f = open(EEPROM_STATUS, 'w')
|
||||
f.write("ok")
|
||||
f.close()
|
||||
def __eeprom_init(self, platform_eeprom):
|
||||
with open(_EEPROM_STATUS, 'w') as f:
|
||||
f.write("ok")
|
||||
|
||||
eeprom_params = ""
|
||||
for attr, val in self.eeprom.__dict__.items():
|
||||
for attr, val in platform_eeprom.__dict__.items():
|
||||
if val is None:
|
||||
continue
|
||||
|
||||
elem = eeprom_default_dict.get(attr)
|
||||
elem = _platform_eeprom_map.get(attr)
|
||||
if elem is None:
|
||||
continue
|
||||
|
||||
@ -122,7 +97,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
|
||||
if attr == "sys_mfg_date":
|
||||
value = datetime.datetime.strptime(value, '%m-%d-%y').strftime('%m/%d/%Y 00:00:00')
|
||||
|
||||
product = product_dict.get(value)
|
||||
product = _product_dict.get(value)
|
||||
if product is not None:
|
||||
value = product
|
||||
if len(eeprom_params) > 0:
|
||||
@ -130,28 +105,51 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
|
||||
eeprom_params += "{0:s}={1:s}".format(elem[1], value)
|
||||
|
||||
orig_stdout = sys.stdout
|
||||
|
||||
sys.stdout = StringIO()
|
||||
try:
|
||||
new_e = eeprom_tlvinfo.TlvInfoDecoder.set_eeprom(self, "", [eeprom_params])
|
||||
eeprom_data = eeprom_tlvinfo.TlvInfoDecoder.set_eeprom(self, "", [eeprom_params])
|
||||
finally:
|
||||
decode_output = sys.stdout.getvalue()
|
||||
sys.stdout = orig_stdout
|
||||
|
||||
eeprom_base.EepromDecoder.write_eeprom(self, new_e)
|
||||
eeprom_base.EepromDecoder.write_eeprom(self, eeprom_data)
|
||||
self.__eeprom_tlv_dict = self.__parse_output(decode_output)
|
||||
|
||||
return True
|
||||
def __parse_output(self, decode_output):
|
||||
EEPROM_DECODE_HEADLINES = 6
|
||||
lines = decode_output.replace('\0', '').split('\n')
|
||||
lines = lines[EEPROM_DECODE_HEADLINES:]
|
||||
res = dict()
|
||||
|
||||
def serial_str(self):
|
||||
return self.eeprom.prod_ser_num
|
||||
for line in lines:
|
||||
try:
|
||||
# match whitespace-separated tag hex, length and value (value is mathced with its whitespaces)
|
||||
match = re.search('(0x[0-9a-fA-F]{2})([\s]+[\S]+[\s]+)([\S]+[\s]*[\S]*)', line)
|
||||
if match is not None:
|
||||
code = match.group(1)
|
||||
value = match.group(3).rstrip('\0')
|
||||
res[code] = value
|
||||
except Exception:
|
||||
pass
|
||||
return res
|
||||
|
||||
def __tlv_get(self, code):
|
||||
return self.__eeprom_tlv_dict.get("0x{:X}".format(code), 'N/A')
|
||||
|
||||
def system_eeprom_info(self):
|
||||
return self.eeprom.__dict__
|
||||
return self.__eeprom_tlv_dict
|
||||
|
||||
def serial_number_str(self):
|
||||
return self.__tlv_get(self._TLV_CODE_SERIAL_NUMBER)
|
||||
|
||||
def serial_str(self):
|
||||
return self.serial_number_str()
|
||||
|
||||
def base_mac_addr(self):
|
||||
return self.eeprom.ext_mac_addr.rstrip('\x00')
|
||||
return self.__tlv_get(self._TLV_CODE_MAC_BASE)
|
||||
|
||||
def part_number_str(self):
|
||||
return self.eeprom.prod_part_num
|
||||
return self.__tlv_get(self._TLV_CODE_PART_NUMBER)
|
||||
|
||||
def modelstr(self):
|
||||
return self.eeprom.prod_name
|
||||
return self.__tlv_get(self._TLV_CODE_PRODUCT_NAME)
|
||||
|
Reference in New Issue
Block a user