-
Notifications
You must be signed in to change notification settings - Fork 672
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
[PoE] Add PoE CLI #3436
Open
SerhiyBoikoPLV
wants to merge
1
commit into
sonic-net:master
Choose a base branch
from
SerhiyBoikoPLV:feat_poe
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[PoE] Add PoE CLI #3436
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
""" | ||
Config CLI plugin for the SONiC Power over Ethernet feature. | ||
This file is auto-generated by sonic-cli-gen tool but adjusted to meet PoE HLD requirenments. | ||
""" | ||
|
||
import copy | ||
import click | ||
import utilities_common.cli as clicommon | ||
import utilities_common.general as general | ||
from config import config_mgmt | ||
|
||
|
||
# Load sonic-cfggen from source since /usr/local/bin/sonic-cfggen does not have .py extension. | ||
sonic_cfggen = general.load_module_from_source('sonic_cfggen', '/usr/local/bin/sonic-cfggen') | ||
|
||
|
||
POE_PORT = 'POE_PORT' | ||
|
||
|
||
def _exit_with_error(*args, **kwargs): | ||
""" Print a message with click.secho and abort CLI. | ||
|
||
Args: | ||
args: Positional arguments to pass to click.secho | ||
kwargs: Keyword arguments to pass to click.secho | ||
""" | ||
|
||
click.secho(*args, **kwargs) | ||
raise click.Abort() | ||
|
||
|
||
def _validate_config_or_raise(cfg): | ||
""" Validate config db data using ConfigMgmt. | ||
|
||
Args: | ||
cfg (Dict): Config DB data to validate. | ||
Raises: | ||
Exception: when cfg does not satisfy YANG schema. | ||
""" | ||
|
||
try: | ||
cfg = sonic_cfggen.FormatConverter.to_serialized(copy.deepcopy(cfg)) | ||
config_mgmt.ConfigMgmt().loadData(cfg) | ||
except Exception as err: | ||
raise Exception('Failed to validate configuration: {}'.format(err)) | ||
|
||
|
||
def _update_entry_validated(db, table, key, data, create_if_not_exists=False): | ||
""" Update entry in table and validate configuration. | ||
If attribute value in data is None, the attribute is deleted. | ||
|
||
Args: | ||
db (swsscommon.ConfigDBConnector): Config DB connector obect. | ||
table (str): Table name to add new entry to. | ||
key (Union[str, Tuple]): Key name in the table. | ||
data (Dict): Entry data. | ||
create_if_not_exists (bool): | ||
In case entry does not exists already a new entry | ||
is not created if this flag is set to False and | ||
creates a new entry if flag is set to True. | ||
Raises: | ||
Exception: when cfg does not satisfy YANG schema. | ||
""" | ||
|
||
cfg = db.get_config() | ||
cfg.setdefault(table, {}) | ||
|
||
if not data: | ||
raise Exception(f"No field/values to update {key}") | ||
|
||
if create_if_not_exists: | ||
cfg[table].setdefault(key, {}) | ||
|
||
if key not in cfg[table]: | ||
raise Exception(f"{key} does not have PoE configuration") | ||
|
||
entry_changed = False | ||
for attr, value in data.items(): | ||
if value == cfg[table][key].get(attr): | ||
continue | ||
entry_changed = True | ||
if value is None: | ||
cfg[table][key].pop(attr, None) | ||
else: | ||
cfg[table][key][attr] = value | ||
|
||
if not entry_changed: | ||
return | ||
|
||
_validate_config_or_raise(cfg) | ||
db.set_entry(table, key, cfg[table][key]) | ||
|
||
|
||
# 'poe' subcommand ("config poe ...") | ||
@click.group( | ||
name="poe", | ||
cls=clicommon.AliasedGroup, | ||
) | ||
def poe(): | ||
""" Configure PoE (Power over Ethernet) feature """ | ||
pass | ||
|
||
|
||
# 'interface' subcommand ("config poe interface ...") | ||
@poe.group( | ||
name="interface", | ||
cls=clicommon.AliasedGroup, | ||
) | ||
def poe_interface(): | ||
""" Configure PoE interface """ | ||
pass | ||
|
||
|
||
# 'status' subcommand ("config poe interface status ...") | ||
@poe_interface.command( | ||
name="status" | ||
) | ||
@click.argument( | ||
"ifname", | ||
nargs=1, | ||
required=True, | ||
) | ||
@click.argument( | ||
"enabled", | ||
nargs=1, | ||
required=True, | ||
type=click.Choice(["enable", "disable"]) | ||
) | ||
@clicommon.pass_db | ||
def poe_intf_status(db, ifname, enabled): | ||
""" Enable or disable PoE on interface """ | ||
|
||
data = {} | ||
if enabled is not None: | ||
data["enabled"] = enabled | ||
|
||
try: | ||
_update_entry_validated(db.cfgdb, POE_PORT, ifname, data) | ||
except Exception as err: | ||
_exit_with_error(f"Error: {err}", fg="red") | ||
|
||
|
||
# 'power-limit' subcommand ("config poe interface power-limit ...") | ||
@poe_interface.command( | ||
name="power-limit" | ||
) | ||
@click.argument( | ||
"ifname", | ||
nargs=1, | ||
required=True, | ||
) | ||
@click.argument( | ||
"power_limit", | ||
nargs=1, | ||
required=True, | ||
type=click.INT | ||
) | ||
@clicommon.pass_db | ||
def poe_intf_power_limit(db, ifname, power_limit): | ||
""" Configure PoE interface power limit """ | ||
|
||
data = {} | ||
if power_limit is not None: | ||
data["pwr_limit"] = power_limit | ||
|
||
try: | ||
_update_entry_validated(db.cfgdb, POE_PORT, ifname, data) | ||
except Exception as err: | ||
_exit_with_error(f"Error: {err}", fg="red") | ||
|
||
|
||
# 'priority' subcommand ("config poe interface priority ...") | ||
@poe_interface.command( | ||
name="priority" | ||
) | ||
@click.argument( | ||
"ifname", | ||
nargs=1, | ||
required=True, | ||
) | ||
@click.argument( | ||
"priority", | ||
nargs=1, | ||
required=True, | ||
type=click.Choice(["low", "high", "crit"]) | ||
) | ||
@clicommon.pass_db | ||
def poe_intf_priority(db, ifname, priority): | ||
""" Configure PoE interface priority """ | ||
|
||
data = {} | ||
if priority is not None: | ||
data["priority"] = priority | ||
|
||
try: | ||
_update_entry_validated(db.cfgdb, POE_PORT, ifname, data) | ||
except Exception as err: | ||
_exit_with_error(f"Error: {err}", fg="red") | ||
|
||
|
||
def register(cli): | ||
""" Register new CLI nodes in root CLI. | ||
|
||
Args: | ||
cli: Root CLI node. | ||
Raises: | ||
Exception: when root CLI already has a command | ||
we are trying to register. | ||
""" | ||
cli_node = poe | ||
if cli_node.name in cli.commands: | ||
raise Exception(f"{cli_node.name} already exists in CLI") | ||
cli.add_command(poe) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please clarify if this CLIs will be available only in supported platforms
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CLI will be available on all platforms. If the platform does not support PoE the user will get an error message when trying to configure anything on an interface.