re-enable all_parameteres collection of first connect
[perl-cwmp.git] / lib / CWMP / Store.pm
1 # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/22/07 14:35:38 CEST
2 package CWMP::Store;
3
4 use strict;
5 use warnings;
6
7
8 use base qw/Class::Accessor/;
9 __PACKAGE__->mk_accessors( qw/
10 module
11 path
12 debug
13 / );
14
15 use Carp qw/confess/;
16 use Data::Dump qw/dump/;
17 use Module::Pluggable search_path => 'CWMP::Store', sub_name => 'possible_stores', require => 1;
18
19 =head1 NAME
20
21 CWMP::Store - parsist CPE state on disk
22
23 =head1 METHODS
24
25 =head2 new
26
27   my $store = CWMP::Store->new({
28         module => 'DBMDeep',
29         path => '/path/to/state.db',
30         clean => 1,
31         debug => 1,
32   });
33
34 =cut
35
36 sub new {
37         my $class = shift;
38         my $self = $class->SUPER::new( @_ );
39
40         confess "requed parametar module is missing" unless $self->module;
41
42         # XXX it's important to call possible_stores once, because current_store won't work
43         my @plugins = $self->possible_stores();
44
45         warn "Found store plugins: ", join(", ", @plugins ), "\n" if $self->debug;
46
47         $self->current_store->open( @_ );
48
49         # so that we don't have to check if it's defined
50         $self->debug( 0 ) unless $self->debug;
51
52         return $self;
53 }
54
55 =head2 current_store
56
57 Returns currnet store plugin object
58
59 =cut
60
61 sub current_store {
62         my $self = shift;
63
64         my $module = $self->module;
65         my $s = $self->only( ref($self).'::'.$module );
66
67         confess "unknown store module $module not one of ", dump( $self->possible_stores ) unless $s;
68
69 #       warn "#### current store = $s\n" if $self->debug > 4;
70
71         return $s;
72 }
73
74 =head2 update_state
75
76   $store->update_state( $state );
77
78 =cut
79
80 sub update_state {
81         my $self = shift;
82
83         my ( $state ) = @_;
84
85         confess "need state" unless $state;
86
87         my $uid = $self->state_to_uid( $state );
88
89         warn "#### update_state( ", dump( $state ), " ) for $uid\n" if $self->debug > 2;
90         $self->current_store->update_uid_state( $uid, $state );
91 }
92
93 =head2 get_state
94
95   my $state = $store->get_state( $uid );
96
97 Returns normal unblessed hash (actually, in-memory copy of state in database).
98
99 =cut
100
101 sub get_state {
102         my $self = shift;
103         my ( $uid ) = @_;
104         confess "need uid" unless $uid;
105
106         warn "#### get_state( $uid )\n" if $self->debug > 4;
107
108         return $self->current_store->get_state( $uid );
109
110 }
111
112
113 =head2 set_state
114
115   $store->set_state( $uid, $state );
116
117 =cut
118
119 sub set_state {
120         my $self = shift;
121         return $self->current_store->set_state( @_ );
122 }
123
124
125 =head2 all_uids
126
127   my @cpe = $store->all_uids;
128
129 =cut
130
131 sub all_uids {
132         my $self = shift;
133         my @cpes = $self->current_store->all_uids;
134         warn "## all_uids = ", dump( @cpes ), "\n" if $self->debug;
135         return @cpes;
136 }
137
138 =head2 state_to_uid
139
140   my $CPE_uid = $store->ID_to_uid( $state );
141
142 It uses C<< DeviceId.SerialNumber >> from C<Inform> message as unique ID
143 for each CPE.
144
145 =cut
146
147 sub state_to_uid {
148         my $self = shift;
149         my ( $state ) = @_;
150
151         warn "#### state_to_uid",dump( $state ),$/ if $self->debug > 4;
152
153         my $uid = $state->{DeviceId}->{SerialNumber} ||
154                 confess "no DeviceId.SerialNumber in ",dump( $state );
155         chomp($uid);
156
157         return $uid;
158 }
159
160 1;