Broadworks/OCIP.pm untested

filter: Statementstmt 0.0% 0 / 165 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 243 CC 3 SCAR 12.1
CC
3
Cov
0%
_list
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 3.4
1 package Broadworks::OCIP;
2  
3 # ABSTRACT: API for communication with Broadworks OCI-P Interface
4  
5 use strict;
6 use warnings;
7 use utf8;
8 use feature 'unicode_strings';
9 use namespace::autoclean;
10  
11 our $VERSION = '0.08'; # VERSION
12 our $AUTHORITY = 'cpan:NIGELM'; # AUTHORITY
13  
14 use Broadworks::OCIP::Response;
15 use Broadworks::OCIP::Throwable;
16 use Config::Any;
17 use Data::UUID;
18 use Digest::MD5 qw( md5_hex );
19 use Digest::SHA1 qw( sha1_hex );
20 use Encode;
21 use IO::Select;
22 use IO::Socket::INET;
23 use Moose;
24 use Method::Signatures;
25 use MooseX::StrictConstructor;
26 use XML::Writer;
27  
28 extends 'Broadworks::OCIP::Methods';
29  
30  
31 # ------------------------------------------------------------------------
32 sub _list {
33 return () unless ( defined( $_[0] ) );
34 return @{ $_[0] } if ( ref( $_[0] ) eq 'ARRAY' );
35 return $_[0];
36 }
37  
38 # ------------------------------------------------------------------------
39  
40  
41 has host => (
42 is => 'ro',
43 isa => 'Str',
44 required => 1,
45 );
46  
47 # ------------------------------------------------------------------------
48  
49  
50 has username => (
51 is => 'ro',
52 isa => 'Str',
53 required => 1,
54 );
55  
56 # ------------------------------------------------------------------------
57  
58  
59 has authhash => (
60 is => 'ro',
61 isa => 'Str',
62 required => 1,
63 );
64  
65 # ------------------------------------------------------------------------
66  
67  
68 has version => (
69 is => 'ro',
70 isa => 'Str',
71 default => '17sp4',
72 );
73  
74 # ------------------------------------------------------------------------
75  
76  
77 has character_set => (
78 is => 'ro',
79 isa => 'Str',
80 default => 'ISO-8859-1',
81 );
82  
83 # ------------------------------------------------------------------------
84  
85  
86 has encoder => (
87 is => 'ro',
88 isa => 'Object',
89 builder => '_build_encoder',
90 );
91  
92 method _build_encoder () {
93 my $character_set = $self->character_set;
94 return find_encoding($character_set)
95 || Broadworks::OCIP::Throwable->throw(
96 message => "Cannot find encoder for $character_set",
97 execution_phase => 'setup',
98 error_code => 'no_encode'
99 );
100 }
101  
102 # ------------------------------------------------------------------------
103  
104  
105 has protocol => (
106 is => 'ro',
107 isa => 'Str',
108 default => 'OCI',
109 );
110  
111 # ------------------------------------------------------------------------
112  
113  
114 has port => (
115 is => 'ro',
116 isa => 'Int',
117 default => 2208,
118 );
119  
120 # ------------------------------------------------------------------------
121  
122  
123 has target => (
124 is => 'ro',
125 isa => 'Str',
126 builder => '_build_target',
127 lazy => 1
128 );
129  
130 method _build_target () {
131 return join( ':', $self->host, $self->port );
132 }
133  
134 # ------------------------------------------------------------------------
135  
136  
137 has timeout => (
138 is => 'rw',
139 isa => 'Int',
140 default => 8,
141 );
142  
143 # ------------------------------------------------------------------------
144  
145  
146 has socket => (
147 is => 'ro',
148 isa => 'Object',
149 builder => '_build_socket',
150 lazy => 1,
151 );
152  
153 method _build_socket () {
154 return IO::Socket::INET->new( $self->target )
155 || Broadworks::OCIP::Throwable->throw(
156 message => sprintf( "Unable to connect to %s - %s\n", $self->target, $! ),
157 execution_phase => 'setup',
158 error_code => 'cant_connect'
159 );
160 }
161  
162 # ------------------------------------------------------------------------
163  
164  
165 has select => (
166 is => 'ro',
167 isa => 'Object',
168 builder => '_build_select',
169 lazy => 1,
170 );
171  
172 method _build_select () {
173 return IO::Select->new( $self->socket )
174 || Broadworks::OCIP::Throwable->throw(
175 message => sprintf( "Unable to build select on socket to %s - %s\n", $self->target, $! ),
176 execution_phase => 'setup',
177 error_code => 'no_select'
178 );
179 }
180  
181 # ------------------------------------------------------------------------
182  
183  
184 has session => (
185 is => 'ro',
186 isa => 'Str',
187 builder => '_build_session',
188 lazy => 1,
189 );
190  
191 method _build_session () { return Data::UUID->new->create_str; }
192  
193 # ------------------------------------------------------------------------
194  
195  
196 has is_authenticated => (
197 is => 'ro',
198 isa => 'Bool',
199 builder => '_build_is_authenticated',
200 predicate => 'is_set_is_authenticated',
201 lazy => 1,
202 );
203  
204 method _build_is_authenticated () {
205  
206 # send authentication request to get nonce
207 $self->send_command_xml( 'AuthenticationRequest', [ userId => $self->username ] );
208 my $res = $self->receive( 'AuthenticationResponse', 1 );
209  
210 # send login request
211 $self->send_command_xml(
212 'LoginRequest14sp4',
213 [ userId => $self->username,
214 signedPassword => lc( md5_hex( join( ':', $res->payload->{nonce}, $self->authhash ) ) )
215 ]
216 );
217 my $res2 = $self->receive( 'LoginResponse14sp4', 1 );
218  
219 return 1;
220 }
221  
222 # ------------------------------------------------------------------------
223  
224  
225 has last_sent => (
226 is => 'rw',
227 isa => 'Str',
228 );
229  
230 # ------------------------------------------------------------------------
231  
232  
233 has trace => (
234 is => 'rw',
235 isa => 'Bool',
236 default => 0
237 );
238  
239 # ----------------------------------------------------------------------
240  
241  
242 around 'BUILDARGS' => sub {
243 my $orig = shift;
244 my $class = shift;
245  
246 my $args;
247 if ( scalar(@_) == 1 ) {
248 if ( not( ref( $_[0] ) ) ) {
249  
250 # single non reference argument - we treat this as a config filename
251 my $fn = shift;
252 my $confset = Config::Any->load_files( { files => [$fn], use_ext => 1 } );
253 unless ($confset) {
254 Broadworks::OCIP::Throwable->throw(
255 message => sprintf( "Unable to handle config file %s - %s", $fn, $! ),
256 execution_phase => 'buildargs',
257 error_code => 'no_config'
258 );
259 }
260 my $config = ( values( %{ $confset->[0] } ) )[0]
261 or Broadworks::OCIP::Throwable->throw(
262 message => sprintf( "No valid config file %s - %s", $fn, $! ),
263 execution_phase => 'buildargs',
264 error_code => 'invalid_config'
265 );
266  
267 $args = $config->{'Broadworks::OCIP'}
268 or Broadworks::OCIP::Throwable->throw(
269 message => sprintf( "No Broadworks::OCIP section in config file %s", $fn ),
270 execution_phase => 'buildargs',
271 error_code => 'duff_config'
272 );
273 }
274 else {
275 # single reference argument - treat as a hash ref
276 $args = $_[0];
277 }
278 }
279 else { # just make some args up
280 $args = {@_};
281 }
282  
283 # convert password to authhash
284 if ( my $password = delete $args->{password} ) {
285 $args->{authhash} = lc( sha1_hex($password) );
286 }
287  
288 $class->$orig($args);
289 };
290  
291 # ----------------------------------------------------------------------
292  
293  
294 method send ($string) {
295  
296 $self->last_sent($string);
297 $self->socket->print( $self->encoder->encode($string) );
298 warn( '>>> ', $string, "\n" ) if ( $self->trace );
299 }
300  
301 # ----------------------------------------------------------------------
302 method receive ($expected,$die_on_error) {
303  
304 my $bytes = '';
305 { # delimit section where we override character handling
306 use bytes;
307 my $select = $self->select;
308 while ( my ($fh) = $select->can_read( $self->{timeout} ) ) {
309 Broadworks::OCIP::Throwable->throw(
310 message => "Timeout on receive for [$expected] - $!\n",
311 execution_phase => 'receive',
312 error_code => 'timeout'
313 ) unless ( defined($fh) );
314  
315 # read - bail out if EOF
316 my $eofs = 0;
317 unless ( sysread $fh, $bytes, 65536, length($bytes) ) {
318 last if ( $eofs++ );
319 next;
320 }
321 last if ( $bytes =~ /<\/BroadsoftDocument>/ );
322 }
323  
324 Broadworks::OCIP::Throwable->throw(
325 message => "No Data on receive - $!\n($bytes)\n",
326 execution_phase => 'receive',
327 error_code => 'no_data'
328 ) unless ( length($bytes) );
329 }
330  
331 # convert string from cruddy encoding to utf8
332 my $str = $self->encoder->decode($bytes);
333 warn( '<<< ', $str, "\n" ) if ( $self->trace );
334  
335 # we rely on the XML decoder handling any character set issues correctly!
336 return ( Broadworks::OCIP::Response->new( xml => $str, expected => $expected, die_on_error => $die_on_error ) );
337 }
338  
339 # ----------------------------------------------------------------------
340 sub _command_xml_parameters {
341 my ( $xw, $parampairs ) = @_;
342  
343 while ( scalar( @{$parampairs} ) ) {
344 my ( $key, $val ) = splice( @{$parampairs}, 0, 2 );
345 if ( ref($val) eq 'ARRAY' ) {
346 $xw->startTag($key);
347 _command_xml_parameters( $xw, $val );
348 $xw->endTag($key);
349 }
350 else {
351  
352 # attribs is there to allow correct tagging of empty elements
353 my @attribs = ( defined($val) && ( $val eq qq[] ) ) ? ( 'xsi:nil' => 'true' ) : ();
354 $xw->dataElement( $key => $val, @attribs );
355 }
356 }
357 }
358  
359 # ----------------------------------------------------------------------
360  
361  
362 method send_command_xml ($cmd, $parampairs) {
363  
364 # start XML build
365 my $xw = XML::Writer->new( OUTPUT => 'self' )
366 or Broadworks::OCIP::Throwable->throw(
367 message => "Cannot build XML object - $!",
368 execution_phase => 'send',
369 error_code => 'xml_fail'
370 );
371 $xw->xmlDecl( $self->character_set );
372  
373 # build document
374 $xw->startTag(
375 'BroadsoftDocument',
376 'protocol' => $self->protocol,
377 'xmlns' => 'C',
378 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
379 );
380 $xw->dataElement( sessionId => $self->session, xmlns => '' );
381  
382 # build command section
383 $xw->startTag( 'command', xmlns => '', 'xsi:type' => $cmd );
384  
385 # inject command parameters
386 _command_xml_parameters( $xw, $parampairs );
387  
388 # close up tags
389 $xw->endTag('command');
390 $xw->endTag('BroadsoftDocument');
391  
392 # get XML string
393 my $xml_string = $xw->end;
394  
395 # send XML string
396 $self->send($xml_string);
397 }
398  
399 # ----------------------------------------------------------------------
400  
401  
402 method send_query ($cmd, @parampairs) {
403  
404 Broadworks::OCIP::Throwable->throw(
405 message => "Not authenticated",
406 execution_phase => 'send_query',
407 error_code => 'no_auth'
408 ) unless ( $self->is_authenticated );
409 my $response = $cmd;
410 $response =~ s/Request/Response/;
411 $self->send_command_xml( $cmd, \@parampairs );
412 return $self->receive( $response, 1 );
413 }
414  
415 # ----------------------------------------------------------------------
416  
417  
418 method send_command ($cmd, @parampairs) {
419  
420 Broadworks::OCIP::Throwable->throw(
421 message => "Not authenticated",
422 execution_phase => 'send_command',
423 error_code => 'no_auth'
424 ) unless ( $self->is_authenticated );
425 $self->send_command_xml( $cmd, \@parampairs );
426 return $self->receive( 'c:SuccessResponse', 0 );
427 }
428  
429 # ----------------------------------------------------------------------
430  
431  
432 method DEMOLISH ($flag) {
433 if ( $self->is_set_is_authenticated ) {
434  
435 # Logout request still does not return despite documentation saying it does,
436 # so we fiddle things in a way to prevent us waiting for no response...
437 $self->send_command_xml( 'LogoutRequest', [ userId => $self->username, reason => 'Object destruction' ] );
438 $self->socket->close;
439 }
440 }
441  
442 # ------------------------------------------------------------------------
443  
444 __PACKAGE__->meta->make_immutable;
445 1;
446  
447 __END__
448  
449 =pod
450  
451 =encoding UTF-8
452  
453 =head1 NAME
454  
455 Broadworks::OCIP - API for communication with Broadworks OCI-P Interface
456  
457 =head1 VERSION
458  
459 version 0.08
460  
461 =for test_synopsis 1;
462 __END__
463  
464 =for stopwords NIGELM
465  
466 =for Pod::Coverage mvp_multivalue_args
467  
468 =head1 SYNOPSIS
469  
470 use Broadworks::OCIP;
471  
472 my $ocip = Broadworks::OCIP->new(params);
473 my $res = $ocip->SystemSoftwareVersionGetRequest();
474  
475 =head1 DESCRIPTION
476  
477 Broadworks::OCIP is a perl interface to the Broadworks OCI-P Provisioning
478 interface. The functions provided reflect the AS OCI-P. The methods supported
479 are documented in the L<Broadworks::OCIP::Methods> module, which is
480 autogenerated from the Broadworks schemas.
481  
482 =head2 Required Parameters
483  
484 =head3 host
485  
486 The host that is being connected to - either a host name or an IP address.
487  
488 =head3 username
489  
490 The username to authenticate with on the remote system.
491  
492 =head3 authhash
493  
494 An authentication hash to use for authenticating the username. Alternatively
495 the password attribute can be set and this is transformed into an appropriate
496 authhash (and the password deleted).
497  
498 =head2 Other attributes
499  
500 =head3 version
501  
502 Broadworks version - currently defaults to C<17sp4>.
503  
504 =head3 character_set
505  
506 The character set to use - currently defaults to C<ISO-8859-1>.
507  
508 =head3 encoder
509  
510 A character set encoder - uses an instance of L<Encode> returned by
511 L<Encode/find_encoding>.
512  
513 =head3 protocol
514  
515 The protocol to implement - always C<OCI>.
516  
517 =head3 port
518  
519 The port number to connect to - default C<2208>.
520  
521 =head3 target
522  
523 The target to connect to - consists of the host and port linked by a colon.
524  
525 =head3 timeout
526  
527 The timeout within the connection in seconds - defaults to C<8> can be changed
528 during the session.
529  
530 =head3 socket
531  
532 The connection socket - automatically set.
533  
534 =head3 select
535  
536 A select object on the connection socket.
537  
538 =head3 session
539  
540 The session identifier for the session - defaults to a L<Data::UUID> string.
541  
542 =head3 is_authenticated
543  
544 Are we authenticated. Checking this forces authentication. If authentication
545 fails then we throw an exception.
546  
547 =head3 last_sent
548  
549 The last sent XML document.
550  
551 =head3 trace
552  
553 Are we tracing. If this is true then we output sent and received data to
554 STDERR.
555  
556 =head1 METHODS
557  
558 =head3 BUILDARGS
559  
560 Standard L<Moose> C<BUILDARGS> function. If a single argument is passed this
561 is treated as a config filename (opened with L<Config::Any>) if it is a scalar,
562 or assumed to be a hash reference, which is expanded up.
563  
564 If a config filename is passed this is opened, and any C<Broadworks::OCIP> is
565 taken as the overall config.
566  
567 Any C<password> attribute is removed from the config and an L<authhash> is put
568 in its place. This means dumping the object will not reveal the password.
569  
570 =head3 send
571  
572 Sends an XML document to the Broadworks remote over the socket. Convert the
573 passed string to the correct character set.
574  
575 =head3 send_command_xml
576  
577 Builds an XML command document from the command passed and the parameters
578 (which are passed as an array ref of pairs).
579  
580 When the document has been created it is send using the L<send> method.
581  
582 =head3 send_query
583  
584 Sends an XML command to the Broadworks remote over the socket, and receives and
585 passes back the expected response document. Throws an exception if the reply
586 type is returned.
587  
588 =head3 send_command
589  
590 Sends an XML command to the Broadworks remote over the socket, and receives and
591 passes back the expected C<SuccessResponse> document. An exception is not
592 thrown if the wrong reply type is returned, however the C<status_ok> attribute
593 should be checked for this.
594  
595 =head3 DEMOLISH
596  
597 On object destruction, if the system has authenticated, sends a
598 C<LogoutRequest> command and then tears down the socket connection.
599  
600 =head1 AUTHOR
601  
602 Nigel Metheringham <Nigel.Metheringham@redcentricplc.com>
603  
604 =head1 COPYRIGHT
605  
606 Copyright 2014 Recentric Solutions Limited. All rights reserved.
607  
608 =head1 AUTHOR
609  
610 Nigel Metheringham <nigelm@cpan.org>
611  
612 =head1 COPYRIGHT AND LICENSE
613  
614 This software is copyright (c) 2018 by Nigel Metheringham.
615  
616 This is free software; you can redistribute it and/or modify it under
617 the same terms as the Perl 5 programming language system itself.
618  
619 =cut