07b93f4c8ccd65a9806b8bcf8da3d278674524f8
[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                         chomp( $state->{ID} );
35                 },
36
37                 'DeviceId' => sub {
38                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
39                         foreach my $name ( keys %$tag_hash ) {
40                                 next if $name eq '_content';
41                                 my $key = $name;
42                                 $key =~ s/^\w+://;      # stip namespace
43                                 $state->{DeviceId}->{ $key } = _tag( $tag_hash, $name, '_content' );
44                         }
45                 },
46                 'EventStruct' => sub {
47                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
48                         push @{ $state->{EventStruct} }, $tag_hash->{EventCode}->{_content};
49                 },
50                 qr/(MaxEnvelopes|CurrentTime|RetryCount)/ => sub {
51                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
52                         $state->{$tag_name} = $tag_hash->{_content};
53                 },
54                 'ParameterValueStruct' => sub {
55                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
56                         # Name/Value tags must be case insnesitive
57                         my $value = (grep( /value/i, keys %$tag_hash ))[0];
58                         $state->{Parameter}->{ _tag($tag_hash, 'Name', '_content') } = _tag($tag_hash, 'Value', '_content' );
59                         $state->{_trigger} = 'ParameterValue';
60                 },
61
62 ];
63
64 =head2 Inform
65
66 Generate InformResponse to CPE
67
68 =cut
69
70 push @$rules,
71         'Inform' => sub {
72                 $state->{_dispatch} = 'InformResponse';         # what reponse to call
73                 $state->{_trigger} = 'Inform';
74         };
75
76 =head2 GetRPCMethodsResponse
77
78 =cut
79
80 push @$rules,
81                 qr/^(?:^\w+:)*string$/ => 'content array',
82                 'MethodList' => sub {
83                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
84                         $state->{MethodList} = _tag( $tag_hash, 'string' );
85                         $state->{_trigger} = 'GetRPCMethodsResponse';
86                 };
87
88 =head2 GetParameterNamesResponse
89
90 =cut
91
92 push @$rules,
93                 'ParameterInfoStruct' => sub {
94                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
95                         my $name = _tag($tag_hash, 'Name', '_content');
96                         my $writable = _tag($tag_hash, 'Writable', '_content' );
97
98                         confess "need state" unless ( $state ); # don't remove!
99
100                         $state->{ParameterInfo}->{$name} = $writable;
101
102                         #warn "## state = dump( $state ), "\n";
103
104                         $state->{_trigger} = 'GetParameterNamesResponse';
105                 };
106         
107 =head2 GetParameterAttributesResponse
108
109 =cut
110
111 push @$rules,
112                 'ParameterAttributeStruct' => sub {
113                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
114                         warn dump( $tag_name, $tag_hash, $context );
115         
116                         confess "need state" unless ( $state ); # don't remove!
117
118                         my $name = _tag($tag_hash, 'Name', '_content');
119
120
121                         $state->{ParameterAttribute}->{$name} = {
122                                 Notification => _tag($tag_hash, 'Notification', '_content' ),
123                                 AccessList => _tag($tag_hash, 'AccessList', 'string' ),
124                         };
125
126                         $state->{_trigger} = 'GetParameterAttributesResponse';
127                 };
128
129 =head2 Fault
130
131 =cut
132
133 push @$rules,
134                 'Fault' => sub {
135                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
136                         $state->{Fault} = {
137                                 FaultCode => _tag( $tag_hash, 'FaultCode', '_content' ),
138                                 FaultString => _tag( $tag_hash, 'FaultString', '_content' ),
139                         };
140                         warn "FAULT: ", $state->{Fault}->{FaultCode}, " ", $state->{Fault}->{FaultString}, "\n";
141                         $state->{_trigger} = 'Fault';
142                 };
143
144 =head1 METHODS
145
146 =head2 parse
147
148   my $state = CWMP::Request->parse( "<soap>request</soap>" );
149
150 =cut
151
152 sub parse {
153         my $self = shift;
154
155         my $xml = shift || confess "no xml?";
156
157         $state = {};
158
159         my $parser = XML::Rules->new(
160 #               start_rules => [
161 #                       '^division_name,fax' => 'skip',
162 #               ],
163                 namespaces => {
164                         'http://schemas.xmlsoap.org/soap/envelope/' => 'soapenv',
165                         'http://schemas.xmlsoap.org/soap/encoding/' => 'soap',
166                         'http://www.w3.org/2001/XMLSchema' => 'xsd',
167                         'http://www.w3.org/2001/XMLSchema-instance' => 'xsi',
168                         'urn:dslforum-org:cwmp-1-0' => '',
169                 },
170                 rules => $rules,
171         );
172
173 #       warn "## created $parser\n";
174
175         $parser->parsestring( $xml );
176
177         undef $parser;
178
179         if ( my $trigger = $state->{_trigger} ) {
180                 warn "### call_trigger( $trigger )\n";
181                 $self->call_trigger( $trigger, $state );
182         }
183         # XXX propagate _trigger (useful for symlinks)
184
185         return $state;
186 }
187
188 =head2 _tag
189
190 Get value of tag. Tag name is case insensitive (don't ask why),
191 we ignore namespaces and can take optional C<sub_key>
192 (usually C<_content>).
193
194   _tag( $tag_hash, $name, $sub_key )
195
196 =cut
197
198 sub _tag {
199         my ( $tag_hash, $name, $sub_key ) = @_;
200         confess "need hash as first argument" unless ( ref $tag_hash eq 'HASH' );
201         $name = (grep { m/^(?:\w+:)*$name$/i } keys %$tag_hash )[0];
202 #       $name =~ s/^\w+://;
203         if ( defined $tag_hash->{$name} ) {
204                 if ( ! defined $sub_key ) {
205                         return $tag_hash->{$name};
206                 } elsif ( defined $tag_hash->{$name}->{$sub_key} ) {
207                         return $tag_hash->{$name}->{$sub_key};
208                 } else {
209                         return if ( $name =~ m/^value$/i );
210                         warn "can't find '$name/$sub_key' in ", dump( $tag_hash );
211                         return;
212                 }
213         } else {
214                 warn "can't find '$name' in ", dump( $tag_hash );
215                 return;
216         }
217 }
218
219 1;