Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PDDF] Remove references to deprecated platform plugins #1485

Merged
merged 1 commit into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 80 additions & 162 deletions pddf_fanutil/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,229 +16,139 @@

VERSION = '2.0'

SYSLOG_IDENTIFIER = "fanutil"
PLATFORM_SPECIFIC_MODULE_NAME = "fanutil"
PLATFORM_SPECIFIC_CLASS_NAME = "FanUtil"
ERROR_PERMISSIONS = 1
ERROR_CHASSIS_LOAD = 2
ERROR_NOT_IMPLEMENTED = 3
ERROR_PDDF_NOT_SUPPORTED = 4

# Global platform-specific fanutil class instance
platform_fanutil = None
# Global platform-specific chassis class instance
platform_chassis = None

# Load the helper class
helper = UtilHelper()

def _wrapper_get_num_fans():
if platform_chassis is not None:
try:
return platform_chassis.get_num_fans()
except NotImplementedError:
pass
return platform_fanutil.get_num_fans()

def _wrapper_get_fan_name(idx):
if platform_chassis is not None:
try:
return platform_chassis.get_fan(idx-1).get_name()
except NotImplementedError:
pass
return "FAN {}".format(idx)

def _wrapper_get_fan_presence(idx):
if platform_chassis is not None:
try:
return platform_chassis.get_fan(idx-1).get_presence()
except NotImplementedError:
pass
return platform_fanutil.get_presence(idx)

def _wrapper_get_fan_status(idx):
if platform_chassis is not None:
try:
return platform_chassis.get_fan(idx-1).get_status()
except NotImplementedError:
pass
return platform_fanutil.get_status(idx)

def _wrapper_get_fan_direction(idx):
if platform_chassis is not None:
try:
return platform_chassis.get_fan(idx-1).get_direction()
except NotImplementedError:
pass
return platform_fanutil.get_direction(idx)

def _wrapper_get_fan_speed(idx):
if platform_chassis is not None:
try:
return platform_chassis.get_fan(idx-1).get_speed_rpm()
except NotImplementedError:
pass
return platform_fanutil.get_speed(idx)

def _wrapper_get_fan_speed_rear(idx):
if platform_chassis is not None:
# This wrapper API is invalid for Pl API 2.0 as every fan
# is treated as a separate fan
return 0
return platform_fanutil.get_speed_rear(idx)

def _wrapper_set_fan_speed(idx, percent):
if platform_chassis is not None:
try:
return platform_chassis.get_fan(idx-1).set_speed(percent)
except NotImplementedError:
pass
return platform_fanutil.set_speed(percent)

def _wrapper_dump_sysfs(idx):
if platform_chassis is not None:
try:
return platform_chassis.get_fan(idx).dump_sysfs()
except NotImplementedError:
pass
return platform_fanutil.dump_sysfs()
# ==================== CLI commands and groups ====================


# This is our main entrypoint - the main 'fanutil' command
# This is our main entrypoint - the main 'pddf_fanutil' command
@click.group()
def cli():
"""pddf_fanutil - Command line utility for providing FAN information"""

global platform_fanutil
global platform_chassis

if os.geteuid() != 0:
click.echo("Root privileges are required for this operation")
sys.exit(1)

# Load the helper class
helper = UtilHelper()
sys.exit(ERROR_PERMISSIONS)

if not helper.check_pddf_mode():
click.echo("PDDF mode should be supported and enabled for this platform for this operation")
sys.exit(1)

# Load new platform api class
try:
import sonic_platform.platform
platform_chassis = sonic_platform.platform.Platform().get_chassis()
except Exception as e:
click.echo("Failed to load chassis due to {}".format(str(e)))
sys.exit(ERROR_PDDF_NOT_SUPPORTED)

# Load platform-specific chassis 2.0 api class
platform_chassis = helper.load_platform_chassis()
if not platform_chassis:
sys.exit(ERROR_CHASSIS_LOAD)

# Load platform-specific fanutil class if new platform object class is not found
if platform_chassis is None:
try:
platform_fanutil = helper.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME)
except Exception as e:
click.echo("Failed to load {}: {}".format(PLATFORM_SPECIFIC_MODULE_NAME, str(e)))
sys.exit(2)

# 'version' subcommand
@cli.command()
def version():
"""Display version info"""
click.echo("PDDF fanutil version {0}".format(VERSION))


# 'numfans' subcommand
@cli.command()
def numfans():
"""Display number of FANs installed on device"""
click.echo(_wrapper_get_num_fans())
num_fans = platform_chassis.get_num_fans()
click.echo(num_fans)


# 'status' subcommand
@cli.command()
@click.option('-i', '--index', default=-1, type=int, help="the index of FAN")
@click.option('-i', '--index', default=-1, type=int, help="Index of FAN (1-based)")
def status(index):
"""Display FAN status"""
supported_fan = list(range(1, _wrapper_get_num_fans()+1))
fan_ids = []
fan_list = []
if (index < 0):
fan_ids = supported_fan
fan_list = platform_chassis.get_all_fans()
default_index = 0
else:
fan_ids = [index]
fan_list = platform_chassis.get_fan(index-1)
default_index = index-1

header = ['FAN', 'Status']
status_table = []

