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