|
| 1 | +""" |
| 2 | +Config CLI plugin for the SONiC Power over Ethernet feature. |
| 3 | +This file is auto-generated by sonic-cli-gen tool but adjusted to meet PoE HLD requirenments. |
| 4 | +""" |
| 5 | + |
| 6 | +import copy |
| 7 | +import click |
| 8 | +import utilities_common.cli as clicommon |
| 9 | +import utilities_common.general as general |
| 10 | +from config import config_mgmt |
| 11 | + |
| 12 | + |
| 13 | +# Load sonic-cfggen from source since /usr/local/bin/sonic-cfggen does not have .py extension. |
| 14 | +sonic_cfggen = general.load_module_from_source('sonic_cfggen', '/usr/local/bin/sonic-cfggen') |
| 15 | + |
| 16 | + |
| 17 | +POE_PORT = 'POE_PORT' |
| 18 | + |
| 19 | + |
| 20 | +def _exit_with_error(*args, **kwargs): |
| 21 | + """ Print a message with click.secho and abort CLI. |
| 22 | +
|
| 23 | + Args: |
| 24 | + args: Positional arguments to pass to click.secho |
| 25 | + kwargs: Keyword arguments to pass to click.secho |
| 26 | + """ |
| 27 | + |
| 28 | + click.secho(*args, **kwargs) |
| 29 | + raise click.Abort() |
| 30 | + |
| 31 | + |
| 32 | +def _validate_config_or_raise(cfg): |
| 33 | + """ Validate config db data using ConfigMgmt. |
| 34 | +
|
| 35 | + Args: |
| 36 | + cfg (Dict): Config DB data to validate. |
| 37 | + Raises: |
| 38 | + Exception: when cfg does not satisfy YANG schema. |
| 39 | + """ |
| 40 | + |
| 41 | + try: |
| 42 | + cfg = sonic_cfggen.FormatConverter.to_serialized(copy.deepcopy(cfg)) |
| 43 | + config_mgmt.ConfigMgmt().loadData(cfg) |
| 44 | + except Exception as err: |
| 45 | + raise Exception('Failed to validate configuration: {}'.format(err)) |
| 46 | + |
| 47 | + |
| 48 | +def _update_entry_validated(db, table, key, data, create_if_not_exists=False): |
| 49 | + """ Update entry in table and validate configuration. |
| 50 | + If attribute value in data is None, the attribute is deleted. |
| 51 | +
|
| 52 | + Args: |
| 53 | + db (swsscommon.ConfigDBConnector): Config DB connector obect. |
| 54 | + table (str): Table name to add new entry to. |
| 55 | + key (Union[str, Tuple]): Key name in the table. |
| 56 | + data (Dict): Entry data. |
| 57 | + create_if_not_exists (bool): |
| 58 | + In case entry does not exists already a new entry |
| 59 | + is not created if this flag is set to False and |
| 60 | + creates a new entry if flag is set to True. |
| 61 | + Raises: |
| 62 | + Exception: when cfg does not satisfy YANG schema. |
| 63 | + """ |
| 64 | + |
| 65 | + cfg = db.get_config() |
| 66 | + cfg.setdefault(table, {}) |
| 67 | + |
| 68 | + if not data: |
| 69 | + raise Exception(f"No field/values to update {key}") |
| 70 | + |
| 71 | + if create_if_not_exists: |
| 72 | + cfg[table].setdefault(key, {}) |
| 73 | + |
| 74 | + if key not in cfg[table]: |
| 75 | + raise Exception(f"{key} does not have PoE configuration") |
| 76 | + |
| 77 | + entry_changed = False |
| 78 | + for attr, value in data.items(): |
| 79 | + if value == cfg[table][key].get(attr): |
| 80 | + continue |
| 81 | + entry_changed = True |
| 82 | + if value is None: |
| 83 | + cfg[table][key].pop(attr, None) |
| 84 | + else: |
| 85 | + cfg[table][key][attr] = value |
| 86 | + |
| 87 | + if not entry_changed: |
| 88 | + return |
| 89 | + |
| 90 | + _validate_config_or_raise(cfg) |
| 91 | + db.set_entry(table, key, cfg[table][key]) |
| 92 | + |
| 93 | + |
| 94 | +# 'poe' subcommand ("config poe ...") |
| 95 | +@click.group( |
| 96 | + name="poe", |
| 97 | + cls=clicommon.AliasedGroup, |
| 98 | +) |
| 99 | +def poe(): |
| 100 | + """ Configure PoE (Power over Ethernet) feature """ |
| 101 | + pass |
| 102 | + |
| 103 | + |
| 104 | +# 'interface' subcommand ("config poe interface ...") |
| 105 | +@poe.group( |
| 106 | + name="interface", |
| 107 | + cls=clicommon.AliasedGroup, |
| 108 | +) |
| 109 | +def poe_interface(): |
| 110 | + """ Configure PoE interface """ |
| 111 | + pass |
| 112 | + |
| 113 | + |
| 114 | +# 'status' subcommand ("config poe interface status ...") |
| 115 | +@poe_interface.command( |
| 116 | + name="status" |
| 117 | +) |
| 118 | +@click.argument( |
| 119 | + "ifname", |
| 120 | + nargs=1, |
| 121 | + required=True, |
| 122 | +) |
| 123 | +@click.argument( |
| 124 | + "enabled", |
| 125 | + nargs=1, |
| 126 | + required=True, |
| 127 | + type=click.Choice(["enable", "disable"]) |
| 128 | +) |
| 129 | +@clicommon.pass_db |
| 130 | +def poe_intf_status(db, ifname, enabled): |
| 131 | + """ Enable or disable PoE on interface """ |
| 132 | + |
| 133 | + data = {} |
| 134 | + if enabled is not None: |
| 135 | + data["enabled"] = enabled |
| 136 | + |
| 137 | + try: |
| 138 | + _update_entry_validated(db.cfgdb, POE_PORT, ifname, data) |
| 139 | + except Exception as err: |
| 140 | + _exit_with_error(f"Error: {err}", fg="red") |
| 141 | + |
| 142 | + |
| 143 | +# 'power-limit' subcommand ("config poe interface power-limit ...") |
| 144 | +@poe_interface.command( |
| 145 | + name="power-limit" |
| 146 | +) |
| 147 | +@click.argument( |
| 148 | + "ifname", |
| 149 | + nargs=1, |
| 150 | + required=True, |
| 151 | +) |
| 152 | +@click.argument( |
| 153 | + "power_limit", |
| 154 | + nargs=1, |
| 155 | + required=True, |
| 156 | + type=click.INT |
| 157 | +) |
| 158 | +@clicommon.pass_db |
| 159 | +def poe_intf_power_limit(db, ifname, power_limit): |
| 160 | + """ Configure PoE interface power limit """ |
| 161 | + |
| 162 | + data = {} |
| 163 | + if power_limit is not None: |
| 164 | + data["pwr_limit"] = power_limit |
| 165 | + |
| 166 | + try: |
| 167 | + _update_entry_validated(db.cfgdb, POE_PORT, ifname, data) |
| 168 | + except Exception as err: |
| 169 | + _exit_with_error(f"Error: {err}", fg="red") |
| 170 | + |
| 171 | + |
| 172 | +# 'priority' subcommand ("config poe interface priority ...") |
| 173 | +@poe_interface.command( |
| 174 | + name="priority" |
| 175 | +) |
| 176 | +@click.argument( |
| 177 | + "ifname", |
| 178 | + nargs=1, |
| 179 | + required=True, |
| 180 | +) |
| 181 | +@click.argument( |
| 182 | + "priority", |
| 183 | + nargs=1, |
| 184 | + required=True, |
| 185 | + type=click.Choice(["low", "high", "crit"]) |
| 186 | +) |
| 187 | +@clicommon.pass_db |
| 188 | +def poe_intf_priority(db, ifname, priority): |
| 189 | + """ Configure PoE interface priority """ |
| 190 | + |
| 191 | + data = {} |
| 192 | + if priority is not None: |
| 193 | + data["priority"] = priority |
| 194 | + |
| 195 | + try: |
| 196 | + _update_entry_validated(db.cfgdb, POE_PORT, ifname, data) |
| 197 | + except Exception as err: |
| 198 | + _exit_with_error(f"Error: {err}", fg="red") |
| 199 | + |
| 200 | + |
| 201 | +def register(cli): |
| 202 | + """ Register new CLI nodes in root CLI. |
| 203 | +
|
| 204 | + Args: |
| 205 | + cli: Root CLI node. |
| 206 | + Raises: |
| 207 | + Exception: when root CLI already has a command |
| 208 | + we are trying to register. |
| 209 | + """ |
| 210 | + cli_node = poe |
| 211 | + if cli_node.name in cli.commands: |
| 212 | + raise Exception(f"{cli_node.name} already exists in CLI") |
| 213 | + cli.add_command(poe) |
0 commit comments