From b89eb6d70116b693b8443c964aa7c35f6de11c63 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Tue, 15 Jan 2019 11:31:58 +0200 Subject: [PATCH] [mellanox] Implement PSU related APIs based on the new platform API * Implement part of PSU related APIs including get_status(), get_presence() Signed-off-by: Kevin Wang --- platform/mellanox/mlnx-platform-api.mk | 7 ++ platform/mellanox/mlnx-platform-api/setup.py | 30 ++++++++ .../sonic_platform_api/__init__.py | 0 .../sonic_platform_api/chassis.py | 28 ++++++++ .../sonic_platform_api/psu.py | 70 +++++++++++++++++++ platform/mellanox/rules.mk | 1 + rules/docker-platform-monitor.mk | 1 + 7 files changed, 137 insertions(+) create mode 100644 platform/mellanox/mlnx-platform-api.mk create mode 100644 platform/mellanox/mlnx-platform-api/setup.py create mode 100644 platform/mellanox/mlnx-platform-api/sonic_platform_api/__init__.py create mode 100644 platform/mellanox/mlnx-platform-api/sonic_platform_api/chassis.py create mode 100644 platform/mellanox/mlnx-platform-api/sonic_platform_api/psu.py diff --git a/platform/mellanox/mlnx-platform-api.mk b/platform/mellanox/mlnx-platform-api.mk new file mode 100644 index 000000000000..98a57d0090ed --- /dev/null +++ b/platform/mellanox/mlnx-platform-api.mk @@ -0,0 +1,7 @@ +# SONIC_PLATFORM_API_PY2 package + +SONIC_PLATFORM_API_PY2 = mlnx_platform_api-1.0-py2-none-any.whl +$(SONIC_PLATFORM_API_PY2)_SRC_PATH = $(PLATFORM_PATH)/mlnx-platform-api +$(SONIC_PLATFORM_API_PY2)_PYTHON_VERSION = 2 +SONIC_PYTHON_WHEELS += $(SONIC_PLATFORM_API_PY2) + diff --git a/platform/mellanox/mlnx-platform-api/setup.py b/platform/mellanox/mlnx-platform-api/setup.py new file mode 100644 index 000000000000..3adddd29057a --- /dev/null +++ b/platform/mellanox/mlnx-platform-api/setup.py @@ -0,0 +1,30 @@ +from setuptools import setup + +setup( + name='mlnx-platform-api', + version='1.0', + description='SONiC platform API implementation on Mellanox platform', + license='Apache 2.0', + author='SONiC Team', + author_email='linuxnetdev@microsoft.com', + url='https://github.com/Azure/sonic-buildimage', + maintainer='Kevin Wang', + maintainer_email='kevinw@mellanox.com', + packages=[ + 'sonic_platform_api', + ], + classifiers=[ + 'Development Status :: 3 - Alpha', + 'Environment :: Plugins', + 'Intended Audience :: Developers', + 'Intended Audience :: Information Technology', + 'Intended Audience :: System Administrators', + 'License :: OSI Approved :: Apache Software License', + 'Natural Language :: English', + 'Operating System :: POSIX :: Linux', + 'Programming Language :: Python :: 2.7', + 'Topic :: Utilities', + ], + keywords='sonic SONiC platform-api PLATFORM-API', +) + diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform_api/__init__.py b/platform/mellanox/mlnx-platform-api/sonic_platform_api/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform_api/chassis.py b/platform/mellanox/mlnx-platform-api/sonic_platform_api/chassis.py new file mode 100644 index 000000000000..398a3a553762 --- /dev/null +++ b/platform/mellanox/mlnx-platform-api/sonic_platform_api/chassis.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +############################################################################# +# Mellanox +# +# Module contains an implementation of SONiC Platform Base API and +# provides the Chassis information which are available in the platform +# +############################################################################# + +import sys + +try: + from sonic_platform_base.chassis_base import ChassisBase + from sonic_platform_api.psu import Psu +except ImportError as e: + raise ImportError (str(e) + "- required module not found") + +MLNX_NUM_PSU = 2 + +class Chassis(ChassisBase): + """Platform-specific Chassis class""" + def __init__(self): + ChassisBase.__init__(self) + for index in range(MLNX_NUM_PSU): + psu = Psu(index) + self._psu_list.append(psu) + diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform_api/psu.py b/platform/mellanox/mlnx-platform-api/sonic_platform_api/psu.py new file mode 100644 index 000000000000..073a217f3bb4 --- /dev/null +++ b/platform/mellanox/mlnx-platform-api/sonic_platform_api/psu.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +############################################################################# +# Mellanox +# +# Module contains an implementation of SONiC Platform Base API and +# provides the PSUs status which are available in the platform +# +############################################################################# + +import os.path + +try: + from sonic_platform_base.psu_base import PsuBase +except ImportError as e: + raise ImportError (str(e) + "- required module not found") + +psu_list = [] + +class Psu(PsuBase): + """Platform-specific Psu class""" + def __init__(self, psu_index): + global psu_list + PsuBase.__init__(self) + # PSU is 1-based on Mellanox platform + self.index = psu_index + 1 + psu_list.append(psu_index) + self.psu_path = "/var/run/hw-management/thermal/" + self.psu_oper_status = "psu{}_pwr_status".format(self.index) + self.psu_presence = "psu{}_status".format(self.index) + if os.path.exists(os.path.join(self.psu_path, self.psu_presence)): + self.presence_file_exists = True + else: + self.presence_file_exists = False + + def get_status(self): + """ + Retrieves the operational status of power supply unit (PSU) defined + + Returns: + bool: True if PSU is operating properly, False if not + """ + status = 0 + try: + with open(os.path.join(self.psu_path, self.psu_oper_status), 'r') as power_status: + status = int(power_status.read()) + except (ValueError, IOError): + status = 0 + + return status == 1 + + def get_presence(self): + """ + Retrieves the presence status of power supply unit (PSU) defined + + Returns: + bool: True if PSU is present, False if not + """ + status = 0 + if self.presence_file_exists: + try: + with open(os.path.join(self.psu_path, self.psu_presence), 'r') as presence_status: + status = int(presence_status.read()) + except (ValueError, IOError): + status = 0 + else: + status = self.index in psu_list + + return status == 1 + diff --git a/platform/mellanox/rules.mk b/platform/mellanox/rules.mk index 28414b7f9d9f..3f0e24d20d80 100644 --- a/platform/mellanox/rules.mk +++ b/platform/mellanox/rules.mk @@ -3,6 +3,7 @@ include $(PLATFORM_PATH)/fw.mk include $(PLATFORM_PATH)/mft.mk include $(PLATFORM_PATH)/mlnx-sai.mk include $(PLATFORM_PATH)/hw-management.mk +include $(PLATFORM_PATH)/mlnx-platform-api.mk include $(PLATFORM_PATH)/docker-syncd-mlnx.mk include $(PLATFORM_PATH)/docker-syncd-mlnx-rpc.mk include $(PLATFORM_PATH)/docker-orchagent-mlnx.mk diff --git a/rules/docker-platform-monitor.mk b/rules/docker-platform-monitor.mk index d6404e18c47f..29829482addd 100644 --- a/rules/docker-platform-monitor.mk +++ b/rules/docker-platform-monitor.mk @@ -5,6 +5,7 @@ $(DOCKER_PLATFORM_MONITOR)_PATH = $(DOCKERS_PATH)/docker-platform-monitor $(DOCKER_PLATFORM_MONITOR)_DEPENDS += $(LIBSWSSCOMMON) $(PYTHON_SWSSCOMMON) $(SONIC_LEDD) $(SONIC_XCVRD) $(DOCKER_PLATFORM_MONITOR)_PYTHON_WHEELS += $(SONIC_PLATFORM_COMMON_PY2) $(DOCKER_PLATFORM_MONITOR)_PYTHON_WHEELS += $(SWSSSDK_PY2) +$(DOCKER_PLATFORM_MONITOR)_PYTHON_WHEELS += $(SONIC_PLATFORM_API_PY2) $(DOCKER_PLATFORM_MONITOR)_LOAD_DOCKERS = $(DOCKER_CONFIG_ENGINE) SONIC_DOCKER_IMAGES += $(DOCKER_PLATFORM_MONITOR)