Netconf/EzEditXML.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 36 Conditioncond n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 202 CC 19 SCAR 39.5
CC
19
Cov
0%
addChildren
156.0
AUTOLOAD
20.0
new
6.0
CRAP 52.1
1 package Net::Netconf::EzEditXML;
2  
3 =head1 NAME
4  
5 Net::Netconf::EzEditXML - Create XML for JUNOS XML API the Easy Way!
6  
7 =head1 SYNOPSIS
8  
9 use Net::Netconf::EzEditXML;
10  
11 $newEzElem = new Net::Netconf::EzEditXML( [$elemName] );
12  
13 $ezElem->toString;
14 $newEzElem = $ezElem->AUTOLOAD;
15 $newEzElem = $ezElem->addPath( $path, [%pathChildren] );
16 $newEzElem = $ezElem->newElement( $elemName, [$elemChildren] );
17 $newEzElem = $ezElem->addElement( $elemName, [$elemChildren] );
18 $newEzElem = $ezElem->addSibling( $sibName );
19 $ezElem->setText( $newText );
20 $ezElem->addXML( $xmlstring );
21 $node = $ezElem->getLibNode;
22  
23 =head1 DESCRIPTION
24  
25 Net::Netconf::EzEditXML is used to create XML documents used for the purpose
26 of JUNOS device configuration and performing operational commands.
27 EzEditXML is a 'syntatic sugar wrapper' around the XML::LibXML
28 library. EzEditXML does not communicate with the JUNOS target
29 devices. That function is handled by the existing
30 Net::Netconf::Manager module.
31  
32 =cut
33  
34 use XML::LibXML;
35 require Exporter;
36 @ISA = qw(Exporter);
37  
38 our $VERSION ='0.01';
39 =head1 METHODS
40  
41 =cut
42  
43 ###############################################################################
44 #-----------------------------------------------------------------------------
45 # METHOD: new
46 #-----------------------------------------------------------------------------
47 =over 4
48  
49 =item new
50  
51 =back
52  
53 =over 2
54  
55 $newEzElem = new Net::Netconf::EzEditXML( [$elemName] );
56  
57 =back
58  
59 =over 4
60  
61 The B<< new >> constructor is used to create the top-level JUNOS XML
62 <configuration> element block. If the $elemName is provided, then
63 this value will be used in place of 'configuration'. This later case
64 can be used when creating XML operational commands.
65  
66 =back
67  
68 =cut
69 #-----------------------------------------------------------------------------
70 ###############################################################################
71  
72 sub new {
73  
74 my($class, $top) = @_;
75 my $self = {};
76  
77 $self->{I_AM_NODE} = new XML::LibXML::Element(
78 (($top)?$top:'configuration'));
79  
80 return bless $self;
81 }
82  
83  
84 ###############################################################################
85 #-----------------------------------------------------------------------------
86 # METHOD: AUTOLOAD
87 #-----------------------------------------------------------------------------
88 ###############################################################################
89  
90 =over 4
91  
92 =item AUTOLOAD
93  
94 =back
95  
96 =over 2
97  
98 $newEzElem = $ezElem->I<< elemTag >>([ $children ]);
99  
100 =back
101  
102 =over 4
103  
104 The EzEditXML B<< AUTOLOAD >> is used to either (a) dynamically create a new
105 element or (b) return an existing element. In the case of (a) the
106 I<< elemTag >> block does not exist in the XML document and it is
107 created using the LibXML::Element underlying library. If there are
108 $children passed to the new I<< elemTag >> then these children will be
109 added per the B<< addChildren >> method.
110  
111 =back
112  
113 =cut
114 #-----------------------------------------------------------------------------
115 ###############################################################################
116  
117 sub AUTOLOAD {
118  
119 my $subname = $AUTOLOAD;
120 my $self = shift;
121 my $e_self = $self->{I_AM_NODE};
122 my $child = {};
123 my $e_child;
124 my $e_name;
125  
126 $subname =~ s/.*:://;
127 return undef if ($subname eq "DESTROY");
128 $subname =~ tr/_/-/;
129  
130 $e_name = $subname;
131  
132 # use the LibXML::findodes to locate the element name in the
133 # exsting XML node and use it if it exists.
134 #
135 if( my (@nodes) = $e_self->findnodes( "./". $e_name ) ) {
136 $e_child = shift @nodes;
137 # ... for list items @nodes may contain many items (e.g. list
138 # of interfaces in a vlan definition ... but e_child will be
139 # the first one in the list fwiw.
140 }
141 # otherwise, create a new element and link it to the parent object
142 #
143 else {
144 $e_child = new XML::LibXML::Element( $e_name );
145 $self->{I_AM_NODE}->appendChild( $e_child );
146 }
147  
148 # now bless the child into the EzEditXML class to allow for
149 # further processing.
150  
151 $child->{I_AM_NODE} = $e_child;
152 bless $child;
153  
154 # see if there are any remaining arguments passed to this
155 # routine. if so add these children to the element
156  
157 $child->addChildren( @_ ) if( $#_ != -1 );
158  
159 return $child;
160 }
161  
162 ##############################################################################
163 ##############################################################################
164 ### --------------------------------------------------------------------------
165 ### JUNOS specific attribute settings for the CLI capabilities
166 ### 'activate', 'deactivate', 'insert', 'delete', and 'rename'
167 ### --------------------------------------------------------------------------
168 ##############################################################################
169 ##############################################################################
170  
171 sub junosDelete_E { $_[0]->setAttribute('delete','delete') }
172 sub junosActivate_E { $_[0]->setAttribute('active','active') }
173 sub junosDeactivate_E { $_[0]->setAttribute('inactive','inactive') }
174  
175 sub junosDelete { junosDelete_E( $_[0]->{I_AM_NODE} )}
176 sub junosActivate { junosActivate_E( $_[0]->{I_AM_NODE} )}
177 sub junosDeactivate { junosDeactivate_E( $_[0]->{I_AM_NODE} )}
178  
179 sub junosRename {
180 my $self = shift;
181 my $new_name = shift;
182 $self->{I_AM_NODE}->setAttribute('rename','rename');
183 $self->{I_AM_NODE}->setAttribute('name', $new_name);
184 }
185  
186 sub junosInsert {
187 my $self = shift;
188 my $before_or_after = shift;
189 my $ref_name = shift;
190 $self->{I_AM_NODE}->setAttribute('insert',$before_or_after);
191 $self->{I_AM_NODE}->setAttribute('name', $ref_name);
192 }
193  
194 ##############################################################################
195 ##############################################################################
196 ### --------------------------------------------------------------------------
197 ### Generic XML utility routines
198 ### --------------------------------------------------------------------------
199 ##############################################################################
200 ##############################################################################
201  
202 ###############################################################################
203 #-----------------------------------------------------------------------------
204 # METHOD: newElement
205 #-----------------------------------------------------------------------------
206 =over 4
207  
208 =item newElement
209  
210 =back
211  
212 =over 2
213  
214 $newEzElem = newElement( $elemName, [$children] );
215  
216 =back
217  
218 =over 4
219  
220 Create a new EzElement with the provided name, but does not attach it
221 to anything. If $children are provided, they are added per the B<<
222 addChildren >> method.
223  
224 =back
225  
226 =cut
227 #-----------------------------------------------------------------------------
228 ###############################################################################
229  
230  
231 sub newElement {
232 my ($class, $e_name, $children) = @_;
233 my $self = {};
234  
235 $self->{I_AM_NODE} = new XML::LibXML::Element( $e_name );
236 bless $self;
237  
238 $self->addChildren($children) if($children);
239  
240 return $self;
241 }
242  
243 ###############################################################################
244 #-----------------------------------------------------------------------------
245 # METHOD: addElement
246 #-----------------------------------------------------------------------------
247 =over 4
248  
249 =item addElement
250  
251 =back
252  
253 =over 2
254  
255 $newEzElem = $ezElem->addElement( $elemName, [$elemChildren] );
256  
257 =back
258  
259 =over 4
260  
261 This method needs to be documented.
262  
263 =back
264  
265 =cut
266 #-----------------------------------------------------------------------------
267 ###############################################################################
268  
269  
270 sub addElement {
271 my ($self, $elem, $children) = @_;
272  
273 my $ezE = (ref $elem)
274 ? $elem
275 : newElement Net::Netconf::EzEditXML( $elem, $children );
276  
277 $self->{I_AM_NODE}->appendChild( $ezE->{I_AM_NODE} );
278  
279 return $ezE;
280 }
281  
282 ###############################################################################
283 #-----------------------------------------------------------------------------
284 # METHOD: addPath
285 #-----------------------------------------------------------------------------
286 =over 4
287  
288 =item addPath
289  
290 =back
291  
292 =over 2
293  
294 $newEzElem = $ezElem->addPath( $path, [$children] );
295  
296 =back
297  
298 =over 4
299  
300 This method needs to be documented.
301  
302 =back
303  
304 =cut
305 #-----------------------------------------------------------------------------
306 ###############################################################################
307  
308 sub addPath {
309  
310 my ($self,$path) = @_;
311 my %elemHash = @_;
312  
313 my @dirs = split /\//, $path;
314 my @ezElems = ();
315 my $ezE;
316  
317 bless $self;
318  
319 # create elements for each directory level. if the directory
320 # level has associated subelements defined, then link them in now.
321  
322 foreach (@dirs) {
323 $ezE = newElement Net::Netconf::EzEditXML( $_ );
324 push @ezElems, $ezE;
325  
326 if($elemHash{$_}) {
327 $ezE->addChildren( $elemHash{$_} );
328 }
329 }
330  
331 # create parent/child relationship linkage on the path
332  
333 for (my $i=0; $i < $#ezElems; $i++) {
334 $ezElems[$i]->addElement( $ezElems[$i+1] );
335 }
336  
337 # add the complete structure to the calling object
338  
339 $self->addElement( $ezElems[0] );
340  
341 # return the first ezE in the path
342  
343 return $ezElems[0];
344 }
345  
346 ###############################################################################
347 #-----------------------------------------------------------------------------
348 # METHOD: addChildren
349 #-----------------------------------------------------------------------------
350 =over 4
351  
352 =item addChildren( $children );
353  
354 =back
355  
356 =over 2
357  
358 $ezElem->addChildren( $textString );
359  
360 =over 4
361  
362 This version of addChildren is used to set the text-node value of the
363 calling element.
364  
365 =back
366  
367 $ezElem->addChildren( [{a=>b},{c=>d},{e=>y}, ...] );
368  
369 =over 4
370  
371 This version of addChildren is used to add unique child-elements as
372 defined in each hash {child-elem-name => child-text-value} reference.
373  
374 =back
375  
376 $ezElem->addChildren( [$s1, $s2, $s3, ...], [$options] );
377  
378 =over 4
379  
380 This version of addChildren is used to add a list of string-items all
381 with the same name as the parent $ezElem. The $options can be:
382  
383 =over 2
384  
385 =item nameItems=>1
386  
387 Include an addition <name> element block wihtin the $ezElem block.
388 Many of JUNOS configuration elements have a <name> element to unqiuely
389 identify multiple items of the same element type. The <interface> list
390 within a VLAN is a good example of this.
391  
392 =item junosDelete=>1
393  
394 Mark the item to be deleted from the configuration.
395  
396 =item junosActivate=>1
397  
398 Mark the item to be activated in the configuration.
399  
400 =item junosDeactivate=>1
401  
402 Mark the item to be deactivated in the configuration.
403  
404 =back
405  
406 =back
407  
408 =back
409  
410 =cut
411 #-----------------------------------------------------------------------------
412 ###############################################################################
413  
414 sub addChildren {
415  
416 my $self = shift;
417 my $argx = shift;
418  
419 my $e_self = $self->{I_AM_NODE};
420 my %opts = @_;
421  
422 # ------------------------------------------------------------------------
423 # There will always only be one arg of element data:
424 # (a) <SCALAR>
425 # (b) [{x=>y},{a=>b},...]
426 # (c) [<SCALAR>,<SCALAR>,...]
427 # ------------------------------------------------------------------------
428  
429 # ------------------------------------------------------------------------
430 # CASE: <SCALAR>
431 # ------------------------------------------------------------------------
432 # If the argument is a SCALAR value, then use the value to set the
433 # text-content for the e_child element. For example, if the
434 # e_child is the element block <host-name>, and the argument is
435 # 'foo-baz', the resulting XML would be:
436 # <host-name>foo-baz</host-name>
437  
438 if (not ref $argx) {
439 $e_self->appendTextNode($argx );
440 return;
441 }
442  
443 # If it's not a SCALAR then it must be an ARRAY/ref; i.e. [...].
444 # We don't accept any other arg types at this point. So if it's
445 # not an [], return now.
446  
447 return unless (ref($argx) eq "ARRAY");
448  
449 my $arefi = @$argx[0]; # see what the array is containing
450  
451 # ------------------------------------------------------------------------
452 # CASE: [{x=>y},{a=>b},...]
453 # ------------------------------------------------------------------------
454 # If the [] contains a list of HASH/ref,
455 # i.e. [{a=>b},{c=>d},{e=f}}], then create sub-elements in the
456 # form of <key>value</key>.
457  
458 if (ref($arefi) eq "HASH") {
459  
460 foreach (@$argx) {
461 my ($key,$value) = each %{$_}; each %{$_};
462  
463 my $e = new XML::LibXML::Element( $key );
464 junosDelete_E( $e ) if($opts{junosDelete});
465 junosActivate_E( $e ) if($opts{junosActivate});
466 junosDeactivate_E( $e ) if($opts{junosDeactivate});
467  
468 $e->appendTextNode ($value);
469 $e_self->appendChild( $e );
470 }
471  
472 return;
473 }
474  
475 # ------------------------------------------------------------------------
476 # CASE: [<SCALAR>,<SCALAR>,...]
477 # ------------------------------------------------------------------------
478 # If the [] contains a list of SCALARs, i.e. ["x","y",z"], then we
479 # are going to create elements of child foreach item in the [].
480 #
481 # So if child was <interface> and [] = ['ge-0/0/0', 'ge-0/0/1'],
482 # then we would create:
483 # <interface>ge-0/0/0</interface>
484 # <interface>ge-0/0/1</interface>
485 #
486 # If the caller included 'nameItems=>1' in the args, then instead
487 # of adding the text value for each child, add a child with
488 # <name><items></name> for each child. In the same example with
489 # nameItems=>1, the result would be:
490 # <interface>
491 # <name>ge-0/0/0</name>
492 # </interface>
493 # <interface>
494 # <name>ge-0/0/1</name>
495 # </interface>
496  
497 my $self_name = $e_self->nodeName;
498 my $e_parent = $e_self->parentNode;
499  
500 # this bit is added to handle the case of adding more same list
501 # items. if the $self node has no children, then it's the first
502 # time children are being added to the list. in this case the we
503 # need to remove the empty $self element that was automatically
504 # created in the AUTOLOAD routine.
505 #
506 $e_parent->removeChild( $e_self ) if(not $e_self->hasChildNodes );
507  
508 while($arefi = shift @$argx) {
509 my $e = new XML::LibXML::Element( $self_name );
510 junosDelete_E( $e ) if($opts{junosDelete});
511 junosActivate_E( $e ) if($opts{junosActivate});
512 junosDeactivate_E( $e ) if($opts{junosDeactivate});
513 if ($opts{nameItems}) {
514 $e->appendTextChild ('name', $arefi);
515 }
516 else {
517 $e->appendTextNode ($arefi);
518 }
519 $e_parent->appendChild( $e );
520 }
521 }
522  
523 ###############################################################################
524 #-----------------------------------------------------------------------------
525 # METHOD: addSibling
526 #-----------------------------------------------------------------------------
527 =over 4
528  
529 =item addSibling
530  
531 =back
532  
533 =over 2
534  
535 $newEzSibling = $ezElem->addSibling;
536  
537 =back
538  
539 =over 4
540  
541 This method needs to be documented.
542  
543 =back
544  
545 =cut
546 #-----------------------------------------------------------------------------
547 ###############################################################################
548  
549 sub addSibling {
550  
551 my $self = shift;
552 my $sib = {};
553  
554 $sib->{I_AM_NODE} = $self->{I_AM_NODE}->cloneNode;
555 $self->{I_AM_NODE}->addSibling( $sib->{I_AM_NODE} );
556  
557 return bless $sib;
558 }
559  
560 ###############################################################################
561 #-----------------------------------------------------------------------------
562 # METHOD: setText
563 #-----------------------------------------------------------------------------
564 =over 4
565  
566 =item setText
567  
568 =back
569  
570 =over 2
571  
572 $ezElem->setText( $newText );
573  
574 =back
575  
576 =over 4
577  
578 This method will re-write the element's text-node value to $newText.
579 This method calls XML::LIbXML::Text->setData underlying method.
580  
581 =back
582  
583 =cut
584 #-----------------------------------------------------------------------------
585 ###############################################################################
586  
587 sub setText {
588 my ($self, $text) = @_;
589 my $e_self = $self->{I_AM_NODE};
590 my $e_text = $e_self->firstChild;
591  
592 $e_text->setData( $text );
593  
594 return $self;
595 }
596  
597 ###############################################################################
598 #-----------------------------------------------------------------------------
599 # METHOD: addXML
600 #-----------------------------------------------------------------------------
601 =over 4
602  
603 =item addXML
604  
605 =back
606  
607 =over 2
608  
609 $ezElem->addXML( $xmlstring );
610  
611 =back
612  
613 =over 4
614  
615 This method accepts a "well-balanced-chunk" of XML string and
616 incorporates/attaches it into $ezElem. This method calls the
617 XML::LibXML::Element->appendWellBalancedChunk method.
618  
619 =back
620  
621 =cut
622 #-----------------------------------------------------------------------------
623 ###############################################################################
624  
625 sub addXML {
626 my ($self, $xmlstring) = @_;
627 $self->{I_AM_NODE}->appendWellBalancedChunk( $xmlstring );
628 }
629  
630 ###############################################################################
631 #-----------------------------------------------------------------------------
632 # METHOD: getLibNode
633 #-----------------------------------------------------------------------------
634 =over 4
635  
636 =item getLibNode
637  
638 =back
639  
640 =over 2
641  
642 $node = $ezElem->getLibNode;
643  
644 =back
645  
646 =over 4
647  
648 This method returns the XML::LibXML node element. The XML::LIbXML
649 library of routines can then be accesses.
650  
651 =back
652  
653 =cut
654 #-----------------------------------------------------------------------------
655 ###############################################################################
656  
657  
658 sub getLibNode {
659 my $self = shift;
660 return $self->{I_AM_NODE}
661 }
662  
663 ###############################################################################
664 #-----------------------------------------------------------------------------
665 # METHOD: toString
666 #-----------------------------------------------------------------------------
667 =over 4
668  
669 =item toString
670  
671 =back
672  
673 =over 2
674  
675 $ezElem->toString
676  
677 =back
678  
679 =over 4
680  
681 This method returns the XML document in string form. It calls the
682 XML::LibXML underlying library to perform this action.
683  
684 =back
685  
686 =cut
687 #-----------------------------------------------------------------------------
688 ###############################################################################
689  
690  
691 sub toString {
692 my $self = shift;
693 return $self->{I_AM_NODE}->toString(1);
694 }