blib/lib/IxNetwork/IxNetwork.pm untested

filter: Statementstmt 0.0% 0 / 246 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 27 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 50 Podpod 0.0% 0 / 43 totaltotal 0.0% 0 / 434 CC 35 SCAR 42.4
CC
35
Cov
0%
_detectTransport
306.0
connect
56.0
_log
12.0
CRAP 69.5
1 #!/usr/bin/perl
2 #
3 # Copyright 1997 - 2018 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 lib ".";
25 use checkDependencies;
26 use IxNetworkLegacy;
27 use IO::Socket;
28
29 package IxNetwork;
30
31 our $VERSION = '8.50';
32
33 =head1 SYNOPSIS
34 IxNetwork is the Perl package for the IxNetwork Low Level API that allows you to configure and run IxNetwork tests.
35
36 =head1 IMPORTANT LINKS
37
38 =over 4
39
40 =item * L<https://github.com/ixiacom/ixnetwork-api-pl>
41
42 =cut
43
44 use constant NL => "\r\n";
45
46 my $gobalSecureTransport = undef;
47
48 sub new {
49 my $class = shift;
50 my $self = {
51 _version => '8.50.1501.9',
52 _OK => '::ixNet::OK',
53 _ERROR => '::ixNet::ERROR',
54 _transportType => undef,
55 _noApiKey => '00000000000000000000000000000000',
56 _debug => undef
57 };
58 $self->{__ixNetworkLegacy} = new IxNetworkLegacy();
59 $self->{__ixNetworkSecure} = undef;
60 $self->{__ixNetworkSecureImportError} = undef;
61 my $ret = eval {
62 checkDependencies::checkDeps();
63 require IxNetworkSecure;
64 IxNetworkSecure->import();
65 $self->{__ixNetworkSecure} = new IxNetworkSecure();
66 1;
67 };
68 if (!$ret and $@) {
69 $self->{__ixNetworkSecure} = undef;
70 $self->{__ixNetworkSecureImportError} = $@."\n";
71 print "WARNING: $@";
72 print "If you are trying to connect to a Windows IxNetwork API Server on TCL port you can safely ignore this warning.\n";
73 }
74
75 bless $self, $class;
76 return $self;
77 }
78
79 sub _getCurrentTransport {
80 my $self = shift;
81
82 if ($self->{__ixNetworkSecure} and $self->{__ixNetworkSecure}->_isConnected()) {
83 return $self->{__ixNetworkSecure};
84 } else {
85 return $self->{__ixNetworkLegacy};
86 }
87 }
88
89 sub _getSecureTransport {
90 my $self = shift;
91
92 if ($self->{__ixNetworkSecure}) {
93 return $self->{__ixNetworkSecure};
94 } else {
95 die $self->{__ixNetworkSecureImportError};
96 }
97 }
98
99 sub _isConnected {
100 my $self = shift;
101
102 if ($self->{__ixNetworkSecure}) {
103 my $ret = $self->{__ixNetworkLegacy}->_isConnected() || $self->{__ixNetworkSecure}->_isConnected();
104 return $ret;
105 }
106
107 return $self->{__ixNetworkLegacy}->_isConnected();
108 }
109
110 sub _log {
111 my $self = shift;
112 my $msg = shift;
113
114 if ($self->{_debug}) {
115 my $now = localtime(time);
116 if (length($msg) > 1024) {
117 $msg = substr($msg, 0, 1024)."...\n";
118 }
119 print '['.$now.'] [IxNet] [debug] '.$msg;
120 }
121 }
122
123 sub _detectTransport {
124 my ($self, $hostname, $port) = @_;
125
126 $self->_log("Detecting transport type...\n");
127 my $_socket = undef;
128 my $_transport = undef;
129 my $usingDefaultPorts = undef;
130 if (not defined $port) {
131 $port = 8009;
132 $usingDefaultPorts = 1;
133 } else {
134 $usingDefaultPorts = 0;
135 }
136
137 my $ret = eval {
138 my $protocol = getprotobyname('tcp');
139 socket($_socket, Socket::AF_INET, Socket::SOCK_STREAM, $protocol);
140 my $inetAddress = gethostbyname($hostname);
141 my $sockAddress = Socket::sockaddr_in($port, $inetAddress);
142 $_socket->blocking(0);
143 CORE::connect($_socket, $sockAddress);
144
145 my $sock = $_socket;
146 my $rin = '';
147 my $win = '';
148 my $buffer = '';
149 my $timeout = 15;
150 my $iterations = 2;
151 vec($rin, fileno($sock), 1) = 1;
152 vec($win, fileno($sock), 1) = 1;
153 my $ein= $rin | $win;
154 my $socketOpen = select(my $rout = $rin, my $wout = $win, my $eout = $ein, 1);
155 while ($iterations > 0 and !$socketOpen) {
156 $socketOpen = select(my $rout = $rin, my $wout = $win, my $eout = $ein, 1);
157 $iterations -= 1;
158 }
159 if (!$socketOpen) {
160 die "Host is unreachable.\n";
161 }
162 $_socket->blocking(1);
163
164 vec($rin, fileno($sock), 1) = 1;
165 my $readOpen = select(my $rout = $rin, undef, undef, $timeout);
166 if (!$readOpen) {
167 vec($win, fileno($sock), 1) = 1;
168 my $writeOpen = select(undef, my $wout = $win, undef, $timeout);
169 if ($writeOpen) {
170 $_transport = $self->{__ixNetworkSecure};
171 }
172 }
173 my $char = undef;
174 $_socket->blocking(0);
175 while(not defined $_transport) {
176 $_socket->read($char, 1);
177
178 if (length($char) == 0) {
179 last;
180 }
181 $buffer .= $char;
182 if (length $buffer == 4) {
183 if (index($buffer, '<001') == 0) {
184 $_transport = $self->{__ixNetworkLegacy};
185 last;
186 } elsif (index($buffer, 'HTTP') != 0) {
187 $_transport = $self->{__ixNetworkSecure};
188 last;
189 }
190 }
191 if ((length $buffer > 50) &&
192 (index($buffer, 'Server: IxNetwork API Server') != -1) ||
193 (index($buffer, 'Server: Connection Manager') != -1)) {
194 $_transport = $self->{__ixNetworkLegacy};
195 last;
196 }
197 }
198 1;
199 };
200 if (!$ret and $@) {
201 if ($_socket) {
202 $_socket->close();
203 $_socket = undef;
204 }
205 if (!$usingDefaultPorts) {
206 die "Unable to connect to ".$hostname.':'.$port.'. Error: '.$@."\n";
207 }
208 }
209 if ($_socket) {
210 $_socket->close();
211 }
212 if ($usingDefaultPorts and (not defined $_transport)) {
213 $_transport = $self->{__ixNetworkSecure};
214 }
215 if (not defined $_transport) {
216 if (!$usingDefaultPorts) {
217 die "Unable to connect to ".$hostname.':'.$port.". Error: Host is unreachable.\n";
218 } else {
219 die "Unable to connect to ".$hostname." using default ports (8009, 443). Error: Host is unreachable.\n";
220 }
221 }
222 $self->_log('Using transport type '.$_transport->{_transportType}."\n");
223 return $_transport;
224 }
225
226 sub setDebug {
227 my $self = shift;
228 my $debug = @_[0];
229
230 $self->{_debug} = $debug;
231
232 $self->{__ixNetworkLegacy}->setDebug(@_);
233 if ($self->{__ixNetworkSecure}) {
234 $self->{__ixNetworkSecure}->setDebug(@_);
235 }
236 }
237
238 sub getRoot {
239 return '::ixNet::OBJ-/';
240 }
241
242 sub getNull {
243 return '::ixNet::OBJ-null';
244 }
245
246 sub setAsync {
247 my $self = shift;
248
249 return $self->_getCurrentTransport()->setAsync(@_);
250 }
251
252 sub setTimeout {
253 my $self = shift;
254
255 return $self->_getCurrentTransport()->setTimeout(@_);
256 }
257
258 sub getApiKey {
259 my $self = shift;
260 my $hostname = @_[0];
261 if ($hostname == "") {
262 my $msg .= "SyntaxError: This method requires at least the hostname argument. An example of a correct method call is:\n\t";
263 $msg .= '$ixNet->getApiKey(<hostname>, "-username", <username>, "-password", <password> [,"-port", <443>] [, "-apiKeyFile", <api.key>])';
264 die $msg."\n";
265 }
266 if (!$self->{__ixNetworkSecure}) {
267 print "Unable to get API key from ".$hostname." due to missing dependencies (see documentation for required dependencies). If you are trying to connect to a Windows IxNetwork API Server on TCL port you can safely ignore this warning.\n";
268 return $self->{_noApiKey};
269 }
270 return $self->_getSecureTransport()->getApiKey(@_);
271 }
272
273 sub getSessions {
274 my $self = shift;
275
276 return $self->_getSecureTransport()->getSessions(@_);
277 }
278
279 sub getSessionInfo {
280 my $self = shift;
281
282 return $self->_getCurrentTransport()->getSessionInfo(@_);
283 }
284
285 sub clearSessions {
286 my $self = shift;
287
288 return $self->_getSecureTransport()->clearSessions(@_);
289 }
290
291 sub clearSession {
292 my $self = shift;
293
294 return $self->_getSecureTransport()->clearSession(@_);
295 }
296
297 sub connect {
298 my $self = shift;
299 my $hostname = @_[0];
300 my @args = @_[1..scalar(@_) - 1];
301
302 my $ret = undef;
303 if ($self->_isConnected() || (not defined $self->{__ixNetworkSecure})) {
304 $ret = $self->_getCurrentTransport()->connect(@_);
305 } else {
306 my $nameValuePairs = {};
307 my $name = undef;
308 my $port = undef;
309 foreach my $arg (@args) {
310 if (index($arg, '-', 0) == 0) {
311 if (not defined $name) {
312 $name = $arg;
313 } else {
314 $nameValuePairs->{$name} = '';
315 }
316 } elsif (defined $name) {
317 $nameValuePairs->{$name} = $arg;
318 $name = undef;
319 }
320 }
321 if (exists $nameValuePairs->{'-port'}) {
322 $port = $nameValuePairs->{'-port'};
323 }
324 $ret = $self->_detectTransport($hostname, $port)->connect(@_);
325 }
326 $self->{_transportType} = $self->_getCurrentTransport()->{_transportType};
327 if ($self->{_transportType} eq 'WebSocket') {
328 $gobalSecureTransport = $self->_getCurrentTransport();
329 }
330
331 return $ret;
332 }
333
334 sub disconnect {
335 my $self = shift;
336
337 my $ret = $self->_getCurrentTransport()->disconnect();
338 if ($gobalSecureTransport) {
339 $gobalSecureTransport = undef;
340 }
341 $self->{_transportType} = undef;
342
343 return $ret;
344 }
345
346 sub help {
347 my $self = shift;
348
349 $self->_getCurrentTransport()->help(@_);
350 }
351
352 sub setSessionParameter {
353 my $self = shift;
354
355 $self->_getCurrentTransport()->setSessionParameter(@_);
356 }
357
358 sub getVersion {
359 my($self) = shift;
360
361 if ($self->_isConnected()) {
362 return $self->_getCurrentTransport()->getVersion();
363 } else {
364 return $self->{_version};
365 }
366 }
367
368 sub getParent {
369 my $self = shift;
370
371 $self->_getCurrentTransport()->getParent(@_);
372 }
373
374 sub exists {
375 my $self = shift;
376
377 $self->_getCurrentTransport()->exists(@_);
378 }
379
380 sub commit {
381 my $self = shift;
382
383 $self->_getCurrentTransport()->commit(@_);
384 }
385
386 sub rollback {
387 my $self = shift;
388
389 $self->_getCurrentTransport()->rollback(@_);
390 }
391
392 sub execute {
393 my $self = shift;
394
395 $self->_getCurrentTransport()->execute(@_);
396 }
397
398 sub add {
399 my $self = shift;
400
401 $self->_getCurrentTransport()->add(@_);
402 }
403
404 sub remove {
405 my $self = shift;
406
407 $self->_getCurrentTransport()->remove(@_);
408 }
409
410 sub setAttribute {
411 my $self = shift;
412
413 $self->_getCurrentTransport()->setAttribute(@_);
414 }
415
416 sub setMultiAttribute {
417 my $self = shift;
418
419 $self->_getCurrentTransport()->setMultiAttribute(@_);
420 }
421
422 sub getAttribute {
423 my $self = shift;
424
425 $self->_getCurrentTransport()->getAttribute(@_);
426 }
427
428 sub getList {
429 my $self = shift;
430
431 $self->_getCurrentTransport()->getList(@_);
432 }
433
434 sub getFilteredList {
435 my $self = shift;
436
437 $self->_getCurrentTransport()->getFilteredList(@_);
438 }
439
440 sub adjustIndexes {
441 my $self = shift;
442
443 $self->_getCurrentTransport()->adjustIndexes(@_);
444 }
445
446 sub remapIds {
447 my $self = shift;
448
449 $self->_getCurrentTransport()->remapIds(@_);
450 }
451
452 sub getResult {
453 my $self = shift;
454
455 $self->_getCurrentTransport()->getResult(@_);
456 }
457
458 sub wait {
459 my $self = shift;
460
461 $self->_getCurrentTransport()->wait(@_);
462 }
463
464 sub isDone {
465 my $self = shift;
466
467 $self->_getCurrentTransport()->isDone(@_);
468 }
469
470 sub isSuccess {
471 my $self = shift;
472
473 $self->_getCurrentTransport()->isSuccess(@_);
474 }
475
476 sub xpath {
477 my $self = shift;
478
479 $self->_getCurrentTransport()->xpath(@_);
480 }
481
482 sub writeTo {
483 my $self = shift;
484
485 $self->_getCurrentTransport()->writeTo(@_);
486 }
487
488 sub readFrom {
489 my $self = shift;
490
491 $self->_getCurrentTransport()->readFrom(@_);
492 }
493
494 END {
495 if ($gobalSecureTransport) {
496 $gobalSecureTransport->disconnect();
497 $gobalSecureTransport = undef;
498 }
499 }
500
501 1;