evil hack to allow introspection of running server
[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         #warn "state used to generate xml = ", dump( $state ) if $self->debug;
55
56         my $X = XML::Generator->new(':pretty');
57
58         my @header;
59         push( @header, $X->ID( $cwmp, { mustUnderstand => 1 }, $state->{ID} )) if $state->{ID};
60         push( @header, $X->NoMoreRequests( $cwmp, $state->{NoMoreRequests} || 0 ));
61
62         return $X->Envelope( $soap, { 'soap:encodingStyle' => "http://schemas.xmlsoap.org/soap/encoding/" },
63                 $X->Header( $soap, @header ),
64                 $X->Body( $soap, $closure->( $X, $state ) ),
65         );
66 }
67
68 =head1 CPE methods
69
70 =head2 GetRPCMethods
71
72   $method->GetRPCMethods( $state );
73
74 =cut
75
76 sub GetRPCMethods {
77         my ( $self, $state ) = @_;
78         $self->xml( $state, sub {
79                 my ( $X, $state ) = @_;
80                 $X->GetRPCMethods( $cwmp, '' );
81         });
82 };
83
84 =head2 SetParameterValues
85
86   $method->SetParameterValues( $state, {
87         param1 => 'value1',
88         param2 => 'value2',
89         ...
90   });
91
92 It doesn't support base64 encoding of values yet.
93
94 To preserve data, it does support repeatable parametar names.
95 Behaviour on this is not defined in protocol.
96
97 =cut
98
99 sub SetParameterValues {
100         my $self = shift;
101         my $state = shift;
102
103         confess "SetParameterValues needs parameters" unless @_;
104
105         my $params = shift || return;
106
107         warn "# SetParameterValues = ", dump( $params ), "\n" if $self->debug;
108
109         $self->xml( $state, sub {
110                 my ( $X, $state ) = @_;
111
112                 $X->SetParameterValues( $cwmp,
113                         $X->ParameterList( [],
114                                 map {
115                                         $X->ParameterValueStruct( [],
116                                                 $X->Name( [], $_ ),
117                                                 $X->Value( [], $params->{$_} )
118                                         )
119                                 } sort keys %$params
120                         )
121                 );
122         });
123 }
124
125
126 =head2 GetParameterValues
127
128   $method->GetParameterValues( $state, [ 'ParameterName', ... ] );
129
130 =cut
131
132 sub _array_param {
133         my $v = shift;
134         confess "array_mandatory(",dump($v),") isn't ARRAY" unless ref($v) eq 'ARRAY';
135         return @$v;
136 }
137
138 sub GetParameterValues {
139         my $self = shift;
140         my $state = shift;
141         my @ParameterNames = _array_param(shift);
142         warn "# GetParameterValues", dump( @ParameterNames ), "\n" if $self->debug;
143
144         $self->xml( $state, sub {
145                 my ( $X, $state ) = @_;
146
147                 $X->GetParameterValues( $cwmp,
148                         $X->ParameterNames( $cwmp,
149                                 map {
150                                         $X->string( $xsd, $_ )
151                                 } @ParameterNames
152                         )
153                 );
154         });
155 }
156
157 =head2 GetParameterNames
158
159   $method->GetParameterNames( $state, [ $ParameterPath, $NextLevel ] );
160
161 =cut
162
163 sub GetParameterNames {
164         my ( $self, $state, $param ) = @_;
165         # default: all, all
166         my ( $ParameterPath, $NextLevel ) = _array_param( $param );
167         $ParameterPath ||= '';
168         $NextLevel ||= 0;
169         warn "# GetParameterNames( '$ParameterPath', $NextLevel )\n" if $self->debug;
170         $self->xml( $state, sub {
171                 my ( $X, $state ) = @_;
172
173                 $X->GetParameterNames( $cwmp,
174                         $X->ParameterPath( $cwmp, $ParameterPath ),
175                         $X->NextLevel( $cwmp, $NextLevel ),
176                 );
177         });
178 }
179
180 =head2 GetParameterAttributes
181
182         $method->GetParameterAttributes( $state, [ $ParametarNames, ... ] );
183
184 =cut
185
186 sub GetParameterAttributes {
187         my ( $self, $state, $param ) = @_;
188         my @ParameterNames = _array_param($param);
189
190         warn "# GetParameterAttributes", dump( @ParameterNames ), "\n" if $self->debug;
191
192         $self->xml( $state, sub {
193                 my ( $X, $state ) = @_;
194
195                 $X->GetParameterAttributes( $cwmp,
196                         $X->ParameterNames( $cwmp,
197                                 map {
198                                         $X->string( $xsd, $_ )
199                                 } @ParameterNames
200                         )
201                 );
202         });
203 }
204
205 =head2 Reboot
206
207   $method->Reboot( $state );
208
209 =cut
210
211 sub Reboot {
212         my ( $self, $state ) = @_;
213         $self->xml( $state, sub {
214                 my ( $X, $state ) = @_;
215                 $X->Reboot();
216         });
217 }
218
219 =head2 FactoryReset
220
221   $method->FactoryReset( $state );
222
223 =cut
224
225 sub FactoryReset {
226         my ( $self, $state ) = @_;
227         $self->xml( $state, sub {
228                 my ( $X, $state ) = @_;
229                 $X->FactoryReset();
230         });
231 }
232
233
234 =head1 Server methods
235
236
237 =head2 InformResponse
238
239   $method->InformResponse( $state );
240
241 =cut
242
243 sub InformResponse {
244         my ( $self, $state ) = @_;
245         $self->xml( $state, sub {
246                 my ( $X, $state ) = @_;
247                 $X->InformResponse( $cwmp,
248                         $X->MaxEnvelopes( $cwmp, 1 )
249                 );
250         });
251 }
252
253 =head1 BUGS
254
255 All other methods are unimplemented.
256
257 =cut
258
259 1;