IxNetwork.pm untested

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