Skip to content

Commit

Permalink
calls limit (#1671)
Browse files Browse the repository at this point in the history
* calls limit
  • Loading branch information
dmitry-sinina authored Jan 6, 2025
1 parent b12932c commit 6a4ebe2
Show file tree
Hide file tree
Showing 10 changed files with 1,796 additions and 19 deletions.
10 changes: 10 additions & 0 deletions app/admin/equipment/gateways.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
:is_shared,
:allow_origination, :allow_termination, :sst_enabled,
:origination_capacity, :termination_capacity, :termination_subscriber_capacity,
:termination_cps_limit, :termination_cps_wsize,
:termination_subscriber_cps_limit, :termination_subscriber_cps_wsize,
:preserve_anonymous_from_domain,
[:termination_src_numberlist_name, proc { |row| row.termination_src_numberlist.try(:name) }],
[:termination_dst_numberlist_name, proc { |row| row.termination_dst_numberlist.try(:name) }],
Expand Down Expand Up @@ -358,6 +360,10 @@ def resource_params
f.input :origination_capacity
f.input :termination_capacity
f.input :termination_subscriber_capacity
f.input :termination_cps_limit
f.input :termination_cps_wsize
f.input :termination_subscriber_cps_limit
f.input :termination_subscriber_cps_wsize
f.input :acd_limit
f.input :asr_limit
f.input :short_calls_limit
Expand Down Expand Up @@ -553,6 +559,10 @@ def resource_params
row :origination_capacity
row :termination_capacity
row :termination_subscriber_capacity
row :termination_cps_limit
row :termination_cps_wsize
row :termination_subscriber_cps_limit
row :termination_subscriber_cps_wsize
row :acd_limit
row :asr_limit
row :short_calls_limit
Expand Down
10 changes: 10 additions & 0 deletions app/models/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@
# term_outbound_proxy :string
# term_use_outbound_proxy :boolean default(FALSE), not null
# termination_capacity :integer(2)
# termination_cps_limit :integer(2)
# termination_cps_wsize :integer(2) default(1), not null
# termination_subscriber_capacity :integer(2)
# termination_subscriber_cps_limit :integer(2)
# termination_subscriber_cps_wsize :integer(2) default(1), not null
# to_rewrite_result :string
# to_rewrite_rule :string
# transit_headers_from_origination :string
Expand Down Expand Up @@ -314,8 +318,14 @@ def normalize_host(value)
validates :origination_capacity,
:termination_capacity,
:termination_subscriber_capacity,
:termination_cps_limit,
:termination_subscriber_cps_limit,
numericality: { greater_than: 0, less_than_or_equal_to: PG_MAX_SMALLINT, allow_nil: true, only_integer: true }

validates :termination_cps_wsize,
:termination_subscriber_cps_wsize,
numericality: { greater_than: 0, less_than_or_equal_to: 120, allow_nil: false, only_integer: true }

validates :port, numericality: { greater_than_or_equal_to: ApplicationRecord::L4_PORT_MIN, less_than_or_equal_to: ApplicationRecord::L4_PORT_MAX, allow_nil: true, only_integer: true }

validates :fake_180_timer, numericality: { greater_than: 0, less_than_or_equal_to: PG_MAX_SMALLINT, allow_nil: true, only_integer: true }
Expand Down
1,649 changes: 1,649 additions & 0 deletions db/migrate/20250104221004_term_gw_cps_limit.rb

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions db/seeds/main/class4.sql
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ insert into disconnect_code (id,namespace_id,code,reason) values (1510, 1, 480,
insert into disconnect_code (id,namespace_id,code,reason) values (1511, 1, 480, 'Dialpeer $id overloaded');
insert into disconnect_code (id,namespace_id,code,reason) values (1512, 1, 480, 'Account $id total capacity reached');
insert into disconnect_code (id,namespace_id,code,reason) values (1513, 1, 480, 'Subscriber capacity limit');
insert into disconnect_code (id,namespace_id,code,reason) values (1514, 1, 480, 'Termination gw cps limit');
insert into disconnect_code (id,namespace_id,code,reason) values (1515, 1, 480, 'Dest. subscriber cps limit');

insert into disconnect_code (id,namespace_id,code,reason) values (1600, 1, 503, 'Resource cache error');
insert into disconnect_code (id,namespace_id,code,reason) values (1601, 1, 503, 'Unknown resource overload');

Expand Down
19 changes: 11 additions & 8 deletions db/seeds/main/switch22.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ INSERT INTO switch22.resource_action (id, name) VALUES (2, 'Try next route');
INSERT INTO switch22.resource_action (id, name) VALUES (3, 'Accept');


INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id) VALUES (1, 'Customer account', 1506, 1);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id) VALUES (3, 'Customer auth', 1507, 1);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id) VALUES (4, 'Customer gateway', 1508, 1);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id) VALUES (2, 'Vendor account', 1509, 2);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id) VALUES (5, 'Vendor gateway', 1510, 2);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id) VALUES (6, 'Dialpeer', 1511, 2);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id) VALUES (7, 'Account total capacity', 1512, 1);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id) VALUES (8, 'Subscriber capacity limit', 1513, 2);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id, rate_limit) VALUES (1, 'Customer account', 1506, 1, false);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id, rate_limit) VALUES (3, 'Customer auth', 1507, 1, false);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id, rate_limit) VALUES (4, 'Customer gateway', 1508, 1, false);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id, rate_limit) VALUES (2, 'Vendor account', 1509, 2, false);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id, rate_limit) VALUES (5, 'Vendor gateway', 1510, 2, false);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id, rate_limit) VALUES (6, 'Dialpeer', 1511, 2, false);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id, rate_limit) VALUES (7, 'Account total capacity', 1512, 1, false);
INSERT INTO switch22.resource_type (id, name, internal_code_id, action_id, rate_limit) VALUES (8, 'Subscriber capacity limit', 1513, 2, false);
insert into switch22.resource_type (id, name, internal_code_id, action_id, rate_limit) values (9, 'Term gw cps', 1514, 2, true);
insert into switch22.resource_type (id, name, internal_code_id, action_id, rate_limit) values (10, 'Subscriber cps', 1515, 2, true);



