import { z } from "zod" import {ospfOptimizerObservedSchema} from "@mmapp/contracts" import { vxlanServersSchema } from "@/lib/vxlan-response" const counter = z.number().int().nonnegative() const base = z.object({ id: z.string().trim().min(1), serverId: z.number().int().positive(), serverName: z.string(), serverSite: z.string(), serverCountry: z.string().default("UN") }) export const ospfCatalogSchema = z.object({ neighbors: z.array(base.extend({ address: z.string(), routerId: z.string(), instance: z.string(), area: z.string(), areaId: z.string(), interface: z.string(), state: z.string(), uptime: z.string().nullable(), stateChanges: counter.nullable(), priority: counter.max(255).nullable() })), interfaces: z.array(base.extend({ instance: z.string(), area: z.string(), areaId: z.string(), interface: z.string(), cost: counter.max(65535).nullable(), type: z.string().nullable(), disabled: z.boolean().nullable(), inactive: z.boolean().nullable(), priority: counter.max(255).nullable(), helloInterval: z.number().finite().nonnegative().nullable(), deadInterval: z.number().finite().nonnegative().nullable(), useBfd: z.boolean().nullable() })), instances: z.array(base.extend({ name: z.string(), routerId: z.string().nullable(), version: z.union([z.literal(2), z.literal(3)]).nullable(), disabled: z.boolean().nullable(), inactive: z.boolean().nullable(), redistribute: z.string() })), bfdSessions: z.array(base.extend({ localAddr: z.string(), remoteAddr: z.string(), interface: z.string(), state: z.string(), uptime: z.string().nullable(), multihop: z.boolean().nullable(), multiplier: counter.max(255).nullable(), txInterval: z.number().finite().nonnegative().nullable(), rxInterval: z.number().finite().nonnegative().nullable(), holdTime: z.number().finite().nonnegative().nullable(), packetsRx: counter.nullable(), packetsTx: counter.nullable(), stateChanges: counter.nullable() })), routes: z.array(base.extend({ destination: z.string(), type: z.enum(["O", "O IA", "O E1", "O E2", "Unknown"]), cost: counter.max(4294967295).nullable(), nextHop: z.string(), via: z.string(), area: z.string() })), failures: z.array(z.object({ serverId: z.number().int().positive(), serverName: z.string(), error: z.string() })), }).refine(data => { const failed = new Set(data.failures.map(f => f.serverId)) return failed.size === data.failures.length && [data.neighbors, data.interfaces, data.instances, data.bfdSessions, data.routes].every(rows => new Set(rows.map(row => JSON.stringify([row.serverId, row.id]))).size === rows.length && rows.every(row => !failed.has(row.serverId))) }) export const ospfServersSchema = vxlanServersSchema export type OspfCatalog = z.infer export const ospfOptimizeSchema = z.object({ serverId: z.number().int().positive(), serverName: z.string(), pingWeight: z.number().int().min(0).max(100), optimizedCount: z.number().int().nonnegative(), applied: z.array(z.object({ interface: z.string().min(1), from: z.number().int().min(1).max(65535), to: z.number().int().min(1).max(65535) })), interfaces: z.array(z.object({ observed: ospfOptimizerObservedSchema.optional(), id: z.string().trim().min(1), interface: z.string(), currentCost: z.number().int().min(1).max(65535), optimalCost: z.number().int().min(1).max(65535), score: z.number().finite(), pingMs: z.number().finite().nonnegative(), dlMbps: z.number().finite().nonnegative(), ulMbps: z.number().finite().nonnegative() })), }).refine(data => data.optimizedCount === data.applied.length && new Set(data.interfaces.map(row => row.id)).size === data.interfaces.length && new Set(data.applied.map(row => row.interface)).size === data.applied.length && data.applied.every(row => data.interfaces.some(iface => iface.interface === row.interface && iface.currentCost === row.from && iface.optimalCost === row.to)))