module PCRF_Tests {

import from TCCEncoding_Functions all;

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

import from DIAMETER_Types all;
import from DIAMETER_Templates all;
import from DIAMETER_ts29_212_Templates all;
import from DIAMETER_Emulation all;

import from Prometheus_Checker all;

type record of hexstring SubscriberConfigs;

modulepar {
	charstring mp_pcrf_hostname := "127.0.0.4";
	integer mp_pcrf_port := 3868;
	charstring mp_pcrf_prometheus_hostname := "127.0.0.5";
	integer mp_pcrf_prometheus_port := c_prometheus_default_http_port;
	charstring mp_diam_local_hostname := "127.0.0.1";
	integer mp_diam_local_port := 3868;
	charstring mp_diam_orig_realm := "localdomain";
	charstring mp_diam_orig_host := "smf.localdomain";
	charstring mp_diam_dest_realm := "localdomain";
	charstring mp_diam_dest_host := "pcrf.localdomain";
	SubscriberConfigs subscribers := {
		/* Existing subscriber, ULA returns SERVICE_GRANTED */
		'001010000000000'H,
		'001010000000001'H
	};
}

/* main component, we typically have one per testcase */
type component MTC_CT {

	/* emulated SMF */
	var DIAMETER_Emulation_CT vc_Gx;
	port DIAMETER_PT Gx_UNIT;
	port DIAMETEREM_PROC_PT Gx_PROC;
	/* global test case guard timer (actual timeout value is set in f_init()) */
	timer T_guard;
}

/* global altstep for global guard timer; */
altstep as_Tguard() runs on MTC_CT {
	[] T_guard.timeout {
			setverdict(fail, "Timeout of T_guard");
			mtc.stop;
		}
}

type component DIAMETER_ConnHdlr_CT extends DIAMETER_ConnHdlr {
	port DIAMETER_Conn_PT DIAMETER_CLIENT;
	port DIAMETEREM_PROC_PT DIAMETER_PROC_CLIENT;
}

function f_diam_connhldr_ct_main(hexstring imsi) runs on DIAMETER_ConnHdlr_CT {
	var DIAMETER_ConnHdlr vc_conn_unused;
	var PDU_DIAMETER msg;
	var UINT32 ete_id;

	f_diameter_expect_imsi(imsi);

	while (true) {
		alt {
		[] DIAMETER_CLIENT.receive(PDU_DIAMETER:?) -> value msg {
			DIAMETER.send(msg);
			}
		[] DIAMETER.receive(PDU_DIAMETER:?) -> value msg {
			DIAMETER_CLIENT.send(msg);
			}
		[] DIAMETER_PROC_CLIENT.getcall(DIAMETEREM_register_eteid:{?,?}) -> param(ete_id, vc_conn_unused) {
			DIAMETER_PROC.call(DIAMETEREM_register_eteid:{ete_id, self}) {
				[] DIAMETER_PROC.getreply(DIAMETEREM_register_eteid:{?,?}) {};
				}
			DIAMETER_PROC_CLIENT.reply(DIAMETEREM_register_eteid:{ete_id, vc_conn_unused});
			}
		}
	}
}

/* per-session component; we typically have 1..N per testcase */
type component Cli_Session_CT extends Prometheus_Checker_CT {
	var SessionPars	g_pars;

	port DIAMETER_Conn_PT Gx;
	port DIAMETEREM_PROC_PT Gx_PROC;

}
function f_diam_connhldr_expect_eteid(UINT32 ete_id) runs on Cli_Session_CT {
	Gx_PROC.call(DIAMETEREM_register_eteid:{ete_id, null}) {
		[] Gx_PROC.getreply(DIAMETEREM_register_eteid:{?,?}) {};
	}
}

/* configuration data for a given Session */
type record SessionPars {
	hexstring	imsi,
	uint32_t	gx_next_hbh_id,
	uint32_t	gx_next_ete_id
}

template (value) SessionPars
t_SessionPars(hexstring imsi, uint32_t	gx_next_hbh_id := 1000, uint32_t gx_next_ete_id := 22220) := {
	imsi := imsi,
	gx_next_hbh_id := gx_next_hbh_id,
	gx_next_ete_id := gx_next_ete_id
}

type function void_fn() runs on Cli_Session_CT;

friend function DiameterForwardUnitdataCallback(PDU_DIAMETER msg)
runs on DIAMETER_Emulation_CT return template PDU_DIAMETER {
	DIAMETER_UNIT.send(msg);
	return omit;
}

friend function f_init_diameter(charstring id) runs on MTC_CT {
	var DIAMETEROps ops := {
		create_cb := refers(DIAMETER_Emulation.ExpectedCreateCallback),
		unitdata_cb := refers(DiameterForwardUnitdataCallback),
		raw := false /* handler mode (IMSI based routing) */
	};
	var DIAMETER_conn_parameters pars;

	/* Gx setup: */
	pars := {
		remote_ip := mp_pcrf_hostname,
		remote_sctp_port := mp_pcrf_port,
		local_ip := mp_diam_local_hostname,
		local_sctp_port := mp_diam_local_port,
		origin_host := mp_diam_orig_host,
		origin_realm := mp_diam_orig_realm,
		auth_app_id := omit,
		vendor_app_id := c_DIAMETER_3GPP_Gx_AID
	};
	vc_Gx := DIAMETER_Emulation_CT.create(id);
	map(vc_Gx:DIAMETER, system:DIAMETER_CODEC_PT);
	connect(vc_Gx:DIAMETER_UNIT, self:Gx_UNIT);
	connect(vc_Gx:DIAMETER_PROC, self:Gx_PROC);
	vc_Gx.start(DIAMETER_Emulation.main(ops, pars, id));

	f_diameter_wait_capability(Gx_UNIT);
	/* Give some time for our emulation to get out of SUSPECT list of SUT (3 watchdog ping-pongs):
	 * RFC6733 sec 5.1
	 * RFC3539 sec 3.4.1 [5]
	 * https://github.com/freeDiameter/freeDiameter/blob/master/libfdcore/p_psm.c#L49
	 */
	f_sleep(1.0);
}

private function f_init(float guard_timeout := 60.0) runs on MTC_CT {
	T_guard.start(guard_timeout);
	activate(as_Tguard());
	f_init_diameter(testcasename());
}

function f_start_handler(void_fn fn, template (omit) SessionPars pars_tmpl := omit)
runs on MTC_CT return Cli_Session_CT {
	var charstring id := testcasename();
	var DIAMETER_ConnHdlr_CT vc_conn_gx;
	var Cli_Session_CT vc_conn;
	var SessionPars pars;

	if (isvalue(pars_tmpl)) {
		pars := valueof(pars_tmpl);
	} else {
		/*TODO: set default values */
	}

	vc_conn := Cli_Session_CT.create(id);

	vc_conn_gx := DIAMETER_ConnHdlr_CT.create(id);
	connect(vc_conn_gx:DIAMETER, vc_Gx:DIAMETER_CLIENT);
	connect(vc_conn_gx:DIAMETER_PROC, vc_Gx:DIAMETER_PROC);
	connect(vc_conn:Gx, vc_conn_gx:DIAMETER_CLIENT);
	connect(vc_conn:Gx_PROC, vc_conn_gx:DIAMETER_PROC_CLIENT);
	vc_conn_gx.start(f_diam_connhldr_ct_main(pars.imsi));

	vc_conn.start(f_handler_init(fn, pars));
	return vc_conn;
}

private function f_handler_init(void_fn fn, SessionPars pars)
runs on Cli_Session_CT {
	g_pars := valueof(pars);
	f_prometheus_init(mp_pcrf_prometheus_hostname, mp_pcrf_prometheus_port);
	fn.apply();
}

/* CCR + CCA against PCRF */
private function f_dia_ccr_cca() runs on Cli_Session_CT {
	var octetstring sess_id := char2oct("foobar");
	var PDU_DIAMETER rx_dia;
	var UINT32 hbh_id := int2oct(g_pars.gx_next_hbh_id, 4);
	var UINT32 ete_id := int2oct(g_pars.gx_next_ete_id, 4);
	var octetstring imsi := char2oct(f_dec_TBCD(imsi_hex2oct(g_pars.imsi)));
	var octetstring apn := char2oct("internet");

	/* Unlike CCR, CCA contains no IMSI. Register ete_id in DIAMETER_Emulation,
	 * so CCA is forwarded back to us in DIAMETER port instead of MTC_CT.DIAMETER_UNIT.
	 */
	f_diam_connhldr_expect_eteid(ete_id);

	/* TODO: change this into a ts_DIA_ULR */
	Gx.send(ts_DIA_Gx_CCR(hbh_id, ete_id,
			      sess_id,
			      {ts_AVP_SubcrIdType(END_USER_IMSI), ts_AVP_SubcrIdData(imsi)},
			      apn,
			      INITIAL_REQUEST,
			      req_num := '00000000'O
			      ));
	g_pars.gx_next_hbh_id := g_pars.gx_next_hbh_id + 1;
	g_pars.gx_next_ete_id := g_pars.gx_next_ete_id + 1;

	alt {
	[] Gx.receive(tr_DIA_Gx_CCA(sess_id)) -> value rx_dia {
		setverdict(pass);
		}
	[] Gx.receive(PDU_DIAMETER:?) -> value rx_dia {
		Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
					log2str("Received unexpected DIAMETER ", rx_dia));
		}
	}
}

/* Test that PCRF can serve metrics over prometheus */
private function f_TC_ccr_cca() runs on Cli_Session_CT {
	var PrometheusExpects expects := valueof({
		ts_PrometheusExpect("gx_rx_ccr", COUNTER, min := 1, max := 1),
		ts_PrometheusExpect("gx_tx_cca", COUNTER, min := 1, max := 1)
	});
	var PrometheusMetrics prom_snapshot := f_prometheus_snapshot(f_prometheus_keys_from_expect(expects));

	f_dia_ccr_cca();

	f_prometheus_expect_from_snapshot(expects, wait_converge := true, snapshot := prom_snapshot);
	setverdict(pass);
}
testcase TC_ccr_cca() runs on MTC_CT {
	var Cli_Session_CT vc_conn;
	var SessionPars pars := valueof(t_SessionPars(subscribers[0]));
	f_init();
	vc_conn := f_start_handler(refers(f_TC_ccr_cca), pars);
	vc_conn.done;
}


control {
	execute( TC_ccr_cca() );
}


}