Info/AMAP.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 206 CC 14 SCAR 23.5
CC
14
Cov
0%
_amap_index
20.0
amap_port
20.0
amap_id
12.0
CRAP 10.4
1 # SNMP::Info::AMAP
2 #
3 # Copyright (c) 2013 Eric Miller
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # * Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of the University of California, Santa Cruz nor the
15 # names of its contributors may be used to endorse or promote products
16 # derived from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29  
30 package SNMP::Info::AMAP;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use SNMP::Info;
36  
37 @SNMP::Info::AMAP::ISA = qw/SNMP::Info Exporter/;
38 @SNMP::Info::AMAP::EXPORT_OK = qw//;
39  
40 our ($VERSION, %FUNCS, %GLOBALS, %MIBS, %MUNGE);
41  
42 $VERSION = '3.985003';
43  
44 %MIBS = (
45 'ALCATEL-IND1-INTERSWITCH-PROTOCOL-MIB' => 'aipAMAPRemDeviceType',
46 );
47  
48 %GLOBALS = (
49  
50 );
51  
52 %FUNCS = (
53 # ALCATEL-IND1-INTERSWITCH-PROTOCOL-MIB::aipAMAPRemHostname
54 'amap_rem_sysname' => 'aipAMAPRemHostname',
55 );
56  
57 %MUNGE = ( 'amap_rem_sysname' => \&SNMP::Info::munge_null, );
58  
59 sub hasAMAP {
60 my $amap = shift;
61  
62 my $amap_ip = $amap->aipAMAPIpAddr() || {};
63  
64 return 1 if ( scalar( keys %$amap_ip ) );
65  
66 return;
67 }
68  
69 # Break up the aipAMAPhostsTable INDEX into MAC and IP address.
70 sub _hosts_table_index {
71 my $idx = shift;
72 my @oids = split( /\./, $idx );
73 my $mac = join( '.', splice( @oids, 0, 6 ) );
74  
75 return ( $mac, join( '.', @oids ) );
76 }
77  
78 # Break up the aipAMAPportConnectionTable INDEX and return MAC
79 sub _conn_table_mac {
80 my $idx = shift;
81 my @oids = split( /\./, $idx );
82 my $local_idx = shift @oids;
83 my $mac = join( '.', splice( @oids, 0, 6 ) );
84  
85 return ($mac);
86 }
87  
88 # Since we need to get IP Addresses from the aipAMAPhostsTable which has
89 # a different index (MAC, IP) than the aipAMAPportConnectionTable which holds
90 # the remote device details we create a combined index and skip any
91 # IPs which have an address of 0.0.0.0. Would like to include only one
92 # address since they should all originate from the same device, but we don't
93 # know if they would all be reachable from the network management application.
94 #
95 # We don't implement partials since this is private index function
96 sub _amap_index {
97 my $amap = shift;
98  
99 my $amap_ip = $amap->aipAMAPIpAddr() || {};
100 my $amap_rport = $amap->aipAMAPLocalPort() || {};
101  
102 my %amap_index;
103 foreach my $key ( keys %$amap_ip ) {
104 my ( $mac, $ip ) = _hosts_table_index($key);
105  
106 next if ( $ip eq '0.0.0.0' );
107 next unless $ip;
108  
109 foreach my $idx ( keys %$amap_rport ) {
110 my $c_mac = _conn_table_mac($idx);
111  
112 if ( $mac eq $c_mac ) {
113 my $index = "$idx.$ip";
114 $amap_index{$index} = $index;
115 }
116 }
117 }
118 return \%amap_index;
119 }
120  
121 # Break up _amap_index INDEX into local index, MAC, remote index, and
122 # IP address
123 sub _amap_index_parts {
124 my $idx = shift;
125 my @oids = split( /\./, $idx );
126 my $local_idx = shift @oids;
127 my $mac = join( '.', splice( @oids, 0, 6 ) );
128 my $rem_idx = shift @oids;
129  
130 return ( $local_idx, $mac, $rem_idx, join( '.', @oids ) );
131 }
132  
133 sub amap_if {
134 my $amap = shift;
135  
136 my $index = $amap->_amap_index() || {};
137 my $if_idx = $amap->aipAMAPLocalIfindex() || {};
138  
139 my %amap_if;
140 foreach my $key ( keys %$index ) {
141 my ( $local_idx, $mac, $rem_idx, $ip ) = _amap_index_parts($key);
142 my $if_key = "$local_idx.$mac.$rem_idx";
143  
144 if ( $key =~ /^$if_key/ ) {
145 my $if = $if_idx->{$if_key};
146 $amap_if{$key} = $if;
147 }
148 }
149  
150 return \%amap_if;
151 }
152  
153 sub amap_ip {
154 my $amap = shift;
155  
156 my $index = $amap->_amap_index() || {};
157  
158 my %amap_ip;
159 foreach my $key ( keys %$index ) {
160 my ( $local_idx, $mac, $rem_idx, $ip ) = _amap_index_parts($key);
161  
162 # MIB says should only be IPv4
163 next unless ( $ip =~ /\d+(\.\d+){3}/ );
164 $amap_ip{$key} = $ip;
165 }
166 return \%amap_ip;
167 }
168  
169 sub amap_port {
170 my $amap = shift;
171  
172 my $index = $amap->_amap_index() || {};
173 my $amap_rport = $amap->aipAMAPLocalPort() || {};
174 my $amap_rslot = $amap->aipAMAPLocalSlot() || {};
175  
176 my %amap_port;
177 foreach my $key ( sort keys %$index ) {
178 my ( $local_idx, $mac, $rem_idx, $ip ) = _amap_index_parts($key);
179 my $p_key = "$local_idx.$mac.$rem_idx";
180  
181 if ( $key =~ /^$p_key/ ) {
182 my $port = $amap_rport->{$p_key};
183 my $slot = $amap_rslot->{$p_key} || 0;
184 next unless $port;
185 $amap_port{$key} = defined $slot ? "$slot\/$port" : $port;
186 }
187 }
188 return \%amap_port;
189 }
190  
191 sub amap_id {
192 my $amap = shift;
193  
194 my $index = $amap->_amap_index() || {};
195 my $amap_name = $amap->amap_rem_sysname() || {};
196  
197 my %amap_name;
198 foreach my $key ( sort keys %$index ) {
199 my ( $local_idx, $mac, $rem_idx, $ip ) = _amap_index_parts($key);
200 my $id_key = "$local_idx.$mac.$rem_idx";
201  
202 if ( $key =~ /^$id_key/ ) {
203 my $name = $amap_name->{$id_key} || 0;
204 next unless $name;
205 $amap_name{$key} = $name;
206 }
207 }
208 return \%amap_name;
209 }
210  
211 sub amap_platform {
212 my $amap = shift;
213  
214 my $index = $amap->_amap_index() || {};
215 my $amap_topo_platform = $amap->aipAMAPRemDeviceType() || {};
216  
217 my %amap_platform;
218 foreach my $key ( keys %$index ) {
219 my ( $local_idx, $mac, $rem_idx, $ip ) = _amap_index_parts($key);
220 my $pf_key = "$local_idx.$mac.$rem_idx";
221  
222 if ( $key =~ /^$pf_key/ ) {
223 my $platform = $amap_topo_platform->{$pf_key};
224 next unless $platform;
225 $amap_platform{$key} = $platform;
226 }
227 }
228 return \%amap_platform;
229 }
230  
231 1;
232 __END__