mirror of
https://github.com/hansjone/netx.git
synced 2026-10-17 23:13:26 +08:00
Elegant multi-command collect: slim aux, declarative enrich, CollectSession.
AuxCommand is key+profile_id only; EnrichJoin copies fields after primary parse; ARP no longer hardcodes if_intf join. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
1f70f7fc25
commit
17d136cf6b
8 changed files with 600 additions and 227 deletions
|
|
@ -3,29 +3,32 @@
|
|||
Layout::
|
||||
|
||||
parsers/
|
||||
common/ # cross-vendor (lldp, vrf_list, vrf_route_summary, pipeline)
|
||||
common/ # cross-vendor + pipeline.prefer_fsm
|
||||
zte/ # implemented status tables
|
||||
cisco/ # skeleton — add command modules here
|
||||
huawei/
|
||||
h3c/
|
||||
juniper/
|
||||
nokia/
|
||||
ericsson/
|
||||
cisco/ huawei/ … # skeletons
|
||||
|
||||
Each vendor package exports ``PARSERS: dict[parser_id, normalize_fn]``.
|
||||
|
||||
Adding a status metric
|
||||
----------------------
|
||||
1. (Recommended) ``cli_templates/<vendor>/<stem>.textfsm`` + ``index`` line
|
||||
2. ``parsers/<vendor>/<metric>.py``: ``RULE_KEYS = ("<stem>",)`` +
|
||||
``normalize(..., fsm_tables=...)`` (use ``common.pipeline.prefer_fsm``)
|
||||
3. Register in the vendor ``PARSERS`` dict
|
||||
4. ``profiles.py``: add ``ParseProfile`` (command match + ``FieldDef`` schema);
|
||||
optional ``aux_commands`` for multi-command collect items
|
||||
1. ``cli_templates/<vendor>/<stem>.textfsm`` + ``index`` (FSM first)
|
||||
2. ``parsers/<vendor>/<metric>.py``: ``RULE_KEYS`` + ``normalize`` + ``prefer_fsm``
|
||||
3. Register in vendor ``PARSERS``
|
||||
4. ``profiles.py``: ``ParseProfile`` (match + ``FieldDef``)
|
||||
|
||||
Collect runs TextFSM rules first → ``fsm_tables``, then calls ``normalize`` with
|
||||
``raw_text`` / ``raws`` / ``fsm_tables`` / ``aux_records``. Prefer FSM; hand-parse
|
||||
only when FSM has no rows.
|
||||
Cross-command (multi aux)
|
||||
-------------------------
|
||||
1. Implement each aux as a normal status profile (steps 1–4)
|
||||
2. On the primary profile::
|
||||
|
||||
aux_commands=[AuxCommand(key="if_intf", profile_id="zte.if_intf")]
|
||||
enrich_joins=[EnrichJoin(from_aux="if_intf", on="interface", take=("vrf",))]
|
||||
|
||||
3. CollectSession caches identical concrete CLI in one batch; enrich runs after
|
||||
primary normalize (no join logic inside the parser).
|
||||
|
||||
Complex joins that cannot be expressed as equal-field copy still go in
|
||||
``normalize`` using ``raws`` / ``aux_records`` / ``fsm_tables``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
"""ZTE: show arp [| one-line].
|
||||
|
||||
Pipeline: TextFSM rule ``zte_zxros_show_arp`` → map rows → hand fallback.
|
||||
VRF filled from aux ``if_intf`` (show running-config if-intf), FSM-first.
|
||||
Pipeline: TextFSM ``zte_zxros_show_arp`` → map → hand fallback.
|
||||
Cross-command VRF comes from profile ``enrich_joins`` (if_intf), not here.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
|
@ -12,7 +12,6 @@ from typing import Any, Mapping
|
|||
from ....lldp_shared import resolve_vendor_key
|
||||
from ....ntc_parse import apply_rules, resolve_cli_platform, row_get
|
||||
from ..common.pipeline import prefer_fsm
|
||||
from .if_intf import parse_if_intf_vrf_map
|
||||
|
||||
RULE_KEYS = ("zte_zxros_show_arp",)
|
||||
|
||||
|
|
@ -34,7 +33,6 @@ def _row_from_fields(
|
|||
exter: str = "",
|
||||
inter: str = "",
|
||||
sub: str = "",
|
||||
vrf: str = "",
|
||||
) -> dict[str, Any] | None:
|
||||
ip = str(ip or "").strip()
|
||||
iface = str(iface or "").strip()
|
||||
|
|
@ -51,7 +49,7 @@ def _row_from_fields(
|
|||
"inter_vlan": str(inter or "").strip()[:32],
|
||||
"sub_interface": str(sub or "").strip()[:128],
|
||||
"entry_type": "dynamic" if dynamic else "static",
|
||||
"vrf": str(vrf or "").strip()[:128],
|
||||
"vrf": "",
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -114,40 +112,6 @@ def _hand_parse(*, raw_text: str, **_kw: Any) -> list[dict[str, Any]]:
|
|||
return out
|
||||
|
||||
|
||||
def _vrf_map_from_aux(
|
||||
*,
|
||||
raws: Mapping[str, str] | None,
|
||||
aux_records: Mapping[str, list[dict[str, Any]]] | None,
|
||||
fsm_tables: Mapping[str, list[dict[str, Any]]] | None,
|
||||
) -> dict[str, str]:
|
||||
recs = list((aux_records or {}).get("if_intf") or [])
|
||||
if recs:
|
||||
return parse_if_intf_vrf_map(rows=recs)
|
||||
fsm_rows = (fsm_tables or {}).get("zte_zxros_show_running_config_if_intf") or []
|
||||
if fsm_rows:
|
||||
mapped = [
|
||||
{
|
||||
"interface": row_get(r, "INTERFACE", "interface"),
|
||||
"vrf": row_get(r, "VRF", "vrf"),
|
||||
}
|
||||
for r in fsm_rows
|
||||
]
|
||||
return parse_if_intf_vrf_map(rows=mapped)
|
||||
raw = str((raws or {}).get("if_intf") or "")
|
||||
if raw.strip():
|
||||
return parse_if_intf_vrf_map(raw_text=raw)
|
||||
return {}
|
||||
|
||||
|
||||
def _enrich_vrf(rows: list[dict[str, Any]], vrf_map: dict[str, str]) -> list[dict[str, Any]]:
|
||||
if not rows:
|
||||
return rows
|
||||
for r in rows:
|
||||
iface = str(r.get("interface") or "").strip()
|
||||
r["vrf"] = (vrf_map.get(iface) or "")[:128]
|
||||
return rows
|
||||
|
||||
|
||||
def normalize_arp(
|
||||
*,
|
||||
raw_text: str,
|
||||
|
|
@ -159,7 +123,7 @@ def normalize_arp(
|
|||
raws: Mapping[str, str] | None = None,
|
||||
aux_records: Mapping[str, list[dict[str, Any]]] | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
_ = params
|
||||
_ = (params, raws, aux_records)
|
||||
tables = dict(fsm_tables or {})
|
||||
if not any(tables.get(k) for k in RULE_KEYS):
|
||||
platform = resolve_cli_platform(
|
||||
|
|
@ -172,7 +136,7 @@ def normalize_arp(
|
|||
tables = apply_rules(
|
||||
platform=platform, text=raw_text, rule_keys=RULE_KEYS, command=cmd
|
||||
)
|
||||
rows = prefer_fsm(
|
||||
return prefer_fsm(
|
||||
tables,
|
||||
RULE_KEYS,
|
||||
_map_fsm_rows,
|
||||
|
|
@ -182,8 +146,6 @@ def normalize_arp(
|
|||
device_type=device_type,
|
||||
command=command,
|
||||
)
|
||||
vrf_map = _vrf_map_from_aux(raws=raws, aux_records=aux_records, fsm_tables=tables)
|
||||
return _enrich_vrf(rows, vrf_map)
|
||||
|
||||
|
||||
normalize_arp.RULE_KEYS = RULE_KEYS
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue