Skip to content
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

power: add ubus NetworkPowerPort #1409

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ Currently available are:
``poe_mib``
Controls PoE switches using the PoE SNMP administration MiBs.

``ubus``
Controls *PoE switches* running OpenWrt using the *ubus* interface.
Further infromation available at <https://openwrt.org/docs/techref/ubus>

Used by:
- `NetworkPowerDriver`_

Expand Down
48 changes: 48 additions & 0 deletions labgrid/driver/power/ubus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
UBUS jsonrpc interface for PoE management on OpenWrt devices. This comes in
handy if devices are connected to a PoE switch running OpenWrt.

The URL given in hosts in exporter.yaml must accept unauthenticated UBUS
calls for the two `poe` calls `info` and `manage`.

Further information is availbe at https://openwrt.org/docs/techref/ubus#acls

NetworkPowerPort:
model: ubus
host: 'http://192.168.1.1/ubus'
index: 1
"""

import requests


def jsonrpc_call(host, path, method, message):
r = requests.post(
aparcar marked this conversation as resolved.
Show resolved Hide resolved
host,
json={
"jsonrpc": "2.0",
"id": 1,
"method": "call",
"params": ["00000000000000000000000000000000", path, method, message],
},
)
r.raise_for_status()
return r.json()["result"]


def power_set(host, port, index, value):
assert port is None

jsonrpc_call(host, "poe", "manage", {"port": f"lan{index}", "enable": value == 1})


def power_get(host, port, index):
assert port is None

poe_info = jsonrpc_call(host, "poe", "info", {})[1]

assert (
f"lan{index}" in poe_info["ports"]
), f"Port lan{index} not found in {poe_info['ports']}"

return poe_info["ports"][f"lan{index}"] != "Disabled"
40 changes: 38 additions & 2 deletions tests/test_powerdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,46 @@ def test_create_shelly_gen1_backend_with_url_in_host(self, target, mocker, host)
expected_host = f"{host}/relay/{index}"
url = urlparse(expected_host)
if url.port is None:
implicit_port = 443 if url.scheme == 'https' else 80
expected_host = expected_host.replace(url.netloc, f'{url.netloc}:{implicit_port}')
implicit_port = 443 if url.scheme == "https" else 80
expected_host = expected_host.replace(
url.netloc, f"{url.netloc}:{implicit_port}"
)

get.assert_called_with(expected_host)

def test_create_ubus_backend(self, target, mocker):
post = mocker.patch("requests.post")
post.return_value.json.return_value = {
"jsonrpc": "2.0",
"id": 1,
"result": [
0,
{
"firmware": "v80.1",
"budget": 77.000000,
"consumption": 1.700000,
"ports": {
"lan1": {
"priority": 0,
"mode": "PoE+",
"status": "Delivering power",
"consumption": 1.700000,
}
},
},
],
}

index = "1"
NetworkPowerPort(
target, "power", model="ubus", host="http://example.com/ubus", index=index
)
d = NetworkPowerDriver(target, "power")
target.activate(d)

d.cycle()
assert d.get() is True

def test_import_backends(self):
import labgrid.driver.power
import labgrid.driver.power.apc
Expand All @@ -253,6 +288,7 @@ def test_import_backends(self):
import labgrid.driver.power.sentry
import labgrid.driver.power.eg_pms2_network
import labgrid.driver.power.shelly_gen1
import labgrid.driver.power.ubus

def test_import_backend_eaton(self):
pytest.importorskip("pysnmp")
Expand Down
Loading