IxNetwork
view release on metacpan or search on metacpan
lib/IxNetwork/IxNetworkSecure.pm view on Meta::CPAN
#!/usr/bin/perl
#
# Copyright 1997 - 2019 by IXIA Keysight
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
use strict;
use File::Basename;
use File::stat;
use File::Spec;
use File::Path;
use IO::Socket::INET;
use Scalar::Util qw( blessed );
our $SSL_ERROR;
my $dependenciespath;
my $libraryFilePath;
BEGIN {
my ($volume, $directory, $file) = File::Spec->splitpath(__FILE__);
$libraryFilePath = File::Spec->rel2abs($directory);
$directory = File::Spec->catdir( (File::Spec->rel2abs($directory), 'dependencies') );
$dependenciespath = $directory;
}
use lib $dependenciespath;
use IO::Socket::SSL;
use LWP::UserAgent;
use Protocol::WebSocket::Client;
use JSON::PP;
use URI::Escape;
use constant NL => "\r\n";
package IxNetworkSecure;
=head1 new
=cut
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
$self->_setDefaults();
$self->{_debug} = undef;
return $self;
}
sub _setDefaults {
my $self = shift;
$self->{_websocket} = undef;
$self->{_evalError} = '1';
$self->{_evalSuccess} = '0';
$self->{_evalResult} = '0';
$self->{_addContentSeparator} = 0;
$self->{_firstItem} = undef;
$self->{_sendContent} = '';
$self->{_buffer} = undef;
$self->{_sendBuffer} = '';
$self->{_decoratedResult} = '';
$self->{_async} = undef;
$self->{_timeout} = undef;
$self->{_serverPayloadSize}= 65535;
$self->{_transportType} = 'WebSocket';
$self->{_OK} = '::ixNet::OK';
$self->{_ERROR} = '::ixNet::ERROR';
$self->{_version} = '9.00.1915.16';
$self->{_noApiKey} = '00000000000000000000000000000000';
$self->{_connectionInfo} = {
port => undef,
verb => undef,
wsVerb => undef,
hostname => undef,
url => undef,
sessionUrl => undef,
restUrl => undef,
wsUrl => undef,
sessionId => undef,
backendType => undef,
applicationType => undef,
closeServerOnDisconnect => undef,
serverUsername => undef
};
$self->{_initialPort} = undef;
$self->{_initialHostname} = undef;
}
sub setDebug {
lib/IxNetwork/IxNetworkSecure.pm view on Meta::CPAN
}
$sessions = $matchedSessions;
if (keys %{ $sessions } == 0) {
die 'There are no sessions available with the serverusername '.$connectArgs->{-serverusername}."\n";
}
if ($connectArgs->{-sessionId} < 1) {
if (keys %{ $sessions } > 1) {
die 'There are multiple sessions available with the serverusername '.$connectArgs->{-serverusername}.'. Please specify -sessionId also.'."\n";
} else {
my @keys = keys %{ $sessions };
$connectArgs->{-sessionId} = @keys[0];
}
}
}
if (!exists $sessions->{$connectArgs->{-sessionId}}) {
die "Invalid sessionId value ($connectArgs->{-sessionId}).\n";
}
$session = $sessions->{$connectArgs->{-sessionId}};
if ($session->{inUse} and (index($connectArgs->{-clientId}, 'HLAPI') == -1)) {
if ((lc $self->_tryGetAttr($session, 'backendType', 'LinuxAPIServer') eq 'connectionmanager') or $connectArgs->{-allowOnlyOneConnection}) {
$self->{_connectionInfo}->{closeServerOnDisconnect} = 0;
die "The requested session is currently in use.\n";
} else {
print 'Warning: you are connecting to session '.$session->{id}." which is in use.\n";
}
}
}
$self->{_connectionInfo}->{applicationType} = $session->{applicationType};
if (!exists $connectArgs->{-closeServerOnDisconnect}) {
if ($self->{_connectionInfo}->{applicationType} eq 'ixnrest') {
$connectArgs->{-closeServerOnDisconnect} = 'true';
} else {
$connectArgs->{-closeServerOnDisconnect} = 'false';
}
} else {
if ($self->_parseAsBool($connectArgs->{-closeServerOnDisconnect})) {
$connectArgs->{-closeServerOnDisconnect} = 'true';
} else {
$connectArgs->{-closeServerOnDisconnect} = 'false';
}
}
$self->{_connectionInfo}->{closeServerOnDisconnect} = $self->_parseAsBool($connectArgs->{-closeServerOnDisconnect});
$self->{_connectionInfo}->{sessionId} = $session->{id};
$self->{_connectionInfo}->{sessionUrl} = $self->{_connectionInfo}->{url}.'/'.$self->{_connectionInfo}->{sessionId};
$self->{_connectionInfo}->{backendType} = $self->_tryGetAttr($session, 'backendType', 'LinuxAPIServer');
$self->{_connectionInfo}->{wsUrl} = $self->{_connectionInfo}->{wsVerb}.'://'.$self->{_connectionInfo}->{hostname}.':'.$self->{_connectionInfo}->{port}.'/ixnetworkweb/ixnrest/ws/api/v1/sessions/'.$self->{_connectionInfo}->{sessionId}.'/ixnetwo...
$self->{_connectionInfo}->{restUrl} = $self->{_connectionInfo}->{sessionUrl}.'/ixnetwork';
$self->{_connectionInfo}->{serverUsername} = $session->{userName};
if ((lc($session->{state}) eq 'initial') or (lc($session->{state}) eq 'stopped')) {
$self->_restSend('POST', $self->{_connectionInfo}->{sessionUrl}.'/operations/start', {applicationType => $connectArgs->{-product}}, $connectArgs->{-connectTimeout});
}
$self->_waitForState('active', $self->{_connectionInfo}->{sessionUrl}, $connectArgs->{-connectTimeout});
if ($self->_parseAsBool($connectArgs->{-allowOnlyOneConnection})) {
$self->_isSessionAvailable($session, 1);
}
if ($self->{_connectionInfo}->{wsVerb} eq 'wss') {
$self->{_websocket} = new IO::Socket::SSL(
PeerHost => $self->{_connectionInfo}->{hostname}.':'.$self->{_connectionInfo}->{port},
PeerPort => $self->{_connectionInfo}->{verb},
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE
) or die "IO::Socket::SSL reported: $! (ssl_error=$SSL_ERROR)";
} else {
$self->{_websocket} = new IO::Socket::INET(
PeerAddr => $self->{_connectionInfo}->{hostname}.':'.$self->{_connectionInfo}->{port},
PeerPort => $self->{_connectionInfo}->{verb},
) or die "IO::Socket::INET reported: $!";
}
$self->{_ws_client} = new Protocol::WebSocket::Client(url => $self->{_connectionInfo}->{wsUrl}, max_fragments_amount=> 10240, max_payload_size=> $self->{_serverPayloadSize});
$self->{_ws_client}->on(write => sub {
my ($ws) = shift;
my $buffer = shift;
$self->ws_write($ws, $buffer);
});
$self->{_ws_client}->on(read => sub {
my ($ws) = shift;
my $buffer = shift;
$self->ws_read($ws, $buffer);
});
$self->{_ws_client}->connect;
$result = $self->_send_recv('ixNet', 'connect',
'-version', $connectArgs->{'-version'},
'-clientType', 'perl',
'-clientId', $connectArgs->{-clientId},
'-clientUsername', $connectArgs->{'-clientusername'},
'-closeServerOnDisconnect', $connectArgs->{-closeServerOnDisconnect} ,
'-apiKey', $self->{_user_agent}{def_headers}{"x-api-key"});
$self->_check_client_version();
1;
};
if (!$ret and $@) {
if ($self->{_connectionInfo}->{sessionUrl} and $self->{_connectionInfo}->{closeServerOnDisconnect}) {
$self->_cleanUpSession($self->{_connectionInfo}->{sessionUrl});
}
$self->_close();
$self->_deleteSession($self->{_connectionInfo}->{sessionUrl});
my $failedConnectionPort;
if (exists $connectArgs->{-port}) {
$failedConnectionPort = $connectArgs->{-port};
} else {
$failedConnectionPort = 443;
}
$self->_setDefaults();
die 'Unable to connect to '.$hostname.':'.$failedConnectionPort.'. Error: '.$self->{_ERROR}.': '.$@."\n";
} else {
return $result;
}
}
sub ws_write {
my ($self) = shift;
my $ws = shift;
my $buffer = shift;
my $paddingSize = 4;
$self->{_recvBuffer} = '';
my $ret = eval {
$self->{_websocket}->write($buffer);
my $recv_buffer = '';
while (1) {
( run in 1.791 second using v1.01-cache-2.11-cpan-437f7b0c052 )