blib/lib/Convert/Cisco.pm

filter: Statementstmt 88.1% 89 / 101 Branchbran 63.3% 19 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 84.2% 123 / 146 CC 17 SCAR 15.9
CC
17
Cov
82%
_decodeCDE
8.7
to_xml
8.1
new
3.0
CRAP 4.9
↑ Summary
1 #=============================================================
2
3 package Convert::Cisco;
4
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 3 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Log::Log4perl qw(get_logger);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use YAML qw(Dump Load);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use XML::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 =head1 NAME
16
17 Convert::Cisco - Module for converting Cisco billing records
18
19 =head1 VERSION
20
21 Version 0.06
22
23 =cut
24
25 3 our $VERSION = '0.06';
26
27 =head1 SYNOPSIS
28
29 Convert Cisco billing record binary files. The format is available on the
30 Cisco Website:
31
32 http://www.cisco.com/univercd/cc/td/doc/product/access/sc/rel9/billinf/r9chap1.htm
33
34 Module used to convert Cisco billing records into XML
35
36 use Convert::Cisco;
37
38 my $obj = Convert::Cisco->new(stylesheet=>"cisco.xsl");
39 $obj->to_xml("test.bin", "test.xml");
40
41 =head1 FUNCTIONS
42
43 =cut
44
45 #-------------------------------------------------------------
46
47 =head2 new
48
49 Constructor method, has the following optional parameters:
50
51 =over
52
53 =item stylesheet
54
55 Adds a xml-stylesheet processing instruction to the top of all converted XML files
56
57 <?xml-stylesheet href="cisco.xsl" type="text/xsl"?>
58
59 =item config
60
61 Billing record configuration data expressed in YAML format. This option is normally
62 only used by the tests because it over-rides the modules field configuration.
63
64 =back
65
66 =cut
67
68 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
69 3 my $class = shift;
70 3 my (%args) = @_;
71 3 my $self = bless {stylesheet=>undef, config=>undef, %args}, $class;
72
73 ### Load record configuration
74 3 ▶ if (defined $self->{config}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'config'}) { } 1 2 3
75 1 $self->{_config} = Load($self->{config});
76 }
77 else {
78 2 $self->{_config} = Load(join("\n", <Convert::Cisco::DATA>));
79 }
80
81 ### Default configuration
82 3 ▶ unless (exists $self->{_config}{"CDE Records"}{6003}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_config"}{"CDE Records"}{6003}) 2 1 3
83 2 $self->{_config}{"CDE Records"}{6003} = {name=>"record_count", spec=>"N"};
84 }
85
86 3 return $self;
87 }
88
89 #-------------------------------------------------------------
90
91 # _decodeCDB
92 #
93 # Returns the configured name for the CDB record
94 #
95
96 42 ▶sub _decodeCDB {
Subroutine _decodeCDB: called CC=2 SCAR=7.2
97 42 my ($self, $key) = @_;
98 42 my $log = get_logger;
99
100 42 ▶ if (exists $self->{_config}{"CDB Names"}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_config'}{'CDB Names'}{$key}) { } 0 42 42
101 0 return $self->{_config}{"CDB Names"}{$key};
102 }
103 else {
104 42 $log->warn("CDB not configured: ", $key);
105 42 return "UNKNOWN"
106 }
107 }
108
109 #-------------------------------------------------------------
110
111 # _decodeCDE
112 #
113 # Unpacks the CDE record based on the configured specification
114 #
115
116 1290 ▶sub _decodeCDE {
Subroutine _decodeCDE: called CC=6 SCAR=21.7
117 1290 my ($self, $key, $value) = @_;
118 1290 my $log = get_logger;
119 1290 my $decodeValue;
120 my $decodeName;
121 1290 my $decodeValueUnformatted;
122
123 1290 ▶ if (exists $self->{_config}{"CDE Records"}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_config'}{'CDE Records'}{$key}) { } 4 1286 1290
124
125 4 $decodeName = $self->{_config}{"CDE Records"}{$key}{name};
126 4 my $spec = $self->{_config}{"CDE Records"}{$key}{spec};
127 4 my $format = $self->{_config}{"CDE Records"}{$key}{format};
128
129 ### Handling for multi-part records
130 4 ▶ if (ref($spec) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec eq 'ARRAY') { } 0 4 4
131 0 $decodeValue = join("-", unpack(join(" ", @{$spec}), $value));
132 }
133 else {
134 4 $decodeValue = unpack($spec, $value);
135 }
136
137 ### Optional output formatting
138 4 ▶ if (defined $format) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $format) 0 4 4
139 0 $decodeValueUnformatted = $decodeValue;
140
141 0 ▶ if ($format eq "epoch2datetime") {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'epoch2datetime') { } 0 0 0
elsif ($format eq 'compoundEpoch2datetime') { } 0 0 0
142 0 $decodeValue = DateTime->from_epoch(epoch => $decodeValueUnformatted)->datetime;
143 }
144 elsif ($format eq "compoundEpoch2datetime") {
145 0 my @timeComponents = split("-", $decodeValueUnformatted);
146 0 $decodeValue = DateTime->from_epoch(epoch => $timeComponents[0])->datetime.".".$timeComponents[1];
147 }
148 else {
149 0 $log->warn("Unsupported format configured for CDE: $key");
150 }
151 }
152 }
153 else {
154 1286 $log->warn("CDE not configured: ", $key);
155 1286 $decodeName = "UNKNOWN";
156 1286 $decodeValue = unpack("H*", $value);
157 }
158
159 1290 return ($decodeValue, $decodeName, $decodeValueUnformatted);
160 }
161
162 #-------------------------------------------------------------
163
164 # _stylesheetDecl
165 #
166 # Write XSL stylesheet declaration
167 #
168
169 2 ▶sub _stylesheetDecl {
Subroutine _stylesheetDecl: called CC=2 SCAR=8.1
170 2 my ($self, $writer) = @_;
171
172 2 ▶ if (defined $self->{stylesheet}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"stylesheet"}) 0 2 2
173 0 $writer->pi('xml-stylesheet', sprintf('href="%s" type="%s"', $self->{stylesheet}, "text/xsl"));
174 }
175 }
176
177 #-------------------------------------------------------------
178
179 =head2 to_xml
180
181 Converts a file into XML format. The current record format is:
182
183 <?xml version="1.0" encoding="UTF-8"?>
184 <cdrs>
185 ..
186 ..
187 <cdb tag="1110" name="EndOfCall">
188 <cde name="calling_party_category" tag="3000">0a</cde>
189 <cde name="user_service_info" tag="3001">8090a3</cde>
190 <cde name="calling_number_nature_of_address" tag="3003">02</cde>
191 ..
192 ..
193 </cdb>
194 ..
195 ..
196
197 B<NOTE:>
198
199 The XML format is subject to change and needs an associated XML DTD or Schema.
200
201 =cut
202
203 3 ▶sub to_xml {
Subroutine to_xml: called CC=8 SCAR=20.9
Pod documented
204 3 my ($self, $filename, $filename_output) = @_;
205 3 my $log = get_logger;
206
207 ### Print the name of the file processed
208 3 $log->debug("Processing: ", $filename);
209
210 ### Input file
211 3 ▶ my $infile = new FileHandle($filename, "r") or $log->logcroak("Cannot open $filename - $!");
Branch: 1 branch
BranchTrueFalseTotal
my $infile = "FileHandle"->new($filename, "r") or $log->logcroak("Cannot open $filename - $!") 2 1 3
212 2 binmode $infile;
213
214 ### Output file
215 2 ▶ my $file = new FileHandle($filename_output, "w") or $log->logcroak("Cannot open $filename_output - $!");
Branch: 1 branch
BranchTrueFalseTotal
my $file = "FileHandle"->new($filename_output, "w") or $log->logcroak("Cannot open $filename_output - $!") 2 0 2
216
217 ### XML Writer object
218 2 my $writer = XML::Writer->new(OUTPUT => $file, DATA_MODE=>1, DATA_INDENT=>2);
219
220 ### Write the start of the file
221 2 $writer->xmlDecl("UTF-8");
222 2 $self->_stylesheetDecl($writer);
223 2 $writer->startTag("cdrs");
224
225 ### Convert the file into CSV format
226 2 my $bin;
227 2 my $i = 0;
228 2 my $recordCount = 0;
229
230 2 ▶ while ($infile->read($bin, 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($infile->read($bin, 4)) 42 2 44
231 42 $i++;
232
233 ### Read the Call Data Block
234 42 my ($cdbTag, $length) = unpack("n2", $bin);
235 42 $infile->read($bin, $length);
236
237 ### Decode the Call Data Elements
238 # TLV format : Tag, Length, Value
239 42 my %cde = unpack("(n n/a*)*", $bin);
240
241 ### Dump the CDB record
242 42 $log->debug("CDB Record:\n", { filter => \&Dump, value => [$cdbTag, \%cde] });
243
244 ### Start the "cdb" block
245 42 $writer->startTag("cdb", tag => $cdbTag, name => $self->_decodeCDB($cdbTag));
246
247 42 foreach my $cdeTag ( sort keys %cde ) {
248 1288 my ($value, $name, $raw) = $self->_decodeCDE($cdeTag, $cde{$cdeTag});
249
250 1288 ▶ if (defined $raw) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw) { } 0 1288 1288
251 0 $writer->dataElement("cde", $value, tag=>$cdeTag, name=>$name, raw=>$raw);
252 }
253 else {
254 1288 $writer->dataElement("cde", $value, tag=>$cdeTag, name=>$name);
255 }
256 }
257
258 ### End "cdb" block
259 42 $writer->endTag("cdb");
260
261 ### Read number of records from Footer record
262 42 ▶ if ($cdbTag == 1100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cdbTag == 1100) 2 40 42
263 2 ($recordCount) = $self->_decodeCDE(6003, $cde{6003});
264 }
265 }
266
267 ### Audit check
268 2 ▶ if ($i != $recordCount) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i != $recordCount) 0 2 2
269 0 $log->logcroak("Footer does not match number of records");
270 }
271
272 ### Cleanup
273 2 $infile->close;
274 2 $writer->endTag("cdrs");
275 2 $file->print("\n");
276 2 $file->close;
277 }
278
279 #-------------------------------------------------------------
280
281 =head1 AUTHOR
282
283 Mark O'Connor, C<< <marko@cpan.org> >>
284
285 =head1 BUGS
286
287 Please report any bugs or feature requests to
288 C<bug-convert-cisco at rt.cpan.org>, or through the web interface at
289 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Convert-Cisco>.
290 I will be notified, and then you'll automatically be notified of progress on
291 your bug as I make changes.
292
293 =head1 SUPPORT
294
295 You can find documentation for this module with the perldoc command.
296
297 perldoc Convert::Cisco
298
299 You can also look for information at:
300
301 =over 4
302
303 =item * AnnoCPAN: Annotated CPAN documentation
304
305 L<http://annocpan.org/dist/Convert-Cisco>
306
307 =item * CPAN Ratings
308
309 L<http://cpanratings.perl.org/d/Convert-Cisco>
310
311 =item * RT: CPAN's request tracker
312
313 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Convert-Cisco>
314
315 =item * Search CPAN
316
317 L<http://search.cpan.org/dist/Convert-Cisco>
318
319 =back
320
321 =head1 ACKNOWLEDGEMENTS
322
323 =head1 COPYRIGHT & LICENSE
324
325 Copyright 2007 Mark O'Connor, all rights reserved.
326
327 This program is free software; you can redistribute it and/or modify it
328 under the same terms as Perl itself.
329
330 =cut
331
332 3 1; # End of Convert::Cisco
333
334 __DATA__
↑ Summary