File Coverage

blib/lib/SNMP/Insight/Moose/MIB.pm
Criterion Covered Total %
statement 57 58 98.2
branch 18 26 69.2
condition n/a
subroutine 13 13 100.0
pod 4 4 100.0
total 92 101 91.0


line stmt bran cond sub pod time code
1             package SNMP::Insight::Moose::MIB;
2              
3             #ABSTRACT: Moose glue to write MIB roles
4              
5 6     6   10363 use Moose;
  6         8  
  6         35  
6 6     6   30111 use Moose::Exporter;
  6         13  
  6         29  
7 6     6   276 use Carp;
  6         9  
  6         515  
8              
9             our $VERSION = '0.001'; #TRIAL VERSION:
10              
11 6     6   3063 use SNMP::Insight::Meta::Attribute::Trait::MIBEntry;
  6         17  
  6         4097  
12              
13             Moose::Exporter->setup_import_methods(
14             with_meta => [ 'mib_oid', 'mib_name', 'has_scalar', 'has_table' ],
15             role_metaroles => {
16             role => ['SNMP::Insight::Meta::Class::Trait::MIB'],
17             },
18             );
19              
20             sub mib_oid {
21 12     12 1 5456 my $meta = shift;
22 12         439 $meta->mib_oid(shift);
23             }
24              
25             sub mib_name {
26 10     10 1 97 my $meta = shift;
27 10         338 $meta->mib_name(shift);
28             }
29              
30             sub has_scalar {
31 50     50 1 501 my ( $meta, $name, %options ) = @_;
32              
33 50         68 my $oid = $options{oid};
34 50 100       1751 $oid =~ /^\./ or $oid = $meta->mib_oid . "." . $oid;
35 50         80 $oid =~ s/^\.//o;
36              
37 50         48 my $munger_code;
38 50 50       92 if ( $options{munger} ) {
39 0         0 $munger_code = _load_munger( $meta, $options{munger} );
40             }
41              
42             my %attribute_options = (
43             traits => ['MIBEntry'],
44             is => 'ro',
45             lazy => 1,
46             oid => $oid,
47             entry_type => 'scalar',
48             default => sub {
49 12     12   25648 my $self = shift;
50 12         40 $self->_mib_read_scalar( $oid, $munger_code );
51             },
52 50         340 );
53 50 50       96 $options{munger} and $attribute_options{munger} = $munger_code;
54              
55 50         224 $meta->add_attribute( $name, %attribute_options );
56             }
57              
58             sub has_table {
59 14     14 1 191 my ( $meta, $name, %options ) = @_;
60              
61 14         21 my $table_oid = $options{oid};
62 14 50       28 $table_oid or croak "Table $name has no oid";
63 14 100       454 $table_oid =~ /^\./ or $table_oid = $meta->mib_oid . "." . $table_oid;
64 14         29 $table_oid =~ s/^\.//o;
65              
66 14         16 my $columns = $options{columns};
67 14 50       30 $columns or croak "Table $name has no columns definition";
68 14         44 while ( my ( $col_name, $col_opts ) = each(%$columns) ) {
69 141         5465 _create_column( $meta, $table_oid, $col_name, $col_opts );
70             }
71              
72 14         569 my $index = $options{index};
73 14 50       27 $index or croak "Table $name has no index defined";
74 14 50       26 $meta->has_attribute($index)
75             or croak "Cannot find index $index for table $name";
76              
77             # TODO check index
78              
79             $meta->add_attribute(
80             $name,
81              
82             traits => ['MIBEntry'],
83             entry_type => 'table',
84             oid => $table_oid,
85              
86             is => 'ro',
87             lazy => 1,
88              
89             default => sub {
90 1     1   10 my $self = shift;
91 1         12 $self->_mib_read_table( $index, [ keys %$columns ] );
92             },
93 14         124 );
94             }
95              
96             sub _load_munger {
97 4     4   5 my ( $meta, $munger ) = @_;
98              
99             # easy case
100 4 50       10 return $munger if ref($munger) eq "CODE";
101              
102 4 50       14 my $metamethod = $meta->find_method_by_name($munger)
103             or croak "No $munger found";
104 4         109 return $metamethod->body;
105             }
106              
107             sub _create_column {
108 141     141   145 my ( $meta, $table_oid, $col_name, $col_opts ) = @_;
109              
110 141 100       300 ref $col_opts eq 'ARRAY' or $col_opts = [$col_opts];
111              
112 141         134 my ( $sub_id, $munger ) = @$col_opts;
113              
114 141         197 my $col_oid = "$table_oid.1.$sub_id";
115 141         195 my $munger_code;
116 141 100       184 $munger and $munger_code = _load_munger( $meta, $munger );
117              
118             my %attribute_options = (
119             is => 'ro',
120             lazy => 1,
121              
122             traits => ['MIBEntry'],
123             oid => $col_oid,
124             entry_type => 'column',
125              
126             default => sub {
127 22     22   226 my $self = shift;
128 22         80 $self->_mib_read_tablerow( $col_oid, $munger_code );
129             },
130 141         647 );
131 141 100       231 $munger and $attribute_options{munger} = $munger_code;
132              
133 141         381 $meta->add_attribute( $col_name, %attribute_options );
134             }
135              
136             1;
137              
138             # Local Variables:
139             # mode: cperl
140             # indent-tabs-mode: nil
141             # cperl-indent-level: 4
142             # cperl-indent-parens-as-block: t
143             # End:
144              
145             __END__
146              
147             =pod
148              
149             =head1 NAME
150              
151             SNMP::Insight::Moose::MIB - Moose glue to write MIB roles
152              
153             =head1 VERSION
154              
155             version 0.001
156              
157             =head1 METHODS
158              
159             =head2 mib_oid
160              
161             Declare the oid of the current MIB. E.g. in IFMIB.pm
162              
163             mib_oid '1.3.6.1.2.1.31';
164              
165             =head2 mib_name
166              
167             Declare the name of the current MIB. E.g.
168              
169             mib_name 'IF-MIB';
170              
171             =head2 has_scalar $name => %options
172              
173             has_scalar 'fooVal' => ( oid => '3' );
174              
175             Declare a scalar entry of a given C<$name> into the current MIB role.
176             Oid is relative to the MIB oid declare with mib_oid, unless it starts with a dot.
177              
178             =head2 has_table $name %options
179              
180             Declare a table of a given C<$name> into the current MIB role.
181              
182             has_table "fooTable" => (
183             oid => "1.1",
184             index => "fooTableIndex",
185             columns => {
186             'fooTableIndex' => 1,
187             'fooTableBars' => 2,
188             'fooTableBaazes' => [ 3, 'munge_baazes' ],
189             }
190             );
191              
192             =over 4
193              
194              
195              
196             =back
197              
198             * oid => $oid
199              
200             * index => $index
201              
202             * columns => %columns
203              
204             =head1 AUTHOR
205              
206             Gabriele Mambrini <g.mambrini@gmail.com>
207              
208             =head1 COPYRIGHT AND LICENSE
209              
210             This software is copyright (c) 2015 by Gabriele Mambrini.
211              
212             This is free software; you can redistribute it and/or modify it under
213             the same terms as the Perl 5 programming language system itself.
214              
215             =cut