From 4eef4f527d4a3cd25cc5343dc07fe26a1bf8fa3f Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Fri, 15 Sep 2023 13:51:18 +0200 Subject: [PATCH] MT#58294 support emergency mappings Change-Id: I081371d77b2d894f4e70b928a6c12233ad4f0beb --- README | 10 ++ bin/create_emergencymapping.pl | 166 +++++++++++++++++++++++++++++++++ bin/provide_scenario.sh | 11 +++ lib/Sipwise/API.pm | 68 ++++++++++++++ 4 files changed, 255 insertions(+) create mode 100755 bin/create_emergencymapping.pl diff --git a/README b/README index e3a73f82..9365afc7 100644 --- a/README +++ b/README @@ -429,6 +429,16 @@ testuser1003@spce.test: location: sip:testuser1004@spce.test mode: add +emergency.yml file syntax: +----------------------------- + +emc_test: + reseller_id: 1 + mappings: + - suffix: emc + code: 112 + prefix: kaka + prefs.json file syntax: ---------------------- diff --git a/bin/create_emergencymapping.pl b/bin/create_emergencymapping.pl new file mode 100755 index 00000000..f6377363 --- /dev/null +++ b/bin/create_emergencymapping.pl @@ -0,0 +1,166 @@ +#!/usr/bin/perl +# +# Copyright: 2023 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 Try::Tiny; +use English; +use Getopt::Long; +use Cwd 'abs_path'; +use Config::Tiny; +use Sipwise::API qw(all); +use YAML::XS qw(DumpFile LoadFile); + +my $config = Config::Tiny->read('/etc/default/ngcp-api'); +my $opts; +if ($config) { + $opts = {}; + $opts->{host} = $config->{_}->{NGCP_API_IP}; + $opts->{port} = $config->{_}->{NGCP_API_PORT}; + $opts->{sslverify} = $config->{_}->{NGCP_API_SSLVERIFY}; +} +my $api = Sipwise::API->new($opts); +$opts = $api->opts; +my $del = 0; +my $ids; +my $ids_path; + +sub usage { + return "Usage:\n$PROGRAM_NAME emergency.yml scenarios_ids.yml\n". + "Options:\n". + " -delete\n". + " -d debug\n". + " -h this help\n"; +} +my $help = 0; +GetOptions ("h|help" => \$help, + "d|debug" => \$opts->{verbose}, + "delete" => \$del) + or die("Error in command line arguments\n".usage()); + +die(usage()) unless (!$help); +if(!$del) { + die("Error: wrong number of arguments\n".usage()) unless ($#ARGV == 1); +} else { + die("Error: wrong number of arguments\n".usage()) unless ($#ARGV == 0); +} + +sub manage_emc +{ + my $emc = shift; + + $emc->{id} = $api->check_emergencymappingcontainer_exists($emc); + if(defined $emc->{id}) { + print "emergencymappingcontainer: already there [$emc->{id}]\n"; + } else { + $emc->{id} = $api->create_emergencymappingcontainer($emc); + print "emergencymappingcontainer: created [$emc->{id}]\n"; + } + return; +} + +sub manage_mappings +{ + my $emc = shift; + my $data = shift; + + foreach my $map (@{$data}) + { + $map->{emergency_container_id} = $emc->{id}; + try { + $map->{id} = $api->create_emergencymapping($map); + print "emergencymapping: created [$map->{id}]\n"; + } catch { + if($opts->{verbose}) { + warn "emergencymapping: not able to create: $_"; + } else { + warn "emergencymapping: not able to create"; + } + }; + } + return; +} + +sub do_delete +{ + my $r = shift; + + foreach (keys %{$r}) + { + my $emc = { + "name" => $_, + "reseller_id" => $r->{$_}->{reseller_id}, + }; + $emc->{id} = $api->check_emergencymappingcontainer_exists($emc); + if(defined $emc->{id}) { + if($api->delete_emergencymappingcontainer($emc->{id})) { + print "deleted [$emc->{name}]\n"; + } else { + die("Error: can't delete [$emc->{name}]"); + } + } else { + print "emergencymapping [$emc->{name}]\n"; + } + } + return; +} + +sub do_create { + my $r = shift; + + foreach (keys %{$r}) + { + print "processing $_\n"; + my $emc = { + "name" => $_, + "reseller_id" => $r->{$_}->{reseller_id}, + }; + my $mappings = $r->{$_}->{mappings}; + manage_emc($emc); + $ids->{emergencymapping}->{$emc->{name}}->{id} = $emc->{id}; + manage_mappings($emc, $mappings); + $ids->{emergencymapping}->{$emc->{name}}->{mappings} = $mappings; + } + return; +} + +sub main { + my $r = shift; + + if ($del) { + print "delete emergencymapping\n" unless $opts->{debug}; + return do_delete($r); + } else { + print "create emergencymapping\n" unless $opts->{debug}; + return do_create($r, $ids); + } +} + +if(! $del) { + $ids_path = abs_path($ARGV[1]); + print "load $ids_path\n"; + $ids = LoadFile($ids_path); +} +main(LoadFile(abs_path($ARGV[0])), $ids); +if(! $del) { + print "save $ids_path\n"; + DumpFile($ids_path, $ids); +} diff --git a/bin/provide_scenario.sh b/bin/provide_scenario.sh index 99f2cb38..fb63ff83 100755 --- a/bin/provide_scenario.sh +++ b/bin/provide_scenario.sh @@ -121,6 +121,12 @@ create_voip_prefs() { echo "$(date) - Setting location mappings" "${BIN_DIR}/set_subscribers_location_map.pl" "${SCEN_CHECK_DIR}/locmap.yml" fi + + if [ -f "${SCEN_CHECK_DIR}/emergency.yml" ]; then + echo "$(date) - Setting emergency mappings" + "${BIN_DIR}/create_emergencymapping.pl" \ + "${SCEN_CHECK_DIR}/emergency.yml" "${SCEN_CHECK_DIR}/scenario_ids.yml" + fi } # $1 domain @@ -178,6 +184,11 @@ delete_voip() { echo "$(date) - Deleting soundsets" "${BIN_DIR}/create_soundsets.pl" -delete "${SCEN_CHECK_DIR}/soundsets.yml" fi + + if [ -f "${SCEN_CHECK_DIR}/emergency.yml" ]; then + echo "$(date) - Deleting emergency mappings" + "${BIN_DIR}/create_emergencymapping.pl" -delete "${SCEN_CHECK_DIR}/emergency.yml" + fi } generate_template() { diff --git a/lib/Sipwise/API.pm b/lib/Sipwise/API.pm index b6678d6b..967ae364 100644 --- a/lib/Sipwise/API.pm +++ b/lib/Sipwise/API.pm @@ -1050,4 +1050,72 @@ sub create_locationmapping { return $self->_create($data, $urldata); } +sub check_emergencymappingcontainer_exists { + my ($self, $data) = @_; + my $urldata = '/api/emergencymappingcontainers/'; + my $collection_id = 'ngcp:emergencymappingcontainers'; + + return $self->_exists($data, $urldata, $collection_id); +} + +sub create_emergencymappingcontainer { + my ($self, $data) = @_; + my $urldata = '/api/emergencymappingcontainers/'; + + return $self->_create($data, $urldata); +} + +sub set_emergencymappingcontainer { + my ($self, $id, $data) = @_; + my $urldata = "/api/emergencymappingcontainers/${id}"; + my $collection_id = 'ngcp:emergencymappingcontainers'; + + return $self->_set_content($data, $urldata); +} + +sub get_emergencymappingcontainer { + my ($self, $id) = @_; + my $urldata = "/api/emergencymappingcontainers/${id}"; + my $collection_id = 'ngcp:emergencymappingcontainers'; + + return $self->_get_content(undef, $urldata); +} + +sub delete_emergencymappingcontainer { + my ($self, $id) = @_; + my $urldata = "/api/emergencymappingcontainers/${id}"; + + my $data = {"emergency_container_id" => $id}; + my $mappings = $self->get_emergencymappings($data); + + foreach my $map (@{$mappings}) { + $self->delete_emergencymapping($map->{id}); + }; + + return $self->_delete($urldata); +} + +sub create_emergencymapping { + my ($self, $data) = @_; + my $urldata = '/api/emergencymappings/'; + + return $self->_create($data, $urldata); +} + +sub get_emergencymappings { + my ($self, $data) = @_; + my $urldata = "/api/emergencymappings/"; + my $collection_id = 'ngcp:emergencymappings'; + + my $collection = $self->_get_content($data, $urldata); + return $collection->{_embedded}->{$collection_id}; +} + +sub delete_emergencymapping { + my ($self, $id) = @_; + my $urldata = "/api/emergencymappings/${id}"; + + return $self->_delete($urldata); +} + 1;