/* REST interface definitions for OsmoS1GW
 *
 * (C) 2025 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 * Author: Vadim Yanitskiy <vyanitskiy@sysmocom.de>
 *
 * All rights reserved.
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

module S1GW_REST_Types {

type charstring GlobalEnbId (pattern "[0-9]#3-[0-9]#(2,3)-[0-9]+");
type charstring Pid (pattern "<[0-9]+.[0-9]+.[0-9]+>");

type integer EnbHandle (0..infinity);
type integer SEID (0..18446744073709551615);
type integer TEID (0..4294967295);
type integer Port (0..65535);

type union ParamMmeId {
	charstring name (pattern "name:*"),
	charstring addr (pattern "addr:[0-9a-f:.]+-[0-9]+")
} with { encode "TEXT" };

type union ParamEnbId {
	charstring handle (pattern "handle:[0-9]+"),
	charstring pid (pattern "pid:[0-9]+.[0-9]+.[0-9]+"),
	charstring genbid (pattern "genbid:[0-9]#3-[0-9]#(2,3)-[0-9]+"),
	charstring sctp_aid (pattern "(enb|mme)-sctp-aid:[0-9]+"),
	charstring enb_conn (pattern "enb-conn:[0-9a-f:.]+-[0-9]+")
} with { encode "TEXT" };

type union ParamErabId {
	charstring pid (pattern "pid:[0-9]+.[0-9]+.[0-9]+")
} with { encode "TEXT" };

type set OperationResult {
	boolean success,
	charstring msg optional
} with {
	variant(msg) "JSON:name as message";
};

type set of MetricsItem MetricsList;
type set MetricsItem {
	MetricType metric_type,
	charstring metric_name,
	integer metric_value
} with {
	variant(metric_type) "JSON:name as type";
	variant(metric_name) "JSON:name as name";
	variant(metric_value) "JSON:name as value";
};

type enumerated MetricType {
	counter,
	gauge
};

type set PfcpAssocInfo {
	PfcpAssocState state,		/* Current association state */
	charstring laddr,		/* Local (bind) IP address */
	charstring raddr,		/* Remote (connect) IP address */
	integer lrts,			/* Local Recovery TimeStamp */
	integer rrts optional		/* Remote Recovery TimeStamp */
};

type enumerated PfcpAssocState {
	connecting,
	connected
};

type set of MmeItem MmeList;
type set MmeItem {
	charstring name,		/* Unique, human-readable identifier */
	charstring laddr optional,	/* Local (bind) IP address (default: any) */
	charstring raddr,		/* Remote (connect) IP address */
	Port rport optional,		/* Remote port (default: 36412) */
	TacList tac_list optional	/* List of allowed TACs (empty means allow-all) */
};

type record of integer TacList;

type set of EnbItem EnbList;
type set EnbItem {
	EnbHandle handle,		/* Unique number in the eNB registry */
	Pid pid,			/* Process ID */
	EnbState state,			/* Connection state */
	integer uptime,			/* Uptime (in seconds) */
	integer erab_count,		/* Total number of E-RABs */
	GlobalEnbId genb_id optional,	/* Global-eNB-ID (e.g. "001-01-1337") */
	charstring enb_saddr optional,	/* Source (remote) address of the eNB */
	charstring mme_daddr optional,	/* Destination (remote) address of the MME */
	Port enb_sport optional,	/* Source (remote) port of the eNB-S1GW connection */
	Port mme_sport optional,	/* Source (local) port of the S1GW-MME connection */
	Port mme_dport optional,	/* Destination (remote) port of the S1GW-MME connection */
	integer enb_sctp_aid optional,	/* SCTP association identifier of the eNB-S1GW connection */
	integer mme_sctp_aid optional	/* SCTP association identifier of the eNB-S1GW connection */
};

type enumerated EnbState {
	connecting,
	connected,
	s1setup
};

type set of ErabItem ErabList;
type set ErabItem {
	integer mme_ue_id,		/* MME-UE-S1AP-ID */
	integer erab_id,		/* E-RAB-ID */
	charstring state,		/* E-RAB FSM state */
	Pid pid,			/* E-RAB FSM process ID */
	SEID pfcp_lseid optional,	/* PFCP SEID (local) */
	SEID pfcp_rseid optional,	/* PFCP SEID (remote) */
	FTEID f_teid_u2c optional,	/* GTP-U F-TEID for UPF -> Core */
	FTEID f_teid_c2u optional,	/* GTP-U F-TEID for UPF <- Core */
	FTEID f_teid_a2u optional,	/* GTP-U F-TEID for UPF <- Access */
	FTEID f_teid_u2a optional	/* GTP-U F-TEID for UPF -> Access */

};

type set FTEID {
	TEID teid,			/* GTP-U TEID */
	charstring tla			/* GTP-U TLA (Transport Layer Address) */
};

external function enc_ParamMmeId(in ParamMmeId mme_id) return charstring
	with { extension "prototype(convert) encode(TEXT)" }
external function enc_ParamEnbId(in ParamEnbId enb_id) return charstring
	with { extension "prototype(convert) encode(TEXT)" }
external function enc_ParamErabId(in ParamErabId erab_id) return charstring
	with { extension "prototype(convert) encode(TEXT)" }

external function enc_MmeItem(in MmeItem mme_item) return octetstring
	with { extension "prototype(convert) encode(JSON)" }

external function dec_OperationResult(in octetstring data) return OperationResult
	with { extension "prototype(convert) decode(JSON)" }
external function dec_MetricsList(in octetstring data) return MetricsList
	with { extension "prototype(convert) decode(JSON)" }
external function dec_PfcpAssocInfo(in octetstring data) return PfcpAssocInfo
	with { extension "prototype(convert) decode(JSON)" }
external function dec_MmeList(in octetstring data) return MmeList
	with { extension "prototype(convert) decode(JSON)" }
external function dec_MmeItem(in octetstring data) return MmeItem
	with { extension "prototype(convert) decode(JSON)" }
external function dec_EnbList(in octetstring data) return EnbList
	with { extension "prototype(convert) decode(JSON)" }
external function dec_EnbItem(in octetstring data) return EnbItem
	with { extension "prototype(convert) decode(JSON)" }
external function dec_ErabList(in octetstring data) return ErabList
	with { extension "prototype(convert) decode(JSON)" }
external function dec_ErabItem(in octetstring data) return ErabItem
	with { extension "prototype(convert) decode(JSON)" }


template (value) MmeItem
ts_MmeItem(template (value) charstring name,
	   template (value) charstring raddr,
	   template (omit) charstring laddr := omit,
	   template (omit) Port rport := omit,
	   template (omit) TacList tac_list := omit) := {
	name := name,
	laddr := laddr,
	raddr := raddr,
	rport := rport,
	tac_list := tac_list
}
template (present) MmeItem
tr_MmeItem(template (present) charstring name := ?,
	   template (present) charstring raddr := ?,
	   template charstring laddr := *,
	   template Port rport := *,
	   template TacList tac_list := *) := {
	name := name,
	laddr := laddr,
	raddr := raddr,
	rport := rport,
	tac_list := tac_list
}

} with { encode "JSON" }
