From 9bc45cf3f5ad74ed5af132eaeff84d35edb9313f Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Mon, 2 Mar 2015 14:32:29 +0100 Subject: [PATCH] MT#11501 dialogic api read and hash config Change-Id: I6adffa64d5ad1fabf6e49b4ac97f22f5e40f3dae --- Build.PL | 1 + lib/NGCP/Panel/Utils/DialogicImg.pm | 106 ++++++++++++++++++++++++---- share/tools/dialogic-test-client.pl | 10 ++- 3 files changed, 103 insertions(+), 14 deletions(-) diff --git a/Build.PL b/Build.PL index 4e230b9841..464acf4649 100644 --- a/Build.PL +++ b/Build.PL @@ -89,6 +89,7 @@ my $builder = Local::Module::Build->new( 'Sereal::Encoder' => 0, 'String::MkPasswd' => 0, 'Sipwise::Base' => 0, + 'Storable' => 0, 'strict' => 0, 'Template' => 0, 'Template::Plugin::JSON::Escape' => 0, diff --git a/lib/NGCP/Panel/Utils/DialogicImg.pm b/lib/NGCP/Panel/Utils/DialogicImg.pm index e0ca4e0f96..e09a601f83 100644 --- a/lib/NGCP/Panel/Utils/DialogicImg.pm +++ b/lib/NGCP/Panel/Utils/DialogicImg.pm @@ -1,6 +1,5 @@ use strict; use warnings; - { package My::Serializer::Custom; use Moo; @@ -9,7 +8,11 @@ use warnings; sub _set_serializer { my $s = Data::Serializer::Raw->new( serializer => 'XML::Simple', - options => { RootName => 'object' } ); + options => { + RootName => 'object', + # KeyAttr => { 'object' => '+classname' }, + # ForceArray => ['IPProfile'], + } ); return $s; } @@ -20,8 +23,10 @@ use warnings; package NGCP::Panel::Utils::DialogicImg; use Moo; -use Types::Standard qw(Int HashRef); +use Digest::MD5 qw/md5_hex/; use HTTP::Tiny; +use Storable qw/freeze/; +use Types::Standard qw(Int HashRef); with 'Role::REST::Client'; has '+type' => ( default => 'application/xml', is => 'rw' ); @@ -456,7 +461,12 @@ sub create_all_sipsip { $self->_create_indent; my $resp = $self->create_bn2020; - + my @in_schedule = map { + { + name => 'vocoder_profile', options => + { PayloadType => $_ }, + }; + } @{ $settings->{in_codecs} }; my $schedule = [ {name => 'network', options => undef}, {name => 'interface_collection', options => undef}, @@ -486,12 +496,7 @@ sub create_all_sipsip { DigitRelay => 'DTMF Packetized', Name => 'ngcp_in_profile', }}, - {name => 'vocoder_profile', options => { - PayloadType => 'G711 ulaw', - }}, - {name => 'vocoder_profile', options => { - PayloadType => 'G711 alaw', - }}, + @in_schedule, {name => 'sip_profile_collection', options => undef}, {name => 'sip_profile', options => undef}, #{run => 'download_profiles'}, @@ -551,12 +556,90 @@ sub create_all_sipsip { ###### OTHER STUFF ###### +sub hash_config_sipsip { + my ($self, $config) = @_; + $Storable::canonical = 1; + return md5_hex(freeze $config); +} + +sub get_config { + my ($self) = @_; + + my $appid = $self->appid; + my $resp = $self->get("/oamp/configuration/objects?appid=$appid"); + my $config = {}; + if ($resp->code != 200) { + warn "failed to get objects\n"; + return $resp; + } + my $classinfo = $self->classinfo; + my $rev_lookup = {}; + @{$rev_lookup}{map {$_->{name}} values %{$classinfo}} = keys %{$classinfo}; + my $root = $resp->data->{object}[0]; + for my $ch (_get_all_children($root)) { + $self->_recursive_get($ch, $rev_lookup); + } + if ($classinfo->{ip_address}{configuredids}[0]) { + ($config->{ip1}) = $self->_get_specifics( + 'ip_address', $classinfo->{ip_address}{configuredids}[0], ['NIIPAddress']); + } + if ($classinfo->{ip_address}{configuredids}[1]) { + ($config->{ip2}) = $self->_get_specifics( + 'ip_address', $classinfo->{ip_address}{configuredids}[1], ['NIIPAddress']); + } + if ($classinfo->{external_gateway}{configuredids}[0]) { + ($config->{ip_client}) = $self->_get_specifics( + 'external_gateway', $classinfo->{external_gateway}{configuredids}[0], ['IPAddress']); + } + if (exists $classinfo->{vocoder_profile}{configuredids}) { + for my $id (@{ $classinfo->{vocoder_profile}{configuredids} }) { + push @{ $config->{in_codecs} }, $self->_get_specifics( + 'vocoder_profile', $id, ['PayloadType']); + } + } + return $config; +} + +sub _recursive_get { + my ($self, $node, $rev_lookup) = @_; + my $classname = $node->{classname}; + my $oname = $rev_lookup->{$classname}; + my $oid = $node->{oid}; + my $appid = $self->appid; + for my $ch (_get_all_children($node)) { + $self->_recursive_get($ch, $rev_lookup); + } + if ($oname) { + $self->pids->{$oname} = $oid; + push @{ $self->classinfo->{$oname}{configuredids} }, $oid; + } + return; +} + +sub _get_specifics { + my ($self, $class, $id, $keys) = @_; + + my $appid = $self->appid; + my $classinfo = $self->classinfo->{$class}; + my $classname = $classinfo->{name}; + my @res; + my $resp = $self->get("/oamp/configuration/objects/$classname/$id?appid=$appid&detaillevel=4"); + if ($resp->code != 200) { + warn "failed to get objects\n"; + return (); + } + for my $key (@{ $keys }) { + push @res, $resp->data->{property}{$key}{value}; + } + return @res; +} + sub _create_indent { my ($self, @class) = @_; my $classinfo = $self->classinfo; @class = keys %{ $classinfo } unless @class; for my $class (@class) { - next if ($classinfo->{$class}{indent}); + next if (exists $classinfo->{$class}{indent}); my $parent = $classinfo->{$class}{parent}; if ($parent eq 'root') { $classinfo->{$class}{indent} = 0; @@ -608,7 +691,6 @@ sub download_channel_groups { sub delete_all_bn2020 { my ($self) = @_; - my $pid = $self->pids->{bn2020}; my $appid = $self->appid; my $resp = $self->get( "/oamp/configuration/objects?appid=$appid", diff --git a/share/tools/dialogic-test-client.pl b/share/tools/dialogic-test-client.pl index bad33b1041..ac556a6a9c 100644 --- a/share/tools/dialogic-test-client.pl +++ b/share/tools/dialogic-test-client.pl @@ -38,12 +38,17 @@ print "LOGGED IN, LOCK OBTAINED ############################\n"; # print_documentation_md($test); +# $resp = $test->get_config; +# p $resp; +# #p $test->classinfo; +# exit; + my $result = $test->create_all_sipsip({ ip1 => '10.15.20.92', ip2 => '10.15.21.10', ip_client => '10.15.20.199', in_codecs => ['G711 ulaw', 'G711 alaw', 'G729', 'AMR'], - out_codecs => ['G711 ulaw', 'G711 alaw', 'G729', 'AMR'], # not yet used + #out_codecs => ['G711 ulaw', 'G711 alaw', 'G729', 'AMR'], # not yet used }, 2, ); @@ -65,7 +70,8 @@ sub print_documentation_md { for my $o (@{ $options }) { my ( $name, $displayname, $default, $choices ) = @{$o}{ 'name', 'displayname', 'default', 'choices' }; - my $choices_str = $choices && @$choices ? join(", ", map { "`$_`" } @{$choices}) : ''; + my $choices_str = $choices && @{$choices} + ? join( ", ", map {"`$_`"} @{$choices} ) : ''; if ($default) { $default = "`$default`"; }