module CBC_Tests {

import from Osmocom_Types all;
import from Socket_API_Definitions all;

import from GSM_Types all;
import from BSSAP_Types all;
import from BSSMAP_Templates all;
import from CBSP_Types all;
import from CBSP_Templates all;
import from CBSP_Adapter all;
import from CBSP_CodecPort all;

import from SABP_Types all;
import from SABP_Templates all;
import from SABP_IEs all;
import from SABP_PDU_Descriptions all;

import from SBC_AP_IEs all;
import from SBC_AP_Constants all;
import from SBC_AP_PDU_Contents all;
import from SBC_AP_PDU_Descriptions all;
import from SBC_AP_Types all;
import from SBC_AP_Templates all;
import from SBC_AP_CodecPort all;
import from SBC_AP_Adapter all;

import from HTTP_Adapter all;
import from HTTPmsg_Types all;
import from ECBE_Types all;

import from CBS_Message all;
import from ECBE_Components all;
import from BSC_ConnectionHandler all;
import from MME_ConnectionHandler all;

const integer MAX_BSC := 2;
const integer MAX_MME := 2;

type record BSC_modulepar_cfg {
	boolean tcp_is_client
};
type record of BSC_modulepar_cfg BSC_modulepar_cfgs;

type record MME_modulepar_cfg {
	boolean sctp_is_client
};
type record of MME_modulepar_cfg MME_modulepar_cfgs;

modulepar {
	charstring mp_local_host := "127.0.0.2";
	charstring mp_cbc_host := "127.0.0.1";
	integer mp_cbc_cbsp_port := 48049;
	integer mp_cbc_sbcap_port := c_SBC_AP_PORT;
	integer mp_cbc_ecbe_port := 12345;
	integer mp_local_cbsp_port := 15000;
	integer mp_local_sbcap_port := 16000;
	/* Must match osmo-cbc.cfg: */
	BSC_modulepar_cfgs mp_bsc_cfg := {
		{ tcp_is_client := true },
		{ tcp_is_client := false }
	};
	MME_modulepar_cfgs mp_mme_cfg := {
		{ sctp_is_client := true },
		{ sctp_is_client := false }
	};
};

type component test_CT extends CBSP_Adapter_CT, http_CT {
	timer g_Tguard := 60.0;
	var integer g_num_bsc;
	var integer g_num_mme;
	var BSC_ConnHdlr g_vc_conn_BSC[MAX_BSC];
	var MME_ConnHdlr g_vc_conn_MME[MAX_MME];
	var BSC_ConnHdlrPars g_pars_BSC[MAX_BSC];
	var MME_ConnHdlrPars g_pars_MME[MAX_MME];
	port BSC_ConnHdlr_Coord_PT COORD_BSC[MAX_BSC];
	port MME_ConnHdlr_Coord_PT COORD_MME[MAX_BSC];
};

private function f_shutdown_helper() runs on test_CT {
	/* Wait for all BSC cons to terminate */
	for (var integer i := 0; i < g_num_bsc; i := i + 1) {
		g_vc_conn_BSC[i].done;
	}
	/* Wait for all MME cons to terminate */
	for (var integer i := 0; i < g_num_mme; i := i + 1) {
		g_vc_conn_MME[i].done;
	}
	all component.stop;
	setverdict(pass);
	mtc.stop;
}

/* altstep for the global guard timer */
private altstep as_Tguard() runs on test_CT {
	[] g_Tguard.timeout {
		setverdict(fail, "Tguard timeout");
		all component.stop;
		mtc.stop;
	}
}

/*
 * BSC Conn Handler:
 */
private function f_BSC_ConnHdlr_start_fn_void() runs on BSC_ConnHdlr {
	log("Default start_fn() function called!");
}
private function f_init_pars_bsc(charstring bsc_host, integer bsc_cbsp_port,
				 charstring cbc_host, integer cbc_cbsp_port,
				 boolean tcp_is_client)
		runs on test_CT return BSC_ConnHdlrPars {
	var BSC_ConnHdlrPars pars := {
		bsc_host := bsc_host,
		bsc_cbsp_port := bsc_cbsp_port,
		cbc_host := cbc_host,
		cbc_cbsp_port := cbc_cbsp_port,
		tcp_is_client := tcp_is_client,
		start_fn := refers(f_BSC_ConnHdlr_start_fn_void),
		exp_cbs_msg := omit,
		cell_list_success := omit,
		tx_fail_list := omit
	};
	return pars;
}

private function f_init_bsc(integer idx, charstring id) runs on test_CT return BSC_ConnHdlr {
	var BSC_ConnHdlr vc_conn;
	id := id & "-BSC" & int2str(idx);
	vc_conn := BSC_ConnHdlr.create(id) alive;
	g_pars_BSC[idx] := f_init_pars_bsc(mp_local_host, mp_local_cbsp_port + idx,
					   mp_cbc_host, mp_cbc_cbsp_port,
					   mp_bsc_cfg[idx].tcp_is_client);
	connect(self:COORD_BSC[idx], vc_conn:COORD);
	return vc_conn;
}

private function f_start_bsc(integer idx, charstring id, BSC_ConnHdlrPars pars)
		runs on test_CT {
	id := id & "-BSC" & int2str(idx);
	g_vc_conn_BSC[idx] := f_init_bsc(idx, id);
	g_vc_conn_BSC[idx].start(f_BSC_ConnHdlr_main(id, pars));
}

/*
 * MME Conn Handler:
 */
private function f_MME_ConnHdlr_start_fn_void() runs on MME_ConnHdlr {
	log("Default start_fn() function called!");
}
private function f_init_pars_mme(charstring mme_host, integer mme_sbcap_port,
				 charstring cbc_host, integer cbc_sbcap_port,
				 boolean sctp_is_client)
		runs on test_CT return MME_ConnHdlrPars {
	var MME_ConnHdlrPars pars := {
		mme_host := mme_host,
		mme_sbcap_port := mme_sbcap_port,
		cbc_host := cbc_host,
		cbc_sbcap_port := cbc_sbcap_port,
		sctp_is_client := sctp_is_client,
		start_fn := refers(f_MME_ConnHdlr_start_fn_void),
		exp_cbs_msg := omit,
		write_replace_warning_req_cause := SBC_AP_Cause_message_accepted,
		write_replace_warning_ind_cause := omit,
		write_repl_unknown_TAIs := omit,
		bcast_cell_id_list := omit
	};
	return pars;
}

private function f_init_mme(integer idx, charstring id) runs on test_CT return MME_ConnHdlr {
	var MME_ConnHdlr vc_conn;
	id := id & "-MME" & int2str(idx);
	vc_conn := MME_ConnHdlr.create(id) alive;
	g_pars_MME[idx] := f_init_pars_mme(mp_local_host, mp_local_sbcap_port + idx,
					   mp_cbc_host, mp_cbc_sbcap_port,
					   mp_mme_cfg[idx].sctp_is_client);
	connect(self:COORD_MME[idx], vc_conn:COORD);
	return vc_conn;
}

private function f_start_mme(integer idx, charstring id, MME_ConnHdlrPars pars)
		runs on test_CT {
	id := id & "-MME" & int2str(idx);
	g_vc_conn_MME[idx] := f_init_mme(idx, id);
	g_vc_conn_MME[idx].start(f_MME_ConnHdlr_main(id, pars));
}

private function f_init(integer num_bsc := 0, integer num_mme := 0) runs on test_CT {
	var HTTP_Adapter_Params http_adapter_pars := {
		http_host := mp_cbc_host,
		http_port := mp_cbc_ecbe_port,
		use_ssl := false
	};
	f_http_init(http_adapter_pars);

	g_num_bsc := num_bsc;
	for (var integer i := 0; i < g_num_bsc; i := i + 1) {
		g_vc_conn_BSC[i] := f_init_bsc(i, testcasename());
	}

	g_num_mme := num_mme;
	for (var integer i := 0; i < g_num_mme; i := i + 1) {
		g_vc_conn_MME[i] := f_init_mme(i, testcasename());
	}
}

function f_start(float t_guard := 60.0) runs on test_CT {
	/* Start guard timer and activate it as default */
	g_Tguard.start(t_guard);
	activate(as_Tguard());

	for (var integer i := 0; i < g_num_bsc; i := i + 1) {
		f_start_bsc(i, testcasename(), g_pars_BSC[i]);
	}
	for (var integer i := 0; i < g_num_mme; i := i + 1) {
		f_start_mme(i, testcasename(), g_pars_MME[i]);
	}

	/* Now wait for conns to be ready: */
	for (var integer i := 0; i < g_num_bsc; i := i + 1) {
		COORD_BSC[i].receive(COORD_MSG_CONNECTED);
	}
	for (var integer i := 0; i < g_num_mme; i := i + 1) {
		COORD_MME[i].receive(COORD_MSG_CONNECTED);
	}
}

/* test whether or not we receive a valid KEEP-ALIVE from the CBC */
private function f_bsc_TC_rx_keepalive() runs on BSC_ConnHdlr {
	var CBSP_PDU rx;
	var CBSP_IE ie;
	rx := f_cbsp_exp(tr_CBSP_KEEP_ALIVE(?));
	f_cbsp_find_ie(rx, CBSP_IEI_KEEP_ALIVE_REP_PERIOD, ie);
}
testcase TC_rx_keepalive() runs on test_CT {

	f_init(num_bsc := 1);
	g_pars_BSC[0].start_fn := refers(f_bsc_TC_rx_keepalive);
	f_start();
	f_shutdown_helper();
}

/* test whether CBC terminates connection if KEEP-ALIVE is not answered by BSC */
private function f_bsc_TC_rx_keepalive_timeout() runs on BSC_ConnHdlr {
	var CBSP_PDU rx;
	var CBSP_IE ie;
	var integer ka_rep_per_s;

	rx := f_cbsp_exp(tr_CBSP_KEEP_ALIVE(?));
	f_cbsp_find_ie(rx, CBSP_IEI_KEEP_ALIVE_REP_PERIOD, ie);

	/* sleep for longer than the keep-alive period */
	ka_rep_per_s := f_cbsp_period2s(ie.body.keep_alive_rep_period);
	f_sleep(int2float(ka_rep_per_s + 5));

	/* expect the CBSP connection to be closed */
	CBSP[0].receive(PortEvent:{connClosed:=?})
}
testcase TC_rx_keepalive_timeout() runs on test_CT {
	f_init(num_bsc := 1);
	g_pars_BSC[0].start_fn := refers(f_bsc_TC_rx_keepalive_timeout);
	f_start(t_guard := 100.0);
	f_shutdown_helper();
}

private const BSSMAP_FIELD_CellIdentificationList cil_BSS := {
	cIl_allInBSS := ''O
};
private function f_bsc_TC_write_replace() runs on BSC_ConnHdlr {
	f_cbsp_handle_write(g_pars.exp_cbs_msg);
	f_sleep(100.0);
}
testcase TC_write_replace() runs on test_CT {
	var CBS_Message msg := {
		msg_id := 42,
		ser_nr := 16752,
		old_ser_nr := omit,
		cell_list := cil_BSS,
		channel_ind := 0,
		category := CBSP_CATEG_NORMAL,
		rep_period := 5,
		num_bcast_req := 3,
		dcs := 1,
		content := {
			{ '00'O, 1 }
		}
	};

	f_init(num_bsc := 1);
	g_pars_BSC[0].exp_cbs_msg := msg;
	g_pars_BSC[0].start_fn := refers(f_bsc_TC_write_replace);
	f_start();
	f_shutdown_helper();
}

testcase TC_selftest() runs on test_CT {
	const octetstring c_load_q := '0700000d0400080000f110012345671200'O;
	const octetstring c_load_q_compl := '0800000f0a000a0000f1100123456700001200'O;
	const octetstring c_reset := '1000000b0400080000f11001234567'O;
	const octetstring c_reset_compl := '1100000b0400080000f11001234567'O;
	const octetstring c_msg_sts_q := '0a0000130e022b0200000400080000f110012345671200'O;
	const octetstring c_msg_sts_q_fail := '0c0000140e022b0200000900090000f11001234567021200'O;
	const octetstring c_kill := '040000110e00000200000400080000f11001234567'O;
	const octetstring c_kill_fail := '060000120e00000200000900090000f1100123456702'O;
	const octetstring c_write_repl := '010000c70e022b0300300400080000f110012345671200050006000407000613020c400107f4f29c9e769f5de337b90c921d1b8d46a3d168341a8d46a3d168341a8d46a3d168341a8d46a3d168341a8d46a3d168341a8d46a3d168341a8d46a3d168341a8d46a3d168341a8d46a3d168341a8d46a3d100'O;
	const octetstring c_write_repl_compl := '020000130e022b0300300400080000f110012345671200'O;
	const octetstring c_write_repl_fail := '030000140e022b0300300900090000f110012345670d1200'O;
	const octetstring c_msg_s_q := '0a0000130e022b0200900400080000f110012345671200'O;
	const octetstring c_msg_s_q_compl := '0b0000160e022b02009008000b0000f110012345670008001200'O;
	const octetstring c_kill_compl := '050000160e022b02008008000b0000f110012345670006001200'O;

	log(dec_CBSP_PDU(c_load_q));
	log(dec_CBSP_PDU(c_load_q_compl));
	log(dec_CBSP_PDU(c_reset));
	log(dec_CBSP_PDU(c_reset_compl));
	log(dec_CBSP_PDU(c_msg_sts_q));
	log(dec_CBSP_PDU(c_msg_sts_q_fail));
	log(dec_CBSP_PDU(c_kill));
	log(dec_CBSP_PDU(c_kill_fail));
	log(dec_CBSP_PDU(c_write_repl));
	log(dec_CBSP_PDU(c_write_repl_compl));
	log(dec_CBSP_PDU(c_write_repl_fail));
	log(dec_CBSP_PDU(c_msg_s_q));
	log(dec_CBSP_PDU(c_msg_s_q_compl));
	log(dec_CBSP_PDU(c_kill_compl));
}

testcase TC_selftest_sabp() runs on test_CT {
	const octetstring c_write := '00000080930000080006000211120007000240c0000f0010000113f0030282ec0613f0030282ec070001400100000d0002012a000900020000000400010100000056029f01b4d90d064297d9ec37e8fe96b3c9a0303bdd68341a8d46a3d168341a8d46a3d168341a8d46a3d168341a8d46a3d168341a8d46a3d168341a8d46a3d168341a8d46a3d168341a8d46a3d168341a8d46a3d10012'O;

	log(dec_SABP_PDU(c_write));
	log(enc_SABP_PDU(dec_SABP_PDU(c_write)));

	var template (value) Service_Areas_List sa_list := {
		ts_SabpSai('62F224'O, '0023'O, '0042'O)
	};
	var template (value) SABP_PDU tx;

	tx := ts_SABP_Write(int2bit(1, 16), int2bit(1, 16), sa_list, 23, 42, '00000000'B, '01011010'B);
	log("Write: ", enc_SABP_PDU(valueof(tx)))

	var Service_Areas_List sa_list2 := { valueof(ts_SabpSai('62F224'O, '1000'O, '0042'O)) };
	for (var integer i := 0; i < 2500; i := i+1) {
		sa_list2 := sa_list2 & {valueof(ts_SabpSai('62F224'O, '2000'O, int2oct(i,2))) };
	}
	tx := ts_SABP_Write(int2bit(2, 16), int2bit(2, 16), sa_list2, 23, 42, '00000000'B, '01011010'B);
	log("Write: ", enc_SABP_PDU(valueof(tx)))

	tx := ts_SABP_Restart(sa_list);
	log("Restart: ", enc_SABP_PDU(valueof(tx)));
}

private function f_bsc_create_and_delete() runs on BSC_ConnHdlr {
	f_cbsp_handle_write(g_pars.exp_cbs_msg, 0, g_pars.cell_list_success);
	f_cbsp_handle_kill(0, g_pars.exp_cbs_msg.msg_id, g_pars.exp_cbs_msg.ser_nr,
			   exp_list:=g_pars.cell_list_success, tx_list:=g_pars.cell_list_success,
			   tx_fail_list:=omit, tx_compl_list:=omit,
			   channel_ind:=g_pars.exp_cbs_msg.channel_ind);
}

private function f_mme_create_and_delete() runs on MME_ConnHdlr {
	f_sbcap_handle_write_replace_warn_req(g_pars.exp_cbs_msg, 0);
	if (ispresent(g_pars.write_replace_warning_ind_cause) and
	    ispresent(g_pars.bcast_cell_id_list)) {
		f_sbcap_tx_write_replace_warn_ind(0, g_pars.exp_cbs_msg,
						  g_pars.write_replace_warning_ind_cause,
						  g_pars.bcast_cell_id_list)
	}
	f_sbcap_handle_stop_warn_req(0, g_pars.exp_cbs_msg);
}

function f_create_and_delete(CBS_Message msg)
runs on test_CT {
	var EcbeCbcMessage ecbe := f_cbs2ecbe(msg, "TTCN-3");
	f_ecbe_tx_post_cbs(ecbe);
	f_ecbe_rx_resp(201);

	f_sleep(2.0);

	f_ecbe_tx_delete_cbs(msg.msg_id);
	f_ecbe_rx_resp(200);
}

private template (value) CBS_Message t_CBSmsg(uint16_t msg_id, uint16_t ser_nr) := {
	msg_id := msg_id,
	ser_nr := ser_nr,
	old_ser_nr := omit,
	cell_list := cil_BSS,
	channel_ind := 0,
	category := CBSP_CATEG_NORMAL,
	rep_period := 5,
	num_bcast_req := 3,
	dcs := 1,
	content := {
		{ '00'O, 1 }
	}
};

/* specify a variety of different Cell Identifier formats to extend test coverage */
testcase TC_ecbe_create_delete_cgi() runs on test_CT {
	f_init(num_bsc := 1);
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list_success;
	var template (value) CBS_Message msg := t_CBSmsg(43, 16752);

	cell_list_success := ts_BSSMAP_CIL_CGI({
		ts_BSSMAP_CI_CGI('901'H, '70'H, 23, 42),
		ts_BSSMAP_CI_CGI('901'H, '70'H, 24, 42),
		ts_BSSMAP_CI_CGI('901'H, '70'H, 24, 43)
	});
	g_pars_BSC[0].start_fn := refers(f_bsc_create_and_delete);
	g_pars_BSC[0].exp_cbs_msg := valueof(msg);
	g_pars_BSC[0].cell_list_success := valueof(cell_list_success);
	f_start();
	f_create_and_delete(valueof(msg));
	f_shutdown_helper();
}
testcase TC_ecbe_create_delete_lac_ci() runs on test_CT {
	f_init(num_bsc := 1);
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list_success;
	var template (value) CBS_Message msg := t_CBSmsg(44, 16752);
	cell_list_success := ts_BSSMAP_CIL_LAC_CI({
		ts_BSSMAP_CI_LAC_CI(10001, 50001),
		ts_BSSMAP_CI_LAC_CI(10002, 50002),
		ts_BSSMAP_CI_LAC_CI(10003, 50003)
	});
	g_pars_BSC[0].start_fn := refers(f_bsc_create_and_delete);
	g_pars_BSC[0].exp_cbs_msg := valueof(msg);
	g_pars_BSC[0].cell_list_success := valueof(cell_list_success);
	f_start();
	f_create_and_delete(valueof(msg));
	f_shutdown_helper();
}
testcase TC_ecbe_create_delete_lac() runs on test_CT {
	f_init(num_bsc := 1);
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list_success;
	var template (value) CBS_Message msg := t_CBSmsg(45, 16752);
	cell_list_success := ts_BSSMAP_CIL_LAC({
		ts_BSSMAP_CI_LAC(10001),
		ts_BSSMAP_CI_LAC(10002),
		ts_BSSMAP_CI_LAC(10003)
	});
	g_pars_BSC[0].start_fn := refers(f_bsc_create_and_delete);
	g_pars_BSC[0].exp_cbs_msg := valueof(msg);
	g_pars_BSC[0].cell_list_success := valueof(cell_list_success);
	f_start();
	f_create_and_delete(valueof(msg));
	f_shutdown_helper();
}
testcase TC_ecbe_create_delete_ci() runs on test_CT {
	f_init(num_bsc := 1);
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list_success;
	var template (value) CBS_Message msg := t_CBSmsg(46, 16752);
	cell_list_success := ts_BSSMAP_CIL_CI({
		ts_BSSMAP_CI_CI(50001),
		ts_BSSMAP_CI_CI(50002),
		ts_BSSMAP_CI_CI(50003)
	});
	g_pars_BSC[0].start_fn := refers(f_bsc_create_and_delete);
	g_pars_BSC[0].exp_cbs_msg := valueof(msg);
	g_pars_BSC[0].cell_list_success := valueof(cell_list_success);
	f_start();
	f_create_and_delete(valueof(msg));
	f_shutdown_helper();
}
testcase TC_ecbe_create_delete_lai() runs on test_CT {
	f_init(num_bsc := 1);
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list_success;
	var template (value) CBS_Message msg := t_CBSmsg(47, 16752);
	cell_list_success := ts_BSSMAP_CIL_LAI({
		ts_BSSMAP_CI_LAI('901'H, '70'H, 25),
		ts_BSSMAP_CI_LAI('901'H, '70'H, 26),
		ts_BSSMAP_CI_LAI('901'H, '70'H, 27)
	});
	g_pars_BSC[0].start_fn := refers(f_bsc_create_and_delete);
	g_pars_BSC[0].exp_cbs_msg := valueof(msg);
	g_pars_BSC[0].cell_list_success := valueof(cell_list_success);
	f_start();
	f_create_and_delete(valueof(msg));
	f_shutdown_helper();
}

/* Create and delete message with MME available. MME reports
 * Write-Replace-Warning-Indication and Stop-Warning-Indication to CBC
 */
testcase TC_ecbe_create_delete_mme_indication() runs on test_CT {
	var template (value) CellId_Broadcast_List bcast_cell_id_li;
	var template (value) CBS_Message msg := t_CBSmsg(48, 16752);

	f_init(num_bsc := 0, num_mme := 1);

	bcast_cell_id_li := {
		ts_SBCAP_CellId_Broadcast_List_Item(ts_SBCAP_ECGI(f_enc_mcc_mnc('901'H, '70'H), 1234)),
		ts_SBCAP_CellId_Broadcast_List_Item(ts_SBCAP_ECGI(f_enc_mcc_mnc('901'H, '70'H), 5678))
	};
	g_pars_MME[0].start_fn := refers(f_mme_create_and_delete);
	g_pars_MME[0].exp_cbs_msg := valueof(msg);
	g_pars_MME[0].write_replace_warning_ind_cause := SBC_AP_Cause_message_accepted;
	g_pars_MME[0].bcast_cell_id_list := valueof(bcast_cell_id_li);
	f_start();
	f_create_and_delete(valueof(msg));
	f_shutdown_helper();
}

/* Create and delete message with MME available. MME reports
 * Write-Replace-Response with Unknown TAI IE.
 */
testcase TC_ecbe_create_delete_mme_unknown_tai() runs on test_CT {
	var template (value) List_of_TAIs write_repl_unknown_TAIs;
	var template (value) CBS_Message msg := t_CBSmsg(48, 16752);

	f_init(num_bsc := 0, num_mme := 1);

	write_repl_unknown_TAIs := {{ts_SBCAP_TAI(f_enc_mcc_mnc('901'H, '70'H), 1234)}};
	g_pars_MME[0].start_fn := refers(f_mme_create_and_delete);
	g_pars_MME[0].exp_cbs_msg := valueof(msg);
	g_pars_MME[0].write_replace_warning_ind_cause := SBC_AP_Cause_message_accepted;
	g_pars_MME[0].write_repl_unknown_TAIs := valueof(write_repl_unknown_TAIs);
	f_start();
	f_create_and_delete(valueof(msg));
	f_shutdown_helper();
}

/* Create and delete message with both BSC and MME available */
testcase TC_ecbe_create_delete_bsc_and_mme() runs on test_CT {
	f_init(num_bsc := 1, num_mme := 1);
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list_success;
	var template (value) CellId_Broadcast_List bcast_cell_id_li;
	var template (value) CBS_Message msg := t_CBSmsg(43, 16752);

	cell_list_success := ts_BSSMAP_CIL_CGI({
		ts_BSSMAP_CI_CGI('901'H, '70'H, 23, 42),
		ts_BSSMAP_CI_CGI('901'H, '70'H, 24, 42),
		ts_BSSMAP_CI_CGI('901'H, '70'H, 24, 43)
	});
	g_pars_BSC[0].start_fn := refers(f_bsc_create_and_delete);
	g_pars_BSC[0].exp_cbs_msg := valueof(msg);
	g_pars_BSC[0].cell_list_success := valueof(cell_list_success);

	bcast_cell_id_li := {
		ts_SBCAP_CellId_Broadcast_List_Item(ts_SBCAP_ECGI(f_enc_mcc_mnc('901'H, '70'H), 1234))
	};
	g_pars_MME[0].start_fn := refers(f_mme_create_and_delete);
	g_pars_MME[0].exp_cbs_msg := valueof(msg);
	g_pars_MME[0].write_replace_warning_ind_cause := SBC_AP_Cause_message_accepted;
	g_pars_MME[0].bcast_cell_id_list := valueof(bcast_cell_id_li);
	f_start();
	f_create_and_delete(valueof(msg));
	f_shutdown_helper();
}

/* Create and delete message with BSC acting as TCP server */
testcase TC_ecbe_create_delete_bsc_server() runs on test_CT {
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list_success;
	var template (value) CBS_Message msg := t_CBSmsg(46, 16752);

	/* The 2nd BSC is the one configured as server, but we only want to test
	 * that one, so initialize both but copy over config of the 2nd one to
	 * the first one, to start only one BSC: */
	f_init(num_bsc := 2);
	g_num_bsc := 1;
	g_pars_BSC[0] := g_pars_BSC[1];

	cell_list_success := ts_BSSMAP_CIL_CI({
		ts_BSSMAP_CI_CI(50001),
		ts_BSSMAP_CI_CI(50002),
		ts_BSSMAP_CI_CI(50003)
	});
	g_pars_BSC[0].start_fn := refers(f_bsc_create_and_delete);
	g_pars_BSC[0].exp_cbs_msg := valueof(msg);
	g_pars_BSC[0].cell_list_success := valueof(cell_list_success);
	f_start();
	f_create_and_delete(valueof(msg));
	f_shutdown_helper();
}

/* Create and delete message with MME acting as SCTP server */
testcase TC_ecbe_create_delete_mme_server() runs on test_CT {
	var template (value) CellId_Broadcast_List bcast_cell_id_li;
	var template (value) CBS_Message msg := t_CBSmsg(48, 16752);

	/* The 2nd MME is the one configured as server, but we only want to test
	 * that one, so initialize both but copy over config of the 2nd one to
	 * the first one, to start only one MME: */
	f_init(num_bsc := 0, num_mme := 2);
	g_num_mme := 1;
	g_pars_MME[0] := g_pars_MME[1];

	bcast_cell_id_li := {
		ts_SBCAP_CellId_Broadcast_List_Item(ts_SBCAP_ECGI(f_enc_mcc_mnc('901'H, '70'H), 1234)),
		ts_SBCAP_CellId_Broadcast_List_Item(ts_SBCAP_ECGI(f_enc_mcc_mnc('901'H, '70'H), 5678))
	};
	g_pars_MME[0].start_fn := refers(f_mme_create_and_delete);
	g_pars_MME[0].exp_cbs_msg := valueof(msg);
	g_pars_MME[0].write_replace_warning_ind_cause := SBC_AP_Cause_message_accepted;
	g_pars_MME[0].bcast_cell_id_list := valueof(bcast_cell_id_li);
	f_start();
	f_create_and_delete(valueof(msg));
	f_shutdown_helper();
}

/* Create 2 concurrent messages against an MME, then delete them */
private function f_mme_TC_concurrent_cbs_msg_mme() runs on MME_ConnHdlr {
	var CBS_Message msg[2];
	msg[0] := g_pars.exp_cbs_msg;
	msg[1] := msg[0];
	msg[1].msg_id := msg[0].msg_id + 1;

	for (var integer i := 0; i < lengthof(msg); i := i + 1) {
		f_sbcap_handle_write_replace_warn_req(msg[i], 0);
		if (ispresent(g_pars.write_replace_warning_ind_cause) and
		    ispresent(g_pars.bcast_cell_id_list)) {
			f_sbcap_tx_write_replace_warn_ind(0, msg[i],
							  g_pars.write_replace_warning_ind_cause,
							  g_pars.bcast_cell_id_list)
		}
	}
	/* Now handle Stop: */
	for (var integer i := 0; i < lengthof(msg); i := i + 1) {
		f_sbcap_handle_stop_warn_req(0, msg[i]);
	}
}
testcase TC_concurrent_cbs_msg_mme() runs on test_CT {
	var template (value) CellId_Broadcast_List bcast_cell_id_li;
	var CBS_Message msg[2];
	var EcbeCbcMessage ecbe;

	msg[0] := valueof(t_CBSmsg(49, 16752));
	msg[1] := msg[0];
	msg[1].msg_id := msg[0].msg_id + 1;

	f_init(num_bsc := 0, num_mme := 1);

	bcast_cell_id_li := {
		ts_SBCAP_CellId_Broadcast_List_Item(ts_SBCAP_ECGI(f_enc_mcc_mnc('901'H, '70'H), 1234))
	};
	g_pars_MME[0].start_fn := refers(f_mme_TC_concurrent_cbs_msg_mme);
	g_pars_MME[0].exp_cbs_msg := msg[0];
	g_pars_MME[0].write_replace_warning_ind_cause := SBC_AP_Cause_message_accepted;
	g_pars_MME[0].bcast_cell_id_list := valueof(bcast_cell_id_li);
	f_start();

	for (var integer i := 0; i < lengthof(msg); i := i + 1) {
		ecbe := f_cbs2ecbe(msg[i], "TTCN-3");
		f_ecbe_tx_post_cbs(ecbe);
		f_ecbe_rx_resp(201);
	}

	f_sleep(2.0);

	for (var integer i := 0; i < lengthof(msg); i := i + 1) {
		f_ecbe_tx_delete_cbs(msg[i].msg_id);
		f_ecbe_rx_resp(200);
	}

	f_shutdown_helper();
}

/* Test ETWS message over CBSP. TS 23.041 9.4.1.2.2 */
testcase TC_ecbe_create_delete_etws_bsc() runs on test_CT {
	f_init(num_bsc := 1);
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list_success;
	var template (value) CBS_Message msg := t_CBSmsg(4352 /* Earthquake */, 16752);
	msg.channel_ind := omit;

	cell_list_success := ts_BSSMAP_CIL_CGI({
		ts_BSSMAP_CI_CGI('901'H, '70'H, 23, 42),
		ts_BSSMAP_CI_CGI('901'H, '70'H, 24, 42)
	});
	g_pars_BSC[0].start_fn := refers(f_bsc_create_and_delete);
	g_pars_BSC[0].exp_cbs_msg := valueof(msg);
	g_pars_BSC[0].cell_list_success := valueof(cell_list_success);
	f_start();
	f_create_and_delete(valueof(msg));
	f_shutdown_helper();
}

/* Test ETWS message over SBc-AP. TS 23.041 9.4.1.2.2 */
testcase TC_ecbe_create_delete_etws_mme() runs on test_CT {
	f_init(num_mme := 1);
	var template (value) CBS_Message msg := t_CBSmsg(4352 /* Earthquake */, 16753);
	msg.channel_ind := omit;

	g_pars_MME[0].start_fn := refers(f_mme_create_and_delete);
	g_pars_MME[0].exp_cbs_msg := valueof(msg);
	f_start();
	f_create_and_delete(valueof(msg));
	f_shutdown_helper();
}

/* Test BSC answering WRITE-REPLACE REQUEST with WRITE-REPLACE FAILURE */
private function f_bsc_TC_create_nack_bsc() runs on BSC_ConnHdlr {
	f_cbsp_handle_write(g_pars.exp_cbs_msg, 0, g_pars.cell_list_success, g_pars.tx_fail_list);
}
testcase TC_create_nack_bsc() runs on test_CT {
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list_success;
	var template (value) CBSP_FailureListItems tx_fail_list;
	var CBS_Message msg;
	var EcbeCbcMessage ecbe;

	msg := valueof(t_CBSmsg(49, 16752));

	f_init(num_bsc := 1, num_mme := 0);

	cell_list_success := ts_BSSMAP_CIL_CGI({
		ts_BSSMAP_CI_CGI('901'H, '70'H, 23, 42),
		ts_BSSMAP_CI_CGI('901'H, '70'H, 24, 42)
	});
	tx_fail_list := {
		CBSP_FailureListItem_CGI(ts_BSSMAP_CI_CGI('901'H, '70'H, 24, 48), CBSP_CAUSE_CELL_ID_NOT_VALID),
		CBSP_FailureListItem_LAC_CI(ts_BSSMAP_CI_LAC_CI(10001, 50001), CBSP_CAUSE_LAI_OR_LAC_NPT_VALID)
	};
	g_pars_BSC[0].start_fn := refers(f_bsc_TC_create_nack_bsc);
	g_pars_BSC[0].exp_cbs_msg := msg;
	g_pars_BSC[0].cell_list_success := valueof(cell_list_success);
	g_pars_BSC[0].tx_fail_list := valueof(tx_fail_list);
	f_start();

	ecbe := f_cbs2ecbe(msg, "TTCN-3");
	f_ecbe_tx_post_cbs(ecbe);
	f_ecbe_rx_resp(201);

	f_shutdown_helper();
}

/* Test MME answering Write-Replace Warning Request with Write-Replace Warning Response cause != accepted */
private function f_mme_TC_create_nack_mme() runs on MME_ConnHdlr {
	f_sbcap_handle_write_replace_warn_req(g_pars.exp_cbs_msg, 0);
}
testcase TC_create_nack_mme() runs on test_CT {
	var template (value) CBS_Message msg := t_CBSmsg(48, 16752);

	f_init(num_bsc := 0, num_mme := 1);

	g_pars_MME[0].start_fn := refers(f_mme_TC_create_nack_mme);
	g_pars_MME[0].exp_cbs_msg := valueof(msg);
	g_pars_MME[0].write_replace_warning_req_cause := SBcAP_Cause_unspecifed_error;
	f_start();
	f_create_and_delete(valueof(msg));
	f_shutdown_helper();
}


/* Test cell in BSC going unavailable for broadcasting and going available again */
private function f_bsc_TC_cell_failure_restart_idle_bsc() runs on BSC_ConnHdlr {
	var template (value) CBSP_FailureListItems fail_list := {
		CBSP_FailureListItem_CGI(ts_BSSMAP_CI_CGI('901'H, '70'H, 23, 42), CBSP_CAUSE_CB_NOT_OPERATIONAL),
		CBSP_FailureListItem_CGI(ts_BSSMAP_CI_CGI('901'H, '70'H, 24, 42), CBSP_CAUSE_CB_NOT_OPERATIONAL)
	};
	f_cbsp_send(ts_CBSP_FAILURE(fail_list, CBSP_BC_MSGT_CBS));
	f_cbsp_send(ts_CBSP_FAILURE(fail_list, CBSP_BC_MSGT_EMERG));

	f_sleep(1.0);

	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	cell_list := ts_BSSMAP_CIL_CGI({
		ts_BSSMAP_CI_CGI('901'H, '70'H, 23, 42),
		ts_BSSMAP_CI_CGI('901'H, '70'H, 24, 42)
	});
	f_cbsp_send(ts_CBSP_RESTART(cell_list, CBSP_BC_MSGT_CBS, CBSP_RI_DATA_LOST));
	f_cbsp_send(ts_CBSP_RESTART(cell_list, CBSP_BC_MSGT_EMERG, CBSP_RI_DATA_LOST));
}
testcase TC_cell_failure_restart_idle_bsc() runs on test_CT {
	f_init(num_bsc := 1, num_mme := 0);
	g_pars_BSC[0].start_fn := refers(f_bsc_TC_cell_failure_restart_idle_bsc);
	f_start();
	f_shutdown_helper();
}

/* Test cell in MME going unavailable for broadcasting and going available again */
private function f_mme_TC_cell_failure_restart_idle_mme() runs on MME_ConnHdlr {
	var template (value) Global_ENB_ID enb_id := ts_Global_ENB_ID_MACRO(f_enc_mcc_mnc('901'H, '70'H), 90);
	var template (value) Failed_Cell_List fail_list := {
		ts_SBCAP_ECGI(f_enc_mcc_mnc('901'H, '70'H), 1234),
		ts_SBCAP_ECGI(f_enc_mcc_mnc('901'H, '70'H), 5678)
	};
	f_SBC_AP_send(ts_SBCAP_PWS_FAILURE(fail_list, enb_id));

	f_sleep(1.0);

	var template (value) Restarted_Cell_List cell_list;
	cell_list := {
		ts_SBCAP_ECGI(f_enc_mcc_mnc('901'H, '70'H), 1234),
		ts_SBCAP_ECGI(f_enc_mcc_mnc('901'H, '70'H), 5678)
	};
	f_SBC_AP_send(ts_SBCAP_PWS_RESTART(cell_list, enb_id));
}
testcase TC_cell_failure_restart_idle_mme() runs on test_CT {
	f_init(num_bsc := 0, num_mme := 1);
	g_pars_MME[0].start_fn := refers(f_mme_TC_cell_failure_restart_idle_mme);
	f_start();
	f_shutdown_helper();
}

/* Test cell actively broadcasting a message in BSC going unavailable for
   broadcasting and going available again. The CBC should reload the announced
   cell with the active messages. See 3GPP TS 48.049 7.8 */
private function f_bsc_TC_cell_failure_restart_active_bsc() runs on BSC_ConnHdlr {
	var template (value) CBSP_FailureListItems fail_list := {
		CBSP_FailureListItem_CGI(g_pars.cell_list_success.cIl_CGI[0], CBSP_CAUSE_CB_NOT_OPERATIONAL)
	};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list := ts_BSSMAP_CIL_CGI({
		g_pars.cell_list_success.cIl_CGI[0]
	});

	/* Guide cell into active broadcast msg state: */
	f_cbsp_handle_write(g_pars.exp_cbs_msg, 0, g_pars.cell_list_success);

	/* BSC reports the cell is down */
	f_cbsp_send(ts_CBSP_FAILURE(fail_list, CBSP_BC_MSGT_CBS));
	f_cbsp_send(ts_CBSP_FAILURE(fail_list, CBSP_BC_MSGT_EMERG));
	f_sleep(1.0);

	/* BSC reports the cell is up again */
	f_cbsp_send(ts_CBSP_RESTART(cell_list, CBSP_BC_MSGT_CBS, CBSP_RI_DATA_LOST));
	f_cbsp_send(ts_CBSP_RESTART(cell_list, CBSP_BC_MSGT_EMERG, CBSP_RI_DATA_LOST));

/* TODO: OS#5641
 * The BSC informs the CBC by sending the RESTART message (see figure 7.8.2.1)
 * containing the Cell List IE identifying the cell(s) being in CBS message
 * operational state or in emergency message operational state and the Recovery
 * Indication IE, indicating whether the broadcast information data is lost or
 * not in the BSC.
 * The RESTART message is sent once per broadcast message type
 * as indicated by the Broadcast Message Type IE.
 */
	f_bsc_create_and_delete();
}
testcase TC_cell_failure_restart_active_bsc() runs on test_CT {
	f_init(num_bsc := 1);
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list_success;
	var template (value) CBS_Message msg := t_CBSmsg(43, 16752);

	cell_list_success := ts_BSSMAP_CIL_CGI({
		ts_BSSMAP_CI_CGI('901'H, '70'H, 23, 42)
	});
	g_pars_BSC[0].start_fn := refers(f_bsc_TC_cell_failure_restart_active_bsc);
	g_pars_BSC[0].exp_cbs_msg := valueof(msg);
	g_pars_BSC[0].cell_list_success := valueof(cell_list_success);
	f_start();

	var EcbeCbcMessage ecbe := f_cbs2ecbe(valueof(msg), "TTCN-3");
	f_ecbe_tx_post_cbs(ecbe);
	f_ecbe_rx_resp(201);

	f_shutdown_helper();
}

/* Test cell actively broadcasting a message in MME going unavailable for
   broadcasting and going available again. The CBC should reload the announced
   cell with the active messages. See 3GPP TS 29.168 4.3.3E.2 */
private function f_mme_TC_cell_failure_restart_active_mme() runs on MME_ConnHdlr {
	var template (value) Global_ENB_ID enb_id := ts_Global_ENB_ID_MACRO(f_enc_mcc_mnc('901'H, '70'H), 90);
	var template (value) Failed_Cell_List fail_list := {
		g_pars.bcast_cell_id_list[0].eCGI
	}
	var template (value) Restarted_Cell_List cell_list := {
		g_pars.bcast_cell_id_list[0].eCGI
	};
	/* Guide cell into active broadcast msg state: */
	f_sbcap_handle_write_replace_warn_req(g_pars.exp_cbs_msg, 0);
	if (ispresent(g_pars.write_replace_warning_ind_cause) and
	    ispresent(g_pars.bcast_cell_id_list)) {
		f_sbcap_tx_write_replace_warn_ind(0, g_pars.exp_cbs_msg,
						  g_pars.write_replace_warning_ind_cause,
						  g_pars.bcast_cell_id_list)
	}

	/* MME reports the cell is down */
	f_SBC_AP_send(ts_SBCAP_PWS_FAILURE(fail_list, enb_id));

	f_sleep(1.0);

	/* MME reports the cell is up again */
	f_SBC_AP_send(ts_SBCAP_PWS_RESTART(cell_list, enb_id));

/* TODO: OS#5641
 * The CBC shall reload the warning message data (with the same Message
 * Identifier and Serial Number) to the (H)eNB by initiating Write Replace
 * Warning procedure(s) as specified in clause 4.3.3.2 with the following
 * additions:
 * - the CBC should set the Warning Area List IE in the Write-Replace
 * Warning Request message to the identities of the cell(s) received in the
 * Restarted-Cell-List which are relevant to the warning message data being
 * reloaded;
 * - the CBC shall copy the Global eNB ID into the Write-Replace
 * Warning Request message; and
 * - the CBC may update the Number of Broadcast Requested, if necessary.
 */
	f_mme_create_and_delete();
}
testcase TC_cell_failure_restart_active_mme() runs on test_CT {
	var template (value) CellId_Broadcast_List bcast_cell_id_li;
	var template (value) CBS_Message msg := t_CBSmsg(48, 16752);

	f_init(num_bsc := 0, num_mme := 1);

	bcast_cell_id_li := {
		ts_SBCAP_CellId_Broadcast_List_Item(ts_SBCAP_ECGI(f_enc_mcc_mnc('901'H, '70'H), 1234))
	};
	g_pars_MME[0].start_fn := refers(f_mme_TC_cell_failure_restart_active_mme);
	g_pars_MME[0].exp_cbs_msg := valueof(msg);
	g_pars_MME[0].write_replace_warning_ind_cause := SBC_AP_Cause_message_accepted;
	g_pars_MME[0].bcast_cell_id_list := valueof(bcast_cell_id_li);
	f_start();

	var EcbeCbcMessage ecbe := f_cbs2ecbe(valueof(msg), "TTCN-3");
	f_ecbe_tx_post_cbs(ecbe);
	f_ecbe_rx_resp(201);

	f_shutdown_helper();
}

control {
	execute( TC_rx_keepalive() );
	execute( TC_rx_keepalive_timeout() );
	execute( TC_ecbe_create_delete_cgi() );
	execute( TC_ecbe_create_delete_lac_ci() );
	execute( TC_ecbe_create_delete_lac() );
	execute( TC_ecbe_create_delete_ci() );
	execute( TC_ecbe_create_delete_lai() );
	execute( TC_ecbe_create_delete_mme_indication() );
	execute( TC_ecbe_create_delete_mme_unknown_tai() );
	execute( TC_ecbe_create_delete_bsc_and_mme() );
	execute( TC_ecbe_create_delete_bsc_server() );
	execute( TC_ecbe_create_delete_mme_server() );
	execute( TC_concurrent_cbs_msg_mme() );

	execute( TC_ecbe_create_delete_etws_bsc() );
	execute( TC_ecbe_create_delete_etws_mme() );

	execute( TC_create_nack_bsc() );
	execute( TC_create_nack_mme() );

	execute( TC_cell_failure_restart_idle_bsc() );
	execute( TC_cell_failure_restart_idle_mme() );

	execute( TC_cell_failure_restart_active_bsc() );
	execute( TC_cell_failure_restart_active_mme() );
}

}
