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