INSERT INTO switch22.switch_interface_out (id, name, type, custom, rank, for_radius) VALUES (890, 'src_number_radius', 'varchar', false, 1050, true);
Expand Down
45 changes: 39 additions & 6 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3538,7 +3538,11 @@ CREATE TABLE class4.gateways (
to_rewrite_result character varying,
privacy_mode_id smallint DEFAULT 0 NOT NULL,
incoming_auth_allow_jwt boolean DEFAULT false NOT NULL,
termination_subscriber_capacity smallint
termination_subscriber_capacity smallint,
termination_cps_limit smallint,
termination_cps_wsize smallint DEFAULT 1 NOT NULL,
termination_subscriber_cps_limit smallint,
termination_subscriber_cps_wsize smallint DEFAULT 1 NOT NULL
);


Expand Down Expand Up @@ -31809,7 +31813,8 @@ CREATE TABLE switch22.resource_type (
id integer NOT NULL,
name character varying NOT NULL,
internal_code_id integer NOT NULL,
action_id integer DEFAULT 1 NOT NULL
action_id integer DEFAULT 1 NOT NULL,
rate_limit boolean DEFAULT false NOT NULL
);


Expand Down Expand Up @@ -33007,9 +33012,18 @@ BEGIN

-- apply capacity limit by destination number
if i_vendor_gw.termination_subscriber_capacity is not null then
i_profile.legb_res = i_profile.legb_res||'8:'||i_vendor_gw.id||'_'||translate(i_profile.dst_prefix_out,': #@', '____')||':'||i_vendor_gw.termination_subscriber_capacity||':1;';
i_profile.legb_res = i_profile.legb_res||'8:'||i_vendor_gw.id||'_'||translate(i_profile.dst_prefix_out,'|;: #@', '______')||':'||i_vendor_gw.termination_subscriber_capacity||':1;';
end if;

