IES/Slot.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« IES.pm ↑ Summary IES/Port.pm »
1 package Device::ZyXEL::IES::Slot;
2 19 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use Net::SNMP::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 0 ▶use Device::ZyXEL::IES::Port;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use Device::ZyXEL::IES::OID;
6  
7 =head1 NAME
8  
9 Device::ZyXEL::IES::Slot - A model of a Slot on an IES.
10  
11 =head1 VERSION
12  
13 Version 0.10
14  
15 =cut
16  
17 our $VERSION = '0.10';
18  
19 =head1 SYNOPSIS
20  
21 Meant to be instanciated from Device::ZyXEL::IES only.
22  
23 =cut
24  
25 # slot ID
26 # 1, 3, 4, 5, 6, 7, 8, 9, 10
27 has 'id' => (
28 isa => 'Int',
29 is => 'ro',
30 required => 1
31 );
32  
33 # slot firmware
34 has 'firmware' => (
35 isa => 'Str',
36 is => 'ro',
37 writer => '_set_firmware'
38 );
39  
40 # slot type, ADSL, VDSL, SHDSL, MSC
41 has 'cardtype' => (
42 isa => 'Str',
43 is => 'ro',
44 writer => '_set_cardtype',
45 );
46  
47  
48 has 'iftype' => (
49 isa => 'Str',
50 is => 'rw',
51 default => 'ADSL'
52 );
53  
54 # A list of ports, 1-24 or 1-48, howmany ever
55 # ports the slot contains.
56 has 'ports' => (
57 traits => ['Hash'],
58 isa => 'HashRef[Device::ZyXEL::IES::Port]',
59 is => 'rw',
60 default => sub { {} },
61 handles => {
62 port_exists => 'exists',
63 get_port => 'get',
64 add_port => 'set',
65 delete_port => 'delete',
66 num_ports => 'count',
67 port_pairs => 'kv'
68 }
69 );
70  
71 our $oid_tr;
72 =head2 BUILD
73
74 Create a handle for a Device::ZyXEL::IES::OID object in order
75 to translate OID names to actual oids.
76
77 =cut
78 sub BUILD {
79 my ( $self, $params ) = @_;
80 my $ports = $self->ports;
81 foreach my $port ( keys %{$ports} ) {
82 $ports->{$port}->slot( $self );
83 }
84 $self->ies->add_slot( $params->{id}, $self );
85 $oid_tr = Device::ZyXEL::IES::OID->new;
86 }
87  
88 after 'ports' => sub {
89 my ( $self, $ports ) = @_;
90 return unless $ports;
91 my $p = $self->ports;
92 foreach my $port ( keys %{$p} ) {
93 $p->{$port}->slot( $self );
94 }
95 };
96  
97 has 'ies' => (
98 isa => 'Device::ZyXEL::IES',
99 is => 'rw',
100 weak_ref => 1,
101 required => 1
102 );
103  
104 =head1 FUNCTIONS
105  
106  
107 =head2 read_oid
108  
109 Uses Net::SNMP::Util to read the value of an oid
110 for a specific slot. Used internally by _set_*
111  
112 =cut
113 sub read_oid {
114 my ($self, $oidname ) = @_;
115  
116 my $oid = $oid_tr->translate( $oidname );
117
118 return "ERROR: translate of $oidname failed" unless defined( $oid );
119
120 $oid = $oid . '.' . $self->id;
121  
122 return $self->ies->read_oid( $oid );
123 }
124  
125  
126 =head2 has_port
127
128 Checks whether or not a specific port id is present in the port list.
129
130 =cut
131 sub has_port {
132 my ($self,$portid) = @_;
133
134 my $ports = $self->ports;
135 return defined( $ports->{$portid} );
136 }
137  
138  
139 =head2 read_firmware
140  
141 reads the firmware version from the IES
142 using Net::SNMP::Util
143  
144 =cut
145  
146 sub read_firmware {
147 my ($self) = @_;
148 my $firmware = $self->read_oid('ZYXEL-IES5000-MIB::slotModuleFWVersion.0');
149 if ( $firmware !~ /ERROR/ ) {
150 $self->_set_firmware( $firmware );
151 }
152 return $firmware;
153 }
154  
155 =head2 alignports
156  
157 Sets up any missing port elements in the list of ports, so
158 that the list always matches the actual number of and id's of the ports
159 on the slot.
160  
161 =cut
162 sub alignports {
163 my ($self, $nofports) = @_;
164
165 my $ports = $self->ports;
166 for ( my $p = 1; $p <= $nofports; $p++ ) {
167 my $portid = sprintf( "%d%02d", $self->id, $p);
168 if ( !defined( $ports->{$portid} ) ) {
169 $self->add_port( $portid, Device::ZyXEL::IES::Port->new( id => $portid, slot => $self) );
170 }
171 }
172  
173 if ( $self->num_ports > $nofports ) {
174 # more ports that we want? - remove them
175 my $num_ports = $self->num_ports;
176 for ( my $p = ($nofports + 1); $p <= $num_ports; $p++ ) {
177 my $portid = sprintf( "%d%02d", $self->id, $p);
178 $self->delete_port( $portid );
179 }
180 }
181 }
182  
183 =head2 read_cardtype
184  
185 reads the cardtype from the IES
186 using Net::SNMP::Util
187  
188 =cut
189  
190 sub read_cardtype {
191 my ($self) = @_;
192 my $cardtype = $self->read_oid('ZYXEL-IES5000-MIB::slotModuleDescr.0');
193 if ( $cardtype !~ /ERROR/ ) {
194 $self->_set_cardtype( $cardtype );
195 }
196  
197 if ( $cardtype =~ /^MSC/ ) {
198 $self->iftype('MSC');
199 $self->alignports(0);
200 }
201 elsif ( $cardtype =~ /^VLC\d\d(\d\d).*$/ ) {
202 $self->iftype('VDSL');
203 $self->alignports($1);
204 }
205 elsif ( $cardtype =~ /^SLC\d\d(\d\d).*$/ ) {
206 $self->iftype('SHDSL');
207 $self->alignports($1);
208 }
209 elsif ( $cardtype =~ /^ALC\d\d(\d\d).*$/ ) {
210 $self->iftype('ADSL');
211 $self->alignports($1);
212 }
213 else {
214 $self->iftype('Unknown');
215 $self->alignports(0);
216 }
217 return $cardtype;
218 }
219  
220 =head2 fetchDetails
221  
222 Fetches all the details of a slot by calling
223 all the read_* methods of this module.
224  
225 =cut
226 sub fetchDetails {
227 my $self = shift;
228 my $meta = $self->meta();
229  
230 foreach my $method ( $meta->get_method_list ) {
231 if ( $method =~ /^read_/ && $method ne 'read_oid' ) {
232 my $res = $self->$method;
233 return $res if $res =~ /ERROR/i;
234 }
235 }
236 return 'OK';
237 }
238  
239 =head2 portInventory
240  
241 Fetches details for all ports on the slot.
242  
243 =cut
244 sub portInventory {
245 my $self = shift;
246  
247 # Will make sure all ports are created.
248 my $cres = $self->read_cardtype();
249 if ( $cres !~ /ERROR/ ) {
250 my $pres;
251 foreach my $port ( $self->port_pairs ) {
252 $pres = $port->[1]->fetchDetails();
253 return $pres if $pres =~ /ERROR/;
254 }
255 }
256 else {
257 return $cres;
258 }
259 return 'OK';
260 }
261  
262  
263 =head1 SUPPORT
264  
265 You can find documentation for this module with the perldoc command.
266  
267 perldoc Device::ZyXEL::IES::Slot
268  
269 You can also look for information at:
270  
271 =over 4
272  
273 =item * RT: CPAN's request tracker
274  
275 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Device-ZyXEL-IES>
276  
277 =item * AnnoCPAN: Annotated CPAN documentation
278  
279 L<http://annocpan.org/dist/Device-ZyXEL-IES>
280  
281 =item * CPAN Ratings
282  
283 L<http://cpanratings.perl.org/d/Device-ZyXEL-IES>
284  
285 =item * Search CPAN
286  
287 L<http://search.cpan.org/dist/Device-ZyXEL-IES/>
288  
289 =back
290  
291  
292 =head1 ACKNOWLEDGEMENTS
293  
294 Fullrate (http://www.fullrate.dk)
295  
296 Thanks for allowing me to be introduced to the "wonderful" device ;)
297 And thanks for donating some of my work time to create this module and
298 sharing it with the world.
299
300 =head1 COPYRIGHT & LICENSE
301
302 Copyright 2012 Jesper Dalberg, all rights reserved.
303
304 This program is free software; you can redistribute it and/or modify it
305 under the same terms as Perl itself.
306  
307 =cut
308  
309 __PACKAGE__->meta->make_immutable;
310  
« IES.pm ↑ Summary IES/Port.pm »