for fan in fan_ids:
msg = ""
fan_name = _wrapper_get_fan_name(fan)
if fan not in supported_fan:
click.echo("Error! The {} is not available on the platform.\n" \
"Number of supported FAN - {}.".format(fan_name, len(supported_fan)))
continue
presence = _wrapper_get_fan_presence(fan)
if presence:
oper_status = _wrapper_get_fan_status(fan)
msg = 'OK' if oper_status else "NOT OK"
else:
msg = 'NOT PRESENT'
status_table.append([fan_name, msg])
for idx, fan in enumerate(fan_list, default_index):
fan_name = helper.try_get(fan.get_name, "Fan {}".format(idx+1))
status = 'NOT PRESENT'
if fan.get_presence():
oper_status = helper.try_get(fan.get_status, 'UNKNOWN')
if oper_status is True:
status = 'OK'
elif oper_status is False:
status = 'NOT OK'
else:
status = oper_status

status_table.append([fan_name, status])

if status_table:
click.echo(tabulate(status_table, header, tablefmt="simple"))


# 'direction' subcommand
@cli.command()
@click.option('-i', '--index', default=-1, type=int, help="the index of FAN")
@click.option('-i', '--index', default=-1, type=int, help="Index of FAN (1-based)")
def direction(index):
"""Display FAN airflow direction"""
supported_fan = list(range(1, _wrapper_get_num_fans() + 1))
fan_ids = []
fan_list = []
if (index < 0):
fan_ids = supported_fan
fan_list = platform_chassis.get_all_fans()
default_index = 0
else:
fan_ids = [index]
fan_list = platform_chassis.get_fan(index-1)
default_index = index-1

header = ['FAN', 'Direction']
status_table = []
dir_table = []

for fan in fan_ids:
fan_name = _wrapper_get_fan_name(fan)
if fan not in supported_fan:
click.echo("Error! The {} is not available on the platform.\n" \
"Number of supported FAN - {}.".format(fan_name, len(supported_fan)))
continue
direction = _wrapper_get_fan_direction(fan)
status_table.append([fan_name, direction.capitalize()])
for idx, fan in enumerate(fan_list, default_index):
fan_name = helper.try_get(fan.get_name, "Fan {}".format(idx+1))
direction = helper.try_get(fan.get_direction, 'N/A')
dir_table.append([fan_name, direction.capitalize()])

if dir_table:
click.echo(tabulate(dir_table, header, tablefmt="simple"))

if status_table:
click.echo(tabulate(status_table, header, tablefmt="simple"))

# 'speed' subcommand
@cli.command()
@click.option('-i', '--index', default=-1, type=int, help="the index of FAN")
@click.option('-i', '--index', default=-1, type=int, help="Index of FAN (1-based)")
def getspeed(index):
"""Display FAN speed in RPM"""
supported_fan = list(range(1, _wrapper_get_num_fans() + 1))
fan_ids = []
fan_list = []
if (index < 0):
fan_ids = supported_fan
fan_list = platform_chassis.get_all_fans()
default_index = 0
else:
fan_ids = [index]
fan_list = platform_chassis.get_fan(index-1)
default_index = index-1

if platform_chassis is not None:
header = ['FAN', 'SPEED (RPM)']
else:
header = ['FAN', 'Front Fan RPM', 'Rear Fan RPM']

status_table = []
header = ['FAN', 'SPEED (RPM)']
speed_table = []

for fan in fan_ids:
fan_name = _wrapper_get_fan_name(fan)
if fan not in supported_fan:
click.echo("Error! The {} is not available on the platform.\n" \
"Number of supported FAN - {}.".format(fan_name, len(supported_fan)))
continue
front = _wrapper_get_fan_speed(fan)
rear = _wrapper_get_fan_speed_rear(fan)
for idx, fan in enumerate(fan_list, default_index):
fan_name = helper.try_get(fan.get_name, "Fan {}".format(idx+1))
rpm = helper.try_get(fan.get_speed_rpm, 'N/A')
speed_table.append([fan_name, rpm])

if platform_chassis is not None:
status_table.append([fan_name, front])
else:
status_table.append([fan_name, front, rear])
if speed_table:
click.echo(tabulate(speed_table, header, tablefmt="simple"))

if status_table:
click.echo(tabulate(status_table, header, tablefmt="simple"))

# 'setspeed' subcommand
@cli.command()
Expand All @@ -249,30 +159,38 @@ def setspeed(speed):
click.echo("speed value is required")
raise click.Abort()

for fan in range(_wrapper_get_num_fans()):
status = _wrapper_set_fan_speed(fan, speed)
fan_list = platform_chassis.get_all_fans()
for idx, fan in enumerate(fan_list):
try:
status = fan.set_speed(speed)
except NotImplementedError:
click.echo("Set speed API not implemented")
sys.exit(0)

if not status:
click.echo("Failed")
sys.exit(1)

click.echo("Successful")


@cli.group()
def debug():
"""pddf_fanutil debug commands"""
pass


@debug.command()
def dump_sysfs():
"""Dump all Fan related SysFS paths"""
for fan in range(_wrapper_get_num_fans()):
status = _wrapper_dump_sysfs(fan)
fan_list = platform_chassis.get_all_fans()
for idx, fan in enumerate(fan_list):
status = fan.dump_sysfs()

if status:
for i in status:
click.echo(i)



if __name__ == '__main__':
cli()
Loading