TT#116100 k-c-t config.yml

* manage phone numbers dynamic assignation

Change-Id: Ic195e7920594010388c0c169b01b5cd03ee7af43
mr9.5.1
Victor Seva 5 years ago
parent 53ba3ec017
commit b0d54e06d8

@ -0,0 +1,62 @@
#!/usr/bin/perl
#
# Copyright: 2021 Sipwise Development Team <support@sipwise.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# On Debian systems, the complete text of the GNU General
# Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
#
use strict;
use warnings;
use English;
use Getopt::Long;
use Cwd 'abs_path';
use YAML::XS qw(DumpFile LoadFile);
sub usage
{
my $output = "usage: $PROGRAM_NAME [-h] config.yml\n";
$output .= "Options:\n";
$output .= "\t-h: this help\n";
return $output
}
my $help = 0;
GetOptions (
"h|help" => \$help,
) or die("Error in command line arguments\n".usage());
die(usage()) unless (!$help);
die("Wrong number of arguments[$#ARGV]\n".usage()) unless ($#ARGV == 0);
my $data = LoadFile(abs_path($ARGV[0]));
# network
my $sip = $data->{sip};
print "$sip->{ip}\n";
print "$sip->{port}\n";
print "$sip->{mport}\n";
#peer
my $peer = $data->{peer};
print "$peer->{ip}\n";
print "$peer->{port}\n";
print "$peer->{mport}\n";
# phone
my $phone = $data->{phone};
print "$phone->{cc}\n";
print "$phone->{ac}\n";
print "$phone->{sn}\n";

@ -0,0 +1,69 @@
#!/usr/bin/perl
#
# Copyright: 2021 Sipwise Development Team <support@sipwise.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# On Debian systems, the complete text of the GNU General
# Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
#
use strict;
use warnings;
use English;
use Getopt::Long;
use Cwd 'abs_path';
use YAML::XS qw(DumpFile LoadFile);
sub usage
{
my $output = "usage: $PROGRAM_NAME [-h] scenario_ids.yml\n";
$output .= "Options:\n";
$output .= "\t-h: this help\n";
return $output
}
my $help = 0;
my $port;
my $mport;
GetOptions (
"h|help" => \$help,
) or die("Error in command line arguments\n".usage());
die(usage()) unless (!$help);
die("Wrong number of arguments[$#ARGV]\n".usage()) unless ($#ARGV == 0);
my $ids = LoadFile(abs_path($ARGV[0]));
my $phone = {
cc => 0,
ac => 0,
sn => 0,
};
foreach my $domain (@{$ids->{domains}})
{
foreach (keys %{$ids->{$domain}})
{
my $subs = $ids->{$domain}->{$_};
if ($subs->{sn} > $phone->{sn}) {
$phone = $subs;
}
foreach my $alias (@{$subs->{alias_numbers}})
{
if ($alias->{sn} > $phone->{sn}) {
$phone = $alias;
}
}
}
}
print "$phone->{cc}:$phone->{ac}:$phone->{sn}\n";

@ -182,7 +182,7 @@ scenario_csv() {
echo "IP=${IP} PORT=${PORT} MPORT=${MPORT}"
echo "PEER_IP=${PEER_IP} PEER_PORT=${PEER_PORT} PEER_MPORT=${PEER_MPORT}"
if ! "${BIN_DIR}/scenario.pl" \
--ip="${IP}" --port="${PORT}" --mport="${MPORT}" \
--ip="${IP}" --port="${PORT}" --mport="${MPORT}" --phone="${PHONE}" \
--peer-ip="${PEER_IP}" --peer-port="${PEER_PORT}" --peer-mport="${PEER_MPORT}" \
"${SCEN_CHECK_DIR}/scenario.yml" "${SCEN_CHECK_DIR}/scenario_ids.yml"
then
@ -218,6 +218,7 @@ Options:
-I: peer IP
-P: sipp peer port base
-M: sipp peer multimedia port base
-n: phone base number(cc:ac:sn). Default: 43:1:1000
Arguments:
scenario_dir
action. 'create' or 'delete'
@ -227,10 +228,11 @@ EOF
IP="127.126.0.1"
PORT=51602
MPORT=45003
PHONE=43:1:1000
PEER_IP="127.0.2.1"
PEER_PORT=51602
PEER_MPORT=45003
while getopts 'hi:p:m:I:P:M:' opt; do
while getopts 'hi:p:m:I:P:M:n:' opt; do
case $opt in
i) IP=${OPTARG};;
p) PORT=${OPTARG};;
@ -238,6 +240,7 @@ while getopts 'hi:p:m:I:P:M:' opt; do
I) PEER_IP=${OPTARG};;
P) PEER_PORT=${OPTARG};;
M) PEER_MPORT=${OPTARG};;
n) PHONE=${OPTARG};;
h) usage; exit 0;;
*) usage; exit 1;;
esac

