MT#18663 MT#20893 MT#21871 align setting allowed ips with api impl

Change-Id: I72ac7b902071c692b9e5745ac4d1431cf957fb99
changes/88/7588/2
Rene Krenn 9 years ago
parent a2892c68dc
commit 018a04fa18

@ -9,6 +9,7 @@ use NGCP::BulkProcessor::Logging qw(
rowinsertskipped rowinsertskipped
rowupdateskipped rowupdateskipped
rowupdated rowupdated
rowsdeleted
); );
use NGCP::BulkProcessor::ConnectorPool qw( use NGCP::BulkProcessor::ConnectorPool qw(
@ -29,9 +30,11 @@ our @EXPORT_OK = qw(
check_table check_table
get_id get_id
forupdate_increment increment
cleanup_ids
); );
#forupdate_increment
my $tablename = 'voip_aig_sequence'; my $tablename = 'voip_aig_sequence';
my $get_db = \&get_provisioning_db; my $get_db = \&get_provisioning_db;
@ -44,8 +47,8 @@ my $indexes = {};
my $insert_unique_fields = []; my $insert_unique_fields = [];
my $start_value = 1; #my $start_value = 1;
my $increment = 1; #my $increment = 1;
sub new { sub new {
@ -61,16 +64,20 @@ sub new {
sub get_id { sub get_id {
my ($xa_db) = @_;
check_table(); check_table();
my $db = &$get_db(); my $db = &$get_db();
$xa_db //= $db;
my $table = $db->tableidentifier($tablename); my $table = $db->tableidentifier($tablename);
my $stmt = 'SELECT ' . $db->columnidentifier('id') . ' FROM ' . $table; my $stmt = 'SELECT ' . $db->columnidentifier('id') . ' FROM ' . $table . ' ORDER BY ID DESC LIMIT 1';
return $db->db_get_value($stmt); return $xa_db->db_get_value($stmt);
} }
sub forupdate_increment {
sub increment {
my ($xa_db) = @_; my ($xa_db) = @_;
@ -79,29 +86,75 @@ sub forupdate_increment {
$xa_db //= $db; $xa_db //= $db;
my $table = $db->tableidentifier($tablename); my $table = $db->tableidentifier($tablename);
my $stmt = 'SELECT ' . $db->columnidentifier('id') . ' FROM ' . $table . ' FOR UPDATE'; my $stmt = insert_stmt($db,__PACKAGE__);
my $id = $xa_db->db_get_value($stmt); if ($xa_db->db_do($stmt,0)) {
if (defined $id) {
$stmt = 'UPDATE ' . $table . ' SET ' . $db->columnidentifier('id') . ' = ? WHERE ' . $db->columnidentifier('id') . ' = ?';
if ($xa_db->db_do($stmt,$id + $increment,$id)) {
rowupdated($db,$tablename,getlogger(__PACKAGE__));
return $id + $increment;
} else {
rowupdateskipped($db,$tablename,0,getlogger(__PACKAGE__));
return undef;
}
} else {
$stmt = insert_stmt($db,__PACKAGE__);
if ($xa_db->db_do($stmt,$start_value)) {
rowinserted($db,$tablename,getlogger(__PACKAGE__)); rowinserted($db,$tablename,getlogger(__PACKAGE__));
return $start_value; return $xa_db->db_last_insert_id();
} else { } else {
rowinsertskipped($db,$tablename,getlogger(__PACKAGE__)); rowinsertskipped($db,$tablename,getlogger(__PACKAGE__));
return undef; return undef;
} }
} }
sub cleanup_ids {
my ($xa_db) = @_;
check_table();
my $db = &$get_db();
$xa_db //= $db;
my $table = $db->tableidentifier($tablename);
#You can't specify target table 'voip_aig_sequence' for update in FROM clause
my $max_id = get_id($xa_db);
if ($max_id) {
my $stmt = 'DELETE FROM ' . $table . ' WHERE ' . $db->columnidentifier('id') . ' < ?';
my $count;
if ($count = $xa_db->db_do($stmt,$max_id)) {
rowsdeleted($db,$tablename,$count,$count,getlogger(__PACKAGE__));
return 1;
} else {
rowsdeleted($db,$tablename,0,0,getlogger(__PACKAGE__));
return 0;
}
} else {
return 0;
} }
}
#sub forupdate_increment {
#
# my ($xa_db) = @_;
#
# check_table();
# my $db = &$get_db();
# $xa_db //= $db;
# my $table = $db->tableidentifier($tablename);
#
# my $stmt = 'SELECT ' . $db->columnidentifier('id') . ' FROM ' . $table . ' ORDER BY ID DESC LIMIT 1 FOR UPDATE';
# my $id = $xa_db->db_get_value($stmt);
# if (defined $id) {
# $stmt = 'UPDATE ' . $table . ' SET ' . $db->columnidentifier('id') . ' = ? WHERE ' . $db->columnidentifier('id') . ' = ?';
# if ($xa_db->db_do($stmt,$id + $increment,$id)) {
# rowupdated($db,$tablename,getlogger(__PACKAGE__));
# return $id + $increment;
# } else {
# rowupdateskipped($db,$tablename,0,getlogger(__PACKAGE__));
# return undef;
# }
# } else {
# $stmt = insert_stmt($db,__PACKAGE__);
# if ($xa_db->db_do($stmt,$start_value)) {
# rowinserted($db,$tablename,getlogger(__PACKAGE__));
# return $start_value;
# } else {
# rowinsertskipped($db,$tablename,getlogger(__PACKAGE__));
# return undef;
# }
# }
#
#}
sub buildrecords_fromrows { sub buildrecords_fromrows {

@ -699,8 +699,7 @@ sub _reset_set_peer_auth_context {
sub set_allowed_ips { sub set_allowed_ips {
my ($mode) = @_; my $static_context = {};
my $static_context = { mode => $mode };
my $result = _set_allowed_ips_checks($static_context); my $result = _set_allowed_ips_checks($static_context);
destroy_all_dbs(); destroy_all_dbs();
@ -715,7 +714,7 @@ sub set_allowed_ips {
next unless _reset_set_allowed_ips_context($context,$imported_subscriber,$rownum); next unless _reset_set_allowed_ips_context($context,$imported_subscriber,$rownum);
_set_allowed_ips($context); _set_allowed_ips($context);
} }
cleanup_aig_sequence_ids($context);
#return 0; #return 0;
return 1; return 1;
}, },
@ -742,10 +741,31 @@ sub set_allowed_ips {
),$warning_count); ),$warning_count);
} }
sub cleanup_aig_sequence_ids {
my ($context) = @_;
eval {
$context->{db}->db_begin();
if (NGCP::BulkProcessor::Dao::Trunk::provisioning::voip_aig_sequence::cleanup_ids($context->{db})) {
_info($context,'voip_aig_sequence cleaned up');
}
if ($dry) {
$context->{db}->db_rollback(0);
} else {
$context->{db}->db_commit();
}
};
my $err = $@;
if ($err) {
eval {
$context->{db}->db_rollback(1);
};
die($err) if !$skip_errors;
}
}
sub set_allowed_ips_batch { sub set_allowed_ips_batch {
my ($mode) = @_; my $static_context = {};
my $static_context = { mode => $mode };
my $result = _set_allowed_ips_checks($static_context); my $result = _set_allowed_ips_checks($static_context);
destroy_all_dbs(); destroy_all_dbs();
@ -770,7 +790,7 @@ sub set_allowed_ips_batch {
} }
} }
} }
cleanup_aig_sequence_ids($context);
#return 0; #return 0;
return 1; return 1;
}, },
@ -835,7 +855,7 @@ sub _set_allowed_ips_preferences {
NGCP::BulkProcessor::Dao::Trunk::provisioning::voip_allowed_ip_groups::delete_groupid($context->{db},$context->{allowed_ip_group_id}); NGCP::BulkProcessor::Dao::Trunk::provisioning::voip_allowed_ip_groups::delete_groupid($context->{db},$context->{allowed_ip_group_id});
_info($context,"($context->{rownum}) " . 'allowed ips group for subscriber ' . $context->{cli} . ' exists, ipnets deleted',1); _info($context,"($context->{rownum}) " . 'allowed ips group for subscriber ' . $context->{cli} . ' exists, ipnets deleted',1);
} else { } else {
$context->{allowed_ip_group_id} = NGCP::BulkProcessor::Dao::Trunk::provisioning::voip_aig_sequence::forupdate_increment($context->{db}); $context->{allowed_ip_group_id} = NGCP::BulkProcessor::Dao::Trunk::provisioning::voip_aig_sequence::increment($context->{db});
_info($context,"($context->{rownum}) " . 'new allowed ips group id for subscriber ' . $context->{cli} . ' aquired',1); _info($context,"($context->{rownum}) " . 'new allowed ips group id for subscriber ' . $context->{cli} . ' aquired',1);
} }

Loading…
Cancel
Save