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 |
=head2 all_uids |
113 |
|
114 |
my @cpe = $store->all_uids; |
115 |
|
116 |
=cut |
117 |
|
118 |
sub all_uids { |
119 |
my $self = shift; |
120 |
my @cpes = $self->current_store->all_uids; |
121 |
warn "## all_uids = ", dump( @cpes ), "\n" if $self->debug; |
122 |
return @cpes; |
123 |
} |
124 |
|
125 |
=head2 state_to_uid |
126 |
|
127 |
my $CPE_uid = $store->ID_to_uid( $state ); |
128 |
|
129 |
It uses C<< DeviceID.SerialNumber >> from C<Inform> message as unique ID |
130 |
for each CPE. |
131 |
|
132 |
=cut |
133 |
|
134 |
sub state_to_uid { |
135 |
my $self = shift; |
136 |
my ( $state ) = @_; |
137 |
|
138 |
warn "#### state_to_uid",dump( $state ),$/ if $self->debug > 4; |
139 |
|
140 |
my $uid = $state->{DeviceID}->{SerialNumber} || |
141 |
confess "no DeviceID.SerialNumber in ",dump( $state ); |
142 |
|
143 |
return $uid; |
144 |
} |
145 |
|
146 |
1; |