module CPF_ConnectionHandler {

/* CPF Connection Handler of UPF_Tests in TTCN-3
 * (C) 2022 by sysmocom - s.f.m.c. GmbH <info@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
 */

import from Misc_Helpers all;
import from General_Types all;
import from Osmocom_Types all;
import from IPL4asp_Types all;
import from Native_Functions all;

import from StatsD_Checker all;

import from TELNETasp_PortType all;
import from Osmocom_VTY_Functions all;

import from PFCP_Types all;
import from PFCP_Emulation all;

/* The system under test is a UPF (User Plane Function).  This component represents a Control Plane Function, with a
 * single association via PFCP to the UPF (User Plane Function). */
type component CPF_ConnHdlr extends StatsD_ConnHdlr {
	port PFCPEM_PT PFCP;
	port TELNETasp_PT UPFVTY;

	var PFCP_Emulation_CT vc_PFCP;

	var TestHdlrParams g_pars;

	var boolean g_vty_initialized := false;
	var integer g_recovery_timestamp;
	var integer g_next_seid_state;
	var integer g_next_local_teid_state;
	var integer g_next_remote_teid_state;
	var integer g_next_ue_addr_state;
}

function f_next_seid() runs on CPF_ConnHdlr return OCT8 {
	g_next_seid_state := g_next_seid_state + 1;
	return int2oct(g_next_seid_state, 8);
}

function f_next_remote_teid() runs on CPF_ConnHdlr return OCT4 {
	g_next_remote_teid_state := g_next_remote_teid_state + 1;
	return int2oct(g_next_remote_teid_state, 4);
}

function f_next_ue_addr() runs on CPF_ConnHdlr return charstring {
	g_next_ue_addr_state := g_next_ue_addr_state + 1;
	if (g_next_ue_addr_state > 254) {
		g_next_ue_addr_state := 16;
	}
	return "192.168.44." & int2str(g_next_ue_addr_state);
}

function f_CPF_ConnHdlr_init_vty() runs on CPF_ConnHdlr {
	if (not g_vty_initialized) {
		map(self:UPFVTY, system:UPFVTY);
		f_vty_set_prompts(UPFVTY);
		f_vty_transceive(UPFVTY, "enable");
		g_vty_initialized := true;
	}
}

private function f_CPF_ConnHdlr_init_pfcp(charstring id) runs on CPF_ConnHdlr {
	id := id & "-PFCP";

	g_recovery_timestamp := f_rnd_int(4294967295);
	g_next_seid_state := (1 + f_rnd_int(65534)) * 65536;
	g_next_local_teid_state := (1 + f_rnd_int(65534)) * 65536;
	g_next_remote_teid_state := (1 + f_rnd_int(65534)) * 65536;
	g_next_ue_addr_state := (1 + f_rnd_int(15)) * 16;

	var PFCP_Emulation_Cfg pfcp_cfg := {
		pfcp_bind_ip := g_pars.local_addr,
		pfcp_bind_port := g_pars.local_port,
		pfcp_remote_ip := g_pars.remote_upf_addr,
		pfcp_remote_port := g_pars.remote_upf_port,
		role := CPF
	};

	vc_PFCP := PFCP_Emulation_CT.create(id) alive;
	connect(self:PFCP, vc_PFCP:CLIENT);
	vc_PFCP.start(PFCP_Emulation.main(pfcp_cfg));
}

/* initialize all parameters */
function f_CPF_ConnHdlr_init(charstring id, TestHdlrParams pars) runs on CPF_ConnHdlr {
	g_pars := valueof(pars);
	f_CPF_ConnHdlr_init_pfcp(id);
	f_CPF_ConnHdlr_init_vty();
}

type record TestHdlrParams {
	charstring remote_upf_addr,
	integer remote_upf_port,
	charstring local_addr,
	integer local_port,
	Node_ID local_node_id
};

/* Note: Do not use valueof() to get a value of this template, use
 * f_gen_test_hdlr_pars() instead in order to get a configuration. */
template (value) TestHdlrParams t_def_TestHdlrPars := {
	remote_upf_addr := "127.0.0.1",
	remote_upf_port := 8805,
	local_addr := "127.0.0.2",
	local_port := 8805
}

}
