Add TextFSM rule tables as dual input to biz-state parsers.

Collectors apply RULE_KEYS (or index stems) into fsm_tables before normalize; parsers prefer FSM or hand-parse.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-09-17 22:18:21 +08:00
parent 24a1a853af
commit 02b8a7073b
25 changed files with 714 additions and 69 deletions

View file

@ -6,10 +6,13 @@ from typing import Any
from ....lldp_shared import NeighborHit, parse_neighbor_output
RULE_KEYS: tuple[str, ...] = ()
def normalize_lldp_neighbors(
*,
raw_text: str,
fsm_tables=None,
vendor: str = "",
device_type: str = "",
command: str = "",
@ -21,7 +24,7 @@ def normalize_lldp_neighbors(
device_type=device_type,
command=command,
)
_ = params
_ = (params, fsm_tables)
rows: list[dict[str, Any]] = []
for h in hits:
rows.append(
@ -34,3 +37,5 @@ def normalize_lldp_neighbors(
}
)
return rows
normalize_lldp_neighbors.RULE_KEYS = RULE_KEYS

View file

@ -0,0 +1,31 @@
"""Shared FSM-vs-hand helpers for biz_state parsers."""
from __future__ import annotations
from typing import Any, Callable, Mapping, Sequence
MapRowsFn = Callable[[list[dict[str, Any]]], list[dict[str, Any]]]
HandFn = Callable[..., list[dict[str, Any]]]
def prefer_fsm(
fsm_tables: Mapping[str, list[dict[str, Any]]] | None,
keys: Sequence[str],
map_rows: MapRowsFn,
hand_fn: HandFn,
*,
raw_text: str,
**kw: Any,
) -> list[dict[str, Any]]:
"""Use the first non-empty FSM table (mapped); otherwise hand-parse raw_text."""
tables = fsm_tables or {}
for key in keys or ():
k = str(key or "").strip()
if not k:
continue
rows = tables.get(k) or []
if rows:
mapped = map_rows(rows)
if mapped:
return mapped
return hand_fn(raw_text=raw_text, **kw)

View file

@ -3,28 +3,16 @@
from __future__ import annotations
import re
from typing import Any
from typing import Any, Mapping
from ....lldp_shared import resolve_vendor_key
from ....ntc_parse import parse_cli, resolve_cli_platform, row_get
from ....ntc_parse import apply_rules, resolve_cli_platform, row_get
from .pipeline import prefer_fsm
RULE_KEYS = ("zte_zxros_show_ip_vrf",)
def normalize_vrf_list(
*,
raw_text: str,
vendor: str = "",
device_type: str = "",
command: str = "",
params: dict[str, str] | None = None,
) -> list[dict[str, Any]]:
_ = params
platform = resolve_cli_platform(
vendor=vendor,
device_type=device_type,
vendor_key=resolve_vendor_key(vendor, device_type),
)
cmd = str(command or "").strip()
rows = parse_cli(platform=platform, command=cmd, text=raw_text) if platform and cmd else []
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:
@ -38,11 +26,15 @@ def normalize_vrf_list(
{
"vrf_name": name[:128],
"rd": row_get(r, "DEFAULT_RD", "RD", "rd")[:64],
"protocols": row_get(r, "PROTOCOLS", "ADDRESS_FAMILY", "protocols")[:64],
"protocols": row_get(r, "PROTOCOLS", "ADDRESS_FAMILY", "INTERFACES", "protocols")[:64],
}
)
if out:
return out
return out
def _hand_parse(*, raw_text: str, **_kw: Any) -> list[dict[str, Any]]:
out: list[dict[str, Any]] = []
seen: set[str] = set()
for line in str(raw_text or "").splitlines():
m = re.match(r"^\s*([A-Za-z0-9_./:-]+)\s+(\d+:\d+|<not set>|\S+:\S+)\s*", line)
if not m:
@ -55,3 +47,37 @@ def normalize_vrf_list(
seen.add(name)
out.append({"vrf_name": name[:128], "rd": m.group(2)[:64], "protocols": ""})
return out
def normalize_vrf_list(
*,
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,
) -> list[dict[str, Any]]:
_ = params
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 ip vrf").strip()
if platform and cmd:
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,
)
normalize_vrf_list.RULE_KEYS = RULE_KEYS

View file

@ -16,16 +16,19 @@ _SOURCE_PATTERNS: list[tuple[str, re.Pattern[str]]] = [
("total", re.compile(r"(?i)^\s*(?:total|totals?)\s+(?:routes?\s+)?(\d+)\s*$")),
]
RULE_KEYS: tuple[str, ...] = ()
def normalize_vrf_route_summary(
*,
raw_text: str,
fsm_tables=None,
vendor: str = "",
device_type: str = "",
command: str = "",
params: dict[str, str] | None = None,
) -> list[dict[str, Any]]:
_ = (vendor, device_type, command)
_ = (vendor, device_type, command, fsm_tables)
vrf = str((params or {}).get("vrf") or (params or {}).get("vrf_name") or "").strip()
text = str(raw_text or "")
found: dict[str, int] = {}
@ -53,3 +56,5 @@ def normalize_vrf_route_summary(
if vrf and not rows:
rows.append({"vrf": vrf[:128], "source": "empty", "networks": 0})
return rows
normalize_vrf_route_summary.RULE_KEYS = RULE_KEYS