module MSC_Simulation {

import from IPL4asp_Types all;

import from IPA_Emulation all;

import from SCCP_Types all;
import from SCCPasp_Types all;
import from SCCP_Emulation all;

/*
import from MobileL3_Types all;
import from MobileL3_CommonIE_Types all;
import from L3_Templates all;

import from BSSAP_Types all;
import from BSSMAP_Templates all;
*/
import from RAN_Emulation all;

import from MGCP_Adapter all;

import from MSC_ConnectionHandler all;

type component MSC_CT {
	/* component references */
	var IPA_Emulation_CT vc_IPA;
	var SCCP_CT vc_SCCP;
	var RAN_Emulation_CT vc_BSSMAP;
	var MGCP_Adapter_CT vc_MGCP_UDP;
	/* test port to SCCP emulation */
	port SCCPasp_PT SCCP;
}

modulepar {
	boolean mp_mgcp_uses_udp := false;
}

function main(charstring local_ip, PortNumber local_port,
	      MSC_SCCP_MTP3_parameters sccp_pars,
	      SCCP_PAR_Address sccp_addr_own,
	      BssmapCreateCallback cb, charstring id) runs on MSC_CT
{
	/* create components */
	vc_IPA := IPA_Emulation_CT.create(id & "-IPA");
	vc_SCCP := SCCP_CT.create(id & "-SCCP");
	vc_BSSMAP := RAN_Emulation_CT.create(id & "-BSSMAP");

	map(vc_IPA:IPA_PORT, system:IPA_CODEC_PT);

	/* connect MTP3 service provider (IPA) to lower side of SCCP */
	connect(vc_IPA:MTP3_SP_PORT, vc_SCCP:MTP3_SCCP_PORT);

	/* connect BSSNAP dispatcher to upper side of SCCP */
	connect(vc_BSSMAP:BSSAP, vc_SCCP:SCCP_SP_PORT);

	if (mp_mgcp_uses_udp == false) {
		/* connect BSSMAP dispatcher to IPA_Emulation MGCP */
		connect(vc_BSSMAP:MGCP, vc_IPA:IPA_MGCP_PORT);
	} else {
		vc_MGCP_UDP := MGCP_Adapter_CT.create(id & "-MGCP_UDP");
		connect(vc_BSSMAP:MGCP, vc_MGCP_UDP:MGCP);
		vc_MGCP_UDP.start(MGCP_Adapter.main());
	}

	vc_IPA.start(IPA_Emulation.main_server(local_ip, local_port));
	vc_SCCP.start(SCCPStart(sccp_pars));

	var RanOps ranops := MSC_ConnectionHandler.MSC_RanOps;
	ranops.create_cb := cb;
	vc_BSSMAP.start(RAN_Emulation.main(ranops, id & "-BSSMAP"));

	/* wait until termination of respective components */
	vc_IPA.done;
	vc_BSSMAP.done;
	vc_SCCP.done;
	if (mp_mgcp_uses_udp) {
		vc_MGCP_UDP.done;
	}
}

}
