Skip to content

Commit 340ec8f

Browse files
vadym-hlushkoSerhiyBoikoPLV
authored andcommitted
[PoE] Add PoE CLI
Add show/config commands Add UT Update command reference Signed-off-by: vadym-hlushko <[email protected]> Signed-off-by: Serhiy Boiko <[email protected]>
1 parent 04a2005 commit 340ec8f

File tree

7 files changed

+1020
-0
lines changed

7 files changed

+1020
-0
lines changed

config/plugins/poe.py

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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)

doc/Command-Reference.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@
249249
* [Historical Memory Statistics for Last 3 Hours](#historical-memory-statistics-for-last-3-hours)
250250
* [Historical Memory Statistics for Specific Metric (Used Memory)](#historical-memory-statistics-for-specific-metric-used-memory)
251251
* [View Memory Statistics Configuration](#view-memory-statistics-configuration)
252+
* [Power over Ethernet](#power-over-ethernet)
253+
* [PoE show commands](#poe-show-commands)
254+
* [PoE config commands](#poe-config-commands)
255+
252256
## Document History
253257

254258
| Version | Modification Date | Details |
@@ -14750,3 +14754,114 @@ Enabled: false
1475014754
Sampling Interval: 5
1475114755
Retention Period: 15
1475214756
```
14757+
14758+
## Power over Ethernet
14759+
14760+
This section explains all the PoE commands that are supported in SONiC.
14761+
14762+
### PoE show commands
14763+
This sub-section contains the show commands.
14764+
14765+
- Show status of all PoE devices:
14766+
```
14767+
show poe status
14768+
```
14769+
```
14770+
admin@sonic:~$ show poe status
14771+
Id PoE ports Total power Power consump Power available Power limit mode HW info Version
14772+
---- ----------- ------------- --------------- ----------------- ------------------ --------- ---------
14773+
0 16 100.000 W 10.000 W 90.000 W port mcu1 0.1.2.3
14774+
1 16 100.000 W 10.000 W 90.000 W class mcu2 0.1.2.3
14775+
```
14776+
14777+
- Show status of all PoE PSEs:
14778+
```
14779+
show poe pse status
14780+
```
14781+
```
14782+
admin@sonic:~$ show poe pse status
14783+
Id Status Temperature SW ver HW ver
14784+
---- -------- ------------- ---------------- ----------------
14785+
0 active 25.000 C 0.1.2.3 4.5.6.7
14786+
1 active 25.000 C 0.1.2.3 4.5.6.7
14787+
2 active 25.000 C 0.1.2.3 4.5.6.7
14788+
3 active 25.000 C 0.1.2.3 4.5.6.7
14789+
```
14790+
14791+
- Show status of all PoE interfaces:
14792+
```
14793+
show poe interface status
14794+
```
14795+
```
14796+
admin@sonic:~$ show poe interface status
14797+
Port Status En/Dis Priority Protocol Class A Class B PWR Consump PWR limit Voltage Current
14798+
----------- ---------- -------- ---------- -------------- --------- --------- ------------- ----------- --------- ---------
14799+
Ethernet0 delivering enable crit 802.3bt Type 3 2 4 10.000 W 50.000 W 50.000 V 0.200 A
14800+
Ethernet1 delivering enable crit 802.3bt Type 3 2 4 10.000 W 50.000 W 50.000 V 0.200 A
14801+
Ethernet2 delivering enable low 802.3bt Type 3 2 4 10.000 W 50.000 W 50.000 V 0.200 A
14802+
Ethernet3 delivering enable low 802.3bt Type 3 2 4 10.000 W 50.000 W 50.000 V 0.200 A
14803+
```
14804+
14805+
- Show current configuration of all PoE interfaces:
14806+
```
14807+
show poe interface configuration
14808+
```
14809+
```
14810+
admin@sonic:~$ show poe interface configuration
14811+
Port En/Dis Power limit Priority
14812+
----------- -------- ------------- ----------
14813+
Ethernet0 enable 50 crit
14814+
Ethernet1 enable 50 crit
14815+
Ethernet2 enable 50 low
14816+
Ethernet3 enable 50 low
14817+
```
14818+
14819+
### PoE config commands
14820+
This sub-section contains the config commands.
14821+
14822+
14823+
- Enable PoE:
14824+
```
14825+
admin@sonic:~$ sudo config poe interface status --help
14826+
Usage: config poe interface status [OPTIONS] IFNAME [enable|disable]
14827+
14828+
Enable or disable PoE on interface
14829+
14830+
Options:
14831+
-h, -?, --help Show this message and exit.
14832+
```
14833+
```
14834+
admin@sonic:~$ sudo config poe interface status Ethernet0 enable
14835+
```
14836+
14837+
14838+
- Configure power limit:
14839+
```
14840+
admin@sonic:~$ sudo config poe interface power-limit --help
14841+
Usage: config poe interface power-limit [OPTIONS] IFNAME POWER_LIMIT
14842+
14843+
Configure PoE interface power limit
14844+
14845+
Options:
14846+
-h, -?, --help Show this message and exit.
14847+
```
14848+
```
14849+
admin@sonic:~$ sudo config poe interface power-limit Ethernet0 25
14850+
```
14851+
14852+
14853+
- Configure priority:
14854+
```
14855+
admin@sonic:~$ sudo config poe interface priority --help
14856+
Usage: config poe interface priority [OPTIONS] IFNAME [low|high|crit]
14857+
14858+
Configure PoE interface priority
14859+
14860+
Options:
14861+
-?, -h, --help Show this message and exit.
14862+
```
14863+
```
14864+
admin@sonic:~$ sudo config poe interface priority Ethernet0 crit
14865+
```
14866+
14867+
Go Back To [Beginning of the document](#) or [Beginning of this section](#power-over-ethernet)

0 commit comments

Comments
 (0)