parent
0dc5e7bd64
commit
3fa77dcdb0
@ -1,134 +1,100 @@
|
||||
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 Data::Dumper;
|
||||
|
||||
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);
|
||||
}
|
||||
use strict;
|
||||
use Moose;
|
||||
extends 'NGCP::Panel::Utils::DeviceBootstrap::RPC';
|
||||
|
||||
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};
|
||||
}
|
||||
|
||||
$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 register_content {
|
||||
my $self = shift;
|
||||
$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>".$self->content_params->{uri}."</string></value></param>
|
||||
</params>
|
||||
</methodCall>";
|
||||
return $self->{register_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 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};
|
||||
}
|
||||
|
||||
$c->log->debug("panasonic redirect call returned: " . Dumper $val);
|
||||
sub parse_rpc_response{
|
||||
my($self,$rpc_response) = @_;
|
||||
return $rpc_response->value->value;
|
||||
}
|
||||
|
||||
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,137 @@
|
||||
package NGCP::Panel::Utils::DeviceBootstrap::RPC;
|
||||
|
||||
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_register{
|
||||
my ($self) = @_;
|
||||
my $c = $self->params->{c};
|
||||
$self->init_content_params();
|
||||
$c->log->debug(Dumper ($self->content_params));
|
||||
my($content,$response_value);
|
||||
if(defined $self->content_params->{mac_old} && $self->content_params->{mac} ne $self->content_params->{mac_old}) {
|
||||
$content = $self->unregister_content();
|
||||
$response_value = $self->rpc_https_call($content);
|
||||
}
|
||||
$content = $self->register_content();
|
||||
$response_value = $self->rpc_https_call($content);
|
||||
return $self->extract_response_description($response_value);
|
||||
}
|
||||
|
||||
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;" );
|
||||
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->params->{redirect_uri_params} ||= '{MAC}';
|
||||
my $uri = $self->get_bootstrap_uri();
|
||||
$self->{content_params} ||= {};
|
||||
$self->content_params->{uri} = URI::Escape::uri_escape($uri);
|
||||
my $mac = $self->params->{mac};
|
||||
$mac =~s/[^A-F0-9]//gi;
|
||||
$self->content_params->{mac} = uc($mac);
|
||||
my $mac_old = $self->params->{mac_old};
|
||||
if(defined $mac_old) {
|
||||
$mac_old =~s/[^A-F0-9]//gi;
|
||||
$mac_old = uc($mac_old);
|
||||
$self->content_params->{mac_old} = $mac_old;
|
||||
}
|
||||
}
|
||||
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){
|
||||
if(!$uri =~/^https?:\/\//i ){
|
||||
$uri = 'http://'.$uri;
|
||||
}
|
||||
}else{
|
||||
my $cfg = $self->get_bootstrap_uri_conf();
|
||||
$uri = "$cfg->{schema}://$cfg->{host}:$cfg->{port}/device/autoprov/config/";
|
||||
}
|
||||
$uri .= $uri_params;
|
||||
return $uri;
|
||||
}
|
||||
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:
|
Loading…
Reference in new issue