feat(ping): add a generic ping sweep every driver inherits
Sweeping a range is orchestration, not device mechanics: the only vendor-specific part is executing a single ping, and NAPALM already standardises that. PingSweepMixin therefore owns the loop, the reply parsing, the target cap and the progress reporting, and is mixed into DeviceTypeDriver so any driver implementing ping() becomes a usable sweep source without writing sweep code of its own. driver_supports_ping() answers "can this driver ping?" by introspection instead of a hand-maintained list, with SUPPORTS_PING = False as the opt-out for a driver that inherits a ping it cannot actually use. The generic implementation is deliberately sequential — a NAPALM connection is a single session and not safe to drive from several threads at once. A driver whose device offers something faster overrides ping_sweep and keeps the return shape; see napalm-opnsense's batched job API version.
This commit is contained in:
@@ -60,6 +60,25 @@ A new driver (FortiGate, pfSense, …) gets `diff_firewall_rules`/
|
||||
`apply_firewall_ruleset` for free the moment it implements the three abstract
|
||||
methods — it never needs to reimplement the reconciliation logic itself.
|
||||
|
||||
**Second worked example — ping sweeps** (`PingSweepMixin`, mixed into
|
||||
`DeviceTypeDriver`, so *every* device-type driver has it):
|
||||
|
||||
```python
|
||||
class PingSweepMixin:
|
||||
# Concrete — the loop, the reply parsing, the target cap and the progress
|
||||
# reporting are the same for every device that can ping at all.
|
||||
def ping_sweep(self, destinations, *, count=1, timeout=1, …) -> PingSweepResultDict:
|
||||
... # calls NAPALM's standard ping() once per destination
|
||||
```
|
||||
|
||||
A driver becomes a usable sweep source the moment it implements NAPALM's
|
||||
`ping()` — nothing else is required, and `driver_supports_ping(cls)` reports
|
||||
whether it did (introspection, not a hand-maintained list). A driver whose
|
||||
device offers something genuinely faster overrides `ping_sweep` and keeps the
|
||||
return shape: `napalm-opnsense` starts a batch of ping jobs over the
|
||||
diagnostics API, waits once for all of them, and reads every result with a
|
||||
single request — a per-host loop would be unusable there.
|
||||
|
||||
This mirrors a similar split already documented on the consumer side, in NetOrk's
|
||||
`docs/ARCHITECTURE.md` ("Device Warnings — Trennung von Erkennung und
|
||||
Präsentation"): drivers return raw signals, the higher layer gives them meaning.
|
||||
|
||||
Reference in New Issue
Block a user