-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add show/config commands Add UT Update command reference Signed-off-by: vadym-hlushko <[email protected]> Signed-off-by: Serhiy Boiko <[email protected]>
- Loading branch information
1 parent
038aaa5
commit 753119b
Showing
7 changed files
with
1,019 additions
and
0 deletions.
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.