line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package BGPmon::Validate; |
2
|
1
|
|
|
1
|
|
77746
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
4
|
1
|
|
|
1
|
|
6
|
use constant FALSE => 0; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
72
|
|
5
|
1
|
|
|
1
|
|
5
|
use constant TRUE => 1; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
43
|
|
6
|
1
|
|
|
1
|
|
1653
|
use BGPmon::Fetch; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use XML::LibXML; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
BEGIN{ |
10
|
|
|
|
|
|
|
require Exporter; |
11
|
|
|
|
|
|
|
our $VERSION = '1.01'; |
12
|
|
|
|
|
|
|
our $AUTOLOAD; |
13
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
14
|
|
|
|
|
|
|
our @EXPORT_OK = qw(init validate get_error_msg |
15
|
|
|
|
|
|
|
get_error_code get_valid_error); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Variables to hold error codes and messages |
21
|
|
|
|
|
|
|
my %error_code; |
22
|
|
|
|
|
|
|
my %error_msg; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use constant NO_ERROR_CODE => 0; |
25
|
|
|
|
|
|
|
use constant NO_ERROR_MSG => 'No Error. Relax with some tea.'; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#Error opening or parsing the XSD file |
28
|
|
|
|
|
|
|
use constant XSD_ERROR => 692; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
#Error codes for parsing the XML message. |
31
|
|
|
|
|
|
|
use constant NO_MSG_GIVEN => 690; |
32
|
|
|
|
|
|
|
use constant NO_MSG_GIVEN_MSG => "Invalid XML message was given."; |
33
|
|
|
|
|
|
|
use constant INVALID_MSG_GIVEN => 691; |
34
|
|
|
|
|
|
|
use constant INVALID_MSG_GIVEN_MSG => "XML message did not validate."; |
35
|
|
|
|
|
|
|
use constant PARSE_MSG_FAILED => 693; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
#My Variables |
38
|
|
|
|
|
|
|
my $parser; |
39
|
|
|
|
|
|
|
my $schema; |
40
|
|
|
|
|
|
|
my $validationError = undef; |
41
|
|
|
|
|
|
|
my $default_xsd_location = 'etc/bgp_monitor_2_00.xsd'; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 NAME |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
BGPmon::Validate |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
This module provides a way to validate XML messages from a BGPmon source |
50
|
|
|
|
|
|
|
against the published XSD. |
51
|
|
|
|
|
|
|
=cut |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 SYNOPSIS |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
use BGPmon::Validate; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
if(BGPmon::Validate::init()){ |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $err_code = BGPmon::Validate::get_error_code('init'); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $err_msg = BGPmon::Validate::get_error_msg('init'); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
print "$err_code : $err_msg\n"; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
exit 1; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $xml_msg; #put your own xml message here |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
if(BGPmon::Validate::validate($xml_msg)){ |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $err_code = BGPmon::Validate::get_error_code('validate'); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $err_msg = BGPmon::Validate::get_error_msg('validate'); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
print "$err_code : $err_msg\n"; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
exit 1; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
else{ |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
print "Message validated.\n"; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 EXPORT |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
init validate get_error_msg get_error_code |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 init |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Loads the default or desired XSD for the module to use for validation. |
101
|
|
|
|
|
|
|
If a different XSD is to be used a later time, simply run init again to load |
102
|
|
|
|
|
|
|
the other XSD. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Input: The desired XSD (optional) |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Output: 1 if there was an error loading the XSD |
107
|
|
|
|
|
|
|
0 if there were no errors |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
sub init{ |
111
|
|
|
|
|
|
|
my $xsdFilename = shift; |
112
|
|
|
|
|
|
|
my $fname = 'init'; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
if(!defined($xsdFilename) or $xsdFilename eq ""){ |
115
|
|
|
|
|
|
|
$xsdFilename = $default_xsd_location; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
$parser = XML::LibXML::->new(); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
eval{ |
121
|
|
|
|
|
|
|
$schema = XML::LibXML::Schema->new(location => $xsdFilename); |
122
|
|
|
|
|
|
|
} or do { |
123
|
|
|
|
|
|
|
$error_code{$fname} = XSD_ERROR; |
124
|
|
|
|
|
|
|
$error_msg{$fname} = $@; |
125
|
|
|
|
|
|
|
return 1; |
126
|
|
|
|
|
|
|
}; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
$error_code{$fname} = NO_ERROR_CODE; |
129
|
|
|
|
|
|
|
$error_msg{$fname} = NO_ERROR_MSG; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
return 0; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 validate |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Will check the XML message against the XSD loaded using BGPmon::Validate::init |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Input: A BGPmon message in XML format |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Output: 0 if the message validated |
143
|
|
|
|
|
|
|
1 if an error has occured |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |
146
|
|
|
|
|
|
|
sub validate{ |
147
|
|
|
|
|
|
|
my $xmlMsg = shift; |
148
|
|
|
|
|
|
|
my $fname = 'validate'; |
149
|
|
|
|
|
|
|
$validationError = undef; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
if(!defined($xmlMsg) or $xmlMsg eq ""){ |
152
|
|
|
|
|
|
|
$error_code{$fname} = NO_MSG_GIVEN; |
153
|
|
|
|
|
|
|
$error_msg{$fname} = NO_MSG_GIVEN_MSG; |
154
|
|
|
|
|
|
|
return 1; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
my $doc; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
eval{ |
161
|
|
|
|
|
|
|
$doc = $parser->parse_string($xmlMsg); |
162
|
|
|
|
|
|
|
} or do { |
163
|
|
|
|
|
|
|
$error_code{$fname} = PARSE_MSG_FAILED; |
164
|
|
|
|
|
|
|
$error_msg{$fname} = $@; |
165
|
|
|
|
|
|
|
return 1; |
166
|
|
|
|
|
|
|
}; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
eval {$schema->validate($doc)}; |
169
|
|
|
|
|
|
|
if($@){ |
170
|
|
|
|
|
|
|
$error_code{$fname} = INVALID_MSG_GIVEN; |
171
|
|
|
|
|
|
|
$error_msg{$fname} = INVALID_MSG_GIVEN_MSG; |
172
|
|
|
|
|
|
|
$validationError = $@->{'message'}; |
173
|
|
|
|
|
|
|
return 1; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
else{ |
176
|
|
|
|
|
|
|
$error_code{$fname} = NO_ERROR_CODE; |
177
|
|
|
|
|
|
|
$error_msg{$fname} = NO_ERROR_MSG; |
178
|
|
|
|
|
|
|
return 0; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head2 get_error_msg |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Will return the error message of the given function name. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Input: A string that contains the function name where an error occured. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Output: The message which represents the error stored from that function. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=cut |
194
|
|
|
|
|
|
|
sub get_error_msg{ |
195
|
|
|
|
|
|
|
my $str = shift; |
196
|
|
|
|
|
|
|
my $fname = 'get_error_msg'; |
197
|
|
|
|
|
|
|
my $toReturn = $error_msg{$str}; |
198
|
|
|
|
|
|
|
return $toReturn; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head2 get_error_code |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Will return the error code of the given function name. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Input: A string that represents the function name where an error occured. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Output: The code which represents the error stored from that function. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=cut |
210
|
|
|
|
|
|
|
sub get_error_code{ |
211
|
|
|
|
|
|
|
my $name = shift; |
212
|
|
|
|
|
|
|
my $fname = 'get_error_code'; |
213
|
|
|
|
|
|
|
my $toReturn = $error_code{$name}; |
214
|
|
|
|
|
|
|
return $toReturn; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head2 get_valid_error |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
Will return the schema validation error if one occurred. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Input: |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Output: The error code form schema validation or undef is there was no error. |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=cut |
226
|
|
|
|
|
|
|
sub get_valid_error{ |
227
|
|
|
|
|
|
|
my $name = shift; |
228
|
|
|
|
|
|
|
my $fname = 'get_valid_error'; |
229
|
|
|
|
|
|
|
return $validationError; |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
1; |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
__END__ |