Skip to content

Commit e520782

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 84cb00a commit e520782

File tree

7 files changed

+1016
-0
lines changed

7 files changed

+1016
-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: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@
224224
* [Static DNS show command](#static-dns-show-command)
225225
* [Wake-on-LAN Commands](#wake-on-lan-commands)
226226
* [Send Wake-on-LAN Magic Packet command](#send-wake-on-lan-magic-packet-command)
227+
* [Power over Ethernet](#power-over-ethernet)
228+
* [PoE show commands](#poe-show-commands)
229+
* [PoE config commands](#poe-config-commands)
227230

228231
## Document History
229232

@@ -13701,3 +13704,114 @@ Sending 3 magic packet to 11:33:55:77:99:bb via interface Vlan1000
1370113704
```
1370213705
1370313706
For the 4th example, it specifise 2 target MAC addresses and `count` is 3. So it'll send 6 magic packets in total.
13707+
13708+
## Power over Ethernet
13709+
13710+
This section explains all the PoE commands that are supported in SONiC.
13711+
13712+
### PoE show commands
13713+
This sub-section contains the show commands.
13714+
13715+
- Show status of all PoE devices:
13716+
```
13717+
show poe status
13718+
```
13719+
```
13720+
admin@sonic:~$ show poe status
13721+
Id PoE ports Total power Power consump Power available Power limit mode HW info Version
13722+
---- ----------- ------------- --------------- ----------------- ------------------ --------- ---------
13723+
0 16 100.000 W 10.000 W 90.000 W port mcu1 0.1.2.3
13724+
1 16 100.000 W 10.000 W 90.000 W class mcu2 0.1.2.3
13725+
```
13726+
13727+
- Show status of all PoE PSEs:
13728+
```
13729+
show poe pse status
13730+
```
13731+
```
13732+
admin@sonic:~$ show poe pse status
13733+
Id Status Temperature SW ver HW ver
13734+
---- -------- ------------- ---------------- ----------------
13735+
0 active 25.000 C 0.1.2.3 4.5.6.7
13736+
1 active 25.000 C 0.1.2.3 4.5.6.7
13737+
2 active 25.000 C 0.1.2.3 4.5.6.7
13738+
3 active 25.000 C 0.1.2.3 4.5.6.7
13739+
```
13740+
13741+
- Show status of all PoE interfaces:
13742+
```
13743+
show poe interface status
13744+
```
13745+
```
13746+
admin@sonic:~$ show poe interface status
13747+
Port Status En/Dis Priority Protocol Class A Class B PWR Consump PWR limit Voltage Current
13748+
----------- ---------- -------- ---------- -------------- --------- --------- ------------- ----------- --------- ---------
13749+
Ethernet0 delivering enable crit 802.3bt Type 3 2 4 10.000 W 50.000 W 50.000 V 0.200 A
13750+
Ethernet1 delivering enable crit 802.3bt Type 3 2 4 10.000 W 50.000 W 50.000 V 0.200 A
13751+
Ethernet2 delivering enable low 802.3bt Type 3 2 4 10.000 W 50.000 W 50.000 V 0.200 A
13752+
Ethernet3 delivering enable low 802.3bt Type 3 2 4 10.000 W 50.000 W 50.000 V 0.200 A
13753+
```
13754+
13755+
- Show current configuration of all PoE interfaces:
13756+
```
13757+
show poe interface configuration
13758+
```
13759+
```
13760+
admin@sonic:~$ show poe interface configuration
13761+
Port En/Dis Power limit Priority
13762+
----------- -------- ------------- ----------
13763+
Ethernet0 enable 50 crit
13764+
Ethernet1 enable 50 crit
13765+
Ethernet2 enable 50 low
13766+
Ethernet3 enable 50 low
13767+
```
13768+
13769+
### PoE config commands
13770+
This sub-section contains the config commands.
13771+
13772+
13773+
- Enable PoE:
13774+
```
13775+
admin@sonic:~$ sudo config poe interface status --help
13776+
Usage: config poe interface status [OPTIONS] IFNAME [enable|disable]
13777+
13778+
Enable or disable PoE on interface
13779+
13780+
Options:
13781+
-h, -?, --help Show this message and exit.
13782+
```
13783+
```
13784+
admin@sonic:~$ sudo config poe interface status Ethernet0 enable
13785+
```
13786+
13787+
13788+
- Configure power limit:
13789+
```
13790+
admin@sonic:~$ sudo config poe interface power-limit --help
13791+
Usage: config poe interface power-limit [OPTIONS] IFNAME POWER_LIMIT
13792+
13793+
Configure PoE interface power limit
13794+
13795+
Options:
13796+
-h, -?, --help Show this message and exit.
13797+
```
13798+
```
13799+
admin@sonic:~$ sudo config poe interface power-limit Ethernet0 25
13800+
```
13801+
13802+
13803+
- Configure priority:
13804+
```
13805+
admin@sonic:~$ sudo config poe interface priority --help
13806+
Usage: config poe interface priority [OPTIONS] IFNAME [low|high|crit]
13807+
13808+
Configure PoE interface priority
13809+
13810+
Options:
13811+
-?, -h, --help Show this message and exit.
13812+
```
13813+
```
13814+
admin@sonic:~$ sudo config poe interface priority Ethernet0 crit
13815+
```
13816+
13817+
Go Back To [Beginning of the document](#) or [Beginning of this section](#power-over-ethernet)

0 commit comments

Comments
 (0)