30 lines
2.3 KiB
TypeScript
30 lines
2.3 KiB
TypeScript
export type OspfNeighborState = "Full" | "2-Way" | "ExStart" | "Down" | "Attempt" | "Init" | "Exchange" | "Loading" | "Unknown"
|
|
export type BfdState = "Up" | "Down" | "Init" | "AdminDown" | "Unknown"
|
|
const neighborStates: Record<string, OspfNeighborState> = { full: "Full", "2-way": "2-Way", exstart: "ExStart", down: "Down", attempt: "Attempt", init: "Init", exchange: "Exchange", loading: "Loading" }
|
|
const bfdStates: Record<string, BfdState> = { up: "Up", down: "Down", init: "Init", admindown: "AdminDown" }
|
|
export function normalizeOspfState(value: string): OspfNeighborState { return neighborStates[value.trim().toLowerCase()] ?? "Unknown" }
|
|
export function normalizeBfdState(value: string): BfdState { return bfdStates[value.trim().toLowerCase()] ?? "Unknown" }
|
|
export function ospfNodeState(states: OspfNeighborState[]): "Full" | "2-Way" | "Pending" | "Down" | "Unknown" | "none" {
|
|
if (!states.length) return "none"
|
|
if (states.includes("Full")) return "Full"
|
|
if (states.includes("Unknown")) return "Unknown"
|
|
if (states.includes("2-Way")) return "2-Way"
|
|
if (states.some(state => state !== "Down")) return "Pending"
|
|
return "Down"
|
|
}
|
|
|
|
/** Correlate only a single observed direct BFD session on an explicitly configured interface. */
|
|
export function ospfBfdConflicts<T extends { localRouter: string; localIface: string; neighborAddress?: string; state: OspfNeighborState; dataUnknown?: boolean }>(
|
|
neighbors: T[],
|
|
interfaces: Array<{ serverId: string; interfaceName: string; useBfd?: boolean | null; active?: boolean | null; dataUnknown?: boolean }>,
|
|
sessions: Array<{ serverId: string; iface: string; remoteAddr: string; state: BfdState; multihop?: boolean | null; dataUnknown?: boolean }>,
|
|
): T[] {
|
|
const ip = (value: string) => value.split("%")[0]?.toLowerCase() ?? ""
|
|
return neighbors.filter(n => {
|
|
if (n.dataUnknown || n.state !== "Full" || !n.neighborAddress || !n.localIface) return false
|
|
if (!interfaces.some(i => !i.dataUnknown && i.active === true && i.useBfd === true && i.serverId === n.localRouter && i.interfaceName === n.localIface)) return false
|
|
const matching = sessions.filter(s => !s.dataUnknown && s.multihop === false && s.serverId === n.localRouter && s.iface === n.localIface && ip(s.remoteAddr) === ip(n.neighborAddress!))
|
|
return matching.length === 1 && (matching[0]!.state === "Down" || matching[0]!.state === "AdminDown")
|
|
})
|
|
}
|