######################################################################
# Terminating, Initial requests
######################################################################
route[MT] {
	t_on_reply("MT_reply");
}

######################################################################
# Replies to Originating Initial Requests
######################################################################
onreply_route[MT_reply] {
	if (!strempty($(ct{tobody.params}))) {
		append_hf("C-Params: $(ct{tobody.params})\r\n");
	}
	# In case of 1xx and 2xx do NAT
	if(status=~"[12][0-9][0-9]")
		route(NATMANAGE);
#!ifdef WITH_RX
        if (t_check_status("183")){
		xlog("L_DBG", "IMS: Received 183 inside term_initial_reply\n");	

		xlog("L_DBG", "About to test if this is a retransmitted reply which is still currently suspended\n");
		if (t_is_retr_async_reply()) {
	                xlog("L_DBG", "Dropping retransmitted reply which is still currently suspended\n");
       		         drop();
       		}

		xlog("L_DBG","Diameter: Term authorizing media via Rx\n");	
		$avp(FTAG_CUSTOM_AVP)=$ft;
		$avp(TTAG_CUSTOM_AVP)=$tt;
		$avp(CALLID_CUSTOM_AVP)=$ci;

		$var(aarret) = Rx_AAR("MT_aar_reply","term","",-1);
		xlog("L_DBG", "AAR return code is $var(aarret)\n");

		 switch ($var(aarret)) {
                        case 1:
                                #suspend was successful and we must break
                                xlog("L_DBG", "Success sending AAR for media\n");
                                exit;

                        case -1:
                                #this is retransmitted response so we just drop it
                                xlog("L_DBG", "AAR still processing dropping retransmitted response\n");
                                drop();
                                exit;

                        default:
                                xlog("L_ERR", "Unable to send AAR for media\n");
                                #comment this if you want to allow even if Rx fails
                                dlg_terminate("all", "Sorry no QoS available");
                                exit;

                }
	}
}

route[MT_aar_reply]
{
        xlog("L_DBG", "IMS: TERM_SESSION_AAR_REPLY\n");

        #this is async so to know status we have to check the reply avp        
	switch ($avp(s:aar_return_code)) {
                case 1:
                        xlog("L_DBG", "Diameter: Orig AAR success on media authorization\n"); 
                        break;
                default:
                        xlog("L_ERR", "IMS: AAR failed Orig\n");
                        xlog("L_ERR", "IMS: ttag: "+ "$avp(TTAG_CUSTOM_AVP)");
                        xlog("L_ERR", "IMS: ftag: "+ "$avp(FTAG_CUSTOM_AVP)");
                        xlog("L_ERR", "IMS: callid: "+ "$avp(CALLID_CUSTOM_AVP)");
                        #comment this if you want to allow even if Rx fails
                        if(dlg_get("$avp(CALLID_CUSTOM_AVP)","$avp(FTAG_CUSTOM_AVP)","$avp(TTAG_CUSTOM_AVP)")){
                                dlg_terminate("all", "Sorry no QoS available");
                                exit;
                        }
        }
#!endif
}


######################################################################
# In-Dialog-MT-Requests
######################################################################
route[MT_indialog] {
	t_on_reply("MT_indialog_reply");
}

onreply_route[MT_indialog_reply] {
	# In case of 1xx and 2xx do NAT
	if(status=~"[12][0-9][0-9]")
		route(NATMANAGE);

#!ifdef WITH_RX
	if(t_check_status("200") && is_method("INVITE")) {
		if (t_is_retr_async_reply()) {
			xlog("L_DBG", "Dropping retransmitted reply which is still currently suspended\n");
			drop();
		}

		xlog("L_DBG", "IMS: TERM_SUBSEQUENT reply. This is a 200 OK to a re-INVITE\n");
		xlog("L_DBG","Diameter: Term authorizing media via Rx\n");      
		$avp(FTAG_CUSTOM_AVP)=$ft;
		$avp(TTAG_CUSTOM_AVP)=$tt;
		$avp(CALLID_CUSTOM_AVP)=$ci;

		$var(aarret) = Rx_AAR("MT_indialog_aar_reply","term","",-1);
		xlog("L_DBG", "AAR return code is $var(aarret)\n");

		switch ($var(aarret)) {
                        case 1:
                                #suspend was successful and we must break
                                xlog("L_DBG", "Success sending AAR for media\n");
                                exit;

                        case -1:
                                #this is retransmitted response so we just drop it
                                xlog("L_DBG", "AAR still processing dropping retransmitted response\n");
                                drop();
                                exit;

                        default:
                                xlog("L_ERR", "Unable to send AAR for media\n");
                                #comment this if you want to allow even if Rx fails
                                dlg_terminate("all", "Sorry no QoS available");
                                exit;

                }
	}
}

route[MT_indialog_aar_reply]
{
        #this is async so to know status we have to check the reply avp
	switch ($avp(s:aar_return_code)) {
                case 1:
                        xlog("L_DBG", "Diameter: Orig AAR success on media authorization\n"); 
                        break;
                default:
                        xlog("L_ERR", "IMS: AAR failed Orig\n");
                        xlog("L_ERR", "IMS: ttag: "+ "$avp(TTAG_CUSTOM_AVP)");
                        xlog("L_ERR", "IMS: ftag: "+ "$avp(FTAG_CUSTOM_AVP)");
                        xlog("L_ERR", "IMS: callid: "+ "$avp(CALLID_CUSTOM_AVP)");
                        #comment this if you want to allow even if Rx fails
                        if(dlg_get("$avp(CALLID_CUSTOM_AVP)","$avp(FTAG_CUSTOM_AVP)","$avp(TTAG_CUSTOM_AVP)")){
                                dlg_terminate("all", "Sorry no QoS available");
                                exit;
                        }
        }
#!endif
}
