#!KAMAILIO
#
# Loadbalancer Config-File
#
# Kamailio (OpenSER) SIP Server v3.1 - default configuration script
#     - web: http://www.kamailio.org
#     - git: http://sip-router.org
#
# Refer to the Core CookBook at http://www.kamailio.org/dokuwiki/doku.php
# for an explanation of possible statements, functions and parameters.
#

# IP-Adress for incoming SIP-Traffic, in the following format:
listen=udp:127.0.0.1:5099
listen=tcp:127.0.0.1:5099

####### Defined Values #########

# *** Value defines - IDs used later in config

# - flags
#	FLT_ - per transaction (message) flags
#!define FLT_CAPTURE 1
#!define FLT_IPV6 2
#!define FLT_ACC 3
#!define FLT_DIALOG 4
#!define FLT_ANONYMIZE 5

#!define DISPATCHER_LIST_QUERYSERVER "1"

####### Global Parameters #########


debug=2
log_stderror=yes

memdbg=5
memlog=5

log_facility=LOG_LOCAL0

fork=yes
children=2
tcp_children=3

# Do SRV-Loadbalancing:
dns_srv_lb=yes
# Always prefer IPv6:
dns_try_ipv6=yes
# DNS-Based failover
use_dns_failover = on
# Query NAPTR-Records as well:
dns_try_naptr=no

/* uncomment the next line to disable the auto discovery of local aliases
   based on reverse DNS on IPs (default on) */
auto_aliases=no

system.shutdownmode = 0 desc "System shutdown mode"

####### Modules Section ########

# set paths to location of modules
mpath="/usr/lib64/kamailio/modules_k/:/usr/lib64/kamailio/modules/:/usr/local/lib/kamailio/modules/"

loadmodule "xlog"
loadmodule "kex"
loadmodule "pv"
loadmodule "ctl"
loadmodule "cdp"
loadmodule "cdp_avp"
loadmodule "ims_diameter_server"
loadmodule "corex"
loadmodule "http_client"
loadmodule "statistics"

# ----- cdp params -----
modparam("cdp","config_file","/etc/kamailio_hss/hss.xml")

# ----- ctl params -----
modparam("ctl", "binrpc", "unix:/var/run/kamailio_hss/kamailio_ctl")

# ----- http_client params -----
modparam("http_client", "httpcon", "hss=>https://hss:password@rest.ng-voice.com/hss")

# -- statistics params --
modparam("statistics", "variable", "diameter_requests")
modparam("statistics", "variable", "diameter_failed_requests")
modparam("statistics", "variable", "diameter_request_time")

####### Routing Logic ########

# Main SIP request routing logic
# - processing of any incoming SIP request starts with this route
route {
	drop();
	exit;
}

event_route[diameter:request] {
	#xlog("Application: $diameter_application, Command $diameter_command\n");
	#file_write("/tmp/diameter.json", $diameter_request);
	# Provide some statistics
	$var(start_time) = $TV(Sn);
	$var(res) = http_connect("hss", "/application/$diameter_application/command/$diameter_command", "application/json", "$diameter_request", "$var(response)");
	#xlog("$diameter_request\n");
	#xlog("API-server HTTP connection: $var(response) Result code $var(res)\n");
	if ($var(res) == 200) {
		$diameter_response = $var(response);
	} else {
		update_stat("diameter_failed_requests", "+1");
	}
	$var(start_secs) = $(var(start_time){s.select,0,.});
	$var(start_usecs) = $(var(start_time){s.select,1,.});
	$var(diff_secs) = $TV(s) - $var(start_secs);
	$var(diff_usecs) = $TV(u) - $var(start_usecs);
	$var(diff_ms) = $var(diff_secs)*1000 + ($var(diff_usecs)/1000);
	$var(stat_add) = "+" + $var(diff_ms);
	#xlog("Reqest took $var(stat_add)ms\n"); 
	update_stat("diameter_requests", "+1");
	update_stat("diameter_request_time", "$var(stat_add)");
}