if i_vendor_gw.termination_cps_limit is not null then
i_profile.legb_res = i_profile.legb_res||'9:'||i_vendor_gw.id||':'||i_vendor_gw.termination_cps_limit||':'||i_vendor_gw.termination_cps_wsize||';';
end if;

if i_vendor_gw.termination_subscriber_cps_limit is not null then
i_profile.legb_res = i_profile.legb_res||'10:'||i_vendor_gw.id||'_'||translate(i_profile.dst_prefix_out,'|;: #@', '______')||':'||i_vendor_gw.termination_subscriber_cps_limit||':'||i_vendor_gw.termination_subscriber_cps_wsize||';';
end if;


IF cardinality(i_diversion) > 0 AND i_vendor_gw.diversion_send_mode_id > 1 THEN
IF i_vendor_gw.diversion_send_mode_id = 2 AND i_vendor_gw.diversion_domain is not null AND i_vendor_gw.diversion_domain!='' THEN
/* Diversion as SIP URI */
Expand Down Expand Up @@ -33808,9 +33822,18 @@ BEGIN

-- apply capacity limit by destination number
if i_vendor_gw.termination_subscriber_capacity is not null then
i_profile.legb_res = i_profile.legb_res||'8:'||i_vendor_gw.id||'_'||translate(i_profile.dst_prefix_out,': #@', '____')||':'||i_vendor_gw.termination_subscriber_capacity||':1;';
i_profile.legb_res = i_profile.legb_res||'8:'||i_vendor_gw.id||'_'||translate(i_profile.dst_prefix_out,'|;: #@', '______')||':'||i_vendor_gw.termination_subscriber_capacity||':1;';
end if;

if i_vendor_gw.termination_cps_limit is not null then
i_profile.legb_res = i_profile.legb_res||'9:'||i_vendor_gw.id||':'||i_vendor_gw.termination_cps_limit||':'||i_vendor_gw.termination_cps_wsize||';';
end if;

if i_vendor_gw.termination_subscriber_cps_limit is not null then
i_profile.legb_res = i_profile.legb_res||'10:'||i_vendor_gw.id||'_'||translate(i_profile.dst_prefix_out,'|;: #@', '______')||':'||i_vendor_gw.termination_subscriber_cps_limit||':'||i_vendor_gw.termination_subscriber_cps_wsize||';';
end if;


IF cardinality(i_diversion) > 0 AND i_vendor_gw.diversion_send_mode_id > 1 THEN
IF i_vendor_gw.diversion_send_mode_id = 2 AND i_vendor_gw.diversion_domain is not null AND i_vendor_gw.diversion_domain!='' THEN
/* Diversion as SIP URI */
Expand Down Expand Up @@ -34550,9 +34573,18 @@ BEGIN

-- apply capacity limit by destination number
if i_vendor_gw.termination_subscriber_capacity is not null then
i_profile.legb_res = i_profile.legb_res||'8:'||i_vendor_gw.id||'_'||translate(i_profile.dst_prefix_out,': #@', '____')||':'||i_vendor_gw.termination_subscriber_capacity||':1;';
i_profile.legb_res = i_profile.legb_res||'8:'||i_vendor_gw.id||'_'||translate(i_profile.dst_prefix_out,'|;: #@', '______')||':'||i_vendor_gw.termination_subscriber_capacity||':1;';
end if;

if i_vendor_gw.termination_cps_limit is not null then
i_profile.legb_res = i_profile.legb_res||'9:'||i_vendor_gw.id||':'||i_vendor_gw.termination_cps_limit||':'||i_vendor_gw.termination_cps_wsize||';';
end if;

if i_vendor_gw.termination_subscriber_cps_limit is not null then
i_profile.legb_res = i_profile.legb_res||'10:'||i_vendor_gw.id||'_'||translate(i_profile.dst_prefix_out,'|;: #@', '______')||':'||i_vendor_gw.termination_subscriber_cps_limit||':'||i_vendor_gw.termination_subscriber_cps_wsize||';';
end if;


