From 734299c56a30b2383d0ca8002461e5953d6f46c8 Mon Sep 17 00:00:00 2001 From: Wirut Getbamrung Date: Fri, 14 Jun 2019 11:14:10 +0700 Subject: [PATCH 1/5] [device/celestica]: add firmware management api --- .../sonic_platform/chassis.py | 10 ++ .../sonic_platform/device.py | 37 +++++ .../sonic_platform/module.py | 131 +++++++++++++++++ .../sonic_platform/chassis.py | 9 ++ .../sonic_platform/device.py | 37 +++++ .../sonic_platform/module.py | 133 ++++++++++++++++++ 6 files changed, 357 insertions(+) create mode 100644 device/celestica/x86_64-cel_e1031-r0/sonic_platform/device.py create mode 100644 device/celestica/x86_64-cel_e1031-r0/sonic_platform/module.py create mode 100644 device/celestica/x86_64-cel_seastone-r0/sonic_platform/device.py create mode 100644 device/celestica/x86_64-cel_seastone-r0/sonic_platform/module.py diff --git a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py index 33fa88f8e700..958dcbe1bf28 100644 --- a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py +++ b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py @@ -17,6 +17,8 @@ try: from sonic_platform_base.chassis_base import ChassisBase from sonic_platform.fan import Fan + from sonic_platform.psu import Psu + from sonic_platform.module import Module except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -26,6 +28,8 @@ SMC_CPLD_PATH = "/sys/devices/platform/e1031.smc/version" MMC_CPLD_PATH = "/sys/devices/platform/e1031.smc/getreg" NUM_FAN = 3 +NUM_PSU = 2 +NUM_MODULE = 3 class Chassis(ChassisBase): @@ -36,6 +40,12 @@ def __init__(self): for index in range(0, NUM_FAN): fan = Fan(index) self._fan_list.append(fan) + for index in range(0, NUM_PSU): + psu = Psu(index) + self._psu_list.append(psu) + for index in range(0, NUM_MODULE): + module = Module(index) + self._module_list.append(module) ChassisBase.__init__(self) def __get_register_value(self, path, register): diff --git a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/device.py b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/device.py new file mode 100644 index 000000000000..9a21bd8dc24b --- /dev/null +++ b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/device.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +############################################################################# +# Celestica +# +# Module contains an implementation of SONiC Platform Base API and +# provides the components firmware management fucntion +# +############################################################################# + +try: + from sonic_platform_base.device_base import DeviceBase +except ImportError as e: + raise ImportError(str(e) + "- required module not found") + + +class Device(DeviceBase): + """Platform-specific Device class""" + + MODULES_NAME = ["SMC_CPLD", "MMC_CPLD", "BIOS"] + + def __init__(self, index, device_type): + self.device_type = device_type + self.index = index + DeviceBase.__init__(self) + + def get_name(self): + """ + Retrieves the name of the device + + Returns: + string: The name of the device + """ + device_name = { + "module": self.MODULES_NAME[self.index] + }.get(self.device_type, None) + return device_name diff --git a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/module.py b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/module.py new file mode 100644 index 000000000000..5605a04afaeb --- /dev/null +++ b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/module.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python + +############################################################################# +# Celestica +# +# Module contains an implementation of SONiC Platform Base API and +# provides the components firmware management fucntion +# +############################################################################# + +import json +import os.path +import shutil +import shlex +import subprocess + +try: + from sonic_platform_base.module_base import ModuleBase + from sonic_platform.device import Device +except ImportError as e: + raise ImportError(str(e) + "- required module not found") + +MMC_CPLD_ADDR = '0x100' +BIOS_VERSION_PATH = "/sys/class/dmi/id/bios_version" +CONFIG_DB_PATH = "/etc/sonic/config_db.json" +SMC_CPLD_PATH = "/sys/devices/platform/e1031.smc/version" +MMC_CPLD_PATH = "/sys/devices/platform/e1031.smc/getreg" + + +class Module(ModuleBase): + """Platform-specific Module class""" + + def __init__(self, module_index): + ModuleBase.__init__(self) + self.index = module_index + self.device_base = Device(self.index, self.DEVICE_TYPE) + self.name = self.device_base.get_name() + + def __run_command(self, command): + # Run bash command and print output to stdout + try: + process = subprocess.Popen( + shlex.split(command), stdout=subprocess.PIPE) + while True: + output = process.stdout.readline() + if output == '' and process.poll() is not None: + break + if output: + print(output.strip()) + rc = process.poll() + if rc != 0: + return False + except: + return False + return True + + def __get_register_value(self, path, register): + # Retrieves the cpld register value + cmd = "echo {1} > {0}; cat {0}".format(path, register) + p = subprocess.Popen( + cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + raw_data, err = p.communicate() + if err is not '': + return None + return raw_data.strip() + + def get_bios_version(self): + # Retrieves the BIOS firmware version + try: + with open(BIOS_VERSION_PATH, 'r') as fd: + bios_version = fd.read() + return bios_version.strip() + except Exception as e: + return None + + def get_cpld_version(self): + # Retrieves the CPLD firmware version + cpld_version = dict() + with open(SMC_CPLD_PATH, 'r') as fd: + smc_cpld_version = fd.read() + smc_cpld_version = 'None' if smc_cpld_version is 'None' else "{}.{}".format( + int(smc_cpld_version[2], 16), int(smc_cpld_version[3], 16)) + + mmc_cpld_version = self.__get_register_value( + MMC_CPLD_PATH, MMC_CPLD_ADDR) + mmc_cpld_version = 'None' if mmc_cpld_version is 'None' else "{}.{}".format( + int(mmc_cpld_version[2], 16), int(mmc_cpld_version[3], 16)) + + cpld_version["SMC_CPLD"] = smc_cpld_version + cpld_version["MMC_CPLD"] = mmc_cpld_version + return cpld_version + + def get_firmware_version(self): + """ + Retrieves the firmware version of module + Returns: + string: The firmware versions of the module + """ + fw_version = None + + if self.name == "BIOS": + fw_version = self.get_bios_version() + elif "CPLD" in self.name: + cpld_version = self.get_cpld_version() + fw_version = cpld_version.get(self.name) + + return fw_version + + def upgrade_firmware(self, image_path): + """ + Install firmware to module + Args: + image_path: A string, path to firmware image + Returns: + A boolean, True if install successfully, False if not + """ + if not os.path.isfile(image_path): + return False + + if "CPLD" in self.name: + img_name = os.path.basename(image_path) + root, ext = os.path.splitext(img_name) + ext = ".vme" if ext == "" else ext + new_image_path = os.path.join("/tmp", (root.lower() + ext)) + shutil.copy(image_path, new_image_path) + install_command = "ispvm %s" % new_image_path + elif self.name == "BIOS": + print("Not supported") + return False + + return self.__run_command(install_command) diff --git a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py index 1d2e9af3757b..4f5e600fe249 100644 --- a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py +++ b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py @@ -17,6 +17,8 @@ try: from sonic_platform_base.chassis_base import ChassisBase from sonic_platform.fan import Fan + from sonic_platform.psu import Psu + from sonic_platform.module import Module except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -24,6 +26,7 @@ GETREG_PATH = "/sys/devices/platform/dx010_cpld/getreg" CONFIG_DB_PATH = "/etc/sonic/config_db.json" NUM_FAN = 5 +NUM_MODULE = 5 CPLD_ADDR_MAPPING = { "CPLD1": "0x100", "CPLD2": "0x200", @@ -41,6 +44,12 @@ def __init__(self): for index in range(0, NUM_FAN): fan = Fan(index) self._fan_list.append(fan) + for index in range(0, NUM_PSU): + psu = Psu(index) + self._psu_list.append(psu) + for index in range(0, NUM_MODULE): + module = Module(index) + self._module_list.append(module) ChassisBase.__init__(self) def __get_register_value(self, path, register): diff --git a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/device.py b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/device.py new file mode 100644 index 000000000000..1a8589f6db9b --- /dev/null +++ b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/device.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +############################################################################# +# Celestica +# +# Module contains an implementation of SONiC Platform Base API and +# provides the components firmware management fucntion +# +############################################################################# + +try: + from sonic_platform_base.device_base import DeviceBase +except ImportError as e: + raise ImportError(str(e) + "- required module not found") + + +class Device(DeviceBase): + """Platform-specific Device class""" + + MODULES_NAME = ["CPLD1", "CPLD2", "CPLD3", "CPLD4", "BIOS"] + + def __init__(self, index, device_type): + self.device_type = device_type + self.index = index + DeviceBase.__init__(self) + + def get_name(self): + """ + Retrieves the name of the device + + Returns: + string: The name of the device + """ + device_name = { + "module": self.MODULES_NAME[self.index] + }.get(self.device_type, None) + return device_name diff --git a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/module.py b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/module.py new file mode 100644 index 000000000000..26888996aacd --- /dev/null +++ b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/module.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python + +############################################################################# +# Celestica +# +# Module contains an implementation of SONiC Platform Base API and +# provides the components firmware management fucntion +# +############################################################################# + +import json +import os.path +import shutil +import shlex +import subprocess + +try: + from sonic_platform_base.module_base import ModuleBase + from sonic_platform.device import Device +except ImportError as e: + raise ImportError(str(e) + "- required module not found") + +CPLD_ADDR_MAPPING = { + "CPLD1": "0x100", + "CPLD2": "0x200", + "CPLD3": "0x280", + "CPLD4": "0x300", + "CPLD5": "0x380" +} +GETREG_PATH = "/sys/devices/platform/dx010_cpld/getreg" +BIOS_VERSION_PATH = "/sys/class/dmi/id/bios_version" + + +class Module(ModuleBase): + """Platform-specific Module class""" + + def __init__(self, module_index): + ModuleBase.__init__(self) + self.index = module_index + self.device_base = Device(self.index, self.DEVICE_TYPE) + self.name = self.device_base.get_name() + + def __run_command(self, command): + # Run bash command and print output to stdout + try: + process = subprocess.Popen( + shlex.split(command), stdout=subprocess.PIPE) + while True: + output = process.stdout.readline() + if output == '' and process.poll() is not None: + break + if output: + print(output.strip()) + rc = process.poll() + if rc != 0: + return False + except: + return False + return True + + def __get_register_value(self, path, register): + # Retrieves the cpld register value + cmd = "echo {1} > {0}; cat {0}".format(path, register) + p = subprocess.Popen( + cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + raw_data, err = p.communicate() + if err is not '': + return None + return raw_data.strip() + + def get_bios_version(self): + # Retrieves the BIOS firmware version + try: + with open(BIOS_VERSION_PATH, 'r') as fd: + bios_version = fd.read() + return bios_version.strip() + except Exception as e: + return None + + def get_cpld_version(self): + # Retrieves the CPLD firmware version + cpld_version = dict() + for cpld_name in CPLD_ADDR_MAPPING: + try: + cpld_addr = CPLD_ADDR_MAPPING[cpld_name] + cpld_version_raw = self.__get_register_value( + GETREG_PATH, cpld_addr) + cpld_version_str = "{}.{}".format(int(cpld_version_raw[2], 16), int( + cpld_version_raw[3], 16)) if cpld_version_raw is not None else 'None' + cpld_version[cpld_name] = cpld_version_str + except Exception as e: + cpld_version[cpld_name] = 'None' + return cpld_version + + def get_firmware_version(self): + """ + Retrieves the firmware version of module + Returns: + string: The firmware versions of the module + """ + fw_version = None + + if self.name == "BIOS": + fw_version = self.get_bios_version() + elif "CPLD" in self.name: + cpld_version = self.get_cpld_version() + fw_version = cpld_version.get(self.name) + + return fw_version + + def upgrade_firmware(self, image_path): + """ + Install firmware to module + Args: + image_path: A string, path to firmware image + Returns: + A boolean, True if install successfully, False if not + """ + if not os.path.isfile(image_path): + return False + + if "CPLD" in self.name: + img_name = os.path.basename(image_path) + root, ext = os.path.splitext(img_name) + ext = ".vme" if ext == "" else ext + new_image_path = os.path.join("/tmp", (root.lower() + ext)) + shutil.copy(image_path, new_image_path) + install_command = "ispvm %s" % new_image_path + elif self.name == "BIOS": + print("Not supported") + return False + + return self.__run_command(install_command) From c999dac67b6952c4e9688795a894634c70e6e730 Mon Sep 17 00:00:00 2001 From: Wirut Getbamrung Date: Mon, 24 Jun 2019 11:24:07 +0700 Subject: [PATCH 2/5] [device/celestica]: move api to chassisbase --- .../sonic_platform/chassis.py | 81 ++++++++---------- .../{module.py => component.py} | 31 ++++--- .../sonic_platform/device.py | 24 ++++-- .../sonic_platform/chassis.py | 83 ++++++++----------- .../{module.py => component.py} | 31 ++++--- .../sonic_platform/device.py | 24 ++++-- 6 files changed, 131 insertions(+), 143 deletions(-) rename device/celestica/x86_64-cel_e1031-r0/sonic_platform/{module.py => component.py} (84%) rename device/celestica/x86_64-cel_seastone-r0/sonic_platform/{module.py => component.py} (84%) diff --git a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py index 958dcbe1bf28..ec06eb13aa33 100644 --- a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py +++ b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py @@ -18,18 +18,14 @@ from sonic_platform_base.chassis_base import ChassisBase from sonic_platform.fan import Fan from sonic_platform.psu import Psu - from sonic_platform.module import Module + from sonic_platform.device import Device + from sonic_platform.component import Component except ImportError as e: raise ImportError(str(e) + "- required module not found") -MMC_CPLD_ADDR = '0x100' -BIOS_VERSION_PATH = "/sys/class/dmi/id/bios_version" CONFIG_DB_PATH = "/etc/sonic/config_db.json" -SMC_CPLD_PATH = "/sys/devices/platform/e1031.smc/version" -MMC_CPLD_PATH = "/sys/devices/platform/e1031.smc/getreg" NUM_FAN = 3 NUM_PSU = 2 -NUM_MODULE = 3 class Chassis(ChassisBase): @@ -43,20 +39,9 @@ def __init__(self): for index in range(0, NUM_PSU): psu = Psu(index) self._psu_list.append(psu) - for index in range(0, NUM_MODULE): - module = Module(index) - self._module_list.append(module) ChassisBase.__init__(self) - - def __get_register_value(self, path, register): - cmd = "echo {1} > {0}; cat {0}".format(path, register) - p = subprocess.Popen( - cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - raw_data, err = p.communicate() - if err is not '': - return 'None' - else: - return raw_data.strip() + self.component_device = Device("component") + self.component_list = self.component_device.get_name_list() def __read_config_db(self): try: @@ -80,39 +65,41 @@ def get_base_mac(self): except KeyError: raise KeyError("Base MAC not found") - def get_component_versions(self): + def get_component_list(self): + """ + Retrieves chassis components list such as BIOS, CPLD, FPGA, etc. + + Returns: + A list containing component name + """ + return self.component_list + + def get_firmware_version(self, component_name): """ Retrieves platform-specific hardware/firmware versions for chassis componenets such as BIOS, CPLD, FPGA, etc. + Args: + type: A string, component name + Returns: A string containing platform-specific component versions """ + self.component = Component(component_name) + if component_name not in self.component_list: + return None + return self.component.get_firmware_version() - component_versions = dict() - - # Get BIOS version - try: - with open(BIOS_VERSION_PATH, 'r') as fd: - bios_version = fd.read() - except IOError: - raise IOError("Unable to open version file !") - - # Get CPLD version - cpld_version = dict() - - with open(SMC_CPLD_PATH, 'r') as fd: - smc_cpld_version = fd.read() - smc_cpld_version = 'None' if smc_cpld_version is 'None' else "{}.{}".format( - int(smc_cpld_version[2], 16), int(smc_cpld_version[3], 16)) - - mmc_cpld_version = self.__get_register_value( - MMC_CPLD_PATH, MMC_CPLD_ADDR) - mmc_cpld_version = 'None' if mmc_cpld_version is 'None' else "{}.{}".format( - int(mmc_cpld_version[2], 16), int(mmc_cpld_version[3], 16)) - - cpld_version["SMC"] = smc_cpld_version - cpld_version["MMC"] = mmc_cpld_version + def install_component_firmware(self, component_name, image_path): + """ + Install firmware to module + Args: + type: A string, component name. + image_path: A string, path to firmware image. - component_versions["CPLD"] = cpld_version - component_versions["BIOS"] = bios_version.strip() - return str(component_versions) + Returns: + A boolean, True if install successfully, False if not + """ + self.component = Component(component_name) + if component_name not in self.component_list: + return False + return self.component.upgrade_firmware(image_path) diff --git a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/module.py b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/component.py similarity index 84% rename from device/celestica/x86_64-cel_e1031-r0/sonic_platform/module.py rename to device/celestica/x86_64-cel_e1031-r0/sonic_platform/component.py index 5605a04afaeb..7a3e05b4828d 100644 --- a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/module.py +++ b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/component.py @@ -3,8 +3,8 @@ ############################################################################# # Celestica # -# Module contains an implementation of SONiC Platform Base API and -# provides the components firmware management fucntion +# Component contains an implementation of SONiC Platform Base API and +# provides the components firmware management function # ############################################################################# @@ -15,8 +15,7 @@ import subprocess try: - from sonic_platform_base.module_base import ModuleBase - from sonic_platform.device import Device + from sonic_platform_base.device_base import DeviceBase except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -27,14 +26,14 @@ MMC_CPLD_PATH = "/sys/devices/platform/e1031.smc/getreg" -class Module(ModuleBase): - """Platform-specific Module class""" +class Component(DeviceBase): + """Platform-specific Component class""" - def __init__(self, module_index): - ModuleBase.__init__(self) - self.index = module_index - self.device_base = Device(self.index, self.DEVICE_TYPE) - self.name = self.device_base.get_name() + DEVICE_TYPE = "component" + + def __init__(self, component_name): + DeviceBase.__init__(self) + self.name = component_name.upper() def __run_command(self, command): # Run bash command and print output to stdout @@ -45,8 +44,6 @@ def __run_command(self, command): output = process.stdout.readline() if output == '' and process.poll() is not None: break - if output: - print(output.strip()) rc = process.poll() if rc != 0: return False @@ -64,7 +61,7 @@ def __get_register_value(self, path, register): return None return raw_data.strip() - def get_bios_version(self): + def __get_bios_version(self): # Retrieves the BIOS firmware version try: with open(BIOS_VERSION_PATH, 'r') as fd: @@ -73,7 +70,7 @@ def get_bios_version(self): except Exception as e: return None - def get_cpld_version(self): + def __get_cpld_version(self): # Retrieves the CPLD firmware version cpld_version = dict() with open(SMC_CPLD_PATH, 'r') as fd: @@ -99,9 +96,9 @@ def get_firmware_version(self): fw_version = None if self.name == "BIOS": - fw_version = self.get_bios_version() + fw_version = self.__get_bios_version() elif "CPLD" in self.name: - cpld_version = self.get_cpld_version() + cpld_version = self.__get_cpld_version() fw_version = cpld_version.get(self.name) return fw_version diff --git a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/device.py b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/device.py index 9a21bd8dc24b..e95d372510d1 100644 --- a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/device.py +++ b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/device.py @@ -3,8 +3,8 @@ ############################################################################# # Celestica # -# Module contains an implementation of SONiC Platform Base API and -# provides the components firmware management fucntion +# Device contains an implementation of SONiC Platform Base API and +# provides the device information # ############################################################################# @@ -17,9 +17,9 @@ class Device(DeviceBase): """Platform-specific Device class""" - MODULES_NAME = ["SMC_CPLD", "MMC_CPLD", "BIOS"] + COMPONENTS_NAME = ["SMC_CPLD", "MMC_CPLD", "BIOS"] - def __init__(self, index, device_type): + def __init__(self, device_type, index=None): self.device_type = device_type self.index = index DeviceBase.__init__(self) @@ -27,11 +27,21 @@ def __init__(self, index, device_type): def get_name(self): """ Retrieves the name of the device - - Returns: + Returns: string: The name of the device """ device_name = { - "module": self.MODULES_NAME[self.index] + "component": self.COMPONENTS_NAME[self.index] }.get(self.device_type, None) return device_name + + def get_name_list(self): + """ + Retrieves list of the device name that available in this device type + Returns: + string: The list of device name + """ + name_list = { + "component": self.COMPONENTS_NAME + }.get(self.device_type, None) + return name_list diff --git a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py index 4f5e600fe249..98e2abb4322c 100644 --- a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py +++ b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py @@ -18,22 +18,14 @@ from sonic_platform_base.chassis_base import ChassisBase from sonic_platform.fan import Fan from sonic_platform.psu import Psu - from sonic_platform.module import Module + from sonic_platform.device import Device + from sonic_platform.component import Component except ImportError as e: raise ImportError(str(e) + "- required module not found") -BIOS_VERSION_PATH = "/sys/class/dmi/id/bios_version" -GETREG_PATH = "/sys/devices/platform/dx010_cpld/getreg" CONFIG_DB_PATH = "/etc/sonic/config_db.json" NUM_FAN = 5 -NUM_MODULE = 5 -CPLD_ADDR_MAPPING = { - "CPLD1": "0x100", - "CPLD2": "0x200", - "CPLD3": "0x280", - "CPLD4": "0x300", - "CPLD5": "0x380" -} +NUM_PSU = 2 class Chassis(ChassisBase): @@ -47,20 +39,9 @@ def __init__(self): for index in range(0, NUM_PSU): psu = Psu(index) self._psu_list.append(psu) - for index in range(0, NUM_MODULE): - module = Module(index) - self._module_list.append(module) ChassisBase.__init__(self) - - def __get_register_value(self, path, register): - cmd = "echo {1} > {0}; cat {0}".format(path, register) - p = subprocess.Popen( - cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - raw_data, err = p.communicate() - if err is not '': - return 'None' - else: - return raw_data.strip() + self.component_device = Device("component") + self.component_list = self.component_device.get_name_list() def __read_config_db(self): try: @@ -84,35 +65,41 @@ def get_base_mac(self): except KeyError: raise KeyError("Base MAC not found") - def get_component_versions(self): + def get_component_list(self): + """ + Retrieves chassis components list such as BIOS, CPLD, FPGA, etc. + + Returns: + A list containing component name + """ + return self.component_list + + def get_firmware_version(self, component_name): """ Retrieves platform-specific hardware/firmware versions for chassis componenets such as BIOS, CPLD, FPGA, etc. + Args: + type: A string, component name + Returns: A string containing platform-specific component versions """ + self.component = Component(component_name) + if component_name not in self.component_list: + return None + return self.component.get_firmware_version() - component_versions = dict() - - # Get BIOS version - try: - with open(BIOS_VERSION_PATH, 'r') as fd: - bios_version = fd.read() - except IOError: - raise IOError("Unable to open version file !") + def install_component_firmware(self, component_name, image_path): + """ + Install firmware to module + Args: + type: A string, component name. + image_path: A string, path to firmware image. - # Get CPLD version - cpld_version = dict() - for cpld_name in CPLD_ADDR_MAPPING: - try: - cpld_addr = CPLD_ADDR_MAPPING[cpld_name] - cpld_version_raw = self.__get_register_value( - GETREG_PATH, cpld_addr) - cpld_version_str = "{}.{}".format(int(cpld_version_raw[2], 16), int( - cpld_version_raw[3], 16)) if cpld_version_raw is not None else 'None' - cpld_version[cpld_name] = cpld_version_str - except Exception, e: - cpld_version[cpld_name] = 'None' - component_versions["CPLD"] = cpld_version - component_versions["BIOS"] = bios_version.strip() - return str(component_versions) + Returns: + A boolean, True if install successfully, False if not + """ + self.component = Component(component_name) + if component_name not in self.component_list: + return False + return self.component.upgrade_firmware(image_path) diff --git a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/module.py b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/component.py similarity index 84% rename from device/celestica/x86_64-cel_seastone-r0/sonic_platform/module.py rename to device/celestica/x86_64-cel_seastone-r0/sonic_platform/component.py index 26888996aacd..b85bab7432eb 100644 --- a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/module.py +++ b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/component.py @@ -3,8 +3,8 @@ ############################################################################# # Celestica # -# Module contains an implementation of SONiC Platform Base API and -# provides the components firmware management fucntion +# Component contains an implementation of SONiC Platform Base API and +# provides the components firmware management function # ############################################################################# @@ -15,8 +15,7 @@ import subprocess try: - from sonic_platform_base.module_base import ModuleBase - from sonic_platform.device import Device + from sonic_platform_base.device_base import DeviceBase except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -31,14 +30,14 @@ BIOS_VERSION_PATH = "/sys/class/dmi/id/bios_version" -class Module(ModuleBase): - """Platform-specific Module class""" +class Component(DeviceBase): + """Platform-specific Component class""" - def __init__(self, module_index): - ModuleBase.__init__(self) - self.index = module_index - self.device_base = Device(self.index, self.DEVICE_TYPE) - self.name = self.device_base.get_name() + DEVICE_TYPE = "component" + + def __init__(self, component_name): + DeviceBase.__init__(self) + self.name = component_name.upper() def __run_command(self, command): # Run bash command and print output to stdout @@ -49,8 +48,6 @@ def __run_command(self, command): output = process.stdout.readline() if output == '' and process.poll() is not None: break - if output: - print(output.strip()) rc = process.poll() if rc != 0: return False @@ -68,7 +65,7 @@ def __get_register_value(self, path, register): return None return raw_data.strip() - def get_bios_version(self): + def __get_bios_version(self): # Retrieves the BIOS firmware version try: with open(BIOS_VERSION_PATH, 'r') as fd: @@ -77,7 +74,7 @@ def get_bios_version(self): except Exception as e: return None - def get_cpld_version(self): + def __get_cpld_version(self): # Retrieves the CPLD firmware version cpld_version = dict() for cpld_name in CPLD_ADDR_MAPPING: @@ -101,9 +98,9 @@ def get_firmware_version(self): fw_version = None if self.name == "BIOS": - fw_version = self.get_bios_version() + fw_version = self.__get_bios_version() elif "CPLD" in self.name: - cpld_version = self.get_cpld_version() + cpld_version = self.__get_cpld_version() fw_version = cpld_version.get(self.name) return fw_version diff --git a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/device.py b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/device.py index 1a8589f6db9b..2d717beed3a8 100644 --- a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/device.py +++ b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/device.py @@ -3,8 +3,8 @@ ############################################################################# # Celestica # -# Module contains an implementation of SONiC Platform Base API and -# provides the components firmware management fucntion +# Device contains an implementation of SONiC Platform Base API and +# provides the device information # ############################################################################# @@ -17,9 +17,9 @@ class Device(DeviceBase): """Platform-specific Device class""" - MODULES_NAME = ["CPLD1", "CPLD2", "CPLD3", "CPLD4", "BIOS"] + COMPONENTS_NAME = ["CPLD1", "CPLD2", "CPLD3", "CPLD4", "BIOS"] - def __init__(self, index, device_type): + def __init__(self, device_type, index=None): self.device_type = device_type self.index = index DeviceBase.__init__(self) @@ -27,11 +27,21 @@ def __init__(self, index, device_type): def get_name(self): """ Retrieves the name of the device - - Returns: + Returns: string: The name of the device """ device_name = { - "module": self.MODULES_NAME[self.index] + "component": self.COMPONENTS_NAME[self.index] }.get(self.device_type, None) return device_name + + def get_name_list(self): + """ + Retrieves list of the device name that available in this device type + Returns: + string: The list of device name + """ + name_list = { + "component": self.COMPONENTS_NAME + }.get(self.device_type, None) + return name_list From a1f9c9463adadf31f0f3fe6a42ffcdd062c251a0 Mon Sep 17 00:00:00 2001 From: Wirut Getbamrung Date: Tue, 25 Jun 2019 11:39:46 +0700 Subject: [PATCH 3/5] [device/celestica]: move component_list to chassisbase --- .../sonic_platform/chassis.py | 17 ++++------------- .../sonic_platform/chassis.py | 17 ++++------------- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py index ec06eb13aa33..8ccbc9524783 100644 --- a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py +++ b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py @@ -40,8 +40,8 @@ def __init__(self): psu = Psu(index) self._psu_list.append(psu) ChassisBase.__init__(self) - self.component_device = Device("component") - self.component_list = self.component_device.get_name_list() + self._component_device = Device("component") + self._component_list = self._component_device.get_name_list() def __read_config_db(self): try: @@ -65,15 +65,6 @@ def get_base_mac(self): except KeyError: raise KeyError("Base MAC not found") - def get_component_list(self): - """ - Retrieves chassis components list such as BIOS, CPLD, FPGA, etc. - - Returns: - A list containing component name - """ - return self.component_list - def get_firmware_version(self, component_name): """ Retrieves platform-specific hardware/firmware versions for chassis @@ -85,7 +76,7 @@ def get_firmware_version(self, component_name): A string containing platform-specific component versions """ self.component = Component(component_name) - if component_name not in self.component_list: + if component_name not in self._component_list: return None return self.component.get_firmware_version() @@ -100,6 +91,6 @@ def install_component_firmware(self, component_name, image_path): A boolean, True if install successfully, False if not """ self.component = Component(component_name) - if component_name not in self.component_list: + if component_name not in self._component_list: return False return self.component.upgrade_firmware(image_path) diff --git a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py index 98e2abb4322c..ee239a5c5a60 100644 --- a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py +++ b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py @@ -40,8 +40,8 @@ def __init__(self): psu = Psu(index) self._psu_list.append(psu) ChassisBase.__init__(self) - self.component_device = Device("component") - self.component_list = self.component_device.get_name_list() + self._component_device = Device("component") + self._component_list = self._component_device.get_name_list() def __read_config_db(self): try: @@ -65,15 +65,6 @@ def get_base_mac(self): except KeyError: raise KeyError("Base MAC not found") - def get_component_list(self): - """ - Retrieves chassis components list such as BIOS, CPLD, FPGA, etc. - - Returns: - A list containing component name - """ - return self.component_list - def get_firmware_version(self, component_name): """ Retrieves platform-specific hardware/firmware versions for chassis @@ -85,7 +76,7 @@ def get_firmware_version(self, component_name): A string containing platform-specific component versions """ self.component = Component(component_name) - if component_name not in self.component_list: + if component_name not in self._component_list: return None return self.component.get_firmware_version() @@ -100,6 +91,6 @@ def install_component_firmware(self, component_name, image_path): A boolean, True if install successfully, False if not """ self.component = Component(component_name) - if component_name not in self.component_list: + if component_name not in self._component_list: return False return self.component.upgrade_firmware(image_path) From afc4c35905c37961cb7e61fee8379abd547f09e7 Mon Sep 17 00:00:00 2001 From: Wirut Getbamrung Date: Wed, 26 Jun 2019 11:25:34 +0700 Subject: [PATCH 4/5] [device/celestica]: rename component list --- .../celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py | 6 +++--- .../x86_64-cel_seastone-r0/sonic_platform/chassis.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py index 8ccbc9524783..25668e2113f7 100644 --- a/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py +++ b/device/celestica/x86_64-cel_e1031-r0/sonic_platform/chassis.py @@ -41,7 +41,7 @@ def __init__(self): self._psu_list.append(psu) ChassisBase.__init__(self) self._component_device = Device("component") - self._component_list = self._component_device.get_name_list() + self._component_name_list = self._component_device.get_name_list() def __read_config_db(self): try: @@ -76,7 +76,7 @@ def get_firmware_version(self, component_name): A string containing platform-specific component versions """ self.component = Component(component_name) - if component_name not in self._component_list: + if component_name not in self._component_name_list: return None return self.component.get_firmware_version() @@ -91,6 +91,6 @@ def install_component_firmware(self, component_name, image_path): A boolean, True if install successfully, False if not """ self.component = Component(component_name) - if component_name not in self._component_list: + if component_name not in self._component_name_list: return False return self.component.upgrade_firmware(image_path) diff --git a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py index ee239a5c5a60..91e456e094ca 100644 --- a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py +++ b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/chassis.py @@ -41,7 +41,7 @@ def __init__(self): self._psu_list.append(psu) ChassisBase.__init__(self) self._component_device = Device("component") - self._component_list = self._component_device.get_name_list() + self._component_name_list = self._component_device.get_name_list() def __read_config_db(self): try: @@ -76,7 +76,7 @@ def get_firmware_version(self, component_name): A string containing platform-specific component versions """ self.component = Component(component_name) - if component_name not in self._component_list: + if component_name not in self._component_name_list: return None return self.component.get_firmware_version() @@ -91,6 +91,6 @@ def install_component_firmware(self, component_name, image_path): A boolean, True if install successfully, False if not """ self.component = Component(component_name) - if component_name not in self._component_list: + if component_name not in self._component_name_list: return False return self.component.upgrade_firmware(image_path) From b195497e2b20354ac3aff3d09f01cd2c1fc9dde0 Mon Sep 17 00:00:00 2001 From: Wirut Getbamrung Date: Tue, 2 Jul 2019 14:41:41 +0700 Subject: [PATCH 5/5] [sonic-platform-common]: Update the sonic-platform-common submodule to the latest commit --- src/sonic-platform-common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sonic-platform-common b/src/sonic-platform-common index 7f95a2a6f09b..56b5b14dec17 160000 --- a/src/sonic-platform-common +++ b/src/sonic-platform-common @@ -1 +1 @@ -Subproject commit 7f95a2a6f09b4dbaec9101fc9545bda95f541bea +Subproject commit 56b5b14dec179ddf445849fae2e7783ead0475b7