| 1 | package SNMP::Insight::Moose::MIB; | ||||||||||
| 2 | |||||||||||
| 3 | #ABSTRACT: Moose glue to write MIB roles | ||||||||||
| 4 | |||||||||||
| 5 | 5 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 5 | ▶ | use Moose::Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 5 | our $VERSION = '0.002'; #TRIAL VERSION: | |||||||||
| 10 | |||||||||||
| 11 | 5 | ▶ | use SNMP::Insight::Meta::Attribute::Trait::MIBEntry; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 5 | 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 | 16 | ▶ | sub mib_oid { | ||||||||
|
Subroutine
mib_oid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | 16 | my $meta = shift; | |||||||||
| 22 | 16 | $meta->mib_oid(shift); | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 14 | ▶ | sub mib_name { | ||||||||
|
Subroutine
mib_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 26 | 14 | my $meta = shift; | |||||||||
| 27 | 14 | $meta->mib_name(shift); | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 51 | ▶ | sub has_scalar { | ||||||||
|
Subroutine
has_scalar: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 31 | 51 | my ( $meta, $name, %options ) = @_; | |||||||||
| 32 | |||||||||||
| 33 | 51 | my $oid = $options{oid}; | |||||||||
| 34 | 51 | ▶ | $oid =~ /^\./ or $oid = $meta->mib_oid . "." . $oid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 51 | $oid =~ s/^\.//o; | |||||||||
| 36 | |||||||||||
| 37 | 51 | my $munger_code; | |||||||||
| 38 | 51 | ▶ | if ( $options{munger} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 1 | $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 | 12 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 12 | my $self = shift; | |||||||||
| 50 | 12 | $self->_mib_read_scalar( $oid, $munger_code ); | |||||||||
| 51 | }, | ||||||||||
| 52 | 51 | ); | |||||||||
| 53 | 51 | ▶ | $options{munger} and $attribute_options{munger} = $munger_code; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | |||||||||||
| 55 | 51 | $meta->add_attribute( $name, %attribute_options ); | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 27 | ▶ | sub has_table { | ||||||||
|
Subroutine
has_table: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||
| 59 | 27 | my ( $meta, $name, %options ) = @_; | |||||||||
| 60 | |||||||||||
| 61 | 27 | my $table_oid = $options{oid}; | |||||||||
| 62 | 27 | ▶ | $table_oid or croak "Table $name has no oid"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 27 | ▶ | $table_oid =~ /^\./ or $table_oid = $meta->mib_oid . "." . $table_oid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 27 | $table_oid =~ s/^\.//o; | |||||||||
| 65 | |||||||||||
| 66 | 27 | my $columns = $options{columns}; | |||||||||
| 67 | 27 | ▶ | $columns or croak "Table $name has no columns definition"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 27 | ▶ | while ( my ( $col_name, $col_opts ) = each(%$columns) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 196 | _create_column( $meta, $table_oid, $col_name, $col_opts ); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 27 | my $index = $options{index}; | |||||||||
| 73 | 27 | ▶ | if ($index) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | |||||||||||
| 75 | # TODO handle multi column index | ||||||||||
| 76 | |||||||||||
| 77 | 27 | ▶ | $meta->has_attribute($index) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 1 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 92 | 1 | my $self = shift; | |||||||||
| 93 | 1 | $self->_mib_read_table( | |||||||||
| 94 | index => $index, | ||||||||||
| 95 | columns => [ keys %$columns ] | ||||||||||
| 96 | ); | ||||||||||
| 97 | }, | ||||||||||
| 98 | 27 | ); | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 15 | ▶ | sub _load_munger { | ||||||||
|
Subroutine
_load_munger: called CC=3 SCAR=11.4
| |||||||||||
| 102 | 15 | my ( $meta, $munger ) = @_; | |||||||||
| 103 | |||||||||||
| 104 | # easy case | ||||||||||
| 105 | 15 | ▶ | return $munger if ref($munger) eq "CODE"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | |||||||||||
| 107 | 15 | ▶ | my $metamethod = $meta->find_method_by_name($munger) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | or croak "No $munger found"; | ||||||||||
| 109 | 15 | return $metamethod->body; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 196 | ▶ | sub _create_column { | ||||||||
|
Subroutine
_create_column: called CC=4 SCAR=13.9
| |||||||||||
| 113 | 196 | my ( $meta, $table_oid, $col_name, $col_opts ) = @_; | |||||||||
| 114 | |||||||||||
| 115 | 196 | ▶ | ref $col_opts eq 'ARRAY' or $col_opts = [$col_opts]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | |||||||||||
| 117 | 196 | my ( $sub_id, $munger ) = @$col_opts; | |||||||||
| 118 | |||||||||||
| 119 | 196 | my $col_oid = "$table_oid.1.$sub_id"; | |||||||||
| 120 | 196 | my $munger_code; | |||||||||
| 121 | 196 | ▶ | $munger and $munger_code = _load_munger( $meta, $munger ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 22 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 132 | 22 | my $self = shift; | |||||||||
| 133 | 22 | $self->_mib_read_tablerow( $col_oid, $munger_code ); | |||||||||
| 134 | }, | ||||||||||
| 135 | 196 | ); | |||||||||
| 136 | 196 | ▶ | $munger and $attribute_options{munger} = $munger_code; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | |||||||||||
| 138 | 196 | $meta->add_attribute( $col_name, %attribute_options ); | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 5 | 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__ | ||||||||||