Squashed commit of the following: commitchanges/01/601/179484a6e87
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Wed Dec 3 02:11:23 2014 +0200 MT#9177 We don't need update device_id in credentials. Rather usual thing. commitd2161a9b3f
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Tue Nov 25 13:16:02 2014 +0200 MT#9177 Return unregister into utils area. commitac8115bcc2
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Sun Nov 16 13:58:35 2014 +0200 MT#9177 Yealink works commit51336e2b18
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Sun Nov 16 12:23:19 2014 +0200 MT#9177 Yealink add_server commit6dc0fde8dd
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Fri Nov 14 09:54:14 2014 +0200 MT#9177 Yealink url via serverName commitec00e65878
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Thu Nov 13 10:55:07 2014 +0200 MT#9177 Yealink requires server length < 20 commit0c29750e74
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Thu Nov 13 10:45:27 2014 +0200 MT#9177 Wonderfully authorization for Panasonic started to work Without = at the end commit7d3c540a82
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Thu Nov 13 10:36:51 2014 +0200 MT#9177 Fix yealink authorization too commite39da24bc9
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Thu Nov 13 10:15:26 2014 +0200 MT#9177 Small fixes for yealink commit9b9e2973ee
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Thu Nov 13 08:12:33 2014 +0200 MT#9177 Add yealink credentials fields commitd63e9ab967
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Thu Nov 13 06:24:46 2014 +0200 MT#9177 Some fixes for this layout commitbd2a026ed4
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Wed Nov 12 22:43:37 2014 +0200 MT#9177 Use custom bootstrap uri in this layout commitbfd2f7a96c
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Wed Nov 12 17:07:50 2014 +0200 MT#9177 Further yealink changes commit3779466cb6
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Wed Nov 12 15:55:47 2014 +0200 MT#9177 Yealink added commit3fa77dcdb0
Author: Irina Peshinskaya <ipeshinskaya@sipwise.com> Date: Wed Nov 5 08:23:56 2014 +0200 MT#9177 Refactor code to separate vendor specific and common logic Change-Id: I41a912a5c8f90b3c58cf49b87a38a3b6a7b64bd6
parent
0dc5e7bd64
commit
e45b214de4
@ -1,134 +1,102 @@
|
||||
package NGCP::Panel::Utils::DeviceBootstrap::Panasonic;
|
||||
|
||||
use Sipwise::Base;
|
||||
use URI::Escape;
|
||||
use MIME::Base64 qw/encode_base64/;
|
||||
use Net::HTTPS::Any qw/https_post/;
|
||||
use RPC::XML::ParserFactory 'XML::LibXML';
|
||||
use RPC::XML;
|
||||
use strict;
|
||||
use Moose;
|
||||
use Data::Dumper;
|
||||
extends 'NGCP::Panel::Utils::DeviceBootstrap::VendorRPC';
|
||||
|
||||
has 'rpc_server_params' => (
|
||||
is => 'rw',
|
||||
isa => 'HashRef',
|
||||
accessor => '_rpc_server_params',
|
||||
);
|
||||
has 'register_content' => (
|
||||
is => 'rw',
|
||||
isa => 'Str',
|
||||
accessor => '_register_content',
|
||||
);
|
||||
has 'unregister_content' => (
|
||||
is => 'rw',
|
||||
isa => 'Str',
|
||||
accessor => '_unregister_content',
|
||||
);
|
||||
sub rpc_server_params{
|
||||
my $self = shift;
|
||||
my $cfg = {
|
||||
proto => 'https',
|
||||
host => 'provisioning.e-connecting.net',
|
||||
port => '443',
|
||||
path => '/redirect/xmlrpc',
|
||||
};
|
||||
$cfg->{headers} = { %{$self->get_basic_authorization($self->params->{credentials})} };
|
||||
$self->{rpc_server_params} = $cfg;
|
||||
return $self->{rpc_server_params};
|
||||
}
|
||||
|
||||
my $cfg = {
|
||||
proto => 'https',
|
||||
host => 'provisioning.e-connecting.net',
|
||||
port => '443',
|
||||
path => '/redirect/xmlrpc',
|
||||
};
|
||||
|
||||
sub prepare {
|
||||
my ($c, $fdev) = @_;
|
||||
my $p = {};
|
||||
|
||||
my $devmod = $fdev->profile->config->device;
|
||||
my $creds = $devmod->autoprov_redirect_credentials;
|
||||
if($creds) {
|
||||
$p->{auth} = encode_base64($creds->user.':'.$creds->password);
|
||||
}
|
||||
sub register_content {
|
||||
my $self = shift;
|
||||
#<param><value><string>".URI::Escape::uri_escape($self->content_params->{uri})."</string></value></param>
|
||||
$self->{register_content} = "<?xml version=\"1.0\"?>
|
||||
<methodCall>
|
||||
<methodName>ipredirect.registerPhone</methodName>
|
||||
<params>
|
||||
<param><value><string>".$self->content_params->{mac}."</string></value></param>
|
||||
<param><value><string><![CDATA[".$self->content_params->{uri}."]]></string></value></param>
|
||||
</params>
|
||||
</methodCall>";
|
||||
return $self->{register_content};
|
||||
}
|
||||
|
||||
$p->{uri} = ($devmod->bootstrap_uri)
|
||||
? $devmod->bootstrap_uri
|
||||
: NGCP::Panel::Utils::DeviceBootstrap::get_baseuri($c);
|
||||
|
||||
if ($p->{uri} !~/\{MAC\}$/){
|
||||
if ($p->{uri} !~/\/$/){
|
||||
$p->{uri} .= '/' ;
|
||||
}
|
||||
$p->{uri} .= '{MAC}' ;
|
||||
}
|
||||
$p->{uri} = URI::Escape::uri_escape($p->{uri});
|
||||
|
||||
return $p;
|
||||
sub unregister_content {
|
||||
my $self = shift;
|
||||
$self->{unregister_content} = "<?xml version=\"1.0\"?>
|
||||
<methodCall>
|
||||
<methodName>ipredirect.unregisterPhone</methodName>
|
||||
<params>
|
||||
<param><value><string>".$self->content_params->{mac_old}."</string></value></param>
|
||||
</params>
|
||||
</methodCall>";
|
||||
return $self->{unregister_content};
|
||||
}
|
||||
|
||||
# return faultString or undef if ok
|
||||
sub check_result {
|
||||
my ($c, $data) = @_;
|
||||
my $val = '';
|
||||
if($data){
|
||||
my $parser = RPC::XML::ParserFactory->new();
|
||||
my $rpc = $parser->parse($data);
|
||||
$val = $rpc->value->value;
|
||||
}
|
||||
sub parse_rpc_response{
|
||||
my($self,$rpc_response) = @_;
|
||||
return $rpc_response->value->value;
|
||||
}
|
||||
|
||||
$c->log->debug("panasonic redirect call returned: " . Dumper $val);
|
||||
sub extract_response_description{
|
||||
my($self,$response_value) = @_;
|
||||
|
||||
if(ref $val eq 'HASH' && $val->{faultString}) {
|
||||
return $val->{faultString};
|
||||
if(('HASH' eq ref $response_value) && $response_value->{faultString}){
|
||||
return $response_value->{faultString};
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
1;
|
||||
|
||||
sub normalize_mac {
|
||||
my ($mac) = @_;
|
||||
return unless($mac);
|
||||
$mac =~s/[^A-F0-9]//gi;
|
||||
$mac = uc($mac);
|
||||
return $mac;
|
||||
}
|
||||
=head1 NAME
|
||||
|
||||
sub unregister {
|
||||
my ($c, $fdev, $mac, $old_mac) = @_;
|
||||
NGCP::Panel::Utils::DeviceBootstrap
|
||||
|
||||
my $p = prepare($c, $fdev);
|
||||
$old_mac = normalize_mac($old_mac);
|
||||
=head1 DESCRIPTION
|
||||
|
||||
my $data = "<?xml version=\"1.0\"?>
|
||||
<methodCall>
|
||||
<methodName>ipredirect.unregisterPhone</methodName>
|
||||
<params>
|
||||
<param><value><string>".$old_mac."</string></value></param>
|
||||
</params>
|
||||
</methodCall>";
|
||||
$c->log->debug("panasonic redirect call $data");
|
||||
|
||||
my($res, $code) = https_post({
|
||||
'host' => $cfg->{host},
|
||||
'port' => $cfg->{port},
|
||||
'path' => $cfg->{path},
|
||||
'headers' => { 'Authorization' => 'Basic '.$p->{auth} },
|
||||
'Content-Type' => 'text/xml',
|
||||
'content' => $data,
|
||||
});
|
||||
return check_result($c, $res);
|
||||
}
|
||||
Make API requests to configure remote redirect servers for requested MAC with autorpov uri.
|
||||
|
||||
sub register {
|
||||
my ($c, $fdev, $mac, $old_mac) = @_;
|
||||
=head1 METHODS
|
||||
|
||||
my $p = prepare($c, $fdev);
|
||||
$mac = normalize_mac($mac);
|
||||
$old_mac = normalize_mac($old_mac);
|
||||
=head2 bootstrap
|
||||
|
||||
# we don't check for the result here, in the worst case
|
||||
# we leave an orphaned entry behind
|
||||
unregister($c, $fdev, $mac, $old_mac) if($old_mac && $old_mac ne $mac);
|
||||
|
||||
my $data = "<?xml version=\"1.0\"?>
|
||||
<methodCall>
|
||||
<methodName>ipredirect.registerPhone</methodName>
|
||||
<params>
|
||||
<param><value><string>".$mac."</string></value></param>
|
||||
<param><value><string>".$p->{uri}."</string></value></param>
|
||||
</params>
|
||||
</methodCall>";
|
||||
$c->log->debug("panasonic redirect call $data");
|
||||
|
||||
my($res, $code) = https_post({
|
||||
'host' => $cfg->{host},
|
||||
'port' => $cfg->{port},
|
||||
'path' => $cfg->{path},
|
||||
'headers' => { 'Authorization' => 'Basic '.$p->{auth} },
|
||||
'Content-Type' => 'text/xml',
|
||||
'content' => $data,
|
||||
});
|
||||
if($res){
|
||||
$c->log->debug("register returned with code $code and data $res");
|
||||
return check_result($c, $res);
|
||||
}else{
|
||||
return 'Empty response';
|
||||
}
|
||||
}
|
||||
Dispatch to proper vendor API call.
|
||||
|
||||
1;
|
||||
=head1 AUTHOR
|
||||
|
||||
Irina Peshinskaya C<< <ipeshinskaya@sipwise.com> >>
|
||||
|
||||
=head1 LICENSE
|
||||
|
||||
This library is free software. You can redistribute it and/or modify
|
||||
it under the same terms as Perl itself.
|
||||
|
||||
=cut
|
||||
# vim: set tabstop=4 expandtab:
|
||||
|
@ -0,0 +1,157 @@
|
||||
package NGCP::Panel::Utils::DeviceBootstrap::VendorRPC;
|
||||
|
||||
use strict;
|
||||
use URI::Escape;
|
||||
use MIME::Base64 qw/encode_base64/;
|
||||
use Net::HTTPS::Any qw/https_post/;
|
||||
use RPC::XML::ParserFactory 'XML::LibXML';
|
||||
use RPC::XML;
|
||||
use Data::Dumper;
|
||||
use Moose;
|
||||
|
||||
has 'params' => (
|
||||
is => 'rw',
|
||||
isa => 'HashRef',
|
||||
);
|
||||
has 'content_params' => (
|
||||
is => 'rw',
|
||||
isa => 'HashRef',
|
||||
);
|
||||
has 'rpc_server_params' => (
|
||||
is => 'rw',
|
||||
isa => 'HashRef',
|
||||
);
|
||||
|
||||
sub redirect_server_call{
|
||||
my ($self, $action) = @_;
|
||||
my $c = $self->params->{c};
|
||||
$self->init_content_params();
|
||||
$c->log->debug(Dumper ($self->content_params));
|
||||
my($content,$response_value,$ret);
|
||||
my $method = $action.'_content';
|
||||
if($self->can($method)){
|
||||
$content = $self->$method();
|
||||
}else{
|
||||
$ret = "Unknown method: $action";
|
||||
}
|
||||
if($content){
|
||||
$response_value = $self->rpc_https_call($content);
|
||||
$ret = $self->extract_response_description($response_value);
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
sub rpc_https_call{
|
||||
my($self, $content, $cfg) = @_;
|
||||
$cfg //= $self->rpc_server_params;
|
||||
my $c = $self->params->{c};
|
||||
$c->log->debug( "host=$cfg->{host}; port=$cfg->{port}; path=$cfg->{path}; content=$content;" );
|
||||
$c->log->debug( Dumper($cfg->{headers}) );
|
||||
my( $page, $response_code, %reply_headers ) = https_post({
|
||||
'host' => $cfg->{host},
|
||||
'port' => $cfg->{port},
|
||||
'path' => $cfg->{path},
|
||||
'headers' => $cfg->{headers},
|
||||
'Content-Type' => 'text/xml',
|
||||
'content' => $content,
|
||||
},);
|
||||
$c->log->info( "response=$response_code; page=$page;" );
|
||||
my $response_value = '';
|
||||
if($page){
|
||||
my $parser = RPC::XML::ParserFactory->new();
|
||||
my $rpc_response = $parser->parse($page);
|
||||
$response_value = $self->parse_rpc_response($rpc_response);
|
||||
$c->log->info("response_value=".Dumper($response_value));
|
||||
}
|
||||
return $response_value;
|
||||
}
|
||||
|
||||
sub init_content_params{
|
||||
my($self) = @_;
|
||||
$self->{content_params} ||= {};
|
||||
$self->content_params->{uri} = $self->get_bootstrap_uri();
|
||||
|
||||
$self->content_params->{mac} = normalize_mac($self->params->{mac});
|
||||
if(defined $self->params->{mac_old}) {
|
||||
$self->content_params->{mac_old} = normalize_mac($self->params->{mac_old});
|
||||
}
|
||||
}
|
||||
sub normalize_mac {
|
||||
my ($mac) = @_;
|
||||
return unless($mac);
|
||||
$mac =~s/[^A-F0-9]//gi;
|
||||
$mac = uc($mac);
|
||||
return $mac;
|
||||
}
|
||||
|
||||
|
||||
sub get_basic_authorization{
|
||||
my($self) = @_;
|
||||
my $authorization = encode_base64(join(':',@{$self->params->{credentials}}{qw/user password/}));
|
||||
$authorization =~s/[ \s]//gis;
|
||||
#$authorization .= '=';
|
||||
return { 'Authorization' => 'Basic '.$authorization };
|
||||
}
|
||||
sub get_bootstrap_uri{
|
||||
my ($self) = @_;
|
||||
my $uri = $self->params->{redirect_uri};
|
||||
my $uri_params = $self->params->{redirect_uri_params} || '';
|
||||
if(!$uri){
|
||||
my $cfg = $self->get_bootstrap_uri_conf();
|
||||
$uri = "$cfg->{schema}://$cfg->{host}:$cfg->{port}/device/autoprov/config/";
|
||||
}
|
||||
$uri .= $uri_params;
|
||||
return $self->process_uri($uri);
|
||||
}
|
||||
|
||||
sub process_uri{
|
||||
my($self,$uri) = @_;
|
||||
if($uri !~/^(?:https?|t?ftp):\/\//i ){
|
||||
$uri = 'http://'.$uri;
|
||||
}
|
||||
if ($uri !~/\{MAC\}$/){
|
||||
if ($uri !~/\/$/){
|
||||
$uri .= '/' ;
|
||||
}
|
||||
$uri .= '{MAC}' ;
|
||||
}
|
||||
return $uri;
|
||||
}
|
||||
#separated as this logic also used in other places, so can be moved to other utils module
|
||||
sub get_bootstrap_uri_conf{
|
||||
my ($self) = @_;
|
||||
my $c = $self->params->{c};
|
||||
my $cfg = {
|
||||
schema => $c->config->{deviceprovisioning}->{secure} ? 'https' : 'http',
|
||||
host => $c->config->{deviceprovisioning}->{host} // $c->req->uri->host,
|
||||
port => $c->config->{deviceprovisioning}->{port} // 1444,
|
||||
};
|
||||
return $cfg;
|
||||
}
|
||||
1;
|
||||
|
||||
=head1 NAME
|
||||
|
||||
NGCP::Panel::Utils::DeviceBootstrap
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Make API requests to configure remote redirect servers for requested MAC with autorpov uri.
|
||||
|
||||
=head1 METHODS
|
||||
|
||||
=head2 bootstrap
|
||||
|
||||
Dispatch to proper vendor API call.
|
||||
|
||||
=head1 AUTHOR
|
||||
|
||||
Irina Peshinskaya C<< <ipeshinskaya@sipwise.com> >>
|
||||
|
||||
=head1 LICENSE
|
||||
|
||||
This library is free software. You can redistribute it and/or modify
|
||||
it under the same terms as Perl itself.
|
||||
|
||||
=cut
|
||||
# vim: set tabstop=4 expandtab:
|
@ -0,0 +1,145 @@
|
||||
package NGCP::Panel::Utils::DeviceBootstrap::Yealink;
|
||||
|
||||
use strict;
|
||||
use Moose;
|
||||
use Digest::MD5 qw/md5_hex/;
|
||||
extends 'NGCP::Panel::Utils::DeviceBootstrap::VendorRPC';
|
||||
|
||||
has 'rpc_server_params' => (
|
||||
is => 'rw',
|
||||
isa => 'HashRef',
|
||||
accessor => '_rpc_server_params',
|
||||
);
|
||||
has 'register_content' => (
|
||||
is => 'rw',
|
||||
isa => 'Str',
|
||||
accessor => '_register_content',
|
||||
);
|
||||
has 'unregister_content' => (
|
||||
is => 'rw',
|
||||
isa => 'Str',
|
||||
accessor => '_unregister_content',
|
||||
);
|
||||
has 'add_server_content' => (
|
||||
is => 'rw',
|
||||
isa => 'Str',
|
||||
accessor => '_add_server_content',
|
||||
);
|
||||
sub rpc_server_params{
|
||||
my $self = shift;
|
||||
my $cfg = {
|
||||
proto => 'https',
|
||||
host => 'rps.yealink.com',
|
||||
port => '443',
|
||||
path => '/xmlrpc',
|
||||
};
|
||||
$cfg->{headers} = { %{$self->get_basic_authorization($self->params->{credentials})} };
|
||||
$self->{rpc_server_params} = $cfg;
|
||||
return $self->{rpc_server_params};
|
||||
}
|
||||
|
||||
sub register_content {
|
||||
my $self = shift;
|
||||
$self->{register_content} ||= "<?xml version='1.0' encoding='UTF-8'?>
|
||||
<methodCall>
|
||||
<methodName>redirect.registerDevice</methodName>
|
||||
<params>
|
||||
<param>
|
||||
<value><string>".$self->content_params->{mac}."</string></value>
|
||||
</param>
|
||||
<param>
|
||||
<value><string><![CDATA[".$self->content_params->{server_name}."]]></string></value>
|
||||
</param>
|
||||
</params>
|
||||
</methodCall>";
|
||||
return $self->{register_content};
|
||||
}
|
||||
|
||||
sub unregister_content {
|
||||
my $self = shift;
|
||||
$self->{unregister_content} ||= "<?xml version='1.0' encoding='UTF-8'?>
|
||||
<methodCall>
|
||||
<methodName>redirect.deRegisterDevice</methodName>
|
||||
<params>
|
||||
<param>
|
||||
<value><string>".$self->content_params->{mac_old}."</string></value>
|
||||
</param>
|
||||
</params>
|
||||
</methodCall>";
|
||||
return $self->{unregister_content};
|
||||
}
|
||||
sub add_server_content {
|
||||
my $self = shift;
|
||||
$self->{add_server_content} ||= "<?xml version='1.0' encoding='UTF-8'?>
|
||||
<methodCall>
|
||||
<methodName>redirect.addServer</methodName>
|
||||
<params>
|
||||
<param>
|
||||
<value>
|
||||
<string><![CDATA[".$self->content_params->{server_name}."]]></string>
|
||||
</value>
|
||||
</param>
|
||||
<param>
|
||||
<value>
|
||||
<string><![CDATA[".$self->content_params->{uri}."]]></string>
|
||||
</value>
|
||||
</param>
|
||||
</params>
|
||||
</methodCall>";
|
||||
return $self->{add_server_content};
|
||||
}
|
||||
|
||||
sub parse_rpc_response{
|
||||
my($self,$rpc_response) = @_;
|
||||
return $rpc_response->value->value;
|
||||
}
|
||||
|
||||
sub extract_response_description{
|
||||
my($self,$response_value) = @_;
|
||||
|
||||
if(('HASH' eq ref $response_value) && $response_value->{faultString}){
|
||||
return $response_value->{faultString};
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
override 'process_uri' => sub {
|
||||
my($self,$uri) = @_;
|
||||
$self->content_params->{uri} = super();
|
||||
$self->uri2server_name();
|
||||
return $self->content_params->{uri};
|
||||
};
|
||||
|
||||
sub uri2server_name{
|
||||
my($self) = @_;
|
||||
#http://stackoverflow.com/questions/4826403/hash-algorithm-with-alphanumeric-output-of-20-characters-max
|
||||
$self->content_params->{server_name} ||= substr(md5_hex($self->content_params->{uri}),0,20);
|
||||
return $self->content_params->{server_name};
|
||||
}
|
||||
1;
|
||||
|
||||
=head1 NAME
|
||||
|
||||
NGCP::Panel::Utils::DeviceBootstrap
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Make API requests to configure remote redirect servers for requested MAC with autorpov uri.
|
||||
|
||||
=head1 METHODS
|
||||
|
||||
=head2 bootstrap
|
||||
|
||||
Dispatch to proper vendor API call.
|
||||
|
||||
=head1 AUTHOR
|
||||
|
||||
Irina Peshinskaya C<< <ipeshinskaya@sipwise.com> >>
|
||||
|
||||
=head1 LICENSE
|
||||
|
||||
This library is free software. You can redistribute it and/or modify
|
||||
it under the same terms as Perl itself.
|
||||
|
||||
=cut
|
||||
# vim: set tabstop=4 expandtab:
|
Loading…
Reference in new issue