Add multi-command collect with FSM-first if-intf aux and batch cache.

ARP binds show running-config if-intf to enrich vrf; identical aux commands reuse session cache.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-09-17 23:30:17 +08:00
parent 0f235133ed
commit 1f70f7fc25
10 changed files with 646 additions and 42 deletions

View file

@ -10,6 +10,7 @@ from typing import Any, Callable
from .arp import is_valid_arp_age, normalize_arp
from .bgp_peer import normalize_bgp_peer
from .if_intf import normalize_if_intf, parse_if_intf_vrf_map
from .interface_brief import normalize_interface_brief
from .isis_adjacency import normalize_isis_adjacency
from .nd6_cache import normalize_nd6_cache
@ -20,6 +21,7 @@ PARSERS: dict[str, NormalizeFn] = {
"isis_adjacency": normalize_isis_adjacency,
"interface_brief": normalize_interface_brief,
"arp": normalize_arp,
"if_intf": normalize_if_intf,
"nd6_cache": normalize_nd6_cache,
"bgp_peer": normalize_bgp_peer,
}
@ -29,7 +31,9 @@ __all__ = [
"is_valid_arp_age",
"normalize_arp",
"normalize_bgp_peer",
"normalize_if_intf",
"normalize_interface_brief",
"normalize_isis_adjacency",
"normalize_nd6_cache",
"parse_if_intf_vrf_map",
]

View file

@ -1,6 +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.
"""
from __future__ import annotations
@ -11,6 +12,7 @@ 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",)
@ -32,6 +34,7 @@ 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()
@ -48,6 +51,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],
}
@ -110,6 +114,40 @@ 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,
@ -118,6 +156,8 @@ def normalize_arp(
device_type: str = "",
command: str = "",
params: dict[str, str] | None = None,
raws: Mapping[str, str] | None = None,
aux_records: Mapping[str, list[dict[str, Any]]] | None = None,
) -> list[dict[str, Any]]:
_ = params
tables = dict(fsm_tables or {})
@ -132,7 +172,7 @@ def normalize_arp(
tables = apply_rules(
platform=platform, text=raw_text, rule_keys=RULE_KEYS, command=cmd
)
return prefer_fsm(
rows = prefer_fsm(
tables,
RULE_KEYS,
_map_fsm_rows,
@ -142,6 +182,8 @@ 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

View file

@ -0,0 +1,113 @@
"""ZTE: show running-config if-intf → interface / VRF map.
Pipeline: TextFSM ``zte_zxros_show_running_config_if_intf`` first; hand fallback.
"""
from __future__ import annotations
import re
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
RULE_KEYS = ("zte_zxros_show_running_config_if_intf",)
_IFACE_RE = re.compile(r"^\s*interface\s+(\S+)\s*$", re.I)
_VRF_RE = re.compile(r"^\s*ip\s+vrf\s+forwarding\s+(\S+)\s*$", re.I)
def _map_fsm_rows(rows: list[dict[str, Any]]) -> list[dict[str, Any]]:
out: list[dict[str, Any]] = []
seen: set[str] = set()
for r in rows:
iface = row_get(r, "INTERFACE", "interface")
vrf = row_get(r, "VRF", "vrf")
if not iface or not vrf or iface in seen:
continue
seen.add(iface)
out.append({"interface": iface[:128], "vrf": vrf[:128]})
return out
def _hand_parse(*, raw_text: str, **_kw: Any) -> list[dict[str, Any]]:
out: list[dict[str, Any]] = []
seen: set[str] = set()
cur = ""
cur_vrf = ""
for raw in str(raw_text or "").splitlines():
line = raw.rstrip()
m_if = _IFACE_RE.match(line)
if m_if:
if cur and cur_vrf and cur not in seen:
seen.add(cur)
out.append({"interface": cur[:128], "vrf": cur_vrf[:128]})
cur = m_if.group(1).strip()
cur_vrf = ""
continue
if line.strip() == "$":
if cur and cur_vrf and cur not in seen:
seen.add(cur)
out.append({"interface": cur[:128], "vrf": cur_vrf[:128]})
cur = ""
cur_vrf = ""
continue
m_vrf = _VRF_RE.match(line)
if m_vrf and cur:
cur_vrf = m_vrf.group(1).strip()
if cur and cur_vrf and cur not in seen:
out.append({"interface": cur[:128], "vrf": cur_vrf[:128]})
return out
def parse_if_intf_vrf_map(raw_text: str = "", *, rows: list[dict[str, Any]] | None = None) -> dict[str, str]:
"""Build interface → vrf from metric rows or raw if-intf text."""
if rows is None:
rows = _hand_parse(raw_text=raw_text)
out: dict[str, str] = {}
for r in rows or []:
iface = str((r or {}).get("interface") or "").strip()
vrf = str((r or {}).get("vrf") or "").strip()
if iface and vrf:
out[iface] = vrf
return out
def normalize_if_intf(
*,
raw_text: str,
fsm_tables: Mapping[str, list[dict[str, Any]]] | None = None,
vendor: str = "",
device_type: str = "",
command: str = "",
params: dict[str, str] | None = None,
raws: Mapping[str, str] | None = None,
aux_records: Mapping[str, list[dict[str, Any]]] | None = None,
) -> list[dict[str, Any]]:
_ = (params, raws, aux_records)
tables = dict(fsm_tables or {})
if not any(tables.get(k) for k in RULE_KEYS):
platform = resolve_cli_platform(
vendor=vendor,
device_type=device_type,
vendor_key=resolve_vendor_key(vendor, device_type),
)
cmd = str(command or "show running-config if-intf").strip()
if platform:
tables = apply_rules(
platform=platform, text=raw_text, rule_keys=RULE_KEYS, command=cmd
)
return prefer_fsm(
tables,
RULE_KEYS,
_map_fsm_rows,
_hand_parse,
raw_text=raw_text,
vendor=vendor,
device_type=device_type,
command=command,
)
normalize_if_intf.RULE_KEYS = RULE_KEYS