sonic-buildimage/device/juniper/x86_64-juniper_qfx5200-r0/plugins/qfx5200_sfp_init.py
Joe LeVeque 3b89e5d467
[Python] Migrate applications/scripts to import sonic-py-common package (#5043)
As part of consolidating all common Python-based functionality into the new sonic-py-common package, this pull request:
1. Redirects all Python applications/scripts in sonic-buildimage repo which previously imported sonic_device_util or sonic_daemon_base to instead import sonic-py-common, which was added in https://github.com/Azure/sonic-buildimage/pull/5003
2. Replaces all calls to `sonic_device_util.get_platform_info()` to instead call `sonic_py_common.get_platform()` and removes any calls to `sonic_device_util.get_machine_info()` which are no longer necessary (i.e., those which were only used to pass the results to `sonic_device_util.get_platform_info()`.
3. Removes unused imports to the now-deprecated sonic-daemon-base package and sonic_device_util.py module

This is the next step toward resolving https://github.com/Azure/sonic-buildimage/issues/4999

Also reverted my previous change in which device_info.get_platform() would first try obtaining the platform ID string from Config DB and fall back to gathering it from machine.conf upon failure because this function is called by sonic-cfggen before the data is in the DB, in which case, the db_connect() call will hang indefinitely, which was not the behavior I expected. As of now, the function will always reference machine.conf.
2020-08-03 11:43:12 -07:00

103 lines
3.5 KiB
Python

#!/usr/bin/env python
#
# Name: juniper_sfp_init.py version: 1.0
#
# Description: Platform-specific SFP Transceiver Initialization 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.
import time
import os.path
import sfputil as jnpr_sfp
from sonic_py_common.logger import Logger
from pprint import pprint
SYSLOG_IDENTIFIER = "sfputil"
# Global logger class instance
logger = Logger(SYSLOG_IDENTIFIER)
DEBUG = False
def i2c_eeprom_dev_update(port, create_eeprom):
eeprom_path = "/sys/class/i2c-adapter/i2c-{0}/{0}-0050/eeprom"
i2c_path = "/sys/class/i2c-adapter/i2c-{0}"
i2c_port = port + jnpr_sfp.SFP_I2C_OFFSET
port_eeprom_path = eeprom_path.format(i2c_port)
port_i2c_path = i2c_path.format(i2c_port)
if create_eeprom:
if not os.path.exists(port_eeprom_path):
try:
i2c_file = open(port_i2c_path + "/new_device", "w")
i2c_file.write("optoe2 0x50")
except IOError as e:
print "Error: unable to write to i2c file: %s" % str(e)
return
else:
if os.path.exists(port_eeprom_path):
try:
i2c_file = open(port_i2c_path + "/delete_device", "w")
i2c_file.write("0x50")
except IOError as e:
print "Error: unable to write to i2c file: %s" % str(e)
return
def gpio_sfp_init():
jnpr_sfp.gpio_sfp_base_init()
time.sleep(2)
#Reset all ports
for port in range(jnpr_sfp.GPIO_PORT_START, jnpr_sfp.GPIO_PORT_END + 1):
logger.log_debug("GPIO SFP port {}".format(port))
jnpr_sfp.gpio_sfp_reset_set(port, 0)
i2c_eeprom_dev_update(port, True)
time.sleep(1)
#Enable optics for all ports which have XCVRs present
for port in range(jnpr_sfp.GPIO_PORT_START, jnpr_sfp.GPIO_PORT_END + 1):
jnpr_sfp.gpio_sfp_lpmode_set(port, 1)
if __name__ == '__main__':
if DEBUG == True:
print "Initializing Juniper SFP module"
gpio_sfp_init()
if DEBUG == True:
print "Juniper GPIO presence pin mapping:"
pprint(jnpr_sfp.gpio_sfp_presence)
print "Juniper GPIO reset pin mapping:"
pprint(jnpr_sfp.gpio_sfp_reset)
print "Juniper GPIO lpmode pin mapping:"
pprint(jnpr_sfp.gpio_sfp_lpmode)