reorg source code tree to make trunk
[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
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 our $state;     # FIXME check this!
49
50 my $tree = CWMP::Tree->new({ debug => 0 });
51
52 my $parser = XML::Rules->new(
53 #       start_rules => [
54 #               '^division_name,fax' => 'skip',
55 #       ],
56         namespaces => {
57                 'http://schemas.xmlsoap.org/soap/envelope/' => 'soapenv',
58                 'http://schemas.xmlsoap.org/soap/encoding/' => 'soap',
59                 'http://www.w3.org/2001/XMLSchema' => 'xsd',
60                 'http://www.w3.org/2001/XMLSchema-instance' => 'xsi',
61                 'urn:dslforum-org:cwmp-1-0' => '',
62         },
63         rules => [
64                 #_default => 'content trim',
65                 x_default => sub {
66                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
67                         warn dump( $tag_name, $tag_hash, $context );
68                 },
69                 'ID' => sub {
70                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
71                         $state->{ID} = $tag_hash->{_content};
72                 },
73                 #
74                 # Inform
75                 #
76                 'Inform' => sub {
77                         $state->{_dispatch} = 'InformResponse';         # what reponse to call
78                 },
79                 'DeviceId' => sub {
80                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
81                         foreach my $name ( keys %$tag_hash ) {
82                                 next if $name eq '_content';
83                                 my $key = $name;
84                                 $key =~ s/^\w+://;      # stip namespace
85                                 $state->{DeviceID}->{ $key } = _tag( $tag_hash, $name, '_content' );
86                         }
87                 },
88                 'EventStruct' => sub {
89                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
90                         push @{ $state->{EventStruct} }, $tag_hash->{EventCode}->{_content};
91                 },
92                 qr/(MaxEnvelopes|CurrentTime|RetryCount)/ => sub {
93                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
94                         $state->{$tag_name} = $tag_hash->{_content};
95                 },
96                 'ParameterValueStruct' => sub {
97                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
98                         # Name/Value tags must be case insnesitive
99                         my $value = (grep( /value/i, keys %$tag_hash ))[0];
100                         $state->{Parameter}->{ _tag($tag_hash, 'Name', '_content') } = _tag($tag_hash, 'Value', '_content' );
101                 },
102                 #
103                 # GetRPCMethodsResponse
104                 #
105                 qr/^(?:^\w+:)*string$/ => 'content array',
106                 'MethodList' => sub {
107                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
108                         $state->{MethodList} = _tag( $tag_hash, 'string' );
109                 },
110                 #
111                 # GetParameterNamesResponse
112                 #
113                 'ParameterInfoStruct' => sub {
114                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
115                         my $name = _tag($tag_hash, 'Name', '_content');
116                         my $writable = _tag($tag_hash, 'Writable', '_content' );
117
118                         confess "need state" unless ( $state ); # don't remove!
119
120                         # XXX dragons ahead: convert name to tree rewriting it into perl
121                         my $s = '$state->{ParameterInfo}->' . $tree->name2perl( $name ) . "->{writable} = $writable;";
122                         eval "$s";
123                         confess "can't eval $s : $@" if ($@);
124
125                         #warn "## state = dump( $state ), "\n";
126                 },
127                 #
128                 # Fault
129                 #
130                 'Fault' => sub {
131                         my ($tag_name, $tag_hash, $context, $parent_data) = @_;
132                         $state->{Fault} = {
133                                 FaultCode => _tag( $tag_hash, 'FaultCode', '_content' ),
134                                 FaultString => _tag( $tag_hash, 'FaultString', '_content' ),
135                         };
136                         warn "FAULT: ", $state->{Fault}->{FaultCode}, " ", $state->{Fault}->{FaultString}, "\n";
137                 }
138         ]
139 );
140
141 =head2 parse
142
143   my $state = CWMP::Request->parse( "<soap>request</soap>" );
144
145 =cut
146
147 sub parse {
148         my $self = shift;
149
150         my $xml = shift || confess "no xml?";
151
152         $state = {};
153         $parser->parsestring( $xml );
154         return $state;
155 }
156
157 1;