sonic-buildimage/device/barefoot/x86_64-accton_wedge100bf_32x-r0/plugins/sfputil.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

251 lines
7.6 KiB
Python

try:
import os
import sys
import importlib
import time
sys.path.append(os.path.dirname(__file__))
import pltfm_mgr_rpc
from pltfm_mgr_rpc.ttypes import *
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.protocol import TMultiplexedProtocol
from sonic_sfp.sfputilbase import SfpUtilBase
except ImportError as e:
raise ImportError(str(e) + "- required module not found")
thrift_server = 'localhost'
transport = None
pltfm_mgr = None
SFP_EEPROM_CACHE = "/var/run/platform/sfp/cache"
class SfpUtil(SfpUtilBase):
"""Platform-specific SfpUtil class"""
PORT_START = 1
PORT_END = 0
PORTS_IN_BLOCK = 0
QSFP_PORT_START = 1
QSFP_PORT_END = 0
EEPROM_OFFSET = 0
QSFP_CHECK_INTERVAL = 4
THRIFT_RETRIES = 5
THRIFT_TIMEOUT = 5
@property
def port_start(self):
self.update_port_info()
return self.PORT_START
@property
def port_end(self):
self.update_port_info()
return self.PORT_END
@property
def qsfp_ports(self):
self.update_port_info()
return list(range(self.QSFP_PORT_START, self.PORTS_IN_BLOCK + 1))
@property
def port_to_eeprom_mapping(self):
print("dependency on sysfs has been removed")
raise Exception()
def __init__(self):
self.ready = False
self.phy_port_dict = {'-1': 'system_not_ready'}
self.phy_port_cur_state = {}
self.qsfp_interval = self.QSFP_CHECK_INTERVAL
if not os.path.exists(os.path.dirname(SFP_EEPROM_CACHE)):
try:
os.makedirs(os.path.dirname(SFP_EEPROM_CACHE))
except OSError as e:
if e.errno != errno.EEXIST:
raise
open(SFP_EEPROM_CACHE, 'ab').close()
SfpUtilBase.__init__(self)
def update_port_info(self):
global pltfm_mgr
if self.QSFP_PORT_END == 0:
self.thrift_setup()
self.QSFP_PORT_END = pltfm_mgr.pltfm_mgr_qsfp_get_max_port()
self.PORT_END = self.QSFP_PORT_END
self.PORTS_IN_BLOCK = self.QSFP_PORT_END
self.thrift_teardown()
def thrift_setup(self):
global thrift_server, transport, pltfm_mgr
transport = TSocket.TSocket(thrift_server, 9090)
transport = TTransport.TBufferedTransport(transport)
bprotocol = TBinaryProtocol.TBinaryProtocol(transport)
pltfm_mgr_client_module = importlib.import_module(".".join(["pltfm_mgr_rpc", "pltfm_mgr_rpc"]))
pltfm_mgr_protocol = TMultiplexedProtocol.TMultiplexedProtocol(bprotocol, "pltfm_mgr_rpc")
pltfm_mgr = pltfm_mgr_client_module.Client(pltfm_mgr_protocol)
for i in range(self.THRIFT_RETRIES):
try:
transport.open()
if i:
# The main thrift server is starded without platform api
# Platform api is added later during syncd initialization
# So we need to wait a little bit before do any platform api call
# Just in case when can't connect from the first try (warm-reboot case)
time.sleep(self.THRIFT_TIMEOUT)
break
except TTransport.TTransportException as e:
if e.type != TTransport.TTransportException.NOT_OPEN or i >= self.THRIFT_RETRIES - 1:
raise e
time.sleep(self.THRIFT_TIMEOUT)
def thrift_teardown(self):
global transport
transport.close()
def get_presence(self, port_num):
# Check for invalid port_num
if port_num < self.port_start or port_num > self.port_end:
return False
presence = False
try:
self.thrift_setup()
presence = pltfm_mgr.pltfm_mgr_qsfp_presence_get(port_num)
self.thrift_teardown()
except Exception as e:
print(e.__doc__)
print(e.message)
return presence
def get_low_power_mode(self, port_num):
# Check for invalid port_num
if port_num < self.port_start or port_num > self.port_end:
return False
self.thrift_setup()
lpmode = pltfm_mgr.pltfm_mgr_qsfp_lpmode_get(port_num)
self.thrift_teardown()
return lpmode
def set_low_power_mode(self, port_num, lpmode):
# Check for invalid port_num
if port_num < self.port_start or port_num > self.port_end:
return False
self.thrift_setup()
status = pltfm_mgr.pltfm_mgr_qsfp_lpmode_set(port_num, lpmode)
self.thrift_teardown()
return (status == 0)
def reset(self, port_num):
# Check for invalid port_num
if port_num < self.port_start or port_num > self.port_end:
return False
self.thrift_setup()
status = pltfm_mgr.pltfm_mgr_qsfp_reset(port_num, True)
status = pltfm_mgr.pltfm_mgr_qsfp_reset(port_num, False)
self.thrift_teardown()
return status
def check_transceiver_change(self):
if not self.ready:
return
self.phy_port_dict = {}
try:
self.thrift_setup()
except:
return
# Get presence of each SFP
for port in range(self.port_start, self.port_end + 1):
try:
sfp_resent = pltfm_mgr.pltfm_mgr_qsfp_presence_get(port)
except:
sfp_resent = False
sfp_state = '1' if sfp_resent else '0'
if port in self.phy_port_cur_state:
if self.phy_port_cur_state[port] != sfp_state:
self.phy_port_dict[port] = sfp_state
else:
self.phy_port_dict[port] = sfp_state
# Update port current state
self.phy_port_cur_state[port] = sfp_state
self.thrift_teardown()
def get_transceiver_change_event(self, timeout=0):
forever = False
if timeout == 0:
forever = True
elif timeout > 0:
timeout = timeout / float(1000) # Convert to secs
else:
print("get_transceiver_change_event:Invalid timeout value", timeout)
return False, {}
while forever or timeout > 0:
if not self.ready:
try:
self.thrift_setup()
self.thrift_teardown()
except:
pass
else:
self.ready = True
self.phy_port_dict = {}
break
elif self.qsfp_interval == 0:
self.qsfp_interval = self.QSFP_CHECK_INTERVAL
# Process transceiver plug-in/out event
self.check_transceiver_change()
# Break if tranceiver state has changed
if bool(self.phy_port_dict):
break
if timeout:
timeout -= 1
if self.qsfp_interval:
self.qsfp_interval -= 1
time.sleep(1)
return self.ready, self.phy_port_dict
def _get_port_eeprom_path(self, port_num, devid):
eeprom_path = None
self.thrift_setup()
presence = pltfm_mgr.pltfm_mgr_qsfp_presence_get(port_num)
if presence == True:
eeprom_cache = open(SFP_EEPROM_CACHE, 'wb')
eeprom_hex = pltfm_mgr.pltfm_mgr_qsfp_info_get(port_num)
eeprom_raw = bytearray.fromhex(eeprom_hex)
eeprom_cache.write(eeprom_raw)
eeprom_cache.close()
eeprom_path = SFP_EEPROM_CACHE
self.thrift_teardown()
return eeprom_path