-- 008_obmp_policy_diff.sql
-- Policy Diff feature -- per-router routing-policy retrieval plus per-neighbor
-- received / kept / advertised counts, populated by the obmp-rib-poller
-- service (obmp-rib-poller/poller.py).
--
-- Why a poller and not BMP: on the lab's XRv9000 24.3.1 routers BMP only
-- carries pre-policy Adj-RIB-In. `route-monitoring inbound post-policy`
-- replaces (does not supplement) the pre-policy export and is not flagged
-- distinctly, and adj-rib-out BMP (RFC 8671) is unsupported on that image.
-- So "kept after inbound policy" and "advertised" are pulled directly from
-- the routers over CLI + NETCONF.
--
-- The Policy Diff dashboard joins router_rib_stats against BMP ip_rib
-- (received, pre-policy) to show received vs kept vs discarded vs advertised.

-- Full route-policy (RPL) bodies, one row per policy per router.
CREATE TABLE IF NOT EXISTS route_policies (
    router_hash_id  uuid          NOT NULL,
    policy_name     varchar(256)  NOT NULL,
    body            text,
    retrieved_at    timestamptz   NOT NULL DEFAULT now(),
    PRIMARY KEY (router_hash_id, policy_name)
);

-- Which route-policy is bound inbound/outbound on each neighbor address-family.
CREATE TABLE IF NOT EXISTS neighbor_policy_bind (
    router_hash_id  uuid          NOT NULL,
    peer_addr       inet          NOT NULL,
    afi             varchar(8)    NOT NULL,   -- ipv4 / ipv6
    direction       varchar(4)    NOT NULL,   -- in / out
    policy_name     varchar(256),
    retrieved_at    timestamptz   NOT NULL DEFAULT now(),
    PRIMARY KEY (router_hash_id, peer_addr, afi, direction)
);

-- Per-neighbor RIB sizes pulled from the router: accepted_count is the
-- post-inbound-policy prefix count (BGP summary PfxRcd), advertised_count is
-- the adj-rib-out size toward that neighbor.
CREATE TABLE IF NOT EXISTS router_rib_stats (
    router_hash_id    uuid        NOT NULL,
    peer_addr         inet        NOT NULL,
    afi               varchar(8)  NOT NULL,
    peer_as           bigint,
    session_state     varchar(32),
    accepted_count    integer,
    advertised_count  integer,
    polled_at         timestamptz NOT NULL DEFAULT now(),
    PRIMARY KEY (router_hash_id, peer_addr, afi)
);

CREATE INDEX IF NOT EXISTS idx_route_policies_router      ON route_policies (router_hash_id);
CREATE INDEX IF NOT EXISTS idx_neighbor_policy_bind_router ON neighbor_policy_bind (router_hash_id);
CREATE INDEX IF NOT EXISTS idx_router_rib_stats_router     ON router_rib_stats (router_hash_id);
