Response.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Access/stubs.pm ↑ Summary Device.pm »
1 #
2 # $Id: Response.pm,v 1.11 2003/03/02 11:12:09 dsw Exp $
3 #
4 # COPYRIGHT AND LICENSE
5 # Copyright (c) 2001-2003, Juniper Networks, Inc.
6 # All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 # 1. Redistributions of source code must retain the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer.
13 # 2. Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following disclaimer
15 # in the documentation and/or other materials provided with the
16 # distribution.
17 # 3. The name of the copyright owner may not be used to
18 # endorse or promote products derived from this software without specific
19 # prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 #
32  
33  
34 package JUNOS::Response;
35  
36 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use vars qw(@ISA $error_tag);
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 @ISA = qw(XML::DOM::Element);
40 $error_tag = "xnm:error";
41  
42 0 ▶use XML::DOM;
Subroutine BEGIN: called CC=1 SCAR=2.6
43 use JUNOS::Trace;
44  
45 sub new
46 {
47 my($class, $self) = @_;
48 $self = [] unless ref $self;
49  
50 bless $self, $class;
51 }
52  
53 sub getFirstError
54 {
55 my($self) = @_;
56  
57 return undef if $#$self < 0;
58  
59 my $err;
60  
61 if ($self->getTagName() eq $error_tag) {
62 $err = $self;
63 } else {
64 my $errors = $self->getElementsByTagName($error_tag);
65 return undef unless defined $errors;
66 return undef unless $errors->getLength;
67 # We only really can return the first error.
68 $err = shift @$errors;
69 }
70  
71 my $rc = {};
72
73 for my $kid ($err->getChildNodes) {
74 next unless $kid->isElementNode;
75  
76 my $name = $kid->getNodeName;
77  
78 my $val;
79 for my $baby ($kid->getChildNodes) {
80 $val .= $baby->getData if $baby->getNodeType == TEXT_NODE;
81 }
82  
83 $name =~ s/^[\s\n]+//;
84 $name =~ s/[\s\n]+$//;
85 $name =~ s/-/_/g;
86 $val =~ s/^[\s\n]+//;
87 $val =~ s/[\s\n]+$//;
88 trace("Noise", "JUNOS::Response: $name -> $val");
89 $rc->{ $name } .= " $val";
90 }
91  
92 # append any extraneous 'output' tag
93 $rc->{message} .= $rc->{output};
94  
95 $rc;
96 }
97  
98 sub toString
99 {
100 my $self = shift;
101  
102 return undef if $#$self < 0;
103 $self->SUPER::toString(@_);
104 }
105  
106 #
107 # private: get_first_element_with_attribute
108 #
109 # This subroutine is called to return the first element containing a specific
110 # attribute, so the caller of this subroutine can get, set or delete
111 # this attribute.
112 #
113 sub get_first_element_with_attribute
114 {
115 my ($node, $attrname) = @_;
116  
117 my $attributes = $node->getAttributes();
118 return $node if ($attributes && $node->getAttribute($attrname));
119  
120 my $nodes = $node->getChildNodes();
121 my $len = $nodes->getLength;
122  
123 for (my $i = 0; $i < $len; $i++) {
124 my $nsnode = get_first_element_with_attribute($nodes->item($i), $attrname);
125 return $nsnode if $nsnode;
126 }
127 return;
128 }
129  
130 #
131 # private: remove_prefix_from_element
132 #
133 # This subroutine is called to remove the namespace prefix from the
134 # XSL file to deal with the JUNOScript responses from old versions
135 # of JUNOS that do not contain default namespace.
136 #
137 sub remove_prefix_from_element
138 {
139 my ($self, $node, $prefix) = @_;
140  
141 my $nodes = $node->getChildNodes();
142 return unless $nodes;
143
144 my $len = $nodes->getLength;
145 return unless $len;
146
147 for (my $i = 0; $i < $len; $i++) {
148 my $item = $nodes->item($i);
149 my $attributes = $item->getAttributes();
150 if ($attributes) {
151 my $attrcount = $attributes->getLength();
152  
153 for (my $i = 0; $i < $attrcount; $i++) {
154 my $attr = $attributes->item($i);
155 my $value = $attr->getNodeValue();
156 $value =~ s/$prefix://g;
157 $attr->setNodeValue($value);
158 }
159 }
160  
161 $self->remove_prefix_from_element($item, $prefix);
162 }
163  
164 return;
165 }
166  
167 use constant XML_NS_ATTRIBUTE => 'xmlns';
168  
169 #
170 # translateXSLtoRelease
171 #
172 # XSLT 1.0 does not deal with default namespace as well as we had hoped.
173 # It requires the xsl file to declare a namspace given the default namespace
174 # from the XML data. It must also prefix all the element names with the
175 # local name.
176 #
177 # Because the default namespace in all JUNOScript responses contains
178 # the JUNOS version, the xsl file cannot point to the same default
179 # namespace for all routers. Also there are backward compatibility
180 # problems in dealing with some JUNOScript responses form older versions
181 # of JUNOS that do not have default namespace (e.g.
182 # <get-bgp-neighbor-information> response from pre-5.1 releases)
183 #
184 # Before transforming the response, the xsl file should be parsed and
185 # its namespace attribute for the JUNOScript response should be replaced with
186 # the default namespace in the response. If the default namespace is not
187 # defined in the xml doc, the prefix will be removed from the XSL file
188 # to deal with the abovementioned backward compatibility problem.
189 #
190 # Hopefully, this subroutine will be made obsolete when XLST 2.0 implementation
191 # is made available.
192 #
193 use constant HASH_REFERENCE => 'HASH';
194 sub translateXSLtoRelease
195 {
196 my ($self, $namespace, $input, $output) = @_;
197  
198 my $xsltparser = new XML::DOM::Parser;
199 unless ($xsltparser) {
200 print STDERR "ERROR: cannot create an XML::DOM::Parser object\n";
201 return;
202 }
203 my $xsltdoc = $xsltparser->parsefile($input);
204 unless ($xsltdoc) {
205 print STDERR "ERROR: cannot parse the XSL input file $input\n";
206 return;
207 }
208 if (ref($namespace) eq HASH_REFERENCE) {
209 my %nstable = %$namespace;
210 foreach my $k (keys(%nstable)) {
211 setXSLtoRelease($self, $k, $nstable{$k}, $xsltdoc);
212 }
213 } else {
214 setXSLtoRelease($self, $namespace, XML_NS_ATTRIBUTE, $xsltdoc);
215 }
216 $xsltdoc->printToFile($output);
217 return $output;
218 }
219  
220 sub setXSLtoRelease
221 {
222 my ($self, $xslns, $xmlns, $xsltdoc) = @_;
223  
224 my $nselem = get_first_element_with_attribute($self, $xmlns);
225 my $dfns = $nselem->getAttribute($xmlns) if $nselem;
226  
227 if ($dfns) {
228 $nselem = get_first_element_with_attribute($xsltdoc, $xslns);
229 return unless $nselem;
230 $nselem->setAttribute($xslns, $dfns);
231 return;
232 }
233  
234 # if there is no instance of the specified namespace in the xml doc,
235 # make sure the prefix is removed from the xsl file. This is for backward
236 # compatiblitiy. Some of the JUNOScript Responses from older
237 # releases do not have the default namespace. The XSL file would
238 # not work with it unless we make this change.
239
240 my ($declare, $prefix) = split(/:/, $xslns);
241 $self->remove_prefix_from_element($xsltdoc, $prefix);
242 }
243  
244 1;
245  
246 __END__
« Access/stubs.pm ↑ Summary Device.pm »