sonic-buildimage/device/juniper/x86_64-juniper_qfx5200-r0/plugins/led_control.py
Joe LeVeque 7f4ab8fbd8
[sonic-utilities] Update submodule; Build and install as a Python 3 wheel (#5926)
Submodule updates include the following commits:

* src/sonic-utilities 9dc58ea...f9eb739 (18):
  > Remove unnecessary calls to str.encode() now that the package is Python 3; Fix deprecation warning (#1260)
  > [generate_dump] Ignoring file/directory not found Errors (#1201)
  > Fixed porstat rate and util issues (#1140)
  > fix error: interface counters is mismatch after warm-reboot (#1099)
  > Remove unnecessary calls to str.decode() now that the package is Python 3 (#1255)
  > [acl-loader] Make list sorting compliant with Python 3 (#1257)
  > Replace hard-coded fast-reboot with variable. And some typo corrections (#1254)
  > [configlet][portconfig] Remove calls to dict.has_key() which is not available in Python 3 (#1247)
  > Remove unnecessary conversions to list() and calls to dict.keys() (#1243)
  > Clean up LGTM alerts (#1239)
  > Add 'requests' as install dependency in setup.py (#1240)
  > Convert to Python 3 (#1128)
  > Fix mock SonicV2Connector in python3: use decode_responses mode so caller code will be the same as python2 (#1238)
  > [tests] Do not trim from PATH if we did not append to it; Clean up/fix shebangs in scripts (#1233)
  > Updates to bgp config and show commands with BGP_INTERNAL_NEIGHBOR table (#1224)
  > [cli]: NAT show commands newline issue after migrated to Python3 (#1204)
  > [doc]: Update Command-Reference.md (#1231)
  > Added 'import sys' in feature.py file (#1232)

* src/sonic-py-swsssdk 9d9f0c6...1664be9 (2):
  > Fix: no need to decode() after redis client scan, so it will work for both python2 and python3 (#96)
  > FieldValueMap `contains`(`in`)  will also work when migrated to libswsscommon(C++ with SWIG wrapper) (#94)

- Also fix Python 3-related issues:
    - Use integer (floor) division in config_samples.py (sonic-config-engine)
    - Replace print statement with print function in eeprom.py plugin for x86_64-kvm_x86_64-r0 platform
    - Update all platform plugins to be compatible with both Python 2 and Python 3
    - Remove shebangs from plugins files which are not intended to be executable
    - Replace tabs with spaces in Python plugin files and fix alignment, because Python 3 is more strict
    - Remove trailing whitespace from plugins files
2020-11-25 10:28:36 -08:00

185 lines
6.4 KiB
Python

#
# Name: led_control.py version: 1.0
#
# Description: Platform-specific LED control functionality for Juniper QFX5200
#
# Copyright (c) 2020, Juniper Networks, Inc.
# All rights reserved.
#
# Notice and Disclaimer: This code is licensed to you under the GNU General
# Public License as published by the Free Software Foundation, version 3 or
# any later version. This code is not an official Juniper product. You can
# obtain a copy of the License at <https://www.gnu.org/licenses/>
#
# OSS License:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Third-Party Code: This code may depend on other components under separate
# copyright notice and license terms. Your use of the source code for those
# components is subject to the terms and conditions of the respective license
# as noted in the Third-Party source code file.
try:
from sonic_led.led_control_base import LedControlBase
import os
import syslog
import glob
from socket import *
from select import *
except ImportError as e:
raise ImportError(str(e) + " - required module not found")
def DBG_PRINT(str):
syslog.openlog("ledi_control")
syslog.syslog(syslog.LOG_INFO, str)
syslog.closelog()
class LedControl(LedControlBase):
"""Platform specific LED control class"""
SONIC_PORT_NAME_PREFIX = "Ethernet"
LED_MODE_OFF = 0
LED_MODE_GREEN = 1
PORT_START = 0
PORT_END = 127
port_to_gpio_pin_mapping = {}
GPIO_SLAVE0_PORT_START = 0
GPIO_SLAVE0_PORT_END = 63
GPIO_SLAVE1_PORT_START = 64
GPIO_SLAVE1_PORT_END = 127
GPIO_LANE0_PORT_LED_OFFSET = 64
GPIO_LANE1_PORT_LED_OFFSET = 80
GPIO_LANE2_PORT_LED_OFFSET = 96
GPIO_LANE3_PORT_LED_OFFSET = 112
# Turn OFF all port leds during init
def gpio_create_file(self, gpio_pin):
gpio_export_path = "/sys/class/gpio/export"
gpio_pin_path = "/sys/class/gpio/gpio" + str(gpio_pin)
if not os.path.exists(gpio_pin_path):
try:
gpio_export_file = open(gpio_export_path, 'w')
gpio_export_file.write(str(gpio_pin))
gpio_export_file.close()
except IOError as e:
print("Error: unable to open export file: %s" % str(e))
return False
return True
def gpio_port_led_init(self, gpio_base, port):
port_led_pin = gpio_base + self.GPIO_LANE0_PORT_LED_OFFSET + 16*(port % 4) + ((port % 64)/4)
if self.gpio_create_file(port_led_pin):
self.port_to_gpio_pin_mapping[port] = port_led_pin
def gpio_port_led_slave_init(self, gpio_base_path, gpio_port_start, gpio_port_end):
flist = glob.glob(gpio_base_path)
if len(flist) == 1:
try:
fp = open(flist[0] + "/base")
gpio_base = int(fp.readline().rstrip())
except IOError as e:
print("Error: unable to open file: %s" % str(e))
return
for port in range(gpio_port_start, gpio_port_end + 1):
self.gpio_port_led_init(gpio_base, port)
def gpio_port_led_base_init(self):
self.gpio_port_led_slave_init("/sys/bus/platform/drivers/gpioslave-tmc/gpioslave-tmc.22/gpio/gpio*",
self.GPIO_SLAVE0_PORT_START, self.GPIO_SLAVE0_PORT_END)
self.gpio_port_led_slave_init("/sys/bus/platform/drivers/gpioslave-tmc/gpioslave-tmc.21/gpio/gpio*",
self.GPIO_SLAVE1_PORT_START, self.GPIO_SLAVE1_PORT_END)
# Write driver for port led
def gpio_led_write(self, gpio_pin, value):
success = False
gpio_pin_path = "/sys/class/gpio/gpio" + str(gpio_pin)
try:
gpio_file = open(gpio_pin_path + "/value", 'w')
gpio_file.write(str(value))
success = True
except IOError as e:
print("error: unable to open file: %s" % str(e))
return success
# Read driver for port led
def gpio_led_read(self, gpio_pin):
gpio_pin_path = "/sys/class/gpio/gpio" + str(gpio_pin)
value = 0
try:
reg_file = open(gpio_pin_path + "/value")
value = int(reg_file.readline().rstrip())
except IOError as e:
print("error: unable to open file: %s" % str(e))
return value
def _initDefaultConfig(self):
DBG_PRINT("start init led")
for port in range(self.PORT_START, self.PORT_END):
self._port_led_mode_update(self.port_to_gpio_pin_mapping[port], self.LED_MODE_OFF)
DBG_PRINT("init led done")
# Helper method to map SONiC port name to index
def _port_name_to_index(self, port_name):
# Strip "Ethernet" off port name
if not port_name.startswith(self.SONIC_PORT_NAME_PREFIX):
return -1
port_idx = int(port_name[len(self.SONIC_PORT_NAME_PREFIX):])
return port_idx
# Convert state up/down to Green/OFF which is 1/0
def _port_state_to_mode(self, state):
if state == "up":
return self.LED_MODE_GREEN
else:
return self.LED_MODE_OFF
# Set the port led mode to Green/OFF
def _port_led_mode_update(self, gpio_pin, ledMode):
if ledMode == self.LED_MODE_GREEN:
self.gpio_led_write(gpio_pin, 1)
else:
self.gpio_led_write(gpio_pin, 0)
# Concrete implementation of port_link_state_change() method
def port_link_state_change(self, portname, state):
port_idx = self._port_name_to_index(portname)
gpio_pin = self.port_to_gpio_pin_mapping[port_idx]
ledMode = self._port_state_to_mode(state)
saveMode = self.gpio_led_read(gpio_pin)
if ledMode == saveMode:
return
self._port_led_mode_update(gpio_pin, ledMode)
DBG_PRINT("update {} led mode from {} to {}".format(portname, saveMode, ledMode))
# Constructor
def __init__(self):
self.gpio_port_led_base_init()
self._initDefaultConfig()