IF cardinality(i_diversion) > 0 AND i_vendor_gw.diversion_send_mode_id > 1 THEN
IF i_vendor_gw.diversion_send_mode_id = 2 AND i_vendor_gw.diversion_domain is not null AND i_vendor_gw.diversion_domain!='' THEN
/* Diversion as SIP URI */
Expand Down Expand Up @@ -50154,6 +50186,7 @@ INSERT INTO "public"."schema_migrations" (version) VALUES
('20241215155451'),
('20241216165130'),
('20241218154326'),
('20241231160003');
('20241231160003'),
('20250104221004');


4 changes: 4 additions & 0 deletions spec/factories/gateways.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@
# term_outbound_proxy :string
# term_use_outbound_proxy :boolean default(FALSE), not null
# termination_capacity :integer(2)
# termination_cps_limit :integer(2)
# termination_cps_wsize :integer(2) default(1), not null
# termination_subscriber_capacity :integer(2)
# termination_subscriber_cps_limit :integer(2)
# termination_subscriber_cps_wsize :integer(2) default(1), not null
# to_rewrite_result :string
# to_rewrite_rule :string
# transit_headers_from_origination :string
Expand Down
4 changes: 4 additions & 0 deletions spec/features/equipment/gateways/export_gateways_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
['Origination capacity', item.origination_capacity.to_s],
['Termination capacity', item.termination_capacity.to_s],
['Termination subscriber capacity', item.termination_subscriber_capacity.to_s],
['Termination cps limit', item.termination_cps_limit.to_s],
['Termination cps wsize', item.termination_cps_wsize.to_s],
['Termination subscriber cps limit', item.termination_subscriber_cps_limit.to_s],
['Termination subscriber cps wsize', item.termination_subscriber_cps_wsize.to_s],
['Acd limit', item.acd_limit.to_s],
['Asr limit', item.asr_limit.to_s],
['Short calls limit', item.short_calls_limit.to_s],
Expand Down
4 changes: 4 additions & 0 deletions spec/models/gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@
# term_outbound_proxy :string
# term_use_outbound_proxy :boolean default(FALSE), not null
# termination_capacity :integer(2)
# termination_cps_limit :integer(2)
# termination_cps_wsize :integer(2) default(1), not null
# termination_subscriber_capacity :integer(2)
# termination_subscriber_cps_limit :integer(2)
# termination_subscriber_cps_wsize :integer(2) default(1), not null
# to_rewrite_result :string
# to_rewrite_rule :string
# transit_headers_from_origination :string
Expand Down
67 changes: 62 additions & 5 deletions spec/sql/switch22/route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ def routing_sp
allow_termination: true,
termination_capacity: vendor_gw_termination_capacity,
termination_subscriber_capacity: vendor_gw_termination_subscriber_capacity,
termination_cps_limit: vendor_gw_termination_cps_limit,
termination_cps_wsize: vendor_gw_termination_cps_wsize,
termination_subscriber_cps_limit: vendor_gw_termination_subscriber_cps_limit,
termination_subscriber_cps_wsize: vendor_gw_termination_subscriber_cps_wsize,
term_append_headers_req: vendor_gw_term_append_headers_req,
diversion_send_mode_id: vendor_gw_diversion_send_mode_id,
diversion_domain: vendor_gw_diversion_domain,
Expand Down Expand Up @@ -613,6 +617,10 @@ def routing_sp

let(:vendor_gw_termination_capacity) { nil }
let(:vendor_gw_termination_subscriber_capacity) { nil }
let(:vendor_gw_termination_cps_limit) { nil }
let(:vendor_gw_termination_cps_wsize) { 1 }
let(:vendor_gw_termination_subscriber_cps_limit) { nil }
let(:vendor_gw_termination_subscriber_cps_wsize) { 1 }

