From 1176e7966f4dd6b3bb3eb0b3e9575d28f0f69a68 Mon Sep 17 00:00:00 2001 From: Dmitry Sinina Date: Fri, 20 Dec 2024 21:15:59 +0200 Subject: [PATCH] fix writecdr sp types (#1663) * fix writecdr sp types --- app/models/cdr/auth_log.rb | 6 +- .../api/rest/admin/auth_log_resource.rb | 6 +- .../20241219142219_fix_cdr_sp_types.rb | 1847 +++++++++++++++++ ...0241219145036_fix_writeauthlog_sp_types.rb | 144 ++ db/cdr_structure.sql | 139 +- .../rest/admin/auth_logs_controller_spec.rb | 6 +- spec/factories/cdr/auth_logs.rb | 6 +- spec/models/cdr/auth_log_spec.rb | 6 +- spec/sql/switch/auth_log_lega_headers_spec.rb | 128 ++ spec/sql/switch/auth_log_spec.rb | 121 ++ 10 files changed, 2357 insertions(+), 52 deletions(-) create mode 100644 db/cdr_migrate/20241219142219_fix_cdr_sp_types.rb create mode 100644 db/cdr_migrate/20241219145036_fix_writeauthlog_sp_types.rb create mode 100644 spec/sql/switch/auth_log_lega_headers_spec.rb create mode 100644 spec/sql/switch/auth_log_spec.rb diff --git a/app/models/cdr/auth_log.rb b/app/models/cdr/auth_log.rb index e3fcf57d0..9cb2046b2 100644 --- a/app/models/cdr/auth_log.rb +++ b/app/models/cdr/auth_log.rb @@ -10,7 +10,7 @@ # from_uri :string # internal_reason :string # nonce :string -# origination_ip :string +# origination_ip :inet # origination_port :integer(4) # pai :string # ppi :string @@ -25,9 +25,9 @@ # ruri :string # success :boolean # to_uri :string -# transport_local_ip :string +# transport_local_ip :inet # transport_local_port :integer(4) -# transport_remote_ip :string +# transport_remote_ip :inet # transport_remote_port :integer(4) # username :string # x_yeti_auth :string diff --git a/app/resources/api/rest/admin/auth_log_resource.rb b/app/resources/api/rest/admin/auth_log_resource.rb index faa90b5b1..099d99d91 100644 --- a/app/resources/api/rest/admin/auth_log_resource.rb +++ b/app/resources/api/rest/admin/auth_log_resource.rb @@ -44,13 +44,13 @@ def self.default_sort ransack_filter :code, type: :number ransack_filter :reason, type: :string ransack_filter :internal_reason, type: :string - ransack_filter :origination_ip, type: :string + ransack_filter :origination_ip, type: :inet ransack_filter :origination_port, type: :number ransack_filter :origination_proto_id, type: :number ransack_filter :transport_proto_id, type: :number - ransack_filter :transport_remote_ip, type: :string + ransack_filter :transport_remote_ip, type: :inet ransack_filter :transport_remote_port, type: :number - ransack_filter :transport_local_ip, type: :string + ransack_filter :transport_local_ip, type: :inet ransack_filter :transport_local_port, type: :number ransack_filter :pop_id, type: :number ransack_filter :node_id, type: :number diff --git a/db/cdr_migrate/20241219142219_fix_cdr_sp_types.rb b/db/cdr_migrate/20241219142219_fix_cdr_sp_types.rb new file mode 100644 index 000000000..0fb134c52 --- /dev/null +++ b/db/cdr_migrate/20241219142219_fix_cdr_sp_types.rb @@ -0,0 +1,1847 @@ +class FixCdrSpTypes < ActiveRecord::Migration[7.0] + + def up + execute %q{ + +DROP FUNCTION switch.writecdr(i_is_master boolean, i_node_id integer, i_pop_id integer, i_routing_attempt integer, i_is_last_cdr boolean, i_lega_transport_protocol_id smallint, i_lega_local_ip character varying, i_lega_local_port integer, i_lega_remote_ip character varying, i_lega_remote_port integer, i_legb_transport_protocol_id smallint, i_legb_local_ip character varying, i_legb_local_port integer, i_legb_remote_ip character varying, i_legb_remote_port integer, i_legb_ruri character varying, i_legb_outbound_proxy character varying, i_time_data json, i_early_media_present boolean, i_legb_disconnect_code integer, i_legb_disconnect_reason character varying, i_disconnect_initiator integer, i_internal_disconnect_code integer, i_internal_disconnect_reason character varying, i_lega_disconnect_code integer, i_lega_disconnect_reason character varying, i_orig_call_id character varying, i_term_call_id character varying, i_local_tag character varying, i_legb_local_tag character varying, i_msg_logger_path character varying, i_dump_level_id smallint, i_audio_recorded boolean, i_rtp_stats_data json, i_rtp_statistics json, i_global_tag character varying, i_resources character varying, i_active_resources json, i_failed_resource_type_id smallint, i_failed_resource_id bigint, i_dtmf_events json, i_versions json, i_is_redirected boolean, i_dynamic json, i_lega_headers json, i_legb_headers json, i_lega_identity json); +DROP FUNCTION switch.writecdr(i_is_master boolean, i_node_id integer, i_pop_id integer, i_routing_attempt integer, i_is_last_cdr boolean, i_lega_transport_protocol_id smallint, i_lega_local_ip character varying, i_lega_local_port integer, i_lega_remote_ip character varying, i_lega_remote_port integer, i_legb_transport_protocol_id smallint, i_legb_local_ip character varying, i_legb_local_port integer, i_legb_remote_ip character varying, i_legb_remote_port integer, i_legb_ruri character varying, i_legb_outbound_proxy character varying, i_time_data json, i_early_media_present boolean, i_legb_disconnect_code integer, i_legb_disconnect_reason character varying, i_disconnect_initiator integer, i_internal_disconnect_code integer, i_internal_disconnect_reason character varying, i_lega_disconnect_code integer, i_lega_disconnect_reason character varying, i_internal_disconnect_code_id smallint, i_orig_call_id character varying, i_term_call_id character varying, i_local_tag character varying, i_legb_local_tag character varying, i_msg_logger_path character varying, i_dump_level_id smallint, i_audio_recorded boolean, i_rtp_stats_data json, i_rtp_statistics json, i_global_tag character varying, i_resources character varying, i_active_resources json, i_failed_resource_type_id smallint, i_failed_resource_id bigint, i_dtmf_events json, i_versions json, i_is_redirected boolean, i_dynamic json, i_lega_headers json, i_legb_headers json, i_lega_identity json); +DROP FUNCTION switch.writecdr(i_is_master boolean, i_node_id integer, i_pop_id integer, i_routing_attempt integer, i_is_last_cdr boolean, i_lega_transport_protocol_id smallint, i_lega_local_ip character varying, i_lega_local_port integer, i_lega_remote_ip character varying, i_lega_remote_port integer, i_legb_transport_protocol_id smallint, i_legb_local_ip character varying, i_legb_local_port integer, i_legb_remote_ip character varying, i_legb_remote_port integer, i_legb_ruri character varying, i_legb_outbound_proxy character varying, i_time_data json, i_early_media_present boolean, i_legb_disconnect_code integer, i_legb_disconnect_reason character varying, i_disconnect_initiator integer, i_internal_disconnect_code integer, i_internal_disconnect_reason character varying, i_lega_disconnect_code integer, i_lega_disconnect_reason character varying, i_internal_disconnect_code_id smallint, i_orig_call_id character varying, i_term_call_id character varying, i_local_tag character varying, i_legb_local_tag character varying, i_msg_logger_path character varying, i_dump_level_id smallint, i_audio_recorded boolean, i_rtp_stats_data json, i_rtp_statistics json, i_global_tag character varying, i_resources character varying, i_active_resources json, i_failed_resource_type_id smallint, i_failed_resource_id bigint, i_dtmf_events json, i_versions json, i_is_redirected boolean, i_dynamic json, i_lega_request_headers json, i_legb_request_headers json, i_legb_reply_headers json, i_lega_identity json); + +CREATE OR REPLACE FUNCTION switch.writecdr( +i_is_master boolean, +i_node_id integer, +i_pop_id integer, +i_routing_attempt integer, +i_is_last_cdr boolean, +i_lega_transport_protocol_id smallint, +i_lega_local_ip inet, +i_lega_local_port integer, +i_lega_remote_ip inet, +i_lega_remote_port integer, +i_legb_transport_protocol_id smallint, +i_legb_local_ip inet, +i_legb_local_port integer, +i_legb_remote_ip inet, +i_legb_remote_port integer, +i_legb_ruri character varying, +i_legb_outbound_proxy character varying, +i_time_data json, +i_early_media_present boolean, +i_legb_disconnect_code integer, +i_legb_disconnect_reason character varying, +i_disconnect_initiator integer, +i_internal_disconnect_code integer, +i_internal_disconnect_reason character varying, +i_lega_disconnect_code integer, +i_lega_disconnect_reason character varying, +i_orig_call_id character varying, +i_term_call_id character varying, +i_local_tag character varying, +i_legb_local_tag character varying, +i_msg_logger_path character varying, +i_dump_level_id smallint, +i_audio_recorded boolean, +i_rtp_stats_data json, +i_rtp_statistics json, +i_global_tag character varying, +i_resources character varying, +i_active_resources json, +i_failed_resource_type_id smallint, +i_failed_resource_id bigint, +i_dtmf_events json, +i_versions json, +i_is_redirected boolean, +i_dynamic json, +i_lega_headers json, +i_legb_headers json, +i_lega_identity json +) RETURNS integer + LANGUAGE plpgsql SECURITY DEFINER COST 10 + AS $$ +DECLARE + v_cdr cdr.cdr%rowtype; + + v_time_data switch.time_data_ty; + v_version_data switch.versions_ty; + v_dynamic switch.dynamic_cdr_data_ty; + + v_nozerolen boolean; + v_config sys.config%rowtype; + + v_rtp_rx_stream_data rtp_statistics.rx_streams%rowtype; + v_rtp_tx_stream_data rtp_statistics.tx_streams%rowtype; + + v_lega_headers switch.lega_headers_ty; + v_legb_headers switch.legb_headers_ty; + v_lega_reason switch.reason_ty; + v_legb_reason switch.reason_ty; +BEGIN + -- raise warning 'type: % id: %', i_failed_resource_type_id, i_failed_resource_id; + -- RAISE warning 'DTMF: %', i_dtmf_events; + + v_time_data:=json_populate_record(null::switch.time_data_ty, i_time_data); + v_version_data:=json_populate_record(null::switch.versions_ty, i_versions); + v_dynamic:=json_populate_record(null::switch.dynamic_cdr_data_ty, i_dynamic); + + v_lega_headers:=json_populate_record(null::switch.lega_headers_ty, i_lega_headers); + v_legb_headers:=json_populate_record(null::switch.legb_headers_ty, i_legb_headers); + + v_cdr.p_charge_info_in = v_lega_headers.p_charge_info; + + v_lega_reason = v_lega_headers.reason; + v_cdr.lega_q850_cause = v_lega_reason.q850_cause; + v_cdr.lega_q850_text = v_lega_reason.q850_text; + v_cdr.lega_q850_params = v_lega_reason.q850_params; + + v_legb_reason = v_legb_headers.reason; + v_cdr.legb_q850_cause = v_legb_reason.q850_cause; + v_cdr.legb_q850_text = v_legb_reason.q850_text; + v_cdr.legb_q850_params = v_legb_reason.q850_params; + + v_cdr.lega_identity = i_lega_identity; + v_cdr.lega_ss_status_id = v_dynamic.lega_ss_status_id; + v_cdr.legb_ss_status_id = v_dynamic.legb_ss_status_id; + + v_cdr.metadata = v_dynamic.metadata::jsonb; + + v_cdr.core_version=v_version_data.core; + v_cdr.yeti_version=v_version_data.yeti; + v_cdr.lega_user_agent=v_version_data.aleg; + v_cdr.legb_user_agent=v_version_data.bleg; + + v_cdr.pop_id=i_pop_id; + v_cdr.node_id=i_node_id; + + v_cdr.src_name_in:=v_dynamic.src_name_in; + v_cdr.src_name_out:=v_dynamic.src_name_out; + + v_cdr.diversion_in:=v_dynamic.diversion_in; + v_cdr.diversion_out:=v_dynamic.diversion_out; + + v_cdr.customer_id:=v_dynamic.customer_id; + v_cdr.customer_external_id:=v_dynamic.customer_external_id; + + v_cdr.customer_acc_id:=v_dynamic.customer_acc_id; + v_cdr.customer_account_check_balance=v_dynamic.customer_acc_check_balance; + v_cdr.customer_acc_external_id=v_dynamic.customer_acc_external_id; + v_cdr.customer_acc_vat:=v_dynamic.customer_acc_vat; + + v_cdr.customer_auth_id:=v_dynamic.customer_auth_id; + v_cdr.customer_auth_external_id:=v_dynamic.customer_auth_external_id; + v_cdr.customer_auth_external_type:=v_dynamic.customer_auth_external_type; + v_cdr.customer_auth_name:=v_dynamic.customer_auth_name; + + v_cdr.vendor_id:=v_dynamic.vendor_id; + v_cdr.vendor_external_id:=v_dynamic.vendor_external_id; + v_cdr.vendor_acc_id:=v_dynamic.vendor_acc_id; + v_cdr.vendor_acc_external_id:=v_dynamic.vendor_acc_external_id; + + v_cdr.destination_id:=v_dynamic.destination_id; + v_cdr.destination_prefix:=v_dynamic.destination_prefix; + v_cdr.dialpeer_id:=v_dynamic.dialpeer_id; + v_cdr.dialpeer_prefix:=v_dynamic.dialpeer_prefix; + + v_cdr.orig_gw_id:=v_dynamic.orig_gw_id; + v_cdr.orig_gw_external_id:=v_dynamic.orig_gw_external_id; + v_cdr.term_gw_id:=v_dynamic.term_gw_id; + v_cdr.term_gw_external_id:=v_dynamic.term_gw_external_id; + + v_cdr.routing_group_id:=v_dynamic.routing_group_id; + v_cdr.rateplan_id:=v_dynamic.rateplan_id; + + v_cdr.routing_attempt=i_routing_attempt; + v_cdr.is_last_cdr=i_is_last_cdr; + + v_cdr.package_counter_id = v_dynamic.package_counter_id; + v_cdr.destination_initial_rate:=v_dynamic.destination_initial_rate::numeric; + v_cdr.destination_next_rate:=v_dynamic.destination_next_rate::numeric; + v_cdr.destination_initial_interval:=v_dynamic.destination_initial_interval; + v_cdr.destination_next_interval:=v_dynamic.destination_next_interval; + v_cdr.destination_fee:=v_dynamic.destination_fee; + v_cdr.destination_rate_policy_id:=v_dynamic.destination_rate_policy_id; + v_cdr.destination_reverse_billing=v_dynamic.destination_reverse_billing; + + v_cdr.dialpeer_initial_rate:=v_dynamic.dialpeer_initial_rate::numeric; + v_cdr.dialpeer_next_rate:=v_dynamic.dialpeer_next_rate::numeric; + v_cdr.dialpeer_initial_interval:=v_dynamic.dialpeer_initial_interval; + v_cdr.dialpeer_next_interval:=v_dynamic.dialpeer_next_interval; + v_cdr.dialpeer_fee:=v_dynamic.dialpeer_fee; + v_cdr.dialpeer_reverse_billing=v_dynamic.dialpeer_reverse_billing; + + /* sockets addresses */ + v_cdr.sign_orig_transport_protocol_id = i_lega_transport_protocol_id; + v_cdr.sign_orig_ip = host(i_lega_remote_ip); + v_cdr.sign_orig_port = NULLIF(i_lega_remote_port,0); + v_cdr.sign_orig_local_ip = host(i_lega_local_ip); + v_cdr.sign_orig_local_port = NULLIF(i_lega_local_port,0); + + v_cdr.sign_term_transport_protocol_id = i_legb_transport_protocol_id; + v_cdr.sign_term_ip = host(i_legb_remote_ip); + v_cdr.sign_term_port = NULLIF(i_legb_remote_port,0); + v_cdr.sign_term_local_ip = host(i_legb_local_ip); + v_cdr.sign_term_local_port = NULLIF(i_legb_local_port,0); + + v_cdr.local_tag=i_local_tag; + v_cdr.legb_local_tag=i_legb_local_tag; + v_cdr.legb_ruri=i_legb_ruri; + v_cdr.legb_outbound_proxy=i_legb_outbound_proxy; + + v_cdr.is_redirected=i_is_redirected; + + /* Call time data */ + v_cdr.time_start:=to_timestamp(v_time_data.time_start); + + select into strict v_config * from sys.config; + + if v_time_data.time_connect is not null then + v_cdr.time_connect:=to_timestamp(v_time_data.time_connect); + v_cdr.duration:=switch.duration_round(v_config, v_time_data.time_end-v_time_data.time_connect); -- rounding + v_nozerolen:=true; + v_cdr.success=true; + else + v_cdr.time_connect:=NULL; + v_cdr.duration:=0; + v_nozerolen:=false; + v_cdr.success=false; + end if; + v_cdr.routing_delay=(v_time_data.leg_b_time-v_time_data.time_start)::real; + v_cdr.pdd=(coalesce(v_time_data.time_18x,v_time_data.time_connect)-v_time_data.time_start)::real; + v_cdr.rtt=(coalesce(v_time_data.time_1xx,v_time_data.time_18x,v_time_data.time_connect)-v_time_data.leg_b_time)::real; + v_cdr.early_media_present=i_early_media_present; + + v_cdr.time_end:=to_timestamp(v_time_data.time_end); + + -- DC processing + v_cdr.legb_disconnect_code:=i_legb_disconnect_code; + v_cdr.legb_disconnect_reason:=i_legb_disconnect_reason; + v_cdr.disconnect_initiator_id:=i_disconnect_initiator; + v_cdr.internal_disconnect_code:=i_internal_disconnect_code; + v_cdr.internal_disconnect_reason:=i_internal_disconnect_reason; + v_cdr.lega_disconnect_code:=i_lega_disconnect_code; + v_cdr.lega_disconnect_reason:=i_lega_disconnect_reason; + + v_cdr.src_prefix_in:=v_dynamic.src_prefix_in; + v_cdr.src_prefix_out:=v_dynamic.src_prefix_out; + v_cdr.dst_prefix_in:=v_dynamic.dst_prefix_in; + v_cdr.dst_prefix_out:=v_dynamic.dst_prefix_out; + + v_cdr.orig_call_id=i_orig_call_id; + v_cdr.term_call_id=i_term_call_id; + + /* removed */ + --v_cdr.dump_file:=i_msg_logger_path; + + v_cdr.dump_level_id:=i_dump_level_id; + v_cdr.audio_recorded:=i_audio_recorded; + + v_cdr.auth_orig_transport_protocol_id=v_dynamic.auth_orig_protocol_id; + v_cdr.auth_orig_ip:=v_dynamic.auth_orig_ip; + v_cdr.auth_orig_ip:=v_dynamic.auth_orig_ip; + v_cdr.auth_orig_port:=v_dynamic.auth_orig_port; + + perform switch.write_rtp_statistics( + i_rtp_statistics, + i_pop_id, + i_node_id, + v_dynamic.orig_gw_id, + v_dynamic.orig_gw_external_id, + v_dynamic.term_gw_id, + v_dynamic.term_gw_external_id, + i_local_tag, + i_legb_local_tag + ); + + v_cdr.global_tag=i_global_tag; + + v_cdr.src_country_id=v_dynamic.src_country_id; + v_cdr.src_network_id=v_dynamic.src_network_id; + v_cdr.dst_country_id=v_dynamic.dst_country_id; + v_cdr.dst_network_id=v_dynamic.dst_network_id; + v_cdr.dst_prefix_routing=v_dynamic.dst_prefix_routing; + v_cdr.src_prefix_routing=v_dynamic.src_prefix_routing; + v_cdr.routing_plan_id=v_dynamic.routing_plan_id; + v_cdr.lrn=v_dynamic.lrn; + v_cdr.lnp_database_id=v_dynamic.lnp_database_id; + + v_cdr.ruri_domain=v_dynamic.ruri_domain; + v_cdr.to_domain=v_dynamic.to_domain; + v_cdr.from_domain=v_dynamic.from_domain; + + v_cdr.src_area_id=v_dynamic.src_area_id; + v_cdr.dst_area_id=v_dynamic.dst_area_id; + v_cdr.routing_tag_ids=v_dynamic.routing_tag_ids; + + + v_cdr.id:=nextval('cdr.cdr_id_seq'::regclass); + v_cdr.uuid:=public.uuid_generate_v1(); + + v_cdr.pai_in=v_dynamic.pai_in; + v_cdr.ppi_in=v_dynamic.ppi_in; + v_cdr.privacy_in=v_dynamic.privacy_in; + v_cdr.rpid_in=v_dynamic.rpid_in; + v_cdr.rpid_privacy_in=v_dynamic.rpid_privacy_in; + v_cdr.pai_out=v_dynamic.pai_out; + v_cdr.ppi_out=v_dynamic.ppi_out; + v_cdr.privacy_out=v_dynamic.privacy_out; + v_cdr.rpid_out=v_dynamic.rpid_out; + v_cdr.rpid_privacy_out=v_dynamic.rpid_privacy_out; + + v_cdr.failed_resource_type_id = i_failed_resource_type_id; + v_cdr.failed_resource_id = i_failed_resource_id; + + v_cdr:=billing.bill_cdr(v_cdr); + + if not v_config.disable_realtime_statistics then + perform stats.update_rt_stats(v_cdr); + end if; + + v_cdr.customer_price = switch.customer_price_round(v_config, v_cdr.customer_price); + v_cdr.customer_price_no_vat = switch.customer_price_round(v_config, v_cdr.customer_price_no_vat); + v_cdr.vendor_price = switch.vendor_price_round(v_config, v_cdr.vendor_price); + + -- generate event to billing engine + perform event.billing_insert_event('cdr_full',v_cdr); + perform event.streaming_insert_event(v_cdr); + INSERT INTO cdr.cdr VALUES( v_cdr.*); + RETURN 0; +END; +$$; + + +CREATE OR REPLACE FUNCTION switch.writecdr( +i_is_master boolean, +i_node_id integer, +i_pop_id integer, +i_routing_attempt integer, +i_is_last_cdr boolean, +i_lega_transport_protocol_id smallint, +i_lega_local_ip inet, +i_lega_local_port integer, +i_lega_remote_ip inet, +i_lega_remote_port integer, +i_legb_transport_protocol_id smallint, +i_legb_local_ip inet, +i_legb_local_port integer, +i_legb_remote_ip inet, +i_legb_remote_port integer, +i_legb_ruri character varying, +i_legb_outbound_proxy character varying, +i_time_data json, +i_early_media_present boolean, +i_legb_disconnect_code integer, +i_legb_disconnect_reason character varying, +i_disconnect_initiator integer, +i_internal_disconnect_code integer, +i_internal_disconnect_reason character varying, +i_lega_disconnect_code integer, +i_lega_disconnect_reason character varying, +i_internal_disconnect_code_id smallint, +i_orig_call_id character varying, +i_term_call_id character varying, +i_local_tag character varying, +i_legb_local_tag character varying, +i_msg_logger_path character varying, +i_dump_level_id smallint, +i_audio_recorded boolean, +i_rtp_stats_data json, +i_rtp_statistics json, +i_global_tag character varying, +i_resources character varying, +i_active_resources json, +i_failed_resource_type_id smallint, +i_failed_resource_id bigint, +i_dtmf_events json, +i_versions json, +i_is_redirected boolean, +i_dynamic json, +i_lega_headers json, +i_legb_headers json, +i_lega_identity json +) RETURNS integer + LANGUAGE plpgsql SECURITY DEFINER COST 10 + AS $$ +DECLARE + v_cdr cdr.cdr%rowtype; + + v_time_data switch.time_data_ty; + v_version_data switch.versions_ty; + v_dynamic switch.dynamic_cdr_data_ty; + + v_nozerolen boolean; + v_config sys.config%rowtype; + + v_rtp_rx_stream_data rtp_statistics.rx_streams%rowtype; + v_rtp_tx_stream_data rtp_statistics.tx_streams%rowtype; + + v_lega_headers switch.lega_headers_ty; + v_legb_headers switch.legb_headers_ty; + v_lega_reason switch.reason_ty; + v_legb_reason switch.reason_ty; +BEGIN + -- raise warning 'type: % id: %', i_failed_resource_type_id, i_failed_resource_id; + -- RAISE warning 'DTMF: %', i_dtmf_events; + + v_time_data:=json_populate_record(null::switch.time_data_ty, i_time_data); + v_version_data:=json_populate_record(null::switch.versions_ty, i_versions); + v_dynamic:=json_populate_record(null::switch.dynamic_cdr_data_ty, i_dynamic); + + v_lega_headers:=json_populate_record(null::switch.lega_headers_ty, i_lega_headers); + v_legb_headers:=json_populate_record(null::switch.legb_headers_ty, i_legb_headers); + + v_cdr.p_charge_info_in = v_lega_headers.p_charge_info; + + v_lega_reason = v_lega_headers.reason; + v_cdr.lega_q850_cause = v_lega_reason.q850_cause; + v_cdr.lega_q850_text = v_lega_reason.q850_text; + v_cdr.lega_q850_params = v_lega_reason.q850_params; + + v_legb_reason = v_legb_headers.reason; + v_cdr.legb_q850_cause = v_legb_reason.q850_cause; + v_cdr.legb_q850_text = v_legb_reason.q850_text; + v_cdr.legb_q850_params = v_legb_reason.q850_params; + + v_cdr.lega_identity = i_lega_identity; + v_cdr.lega_ss_status_id = v_dynamic.lega_ss_status_id; + v_cdr.legb_ss_status_id = v_dynamic.legb_ss_status_id; + + v_cdr.metadata = v_dynamic.metadata::jsonb; + + v_cdr.core_version=v_version_data.core; + v_cdr.yeti_version=v_version_data.yeti; + v_cdr.lega_user_agent=v_version_data.aleg; + v_cdr.legb_user_agent=v_version_data.bleg; + + v_cdr.pop_id=i_pop_id; + v_cdr.node_id=i_node_id; + + v_cdr.src_name_in:=v_dynamic.src_name_in; + v_cdr.src_name_out:=v_dynamic.src_name_out; + + v_cdr.diversion_in:=v_dynamic.diversion_in; + v_cdr.diversion_out:=v_dynamic.diversion_out; + + v_cdr.customer_id:=v_dynamic.customer_id; + v_cdr.customer_external_id:=v_dynamic.customer_external_id; + + v_cdr.customer_acc_id:=v_dynamic.customer_acc_id; + v_cdr.customer_account_check_balance=v_dynamic.customer_acc_check_balance; + v_cdr.customer_acc_external_id=v_dynamic.customer_acc_external_id; + v_cdr.customer_acc_vat:=v_dynamic.customer_acc_vat; + + v_cdr.customer_auth_id:=v_dynamic.customer_auth_id; + v_cdr.customer_auth_external_id:=v_dynamic.customer_auth_external_id; + v_cdr.customer_auth_external_type:=v_dynamic.customer_auth_external_type; + v_cdr.customer_auth_name:=v_dynamic.customer_auth_name; + + v_cdr.vendor_id:=v_dynamic.vendor_id; + v_cdr.vendor_external_id:=v_dynamic.vendor_external_id; + v_cdr.vendor_acc_id:=v_dynamic.vendor_acc_id; + v_cdr.vendor_acc_external_id:=v_dynamic.vendor_acc_external_id; + + v_cdr.package_counter_id = v_dynamic.package_counter_id; + v_cdr.destination_id:=v_dynamic.destination_id; + v_cdr.destination_prefix:=v_dynamic.destination_prefix; + v_cdr.dialpeer_id:=v_dynamic.dialpeer_id; + v_cdr.dialpeer_prefix:=v_dynamic.dialpeer_prefix; + + v_cdr.orig_gw_id:=v_dynamic.orig_gw_id; + v_cdr.orig_gw_external_id:=v_dynamic.orig_gw_external_id; + v_cdr.term_gw_id:=v_dynamic.term_gw_id; + v_cdr.term_gw_external_id:=v_dynamic.term_gw_external_id; + + v_cdr.routing_group_id:=v_dynamic.routing_group_id; + v_cdr.rateplan_id:=v_dynamic.rateplan_id; + + v_cdr.routing_attempt=i_routing_attempt; + v_cdr.is_last_cdr=i_is_last_cdr; + + v_cdr.destination_initial_rate:=v_dynamic.destination_initial_rate::numeric; + v_cdr.destination_next_rate:=v_dynamic.destination_next_rate::numeric; + v_cdr.destination_initial_interval:=v_dynamic.destination_initial_interval; + v_cdr.destination_next_interval:=v_dynamic.destination_next_interval; + v_cdr.destination_fee:=v_dynamic.destination_fee; + v_cdr.destination_rate_policy_id:=v_dynamic.destination_rate_policy_id; + v_cdr.destination_reverse_billing=v_dynamic.destination_reverse_billing; + + v_cdr.dialpeer_initial_rate:=v_dynamic.dialpeer_initial_rate::numeric; + v_cdr.dialpeer_next_rate:=v_dynamic.dialpeer_next_rate::numeric; + v_cdr.dialpeer_initial_interval:=v_dynamic.dialpeer_initial_interval; + v_cdr.dialpeer_next_interval:=v_dynamic.dialpeer_next_interval; + v_cdr.dialpeer_fee:=v_dynamic.dialpeer_fee; + v_cdr.dialpeer_reverse_billing=v_dynamic.dialpeer_reverse_billing; + + /* sockets addresses */ + v_cdr.sign_orig_transport_protocol_id = i_lega_transport_protocol_id; + v_cdr.sign_orig_ip = host(i_lega_remote_ip); + v_cdr.sign_orig_port = NULLIF(i_lega_remote_port,0); + v_cdr.sign_orig_local_ip = host(i_lega_local_ip); + v_cdr.sign_orig_local_port = NULLIF(i_lega_local_port,0); + + v_cdr.sign_term_transport_protocol_id = i_legb_transport_protocol_id; + v_cdr.sign_term_ip = host(i_legb_remote_ip); + v_cdr.sign_term_port = NULLIF(i_legb_remote_port,0); + v_cdr.sign_term_local_ip = host(i_legb_local_ip); + v_cdr.sign_term_local_port = NULLIF(i_legb_local_port,0); + + v_cdr.local_tag=i_local_tag; + v_cdr.legb_local_tag=i_legb_local_tag; + v_cdr.legb_ruri=i_legb_ruri; + v_cdr.legb_outbound_proxy=i_legb_outbound_proxy; + + v_cdr.is_redirected=i_is_redirected; + + /* Call time data */ + v_cdr.time_start:=to_timestamp(v_time_data.time_start); + + select into strict v_config * from sys.config; + + if v_time_data.time_connect is not null then + v_cdr.time_connect:=to_timestamp(v_time_data.time_connect); + v_cdr.duration:=switch.duration_round(v_config, v_time_data.time_end-v_time_data.time_connect); -- rounding + v_nozerolen:=true; + v_cdr.success=true; + else + v_cdr.time_connect:=NULL; + v_cdr.duration:=0; + v_nozerolen:=false; + v_cdr.success=false; + end if; + v_cdr.routing_delay=(v_time_data.leg_b_time-v_time_data.time_start)::real; + v_cdr.pdd=(coalesce(v_time_data.time_18x,v_time_data.time_connect)-v_time_data.time_start)::real; + v_cdr.rtt=(coalesce(v_time_data.time_1xx,v_time_data.time_18x,v_time_data.time_connect)-v_time_data.leg_b_time)::real; + v_cdr.early_media_present=i_early_media_present; + + v_cdr.time_end:=to_timestamp(v_time_data.time_end); + + -- DC processing + v_cdr.legb_disconnect_code=i_legb_disconnect_code; + v_cdr.legb_disconnect_reason=i_legb_disconnect_reason; + v_cdr.disconnect_initiator_id=i_disconnect_initiator; + v_cdr.internal_disconnect_code_id=i_internal_disconnect_code_id; + v_cdr.internal_disconnect_code=i_internal_disconnect_code; + v_cdr.internal_disconnect_reason=i_internal_disconnect_reason; + v_cdr.lega_disconnect_code=i_lega_disconnect_code; + v_cdr.lega_disconnect_reason=i_lega_disconnect_reason; + + v_cdr.src_prefix_in:=v_dynamic.src_prefix_in; + v_cdr.src_prefix_out:=v_dynamic.src_prefix_out; + v_cdr.dst_prefix_in:=v_dynamic.dst_prefix_in; + v_cdr.dst_prefix_out:=v_dynamic.dst_prefix_out; + + v_cdr.orig_call_id=i_orig_call_id; + v_cdr.term_call_id=i_term_call_id; + + /* removed */ + --v_cdr.dump_file:=i_msg_logger_path; + + v_cdr.dump_level_id:=i_dump_level_id; + v_cdr.audio_recorded:=i_audio_recorded; + + v_cdr.auth_orig_transport_protocol_id=v_dynamic.auth_orig_protocol_id; + v_cdr.auth_orig_ip:=v_dynamic.auth_orig_ip; + v_cdr.auth_orig_ip:=v_dynamic.auth_orig_ip; + v_cdr.auth_orig_port:=v_dynamic.auth_orig_port; + + perform switch.write_rtp_statistics( + i_rtp_statistics, + i_pop_id, + i_node_id, + v_dynamic.orig_gw_id, + v_dynamic.orig_gw_external_id, + v_dynamic.term_gw_id, + v_dynamic.term_gw_external_id, + i_local_tag, + i_legb_local_tag + ); + + v_cdr.global_tag=i_global_tag; + + v_cdr.src_country_id=v_dynamic.src_country_id; + v_cdr.src_network_id=v_dynamic.src_network_id; + v_cdr.dst_country_id=v_dynamic.dst_country_id; + v_cdr.dst_network_id=v_dynamic.dst_network_id; + v_cdr.dst_prefix_routing=v_dynamic.dst_prefix_routing; + v_cdr.src_prefix_routing=v_dynamic.src_prefix_routing; + v_cdr.routing_plan_id=v_dynamic.routing_plan_id; + v_cdr.lrn=v_dynamic.lrn; + v_cdr.lnp_database_id=v_dynamic.lnp_database_id; + + v_cdr.ruri_domain=v_dynamic.ruri_domain; + v_cdr.to_domain=v_dynamic.to_domain; + v_cdr.from_domain=v_dynamic.from_domain; + + v_cdr.src_area_id=v_dynamic.src_area_id; + v_cdr.dst_area_id=v_dynamic.dst_area_id; + v_cdr.routing_tag_ids=v_dynamic.routing_tag_ids; + + + v_cdr.id:=nextval('cdr.cdr_id_seq'::regclass); + v_cdr.uuid:=public.uuid_generate_v1(); + + v_cdr.pai_in=v_dynamic.pai_in; + v_cdr.ppi_in=v_dynamic.ppi_in; + v_cdr.privacy_in=v_dynamic.privacy_in; + v_cdr.rpid_in=v_dynamic.rpid_in; + v_cdr.rpid_privacy_in=v_dynamic.rpid_privacy_in; + v_cdr.pai_out=v_dynamic.pai_out; + v_cdr.ppi_out=v_dynamic.ppi_out; + v_cdr.privacy_out=v_dynamic.privacy_out; + v_cdr.rpid_out=v_dynamic.rpid_out; + v_cdr.rpid_privacy_out=v_dynamic.rpid_privacy_out; + + v_cdr.failed_resource_type_id = i_failed_resource_type_id; + v_cdr.failed_resource_id = i_failed_resource_id; + + v_cdr:=billing.bill_cdr(v_cdr); + + if not v_config.disable_realtime_statistics then + perform stats.update_rt_stats(v_cdr); + end if; + + v_cdr.customer_price = switch.customer_price_round(v_config, v_cdr.customer_price); + v_cdr.customer_price_no_vat = switch.customer_price_round(v_config, v_cdr.customer_price_no_vat); + v_cdr.vendor_price = switch.vendor_price_round(v_config, v_cdr.vendor_price); + + -- generate event to billing engine + perform event.billing_insert_event('cdr_full',v_cdr); + perform event.streaming_insert_event(v_cdr); + INSERT INTO cdr.cdr VALUES( v_cdr.*); + RETURN 0; +END; +$$; + +CREATE OR REPLACE FUNCTION switch.writecdr( +i_is_master boolean, +i_node_id integer, +i_pop_id integer, +i_routing_attempt integer, +i_is_last_cdr boolean, +i_lega_transport_protocol_id smallint, +i_lega_local_ip inet, +i_lega_local_port integer, +i_lega_remote_ip inet, +i_lega_remote_port integer, +i_legb_transport_protocol_id smallint, +i_legb_local_ip inet, +i_legb_local_port integer, +i_legb_remote_ip inet, +i_legb_remote_port integer, +i_legb_ruri character varying, +i_legb_outbound_proxy character varying, +i_time_data json, +i_early_media_present boolean, +i_legb_disconnect_code integer, +i_legb_disconnect_reason character varying, +i_disconnect_initiator integer, +i_internal_disconnect_code integer, +i_internal_disconnect_reason character varying, +i_lega_disconnect_code integer, +i_lega_disconnect_reason character varying, +i_internal_disconnect_code_id smallint, +i_orig_call_id character varying, +i_term_call_id character varying, +i_local_tag character varying, +i_legb_local_tag character varying, +i_msg_logger_path character varying, +i_dump_level_id smallint, +i_audio_recorded boolean, +i_rtp_stats_data json, +i_rtp_statistics json, +i_global_tag character varying, +i_resources character varying, +i_active_resources json, +i_failed_resource_type_id smallint, +i_failed_resource_id bigint, +i_dtmf_events json, +i_versions json, +i_is_redirected boolean, +i_dynamic json, +i_lega_request_headers json, +i_legb_request_headers json, +i_legb_reply_headers json, +i_lega_identity json +) RETURNS integer + LANGUAGE plpgsql SECURITY DEFINER COST 10 + AS $$ +DECLARE + v_cdr cdr.cdr%rowtype; + + v_time_data switch.time_data_ty; + v_version_data switch.versions_ty; + v_dynamic switch.dynamic_cdr_data_ty; + + v_nozerolen boolean; + v_config sys.config%rowtype; + + v_rtp_rx_stream_data rtp_statistics.rx_streams%rowtype; + v_rtp_tx_stream_data rtp_statistics.tx_streams%rowtype; + + v_lega_request_headers switch.lega_request_headers_ty; + v_legb_request_headers switch.legb_request_headers_ty; + v_legb_reply_headers switch.legb_reply_headers_ty; + v_lega_reason switch.reason_ty; + v_legb_reason switch.reason_ty; +BEGIN + -- raise warning 'type: % id: %', i_failed_resource_type_id, i_failed_resource_id; + -- RAISE warning 'DTMF: %', i_dtmf_events; + + v_time_data:=json_populate_record(null::switch.time_data_ty, i_time_data); + v_version_data:=json_populate_record(null::switch.versions_ty, i_versions); + v_dynamic:=json_populate_record(null::switch.dynamic_cdr_data_ty, i_dynamic); + + v_lega_request_headers = json_populate_record(null::switch.lega_request_headers_ty, i_lega_request_headers); + v_legb_request_headers = json_populate_record(null::switch.legb_request_headers_ty, i_legb_request_headers); + v_legb_reply_headers = json_populate_record(null::switch.legb_reply_headers_ty, i_legb_reply_headers); + + v_cdr.p_charge_info_in = v_lega_request_headers.p_charge_info; + + v_lega_reason = v_lega_request_headers.reason; + v_cdr.lega_q850_cause = v_lega_reason.q850_cause; + v_cdr.lega_q850_text = v_lega_reason.q850_text; + v_cdr.lega_q850_params = v_lega_reason.q850_params; + + v_legb_reason = v_legb_reply_headers.reason; + v_cdr.legb_q850_cause = v_legb_reason.q850_cause; + v_cdr.legb_q850_text = v_legb_reason.q850_text; + v_cdr.legb_q850_params = v_legb_reason.q850_params; + + v_cdr.diversion_in = array_to_string(v_lega_request_headers.diversion, ','); + v_cdr.pai_in = array_to_string(v_lega_request_headers.p_asserted_identity, ','); + v_cdr.ppi_in = v_lega_request_headers.p_preferred_identity; + v_cdr.privacy_in = array_to_string(v_lega_request_headers.privacy, ','); + v_cdr.rpid_in = array_to_string(v_lega_request_headers.remote_party_id, ','); + v_cdr.rpid_privacy_in = array_to_string(v_lega_request_headers.rpid_privacy, ','); + + v_cdr.diversion_out = array_to_string(v_legb_request_headers.diversion, ','); + v_cdr.pai_out = array_to_string(v_legb_request_headers.p_asserted_identity, ','); + v_cdr.ppi_out = v_legb_request_headers.p_preferred_identity; + v_cdr.privacy_out = array_to_string(v_legb_request_headers.privacy, ','); + v_cdr.rpid_out = array_to_string(v_legb_request_headers.remote_party_id, ','); + v_cdr.rpid_privacy_out = array_to_string(v_legb_request_headers.rpid_privacy, ','); + + v_cdr.lega_identity = i_lega_identity; + v_cdr.lega_ss_status_id = v_dynamic.lega_ss_status_id; + v_cdr.legb_ss_status_id = v_dynamic.legb_ss_status_id; + + v_cdr.metadata = v_dynamic.metadata::jsonb; + + v_cdr.core_version=v_version_data.core; + v_cdr.yeti_version=v_version_data.yeti; + v_cdr.lega_user_agent=v_version_data.aleg; + v_cdr.legb_user_agent=v_version_data.bleg; + + v_cdr.pop_id=i_pop_id; + v_cdr.node_id=i_node_id; + + v_cdr.src_name_in:=v_dynamic.src_name_in; + v_cdr.src_name_out:=v_dynamic.src_name_out; + + v_cdr.customer_id:=v_dynamic.customer_id; + v_cdr.customer_external_id:=v_dynamic.customer_external_id; + + v_cdr.customer_acc_id:=v_dynamic.customer_acc_id; + v_cdr.customer_account_check_balance=v_dynamic.customer_acc_check_balance; + v_cdr.customer_acc_external_id=v_dynamic.customer_acc_external_id; + v_cdr.customer_acc_vat:=v_dynamic.customer_acc_vat; + + v_cdr.customer_auth_id:=v_dynamic.customer_auth_id; + v_cdr.customer_auth_external_id:=v_dynamic.customer_auth_external_id; + v_cdr.customer_auth_external_type:=v_dynamic.customer_auth_external_type; + v_cdr.customer_auth_name:=v_dynamic.customer_auth_name; + + v_cdr.vendor_id:=v_dynamic.vendor_id; + v_cdr.vendor_external_id:=v_dynamic.vendor_external_id; + v_cdr.vendor_acc_id:=v_dynamic.vendor_acc_id; + v_cdr.vendor_acc_external_id:=v_dynamic.vendor_acc_external_id; + + v_cdr.package_counter_id = v_dynamic.package_counter_id; + v_cdr.destination_id:=v_dynamic.destination_id; + v_cdr.destination_prefix:=v_dynamic.destination_prefix; + v_cdr.dialpeer_id:=v_dynamic.dialpeer_id; + v_cdr.dialpeer_prefix:=v_dynamic.dialpeer_prefix; + + v_cdr.orig_gw_id:=v_dynamic.orig_gw_id; + v_cdr.orig_gw_external_id:=v_dynamic.orig_gw_external_id; + v_cdr.term_gw_id:=v_dynamic.term_gw_id; + v_cdr.term_gw_external_id:=v_dynamic.term_gw_external_id; + + v_cdr.routing_group_id:=v_dynamic.routing_group_id; + v_cdr.rateplan_id:=v_dynamic.rateplan_id; + + v_cdr.routing_attempt=i_routing_attempt; + v_cdr.is_last_cdr=i_is_last_cdr; + + v_cdr.destination_initial_rate:=v_dynamic.destination_initial_rate::numeric; + v_cdr.destination_next_rate:=v_dynamic.destination_next_rate::numeric; + v_cdr.destination_initial_interval:=v_dynamic.destination_initial_interval; + v_cdr.destination_next_interval:=v_dynamic.destination_next_interval; + v_cdr.destination_fee:=v_dynamic.destination_fee; + v_cdr.destination_rate_policy_id:=v_dynamic.destination_rate_policy_id; + v_cdr.destination_reverse_billing=v_dynamic.destination_reverse_billing; + + v_cdr.dialpeer_initial_rate:=v_dynamic.dialpeer_initial_rate::numeric; + v_cdr.dialpeer_next_rate:=v_dynamic.dialpeer_next_rate::numeric; + v_cdr.dialpeer_initial_interval:=v_dynamic.dialpeer_initial_interval; + v_cdr.dialpeer_next_interval:=v_dynamic.dialpeer_next_interval; + v_cdr.dialpeer_fee:=v_dynamic.dialpeer_fee; + v_cdr.dialpeer_reverse_billing=v_dynamic.dialpeer_reverse_billing; + + /* sockets addresses */ + v_cdr.sign_orig_transport_protocol_id = i_lega_transport_protocol_id; + v_cdr.sign_orig_ip = host(i_lega_remote_ip); + v_cdr.sign_orig_port = NULLIF(i_lega_remote_port,0); + v_cdr.sign_orig_local_ip = host(i_lega_local_ip); + v_cdr.sign_orig_local_port = NULLIF(i_lega_local_port,0); + + v_cdr.sign_term_transport_protocol_id = i_legb_transport_protocol_id; + v_cdr.sign_term_ip = host(i_legb_remote_ip); + v_cdr.sign_term_port = NULLIF(i_legb_remote_port,0); + v_cdr.sign_term_local_ip = host(i_legb_local_ip); + v_cdr.sign_term_local_port = NULLIF(i_legb_local_port,0); + + v_cdr.local_tag = i_local_tag; + v_cdr.legb_local_tag = i_legb_local_tag; + v_cdr.legb_ruri = i_legb_ruri; + v_cdr.legb_outbound_proxy = i_legb_outbound_proxy; + + v_cdr.is_redirected = i_is_redirected; + + /* Call time data */ + v_cdr.time_start = to_timestamp(v_time_data.time_start); + + select into strict v_config * from sys.config; + + if v_time_data.time_connect is not null then + v_cdr.time_connect = to_timestamp(v_time_data.time_connect); + v_cdr.duration = switch.duration_round(v_config, v_time_data.time_end-v_time_data.time_connect); -- rounding + v_nozerolen = true; + v_cdr.success = true; + else + v_cdr.time_connect = NULL; + v_cdr.duration = 0; + v_nozerolen = false; + v_cdr.success = false; + end if; + v_cdr.routing_delay = (v_time_data.leg_b_time-v_time_data.time_start)::real; + v_cdr.pdd = (coalesce(v_time_data.time_18x,v_time_data.time_connect)-v_time_data.time_start)::real; + v_cdr.rtt = (coalesce(v_time_data.time_1xx,v_time_data.time_18x,v_time_data.time_connect)-v_time_data.leg_b_time)::real; + v_cdr.early_media_present = i_early_media_present; + + v_cdr.time_end = to_timestamp(v_time_data.time_end); + + -- DC processing + v_cdr.legb_disconnect_code = i_legb_disconnect_code; + v_cdr.legb_disconnect_reason = i_legb_disconnect_reason; + v_cdr.disconnect_initiator_id = i_disconnect_initiator; + v_cdr.internal_disconnect_code_id = i_internal_disconnect_code_id; + v_cdr.internal_disconnect_code = i_internal_disconnect_code; + v_cdr.internal_disconnect_reason = i_internal_disconnect_reason; + v_cdr.lega_disconnect_code = i_lega_disconnect_code; + v_cdr.lega_disconnect_reason = i_lega_disconnect_reason; + + v_cdr.src_prefix_in = v_dynamic.src_prefix_in; + v_cdr.src_prefix_out = v_dynamic.src_prefix_out; + v_cdr.dst_prefix_in = v_dynamic.dst_prefix_in; + v_cdr.dst_prefix_out = v_dynamic.dst_prefix_out; + + v_cdr.orig_call_id = i_orig_call_id; + v_cdr.term_call_id = i_term_call_id; + + /* removed */ + --v_cdr.dump_file = i_msg_logger_path; + + v_cdr.dump_level_id = i_dump_level_id; + v_cdr.audio_recorded = i_audio_recorded; + + v_cdr.auth_orig_transport_protocol_id = v_dynamic.auth_orig_protocol_id; + v_cdr.auth_orig_ip = v_dynamic.auth_orig_ip; + v_cdr.auth_orig_ip = v_dynamic.auth_orig_ip; + v_cdr.auth_orig_port = v_dynamic.auth_orig_port; + + perform switch.write_rtp_statistics( + i_rtp_statistics, + i_pop_id, + i_node_id, + v_dynamic.orig_gw_id, + v_dynamic.orig_gw_external_id, + v_dynamic.term_gw_id, + v_dynamic.term_gw_external_id, + i_local_tag, + i_legb_local_tag + ); + + v_cdr.global_tag = i_global_tag; + + v_cdr.src_country_id = v_dynamic.src_country_id; + v_cdr.src_network_id = v_dynamic.src_network_id; + v_cdr.src_network_type_id = v_dynamic.src_network_type_id; + v_cdr.dst_country_id = v_dynamic.dst_country_id; + v_cdr.dst_network_id = v_dynamic.dst_network_id; + v_cdr.dst_network_type_id = v_dynamic.dst_network_type_id; + + v_cdr.dst_prefix_routing = v_dynamic.dst_prefix_routing; + v_cdr.src_prefix_routing = v_dynamic.src_prefix_routing; + v_cdr.routing_plan_id = v_dynamic.routing_plan_id; + v_cdr.lrn = v_dynamic.lrn; + v_cdr.lnp_database_id = v_dynamic.lnp_database_id; + + v_cdr.ruri_domain = v_dynamic.ruri_domain; + v_cdr.to_domain = v_dynamic.to_domain; + v_cdr.from_domain = v_dynamic.from_domain; + + v_cdr.src_area_id = v_dynamic.src_area_id; + v_cdr.dst_area_id = v_dynamic.dst_area_id; + v_cdr.routing_tag_ids = v_dynamic.routing_tag_ids; + + + v_cdr.id = nextval('cdr.cdr_id_seq'::regclass); + v_cdr.uuid = public.uuid_generate_v1(); + + v_cdr.failed_resource_type_id = i_failed_resource_type_id; + v_cdr.failed_resource_id = i_failed_resource_id; + + v_cdr = billing.bill_cdr(v_cdr); + + if not v_config.disable_realtime_statistics then + perform stats.update_rt_stats(v_cdr); + end if; + + v_cdr.customer_price = switch.customer_price_round(v_config, v_cdr.customer_price); + v_cdr.customer_price_no_vat = switch.customer_price_round(v_config, v_cdr.customer_price_no_vat); + v_cdr.vendor_price = switch.vendor_price_round(v_config, v_cdr.vendor_price); + + -- generate event to billing engine + perform event.billing_insert_event('cdr_full',v_cdr); + perform event.streaming_insert_event(v_cdr); + INSERT INTO cdr.cdr VALUES( v_cdr.*); + RETURN 0; +END; +$$; + } + + end + + def down + execute %q{ + +DROP FUNCTION switch.writecdr( +i_is_master boolean, +i_node_id integer, +i_pop_id integer, +i_routing_attempt integer, +i_is_last_cdr boolean, +i_lega_transport_protocol_id smallint, +i_lega_local_ip inet, +i_lega_local_port integer, +i_lega_remote_ip inet, +i_lega_remote_port integer, +i_legb_transport_protocol_id smallint, +i_legb_local_ip inet, +i_legb_local_port integer, +i_legb_remote_ip inet, +i_legb_remote_port integer, +i_legb_ruri character varying, +i_legb_outbound_proxy character varying, +i_time_data json, +i_early_media_present boolean, +i_legb_disconnect_code integer, +i_legb_disconnect_reason character varying, +i_disconnect_initiator integer, +i_internal_disconnect_code integer, +i_internal_disconnect_reason character varying, +i_lega_disconnect_code integer, +i_lega_disconnect_reason character varying, +i_orig_call_id character varying, +i_term_call_id character varying, +i_local_tag character varying, +i_legb_local_tag character varying, +i_msg_logger_path character varying, +i_dump_level_id smallint, +i_audio_recorded boolean, +i_rtp_stats_data json, +i_rtp_statistics json, +i_global_tag character varying, +i_resources character varying, +i_active_resources json, +i_failed_resource_type_id smallint, +i_failed_resource_id bigint, +i_dtmf_events json, +i_versions json, +i_is_redirected boolean, +i_dynamic json, +i_lega_headers json, +i_legb_headers json, +i_lega_identity json +); + +DROP FUNCTION switch.writecdr( +i_is_master boolean, +i_node_id integer, +i_pop_id integer, +i_routing_attempt integer, +i_is_last_cdr boolean, +i_lega_transport_protocol_id smallint, +i_lega_local_ip inet, +i_lega_local_port integer, +i_lega_remote_ip inet, +i_lega_remote_port integer, +i_legb_transport_protocol_id smallint, +i_legb_local_ip inet, +i_legb_local_port integer, +i_legb_remote_ip inet, +i_legb_remote_port integer, +i_legb_ruri character varying, +i_legb_outbound_proxy character varying, +i_time_data json, +i_early_media_present boolean, +i_legb_disconnect_code integer, +i_legb_disconnect_reason character varying, +i_disconnect_initiator integer, +i_internal_disconnect_code integer, +i_internal_disconnect_reason character varying, +i_lega_disconnect_code integer, +i_lega_disconnect_reason character varying, +i_internal_disconnect_code_id smallint, +i_orig_call_id character varying, +i_term_call_id character varying, +i_local_tag character varying, +i_legb_local_tag character varying, +i_msg_logger_path character varying, +i_dump_level_id smallint, +i_audio_recorded boolean, +i_rtp_stats_data json, +i_rtp_statistics json, +i_global_tag character varying, +i_resources character varying, +i_active_resources json, +i_failed_resource_type_id smallint, +i_failed_resource_id bigint, +i_dtmf_events json, +i_versions json, +i_is_redirected boolean, +i_dynamic json, +i_lega_headers json, +i_legb_headers json, +i_lega_identity json +); + +DROP FUNCTION switch.writecdr( +i_is_master boolean, +i_node_id integer, +i_pop_id integer, +i_routing_attempt integer, +i_is_last_cdr boolean, +i_lega_transport_protocol_id smallint, +i_lega_local_ip inet, +i_lega_local_port integer, +i_lega_remote_ip inet, +i_lega_remote_port integer, +i_legb_transport_protocol_id smallint, +i_legb_local_ip inet, +i_legb_local_port integer, +i_legb_remote_ip inet, +i_legb_remote_port integer, +i_legb_ruri character varying, +i_legb_outbound_proxy character varying, +i_time_data json, +i_early_media_present boolean, +i_legb_disconnect_code integer, +i_legb_disconnect_reason character varying, +i_disconnect_initiator integer, +i_internal_disconnect_code integer, +i_internal_disconnect_reason character varying, +i_lega_disconnect_code integer, +i_lega_disconnect_reason character varying, +i_internal_disconnect_code_id smallint, +i_orig_call_id character varying, +i_term_call_id character varying, +i_local_tag character varying, +i_legb_local_tag character varying, +i_msg_logger_path character varying, +i_dump_level_id smallint, +i_audio_recorded boolean, +i_rtp_stats_data json, +i_rtp_statistics json, +i_global_tag character varying, +i_resources character varying, +i_active_resources json, +i_failed_resource_type_id smallint, +i_failed_resource_id bigint, +i_dtmf_events json, +i_versions json, +i_is_redirected boolean, +i_dynamic json, +i_lega_request_headers json, +i_legb_request_headers json, +i_legb_reply_headers json, +i_lega_identity json +); + +CREATE OR REPLACE FUNCTION switch.writecdr(i_is_master boolean, i_node_id integer, i_pop_id integer, i_routing_attempt integer, i_is_last_cdr boolean, i_lega_transport_protocol_id smallint, i_lega_local_ip character varying, i_lega_local_port integer, i_lega_remote_ip character varying, i_lega_remote_port integer, i_legb_transport_protocol_id smallint, i_legb_local_ip character varying, i_legb_local_port integer, i_legb_remote_ip character varying, i_legb_remote_port integer, i_legb_ruri character varying, i_legb_outbound_proxy character varying, i_time_data json, i_early_media_present boolean, i_legb_disconnect_code integer, i_legb_disconnect_reason character varying, i_disconnect_initiator integer, i_internal_disconnect_code integer, i_internal_disconnect_reason character varying, i_lega_disconnect_code integer, i_lega_disconnect_reason character varying, i_orig_call_id character varying, i_term_call_id character varying, i_local_tag character varying, i_legb_local_tag character varying, i_msg_logger_path character varying, i_dump_level_id smallint, i_audio_recorded boolean, i_rtp_stats_data json, i_rtp_statistics json, i_global_tag character varying, i_resources character varying, i_active_resources json, i_failed_resource_type_id smallint, i_failed_resource_id bigint, i_dtmf_events json, i_versions json, i_is_redirected boolean, i_dynamic json, i_lega_headers json, i_legb_headers json, i_lega_identity json) RETURNS integer + LANGUAGE plpgsql SECURITY DEFINER COST 10 + AS $$ +DECLARE + v_cdr cdr.cdr%rowtype; + + v_time_data switch.time_data_ty; + v_version_data switch.versions_ty; + v_dynamic switch.dynamic_cdr_data_ty; + + v_nozerolen boolean; + v_config sys.config%rowtype; + + v_rtp_rx_stream_data rtp_statistics.rx_streams%rowtype; + v_rtp_tx_stream_data rtp_statistics.tx_streams%rowtype; + + v_lega_headers switch.lega_headers_ty; + v_legb_headers switch.legb_headers_ty; + v_lega_reason switch.reason_ty; + v_legb_reason switch.reason_ty; +BEGIN + -- raise warning 'type: % id: %', i_failed_resource_type_id, i_failed_resource_id; + -- RAISE warning 'DTMF: %', i_dtmf_events; + + v_time_data:=json_populate_record(null::switch.time_data_ty, i_time_data); + v_version_data:=json_populate_record(null::switch.versions_ty, i_versions); + v_dynamic:=json_populate_record(null::switch.dynamic_cdr_data_ty, i_dynamic); + + v_lega_headers:=json_populate_record(null::switch.lega_headers_ty, i_lega_headers); + v_legb_headers:=json_populate_record(null::switch.legb_headers_ty, i_legb_headers); + + v_cdr.p_charge_info_in = v_lega_headers.p_charge_info; + + v_lega_reason = v_lega_headers.reason; + v_cdr.lega_q850_cause = v_lega_reason.q850_cause; + v_cdr.lega_q850_text = v_lega_reason.q850_text; + v_cdr.lega_q850_params = v_lega_reason.q850_params; + + v_legb_reason = v_legb_headers.reason; + v_cdr.legb_q850_cause = v_legb_reason.q850_cause; + v_cdr.legb_q850_text = v_legb_reason.q850_text; + v_cdr.legb_q850_params = v_legb_reason.q850_params; + + v_cdr.lega_identity = i_lega_identity; + v_cdr.lega_ss_status_id = v_dynamic.lega_ss_status_id; + v_cdr.legb_ss_status_id = v_dynamic.legb_ss_status_id; + + v_cdr.metadata = v_dynamic.metadata::jsonb; + + v_cdr.core_version=v_version_data.core; + v_cdr.yeti_version=v_version_data.yeti; + v_cdr.lega_user_agent=v_version_data.aleg; + v_cdr.legb_user_agent=v_version_data.bleg; + + v_cdr.pop_id=i_pop_id; + v_cdr.node_id=i_node_id; + + v_cdr.src_name_in:=v_dynamic.src_name_in; + v_cdr.src_name_out:=v_dynamic.src_name_out; + + v_cdr.diversion_in:=v_dynamic.diversion_in; + v_cdr.diversion_out:=v_dynamic.diversion_out; + + v_cdr.customer_id:=v_dynamic.customer_id; + v_cdr.customer_external_id:=v_dynamic.customer_external_id; + + v_cdr.customer_acc_id:=v_dynamic.customer_acc_id; + v_cdr.customer_account_check_balance=v_dynamic.customer_acc_check_balance; + v_cdr.customer_acc_external_id=v_dynamic.customer_acc_external_id; + v_cdr.customer_acc_vat:=v_dynamic.customer_acc_vat; + + v_cdr.customer_auth_id:=v_dynamic.customer_auth_id; + v_cdr.customer_auth_external_id:=v_dynamic.customer_auth_external_id; + v_cdr.customer_auth_external_type:=v_dynamic.customer_auth_external_type; + v_cdr.customer_auth_name:=v_dynamic.customer_auth_name; + + v_cdr.vendor_id:=v_dynamic.vendor_id; + v_cdr.vendor_external_id:=v_dynamic.vendor_external_id; + v_cdr.vendor_acc_id:=v_dynamic.vendor_acc_id; + v_cdr.vendor_acc_external_id:=v_dynamic.vendor_acc_external_id; + + v_cdr.destination_id:=v_dynamic.destination_id; + v_cdr.destination_prefix:=v_dynamic.destination_prefix; + v_cdr.dialpeer_id:=v_dynamic.dialpeer_id; + v_cdr.dialpeer_prefix:=v_dynamic.dialpeer_prefix; + + v_cdr.orig_gw_id:=v_dynamic.orig_gw_id; + v_cdr.orig_gw_external_id:=v_dynamic.orig_gw_external_id; + v_cdr.term_gw_id:=v_dynamic.term_gw_id; + v_cdr.term_gw_external_id:=v_dynamic.term_gw_external_id; + + v_cdr.routing_group_id:=v_dynamic.routing_group_id; + v_cdr.rateplan_id:=v_dynamic.rateplan_id; + + v_cdr.routing_attempt=i_routing_attempt; + v_cdr.is_last_cdr=i_is_last_cdr; + + v_cdr.package_counter_id = v_dynamic.package_counter_id; + v_cdr.destination_initial_rate:=v_dynamic.destination_initial_rate::numeric; + v_cdr.destination_next_rate:=v_dynamic.destination_next_rate::numeric; + v_cdr.destination_initial_interval:=v_dynamic.destination_initial_interval; + v_cdr.destination_next_interval:=v_dynamic.destination_next_interval; + v_cdr.destination_fee:=v_dynamic.destination_fee; + v_cdr.destination_rate_policy_id:=v_dynamic.destination_rate_policy_id; + v_cdr.destination_reverse_billing=v_dynamic.destination_reverse_billing; + + v_cdr.dialpeer_initial_rate:=v_dynamic.dialpeer_initial_rate::numeric; + v_cdr.dialpeer_next_rate:=v_dynamic.dialpeer_next_rate::numeric; + v_cdr.dialpeer_initial_interval:=v_dynamic.dialpeer_initial_interval; + v_cdr.dialpeer_next_interval:=v_dynamic.dialpeer_next_interval; + v_cdr.dialpeer_fee:=v_dynamic.dialpeer_fee; + v_cdr.dialpeer_reverse_billing=v_dynamic.dialpeer_reverse_billing; + + /* sockets addresses */ + v_cdr.sign_orig_transport_protocol_id=i_lega_transport_protocol_id; + v_cdr.sign_orig_ip=i_lega_remote_ip; + v_cdr.sign_orig_port=NULLIF(i_lega_remote_port,0); + v_cdr.sign_orig_local_ip=i_lega_local_ip; + v_cdr.sign_orig_local_port=NULLIF(i_lega_local_port,0); + + v_cdr.sign_term_transport_protocol_id=i_legb_transport_protocol_id; + v_cdr.sign_term_ip=i_legb_remote_ip; + v_cdr.sign_term_port=NULLIF(i_legb_remote_port,0); + v_cdr.sign_term_local_ip=i_legb_local_ip; + v_cdr.sign_term_local_port=NULLIF(i_legb_local_port,0); + + v_cdr.local_tag=i_local_tag; + v_cdr.legb_local_tag=i_legb_local_tag; + v_cdr.legb_ruri=i_legb_ruri; + v_cdr.legb_outbound_proxy=i_legb_outbound_proxy; + + v_cdr.is_redirected=i_is_redirected; + + /* Call time data */ + v_cdr.time_start:=to_timestamp(v_time_data.time_start); + + select into strict v_config * from sys.config; + + if v_time_data.time_connect is not null then + v_cdr.time_connect:=to_timestamp(v_time_data.time_connect); + v_cdr.duration:=switch.duration_round(v_config, v_time_data.time_end-v_time_data.time_connect); -- rounding + v_nozerolen:=true; + v_cdr.success=true; + else + v_cdr.time_connect:=NULL; + v_cdr.duration:=0; + v_nozerolen:=false; + v_cdr.success=false; + end if; + v_cdr.routing_delay=(v_time_data.leg_b_time-v_time_data.time_start)::real; + v_cdr.pdd=(coalesce(v_time_data.time_18x,v_time_data.time_connect)-v_time_data.time_start)::real; + v_cdr.rtt=(coalesce(v_time_data.time_1xx,v_time_data.time_18x,v_time_data.time_connect)-v_time_data.leg_b_time)::real; + v_cdr.early_media_present=i_early_media_present; + + v_cdr.time_end:=to_timestamp(v_time_data.time_end); + + -- DC processing + v_cdr.legb_disconnect_code:=i_legb_disconnect_code; + v_cdr.legb_disconnect_reason:=i_legb_disconnect_reason; + v_cdr.disconnect_initiator_id:=i_disconnect_initiator; + v_cdr.internal_disconnect_code:=i_internal_disconnect_code; + v_cdr.internal_disconnect_reason:=i_internal_disconnect_reason; + v_cdr.lega_disconnect_code:=i_lega_disconnect_code; + v_cdr.lega_disconnect_reason:=i_lega_disconnect_reason; + + v_cdr.src_prefix_in:=v_dynamic.src_prefix_in; + v_cdr.src_prefix_out:=v_dynamic.src_prefix_out; + v_cdr.dst_prefix_in:=v_dynamic.dst_prefix_in; + v_cdr.dst_prefix_out:=v_dynamic.dst_prefix_out; + + v_cdr.orig_call_id=i_orig_call_id; + v_cdr.term_call_id=i_term_call_id; + + /* removed */ + --v_cdr.dump_file:=i_msg_logger_path; + + v_cdr.dump_level_id:=i_dump_level_id; + v_cdr.audio_recorded:=i_audio_recorded; + + v_cdr.auth_orig_transport_protocol_id=v_dynamic.auth_orig_protocol_id; + v_cdr.auth_orig_ip:=v_dynamic.auth_orig_ip; + v_cdr.auth_orig_ip:=v_dynamic.auth_orig_ip; + v_cdr.auth_orig_port:=v_dynamic.auth_orig_port; + + perform switch.write_rtp_statistics( + i_rtp_statistics, + i_pop_id, + i_node_id, + v_dynamic.orig_gw_id, + v_dynamic.orig_gw_external_id, + v_dynamic.term_gw_id, + v_dynamic.term_gw_external_id, + i_local_tag, + i_legb_local_tag + ); + + v_cdr.global_tag=i_global_tag; + + v_cdr.src_country_id=v_dynamic.src_country_id; + v_cdr.src_network_id=v_dynamic.src_network_id; + v_cdr.dst_country_id=v_dynamic.dst_country_id; + v_cdr.dst_network_id=v_dynamic.dst_network_id; + v_cdr.dst_prefix_routing=v_dynamic.dst_prefix_routing; + v_cdr.src_prefix_routing=v_dynamic.src_prefix_routing; + v_cdr.routing_plan_id=v_dynamic.routing_plan_id; + v_cdr.lrn=v_dynamic.lrn; + v_cdr.lnp_database_id=v_dynamic.lnp_database_id; + + v_cdr.ruri_domain=v_dynamic.ruri_domain; + v_cdr.to_domain=v_dynamic.to_domain; + v_cdr.from_domain=v_dynamic.from_domain; + + v_cdr.src_area_id=v_dynamic.src_area_id; + v_cdr.dst_area_id=v_dynamic.dst_area_id; + v_cdr.routing_tag_ids=v_dynamic.routing_tag_ids; + + + v_cdr.id:=nextval('cdr.cdr_id_seq'::regclass); + v_cdr.uuid:=public.uuid_generate_v1(); + + v_cdr.pai_in=v_dynamic.pai_in; + v_cdr.ppi_in=v_dynamic.ppi_in; + v_cdr.privacy_in=v_dynamic.privacy_in; + v_cdr.rpid_in=v_dynamic.rpid_in; + v_cdr.rpid_privacy_in=v_dynamic.rpid_privacy_in; + v_cdr.pai_out=v_dynamic.pai_out; + v_cdr.ppi_out=v_dynamic.ppi_out; + v_cdr.privacy_out=v_dynamic.privacy_out; + v_cdr.rpid_out=v_dynamic.rpid_out; + v_cdr.rpid_privacy_out=v_dynamic.rpid_privacy_out; + + v_cdr.failed_resource_type_id = i_failed_resource_type_id; + v_cdr.failed_resource_id = i_failed_resource_id; + + v_cdr:=billing.bill_cdr(v_cdr); + + if not v_config.disable_realtime_statistics then + perform stats.update_rt_stats(v_cdr); + end if; + + v_cdr.customer_price = switch.customer_price_round(v_config, v_cdr.customer_price); + v_cdr.customer_price_no_vat = switch.customer_price_round(v_config, v_cdr.customer_price_no_vat); + v_cdr.vendor_price = switch.vendor_price_round(v_config, v_cdr.vendor_price); + + -- generate event to billing engine + perform event.billing_insert_event('cdr_full',v_cdr); + perform event.streaming_insert_event(v_cdr); + INSERT INTO cdr.cdr VALUES( v_cdr.*); + RETURN 0; +END; +$$; + + +CREATE OR REPLACE FUNCTION switch.writecdr(i_is_master boolean, i_node_id integer, i_pop_id integer, i_routing_attempt integer, i_is_last_cdr boolean, i_lega_transport_protocol_id smallint, i_lega_local_ip character varying, i_lega_local_port integer, i_lega_remote_ip character varying, i_lega_remote_port integer, i_legb_transport_protocol_id smallint, i_legb_local_ip character varying, i_legb_local_port integer, i_legb_remote_ip character varying, i_legb_remote_port integer, i_legb_ruri character varying, i_legb_outbound_proxy character varying, i_time_data json, i_early_media_present boolean, i_legb_disconnect_code integer, i_legb_disconnect_reason character varying, i_disconnect_initiator integer, i_internal_disconnect_code integer, i_internal_disconnect_reason character varying, i_lega_disconnect_code integer, i_lega_disconnect_reason character varying, i_internal_disconnect_code_id smallint, i_orig_call_id character varying, i_term_call_id character varying, i_local_tag character varying, i_legb_local_tag character varying, i_msg_logger_path character varying, i_dump_level_id smallint, i_audio_recorded boolean, i_rtp_stats_data json, i_rtp_statistics json, i_global_tag character varying, i_resources character varying, i_active_resources json, i_failed_resource_type_id smallint, i_failed_resource_id bigint, i_dtmf_events json, i_versions json, i_is_redirected boolean, i_dynamic json, i_lega_headers json, i_legb_headers json, i_lega_identity json) RETURNS integer + LANGUAGE plpgsql SECURITY DEFINER COST 10 + AS $$ +DECLARE + v_cdr cdr.cdr%rowtype; + + v_time_data switch.time_data_ty; + v_version_data switch.versions_ty; + v_dynamic switch.dynamic_cdr_data_ty; + + v_nozerolen boolean; + v_config sys.config%rowtype; + + v_rtp_rx_stream_data rtp_statistics.rx_streams%rowtype; + v_rtp_tx_stream_data rtp_statistics.tx_streams%rowtype; + + v_lega_headers switch.lega_headers_ty; + v_legb_headers switch.legb_headers_ty; + v_lega_reason switch.reason_ty; + v_legb_reason switch.reason_ty; +BEGIN + -- raise warning 'type: % id: %', i_failed_resource_type_id, i_failed_resource_id; + -- RAISE warning 'DTMF: %', i_dtmf_events; + + v_time_data:=json_populate_record(null::switch.time_data_ty, i_time_data); + v_version_data:=json_populate_record(null::switch.versions_ty, i_versions); + v_dynamic:=json_populate_record(null::switch.dynamic_cdr_data_ty, i_dynamic); + + v_lega_headers:=json_populate_record(null::switch.lega_headers_ty, i_lega_headers); + v_legb_headers:=json_populate_record(null::switch.legb_headers_ty, i_legb_headers); + + v_cdr.p_charge_info_in = v_lega_headers.p_charge_info; + + v_lega_reason = v_lega_headers.reason; + v_cdr.lega_q850_cause = v_lega_reason.q850_cause; + v_cdr.lega_q850_text = v_lega_reason.q850_text; + v_cdr.lega_q850_params = v_lega_reason.q850_params; + + v_legb_reason = v_legb_headers.reason; + v_cdr.legb_q850_cause = v_legb_reason.q850_cause; + v_cdr.legb_q850_text = v_legb_reason.q850_text; + v_cdr.legb_q850_params = v_legb_reason.q850_params; + + v_cdr.lega_identity = i_lega_identity; + v_cdr.lega_ss_status_id = v_dynamic.lega_ss_status_id; + v_cdr.legb_ss_status_id = v_dynamic.legb_ss_status_id; + + v_cdr.metadata = v_dynamic.metadata::jsonb; + + v_cdr.core_version=v_version_data.core; + v_cdr.yeti_version=v_version_data.yeti; + v_cdr.lega_user_agent=v_version_data.aleg; + v_cdr.legb_user_agent=v_version_data.bleg; + + v_cdr.pop_id=i_pop_id; + v_cdr.node_id=i_node_id; + + v_cdr.src_name_in:=v_dynamic.src_name_in; + v_cdr.src_name_out:=v_dynamic.src_name_out; + + v_cdr.diversion_in:=v_dynamic.diversion_in; + v_cdr.diversion_out:=v_dynamic.diversion_out; + + v_cdr.customer_id:=v_dynamic.customer_id; + v_cdr.customer_external_id:=v_dynamic.customer_external_id; + + v_cdr.customer_acc_id:=v_dynamic.customer_acc_id; + v_cdr.customer_account_check_balance=v_dynamic.customer_acc_check_balance; + v_cdr.customer_acc_external_id=v_dynamic.customer_acc_external_id; + v_cdr.customer_acc_vat:=v_dynamic.customer_acc_vat; + + v_cdr.customer_auth_id:=v_dynamic.customer_auth_id; + v_cdr.customer_auth_external_id:=v_dynamic.customer_auth_external_id; + v_cdr.customer_auth_external_type:=v_dynamic.customer_auth_external_type; + v_cdr.customer_auth_name:=v_dynamic.customer_auth_name; + + v_cdr.vendor_id:=v_dynamic.vendor_id; + v_cdr.vendor_external_id:=v_dynamic.vendor_external_id; + v_cdr.vendor_acc_id:=v_dynamic.vendor_acc_id; + v_cdr.vendor_acc_external_id:=v_dynamic.vendor_acc_external_id; + + v_cdr.package_counter_id = v_dynamic.package_counter_id; + v_cdr.destination_id:=v_dynamic.destination_id; + v_cdr.destination_prefix:=v_dynamic.destination_prefix; + v_cdr.dialpeer_id:=v_dynamic.dialpeer_id; + v_cdr.dialpeer_prefix:=v_dynamic.dialpeer_prefix; + + v_cdr.orig_gw_id:=v_dynamic.orig_gw_id; + v_cdr.orig_gw_external_id:=v_dynamic.orig_gw_external_id; + v_cdr.term_gw_id:=v_dynamic.term_gw_id; + v_cdr.term_gw_external_id:=v_dynamic.term_gw_external_id; + + v_cdr.routing_group_id:=v_dynamic.routing_group_id; + v_cdr.rateplan_id:=v_dynamic.rateplan_id; + + v_cdr.routing_attempt=i_routing_attempt; + v_cdr.is_last_cdr=i_is_last_cdr; + + v_cdr.destination_initial_rate:=v_dynamic.destination_initial_rate::numeric; + v_cdr.destination_next_rate:=v_dynamic.destination_next_rate::numeric; + v_cdr.destination_initial_interval:=v_dynamic.destination_initial_interval; + v_cdr.destination_next_interval:=v_dynamic.destination_next_interval; + v_cdr.destination_fee:=v_dynamic.destination_fee; + v_cdr.destination_rate_policy_id:=v_dynamic.destination_rate_policy_id; + v_cdr.destination_reverse_billing=v_dynamic.destination_reverse_billing; + + v_cdr.dialpeer_initial_rate:=v_dynamic.dialpeer_initial_rate::numeric; + v_cdr.dialpeer_next_rate:=v_dynamic.dialpeer_next_rate::numeric; + v_cdr.dialpeer_initial_interval:=v_dynamic.dialpeer_initial_interval; + v_cdr.dialpeer_next_interval:=v_dynamic.dialpeer_next_interval; + v_cdr.dialpeer_fee:=v_dynamic.dialpeer_fee; + v_cdr.dialpeer_reverse_billing=v_dynamic.dialpeer_reverse_billing; + + /* sockets addresses */ + v_cdr.sign_orig_transport_protocol_id=i_lega_transport_protocol_id; + v_cdr.sign_orig_ip=i_lega_remote_ip; + v_cdr.sign_orig_port=NULLIF(i_lega_remote_port,0); + v_cdr.sign_orig_local_ip=i_lega_local_ip; + v_cdr.sign_orig_local_port=NULLIF(i_lega_local_port,0); + + v_cdr.sign_term_transport_protocol_id=i_legb_transport_protocol_id; + v_cdr.sign_term_ip=i_legb_remote_ip; + v_cdr.sign_term_port=NULLIF(i_legb_remote_port,0); + v_cdr.sign_term_local_ip=i_legb_local_ip; + v_cdr.sign_term_local_port=NULLIF(i_legb_local_port,0); + + v_cdr.local_tag=i_local_tag; + v_cdr.legb_local_tag=i_legb_local_tag; + v_cdr.legb_ruri=i_legb_ruri; + v_cdr.legb_outbound_proxy=i_legb_outbound_proxy; + + v_cdr.is_redirected=i_is_redirected; + + /* Call time data */ + v_cdr.time_start:=to_timestamp(v_time_data.time_start); + + select into strict v_config * from sys.config; + + if v_time_data.time_connect is not null then + v_cdr.time_connect:=to_timestamp(v_time_data.time_connect); + v_cdr.duration:=switch.duration_round(v_config, v_time_data.time_end-v_time_data.time_connect); -- rounding + v_nozerolen:=true; + v_cdr.success=true; + else + v_cdr.time_connect:=NULL; + v_cdr.duration:=0; + v_nozerolen:=false; + v_cdr.success=false; + end if; + v_cdr.routing_delay=(v_time_data.leg_b_time-v_time_data.time_start)::real; + v_cdr.pdd=(coalesce(v_time_data.time_18x,v_time_data.time_connect)-v_time_data.time_start)::real; + v_cdr.rtt=(coalesce(v_time_data.time_1xx,v_time_data.time_18x,v_time_data.time_connect)-v_time_data.leg_b_time)::real; + v_cdr.early_media_present=i_early_media_present; + + v_cdr.time_end:=to_timestamp(v_time_data.time_end); + + -- DC processing + v_cdr.legb_disconnect_code=i_legb_disconnect_code; + v_cdr.legb_disconnect_reason=i_legb_disconnect_reason; + v_cdr.disconnect_initiator_id=i_disconnect_initiator; + v_cdr.internal_disconnect_code_id=i_internal_disconnect_code_id; + v_cdr.internal_disconnect_code=i_internal_disconnect_code; + v_cdr.internal_disconnect_reason=i_internal_disconnect_reason; + v_cdr.lega_disconnect_code=i_lega_disconnect_code; + v_cdr.lega_disconnect_reason=i_lega_disconnect_reason; + + v_cdr.src_prefix_in:=v_dynamic.src_prefix_in; + v_cdr.src_prefix_out:=v_dynamic.src_prefix_out; + v_cdr.dst_prefix_in:=v_dynamic.dst_prefix_in; + v_cdr.dst_prefix_out:=v_dynamic.dst_prefix_out; + + v_cdr.orig_call_id=i_orig_call_id; + v_cdr.term_call_id=i_term_call_id; + + /* removed */ + --v_cdr.dump_file:=i_msg_logger_path; + + v_cdr.dump_level_id:=i_dump_level_id; + v_cdr.audio_recorded:=i_audio_recorded; + + v_cdr.auth_orig_transport_protocol_id=v_dynamic.auth_orig_protocol_id; + v_cdr.auth_orig_ip:=v_dynamic.auth_orig_ip; + v_cdr.auth_orig_ip:=v_dynamic.auth_orig_ip; + v_cdr.auth_orig_port:=v_dynamic.auth_orig_port; + + perform switch.write_rtp_statistics( + i_rtp_statistics, + i_pop_id, + i_node_id, + v_dynamic.orig_gw_id, + v_dynamic.orig_gw_external_id, + v_dynamic.term_gw_id, + v_dynamic.term_gw_external_id, + i_local_tag, + i_legb_local_tag + ); + + v_cdr.global_tag=i_global_tag; + + v_cdr.src_country_id=v_dynamic.src_country_id; + v_cdr.src_network_id=v_dynamic.src_network_id; + v_cdr.dst_country_id=v_dynamic.dst_country_id; + v_cdr.dst_network_id=v_dynamic.dst_network_id; + v_cdr.dst_prefix_routing=v_dynamic.dst_prefix_routing; + v_cdr.src_prefix_routing=v_dynamic.src_prefix_routing; + v_cdr.routing_plan_id=v_dynamic.routing_plan_id; + v_cdr.lrn=v_dynamic.lrn; + v_cdr.lnp_database_id=v_dynamic.lnp_database_id; + + v_cdr.ruri_domain=v_dynamic.ruri_domain; + v_cdr.to_domain=v_dynamic.to_domain; + v_cdr.from_domain=v_dynamic.from_domain; + + v_cdr.src_area_id=v_dynamic.src_area_id; + v_cdr.dst_area_id=v_dynamic.dst_area_id; + v_cdr.routing_tag_ids=v_dynamic.routing_tag_ids; + + + v_cdr.id:=nextval('cdr.cdr_id_seq'::regclass); + v_cdr.uuid:=public.uuid_generate_v1(); + + v_cdr.pai_in=v_dynamic.pai_in; + v_cdr.ppi_in=v_dynamic.ppi_in; + v_cdr.privacy_in=v_dynamic.privacy_in; + v_cdr.rpid_in=v_dynamic.rpid_in; + v_cdr.rpid_privacy_in=v_dynamic.rpid_privacy_in; + v_cdr.pai_out=v_dynamic.pai_out; + v_cdr.ppi_out=v_dynamic.ppi_out; + v_cdr.privacy_out=v_dynamic.privacy_out; + v_cdr.rpid_out=v_dynamic.rpid_out; + v_cdr.rpid_privacy_out=v_dynamic.rpid_privacy_out; + + v_cdr.failed_resource_type_id = i_failed_resource_type_id; + v_cdr.failed_resource_id = i_failed_resource_id; + + v_cdr:=billing.bill_cdr(v_cdr); + + if not v_config.disable_realtime_statistics then + perform stats.update_rt_stats(v_cdr); + end if; + + v_cdr.customer_price = switch.customer_price_round(v_config, v_cdr.customer_price); + v_cdr.customer_price_no_vat = switch.customer_price_round(v_config, v_cdr.customer_price_no_vat); + v_cdr.vendor_price = switch.vendor_price_round(v_config, v_cdr.vendor_price); + + -- generate event to billing engine + perform event.billing_insert_event('cdr_full',v_cdr); + perform event.streaming_insert_event(v_cdr); + INSERT INTO cdr.cdr VALUES( v_cdr.*); + RETURN 0; +END; +$$; + +CREATE OR REPLACE FUNCTION switch.writecdr(i_is_master boolean, i_node_id integer, i_pop_id integer, i_routing_attempt integer, i_is_last_cdr boolean, i_lega_transport_protocol_id smallint, i_lega_local_ip character varying, i_lega_local_port integer, i_lega_remote_ip character varying, i_lega_remote_port integer, i_legb_transport_protocol_id smallint, i_legb_local_ip character varying, i_legb_local_port integer, i_legb_remote_ip character varying, i_legb_remote_port integer, i_legb_ruri character varying, i_legb_outbound_proxy character varying, i_time_data json, i_early_media_present boolean, i_legb_disconnect_code integer, i_legb_disconnect_reason character varying, i_disconnect_initiator integer, i_internal_disconnect_code integer, i_internal_disconnect_reason character varying, i_lega_disconnect_code integer, i_lega_disconnect_reason character varying, i_internal_disconnect_code_id smallint, i_orig_call_id character varying, i_term_call_id character varying, i_local_tag character varying, i_legb_local_tag character varying, i_msg_logger_path character varying, i_dump_level_id smallint, i_audio_recorded boolean, i_rtp_stats_data json, i_rtp_statistics json, i_global_tag character varying, i_resources character varying, i_active_resources json, i_failed_resource_type_id smallint, i_failed_resource_id bigint, i_dtmf_events json, i_versions json, i_is_redirected boolean, i_dynamic json, i_lega_request_headers json, i_legb_request_headers json, i_legb_reply_headers json, i_lega_identity json) RETURNS integer + LANGUAGE plpgsql SECURITY DEFINER COST 10 + AS $$ +DECLARE + v_cdr cdr.cdr%rowtype; + + v_time_data switch.time_data_ty; + v_version_data switch.versions_ty; + v_dynamic switch.dynamic_cdr_data_ty; + + v_nozerolen boolean; + v_config sys.config%rowtype; + + v_rtp_rx_stream_data rtp_statistics.rx_streams%rowtype; + v_rtp_tx_stream_data rtp_statistics.tx_streams%rowtype; + + v_lega_request_headers switch.lega_request_headers_ty; + v_legb_request_headers switch.legb_request_headers_ty; + v_legb_reply_headers switch.legb_reply_headers_ty; + v_lega_reason switch.reason_ty; + v_legb_reason switch.reason_ty; +BEGIN + -- raise warning 'type: % id: %', i_failed_resource_type_id, i_failed_resource_id; + -- RAISE warning 'DTMF: %', i_dtmf_events; + + v_time_data:=json_populate_record(null::switch.time_data_ty, i_time_data); + v_version_data:=json_populate_record(null::switch.versions_ty, i_versions); + v_dynamic:=json_populate_record(null::switch.dynamic_cdr_data_ty, i_dynamic); + + v_lega_request_headers = json_populate_record(null::switch.lega_request_headers_ty, i_lega_request_headers); + v_legb_request_headers = json_populate_record(null::switch.legb_request_headers_ty, i_legb_request_headers); + v_legb_reply_headers = json_populate_record(null::switch.legb_reply_headers_ty, i_legb_reply_headers); + + v_cdr.p_charge_info_in = v_lega_request_headers.p_charge_info; + + v_lega_reason = v_lega_request_headers.reason; + v_cdr.lega_q850_cause = v_lega_reason.q850_cause; + v_cdr.lega_q850_text = v_lega_reason.q850_text; + v_cdr.lega_q850_params = v_lega_reason.q850_params; + + v_legb_reason = v_legb_reply_headers.reason; + v_cdr.legb_q850_cause = v_legb_reason.q850_cause; + v_cdr.legb_q850_text = v_legb_reason.q850_text; + v_cdr.legb_q850_params = v_legb_reason.q850_params; + + v_cdr.diversion_in = array_to_string(v_lega_request_headers.diversion, ','); + v_cdr.pai_in = array_to_string(v_lega_request_headers.p_asserted_identity, ','); + v_cdr.ppi_in = v_lega_request_headers.p_preferred_identity; + v_cdr.privacy_in = array_to_string(v_lega_request_headers.privacy, ','); + v_cdr.rpid_in = array_to_string(v_lega_request_headers.remote_party_id, ','); + v_cdr.rpid_privacy_in = array_to_string(v_lega_request_headers.rpid_privacy, ','); + + v_cdr.diversion_out = array_to_string(v_legb_request_headers.diversion, ','); + v_cdr.pai_out = array_to_string(v_legb_request_headers.p_asserted_identity, ','); + v_cdr.ppi_out = v_legb_request_headers.p_preferred_identity; + v_cdr.privacy_out = array_to_string(v_legb_request_headers.privacy, ','); + v_cdr.rpid_out = array_to_string(v_legb_request_headers.remote_party_id, ','); + v_cdr.rpid_privacy_out = array_to_string(v_legb_request_headers.rpid_privacy, ','); + + v_cdr.lega_identity = i_lega_identity; + v_cdr.lega_ss_status_id = v_dynamic.lega_ss_status_id; + v_cdr.legb_ss_status_id = v_dynamic.legb_ss_status_id; + + v_cdr.metadata = v_dynamic.metadata::jsonb; + + v_cdr.core_version=v_version_data.core; + v_cdr.yeti_version=v_version_data.yeti; + v_cdr.lega_user_agent=v_version_data.aleg; + v_cdr.legb_user_agent=v_version_data.bleg; + + v_cdr.pop_id=i_pop_id; + v_cdr.node_id=i_node_id; + + v_cdr.src_name_in:=v_dynamic.src_name_in; + v_cdr.src_name_out:=v_dynamic.src_name_out; + + v_cdr.customer_id:=v_dynamic.customer_id; + v_cdr.customer_external_id:=v_dynamic.customer_external_id; + + v_cdr.customer_acc_id:=v_dynamic.customer_acc_id; + v_cdr.customer_account_check_balance=v_dynamic.customer_acc_check_balance; + v_cdr.customer_acc_external_id=v_dynamic.customer_acc_external_id; + v_cdr.customer_acc_vat:=v_dynamic.customer_acc_vat; + + v_cdr.customer_auth_id:=v_dynamic.customer_auth_id; + v_cdr.customer_auth_external_id:=v_dynamic.customer_auth_external_id; + v_cdr.customer_auth_external_type:=v_dynamic.customer_auth_external_type; + v_cdr.customer_auth_name:=v_dynamic.customer_auth_name; + + v_cdr.vendor_id:=v_dynamic.vendor_id; + v_cdr.vendor_external_id:=v_dynamic.vendor_external_id; + v_cdr.vendor_acc_id:=v_dynamic.vendor_acc_id; + v_cdr.vendor_acc_external_id:=v_dynamic.vendor_acc_external_id; + + v_cdr.package_counter_id = v_dynamic.package_counter_id; + v_cdr.destination_id:=v_dynamic.destination_id; + v_cdr.destination_prefix:=v_dynamic.destination_prefix; + v_cdr.dialpeer_id:=v_dynamic.dialpeer_id; + v_cdr.dialpeer_prefix:=v_dynamic.dialpeer_prefix; + + v_cdr.orig_gw_id:=v_dynamic.orig_gw_id; + v_cdr.orig_gw_external_id:=v_dynamic.orig_gw_external_id; + v_cdr.term_gw_id:=v_dynamic.term_gw_id; + v_cdr.term_gw_external_id:=v_dynamic.term_gw_external_id; + + v_cdr.routing_group_id:=v_dynamic.routing_group_id; + v_cdr.rateplan_id:=v_dynamic.rateplan_id; + + v_cdr.routing_attempt=i_routing_attempt; + v_cdr.is_last_cdr=i_is_last_cdr; + + v_cdr.destination_initial_rate:=v_dynamic.destination_initial_rate::numeric; + v_cdr.destination_next_rate:=v_dynamic.destination_next_rate::numeric; + v_cdr.destination_initial_interval:=v_dynamic.destination_initial_interval; + v_cdr.destination_next_interval:=v_dynamic.destination_next_interval; + v_cdr.destination_fee:=v_dynamic.destination_fee; + v_cdr.destination_rate_policy_id:=v_dynamic.destination_rate_policy_id; + v_cdr.destination_reverse_billing=v_dynamic.destination_reverse_billing; + + v_cdr.dialpeer_initial_rate:=v_dynamic.dialpeer_initial_rate::numeric; + v_cdr.dialpeer_next_rate:=v_dynamic.dialpeer_next_rate::numeric; + v_cdr.dialpeer_initial_interval:=v_dynamic.dialpeer_initial_interval; + v_cdr.dialpeer_next_interval:=v_dynamic.dialpeer_next_interval; + v_cdr.dialpeer_fee:=v_dynamic.dialpeer_fee; + v_cdr.dialpeer_reverse_billing=v_dynamic.dialpeer_reverse_billing; + + /* sockets addresses */ + v_cdr.sign_orig_transport_protocol_id = i_lega_transport_protocol_id; + v_cdr.sign_orig_ip = i_lega_remote_ip; + v_cdr.sign_orig_port = NULLIF(i_lega_remote_port,0); + v_cdr.sign_orig_local_ip = i_lega_local_ip; + v_cdr.sign_orig_local_port = NULLIF(i_lega_local_port,0); + + v_cdr.sign_term_transport_protocol_id = i_legb_transport_protocol_id; + v_cdr.sign_term_ip = i_legb_remote_ip; + v_cdr.sign_term_port = NULLIF(i_legb_remote_port,0); + v_cdr.sign_term_local_ip = i_legb_local_ip; + v_cdr.sign_term_local_port = NULLIF(i_legb_local_port,0); + + v_cdr.local_tag = i_local_tag; + v_cdr.legb_local_tag = i_legb_local_tag; + v_cdr.legb_ruri = i_legb_ruri; + v_cdr.legb_outbound_proxy = i_legb_outbound_proxy; + + v_cdr.is_redirected = i_is_redirected; + + /* Call time data */ + v_cdr.time_start = to_timestamp(v_time_data.time_start); + + select into strict v_config * from sys.config; + + if v_time_data.time_connect is not null then + v_cdr.time_connect = to_timestamp(v_time_data.time_connect); + v_cdr.duration = switch.duration_round(v_config, v_time_data.time_end-v_time_data.time_connect); -- rounding + v_nozerolen = true; + v_cdr.success = true; + else + v_cdr.time_connect = NULL; + v_cdr.duration = 0; + v_nozerolen = false; + v_cdr.success = false; + end if; + v_cdr.routing_delay = (v_time_data.leg_b_time-v_time_data.time_start)::real; + v_cdr.pdd = (coalesce(v_time_data.time_18x,v_time_data.time_connect)-v_time_data.time_start)::real; + v_cdr.rtt = (coalesce(v_time_data.time_1xx,v_time_data.time_18x,v_time_data.time_connect)-v_time_data.leg_b_time)::real; + v_cdr.early_media_present = i_early_media_present; + + v_cdr.time_end = to_timestamp(v_time_data.time_end); + + -- DC processing + v_cdr.legb_disconnect_code = i_legb_disconnect_code; + v_cdr.legb_disconnect_reason = i_legb_disconnect_reason; + v_cdr.disconnect_initiator_id = i_disconnect_initiator; + v_cdr.internal_disconnect_code_id = i_internal_disconnect_code_id; + v_cdr.internal_disconnect_code = i_internal_disconnect_code; + v_cdr.internal_disconnect_reason = i_internal_disconnect_reason; + v_cdr.lega_disconnect_code = i_lega_disconnect_code; + v_cdr.lega_disconnect_reason = i_lega_disconnect_reason; + + v_cdr.src_prefix_in = v_dynamic.src_prefix_in; + v_cdr.src_prefix_out = v_dynamic.src_prefix_out; + v_cdr.dst_prefix_in = v_dynamic.dst_prefix_in; + v_cdr.dst_prefix_out = v_dynamic.dst_prefix_out; + + v_cdr.orig_call_id = i_orig_call_id; + v_cdr.term_call_id = i_term_call_id; + + /* removed */ + --v_cdr.dump_file = i_msg_logger_path; + + v_cdr.dump_level_id = i_dump_level_id; + v_cdr.audio_recorded = i_audio_recorded; + + v_cdr.auth_orig_transport_protocol_id = v_dynamic.auth_orig_protocol_id; + v_cdr.auth_orig_ip = v_dynamic.auth_orig_ip; + v_cdr.auth_orig_ip = v_dynamic.auth_orig_ip; + v_cdr.auth_orig_port = v_dynamic.auth_orig_port; + + perform switch.write_rtp_statistics( + i_rtp_statistics, + i_pop_id, + i_node_id, + v_dynamic.orig_gw_id, + v_dynamic.orig_gw_external_id, + v_dynamic.term_gw_id, + v_dynamic.term_gw_external_id, + i_local_tag, + i_legb_local_tag + ); + + v_cdr.global_tag = i_global_tag; + + v_cdr.src_country_id = v_dynamic.src_country_id; + v_cdr.src_network_id = v_dynamic.src_network_id; + v_cdr.src_network_type_id = v_dynamic.src_network_type_id; + v_cdr.dst_country_id = v_dynamic.dst_country_id; + v_cdr.dst_network_id = v_dynamic.dst_network_id; + v_cdr.dst_network_type_id = v_dynamic.dst_network_type_id; + + v_cdr.dst_prefix_routing = v_dynamic.dst_prefix_routing; + v_cdr.src_prefix_routing = v_dynamic.src_prefix_routing; + v_cdr.routing_plan_id = v_dynamic.routing_plan_id; + v_cdr.lrn = v_dynamic.lrn; + v_cdr.lnp_database_id = v_dynamic.lnp_database_id; + + v_cdr.ruri_domain = v_dynamic.ruri_domain; + v_cdr.to_domain = v_dynamic.to_domain; + v_cdr.from_domain = v_dynamic.from_domain; + + v_cdr.src_area_id = v_dynamic.src_area_id; + v_cdr.dst_area_id = v_dynamic.dst_area_id; + v_cdr.routing_tag_ids = v_dynamic.routing_tag_ids; + + + v_cdr.id = nextval('cdr.cdr_id_seq'::regclass); + v_cdr.uuid = public.uuid_generate_v1(); + + v_cdr.failed_resource_type_id = i_failed_resource_type_id; + v_cdr.failed_resource_id = i_failed_resource_id; + + v_cdr = billing.bill_cdr(v_cdr); + + if not v_config.disable_realtime_statistics then + perform stats.update_rt_stats(v_cdr); + end if; + + v_cdr.customer_price = switch.customer_price_round(v_config, v_cdr.customer_price); + v_cdr.customer_price_no_vat = switch.customer_price_round(v_config, v_cdr.customer_price_no_vat); + v_cdr.vendor_price = switch.vendor_price_round(v_config, v_cdr.vendor_price); + + -- generate event to billing engine + perform event.billing_insert_event('cdr_full',v_cdr); + perform event.streaming_insert_event(v_cdr); + INSERT INTO cdr.cdr VALUES( v_cdr.*); + RETURN 0; +END; +$$; + } + + end + +end diff --git a/db/cdr_migrate/20241219145036_fix_writeauthlog_sp_types.rb b/db/cdr_migrate/20241219145036_fix_writeauthlog_sp_types.rb new file mode 100644 index 000000000..226119a2b --- /dev/null +++ b/db/cdr_migrate/20241219145036_fix_writeauthlog_sp_types.rb @@ -0,0 +1,144 @@ +class FixWriteauthlogSpTypes < ActiveRecord::Migration[7.0] + + def up + execute %q{ +alter table auth_log.auth_log + alter column transport_remote_ip type inet USING transport_remote_ip::inet, + alter column transport_local_ip type inet USING transport_local_ip::inet, + alter column origination_ip type inet USING origination_ip::inet; + +alter type switch.lega_request_headers_ty + add attribute x_yeti_auth varchar, + add attribute x_orig_ip varchar, + add attribute x_orig_port varchar, + add attribute x_orig_proto varchar; + +CREATE FUNCTION switch.write_auth_log( +i_is_master boolean, +i_node_id integer, +i_pop_id integer, +i_request_time double precision, +i_transport_proto_id smallint, +i_transport_remote_ip inet, +i_transport_remote_port integer, +i_transport_local_ip inet, +i_transport_local_port integer, +i_username character varying, +i_realm character varying, +i_method character varying, +i_ruri character varying, +i_from_uri character varying, +i_to_uri character varying, +i_call_id character varying, +i_success boolean, +i_code smallint, +i_reason character varying, +i_internal_reason character varying, +i_nonce character varying, +i_response character varying, +i_gateway_id integer, +i_lega_request_headers json +) RETURNS integer + LANGUAGE plpgsql SECURITY DEFINER COST 10 + AS $$ +DECLARE + v_lega_request_headers switch.lega_request_headers_ty; + v_log auth_log.auth_log%rowtype; +BEGIN + + v_log.node_id = i_node_id; + v_log.pop_id = i_pop_id; + v_log.request_time = to_timestamp(i_request_time); + v_log.transport_proto_id = i_transport_proto_id; + v_log.transport_remote_ip = i_transport_remote_ip; + v_log.transport_remote_port = i_transport_remote_port; + v_log.transport_local_ip = i_transport_local_ip; + v_log.transport_local_port = i_transport_local_port; + + v_log.username = i_username; + v_log.realm = i_realm; + v_log.request_method = i_method; + v_log.ruri = i_ruri; + v_log.from_uri = i_from_uri; + v_log.to_uri = i_to_uri; + v_log.call_id = i_call_id; + v_log.success = i_success; + v_log.code = i_code; + v_log.reason = i_reason; + v_log.internal_reason = i_internal_reason; + v_log.nonce = i_nonce; + v_log.response = i_response; + v_log.gateway_id = i_gateway_id; + + v_lega_request_headers = json_populate_record(null::switch.lega_request_headers_ty, i_lega_request_headers); + + v_log.origination_ip = v_lega_request_headers.x_orig_ip::inet; + v_log.origination_port = v_lega_request_headers.x_orig_port::integer; + v_log.origination_proto_id = v_lega_request_headers.x_orig_proto::smallint; + + v_log.x_yeti_auth = v_lega_request_headers.x_yeti_auth; + v_log.diversion = array_to_string(v_lega_request_headers.diversion, ','); + v_log.pai = array_to_string(v_lega_request_headers.p_asserted_identity, ','); + v_log.ppi = v_lega_request_headers.p_preferred_identity; + v_log.privacy = array_to_string(v_lega_request_headers.privacy, ','); + v_log.rpid = array_to_string(v_lega_request_headers.remote_party_id, ','); + v_log.rpid_privacy = array_to_string(v_lega_request_headers.rpid_privacy, ','); + + v_log.id = nextval('auth_log.auth_log_id_seq'); + + insert into auth_log.auth_log values(v_log.*); + + RETURN 0; +END; +$$; + + + } + end + + def down + execute %q{ + + alter table auth_log.auth_log + alter column transport_remote_ip type varchar USING transport_remote_ip::varchar, + alter column transport_local_ip type varchar USING transport_local_ip::varchar, + alter column origination_ip type varchar USING origination_ip::varchar; + + alter type switch.lega_request_headers_ty + drop attribute x_yeti_auth, + drop attribute x_orig_ip, + drop attribute x_orig_port, + drop attribute x_orig_proto; + + +DROP FUNCTION switch.write_auth_log( +i_is_master boolean, +i_node_id integer, +i_pop_id integer, +i_request_time double precision, +i_transport_proto_id smallint, +i_transport_remote_ip inet, +i_transport_remote_port integer, +i_transport_local_ip inet, +i_transport_local_port integer, +i_username character varying, +i_realm character varying, +i_method character varying, +i_ruri character varying, +i_from_uri character varying, +i_to_uri character varying, +i_call_id character varying, +i_success boolean, +i_code smallint, +i_reason character varying, +i_internal_reason character varying, +i_nonce character varying, +i_response character varying, +i_gateway_id integer, +i_lega_request_headers json +); + + } + end + +end diff --git a/db/cdr_structure.sql b/db/cdr_structure.sql index ded89c237..5f84e0ee2 100644 --- a/db/cdr_structure.sql +++ b/db/cdr_structure.sql @@ -325,7 +325,11 @@ CREATE TYPE switch.lega_request_headers_ty AS ( p_preferred_identity character varying, privacy character varying[], remote_party_id character varying[], - rpid_privacy character varying[] + rpid_privacy character varying[], + x_yeti_auth character varying, + x_orig_ip character varying, + x_orig_port character varying, + x_orig_proto character varying ); @@ -974,6 +978,65 @@ CREATE FUNCTION switch.vendor_price_round(i_config sys.config, i_amount numeric) $$; +-- +-- Name: write_auth_log(boolean, integer, integer, double precision, smallint, inet, integer, inet, integer, character varying, character varying, character varying, character varying, character varying, character varying, character varying, boolean, smallint, character varying, character varying, character varying, character varying, integer, json); Type: FUNCTION; Schema: switch; Owner: - +-- + +CREATE FUNCTION switch.write_auth_log(i_is_master boolean, i_node_id integer, i_pop_id integer, i_request_time double precision, i_transport_proto_id smallint, i_transport_remote_ip inet, i_transport_remote_port integer, i_transport_local_ip inet, i_transport_local_port integer, i_username character varying, i_realm character varying, i_method character varying, i_ruri character varying, i_from_uri character varying, i_to_uri character varying, i_call_id character varying, i_success boolean, i_code smallint, i_reason character varying, i_internal_reason character varying, i_nonce character varying, i_response character varying, i_gateway_id integer, i_lega_request_headers json) RETURNS integer + LANGUAGE plpgsql SECURITY DEFINER COST 10 + AS $$ +DECLARE + v_lega_request_headers switch.lega_request_headers_ty; + v_log auth_log.auth_log%rowtype; +BEGIN + + v_log.node_id = i_node_id; + v_log.pop_id = i_pop_id; + v_log.request_time = to_timestamp(i_request_time); + v_log.transport_proto_id = i_transport_proto_id; + v_log.transport_remote_ip = i_transport_remote_ip; + v_log.transport_remote_port = i_transport_remote_port; + v_log.transport_local_ip = i_transport_local_ip; + v_log.transport_local_port = i_transport_local_port; + + v_log.username = i_username; + v_log.realm = i_realm; + v_log.request_method = i_method; + v_log.ruri = i_ruri; + v_log.from_uri = i_from_uri; + v_log.to_uri = i_to_uri; + v_log.call_id = i_call_id; + v_log.success = i_success; + v_log.code = i_code; + v_log.reason = i_reason; + v_log.internal_reason = i_internal_reason; + v_log.nonce = i_nonce; + v_log.response = i_response; + v_log.gateway_id = i_gateway_id; + + v_lega_request_headers = json_populate_record(null::switch.lega_request_headers_ty, i_lega_request_headers); + + v_log.origination_ip = v_lega_request_headers.x_orig_ip::inet; + v_log.origination_port = v_lega_request_headers.x_orig_port::integer; + v_log.origination_proto_id = v_lega_request_headers.x_orig_proto::smallint; + + v_log.x_yeti_auth = v_lega_request_headers.x_yeti_auth; + v_log.diversion = array_to_string(v_lega_request_headers.diversion, ','); + v_log.pai = array_to_string(v_lega_request_headers.p_asserted_identity, ','); + v_log.ppi = v_lega_request_headers.p_preferred_identity; + v_log.privacy = array_to_string(v_lega_request_headers.privacy, ','); + v_log.rpid = array_to_string(v_lega_request_headers.remote_party_id, ','); + v_log.rpid_privacy = array_to_string(v_lega_request_headers.rpid_privacy, ','); + + v_log.id = nextval('auth_log.auth_log_id_seq'); + + insert into auth_log.auth_log values(v_log.*); + + RETURN 0; +END; +$$; + + -- -- Name: write_auth_log(boolean, integer, integer, double precision, smallint, character varying, integer, character varying, integer, character varying, character varying, character varying, character varying, character varying, character varying, character varying, boolean, smallint, character varying, character varying, character varying, character varying, integer, character varying, character varying, character varying, integer, smallint, character varying, character varying, character varying, character varying, character varying); Type: FUNCTION; Schema: switch; Owner: - -- @@ -1145,10 +1208,10 @@ $$; -- --- Name: writecdr(boolean, integer, integer, integer, boolean, smallint, character varying, integer, character varying, integer, smallint, character varying, integer, character varying, integer, character varying, character varying, json, boolean, integer, character varying, integer, integer, character varying, integer, character varying, character varying, character varying, character varying, character varying, character varying, smallint, boolean, json, json, character varying, character varying, json, smallint, bigint, json, json, boolean, json, json, json, json); Type: FUNCTION; Schema: switch; Owner: - +-- Name: writecdr(boolean, integer, integer, integer, boolean, smallint, inet, integer, inet, integer, smallint, inet, integer, inet, integer, character varying, character varying, json, boolean, integer, character varying, integer, integer, character varying, integer, character varying, character varying, character varying, character varying, character varying, character varying, smallint, boolean, json, json, character varying, character varying, json, smallint, bigint, json, json, boolean, json, json, json, json); Type: FUNCTION; Schema: switch; Owner: - -- -CREATE FUNCTION switch.writecdr(i_is_master boolean, i_node_id integer, i_pop_id integer, i_routing_attempt integer, i_is_last_cdr boolean, i_lega_transport_protocol_id smallint, i_lega_local_ip character varying, i_lega_local_port integer, i_lega_remote_ip character varying, i_lega_remote_port integer, i_legb_transport_protocol_id smallint, i_legb_local_ip character varying, i_legb_local_port integer, i_legb_remote_ip character varying, i_legb_remote_port integer, i_legb_ruri character varying, i_legb_outbound_proxy character varying, i_time_data json, i_early_media_present boolean, i_legb_disconnect_code integer, i_legb_disconnect_reason character varying, i_disconnect_initiator integer, i_internal_disconnect_code integer, i_internal_disconnect_reason character varying, i_lega_disconnect_code integer, i_lega_disconnect_reason character varying, i_orig_call_id character varying, i_term_call_id character varying, i_local_tag character varying, i_legb_local_tag character varying, i_msg_logger_path character varying, i_dump_level_id smallint, i_audio_recorded boolean, i_rtp_stats_data json, i_rtp_statistics json, i_global_tag character varying, i_resources character varying, i_active_resources json, i_failed_resource_type_id smallint, i_failed_resource_id bigint, i_dtmf_events json, i_versions json, i_is_redirected boolean, i_dynamic json, i_lega_headers json, i_legb_headers json, i_lega_identity json) RETURNS integer +CREATE FUNCTION switch.writecdr(i_is_master boolean, i_node_id integer, i_pop_id integer, i_routing_attempt integer, i_is_last_cdr boolean, i_lega_transport_protocol_id smallint, i_lega_local_ip inet, i_lega_local_port integer, i_lega_remote_ip inet, i_lega_remote_port integer, i_legb_transport_protocol_id smallint, i_legb_local_ip inet, i_legb_local_port integer, i_legb_remote_ip inet, i_legb_remote_port integer, i_legb_ruri character varying, i_legb_outbound_proxy character varying, i_time_data json, i_early_media_present boolean, i_legb_disconnect_code integer, i_legb_disconnect_reason character varying, i_disconnect_initiator integer, i_internal_disconnect_code integer, i_internal_disconnect_reason character varying, i_lega_disconnect_code integer, i_lega_disconnect_reason character varying, i_orig_call_id character varying, i_term_call_id character varying, i_local_tag character varying, i_legb_local_tag character varying, i_msg_logger_path character varying, i_dump_level_id smallint, i_audio_recorded boolean, i_rtp_stats_data json, i_rtp_statistics json, i_global_tag character varying, i_resources character varying, i_active_resources json, i_failed_resource_type_id smallint, i_failed_resource_id bigint, i_dtmf_events json, i_versions json, i_is_redirected boolean, i_dynamic json, i_lega_headers json, i_legb_headers json, i_lega_identity json) RETURNS integer LANGUAGE plpgsql SECURITY DEFINER COST 10 AS $$ DECLARE @@ -1262,17 +1325,17 @@ BEGIN v_cdr.dialpeer_reverse_billing=v_dynamic.dialpeer_reverse_billing; /* sockets addresses */ - v_cdr.sign_orig_transport_protocol_id=i_lega_transport_protocol_id; - v_cdr.sign_orig_ip=i_lega_remote_ip; - v_cdr.sign_orig_port=NULLIF(i_lega_remote_port,0); - v_cdr.sign_orig_local_ip=i_lega_local_ip; - v_cdr.sign_orig_local_port=NULLIF(i_lega_local_port,0); - - v_cdr.sign_term_transport_protocol_id=i_legb_transport_protocol_id; - v_cdr.sign_term_ip=i_legb_remote_ip; - v_cdr.sign_term_port=NULLIF(i_legb_remote_port,0); - v_cdr.sign_term_local_ip=i_legb_local_ip; - v_cdr.sign_term_local_port=NULLIF(i_legb_local_port,0); + v_cdr.sign_orig_transport_protocol_id = i_lega_transport_protocol_id; + v_cdr.sign_orig_ip = host(i_lega_remote_ip); + v_cdr.sign_orig_port = NULLIF(i_lega_remote_port,0); + v_cdr.sign_orig_local_ip = host(i_lega_local_ip); + v_cdr.sign_orig_local_port = NULLIF(i_lega_local_port,0); + + v_cdr.sign_term_transport_protocol_id = i_legb_transport_protocol_id; + v_cdr.sign_term_ip = host(i_legb_remote_ip); + v_cdr.sign_term_port = NULLIF(i_legb_remote_port,0); + v_cdr.sign_term_local_ip = host(i_legb_local_ip); + v_cdr.sign_term_local_port = NULLIF(i_legb_local_port,0); v_cdr.local_tag=i_local_tag; v_cdr.legb_local_tag=i_legb_local_tag; @@ -1402,10 +1465,10 @@ $$; -- --- Name: writecdr(boolean, integer, integer, integer, boolean, smallint, character varying, integer, character varying, integer, smallint, character varying, integer, character varying, integer, character varying, character varying, json, boolean, integer, character varying, integer, integer, character varying, integer, character varying, smallint, character varying, character varying, character varying, character varying, character varying, smallint, boolean, json, json, character varying, character varying, json, smallint, bigint, json, json, boolean, json, json, json, json); Type: FUNCTION; Schema: switch; Owner: - +-- Name: writecdr(boolean, integer, integer, integer, boolean, smallint, inet, integer, inet, integer, smallint, inet, integer, inet, integer, character varying, character varying, json, boolean, integer, character varying, integer, integer, character varying, integer, character varying, smallint, character varying, character varying, character varying, character varying, character varying, smallint, boolean, json, json, character varying, character varying, json, smallint, bigint, json, json, boolean, json, json, json, json); Type: FUNCTION; Schema: switch; Owner: - -- -CREATE FUNCTION switch.writecdr(i_is_master boolean, i_node_id integer, i_pop_id integer, i_routing_attempt integer, i_is_last_cdr boolean, i_lega_transport_protocol_id smallint, i_lega_local_ip character varying, i_lega_local_port integer, i_lega_remote_ip character varying, i_lega_remote_port integer, i_legb_transport_protocol_id smallint, i_legb_local_ip character varying, i_legb_local_port integer, i_legb_remote_ip character varying, i_legb_remote_port integer, i_legb_ruri character varying, i_legb_outbound_proxy character varying, i_time_data json, i_early_media_present boolean, i_legb_disconnect_code integer, i_legb_disconnect_reason character varying, i_disconnect_initiator integer, i_internal_disconnect_code integer, i_internal_disconnect_reason character varying, i_lega_disconnect_code integer, i_lega_disconnect_reason character varying, i_internal_disconnect_code_id smallint, i_orig_call_id character varying, i_term_call_id character varying, i_local_tag character varying, i_legb_local_tag character varying, i_msg_logger_path character varying, i_dump_level_id smallint, i_audio_recorded boolean, i_rtp_stats_data json, i_rtp_statistics json, i_global_tag character varying, i_resources character varying, i_active_resources json, i_failed_resource_type_id smallint, i_failed_resource_id bigint, i_dtmf_events json, i_versions json, i_is_redirected boolean, i_dynamic json, i_lega_headers json, i_legb_headers json, i_lega_identity json) RETURNS integer +CREATE FUNCTION switch.writecdr(i_is_master boolean, i_node_id integer, i_pop_id integer, i_routing_attempt integer, i_is_last_cdr boolean, i_lega_transport_protocol_id smallint, i_lega_local_ip inet, i_lega_local_port integer, i_lega_remote_ip inet, i_lega_remote_port integer, i_legb_transport_protocol_id smallint, i_legb_local_ip inet, i_legb_local_port integer, i_legb_remote_ip inet, i_legb_remote_port integer, i_legb_ruri character varying, i_legb_outbound_proxy character varying, i_time_data json, i_early_media_present boolean, i_legb_disconnect_code integer, i_legb_disconnect_reason character varying, i_disconnect_initiator integer, i_internal_disconnect_code integer, i_internal_disconnect_reason character varying, i_lega_disconnect_code integer, i_lega_disconnect_reason character varying, i_internal_disconnect_code_id smallint, i_orig_call_id character varying, i_term_call_id character varying, i_local_tag character varying, i_legb_local_tag character varying, i_msg_logger_path character varying, i_dump_level_id smallint, i_audio_recorded boolean, i_rtp_stats_data json, i_rtp_statistics json, i_global_tag character varying, i_resources character varying, i_active_resources json, i_failed_resource_type_id smallint, i_failed_resource_id bigint, i_dtmf_events json, i_versions json, i_is_redirected boolean, i_dynamic json, i_lega_headers json, i_legb_headers json, i_lega_identity json) RETURNS integer LANGUAGE plpgsql SECURITY DEFINER COST 10 AS $$ DECLARE @@ -1519,17 +1582,17 @@ BEGIN v_cdr.dialpeer_reverse_billing=v_dynamic.dialpeer_reverse_billing; /* sockets addresses */ - v_cdr.sign_orig_transport_protocol_id=i_lega_transport_protocol_id; - v_cdr.sign_orig_ip=i_lega_remote_ip; - v_cdr.sign_orig_port=NULLIF(i_lega_remote_port,0); - v_cdr.sign_orig_local_ip=i_lega_local_ip; - v_cdr.sign_orig_local_port=NULLIF(i_lega_local_port,0); - - v_cdr.sign_term_transport_protocol_id=i_legb_transport_protocol_id; - v_cdr.sign_term_ip=i_legb_remote_ip; - v_cdr.sign_term_port=NULLIF(i_legb_remote_port,0); - v_cdr.sign_term_local_ip=i_legb_local_ip; - v_cdr.sign_term_local_port=NULLIF(i_legb_local_port,0); + v_cdr.sign_orig_transport_protocol_id = i_lega_transport_protocol_id; + v_cdr.sign_orig_ip = host(i_lega_remote_ip); + v_cdr.sign_orig_port = NULLIF(i_lega_remote_port,0); + v_cdr.sign_orig_local_ip = host(i_lega_local_ip); + v_cdr.sign_orig_local_port = NULLIF(i_lega_local_port,0); + + v_cdr.sign_term_transport_protocol_id = i_legb_transport_protocol_id; + v_cdr.sign_term_ip = host(i_legb_remote_ip); + v_cdr.sign_term_port = NULLIF(i_legb_remote_port,0); + v_cdr.sign_term_local_ip = host(i_legb_local_ip); + v_cdr.sign_term_local_port = NULLIF(i_legb_local_port,0); v_cdr.local_tag=i_local_tag; v_cdr.legb_local_tag=i_legb_local_tag; @@ -1660,10 +1723,10 @@ $$; -- --- Name: writecdr(boolean, integer, integer, integer, boolean, smallint, character varying, integer, character varying, integer, smallint, character varying, integer, character varying, integer, character varying, character varying, json, boolean, integer, character varying, integer, integer, character varying, integer, character varying, smallint, character varying, character varying, character varying, character varying, character varying, smallint, boolean, json, json, character varying, character varying, json, smallint, bigint, json, json, boolean, json, json, json, json, json); Type: FUNCTION; Schema: switch; Owner: - +-- Name: writecdr(boolean, integer, integer, integer, boolean, smallint, inet, integer, inet, integer, smallint, inet, integer, inet, integer, character varying, character varying, json, boolean, integer, character varying, integer, integer, character varying, integer, character varying, smallint, character varying, character varying, character varying, character varying, character varying, smallint, boolean, json, json, character varying, character varying, json, smallint, bigint, json, json, boolean, json, json, json, json, json); Type: FUNCTION; Schema: switch; Owner: - -- -CREATE FUNCTION switch.writecdr(i_is_master boolean, i_node_id integer, i_pop_id integer, i_routing_attempt integer, i_is_last_cdr boolean, i_lega_transport_protocol_id smallint, i_lega_local_ip character varying, i_lega_local_port integer, i_lega_remote_ip character varying, i_lega_remote_port integer, i_legb_transport_protocol_id smallint, i_legb_local_ip character varying, i_legb_local_port integer, i_legb_remote_ip character varying, i_legb_remote_port integer, i_legb_ruri character varying, i_legb_outbound_proxy character varying, i_time_data json, i_early_media_present boolean, i_legb_disconnect_code integer, i_legb_disconnect_reason character varying, i_disconnect_initiator integer, i_internal_disconnect_code integer, i_internal_disconnect_reason character varying, i_lega_disconnect_code integer, i_lega_disconnect_reason character varying, i_internal_disconnect_code_id smallint, i_orig_call_id character varying, i_term_call_id character varying, i_local_tag character varying, i_legb_local_tag character varying, i_msg_logger_path character varying, i_dump_level_id smallint, i_audio_recorded boolean, i_rtp_stats_data json, i_rtp_statistics json, i_global_tag character varying, i_resources character varying, i_active_resources json, i_failed_resource_type_id smallint, i_failed_resource_id bigint, i_dtmf_events json, i_versions json, i_is_redirected boolean, i_dynamic json, i_lega_request_headers json, i_legb_request_headers json, i_legb_reply_headers json, i_lega_identity json) RETURNS integer +CREATE FUNCTION switch.writecdr(i_is_master boolean, i_node_id integer, i_pop_id integer, i_routing_attempt integer, i_is_last_cdr boolean, i_lega_transport_protocol_id smallint, i_lega_local_ip inet, i_lega_local_port integer, i_lega_remote_ip inet, i_lega_remote_port integer, i_legb_transport_protocol_id smallint, i_legb_local_ip inet, i_legb_local_port integer, i_legb_remote_ip inet, i_legb_remote_port integer, i_legb_ruri character varying, i_legb_outbound_proxy character varying, i_time_data json, i_early_media_present boolean, i_legb_disconnect_code integer, i_legb_disconnect_reason character varying, i_disconnect_initiator integer, i_internal_disconnect_code integer, i_internal_disconnect_reason character varying, i_lega_disconnect_code integer, i_lega_disconnect_reason character varying, i_internal_disconnect_code_id smallint, i_orig_call_id character varying, i_term_call_id character varying, i_local_tag character varying, i_legb_local_tag character varying, i_msg_logger_path character varying, i_dump_level_id smallint, i_audio_recorded boolean, i_rtp_stats_data json, i_rtp_statistics json, i_global_tag character varying, i_resources character varying, i_active_resources json, i_failed_resource_type_id smallint, i_failed_resource_id bigint, i_dtmf_events json, i_versions json, i_is_redirected boolean, i_dynamic json, i_lega_request_headers json, i_legb_request_headers json, i_legb_reply_headers json, i_lega_identity json) RETURNS integer LANGUAGE plpgsql SECURITY DEFINER COST 10 AS $$ DECLARE @@ -1791,15 +1854,15 @@ BEGIN /* sockets addresses */ v_cdr.sign_orig_transport_protocol_id = i_lega_transport_protocol_id; - v_cdr.sign_orig_ip = i_lega_remote_ip; + v_cdr.sign_orig_ip = host(i_lega_remote_ip); v_cdr.sign_orig_port = NULLIF(i_lega_remote_port,0); - v_cdr.sign_orig_local_ip = i_lega_local_ip; + v_cdr.sign_orig_local_ip = host(i_lega_local_ip); v_cdr.sign_orig_local_port = NULLIF(i_lega_local_port,0); v_cdr.sign_term_transport_protocol_id = i_legb_transport_protocol_id; - v_cdr.sign_term_ip = i_legb_remote_ip; + v_cdr.sign_term_ip = host(i_legb_remote_ip); v_cdr.sign_term_port = NULLIF(i_legb_remote_port,0); - v_cdr.sign_term_local_ip = i_legb_local_ip; + v_cdr.sign_term_local_ip = host(i_legb_local_ip); v_cdr.sign_term_local_port = NULLIF(i_legb_local_port,0); v_cdr.local_tag = i_local_tag; @@ -1964,11 +2027,11 @@ CREATE TABLE auth_log.auth_log ( pop_id smallint, request_time timestamp with time zone NOT NULL, transport_proto_id smallint, - transport_remote_ip character varying, + transport_remote_ip inet, transport_remote_port integer, - transport_local_ip character varying, + transport_local_ip inet, transport_local_port integer, - origination_ip character varying, + origination_ip inet, origination_port integer, origination_proto_id smallint, username character varying, @@ -4711,6 +4774,8 @@ INSERT INTO "public"."schema_migrations" (version) VALUES ('20241006113650'), ('20241006123022'), ('20241216143200'), -('20241217212213'); +('20241217212213'), +('20241219142219'), +('20241219145036'); diff --git a/spec/controllers/api/rest/admin/auth_logs_controller_spec.rb b/spec/controllers/api/rest/admin/auth_logs_controller_spec.rb index 44d453b8f..050fa14aa 100644 --- a/spec/controllers/api/rest/admin/auth_logs_controller_spec.rb +++ b/spec/controllers/api/rest/admin/auth_logs_controller_spec.rb @@ -100,13 +100,13 @@ it_behaves_like :jsonapi_filters_by_number_field, :code, type: :number it_behaves_like :jsonapi_filters_by_string_field, :reason it_behaves_like :jsonapi_filters_by_string_field, :internal_reason - it_behaves_like :jsonapi_filters_by_string_field, :origination_ip + it_behaves_like :jsonapi_filters_by_inet_field, :origination_ip it_behaves_like :jsonapi_filters_by_number_field, :origination_port, type: :number it_behaves_like :jsonapi_filters_by_number_field, :origination_proto_id, type: :number it_behaves_like :jsonapi_filters_by_number_field, :transport_proto_id, type: :number - it_behaves_like :jsonapi_filters_by_string_field, :transport_remote_ip + it_behaves_like :jsonapi_filters_by_inet_field, :transport_remote_ip it_behaves_like :jsonapi_filters_by_number_field, :transport_remote_port, type: :number - it_behaves_like :jsonapi_filters_by_string_field, :transport_local_ip + it_behaves_like :jsonapi_filters_by_inet_field, :transport_local_ip it_behaves_like :jsonapi_filters_by_number_field, :transport_local_port, type: :number it_behaves_like :jsonapi_filters_by_number_field, :pop_id, type: :number it_behaves_like :jsonapi_filters_by_number_field, :node_id, type: :number diff --git a/spec/factories/cdr/auth_logs.rb b/spec/factories/cdr/auth_logs.rb index d8e32ca1b..f9bc71ea3 100644 --- a/spec/factories/cdr/auth_logs.rb +++ b/spec/factories/cdr/auth_logs.rb @@ -10,7 +10,7 @@ # from_uri :string # internal_reason :string # nonce :string -# origination_ip :string +# origination_ip :inet # origination_port :integer(4) # pai :string # ppi :string @@ -25,9 +25,9 @@ # ruri :string # success :boolean # to_uri :string -# transport_local_ip :string +# transport_local_ip :inet # transport_local_port :integer(4) -# transport_remote_ip :string +# transport_remote_ip :inet # transport_remote_port :integer(4) # username :string # x_yeti_auth :string diff --git a/spec/models/cdr/auth_log_spec.rb b/spec/models/cdr/auth_log_spec.rb index 478d091e4..8a025b79d 100644 --- a/spec/models/cdr/auth_log_spec.rb +++ b/spec/models/cdr/auth_log_spec.rb @@ -10,7 +10,7 @@ # from_uri :string # internal_reason :string # nonce :string -# origination_ip :string +# origination_ip :inet # origination_port :integer(4) # pai :string # ppi :string @@ -25,9 +25,9 @@ # ruri :string # success :boolean # to_uri :string -# transport_local_ip :string +# transport_local_ip :inet # transport_local_port :integer(4) -# transport_remote_ip :string +# transport_remote_ip :inet # transport_remote_port :integer(4) # username :string # x_yeti_auth :string diff --git a/spec/sql/switch/auth_log_lega_headers_spec.rb b/spec/sql/switch/auth_log_lega_headers_spec.rb new file mode 100644 index 000000000..7fa23c9c9 --- /dev/null +++ b/spec/sql/switch/auth_log_lega_headers_spec.rb @@ -0,0 +1,128 @@ +# frozen_string_literal: true + +RSpec.describe 'switch.write_auth_log()' do + subject do + SqlCaller::Cdr.execute(%(SELECT * FROM switch.write_auth_log( + #{is_master}::boolean, + #{node_id}::integer, + #{pop_id}::integer, + '#{request_time.to_f}'::double precision, + #{transport_proto_id}::smallint, + '#{transport_remote_ip}'::inet, + #{transport_remote_port}::integer, + '#{transport_local_ip}'::inet, + #{transport_local_port}::integer, + '#{username}'::varchar, + '#{realm}'::varchar, + '#{i_method}'::varchar, + '#{ruri}'::varchar, + '#{from_uri}'::varchar, + '#{to_uri}'::varchar, + '#{call_id}'::varchar, + #{success}::boolean, + #{code}::smallint, + '#{reason}'::varchar, + '#{internal_reason}'::varchar, + '#{nonce}'::varchar, + '#{response}'::varchar, + #{gateway_id}::integer, + '#{lega_request_headers}'::json + );)) + end + + let(:is_master) { true } + let(:node_id) { 1 } + let(:pop_id) { 1 } + + let(:request_time) { 10.seconds.ago } + let(:transport_proto_id) { 1 } + let(:transport_remote_ip) { '1.1.1.1' } + let(:transport_remote_port) { 5060 } + let(:transport_local_ip) { '2.2.2.2' } + let(:transport_local_port) { 6050 } + let(:username) { 'user' } + let(:realm) { 'auth-realm' } + let(:i_method) { 'REGISTER' } + let(:ruri) { 'sip:ruri@domain' } + let(:from_uri) { '"from" ' } + let(:to_uri) { '"to" ' } + let(:call_id) { 'fAtogKNE7RNF5ba7rm9A8Do1hFo1xOX7ppWo72vDV4gErsyBacSOhtI6ORKKedCJJ' } + let(:success) { true } + let(:code) { 200 } + let(:reason) { 'OK' } + let(:internal_reason) { 'OK' } + let(:nonce) { 'VoYxUGHsXYtUTsc5fxFbYbt6adrwaZt45RJGzqLrPJNigemF7JuedrUJ6QwwieP1h' } + let(:response) { 'yYmjZN2SKy92dAshXSeZWbfHPw9wKqMm5FitX5BmeGEtF7k8hiKfmgjgBohxv44bs' } + let(:gateway_id) { 100 } + + let(:lega_request_headers) { + { + x_yeti_auth: x_yeti_auth, + diversion: diversion, + x_orig_ip: origination_ip, + x_orig_port: origination_port, + x_orig_proto: origination_proto_id, + p_asserted_identity: pai, + p_preferred_identity: ppi, + privacy: privacy, + remote_party_id: rpid, + rpid_privacy: rpid_privacy + }.to_json + } + + let(:x_yeti_auth) { '4v96qlIguGsxVQGg' } + let(:diversion) { ['"Diversion1" ', '"Diversion2" '] } + let(:origination_ip) { '8.8.8.8' } + let(:origination_port) { 5070 } + let(:origination_proto_id) { 2 } + let(:pai) { ['"Pai1" ', '"Pai2" '] } + let(:ppi) { '"ppi" ' } + let(:privacy) { %w[id critical] } + let(:rpid) { ['"rpid1" ', '"rpid2" '] } + let(:rpid_privacy) { %w[full not-full] } + + context 'Create' do + it 'creates Auth log' do + expect { subject }.to change { Cdr::AuthLog.count }.by(1) + end + + it 'creates Auth log with expected attributes' do + subject + log = Cdr::AuthLog.last + expect(log).to have_attributes( + id: kind_of(Integer), + node_id: node_id, + pop_id: pop_id, + request_time: be_within(1.second).of(request_time), + transport_proto_id: transport_proto_id, + transport_remote_ip: transport_remote_ip, + transport_remote_port: transport_remote_port, + transport_local_ip: transport_local_ip, + transport_local_port: transport_local_port, + username: username, + realm: realm, + ruri: ruri, + from_uri: from_uri, + to_uri: to_uri, + call_id: call_id, + success: success, + code: code, + reason: reason, + internal_reason: internal_reason, + nonce: nonce, + response: response, + gateway_id: gateway_id, + x_yeti_auth: x_yeti_auth, + diversion: diversion.join(','), + origination_ip: origination_ip, + origination_port: origination_port, + origination_proto_id: origination_proto_id, + pai: pai.join(','), + ppi: ppi, + privacy: privacy.join(','), + rpid: rpid.join(','), + rpid_privacy: rpid_privacy.join(',') + ) + end + end +end diff --git a/spec/sql/switch/auth_log_spec.rb b/spec/sql/switch/auth_log_spec.rb new file mode 100644 index 000000000..7e7798283 --- /dev/null +++ b/spec/sql/switch/auth_log_spec.rb @@ -0,0 +1,121 @@ +# frozen_string_literal: true + +RSpec.describe 'switch.write_auth_log()' do + subject do + SqlCaller::Cdr.execute(%(SELECT * FROM switch.write_auth_log( + #{is_master}::boolean, + #{node_id}::integer, + #{pop_id}::integer, + '#{request_time.to_f}'::double precision, + #{transport_proto_id}::smallint, + '#{transport_remote_ip}'::varchar, + #{transport_remote_port}::integer, + '#{transport_local_ip}'::varchar, + #{transport_local_port}::integer, + '#{username}'::varchar, + '#{realm}'::varchar, + '#{i_method}'::varchar, + '#{ruri}'::varchar, + '#{from_uri}'::varchar, + '#{to_uri}'::varchar, + '#{call_id}'::varchar, + #{success}::boolean, + #{code}::smallint, + '#{reason}'::varchar, + '#{internal_reason}'::varchar, + '#{nonce}'::varchar, + '#{response}'::varchar, + #{gateway_id}::integer, + '#{x_yeti_auth}'::varchar, + '#{diversion}'::varchar, + '#{origination_ip}'::varchar, + #{origination_port}::integer, + #{origination_proto_id}::smallint, + '#{pai}'::varchar, + '#{ppi}'::varchar, + '#{privacy}'::varchar, + '#{rpid}'::varchar, + '#{rpid_privacy}'::varchar + );)) + end + + let(:is_master) { true } + let(:node_id) { 1 } + let(:pop_id) { 1 } + + let(:request_time) { 10.seconds.ago } + let(:transport_proto_id) { 1 } + let(:transport_remote_ip) { '1.1.1.1' } + let(:transport_remote_port) { 5060 } + let(:transport_local_ip) { '2.2.2.2' } + let(:transport_local_port) { 6050 } + let(:username) { 'user' } + let(:realm) { 'auth-realm' } + let(:i_method) { 'REGISTER' } + let(:ruri) { 'sip:ruri@domain' } + let(:from_uri) { '"from" ' } + let(:to_uri) { '"to" ' } + let(:call_id) { 'fAtogKNE7RNF5ba7rm9A8Do1hFo1xOX7ppWo72vDV4gErsyBacSOhtI6ORKKedCJJ' } + let(:success) { true } + let(:code) { 200 } + let(:reason) { 'OK' } + let(:internal_reason) { 'OK' } + let(:nonce) { 'VoYxUGHsXYtUTsc5fxFbYbt6adrwaZt45RJGzqLrPJNigemF7JuedrUJ6QwwieP1h' } + let(:response) { 'yYmjZN2SKy92dAshXSeZWbfHPw9wKqMm5FitX5BmeGEtF7k8hiKfmgjgBohxv44bs' } + let(:gateway_id) { 100 } + let(:x_yeti_auth) { '4v96qlIguGsxVQGg' } + let(:diversion) { '"Diversion" ' } + let(:origination_ip) { '8.8.8.8' } + let(:origination_port) { 5070 } + let(:origination_proto_id) { 2 } + let(:pai) { '"Pai" ' } + let(:ppi) { '"ppi" ' } + let(:privacy) { 'id,critical' } + let(:rpid) { '"rpid" ' } + let(:rpid_privacy) { 'full' } + + context 'Create' do + it 'creates Auth log' do + expect { subject }.to change { Cdr::AuthLog.count }.by(1) + end + + it 'creates Auth log with expected attributes' do + subject + log = Cdr::AuthLog.last + expect(log).to have_attributes( + id: kind_of(Integer), + node_id: node_id, + pop_id: pop_id, + request_time: be_within(1.second).of(request_time), + transport_proto_id: transport_proto_id, + transport_remote_ip: transport_remote_ip, + transport_remote_port: transport_remote_port, + transport_local_ip: transport_local_ip, + transport_local_port: transport_local_port, + username: username, + realm: realm, + ruri: ruri, + from_uri: from_uri, + to_uri: to_uri, + call_id: call_id, + success: success, + code: code, + reason: reason, + internal_reason: internal_reason, + nonce: nonce, + response: response, + gateway_id: gateway_id, + x_yeti_auth: x_yeti_auth, + diversion: diversion, + origination_ip: origination_ip, + origination_port: origination_port, + origination_proto_id: origination_proto_id, + pai: pai, + ppi: ppi, + privacy: privacy, + rpid: rpid, + rpid_privacy: rpid_privacy + ) + end + end +end