99770fa6290f5aa10214a98e924c55458fcde9be
[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 GetParameterAttributesResponse
107
108 =cut
109
110 push @$rules,
111                 'ParameterAttributeStruct' => sub {
112                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
113                         warn dump( $tag_name, $tag_hash, $context );
114         
115                         confess "need state" unless ( $state ); # don't remove!
116
117                         my $name = _tag($tag_hash, 'Name', '_content');
118
119
120                         $state->{ParameterAttribute}->{$name} = {
121                                 Notification => _tag($tag_hash, 'Notification', '_content' ),
122                                 AccessList => _tag($tag_hash, 'AccessList', 'string' ),
123                         };
124
125                         $state->{_trigger} = 'GetParameterAttributesResponse';
126                 };
127
128 =head2 Fault
129
130 =cut
131
132 push @$rules,
133                 'Fault' => sub {
134                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
135                         $state->{Fault} = {
136                                 FaultCode => _tag( $tag_hash, 'FaultCode', '_content' ),
137                                 FaultString => _tag( $tag_hash, 'FaultString', '_content' ),
138                         };
139                         warn "FAULT: ", $state->{Fault}->{FaultCode}, " ", $state->{Fault}->{FaultString}, "\n";
140                         $state->{_trigger} = 'Fault';
141                 };
142
143 =head1 METHODS
144
145 =head2 parse
146
147   my $state = CWMP::Request->parse( "<soap>request</soap>" );
148
149 =cut
150
151 sub parse {
152         my $self = shift;
153
154         my $xml = shift || confess "no xml?";
155
156         $state = {};
157
158         my $parser = XML::Rules->new(
159 #               start_rules => [
160 #                       '^division_name,fax' => 'skip',
161 #               ],
162                 namespaces => {
163                         'http://schemas.xmlsoap.org/soap/envelope/' => 'soapenv',
164                         'http://schemas.xmlsoap.org/soap/encoding/' => 'soap',
165                         'http://www.w3.org/2001/XMLSchema' => 'xsd',
166                         'http://www.w3.org/2001/XMLSchema-instance' => 'xsi',
167                         'urn:dslforum-org:cwmp-1-0' => '',
168                 },
169                 rules => $rules,
170         );
171
172 #       warn "## created $parser\n";
173
174         $parser->parsestring( $xml );
175
176         undef $parser;
177
178         if ( my $trigger = $state->{_trigger} ) {
179                 warn "### call_trigger( $trigger )\n";
180                 $self->call_trigger( $trigger, $state );
181         }
182         # XXX propagate _trigger (useful for symlinks)
183
184         return $state;
185 }
186
187 =head2 _tag
188
189 Get value of tag. Tag name is case insensitive (don't ask why),
190 we ignore namespaces and can take optional C<sub_key>
191 (usually C<_content>).
192
193   _tag( $tag_hash, $name, $sub_key )
194
195 =cut
196
197 sub _tag {
198         my ( $tag_hash, $name, $sub_key ) = @_;
199         confess "need hash as first argument" unless ( ref $tag_hash eq 'HASH' );
200         $name = (grep { m/^(?:\w+:)*$name$/i } keys %$tag_hash )[0];
201 #       $name =~ s/^\w+://;
202         if ( defined $tag_hash->{$name} ) {
203                 if ( ! defined $sub_key ) {
204                         return $tag_hash->{$name};
205                 } elsif ( defined $tag_hash->{$name}->{$sub_key} ) {
206                         return $tag_hash->{$name}->{$sub_key};
207                 } else {
208                         return if ( $name =~ m/^value$/i );
209                         warn "can't find '$name/$sub_key' in ", dump( $tag_hash );
210                         return;
211                 }
212         } else {
213                 warn "can't find '$name' in ", dump( $tag_hash );
214                 return;
215         }
216 }
217
218 1;