re-enable all_parameteres collection of first connect
[perl-cwmp.git] / bin / dump2test.pl
1 #!/usr/bin/perl -w
2
3 # dump2test.pl
4 #
5 # convert dump from one CPE to test files
6
7 use strict;
8 use File::Find;
9 use File::Path qw/mkpath/;
10 use Data::Dump qw/dump/;
11 use File::Slurp;
12 use blib;
13 use CWMP::Request;
14 use YAML::Syck;
15
16 my $path = shift @ARGV || die "usage: $0 dump/client_ip/\n";
17
18 my $requests;
19
20 find({
21         no_chdir => 1,
22         wanted => sub {
23                 my $path = $File::Find::name;
24
25                 return unless -l $path;
26
27                 if ( $path =~ m!\d+-(.+)!) {
28                         my $name = $1;
29                         $requests->{$name} = $path;
30                 } else {
31                         warn "can't find request name in $path\n";
32                 }
33
34         }
35 }, $path);
36
37 warn "## requests = ",dump( $requests );
38
39 my $test_path = 't/dump/';
40
41 sub xml2state {
42         my $xml = shift;
43         $xml =~ s/^.*?</</s;
44         return CWMP::Request->parse( $xml );
45 }
46
47 if ( my $i = $requests->{Inform} ) {
48
49         my $xml = read_file($i);
50
51 #       warn "## xml: $xml\n";
52
53         my $state = xml2state( $xml );
54
55 #       warn "## state = ",dump( $state );
56
57         $test_path .=
58                 $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.HardwareVersion'}
59                 . '/' .
60                 $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.SoftwareVersion'}
61                 ;
62
63 } else {
64         die "need Inform found ", dump( $requests );
65 }
66
67 mkpath $test_path unless -e $test_path;
68
69 warn "dumping new tests into $test_path\n";
70
71 foreach my $name ( keys %$requests ) {
72         my $from = $requests->{$name};
73         my $to   = "$test_path/$name";
74         if ( -e $to ) {
75                 warn "SKIP $to\n";
76                 next;
77         }
78         warn "## $from -> $to\n";
79         my $xml = read_file( $from );
80         write_file( $to, $xml );
81         DumpFile( "$to.yml", xml2state( $xml ) );
82         warn "created $to\n";
83 }