@ -30,22 +30,24 @@ fi
usage() {
cat <<EOF
Usage: ${ME} [-h] [-i IP] [-p port] [-m mport] [-x GROUP] action
Usage: ${ME} [-h] [-f config.yml] [-x GROUP] action
Options:
-h: this help
-i: IP. Default: 127.126.0.1
-p: sipp port base. Default: 51602
-m: sipp multimedia port base. Default: 45003
-f: config.yml path. Default: ${BASE_DIR}/config.yml
-x: set GROUP scenario. Default: scenarios
Arguments:
action. 'create' or 'delete'
EOF
}
CONFIG=${BASE_DIR}/config.yml
IP="127.126.0.1"
PORT=51602
MPORT=45003
PHONE_CC=43
PHONE_AC=1
PHONE_SN=1000
PEER_IP="127.0.2.1"
PEER_PORT=51602
PEER_MPORT=45003
@ -63,16 +65,10 @@ get_scenarios() {
fi
}
while getopts 'hi:p:m:I:P:M:x:t:' opt; do
while getopts 'hf:x:' opt; do
case $opt in
i) IP=${OPTARG};;
p) PORT=${OPTARG};;
m) MPORT=${OPTARG};;
I) PEER_IP=${OPTARG};;
P) PEER_PORT=${OPTARG};;
M) PEER_MPORT=${OPTARG};;
f) CONFIG=${OPTARG};;
x) GROUP=${OPTARG};;
t) PROFILE=${OPTARG};;
h) usage; exit 0;;
*) usage; exit 1;;
esac
@ -86,24 +82,26 @@ if [[ $# != 1 ]]; then
fi
ACTION="$1"
if [ -z "${PROFILE}" ]; then
ngcp_type=$(command -v ngcp-type)
if [ -n "${ngcp_type}" ]; then
case $(${ngcp_type}) in
sppro|carrier) PROFILE=PRO;;
spce) PROFILE=CE;;
*) ;;
esac
echo "ngcp-type: profile ${PROFILE}"
get_config() {
local data=()
if [ ! -f "${CONFIG}" ]; then
echo "can't read ${CONFIG} file" >&2
exit 4
fi
fi
if [ "${PROFILE}" != "CE" ] && [ "${PROFILE}" != "PRO" ]; then
echo "PROFILE ${PROFILE} unknown" >&2
usage
exit 2
fi
mapfile -t data < <("${BIN_DIR}/get_config.pl" "${CONFIG}")
IP=${data[0]}
PORT=${data[1]}
MPORT=${data[2]}
PEER_IP=${data[3]}
PEER_PORT=${data[4]}
PEER_MPORT=${data[5]}
PHONE_CC=${data[6]}
PHONE_AC=${data[7]}
PHONE_SN=${data[8]}
}
update_network() {
local data=()
local scen=$1
local ids=${scen}/scenario_ids.yml
@ -121,6 +119,9 @@ update_network() {
if [[ ${data[1]} -ne 2 ]]; then
PEER_MPORT=${data[1]}
fi
mapfile -d: -t data < <("${BIN_DIR}/provide_next_phone.pl" "${ids}")
PHONE_SN=${data[2]}
}
create() {
@ -128,6 +129,7 @@ create() {
echo "*** $1 IP:${IP} PORT:${PORT} MPORT:${MPORT} ***"
"${BIN_DIR}/provide_scenario.sh" \
-i "${IP}" -p "${PORT}" -m "${MPORT}" \
-n "${PHONE_CC}:${PHONE_AC}:${PHONE_SN}" \
-I "${PEER_IP}" -P "${PEER_PORT}" -M "${PEER_MPORT}" \
"${scen}" "${ACTION}"
echo "*** done ***"
@ -139,6 +141,7 @@ delete() {
"${BIN_DIR}/provide_scenario.sh" "${scen}" "${ACTION}"
}
get_config
get_scenarios
case ${ACTION} in

@ -41,6 +41,7 @@ sub usage
$output .= "\t--ip: IP\n";
$output .= "\t--port: base sipp port\n";
$output .= "\t--mport: base sipp media port\n";
$output .= "\t--phone: base phone number\n";
$output .= "\t--peer-ip: peer IP\n";
$output .= "\t--peer-port: peer base sipp port\n";
$output .= "\t--peer-mport: peer base sipp media port\n";
@ -61,11 +62,13 @@ my $net_data = {
mport => 45003,
},
};
my $phone = "43:1:1000";
GetOptions (
"h|help" => \$help,
"ip=s" => \$net_data->{scen}->{ip},
"port=i" => \$net_data->{scen}->{port},
"mport=i" => \$net_data->{scen}->{mport},
"phone=s" => \$phone,
"peer-ip=s" => \$net_data->{peer}->{ip},
"peer-port=i" => \$net_data->{peer}->{port},
"peer-mport=i" => \$net_data->{peer}->{mport},
@ -92,6 +95,48 @@ our $tt = Template->new({
INCLUDE_PATH => $template_dir,
INTERPOLATE => 1,
}) || die "$Template::ERROR\n";
our ($phone_cc, $phone_ac, $phone_sn) = split(/:/, $phone);
$phone_sn = int($phone_sn);
sub key_domain
{
my $domain = shift;
my $key_dom = $domain =~ tr/\./_/r;
return $key_dom =~ tr/\-/_/r;
}
sub manage_phones
{
my ($data) = @_;
foreach my $domain (sort keys %{$data->{subscribers}}) {
my $key_dom = key_domain($domain);
push(@{$ids->{domains}}, $key_dom);
foreach my $subs (sort keys %{$data->{subscribers}->{$domain}}) {
my $subs_data = $data->{subscribers}->{$domain}->{$subs};
$ids->{$key_dom}->{$subs}->{cc} = $subs_data->{cc} = $phone_cc;
$ids->{$key_dom}->{$subs}->{ac} = $subs_data->{ac} = $phone_ac;
$ids->{$key_dom}->{$subs}->{sn} = $subs_data->{sn} = $phone_sn++;
$ids->{$key_dom}->{$subs}->{phone_number} = $subs_data->{cc} . $subs_data->{ac} . $subs_data->{sn};
}
foreach my $subs (sort keys %{$data->{subscribers}->{$domain}}) {
my $subs_data = $data->{subscribers}->{$domain}->{$subs};
foreach (@{$subs_data->{alias_numbers}}) {
my $alias_data = {
cc => $phone_cc,
ac => $phone_ac,
sn => $phone_sn++,
};
$alias_data->{phone_number} = $alias_data->{cc} . $alias_data->{ac} . $alias_data->{sn};
push(@{$ids->{$key_dom}->{$subs}->{alias_numbers}}, $alias_data);
}
if(defined($ids->{$key_dom}->{$subs}->{alias_numbers})) {
$subs_data->{alias_numbers} = $ids->{$key_dom}->{$subs}->{alias_numbers};
}
}
}
}
sub new_csv
{
@ -118,6 +163,7 @@ sub get_subs_info
if (defined($data_sub->{$data->{domain}}))
{
my $domain = $data->{domain};
my $key_dom = key_domain($domain);
if (defined($data_sub->{$domain}->{$data->{username}}))
{
my $username = $data->{username};
@ -346,6 +392,7 @@ sub generate_foreign_dom
return;
}
manage_phones($cf);
generate($cf);
generate_presence($cf);
DumpFile(abs_path($ARGV[1]), $ids);

@ -0,0 +1,13 @@
---
sip:
ip: 127.126.0.1
port: 51602
mport: 45003
peer:
ip: 127.0.2.1
port: 51602
mport: 45003
phone:
cc: 43
ac: 1
sn: 1000
Loading…
Cancel
Save