From b0d54e06d8dee23bcea3fd2eb66e60b72453bc84 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Tue, 30 Mar 2021 15:35:00 +0200 Subject: [PATCH] TT#116100 k-c-t config.yml * manage phone numbers dynamic assignation Change-Id: Ic195e7920594010388c0c169b01b5cd03ee7af43 --- bin/get_config.pl | 62 +++++++++++++++++++++++++++++++++++ bin/provide_next_phone.pl | 69 +++++++++++++++++++++++++++++++++++++++ bin/provide_scenario.sh | 7 ++-- bin/provide_scenarios.sh | 57 +++++++++++++++++--------------- bin/scenario.pl | 47 ++++++++++++++++++++++++++ config.yml | 13 ++++++++ 6 files changed, 226 insertions(+), 29 deletions(-) create mode 100755 bin/get_config.pl create mode 100755 bin/provide_next_phone.pl create mode 100644 config.yml diff --git a/bin/get_config.pl b/bin/get_config.pl new file mode 100755 index 00000000..9fb858f0 --- /dev/null +++ b/bin/get_config.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl +# +# Copyright: 2021 Sipwise Development Team +# +# 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 . +# +# 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"; diff --git a/bin/provide_next_phone.pl b/bin/provide_next_phone.pl new file mode 100755 index 00000000..d54599a1 --- /dev/null +++ b/bin/provide_next_phone.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl +# +# Copyright: 2021 Sipwise Development Team +# +# 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 . +# +# 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"; diff --git a/bin/provide_scenario.sh b/bin/provide_scenario.sh index c012df8d..0d2dd0b2 100755 --- a/bin/provide_scenario.sh +++ b/bin/provide_scenario.sh @@ -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 diff --git a/bin/provide_scenarios.sh b/bin/provide_scenarios.sh index 31c8b252..41c6cabe 100755 --- a/bin/provide_scenarios.sh +++ b/bin/provide_scenarios.sh @@ -30,22 +30,24 @@ fi usage() { cat <&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 diff --git a/bin/scenario.pl b/bin/scenario.pl index 31ffe00d..bfbb5e88 100755 --- a/bin/scenario.pl +++ b/bin/scenario.pl @@ -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); diff --git a/config.yml b/config.yml new file mode 100644 index 00000000..d838809e --- /dev/null +++ b/config.yml @@ -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