let!(:customer) { create(:contractor, customer: true, enabled: true) }
let!(:customer_account) {
Expand Down Expand Up @@ -4659,8 +4667,8 @@ def routing_sp
end
end

context 'Termination gw capacity limit' do
context 'no termination gw capacity limit' do
context 'Termination gw capacity/cps limit' do
context 'no termination gw capacity/cps limit' do
it 'response with ok ' do
expect(subject.size).to eq(2)
expect(subject.first[:legb_res]).to eq('')
Expand All @@ -4678,13 +4686,62 @@ def routing_sp
end
end

context 'termination gw capacity limit' do
context 'termination gw subscriber capacity limit' do
let(:vendor_gw_termination_subscriber_capacity) { 20 }
let(:uri_name) { '#380_961 234:567@' }
let(:uri_name) { '#38;0|_961 234:567@' }

it 'response with ok ' do
expect(subject.size).to eq(2)
expect(subject.first[:legb_res]).to eq("8:#{vendor_gateway.id}__38_0__961_234_567_:#{vendor_gw_termination_subscriber_capacity}:1;")
expect(subject.second[:disconnect_code_id]).to eq(113) # last profile with route not found error
end
end

context 'termination gw cps limit' do
let(:vendor_gw_termination_cps_limit) { 11 }
let(:vendor_gw_termination_cps_wsize) { 30 }
it 'response with ok ' do
expect(subject.size).to eq(2)
expect(subject.first[:legb_res]).to eq("9:#{vendor_gateway.id}:#{vendor_gw_termination_cps_limit}:#{vendor_gw_termination_cps_wsize};")
expect(subject.second[:disconnect_code_id]).to eq(113) # last profile with route not found error
end
end

context 'termination gw subscriber cps limit' do
let(:vendor_gw_termination_subscriber_cps_limit) { 99 }
let(:vendor_gw_termination_subscriber_cps_wsize) { 15 }
let(:uri_name) { '#38;0|_961 234:567@' }

it 'response with ok ' do
expect(subject.size).to eq(2)
expect(subject.first[:legb_res]).to eq("10:#{vendor_gateway.id}__38_0__961_234_567_:#{vendor_gw_termination_subscriber_cps_limit}:#{vendor_gw_termination_subscriber_cps_wsize};")
expect(subject.second[:disconnect_code_id]).to eq(113) # last profile with route not found error
end
end

context 'termination gw all limits enabled' do
let(:vendor_gw_termination_capacity) { 10 }
let(:vendor_gw_termination_subscriber_capacity) { 20 }

let(:vendor_gw_termination_cps_limit) { 11 }
let(:vendor_gw_termination_cps_wsize) { 30 }

let(:vendor_gw_termination_subscriber_cps_limit) { 99 }
let(:vendor_gw_termination_subscriber_cps_wsize) { 15 }
let(:uri_name) { '#38;0|_961 234:567@' }

let(:expected_resources) {
[
"5:#{vendor_gateway.id}:#{vendor_gw_termination_capacity}:1",
"8:#{vendor_gateway.id}__38_0__961_234_567_:#{vendor_gw_termination_subscriber_capacity}:1",
"9:#{vendor_gateway.id}:#{vendor_gw_termination_cps_limit}:#{vendor_gw_termination_cps_wsize}",
"10:#{vendor_gateway.id}__38_0__961_234_567_:#{vendor_gw_termination_subscriber_cps_limit}:#{vendor_gw_termination_subscriber_cps_wsize}"
]
}

it 'response with ok ' do
expect(subject.size).to eq(2)
expect(subject.first[:legb_res]).to eq("8:#{vendor_gateway.id}__380_961_234_567_:#{vendor_gw_termination_subscriber_capacity}:1;")
expect(subject.first[:legb_res].split(';')).to eq(expected_resources)
expect(subject.second[:disconnect_code_id]).to eq(113) # last profile with route not found error
end
end
Expand Down

0 comments on commit 6a4ebe2

Please sign in to comment.