| 1 | #!/usr/bin/perl | ||
| 2 | # | ||
| 3 | # Copyright 1997 - 2019 by IXIA Keysight | ||
| 4 | # | ||
| 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 6 | # of this software and associated documentation files (the "Software"), | ||
| 7 | # to deal in the Software without restriction, including without limitation | ||
| 8 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
| 9 | # and/or sell copies of the Software, and to permit persons to whom the | ||
| 10 | # Software is furnished to do so, subject to the following conditions: | ||
| 11 | # | ||
| 12 | # The above copyright notice and this permission notice shall be included in | ||
| 13 | # all copies or substantial portions of the Software. | ||
| 14 | # | ||
| 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 21 | # THE SOFTWARE. | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use File::Basename; | ||
| 25 | use File::stat; | ||
| 26 | use File::Spec; | ||
| 27 | use File::Path; | ||
| 28 | use IO::Socket::INET; | ||
| 29 | use Scalar::Util qw( blessed ); | ||
| 30 | our $SSL_ERROR; | ||
| 31 | my $dependenciespath; | ||
| 32 | my $libraryFilePath; | ||
| 33 | BEGIN { | ||
| 34 | my ($volume, $directory, $file) = File::Spec->splitpath(__FILE__); | ||
| 35 | $libraryFilePath = File::Spec->rel2abs($directory); | ||
| 36 | $directory = File::Spec->catdir( (File::Spec->rel2abs($directory), 'dependencies') ); | ||
| 37 | $dependenciespath = $directory; | ||
| 38 | } | ||
| 39 | use lib $dependenciespath; | ||
| 40 | use IO::Socket::SSL; | ||
| 41 | use LWP::UserAgent; | ||
| 42 | use Protocol::WebSocket::Client; | ||
| 43 | use JSON::PP; | ||
| 44 | use URI::Escape; | ||
| 45 | use constant NL => "\r\n"; | ||
| 46 | |||
| 47 | package IxNetworkSecure; | ||
| 48 | |||
| 49 | =head1 new | ||
| 50 | =cut | ||
| 51 | sub new { | ||
| 52 | my $class = shift; | ||
| 53 | my $self = {}; | ||
| 54 | bless $self, $class; | ||
| 55 | $self->_setDefaults(); | ||
| 56 | $self->{_debug} = undef; | ||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _setDefaults { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | $self->{_websocket} = undef; | ||
| 64 | $self->{_evalError} = '1'; | ||
| 65 | $self->{_evalSuccess} = '0'; | ||
| 66 | $self->{_evalResult} = '0'; | ||
| 67 | $self->{_addContentSeparator} = 0; | ||
| 68 | $self->{_firstItem} = undef; | ||
| 69 | $self->{_sendContent} = ''; | ||
| 70 | $self->{_buffer} = undef; | ||
| 71 | $self->{_sendBuffer} = ''; | ||
| 72 | $self->{_decoratedResult} = ''; | ||
| 73 | $self->{_async} = undef; | ||
| 74 | $self->{_timeout} = undef; | ||
| 75 | $self->{_serverPayloadSize}= 65535; | ||
| 76 | $self->{_transportType} = 'WebSocket'; | ||
| 77 | $self->{_OK} = '::ixNet::OK'; | ||
| 78 | $self->{_ERROR} = '::ixNet::ERROR'; | ||
| 79 | $self->{_version} = '9.00.1915.16'; | ||
| 80 | $self->{_noApiKey} = '00000000000000000000000000000000'; | ||
| 81 | $self->{_connectionInfo} = { | ||
| 82 | port => undef, | ||
| 83 | verb => undef, | ||
| 84 | wsVerb => undef, | ||
| 85 | hostname => undef, | ||
| 86 | url => undef, | ||
| 87 | sessionUrl => undef, | ||
| 88 | restUrl => undef, | ||
| 89 | wsUrl => undef, | ||
| 90 | sessionId => undef, | ||
| 91 | backendType => undef, | ||
| 92 | applicationType => undef, | ||
| 93 | closeServerOnDisconnect => undef, | ||
| 94 | serverUsername => undef | ||
| 95 | }; | ||
| 96 | $self->{_initialPort} = undef; | ||
| 97 | $self->{_initialHostname} = undef; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub setDebug { | ||
| 101 | my($self, $debug) = @_; | ||
| 102 | if ($debug) { | ||
| 103 | $self->{_debug} = 1; | ||
| 104 | } else { | ||
| 105 | $self->{_debug} = undef; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | sub getRoot { | ||
| 110 | my($self) = @_; | ||
| 111 | return '::ixNet::OBJ-/'; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub getNull { | ||
| 115 | my($self) = @_; | ||
| 116 | return '::ixNet::OBJ-null'; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub setAsync { | ||
| 120 | my($self) = @_; | ||
| 121 | $self->{_async} = '1'; | ||
| 122 | return $self; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub setTimeout { | ||
| 126 | my($self, $timeout) = @_; | ||
| 127 | if (defined $timeout) { | ||
| 128 | $self->{_timeout} = $timeout; | ||
| 129 | } else { | ||
| 130 | die "A timeout value must be provided.\n"; | ||
| 131 | } | ||
| 132 | return $self; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub getApiKey { | ||
| 136 | my ($self) = shift; | ||
| 137 | my ($hostname) = shift; | ||
| 138 | |||
| 139 | my %defaultArgs = ( | ||
| 140 | -apiKeyFile => 'api.key', | ||
| 141 | -port => 443 | ||
| 142 | ); | ||
| 143 | |||
| 144 | my $sessionArgs = $self->_get_arg_map(\%defaultArgs, @_); | ||
| 145 | my $port = $sessionArgs->{-port}; | ||
| 146 | my $apiKeyFile = $sessionArgs->{-apiKeyFile}; | ||
| 147 | |||
| 148 | if (!$self->_isConnected()) { | ||
| 149 | $self->_createHeaders(); | ||
| 150 | } | ||
| 151 | |||
| 152 | my $username = ''; | ||
| 153 | my $password = ''; | ||
| 154 | if ((!exists $sessionArgs->{-username}) or (!exists $sessionArgs->{-password})) { | ||
| 155 | if (scalar(@_) >= 2) { | ||
| 156 | $username = @_[0]; | ||
| 157 | $password = @_[1]; | ||
| 158 | } | ||
| 159 | } else { | ||
| 160 | $username = $sessionArgs->{-username}; | ||
| 161 | $password = $sessionArgs->{-password}; | ||
| 162 | } | ||
| 163 | my $auth = ''; | ||
| 164 | my $ret = eval { | ||
| 165 | $auth = $self->_restSend('POST', 'https://'.$hostname.':'.$port.'/api/v1/auth/session', {'username' => $username, 'password' => $password}, 180); | ||
| 166 | 1; | ||
| 167 | }; | ||
| 168 | |||
| 169 | if (!$ret and $@ or !$auth) { | ||
| 170 | if ($@ =~ /IxNetAuthenticationError/) { | ||
| 171 | my $msg = 'Unable to get API key from '.$hostname.':'.$port.'. Error: '.$@."\n"; | ||
| 172 | $msg .= "Please check the getApiKey command arguments.\n"; | ||
| 173 | $msg .= "An example of a correct method call is:\n\t"; | ||
| 174 | $msg .= '$ixNet->getApiKey(<hostname>, "-username", <username>, "-password", <password> [,"-port", <443>] [, "-apiKeyFile", <api.key>])'; | ||
| 175 | die $msg."\n"; | ||
| 176 | } else { | ||
| 177 | die 'Unable to get API key from '.$hostname.':'.$port."\n"; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | my $apiKeyPath = ''; | ||
| 182 | if (!File::Spec->file_name_is_absolute($apiKeyFile)) { | ||
| 183 | my $cwdFilePath = File::Spec->rel2abs($apiKeyFile); | ||
| 184 | $apiKeyPath = $self->_tryWriteAPIKey($cwdFilePath, $auth->{apiKey}); | ||
| 185 | if (not defined $apiKeyPath) { | ||
| 186 | $apiKeyPath = $self->_tryWriteAPIKey(File::Spec->rel2abs($apiKeyFile, $libraryFilePath), $auth->{apiKey}); | ||
| 187 | } | ||
| 188 | } else { | ||
| 189 | $apiKeyPath = $self->_tryWriteAPIKey($apiKeyFile, $auth->{apiKey}); | ||
| 190 | } | ||
| 191 | |||
| 192 | if ($apiKeyPath) { | ||
| 193 | $self->_log("The API key was saved at: $apiKeyPath\n"); | ||
| 194 | } else { | ||
| 195 | $self->_log("Could not save API key to disk.\n"); | ||
| 196 | } | ||
| 197 | |||
| 198 | return $auth->{apiKey}; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub getSessions { | ||
| 202 | my($self) = shift; | ||
| 203 | my ($hostname) = shift; | ||
| 204 | |||
| 205 | my $baseURL = ''; | ||
| 206 | my $port = undef; | ||
| 207 | if (!$self->_isConnected()) { | ||
| 208 | if (!$self->{_connectionInfo}->{url}) { | ||
| 209 | my %defaultArgs = ( | ||
| 210 | -apiKey => '', | ||
| 211 | -apiKeyFile => 'api.key', | ||
| 212 | -port => 443 | ||
| 213 | ); | ||
| 214 | my $sessionArgs = $self->_get_arg_map(\%defaultArgs, @_); | ||
| 215 | $self->_createHeaders($sessionArgs->{-apiKey}, $sessionArgs->{-apiKeyFile}); | ||
| 216 | $baseURL = $self->_getBaseURL($hostname, $sessionArgs); | ||
| 217 | $port = $sessionArgs->{-port}; | ||
| 218 | } else { | ||
| 219 | $baseURL = $self->{_connectionInfo}->{url}; | ||
| 220 | $port = $self->{_connectionInfo}->{port} | ||
| 221 | } | ||
| 222 | } else { | ||
| 223 | my %defaultArgs = ( | ||
| 224 | -port => 443 | ||
| 225 | ); | ||
| 226 | my $sessionArgs = $self->_get_arg_map(\%defaultArgs, @_); | ||
| 227 | $port = $sessionArgs->{-port}; | ||
| 228 | $baseURL = $self->{_connectionInfo}->{url}; | ||
| 229 | |||
| 230 | if (($hostname ne $self->{_initialHostname} and $hostname ne $self->{_connectionInfo}->{hostname}) or | ||
| 231 | ($port ne $self->{_initialPort} and $port ne $self->{_connectionInfo}->{port})) { | ||
| 232 | die "A connection has already been established to ".$self->{_connectionInfo}->{hostname}.":".$self->{_connectionInfo}->{port}.". In order to query ".$hostname.":".$port." you must first disconnect.\n"; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | my @response = (); | ||
| 237 | my $response_ref = $self->_restSend('GET', $baseURL); | ||
| 238 | if (ref($response_ref) ne 'ARRAY') { | ||
| 239 | @response = ($response_ref); | ||
| 240 | } else { | ||
| 241 | @response = @$response_ref; | ||
| 242 | } | ||
| 243 | |||
| 244 | my @sessions = (); | ||
| 245 | foreach my $session (@response) { | ||
| 246 | if ((lc $session->{applicationType} eq 'ixnrest') or (lc $self->_tryGetAttr($session, 'backendType', 'LinuxAPIServer' eq 'ixnetwork'))) { | ||
| 247 | push(@sessions, $self->_getDetailedSessionInfo($session, $baseURL, $port)) | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | return @sessions; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub getSessionInfo { | ||
| 255 | my($self) = shift; | ||
| 256 | $self->_isConnected(1); | ||
| 257 | my $session = $self->_restSend('GET', $self->{_connectionInfo}->{sessionUrl}); | ||
| 258 | return $self->_getDetailedSessionInfo($session); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub clearSessions { | ||
| 262 | my ($self) = shift; | ||
| 263 | my ($hostname) = shift; | ||
| 264 | |||
| 265 | my @deleted_sessions = (); | ||
| 266 | my @sessions = $self->getSessions($hostname, @_); | ||
| 267 | foreach my $session (@sessions) { | ||
| 268 | if (($session->{backendType} ne 'ixnetwork') and ($session->{state} eq 'active') and !$self->_parseAsBool($session->{inUse})) { | ||
| 269 | $self->_cleanUpSession($session->{sessionUrl}); | ||
| 270 | push(@deleted_sessions, $session->{sessionUrl}); | ||
| 271 | } | ||
| 272 | } | ||
| 273 | return @deleted_sessions; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub clearSession { | ||
| 277 | my ($self) = shift; | ||
| 278 | my ($hostname) = shift; | ||
| 279 | |||
| 280 | my %defaultArgs = ( | ||
| 281 | -sessionId => '', | ||
| 282 | -force => 0 | ||
| 283 | ); | ||
| 284 | my $operationArgs = $self->_get_arg_map(\%defaultArgs, @_); | ||
| 285 | my $id = $operationArgs->{-sessionId}; | ||
| 286 | if (!$id) { | ||
| 287 | die "IxNetError: A session ID must be provided in order to clear a specific session.\n"; | ||
| 288 | } | ||
| 289 | |||
| 290 | my @sessions = $self->getSessions($hostname, @_); | ||
| 291 | my $sessions = {}; | ||
| 292 | my $session = {}; | ||
| 293 | for my $s (@sessions) { | ||
| 294 | $sessions->{$s->{id}} = $s; | ||
| 295 | } | ||
| 296 | |||
| 297 | if (!exists $sessions->{$id}) { | ||
| 298 | my $enumeratedSessions = join(', ', keys(%{ $sessions })); | ||
| 299 | die 'Session '.$id.' cannot be found in the list of sessions IDs: '.$enumeratedSessions; | ||
| 300 | } else { | ||
| 301 | $session = $sessions->{$id}; | ||
| 302 | } | ||
| 303 | if ($operationArgs->{-force} and ($session->{state} eq 'initial')) { | ||
| 304 | $self->_restSend('POST', $session->{sessionUrl}.'/operations/start'); | ||
| 305 | $self->_waitForState('active', $session->{sessionUrl}); | ||
| 306 | $self->_cleanUpSession($session->{sessionUrl}); | ||
| 307 | return $self->_OK; | ||
| 308 | } elsif (($session->{state} eq 'active') and ($operationArgs->{-force} or !$self->_parseAsBool($session->{inUse}))) { | ||
| 309 | if ($session->{backendType} eq 'ixnetwork') { | ||
| 310 | return "Clearing IxNetwork standalone sessions is not supported."; | ||
| 311 | } elsif ($self->_isConnected() and ($self->{_connectionInfo}->{sessionId} == $id)) { | ||
| 312 | $self->{_connectionInfo}->{closeServerOnDisconnect} = 1; | ||
| 313 | return $self->disconnect(); | ||
| 314 | } else { | ||
| 315 | $self->_cleanUpSession($session->{sessionUrl}); | ||
| 316 | return $self->{_OK}; | ||
| 317 | } | ||
| 318 | } elsif ($operationArgs->{-force} and ($session->{state} eq 'stopped')) { | ||
| 319 | $self->_deleteSession($session->{sessionUrl}); | ||
| 320 | return $self->{_OK}; | ||
| 321 | } | ||
| 322 | |||
| 323 | die 'Session '.$id.' cannot be cleared as it is currently in '.$session->{state}.' state. Please specify -force true if you want to forcefully clear in use sessions.'; | ||
| 324 | } | ||
| 325 | |||
| 326 | sub _isConnected { | ||
| 327 | my ($self, $raiseError) = @_; | ||
| 328 | if (not defined $raiseError) { | ||
| 329 | $raiseError = 0; | ||
| 330 | } | ||
| 331 | |||
| 332 | if (!defined($self->{_websocket})) { | ||
| 333 | if ($raiseError) { | ||
| 334 | die "not connected\n"; | ||
| 335 | } else { | ||
| 336 | return 0; | ||
| 337 | } | ||
| 338 | } else { | ||
| 339 | return 1; | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | sub _get_arg_map { | ||
| 344 | my $self = shift; | ||
| 345 | my $default_args = shift; | ||
| 346 | my $name = undef; | ||
| 347 | foreach my $arg (@_) { | ||
| 348 | if (index($arg, '-') == 0 && not defined $name) { | ||
| 349 | $name = $arg; | ||
| 350 | } elsif (defined $name) { | ||
| 351 | $default_args->{$name} = $arg; | ||
| 352 | $name = undef; | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | return $default_args; | ||
| 357 | } | ||
| 358 | |||
| 359 | sub _restSend { | ||
| 360 | my($self) = shift; | ||
| 361 | my $method = shift; | ||
| 362 | my $url = shift; | ||
| 363 | my $payload = shift; | ||
| 364 | my $timeout = do { my $arg = shift; defined($arg) ? $arg : 0 }; | ||
| 365 | my $json = JSON::PP->new->utf8; | ||
| 366 | |||
| 367 | $self->_log("$method $url\n"); | ||
| 368 | |||
| 369 | my $response = undef; | ||
| 370 | my $returnJson = 1; | ||
| 371 | if (ref($payload) && defined $payload->{file_transfer}) { | ||
| 372 | $returnJson = 0; | ||
| 373 | if ($method eq 'GET') { | ||
| 374 | $response = $self->{_user_agent}->mirror($url, $payload->{filename}); | ||
| 375 | } else { | ||
| 376 | my $content = ''; | ||
| 377 | my $content_length = 0; | ||
| 378 | if (defined $payload->{filename}) { | ||
| 379 | open(my $fid, "<:raw", $payload->{filename}); | ||
| 380 | $content_length = File::stat::stat($payload->{filename})->size(); | ||
| 381 | read($fid, $content, $content_length); | ||
| 382 | close $fid; | ||
| 383 | } elsif (defined $payload->{file_content}) { | ||
| 384 | $content = $json->encode($payload->{file_content}); | ||
| 385 | $content_length = length($content); | ||
| 386 | } | ||
| 387 | $response = $self->{_user_agent}->post($url, | ||
| 388 | Content_Length => $content_length, | ||
| 389 | Content_Type => 'application/octet-stream', | ||
| 390 | Content => $content); | ||
| 391 | } | ||
| 392 | } else { | ||
| 393 | my $request = new HTTP::Request($method => $url, ['Content_Type' => 'application/json']); | ||
| 394 | |||
| 395 | if (defined $payload) { | ||
| 396 | $request->content($json->encode($payload)); | ||
| 397 | } | ||
| 398 | if ($timeout) { | ||
| 399 | $self->{_user_agent}->timeout($timeout); | ||
| 400 | } | ||
| 401 | $response = $self->{_user_agent}->request($request); | ||
| 402 | } | ||
| 403 | |||
| 404 | $self->_log($response->code." ".$response->message."\n"); | ||
| 405 | |||
| 406 | if ($response->code == 204) { | ||
| 407 | return undef; | ||
| 408 | } | ||
| 409 | |||
| 410 | if ($response->is_success) { | ||
| 411 | if($returnJson) { | ||
| 412 | # The opposite of encode: expects a JSON text and tries to parse it, | ||
| 413 | # returning the resulting simple scalar or reference. Croaks on error. | ||
| 414 | # JSON numbers and strings become simple Perl scalars. JSON arrays become | ||
| 415 | # Perl arrayrefs and JSON objects become Perl hashrefs. true becomes 1 (JSON::true), | ||
| 416 | # false becomes 0 (JSON::false) and null becomes undef. | ||
| 417 | my $ret = eval { | ||
| 418 | $response = $json->decode($response->decoded_content); | ||
| 419 | 1; | ||
| 420 | }; | ||
| 421 | if (!$ret and $@) { | ||
| 422 | die "Connection handshake failed - invalid JSON response.\n"; | ||
| 423 | } | ||
| 424 | return $response; | ||
| 425 | } else { | ||
| 426 | return $response->code; | ||
| 427 | } | ||
| 428 | } elsif ($response->code == 307) { | ||
| 429 | if (($url =~ qr/(?<baseUrl>https?:\/\/[^\/]+:\d+)/) || ($url =~ qr/(?<baseUrl>https?:\/\/[^\/]+)/)) { | ||
| 430 | $url = $+{baseUrl}.$response->header('Location'); | ||
| 431 | } | ||
| 432 | return $self->_restSend($method, $url, $payload); | ||
| 433 | } else { | ||
| 434 | my $errorsText = ''; | ||
| 435 | my $ret = eval { | ||
| 436 | my $errorsContent = $json->decode($response->decoded_content); | ||
| 437 | if (exists $errorsContent->{error}) { | ||
| 438 | $errorsText = $errorsContent->{error}; | ||
| 439 | } elsif (exists $errorsContent->{errors}) { | ||
| 440 | $errorsText = join(', ', @{ $errorsContent->{errors} }); | ||
| 441 | } | ||
| 442 | 1; | ||
| 443 | }; | ||
| 444 | if (!$ret and $@) { | ||
| 445 | $errorsText = $response->message; | ||
| 446 | } | ||
| 447 | if (($response->code == 401) or ($response->code == 403)) { | ||
| 448 | die 'IxNetAuthenticationError: '.$errorsText."\n"; | ||
| 449 | } else { | ||
| 450 | die $response->code." ".$errorsText."\n"; | ||
| 451 | } | ||
| 452 | } | ||
| 453 | } | ||
| 454 | |||
| 455 | sub _createHeaders { | ||
| 456 | my $self = shift; | ||
| 457 | my $apiKey = do { my $arg = shift; defined($arg) ? $arg : '' }; | ||
| 458 | my $apiKeyFile = do { my $arg = shift; defined($arg) ? $arg : '' }; | ||
| 459 | |||
| 460 | my $apiKeyValue = $self->{_noApiKey}; | ||
| 461 | |||
| 462 | if ($apiKey) { | ||
| 463 | $apiKeyValue = $apiKey; | ||
| 464 | } elsif ($apiKeyFile) { | ||
| 465 | my $apiKeyFile = $apiKeyFile; | ||
| 466 | if (!File::Spec->file_name_is_absolute($apiKeyFile)) { | ||
| 467 | my $cwdFilePath = File::Spec->rel2abs($apiKeyFile); | ||
| 468 | $apiKeyValue = $self->_tryReadAPIKey($cwdFilePath); | ||
| 469 | if (not defined $apiKeyValue) { | ||
| 470 | $apiKeyValue = $self->_tryReadAPIKey(File::Spec->rel2abs($apiKeyFile, $libraryFilePath)); | ||
| 471 | } | ||
| 472 | } else { | ||
| 473 | $apiKeyValue = $self->_tryReadAPIKey($apiKeyFile); | ||
| 474 | } | ||
| 475 | } | ||
| 476 | $self->{_user_agent} = new LWP::UserAgent(); | ||
| 477 | $self->{_user_agent}->protocols_allowed(['https', 'http']); | ||
| 478 | $self->{_user_agent}->ssl_opts(verify_hostname => 0); | ||
| 479 | $self->{_user_agent}->default_header('X-Api-Key' => $apiKeyValue); | ||
| 480 | $self->{_user_agent}->default_header('IxNetwork-Lib' => 'IxNetwork perl client v.'.$self->{_version}); | ||
| 481 | } | ||
| 482 | |||
| 483 | sub connect { | ||
| 484 | my ($self) = shift; | ||
| 485 | my ($hostname) = shift; | ||
| 486 | |||
| 487 | my %defaultArgs = ( | ||
| 488 | -sessionId => 0, | ||
| 489 | -clientId => 'perl', | ||
| 490 | -version => '5.30', | ||
| 491 | -connectTimeout => 450, | ||
| 492 | -allowOnlyOneConnection => 0, | ||
| 493 | -apiKey => '', | ||
| 494 | -apiKeyFile => 'api.key', | ||
| 495 | -product => 'ixnrest', | ||
| 496 | -clientusername => getlogin(), | ||
| 497 | -serverusername => '' | ||
| 498 | ); | ||
| 499 | |||
| 500 | my $connectArgs = $self->_get_arg_map(\%defaultArgs, @_); | ||
| 501 | $connectArgs->{-allowOnlyOneConnection} = $self->_parseAsBool($connectArgs->{-allowOnlyOneConnection}); | ||
| 502 | |||
| 503 | if ($self->_isConnected()) { | ||
| 504 | my $port = 443; | ||
| 505 | if (exists $connectArgs->{-port}) { | ||
| 506 | $port = $connectArgs->{-port}; | ||
| 507 | } | ||
| 508 | if (($hostname ne $self->{_initialHostname} and $hostname ne $self->{_connectionInfo}->{hostname}) or | ||
| 509 | ($port ne $self->{_initialPort} and $port ne $self->{_connectionInfo}->{port})) { | ||
| 510 | return "Cannot connect to ".$hostname.':'.$port." as a connection is already established to ".$self->{_connectionInfo}->{hostname}.':'.$self->{_connectionInfo}->{port}.". Please execute disconnect before trying this command again."; | ||
| 511 | } elsif ($connectArgs->{-sessionId} and $connectArgs->{-sessionId} != $self->{_connectionInfo}->{sessionId}) { | ||
| 512 | return "Cannot connect to session ".$connectArgs->{-sessionId}." as a connection is already established to session ".$self->{_connectionInfo}->{sessionId}.". Please execute disconnect before trying this command again."; | ||
| 513 | } elsif ($connectArgs->{-serverusername} and $self->{_connectionInfo}->{backendType} ne 'ixnetwork' and $connectArgs->{-serverusername} ne $self->{_connectionInfo}->{serverUsername}) { | ||
| 514 | return "Cannot connect to a session associated with ".$connectArgs->{-serverusername}." as a connection is already established to a session associated with ".$self->{_connectionInfo}->{serverUsername}.". Please execute disconnect before trying this command again."; | ||
| 515 | } else { | ||
| 516 | return $self->{_OK}; | ||
| 517 | } | ||
| 518 | } | ||
| 519 | |||
| 520 | $self->_createHeaders($connectArgs->{-apiKey}, $connectArgs->{-apiKeyFile}); | ||
| 521 | |||
| 522 | $self->_getBaseURL($hostname, $connectArgs, 1); | ||
| 523 | my $result = undef; | ||
| 524 | my $ret = eval { | ||
| 525 | my $session = {}; | ||
| 526 | if (($connectArgs->{-sessionId}) < 1 and !$connectArgs->{-serverusername}) { | ||
| 527 | $session = $self->_restSend('POST', $self->{_connectionInfo}->{url}, {applicationType => $connectArgs->{-product}}, $connectArgs->{-connectTimeout}); | ||
| 528 | } else { | ||
| 529 | my @sessions = $self->getSessions($self->{_initialHostname}, @_); | ||
| 530 | my $sessions = {}; | ||
| 531 | for my $s (@sessions) { | ||
| 532 | $sessions->{$s->{id}} = $s; | ||
| 533 | } | ||
| 534 | if ($connectArgs->{-serverusername}) { | ||
| 535 | my $matchedSessions = {}; | ||
| 536 | while(my($k, $v) = each %{ $sessions }) { | ||
| 537 | if ((lc $v->{userName}) eq (lc $connectArgs->{-serverusername})) { | ||
| 538 | $matchedSessions->{$k} = $v; | ||
| 539 | } | ||
| 540 | } | ||
| 541 | $sessions = $matchedSessions; | ||
| 542 | if (keys %{ $sessions } == 0) { | ||
| 543 | die 'There are no sessions available with the serverusername '.$connectArgs->{-serverusername}."\n"; | ||
| 544 | } | ||
| 545 | if ($connectArgs->{-sessionId} < 1) { | ||
| 546 | if (keys %{ $sessions } > 1) { | ||
| 547 | die 'There are multiple sessions available with the serverusername '.$connectArgs->{-serverusername}.'. Please specify -sessionId also.'."\n"; | ||
| 548 | } else { | ||
| 549 | my @keys = keys %{ $sessions }; | ||
| 550 | $connectArgs->{-sessionId} = @keys[0]; | ||
| 551 | } | ||
| 552 | } | ||
| 553 | } | ||
| 554 | if (!exists $sessions->{$connectArgs->{-sessionId}}) { | ||
| 555 | die "Invalid sessionId value ($connectArgs->{-sessionId}).\n"; | ||
| 556 | } | ||
| 557 | $session = $sessions->{$connectArgs->{-sessionId}}; | ||
| 558 | if ($session->{inUse} and (index($connectArgs->{-clientId}, 'HLAPI') == -1)) { | ||
| 559 | if ((lc $self->_tryGetAttr($session, 'backendType', 'LinuxAPIServer') eq 'connectionmanager') or $connectArgs->{-allowOnlyOneConnection}) { | ||
| 560 | $self->{_connectionInfo}->{closeServerOnDisconnect} = 0; | ||
| 561 | die "The requested session is currently in use.\n"; | ||
| 562 | } else { | ||
| 563 | print 'Warning: you are connecting to session '.$session->{id}." which is in use.\n"; | ||
| 564 | } | ||
| 565 | } | ||
| 566 | } | ||
| 567 | |||
| 568 | $self->{_connectionInfo}->{applicationType} = $session->{applicationType}; | ||
| 569 | if (!exists $connectArgs->{-closeServerOnDisconnect}) { | ||
| 570 | if ($self->{_connectionInfo}->{applicationType} eq 'ixnrest') { | ||
| 571 | $connectArgs->{-closeServerOnDisconnect} = 'true'; | ||
| 572 | } else { | ||
| 573 | $connectArgs->{-closeServerOnDisconnect} = 'false'; | ||
| 574 | } | ||
| 575 | } else { | ||
| 576 | if ($self->_parseAsBool($connectArgs->{-closeServerOnDisconnect})) { | ||
| 577 | $connectArgs->{-closeServerOnDisconnect} = 'true'; | ||
| 578 | } else { | ||
| 579 | $connectArgs->{-closeServerOnDisconnect} = 'false'; | ||
| 580 | } | ||
| 581 | } | ||
| 582 | $self->{_connectionInfo}->{closeServerOnDisconnect} = $self->_parseAsBool($connectArgs->{-closeServerOnDisconnect}); | ||
| 583 | $self->{_connectionInfo}->{sessionId} = $session->{id}; | ||
| 584 | $self->{_connectionInfo}->{sessionUrl} = $self->{_connectionInfo}->{url}.'/'.$self->{_connectionInfo}->{sessionId}; | ||
| 585 | $self->{_connectionInfo}->{backendType} = $self->_tryGetAttr($session, 'backendType', 'LinuxAPIServer'); | ||
| 586 | $self->{_connectionInfo}->{wsUrl} = $self->{_connectionInfo}->{wsVerb}.'://'.$self->{_connectionInfo}->{hostname}.':'.$self->{_connectionInfo}->{port}.'/ixnetworkweb/ixnrest/ws/api/v1/sessions/'.$self->{_connectionInfo}->{sessionId}.'/ixnetwork/globals/ixnet?closeServerOnDisconnect='.$self->{_connectionInfo}->{closeServerOnDisconnect}.'&clientType='.$connectArgs->{-clientId}.'&clientUsername='.$connectArgs->{'-clientusername'}; | ||
| 587 | $self->{_connectionInfo}->{restUrl} = $self->{_connectionInfo}->{sessionUrl}.'/ixnetwork'; | ||
| 588 | $self->{_connectionInfo}->{serverUsername} = $session->{userName}; | ||
| 589 | |||
| 590 | if ((lc($session->{state}) eq 'initial') or (lc($session->{state}) eq 'stopped')) { | ||
| 591 | $self->_restSend('POST', $self->{_connectionInfo}->{sessionUrl}.'/operations/start', {applicationType => $connectArgs->{-product}}, $connectArgs->{-connectTimeout}); | ||
| 592 | } | ||
| 593 | $self->_waitForState('active', $self->{_connectionInfo}->{sessionUrl}, $connectArgs->{-connectTimeout}); | ||
| 594 | |||
| 595 | if ($self->_parseAsBool($connectArgs->{-allowOnlyOneConnection})) { | ||
| 596 | $self->_isSessionAvailable($session, 1); | ||
| 597 | } | ||
| 598 | |||
| 599 | if ($self->{_connectionInfo}->{wsVerb} eq 'wss') { | ||
| 600 | $self->{_websocket} = new IO::Socket::SSL( | ||
| 601 | PeerHost => $self->{_connectionInfo}->{hostname}.':'.$self->{_connectionInfo}->{port}, | ||
| 602 | PeerPort => $self->{_connectionInfo}->{verb}, | ||
| 603 | SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE | ||
| 604 | ) or die "IO::Socket::SSL reported: $! (ssl_error=$SSL_ERROR)"; | ||
| 605 | } else { | ||
| 606 | $self->{_websocket} = new IO::Socket::INET( | ||
| 607 | PeerAddr => $self->{_connectionInfo}->{hostname}.':'.$self->{_connectionInfo}->{port}, | ||
| 608 | PeerPort => $self->{_connectionInfo}->{verb}, | ||
| 609 | ) or die "IO::Socket::INET reported: $!"; | ||
| 610 | } | ||
| 611 | |||
| 612 | $self->{_ws_client} = new Protocol::WebSocket::Client(url => $self->{_connectionInfo}->{wsUrl}, max_fragments_amount=> 10240, max_payload_size=> $self->{_serverPayloadSize}); | ||
| 613 | $self->{_ws_client}->on(write => sub { | ||
| 614 | my ($ws) = shift; | ||
| 615 | my $buffer = shift; | ||
| 616 | $self->ws_write($ws, $buffer); | ||
| 617 | }); | ||
| 618 | $self->{_ws_client}->on(read => sub { | ||
| 619 | my ($ws) = shift; | ||
| 620 | my $buffer = shift; | ||
| 621 | $self->ws_read($ws, $buffer); | ||
| 622 | }); | ||
| 623 | $self->{_ws_client}->connect; | ||
| 624 | |||
| 625 | $result = $self->_send_recv('ixNet', 'connect', | ||
| 626 | '-version', $connectArgs->{'-version'}, | ||
| 627 | '-clientType', 'perl', | ||
| 628 | '-clientId', $connectArgs->{-clientId}, | ||
| 629 | '-clientUsername', $connectArgs->{'-clientusername'}, | ||
| 630 | '-closeServerOnDisconnect', $connectArgs->{-closeServerOnDisconnect} , | ||
| 631 | '-apiKey', $self->{_user_agent}{def_headers}{"x-api-key"}); | ||
| 632 | $self->_check_client_version(); | ||
| 633 | 1; | ||
| 634 | }; | ||
| 635 | if (!$ret and $@) { | ||
| 636 | if ($self->{_connectionInfo}->{sessionUrl} and $self->{_connectionInfo}->{closeServerOnDisconnect}) { | ||
| 637 | $self->_cleanUpSession($self->{_connectionInfo}->{sessionUrl}); | ||
| 638 | } | ||
| 639 | $self->_close(); | ||
| 640 | $self->_deleteSession($self->{_connectionInfo}->{sessionUrl}); | ||
| 641 | my $failedConnectionPort; | ||
| 642 | if (exists $connectArgs->{-port}) { | ||
| 643 | $failedConnectionPort = $connectArgs->{-port}; | ||
| 644 | } else { | ||
| 645 | $failedConnectionPort = 443; | ||
| 646 | } | ||
| 647 | $self->_setDefaults(); | ||
| 648 | die 'Unable to connect to '.$hostname.':'.$failedConnectionPort.'. Error: '.$self->{_ERROR}.': '.$@."\n"; | ||
| 649 | } else { | ||
| 650 | return $result; | ||
| 651 | } | ||
| 652 | } | ||
| 653 | |||
| 654 | sub ws_write { | ||
| 655 | my ($self) = shift; | ||
| 656 | my $ws = shift; | ||
| 657 | my $buffer = shift; | ||
| 658 | my $paddingSize = 4; | ||
| 659 | $self->{_recvBuffer} = ''; | ||
| 660 | my $ret = eval { | ||
| 661 | $self->{_websocket}->write($buffer); | ||
| 662 | my $recv_buffer = ''; | ||
| 663 | |||
| 664 | while (1) { | ||
| 665 | my $bytes = ''; | ||
| 666 | sysread($self->{_websocket}, $bytes, $self->{_serverPayloadSize}); | ||
| 667 | $recv_buffer .= $bytes; | ||
| 668 | if (not $self->{_ws_client}->{hs}->is_done) { | ||
| 669 | last; | ||
| 670 | } | ||
| 671 | my $startIndex = index($recv_buffer, '<009'); | ||
| 672 | my $stopIndex = index($recv_buffer, '>', $startIndex); | ||
| 673 | if ($startIndex != -1 && $stopIndex != -1) { | ||
| 674 | my $contentLength = int(substr($recv_buffer, $startIndex + $paddingSize, $stopIndex)); | ||
| 675 | if (length($recv_buffer) >= (($stopIndex + $contentLength +1 - $paddingSize) / $self->{_serverPayloadSize}) * ($self->{_serverPayloadSize} + $paddingSize)) { | ||
| 676 | last; | ||
| 677 | } | ||
| 678 | } | ||
| 679 | } | ||
| 680 | $self->{_ws_client}->read($recv_buffer); | ||
| 681 | 1; | ||
| 682 | }; | ||
| 683 | if (!$ret and $@) { | ||
| 684 | $self->_close(); | ||
| 685 | die 'Connection to the remote IxNetwork instance has been closed: '.$@."\n"; | ||
| 686 | } | ||
| 687 | } | ||
| 688 | |||
| 689 | sub ws_read { | ||
| 690 | my ($self) = shift; | ||
| 691 | my $ws = shift; | ||
| 692 | $self->{_recvBuffer} = shift; | ||
| 693 | } | ||
| 694 | |||
| 695 | sub disconnect { | ||
| 696 | my($self) = @_; | ||
| 697 | |||
| 698 | if ($self->_isConnected()) { | ||
| 699 | $self->_close(); | ||
| 700 | # bye bye $self->_cleanUpSession() forever | ||
| 701 | #if ($self->{_connectionInfo}->{closeServerOnDisconnect}) { | ||
| 702 | # $self->_cleanUpSession($self->{_connectionInfo}->{sessionUrl}); | ||
| 703 | #} | ||
| 704 | $self->_setDefaults(); | ||
| 705 | return $self->{_OK}; | ||
| 706 | } else { | ||
| 707 | return "not connected"; | ||
| 708 | } | ||
| 709 | } | ||
| 710 | |||
| 711 | sub _cleanUpSession { | ||
| 712 | my ($self, $url) = @_; | ||
| 713 | |||
| 714 | eval { | ||
| 715 | $self->_restSend('POST', $url.'/operations/stop'); | ||
| 716 | $self->_waitForState('stopped', $url); | ||
| 717 | }; | ||
| 718 | $self->_deleteSession($url); | ||
| 719 | } | ||
| 720 | |||
| 721 | sub _deleteSession { | ||
| 722 | my ($self, $url) = @_; | ||
| 723 | |||
| 724 | eval { | ||
| 725 | $self->_restSend('DELETE', $url); | ||
| 726 | }; | ||
| 727 | } | ||
| 728 | |||
| 729 | sub _waitForState { | ||
| 730 | my ($self, $state, $url, $timeout) = @_; | ||
| 731 | |||
| 732 | if (not defined $timeout) { | ||
| 733 | $timeout = 450; | ||
| 734 | } | ||
| 735 | |||
| 736 | my $startTime = time; | ||
| 737 | my $sessionState = ''; | ||
| 738 | my $resp = {}; | ||
| 739 | |||
| 740 | while ((time - $startTime) < $timeout) { | ||
| 741 | my $ret = eval { | ||
| 742 | $resp = $self->_restSend('GET', $url); | ||
| 743 | $sessionState = lc($resp->{state}); | ||
| 744 | 1; | ||
| 745 | }; | ||
| 746 | if (!$ret and $@) { | ||
| 747 | $self->_log($@); | ||
| 748 | die; | ||
| 749 | } | ||
| 750 | if (($sessionState eq $state) or ($state eq 'stopped' and ($sessionState eq 'initial' or $sessionState eq 'abnormallystopped'))) { | ||
| 751 | return; | ||
| 752 | } elsif (($state eq 'active' and ($sessionState eq 'stopped' or $sessionState eq 'stopping' or $sessionState eq 'abnormallystopped')) or | ||
| 753 | ($state eq 'stopped' and ($sessionState eq 'starting' or $sessionState eq 'active'))) { | ||
| 754 | die 'Session '.$self->{_connectionInfo}->{id}.' was expected to reach state '.$state.'. It reached the invalid state '.$sessionState.".\n"; | ||
| 755 | } | ||
| 756 | sleep(1.5); | ||
| 757 | } | ||
| 758 | |||
| 759 | } | ||
| 760 | |||
| 761 | sub help { | ||
| 762 | my($self) = @_; | ||
| 763 | my @args = @_[1..scalar(@_) - 1]; | ||
| 764 | |||
| 765 | return $self->_send_recv('ixNet', 'help', @args); | ||
| 766 | } | ||
| 767 | |||
| 768 | sub setSessionParameter { | ||
| 769 | my($self) = @_; | ||
| 770 | my @args = @_[1..scalar(@_) - 1]; | ||
| 771 | |||
| 772 | if (scalar(@args) % 2 == 0) { | ||
| 773 | return $self->_send_recv('ixNet', 'setSessionParameter', @args); | ||
| 774 | } else { | ||
| 775 | die "setSessionParameter requires an even number of name/value pairs\n"; | ||
| 776 | } | ||
| 777 | } | ||
| 778 | |||
| 779 | sub getVersion { | ||
| 780 | my($self) = shift; | ||
| 781 | |||
| 782 | if ($self->_isConnected()) { | ||
| 783 | return $self->_send_recv('ixNet', 'getVersion'); | ||
| 784 | } else { | ||
| 785 | return $self->{_version}; | ||
| 786 | } | ||
| 787 | } | ||
| 788 | |||
| 789 | sub getParent { | ||
| 790 | my($self) = @_; | ||
| 791 | |||
| 792 | return $self->_send_recv('ixNet', 'getParent', $_[1]); | ||
| 793 | } | ||
| 794 | |||
| 795 | sub exists { | ||
| 796 | my($self) = @_; | ||
| 797 | |||
| 798 | return $self->_send_recv('ixNet', 'exists', $_[1]); | ||
| 799 | } | ||
| 800 | |||
| 801 | sub commit { | ||
| 802 | my($self) = @_; | ||
| 803 | |||
| 804 | return $self->_send_recv('ixNet', 'commit'); | ||
| 805 | } | ||
| 806 | |||
| 807 | sub rollback { | ||
| 808 | my($self) = @_; | ||
| 809 | |||
| 810 | return $self->_send_recv('ixNet', 'rollback'); | ||
| 811 | } | ||
| 812 | |||
| 813 | sub execute { | ||
| 814 | my($self) = @_; | ||
| 815 | my @args = @_[1..scalar(@_) - 1]; | ||
| 816 | |||
| 817 | return $self->_send_recv('ixNet', 'exec', @args); | ||
| 818 | } | ||
| 819 | |||
| 820 | sub add { | ||
| 821 | my($self) = @_; | ||
| 822 | my @args = @_[1..scalar(@_) - 1]; | ||
| 823 | |||
| 824 | return $self->_send_recv('ixNet', 'add', @args); | ||
| 825 | } | ||
| 826 | |||
| 827 | sub remove { | ||
| 828 | my($self) = @_; | ||
| 829 | my @args = @_[1..scalar(@_) - 1]; | ||
| 830 | |||
| 831 | return $self->_send_recv('ixNet', 'remove', @args); | ||
| 832 | } | ||
| 833 | |||
| 834 | sub setAttribute { | ||
| 835 | my($self) = @_; | ||
| 836 | my @args = @_[1..scalar(@_) - 1]; | ||
| 837 | $self->{_buffer} = 1; | ||
| 838 | |||
| 839 | return$self->_send_recv('ixNet', 'setAttribute', @args); | ||
| 840 | } | ||
| 841 | |||
| 842 | sub setMultiAttribute { | ||
| 843 | my($self) = @_; | ||
| 844 | my @args = @_[1..scalar(@_) - 1]; | ||
| 845 | $self->{_buffer} = 1; | ||
| 846 | |||
| 847 | return $self->_send_recv('ixNet', 'setMultiAttribute', @args); | ||
| 848 | } | ||
| 849 | |||
| 850 | sub getAttribute { | ||
| 851 | my($self) = @_; | ||
| 852 | my @args = @_[1..scalar(@_) - 1]; | ||
| 853 | |||
| 854 | return $self->_send_recv('ixNet', 'getAttribute', @args) | ||
| 855 | } | ||
| 856 | |||
| 857 | sub getList { | ||
| 858 | my($self) = @_; | ||
| 859 | my @args = @_[1..scalar(@_) - 1]; | ||
| 860 | |||
| 861 | return $self->_send_recv('ixNet', 'getList', @args); | ||
| 862 | } | ||
| 863 | |||
| 864 | sub getFilteredList { | ||
| 865 | my($self) = @_; | ||
| 866 | my @args = @_[1..scalar(@_) - 1]; | ||
| 867 | |||
| 868 | return $self->_send_recv('ixNet', 'getFilteredList', @args); | ||
| 869 | } | ||
| 870 | |||
| 871 | sub adjustIndexes { | ||
| 872 | my($self) = @_; | ||
| 873 | my @args = @_[1..scalar(@_) - 1]; | ||
| 874 | |||
| 875 | return $self->_send_recv('ixNet', 'adjustIndexes', @args); | ||
| 876 | } | ||
| 877 | |||
| 878 | sub remapIds { | ||
| 879 | my($self) = @_; | ||
| 880 | my @args = @_[1..scalar(@_) - 1]; | ||
| 881 | |||
| 882 | return $self->_send_recv('ixNet', 'remapIds', @args); | ||
| 883 | } | ||
| 884 | |||
| 885 | sub getResult { | ||
| 886 | my($self) = @_; | ||
| 887 | my @args = @_[1..scalar(@_) - 1]; | ||
| 888 | |||
| 889 | return $self->_send_recv('ixNet', 'getResult', @args); | ||
| 890 | } | ||
| 891 | |||
| 892 | sub wait { | ||
| 893 | my($self) = @_; | ||
| 894 | my @args = @_[1..scalar(@_) - 1]; | ||
| 895 | |||
| 896 | return $self->_send_recv('ixNet', 'wait', @args); | ||
| 897 | } | ||
| 898 | |||
| 899 | sub isDone { | ||
| 900 | my($self) = @_; | ||
| 901 | my @args = @_[1..scalar(@_) - 1]; | ||
| 902 | |||
| 903 | return $self->_send_recv('ixNet', 'isDone', @args); | ||
| 904 | } | ||
| 905 | |||
| 906 | sub isSuccess { | ||
| 907 | my($self) = @_; | ||
| 908 | my @args = @_[1..scalar(@_) - 1]; | ||
| 909 | |||
| 910 | return $self->_send_recv('ixNet', 'isSuccess', @args); | ||
| 911 | } | ||
| 912 | |||
| 913 | sub xpath { | ||
| 914 | my($self) = @_; | ||
| 915 | my @args = @_[1..scalar(@_) - 1]; | ||
| 916 | |||
| 917 | return $self->_send_recv('ixNet', 'xpath', @args); | ||
| 918 | } | ||
| 919 | |||
| 920 | sub writeTo { | ||
| 921 | my($self) = shift; | ||
| 922 | my $filename = shift; | ||
| 923 | my @args = @_; | ||
| 924 | |||
| 925 | my $relativeFile = 0; | ||
| 926 | for (@args) { | ||
| 927 | if ($_ eq '-ixNetRelative') { | ||
| 928 | $relativeFile = 1; | ||
| 929 | last; | ||
| 930 | } | ||
| 931 | } | ||
| 932 | if ($relativeFile) { | ||
| 933 | return $self->_send_recv('ixNet', 'writeTo', $filename, @args); | ||
| 934 | } else { | ||
| 935 | return $self->_create_file_on_server($filename); | ||
| 936 | } | ||
| 937 | } | ||
| 938 | |||
| 939 | sub readFrom { | ||
| 940 | my($self) = shift; | ||
| 941 | my $filename = shift; | ||
| 942 | my @args = @_; | ||
| 943 | |||
| 944 | my $relativeFile = 0; | ||
| 945 | for (@args) { | ||
| 946 | if ($_ eq '-ixNetRelative') { | ||
| 947 | $relativeFile = 1; | ||
| 948 | last; | ||
| 949 | } | ||
| 950 | } | ||
| 951 | if ($relativeFile) { | ||
| 952 | return $self->_send_recv('ixNet', 'readFrom', $filename, @args); | ||
| 953 | } else { | ||
| 954 | return $self->_put_file_on_server($filename); | ||
| 955 | } | ||
| 956 | } | ||
| 957 | |||
| 958 | sub _isSessionAvailable { | ||
| 959 | my ($self, $session, $raiseError) = @_; | ||
| 960 | |||
| 961 | my $sessionInformation = $self->_getDetailedSessionInfo($session); | ||
| 962 | if ($sessionInformation->{inUse}) { | ||
| 963 | if ($raiseError) { | ||
| 964 | die "The requested session is currently in use.\n"; | ||
| 965 | } | ||
| 966 | return 0; | ||
| 967 | } | ||
| 968 | return 1; | ||
| 969 | } | ||
| 970 | |||
| 971 | sub _getDetailedSessionInfo { | ||
| 972 | my ($self, $session, $baseURL, $port) = @_; | ||
| 973 | if (not defined $baseURL) { | ||
| 974 | $baseURL = $self->{_connectionInfo}->{url}; | ||
| 975 | $port = $self->{_connectionInfo}->{port}; | ||
| 976 | } | ||
| 977 | |||
| 978 | my $sessionURL = $baseURL."/".$session->{id}; | ||
| 979 | my $sessionIxNetworkURL = $sessionURL."/ixnetwork"; | ||
| 980 | my $ixnet = undef; | ||
| 981 | |||
| 982 | if (lc $session->{state} eq 'active'){ | ||
| 983 | eval { | ||
| 984 | my $payload = undef; | ||
| 985 | $ixnet = $self->_restSend('GET', $sessionIxNetworkURL."/globals/ixnet", $payload, 180); | ||
| 986 | }; | ||
| 987 | } | ||
| 988 | if (!$ixnet) { | ||
| 989 | $ixnet->{isActive} = 'false'; | ||
| 990 | $ixnet->{connectedClients} = []; | ||
| 991 | } | ||
| 992 | my $inUse = $self->_parseAsBool($ixnet->{isActive} or ($session->{subState} and (substr(lc $session->{subState}, 0, length('in use')) eq 'in use'))); | ||
| 993 | return { | ||
| 994 | id => $session->{id}, | ||
| 995 | port => $port, | ||
| 996 | url => $sessionIxNetworkURL, | ||
| 997 | sessionUrl => $sessionURL, | ||
| 998 | applicationType => $session->{applicationType}, | ||
| 999 | backendType => $self->_tryGetAttr($session, 'backendType', 'LinuxAPIServer'), | ||
| 1000 | state => lc $session->{state}, | ||
| 1001 | subState => $session->{subState}, | ||
| 1002 | inUse => $inUse, | ||
| 1003 | userName => $session->{userName}, | ||
| 1004 | connectedClients => $ixnet->{connectedClients}, | ||
| 1005 | createdOn => $session->{createdOn}, | ||
| 1006 | startedOn => $self->_tryGetAttr($session, 'startedOn', ''), | ||
| 1007 | currentTime => $self->_tryGetAttr($session, 'currentTime', ''), | ||
| 1008 | stoppedOn => $self->_tryGetAttr($session, 'stoppedOn', '') | ||
| 1009 | }; | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | sub _normalizeFilePaths { | ||
| 1013 | my ($self) = shift; | ||
| 1014 | my $path = shift; | ||
| 1015 | while (index($path, '\\') != -1) { | ||
| 1016 | my $i = index($path, '\\'); | ||
| 1017 | substr($path, $i, 1) = '/'; | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | return $path; | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | sub _put_file_on_server { | ||
| 1024 | my($self) = shift; | ||
| 1025 | my $fileName = shift; | ||
| 1026 | my ($baseName, $dirs, $suffix) = File::Basename::fileparse($self->_normalizeFilePaths($fileName)); | ||
| 1027 | my $files = $self->_restSend('GET', $self->{_connectionInfo}->{sessionUrl}.'/ixnetwork/files'); | ||
| 1028 | my $absoluteFileName = $self->_normalizeFilePaths($files->{'absolute'}); | ||
| 1029 | my $remoteFilename = $absoluteFileName.'/'.$baseName; | ||
| 1030 | $self->_restSend('POST', $self->{_connectionInfo}->{sessionUrl}.'/ixnetwork/files?filename='.URI::Escape::uri_escape($baseName), {file_transfer => '1', filename => $fileName}); | ||
| 1031 | return $self->_send_recv('ixNet', 'readFrom', $remoteFilename, '-ixNetRelative'); | ||
| 1032 | } | ||
| 1033 | |||
| 1034 | sub _create_file_on_server { | ||
| 1035 | my($self) = shift; | ||
| 1036 | my $fileName = shift; | ||
| 1037 | my ($baseName, $dirs, $suffix) = File::Basename::fileparse($self->_normalizeFilePaths($fileName)); | ||
| 1038 | my $files = $self->_restSend('GET', $self->{_connectionInfo}->{sessionUrl}.'/ixnetwork/files'); | ||
| 1039 | my $absoluteFileName = $self->_normalizeFilePaths($files->{'absolute'}); | ||
| 1040 | my $remoteFilename = $absoluteFileName.'/'.$baseName; | ||
| 1041 | $self->_restSend('POST', $self->{_connectionInfo}->{sessionUrl}.'/ixnetwork/files?filename='.URI::Escape::uri_escape($baseName), {file_transfer => '1', file_content => {}}); | ||
| 1042 | return $self->_send_recv('ixNet', 'writeTo', $remoteFilename, '-ixNetRelative', '-overwrite', '-remote', $fileName); | ||
| 1043 | } | ||
| 1044 | |||
| 1045 | sub _close { | ||
| 1046 | my($self) = shift; | ||
| 1047 | if ($self->{_websocket}) { | ||
| 1048 | close($self->{_websocket}); | ||
| 1049 | } | ||
| 1050 | $self->{_websocket} = undef; | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | sub _join { | ||
| 1054 | my($self) = @_; | ||
| 1055 | my @args = @_[1..scalar(@_) - 1]; | ||
| 1056 | |||
| 1057 | foreach my $arg (@args) { | ||
| 1058 | if (ref($arg) eq 'ARRAY') { | ||
| 1059 | if ($self->{_addContentSeparator} == 0) { | ||
| 1060 | $self->{_sendContent} .= chr(0x02); | ||
| 1061 | } | ||
| 1062 | if ($self->{_addContentSeparator} > 0) { | ||
| 1063 | $self->{_sendContent} .= '{'; | ||
| 1064 | } | ||
| 1065 | $self->{_addContentSeparator} += 1; | ||
| 1066 | $self->{_firstItem} = 1; | ||
| 1067 | if (scalar(@$arg) == 0) { | ||
| 1068 | $self->{_sendContent} .= '{}'; | ||
| 1069 | } else { | ||
| 1070 | foreach my $item ($arg) { | ||
| 1071 | $self->_join(@$item); | ||
| 1072 | } | ||
| 1073 | } | ||
| 1074 | if ($self->{_addContentSeparator} > 1) { | ||
| 1075 | $self->{_sendContent} .= '}'; | ||
| 1076 | } | ||
| 1077 | $self->{_addContentSeparator} -= 1; | ||
| 1078 | } else { | ||
| 1079 | if ($self->{_addContentSeparator} == 0 and length($self->{_sendContent}) > 0) { | ||
| 1080 | $self->{_sendContent} .= chr(0x02); | ||
| 1081 | } elsif ($self->{_addContentSeparator} > 0) { | ||
| 1082 | if (not defined $self->{_firstItem}) { | ||
| 1083 | $self->{_sendContent} .= ' '; | ||
| 1084 | } else { | ||
| 1085 | $self->{_firstItem} = undef; | ||
| 1086 | } | ||
| 1087 | } | ||
| 1088 | if (not defined $arg) { | ||
| 1089 | $arg = ''; | ||
| 1090 | } | ||
| 1091 | if (length($arg) == 0 and length($self->{_sendContent}) > 0) { | ||
| 1092 | $self->{_sendContent} .= '{}'; | ||
| 1093 | } elsif (index($arg, ' ') != -1 and $self->{_addContentSeparator} > 0) { | ||
| 1094 | $self->{_sendContent} .= "{$arg}"; | ||
| 1095 | } else { | ||
| 1096 | $self->{_sendContent} .= $arg; | ||
| 1097 | } | ||
| 1098 | } | ||
| 1099 | } | ||
| 1100 | } | ||
| 1101 | |||
| 1102 | sub _send_recv { | ||
| 1103 | my($self) = @_; | ||
| 1104 | my @args = @_[1..scalar(@_) - 1]; | ||
| 1105 | $self->{_addContentSeparator} = undef; | ||
| 1106 | $self->{_firstItem} = 1; | ||
| 1107 | |||
| 1108 | my @argList = @args; | ||
| 1109 | |||
| 1110 | if (defined $self->{_async}) { | ||
| 1111 | splice(@argList, 1, 0, '-async'); | ||
| 1112 | } | ||
| 1113 | |||
| 1114 | if (defined $self->{_timeout}) { | ||
| 1115 | splice(@argList, 1, 0, $self->{_timeout}); | ||
| 1116 | splice(@argList, 1, 0, '-timeout'); | ||
| 1117 | } | ||
| 1118 | |||
| 1119 | foreach my $arg (@argList) { | ||
| 1120 | $self->_join($arg); | ||
| 1121 | } | ||
| 1122 | |||
| 1123 | $self->{_sendBuffer} .= $self->{_sendContent}.chr(0x03); | ||
| 1124 | if (not defined $self->{_buffer}) { | ||
| 1125 | $self->_log('Sending: '.$self->{_sendBuffer}."\n"); | ||
| 1126 | my $ret = eval { | ||
| 1127 | $self->{_ws_client}->write(sprintf("<001><002><009%d>%s", length($self->{_sendBuffer}), $self->{_sendBuffer})); | ||
| 1128 | 1; | ||
| 1129 | }; | ||
| 1130 | if (!$ret and $@) { | ||
| 1131 | $self->_close(); | ||
| 1132 | die 'Connection to the remote IxNetwork instance has been closed: '.$@."\n"; | ||
| 1133 | } | ||
| 1134 | $self->{_sendBuffer} = ''; | ||
| 1135 | } | ||
| 1136 | |||
| 1137 | $self->{_async} = undef; | ||
| 1138 | $self->{_timeout} = undef; | ||
| 1139 | $self->{_buffer} = undef; | ||
| 1140 | $self->{_sendContent} = ''; | ||
| 1141 | |||
| 1142 | if (length($self->{_sendBuffer}) > 0) { | ||
| 1143 | return $self->{_OK}; | ||
| 1144 | } else { | ||
| 1145 | return $self->_recv(); | ||
| 1146 | } | ||
| 1147 | } | ||
| 1148 | |||
| 1149 | sub _recv { | ||
| 1150 | my($self) = shift; | ||
| 1151 | |||
| 1152 | my $ret = eval { | ||
| 1153 | while(1) { | ||
| 1154 | my $commandId = undef; | ||
| 1155 | my $contentLength = int(0); | ||
| 1156 | |||
| 1157 | my $startIndex = index($self->{_recvBuffer}, '<'); | ||
| 1158 | my $stopIndex = index($self->{_recvBuffer}, '>'); | ||
| 1159 | if ($startIndex != -1 && $stopIndex != -1) { | ||
| 1160 | $commandId = substr($self->{_recvBuffer}, $startIndex + 1, $startIndex + 3); | ||
| 1161 | if ($startIndex + 4 < $stopIndex) { | ||
| 1162 | $contentLength = int(substr($self->{_recvBuffer}, $startIndex + 4, $stopIndex)); | ||
| 1163 | } | ||
| 1164 | } | ||
| 1165 | if ($commandId == 1) { | ||
| 1166 | $self->{_evalResult} = $self->{_evalError}; | ||
| 1167 | } elsif ($commandId == 4) { | ||
| 1168 | $self->{_evalResult} = substr($self->{_recvBuffer}, $stopIndex + 1, $contentLength); | ||
| 1169 | } elsif ($commandId == 7) { | ||
| 1170 | my $filename = substr($self->{_recvBuffer}, $stopIndex + 1, $contentLength); | ||
| 1171 | my ($remoteFilename, $dirs, $suffix) = File::Basename::fileparse($self->_normalizeFilePaths($filename)); | ||
| 1172 | if (!(-d $dirs)) { | ||
| 1173 | File::Path::make_path($dirs); | ||
| 1174 | } | ||
| 1175 | $self->_restSend('GET', $self->{_connectionInfo}->{sessionUrl}.'/ixnetwork/files?filename='.URI::Escape::uri_escape($remoteFilename), {file_transfer => '1', filename => $filename}); | ||
| 1176 | } elsif ($commandId == 9) { | ||
| 1177 | $self->{_decoratedResult} = substr($self->{_recvBuffer}, $stopIndex + 1, $contentLength); | ||
| 1178 | last; | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | $self->{_recvBuffer} = substr($self->{_recvBuffer}, $stopIndex + 1 + $contentLength); | ||
| 1182 | } | ||
| 1183 | 1; | ||
| 1184 | }; | ||
| 1185 | if (!$ret and $@) { | ||
| 1186 | die $@."\n"; | ||
| 1187 | } | ||
| 1188 | |||
| 1189 | if ($self->{_evalResult} eq $self->{_evalError}) { | ||
| 1190 | die $self->{_decoratedResult}."\n"; | ||
| 1191 | } | ||
| 1192 | |||
| 1193 | $self->_log('Received: '.$self->{_decoratedResult}."\n"); | ||
| 1194 | |||
| 1195 | if (index($self->{_decoratedResult}, '(') == 0) { | ||
| 1196 | #return $self->_parseStringArray($self->{_decoratedResult}); | ||
| 1197 | my @array; | ||
| 1198 | eval '@array = '.$self->{_decoratedResult}; | ||
| 1199 | return @array; | ||
| 1200 | } else { | ||
| 1201 | return $self->{_decoratedResult}; | ||
| 1202 | } | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | sub _parseStringArray { | ||
| 1206 | my $self= shift; | ||
| 1207 | my $string= shift; | ||
| 1208 | my $offset = shift; | ||
| 1209 | my @array; | ||
| 1210 | my $start = -1; | ||
| 1211 | my $buffer = ''; | ||
| 1212 | my $ignoreNextChar = 0; | ||
| 1213 | my $endTag = ''; | ||
| 1214 | my $startIndex = 0; | ||
| 1215 | if (defined($offset)) { | ||
| 1216 | $startIndex = $$offset; | ||
| 1217 | } | ||
| 1218 | for my $i ($startIndex..length($string)-1) { | ||
| 1219 | my $char = substr($string, $i, 1); | ||
| 1220 | if ($start>=0) { | ||
| 1221 | if ($endTag eq ",") { | ||
| 1222 | if ($char =~ /^\d/) { | ||
| 1223 | $buffer += $char; | ||
| 1224 | } else { | ||
| 1225 | push (@array,$buffer); | ||
| 1226 | $buffer = ''; | ||
| 1227 | $start = -1; | ||
| 1228 | $endTag = ''; | ||
| 1229 | } | ||
| 1230 | } elsif ($ignoreNextChar eq 0 && $char eq $endTag) { | ||
| 1231 | push (@array,$buffer); | ||
| 1232 | $buffer = ''; | ||
| 1233 | $start = -1; | ||
| 1234 | $endTag = ''; | ||
| 1235 | } else { | ||
| 1236 | if ($ignoreNextChar) { | ||
| 1237 | $ignoreNextChar = 0; | ||
| 1238 | } else { | ||
| 1239 | if ($char eq "\\") { | ||
| 1240 | $ignoreNextChar = 1; | ||
| 1241 | } | ||
| 1242 | } | ||
| 1243 | $buffer += $char; | ||
| 1244 | } | ||
| 1245 | } elsif ($char eq "'" || $char eq '"') { | ||
| 1246 | $start = $i; | ||
| 1247 | $endTag = $char; | ||
| 1248 | $buffer = ''; | ||
| 1249 | } elsif ($char eq " ") { | ||
| 1250 | # do nothing (spaces between elements) | ||
| 1251 | } elsif ($char eq ",") { | ||
| 1252 | if ($endTag eq ",") { | ||
| 1253 | push (@array,''); | ||
| 1254 | $endTag = ''; | ||
| 1255 | } else { | ||
| 1256 | $endTag = ","; | ||
| 1257 | } | ||
| 1258 | } elsif ($char eq ")") { | ||
| 1259 | if (defined $offset) { | ||
| 1260 | $$offset = $i; | ||
| 1261 | } | ||
| 1262 | return @array; | ||
| 1263 | } elsif ($char eq "(") { | ||
| 1264 | $i = $i + 1; | ||
| 1265 | push (@array, $self->_parseDecoratedResult2($buffer, \$i)); | ||
| 1266 | } elsif ($char =~ /^\d/) { | ||
| 1267 | $start = $i; | ||
| 1268 | $endTag = ","; | ||
| 1269 | $buffer = $char; | ||
| 1270 | } | ||
| 1271 | } | ||
| 1272 | return @array; | ||
| 1273 | } | ||
| 1274 | |||
| 1275 | sub _check_client_version { | ||
| 1276 | my($self) = @_; | ||
| 1277 | my $version = $self->getVersion(); | ||
| 1278 | if ($self->{_version} ne $version) { | ||
| 1279 | print("WARNING: IxNetwork Perl library version ".$self->{_version}." does not match the IxNetwork server version ".$version."\n"); | ||
| 1280 | } | ||
| 1281 | } | ||
| 1282 | |||
| 1283 | sub _tryWriteAPIKey { | ||
| 1284 | my($self, $dstFile, $key) = @_; | ||
| 1285 | |||
| 1286 | open(my $fid, '>', $dstFile); | ||
| 1287 | print $fid $key or return undef; | ||
| 1288 | close($fid); | ||
| 1289 | |||
| 1290 | return $dstFile; | ||
| 1291 | } | ||
| 1292 | |||
| 1293 | sub _tryReadAPIKey { | ||
| 1294 | my($self, $dstFile) = @_; | ||
| 1295 | my $key = ''; | ||
| 1296 | local $/ = undef; | ||
| 1297 | open(my $fid, '<', $dstFile) or return undef; | ||
| 1298 | $key = <$fid>; | ||
| 1299 | close($fid); | ||
| 1300 | return $key; | ||
| 1301 | } | ||
| 1302 | |||
| 1303 | sub _log { | ||
| 1304 | my $self = shift; | ||
| 1305 | my $msg = shift; | ||
| 1306 | |||
| 1307 | if ($self->{_debug}) { | ||
| 1308 | my $now = localtime(time); | ||
| 1309 | if (length($msg) > 1024) { | ||
| 1310 | $msg = substr($msg, 0, 1024)."...\n"; | ||
| 1311 | } | ||
| 1312 | print '['.$now.'] [IxNet] [debug] '.$msg; | ||
| 1313 | } | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | sub _parseAsBool { | ||
| 1317 | my $self = shift; | ||
| 1318 | my $value = shift; | ||
| 1319 | |||
| 1320 | if (! defined $value || ! $value || lc $value eq "false") { | ||
| 1321 | return 0; | ||
| 1322 | } | ||
| 1323 | |||
| 1324 | return 1; | ||
| 1325 | } | ||
| 1326 | |||
| 1327 | sub _getBaseURL { | ||
| 1328 | my ($self, $hostname, $connectionArgs, $store) = @_; | ||
| 1329 | |||
| 1330 | if (not defined $store) { | ||
| 1331 | $store = 0; | ||
| 1332 | } | ||
| 1333 | |||
| 1334 | my $url = ''; | ||
| 1335 | my @params = (); | ||
| 1336 | my $attempts = 0; | ||
| 1337 | if (exists $connectionArgs->{'-port'}) { | ||
| 1338 | @params = ( | ||
| 1339 | [('https', $connectionArgs->{'-port'})], | ||
| 1340 | [('http', $connectionArgs->{'-port'})] | ||
| 1341 | ); | ||
| 1342 | } else { | ||
| 1343 | @params = ( | ||
| 1344 | ['https', 443] | ||
| 1345 | ); | ||
| 1346 | } | ||
| 1347 | |||
| 1348 | foreach my $connectionParam (@params) { | ||
| 1349 | $url = $self->_createUrl($connectionParam->[0], $hostname, $connectionParam->[1]); | ||
| 1350 | |||
| 1351 | my $ret = eval { | ||
| 1352 | $url = $self->_restGetRedirect($url, 30); | ||
| 1353 | my $port = ''; | ||
| 1354 | my $host = ''; | ||
| 1355 | my $verb = ''; | ||
| 1356 | if ($store) { | ||
| 1357 | if ($url =~ qr/(?<verb>https?):\/\/(?<hostname>[^\/]+):(?<port>\d+)/) { | ||
| 1358 | $port = $+{port}; | ||
| 1359 | $host = $+{hostname}; | ||
| 1360 | $verb = $+{verb}; | ||
| 1361 | } elsif ($url =~ qr/(?<verb>https?):\/\/(?<hostname>[^\/:]+)/) { | ||
| 1362 | if ($+{verb} eq "http") { | ||
| 1363 | $port = 80; | ||
| 1364 | } else { | ||
| 1365 | $port = 443; | ||
| 1366 | } | ||
| 1367 | $host = $+{hostname}; | ||
| 1368 | $verb = $+{verb}; | ||
| 1369 | } | ||
| 1370 | $self->_setConnectionInfo($verb, $host, $port, $url); | ||
| 1371 | $self->{_initialHostname} = $hostname; | ||
| 1372 | if (exists $connectionArgs->{'-port'}) { | ||
| 1373 | $self->{_initialPort} = $connectionArgs->{'-port'}; | ||
| 1374 | } else { | ||
| 1375 | $self->{_initialPort} = 443; | ||
| 1376 | } | ||
| 1377 | } | ||
| 1378 | 1; | ||
| 1379 | }; | ||
| 1380 | |||
| 1381 | if (!$ret and $@) { | ||
| 1382 | if ($@ =~ /IxNetAuthenticationError/) { | ||
| 1383 | die "The API key is either missing or incorrect.\n"; | ||
| 1384 | } | ||
| 1385 | $attempts++; | ||
| 1386 | } else { | ||
| 1387 | last; | ||
| 1388 | } | ||
| 1389 | |||
| 1390 | } | ||
| 1391 | my $paramSize = @params; | ||
| 1392 | if ($attempts == $paramSize) { | ||
| 1393 | if (exists $connectionArgs->{'-port'}) { | ||
| 1394 | die 'Unable to connect to '.$hostname.':'.$connectionArgs->{'-port'}.". Error: Host is unreachable\n"; | ||
| 1395 | } else { | ||
| 1396 | die "Unable to connect to ".$hostname." using default ports (8009 or 443). Error: Host is unreachable.\n"; | ||
| 1397 | } | ||
| 1398 | } | ||
| 1399 | |||
| 1400 | return $url; | ||
| 1401 | } | ||
| 1402 | |||
| 1403 | sub _createUrl { | ||
| 1404 | my ($self, $verb, $hostname, $port) = @_; | ||
| 1405 | |||
| 1406 | return $verb.'://'.$hostname.':'.$port.'/api/v1/sessions'; | ||
| 1407 | } | ||
| 1408 | |||
| 1409 | sub _setConnectionInfo { | ||
| 1410 | my ($self, $verb, $hostname, $port, $url) = @_; | ||
| 1411 | |||
| 1412 | $self->{_connectionInfo}->{verb} = $verb; | ||
| 1413 | if ($verb eq "http") { | ||
| 1414 | $self->{_connectionInfo}->{wsVerb} = 'ws'; | ||
| 1415 | } else { | ||
| 1416 | $self->{_connectionInfo}->{wsVerb} = 'wss'; | ||
| 1417 | } | ||
| 1418 | $self->{_connectionInfo}->{hostname} = $hostname; | ||
| 1419 | $self->{_connectionInfo}->{port} = $port; | ||
| 1420 | $self->{_connectionInfo}->{url} = $url; | ||
| 1421 | } | ||
| 1422 | |||
| 1423 | sub _restGetRedirect { | ||
| 1424 | my ($self, $url, $timeout) = @_; | ||
| 1425 | |||
| 1426 | $self->_log("HEAD ".$url."\n"); | ||
| 1427 | |||
| 1428 | my $response = undef; | ||
| 1429 | my $request = undef; | ||
| 1430 | my $request = new HTTP::Request('HEAD' => $url); | ||
| 1431 | |||
| 1432 | $self->{_user_agent}->timeout($timeout); | ||
| 1433 | $response = $self->{_user_agent}->request($request); | ||
| 1434 | $self->_log($response->code.' '.$response->message."\n"); | ||
| 1435 | |||
| 1436 | my $responseUrl = ''; | ||
| 1437 | if ($response->is_redirect and defined $response->header('location')) { | ||
| 1438 | $responseUrl = $response->header('location'); | ||
| 1439 | return $self->_restGetRedirect($responseUrl, $timeout); | ||
| 1440 | } elsif (defined $response->previous and $response->previous->is_redirect and defined $response->previous->header('location')) { | ||
| 1441 | $responseUrl = $response->previous->header('location'); | ||
| 1442 | } else { | ||
| 1443 | $responseUrl = $response->request->uri; | ||
| 1444 | } | ||
| 1445 | |||
| 1446 | if ($response->code == 401 || $response->code == 403) { | ||
| 1447 | die 'IxNetAuthenticationError: '.$response->code.' '.$response->message."\n"; | ||
| 1448 | } elsif ($response->header('client-warning') =~ /Internal/) { | ||
| 1449 | die "Server Error\n"; | ||
| 1450 | } | ||
| 1451 | |||
| 1452 | my @a = split(/\?/, $responseUrl); | ||
| 1453 | $responseUrl = $a[0]; | ||
| 1454 | @a = split(/\#/, $responseUrl); | ||
| 1455 | $responseUrl = $a[0]; | ||
| 1456 | |||
| 1457 | return $responseUrl; | ||
| 1458 | } | ||
| 1459 | |||
| 1460 | sub _tryGetAttr { | ||
| 1461 | my ($self, $obj, $attr, $default) = @_; | ||
| 1462 | my $ret = ''; | ||
| 1463 | if (exists $obj->{$attr}) { | ||
| 1464 | $ret = $obj->{$attr}; | ||
| 1465 | } else { | ||
| 1466 | $ret = $default; | ||
| 1467 | } | ||
| 1468 | |||
| 1469 | return $ret; | ||
| 1470 | } | ||
| 1471 | |||
| 1472 | 1; |