File Coverage

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