/*
 * Copyright (c) 2025, Canonical, Ltd.
 * Copyright (c) 2025, STACKIT GmbH & Co. KG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ROUTE_H
#define ROUTE_H 1

#include <stdbool.h>
#include <netinet/in.h>
#include <net/if.h>
#include "openvswitch/hmap.h"
#include "vec.h"
#include "sset.h"
#include "smap.h"

struct hmap;
struct ovsdb_idl_index;
struct uuidset;
struct route_data;
struct sbrec_chassis;
struct sbrec_port_binding;
struct sbrec_datapath_binding;
struct sbrec_advertised_route;

struct route_ctx_in {
    const struct sbrec_advertised_route_table *advertised_route_table;
    struct ovsdb_idl_index *service_monitor_by_selector;
    struct ovsdb_idl_index *sbrec_port_binding_by_name;
    const struct sbrec_chassis *chassis;
    const char *dynamic_routing_port_mapping;
    const struct hmap *local_datapaths;
    struct shash *local_bindings;
};

struct route_ctx_out {
    struct hmap *tracked_re_datapaths;

    /* Contains the tracked_ports that in the last run were bound locally. */
    struct sset *tracked_ports_local;

    /* Contains the tracked_ports that in the last run were not bound
     * locally. */
    struct sset *tracked_ports_remote;

    /* Contains logical ports referenced by health-gated advertised routes. */
    struct sset *health_check_ports;

    /* Contains Service_Monitor UUIDs matched during the last route run. */
    struct uuidset *relevant_service_monitors;

    /* Contains all the currently configured dynamic-routing-port-name values
     * on all datapaths.
     */
    struct sset *filtered_ports;

    /* Contains struct advertise_datapath_entry */
    struct hmap *announce_routes;

    /* Contains struct advertised_route_status entries recorded by
     * route_run(), sorted by Advertised_Route UUID for publication in
     * Advertised_Route_Status by route_exchange_run(). */
    struct vector *advertised_route_status;
};

/* Per-route advertisement decision recorded by route_run() so that
 * route_exchange_run() (which holds a writable SB txn) can publish it for the
 * local chassis in Advertised_Route_Status. */
struct advertised_route_status {
    const struct sbrec_advertised_route *route;
    const char *desired_status;
    const char *withdrawal_reason;
    const char *withdrawal_reason_value;
};

struct advertise_datapath_entry {
    struct hmap_node node;

    const struct sbrec_datapath_binding *db;
    bool maintain_vrf;
    char vrf_name[IFNAMSIZ + 1];
    struct in6_addr ipv4_nexthop;
    struct in6_addr ipv6_nexthop;

    struct hmap routes;

    /* Result of the most recent attempt to reconcile 'routes' with the
     * chassis routing table. */
    bool routes_synced;
    int route_error;
    const char *route_error_description;

    /* The name of the port bindings locally bound for this datapath and
     * running route exchange logic.
     * The key is the port name and the value is the ifname if set. This may
     * be empty if the all ports specified dynamic-routing-port-name, but no
     * such referenced port is local. In this case we should only advertise
     * routes but not learn them. */
    struct smap bound_ports;
};

struct advertise_route_entry {
    struct hmap_node node;
    struct in6_addr addr;
    struct in6_addr nexthop;
    unsigned int plen;
    unsigned int priority;
};

const struct sbrec_port_binding *route_exchange_find_port(
    struct ovsdb_idl_index *sbrec_port_binding_by_name,
    const struct sbrec_chassis *chassis,
    const struct sbrec_port_binding *pb,
    const char **dynamic_routing_port_name);
uint32_t advertise_route_hash(const struct in6_addr *dst,
                              const struct in6_addr *nexthop,
                              unsigned int plen);
struct advertise_route_entry
advertise_route_from_route_data(const struct route_data *);
void route_run(struct route_ctx_in *, struct route_ctx_out *);
void route_cleanup(struct hmap *announce_routes);
void advertised_route_status_clear(struct vector *statuses);
void advertised_route_status_sort(struct vector *statuses);
const struct advertised_route_status *advertised_route_status_find(
    const struct vector *statuses,
    const struct sbrec_advertised_route *route);
uint32_t route_get_table_id(const struct sbrec_datapath_binding *);
struct advertise_route_entry *
advertise_route_find(unsigned int priority, const struct in6_addr *prefix,
                     unsigned int plen, const struct in6_addr *nexthop,
                     const struct hmap *advertised_routes);
struct advertise_datapath_entry *
advertise_datapath_find(const struct hmap *datapaths,
                        const struct sbrec_datapath_binding *);

#endif /* ROUTE_H */
