/*
 * Copyright (C) 2020 Volta Networks, Inc
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 *
 */
#ifndef _PATH_TED_H
#define _PATH_TED_H
#ifdef __cplusplus
extern "C" {
#endif
#include 
#include 
#include 
#include "linklist.h"
#include "log.h"
#include "command.h"
#include "stream.h"
#include "prefix.h"
#include "zclient.h"
#include "link_state.h"
extern struct ted_state ted_state_g;
#define TIMER_RETRY_DELAY 5 /* Timeout in seconds between ls sync request */
#define TED_KEY 1
#define TED_ASN 1
#define TED_NAME "PATHD TED"
enum igp_import {
	IMPORT_UNKNOWN = 0,
	IMPORT_ISIS,
	IMPORT_OSPFv2,
	IMPORT_OSPFv3
};
struct ted_state {
	struct thread_master *main;
	/* Status of TED: enable or disable */
	bool enabled;
	/* From which igp is going to receive data */
	enum igp_import import;
	/* The TED itself as in link_state.h */
	struct ls_ted *ted;
	/* Timer for ted sync */
	struct thread *t_link_state_sync;
	/* Timer for refresh sid in segment list */
	struct thread *t_segment_list_refresh;
	/* delay interval in seconds */
	uint32_t link_state_delay_interval;
	/* delay interval refresh in seconds */
	uint32_t segment_list_refresh_interval;
	struct debug dbg;
};
/* Debug flags. */
#define PATH_TED_DEBUG_BASIC   0x01
#define PATH_TED_DEBUG(fmt, ...)                                               \
	do {                                                                   \
		if (DEBUG_FLAGS_CHECK(&ted_state_g.dbg, PATH_TED_DEBUG_BASIC))  \
			DEBUGD(&ted_state_g.dbg, "mpls-te: " fmt, ##__VA_ARGS__); \
	} while (0)
#define PATH_TED_ERROR(fmt, ...)                                               \
	do {                                                                   \
		if (DEBUG_FLAGS_CHECK(&ted_state_g.dbg, PATH_TED_DEBUG_BASIC)) \
			DEBUGE(&ted_state_g.dbg, "mpls-te: " fmt, ##__VA_ARGS__); \
	} while (0)
#define PATH_TED_WARN(fmt, ...)                                                \
	do {                                                                   \
		if (DEBUG_FLAGS_CHECK(&ted_state_g.dbg, PATH_TED_DEBUG_BASIC))  \
			DEBUGW(&ted_state_g.dbg, "mpls-te: " fmt, ##__VA_ARGS__); \
	} while (0)
#define PATH_TED_INFO(fmt, ...)                                                \
	do {                                                                   \
		if (DEBUG_FLAGS_CHECK(&ted_state_g.dbg, PATH_TED_DEBUG_BASIC)) \
			DEBUGI(&ted_state_g.dbg, "mpls-te: " fmt, ##__VA_ARGS__); \
	} while (0)
/* TED management functions */
bool path_ted_is_initialized(void);
void path_ted_init(struct thread_master *master);
uint32_t path_ted_teardown(void);
void path_ted_timer_sync_cancel(void);
void path_ted_timer_refresh_cancel(void);
int path_ted_segment_list_refresh(void);
/* TED configuration functions */
uint32_t path_ted_config_write(struct vty *vty);
void path_ted_show_debugging(struct vty *vty);
/* TED util functions */
/* clang-format off */
#define LS_MSG_EVENT_PRINT(event) event == LS_MSG_EVENT_ADD?"add"\
		    : event == LS_MSG_EVENT_DELETE?"del"\
		    : event == LS_MSG_EVENT_UPDATE?"upd"\
		    : event == LS_MSG_EVENT_SYNC?"syn"\
		    : event == LS_MSG_EVENT_SYNC?"und" : "none"
#define LS_MSG_TYPE_PRINT(type) type == LS_MSG_TYPE_NODE?"node"\
		    : type == LS_MSG_TYPE_ATTRIBUTES?"att"\
		    : type == LS_MSG_TYPE_PREFIX?"pre" : "none"
#define LS_IGP_PRINT(type) type == ISIS_L1?"ISIS_L1"\
		    : type == ISIS_L2?"ISIS_L2"\
		    : type == DIRECT?"DIRECT"\
		    : type == STATIC?"STATIC"\
		    : type == OSPFv2?"OSPFv2" : "none"
#define PATH_TED_IGP_PRINT(type) type == IMPORT_OSPFv2?"OSPFv2"\
		    : type == IMPORT_OSPFv3?"OSPFv3"\
		    : type == IMPORT_ISIS?"ISIS" : "none"
/* clang-format on */
uint32_t path_ted_get_current_igp(uint32_t);
/* TED Query functions */
/*
 * Type of queries from draft-ietf-spring-segment-routing-policy-07 for types
 * f,c,e
 */
/**
 * Search for sid based in prefix and optional algo
 *
 * @param prefix	Net prefix to resolv
 * @param algo		Algorithm for link state
 *
 * @return		sid of attribute
 */
uint32_t path_ted_query_type_c(struct prefix *prefix, uint8_t algo);
/**
 * Search for sid based in prefix and interface id
 *
 * @param prefix	Net prefix to resolv
 * @param iface_id	The interface id
 *
 * @return		sid of attribute
 */
uint32_t path_ted_query_type_e(struct prefix *prefix, uint32_t iface_id);
/**
 * Search for sid based in local, remote pair
 *
 * @param local		local ip of attribute
 * @param remote	remote ip of attribute
 *
 * @return		sid of attribute
 */
uint32_t path_ted_query_type_f(struct ipaddr *local, struct ipaddr *remote);
/**
 * Handle the received opaque msg
 *
 * @param msg	Holds the ted data
 *
 * @return		sid of attribute
 */
uint32_t path_ted_rcvd_message(struct ls_message *msg);
#ifdef __cplusplus
}
#endif
#endif /* _PATH_TED_H */