r210@brr: dpavlin | 2007-11-14 19:15:41 +0100
[perl-cwmp.git] / lib / CWMP / Methods.pm
1 package CWMP::Methods;
2
3 use strict;
4 use warnings;
5
6
7 use base qw/Class::Accessor/;
8 __PACKAGE__->mk_accessors( qw/debug/ );
9
10 use XML::Generator;
11 use Carp qw/confess/;
12 use Data::Dump qw/dump/;
13
14 =head1 NAME
15
16 CWMP::Methods - generate SOAP meesages for CPE
17
18 =head1 METHODS
19
20 =head2 new
21
22   my $method = CWMP::Methods->new({ debug => 1 });
23
24 =cut
25
26 sub new {
27         my $class = shift;
28         my $self = $class->SUPER::new( @_ );
29
30         warn "created XML::Generator object\n" if $self->debug;
31
32         return $self;
33 }
34
35 =head2 xml
36
37 Used to implement methods which modify just body of soap message.
38 For examples, see source of this module.
39
40 =cut
41
42 my $cwmp = [ cwmp => 'urn:dslforum-org:cwmp-1-0' ];
43 my $soap = [ soap => 'http://schemas.xmlsoap.org/soap/envelope/' ];
44 my $xsd  = [ xsd  => 'http://www.w3.org/2001/XMLSchema-instance' ];
45
46 sub xml {
47         my $self = shift;
48
49         my ( $state, $closure ) = @_;
50
51         confess "no state?" unless ($state);
52         confess "no body closure" unless ( $closure );
53
54         confess "no ID in state ", dump( $state ) unless ( $state->{ID} );
55
56         #warn "state used to generate xml = ", dump( $state ) if $self->debug;
57
58         my $X = XML::Generator->new(':pretty');
59
60         return $X->Envelope( $soap, { 'soap:encodingStyle' => "http://schemas.xmlsoap.org/soap/encoding/" },
61                 $X->Header( $soap,
62                         $X->ID( $cwmp, { mustUnderstand => 1 }, $state->{ID} ),
63                         $X->NoMoreRequests( $cwmp, $state->{NoMoreRequests} || 0 ),
64                 ),
65                 $X->Body( $soap, $closure->( $X, $state ) ),
66         );
67 }
68
69 =head1 CPE methods
70
71 =head2 GetRPCMethods
72
73   $method->GetRPCMethods( $state );
74
75 =cut
76
77 sub GetRPCMethods {
78         my ( $self, $state ) = @_;
79         $self->xml( $state, sub {
80                 my ( $X, $state ) = @_;
81                 $X->GetRPCMethods();
82         });
83 };
84
85 =head2 SetParameterValues
86
87   $method->SetParameterValues( $state, {
88         param1 => 'value1',
89         param2 => 'value2',
90         ...
91   });
92
93 It doesn't support base64 encoding of values yet.
94
95 To preserve data, it does support repeatable parametar names.
96 Behaviour on this is not defined in protocol.
97
98 =cut
99
100 sub SetParameterValues {
101         my $self = shift;
102         my $state = shift;
103
104         confess "SetParameterValues needs parameters" unless @_;
105
106         my $params = shift || return;
107
108         warn "# SetParameterValues = ", dump( $params ), "\n" if $self->debug;
109
110         $self->xml( $state, sub {
111                 my ( $X, $state ) = @_;
112
113                 $X->SetParameterValues( $cwmp,
114                         $X->ParameterList( $cwmp,
115                                 $X->ParameterNames( $cwmp,
116                                         map {
117                                                 $X->ParameterValueStruct( $cwmp,
118                                                         $X->Name( $cwmp, $_ ),
119                                                         $X->Value( $cwmp, $params->{$_} )
120                                                 )
121                                         } sort keys %$params
122                                 )
123                         )
124                 );
125         });
126 }
127
128
129 =head2 GetParameterValues
130
131   $method->GetParameterValues( $state, [ 'ParameterName', ... ] );
132
133 =cut
134
135 sub _array_param {
136         my $v = shift;
137         confess "array_mandatory(",dump($v),") isn't ARRAY" unless ref($v) eq 'ARRAY';
138         return @$v;
139 }
140
141 sub GetParameterValues {
142         my $self = shift;
143         my $state = shift;
144         my @ParameterNames = _array_param(shift);
145         confess "GetParameterValues need ParameterNames" unless @ParameterNames;
146         warn "# GetParameterValues", dump( @ParameterNames ), "\n" if $self->debug;
147
148         $self->xml( $state, sub {
149                 my ( $X, $state ) = @_;
150
151                 $X->GetParameterValues( $cwmp,
152                         $X->ParameterNames( $cwmp,
153                                 map {
154                                         $X->string( $xsd, $_ )
155                                 } @ParameterNames
156                         )
157                 );
158         });
159 }
160
161 =head2 GetParameterNames
162
163   $method->GetParameterNames( $state, [ $ParameterPath, $NextLevel ] );
164
165 =cut
166
167 sub GetParameterNames {
168         my ( $self, $state, $param ) = @_;
169         # default: all, all
170         my ( $ParameterPath, $NextLevel ) = _array_param( $param );
171         $ParameterPath ||= '';
172         $NextLevel ||= 0;
173         warn "# GetParameterNames( '$ParameterPath', $NextLevel )\n" if $self->debug;
174         $self->xml( $state, sub {
175                 my ( $X, $state ) = @_;
176
177                 $X->GetParameterNames( $cwmp,
178                         $X->ParameterPath( $cwmp, $ParameterPath ),
179                         $X->NextLevel( $cwmp, $NextLevel ),
180                 );
181         });
182 }
183
184 =head2 Reboot
185
186   $method->Reboot( $state );
187
188 =cut
189
190 sub Reboot {
191         my ( $self, $state ) = @_;
192         $self->xml( $state, sub {
193                 my ( $X, $state ) = @_;
194                 $X->Reboot();
195         });
196 }
197
198
199 =head1 Server methods
200
201
202 =head2 InformResponse
203
204   $method->InformResponse( $state );
205
206 =cut
207
208 sub InformResponse {
209         my ( $self, $state ) = @_;
210         $self->xml( $state, sub {
211                 my ( $X, $state ) = @_;
212                 $X->InformResponse( $cwmp,
213                         $X->MaxEnvelopes( $cwmp, 1 )
214                 );
215         });
216 }
217
218 =head1 BUGS
219
220 All other methods are unimplemented.
221
222 =cut
223
224 1;