r260@brr: dpavlin | 2007-11-25 13:51:25 +0100
[perl-cwmp.git] / lib / CWMP / Request.pm
1 package CWMP::Request;
2
3 use warnings;
4 use strict;
5
6 use XML::Rules;
7 use Data::Dump qw/dump/;
8 use Carp qw/confess cluck/;
9 use Class::Trigger;
10
11 #use Devel::LeakTrace::Fast;
12
13 =head1 NAME
14
15 CWMP::Request - parse SOAP request metods
16
17 =head1 CPE metods
18
19 All methods described below call triggers with same name
20
21 =cut
22
23 our $state;     # FIXME check this!
24
25 our $rules =  [
26                 #_default => 'content trim',
27                 x_default => sub {
28                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
29                         warn dump( $tag_name, $tag_hash, $context );
30                 },
31                 'ID' => sub {
32                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
33                         $state->{ID} = $tag_hash->{_content};
34                 },
35
36                 'DeviceId' => sub {
37                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
38                         foreach my $name ( keys %$tag_hash ) {
39                                 next if $name eq '_content';
40                                 my $key = $name;
41                                 $key =~ s/^\w+://;      # stip namespace
42                                 $state->{DeviceID}->{ $key } = _tag( $tag_hash, $name, '_content' );
43                         }
44                 },
45                 'EventStruct' => sub {
46                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
47                         push @{ $state->{EventStruct} }, $tag_hash->{EventCode}->{_content};
48                 },
49                 qr/(MaxEnvelopes|CurrentTime|RetryCount)/ => sub {
50                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
51                         $state->{$tag_name} = $tag_hash->{_content};
52                 },
53                 'ParameterValueStruct' => sub {
54                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
55                         # Name/Value tags must be case insnesitive
56                         my $value = (grep( /value/i, keys %$tag_hash ))[0];
57                         $state->{Parameter}->{ _tag($tag_hash, 'Name', '_content') } = _tag($tag_hash, 'Value', '_content' );
58                         $state->{_trigger} = 'ParameterValue';
59                 },
60
61 ];
62
63 =head2 Inform
64
65 Generate InformResponse to CPE
66
67 =cut
68
69 push @$rules,
70         'Inform' => sub {
71                 $state->{_dispatch} = 'InformResponse';         # what reponse to call
72                 $state->{_trigger} = 'Inform';
73         };
74
75 =head2 GetRPCMethodsResponse
76
77 =cut
78
79 push @$rules,
80                 qr/^(?:^\w+:)*string$/ => 'content array',
81                 'MethodList' => sub {
82                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
83                         $state->{MethodList} = _tag( $tag_hash, 'string' );
84                         $state->{_trigger} = 'GetRPCMethodsResponse';
85                 };
86
87 =head2 GetParameterNamesResponse
88
89 =cut
90
91 push @$rules,
92                 'ParameterInfoStruct' => sub {
93                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
94                         my $name = _tag($tag_hash, 'Name', '_content');
95                         my $writable = _tag($tag_hash, 'Writable', '_content' );
96
97                         confess "need state" unless ( $state ); # don't remove!
98
99                         $state->{ParameterInfo}->{$name} = $writable;
100
101                         #warn "## state = dump( $state ), "\n";
102
103                         $state->{_trigger} = 'GetParameterNamesResponse';
104                 };
105         
106 =head2 Fault
107
108 =cut
109
110 push @$rules,
111                 'Fault' => sub {
112                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
113                         $state->{Fault} = {
114                                 FaultCode => _tag( $tag_hash, 'FaultCode', '_content' ),
115                                 FaultString => _tag( $tag_hash, 'FaultString', '_content' ),
116                         };
117                         warn "FAULT: ", $state->{Fault}->{FaultCode}, " ", $state->{Fault}->{FaultString}, "\n";
118                         $state->{_trigger} = 'Fault';
119                 };
120
121 =head1 METHODS
122
123 =head2 parse
124
125   my $state = CWMP::Request->parse( "<soap>request</soap>" );
126
127 =cut
128
129 my $parser = XML::Rules->new(
130 #               start_rules => [
131 #                       '^division_name,fax' => 'skip',
132 #               ],
133                 namespaces => {
134                         'http://schemas.xmlsoap.org/soap/envelope/' => 'soapenv',
135                         'http://schemas.xmlsoap.org/soap/encoding/' => 'soap',
136                         'http://www.w3.org/2001/XMLSchema' => 'xsd',
137                         'http://www.w3.org/2001/XMLSchema-instance' => 'xsi',
138                         'urn:dslforum-org:cwmp-1-0' => '',
139                 },
140                 rules => $rules,
141 );
142
143 sub parse {
144         my $self = shift;
145
146         my $xml = shift || confess "no xml?";
147
148         $state = {};
149
150         $parser->parsestring( $xml );
151         if ( my $trigger = $state->{_trigger} ) {
152                 warn "### call_trigger( $trigger )\n";
153                 $self->call_trigger( $trigger, $state );
154         }
155         # XXX don't propagate _trigger (useful?)
156         delete( $state->{_trigger} );
157         return $state;
158 }
159
160 =head2 _tag
161
162 Get value of tag. Tag name is case insensitive (don't ask why),
163 we ignore namespaces and can take optional C<sub_key>
164 (usually C<_content>).
165
166   _tag( $tag_hash, $name, $sub_key )
167
168 =cut
169
170 sub _tag {
171         my ( $tag_hash, $name, $sub_key ) = @_;
172         confess "need hash as first argument" unless ( ref $tag_hash eq 'HASH' );
173         $name = (grep { m/^(?:\w+:)*$name$/i } keys %$tag_hash )[0];
174 #       $name =~ s/^\w+://;
175         if ( defined $tag_hash->{$name} ) {
176                 if ( ! defined $sub_key ) {
177                         return $tag_hash->{$name};
178                 } elsif ( defined $tag_hash->{$name}->{$sub_key} ) {
179                         return $tag_hash->{$name}->{$sub_key};
180                 } else {
181                         return if ( $name =~ m/^value$/i );
182                         warn "can't find '$name/$sub_key' in ", dump( $tag_hash );
183                         return;
184                 }
185         } else {
186                 warn "can't find '$name' in ", dump( $tag_hash );
187                 return;
188         }
189 }
190
191 1;