Info/Layer3/Foundry.pm untested

filter: Statementstmt 0.0% 0 / 577 Branchbran 0.0% 0 / 220 Conditioncond 0.0% 0 / 263 MC/DCmcdc 0.0% 0 / 141 Subroutinesub 0.0% 0 / 64 Podpod 0.0% 0 / 59 totaltotal 0.0% 0 / 1324 CC 111 SCAR 31.7
CC
111
Cov
0%
set_i_vlan
90.0
os_ver
56.0
v_name
42.0
CRAP 23.7
1 # SNMP::Info::Layer3::Foundry - SNMP Interface to Foundry devices
2 #
3 # Copyright (c) 2008 Max Baker changes from version 0.8 and beyond.
4 #
5 # Copyright (c) 2002,2003 Regents of the University of California
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # * Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of the University of California, Santa Cruz nor the
17 # names of its contributors may be used to endorse or promote products
18 # derived from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31  
32 package SNMP::Info::Layer3::Foundry;
33  
34 use strict;
35 use warnings;
36 use Exporter;
37 use SNMP::Info::Layer3;
38 use SNMP::Info::FDP;
39  
40 @SNMP::Info::Layer3::Foundry::ISA = qw/
41 SNMP::Info::FDP
42 SNMP::Info::Layer3
43 Exporter
44 /;
45 @SNMP::Info::Layer3::Foundry::EXPORT_OK = qw//;
46  
47 our ($VERSION, %GLOBALS, %FUNCS, %MIBS, %MUNGE);
48  
49 $VERSION = '3.985003';
50  
51 %MIBS = (
52 %SNMP::Info::Layer3::MIBS,
53 %SNMP::Info::FDP::MIBS,
54  
55 'FOUNDRY-SN-ROOT-MIB' => 'foundry',
56 'FOUNDRY-SN-AGENT-MIB' => 'snChasPwrSupplyDescription',
57 'FOUNDRY-SN-SWITCH-GROUP-MIB' => 'snSwGroupOperMode',
58 'FOUNDRY-SN-IP-MIB' => 'snRtIpPortIfAddress',
59 'FOUNDRY-SN-STACKING-MIB' => 'snStackingOperUnitRole',
60 'FDRY-POE-MIB' => 'snAgentPoeGblPowerCapacityTotal',
61 'BROCADE-PRODUCTS-MIB' => 'brocadeProducts',
62 'MAU-MIB' => 'ifMauAutoNegAdminStatus',
63 );
64  
65 %GLOBALS = (
66 %SNMP::Info::Layer3::GLOBALS,
67 %SNMP::Info::FDP::GLOBALS,
68  
69 'mac' => 'ifPhysAddress.1',
70 'chassis' => 'entPhysicalDescr.1',
71 'temp' => 'snChasActualTemperature',
72 'ps1_type' => 'snChasPwrSupplyDescription.1',
73 'ps2_type' => 'snChasPwrSupplyDescription.2',
74 'ps1_status' => 'snChasPwrSupplyOperStatus.1',
75 'ps2_status' => 'snChasPwrSupplyOperStatus.2',
76 'fan' => 'snChasFanOperStatus.1',
77 'img_ver' => 'snAgImgVer',
78 'ch_serial' => 'snChasSerNum',
79  
80 );
81  
82 %FUNCS = (
83 %SNMP::Info::Layer3::FUNCS,
84 %SNMP::Info::FDP::FUNCS,
85  
86 # FOUNDRY-SN-SWITCH-GROUP-MIB
87 # snSwPortInfoTable - Switch Port Information Group
88 'sw_index' => 'snSwPortIfIndex',
89 'sw_duplex' => 'snSwPortInfoChnMode',
90 'sw_type' => 'snSwPortInfoMediaType',
91 'sw_speed' => 'snSwPortInfoSpeed',
92  
93 # FOUNDRY-SN-SWITCH-GROUP-MIB::snSwIfInfoTable, read only for ports
94 # that do not auto-negotiate (see i_speed_admin)
95 '_sw_if_speed' => 'snSwIfInfoSpeed',
96 '_sw_if_duplex' => 'snSwIfInfoChnMode',
97  
98 # MAU-MIB::ifMauAutoNegTable
99 '_mau_autoneg_admin' => 'ifMauAutoNegAdminStatus',
100  
101 # FOUNDRY-SN-AGENT-MIB::snAgentConfigModule2Table
102 'ag_mod2_type' => 'snAgentConfigModule2Type',
103  
104 # FOUNDRY-SN-AGENT-MIB::snAgentConfigModuleTable
105 'ag_mod_type' => 'snAgentConfigModuleType',
106  
107 # FOUNDRY-SN-AGENT-MIB::snVLanByPortTable
108 'stp_i_id' => 'snVLanByPortVLanId',
109 'stp_i_mac' => 'snVLanByPortBaseBridgeAddress',
110 'stp_i_time' => 'snVLanByPortStpTimeSinceTopologyChange',
111 'stp_i_ntop' => 'snVLanByPortStpTopChanges',
112 'stp_i_root' => 'snVLanByPortStpDesignatedRoot',
113 'stp_i_root_port' => 'snVLanByPortStpRootPort',
114 'stp_i_priority' => 'snVLanByPortStpPriority',
115  
116 # FOUNDRY-SN-SWITCH-GROUP-MIB VLAN configuration and membership tables.
117 # Use these tables when the standard bridge methods return no data.
118 '_vlan_cfg_id' => 'snVLanByPortCfgVLanId',
119 '_vlan_cfg_name' => 'snVLanByPortCfgVLanName',
120 '_vlan_member_vlan_id' => 'snVLanByPortMemberVLanId',
121 '_vlan_member_port_id' => 'snVLanByPortMemberPortId',
122 '_vlan_member_status' => 'snVLanByPortMemberRowStatus',
123 '_vlan_member_tag_mode' => 'snVLanByPortMemberTagMode',
124  
125 # FOUNDRY-SN-IP-MIB::snRtIpPortIfAddrTable
126 '_ip_if_index' => 'snRtIpPortIfAddrInterfaceIndex',
127 '_ip_if_address' => 'snRtIpPortIfAddress',
128 '_ip_if_netmask' => 'snRtIpPortIfSubnetMask',
129 '_ip_if_status' => 'snRtIpPortIfRowStatus',
130  
131 # FOUNDRY-SN-AGENT-MIB::snPortStpTable
132 'stp_p_id' => 'snPortStpPortNum',
133 'stp_p_stg_id' => 'snPortStpVLanId',
134 'stp_p_priority' => 'snPortStpPortPriority',
135 'stp_p_state' => 'snPortStpPortState',
136 'stp_p_cost' => 'snPortStpPortDesignatedCost',
137 'stp_p_root' => 'snPortStpPortDesignatedRoot',
138 'stp_p_bridge' => 'snPortStpPortDesignatedBridge',
139 'stp_p_port' => 'snPortStpPortDesignatedPort',
140 );
141  
142 %MUNGE = (
143 %SNMP::Info::Layer3::MUNGE,
144 %SNMP::Info::FDP::MUNGE,
145  
146 'ag_mod2_type' => \&SNMP::Info::munge_e_type,
147 'ag_mod_type' => \&SNMP::Info::munge_e_type,
148 'stp_i_mac' => \&SNMP::Info::munge_mac,
149 'stp_i_root' => \&SNMP::Info::munge_prio_mac,
150 'stp_p_root' => \&SNMP::Info::munge_prio_mac,
151 'stp_p_bridge' => \&SNMP::Info::munge_prio_mac,
152 'stp_p_port' => \&SNMP::Info::munge_prio_port,
153 );
154  
155 # FastIron's single spanning tree VLAN holds every port; it is not
156 # configured by the operator, so it is not reported
157 my $SINGLE_STP_VLAN_NAME = 'Single-spanning-tree-vlan';
158  
159 sub v_name {
160 my $foundry = shift;
161 my $partial = shift;
162  
163 unless ( $foundry->_foundry_vlan_source() eq 'foundry' ) {
164 my $names = $foundry->SUPER::v_name($partial);
165 return $names unless ref $names eq 'HASH';
166 return {
167 map { $_ => $names->{$_} }
168 grep { ( $names->{$_} // '' ) ne $SINGLE_STP_VLAN_NAME }
169 keys %$names
170 };
171 }
172  
173 my $vlan_ids = $foundry->_vlan_cfg_id($partial) || {};
174 my $vlan_names = $foundry->_vlan_cfg_name($partial) || {};
175  
176 my %v_name;
177 foreach my $idx ( keys %$vlan_ids ) {
178 my $vlan = $vlan_ids->{$idx};
179 next unless defined $vlan;
180 my $name = $vlan_names->{$idx};
181 next if defined $name and $name eq $SINGLE_STP_VLAN_NAME;
182 $v_name{$vlan} = defined $name ? $name : '';
183 }
184  
185 return \%v_name;
186 }
187  
188 sub _foundry_vlan_source {
189 my $foundry = shift;
190  
191 return $foundry->{_foundry_vlan_source}
192 if defined $foundry->{_foundry_vlan_source};
193  
194 my @bridge_data = (
195 $foundry->SUPER::v_name(),
196 $foundry->SUPER::i_vlan(),
197 $foundry->SUPER::i_vlan_membership(),
198 $foundry->SUPER::i_vlan_membership_untagged(),
199 );
200  
201 foreach my $data (@bridge_data) {
202 next unless ref $data eq 'HASH';
203 return $foundry->{_foundry_vlan_source} = 'bridge'
204 if keys %$data;
205 }
206  
207 my $cfg_ids = $foundry->_vlan_cfg_id() || {};
208 my $member_ids = $foundry->_vlan_member_vlan_id() || {};
209 return $foundry->{_foundry_vlan_source} = 'foundry'
210 if keys %$cfg_ids or keys %$member_ids;
211  
212 return $foundry->{_foundry_vlan_source} = 'bridge';
213 }
214  
215 # Q-BRIDGE port lists are unreliable on some ICX firmware (08.0.80 serves
216 # lists that are not port bitmaps), and the FastIron table matches them
217 # where they are correct, so prefer it when it carries the tag mode
218 sub _foundry_member_source {
219 my $foundry = shift;
220  
221 return $foundry->{_foundry_member_source}
222 if defined $foundry->{_foundry_member_source};
223  
224 # Devices without a tag mode column skip the other membership walks
225 my $tag_modes = $foundry->_vlan_member_tag_mode() || {};
226 my $tagged = keys %$tag_modes
227 && grep { defined $_->{tag_mode} } @{ $foundry->_vlan_member_rows() };
228 return $foundry->{_foundry_member_source}
229 = $tagged ? 'foundry' : $foundry->_foundry_vlan_source();
230 }
231  
232 sub _single_stp_vlans {
233 my $foundry = shift;
234  
235 my $vlan_ids = $foundry->_vlan_cfg_id() || {};
236 my $vlan_names = $foundry->_vlan_cfg_name() || {};
237  
238 return {
239 map { $vlan_ids->{$_} => 1 }
240 grep {
241 defined $vlan_ids->{$_}
242 and ( $vlan_names->{$_} // '' ) eq $SINGLE_STP_VLAN_NAME
243 } keys %$vlan_ids
244 };
245 }
246  
247 sub _vlan_member_rows {
248 my $foundry = shift;
249  
250 my $vlan_ids = $foundry->_vlan_member_vlan_id() || {};
251 my $port_ids = $foundry->_vlan_member_port_id() || {};
252 my $statuses = $foundry->_vlan_member_status() || {};
253 my $tag_modes = $foundry->_vlan_member_tag_mode() || {};
254 my $stp_vlans = $foundry->_single_stp_vlans();
255  
256 my @rows;
257 foreach my $idx ( keys %$vlan_ids ) {
258 my ($vlan_idx, $port_idx) = $idx =~ /^(\d+)\.(\d+)$/;
259 next unless defined $vlan_idx and defined $port_idx;
260  
261 my $vlan = $vlan_ids->{$idx};
262 my $port = $port_ids->{$idx};
263 next unless defined $vlan and defined $port and $vlan and $port;
264 next if $stp_vlans->{$vlan};
265  
266 my $status = $statuses->{$idx};
267 next unless defined $status and $status =~ /^(?:valid|2)(?:\(2\))?$/i;
268  
269 push @rows, {
270 vlan => $vlan,
271 ifindex => $port,
272 tag_mode => $tag_modes->{$idx},
273 };
274 }
275  
276 return \@rows;
277 }
278  
279 sub _foundry_ip_rows {
280 my $foundry = shift;
281  
282 my $addresses = $foundry->_ip_if_address() || {};
283 my $ifindexes = $foundry->_ip_if_index() || {};
284 my $netmasks = $foundry->_ip_if_netmask() || {};
285 my $statuses = $foundry->_ip_if_status() || {};
286  
287 my @rows;
288 foreach my $idx ( keys %$addresses ) {
289 my ($index_if, $index_ip) = $idx =~ /^(\d+)\.(\d+\.\d+\.\d+\.\d+)$/;
290 next unless defined $index_if and defined $index_ip;
291  
292 my $ifindex = $ifindexes->{$idx};
293 my $address = $addresses->{$idx};
294 my $netmask = $netmasks->{$idx};
295 my $status = $statuses->{$idx};
296 next
297 unless defined $ifindex and defined $address and defined $netmask;
298 next unless defined $status and $status =~ /^(?:valid|2)(?:\(2\))?$/i;
299  
300 push @rows, {
301 ifindex => $ifindex,
302 address => $address,
303 netmask => $netmask,
304 };
305 }
306  
307 return \@rows;
308 }
309  
310 sub _foundry_ip_source {
311 my $foundry = shift;
312  
313 return $foundry->{_foundry_ip_source}
314 if defined $foundry->{_foundry_ip_source};
315  
316 my $ip_index = $foundry->SUPER::ip_index() || {};
317 return $foundry->{_foundry_ip_source} = 'ip_mib'
318 if keys %$ip_index;
319  
320 my $addresses = $foundry->_ip_if_address() || {};
321 return $foundry->{_foundry_ip_source} = 'foundry'
322 if keys %$addresses;
323  
324 return $foundry->{_foundry_ip_source} = 'ip_mib';
325 }
326  
327 sub ip_index {
328 my $foundry = shift;
329  
330 return $foundry->SUPER::ip_index()
331 unless $foundry->_foundry_ip_source() eq 'foundry';
332  
333 my %ip_index;
334 foreach my $row ( @{ $foundry->_foundry_ip_rows() } ) {
335 $ip_index{ $row->{address} } = $row->{ifindex};
336 }
337  
338 return \%ip_index;
339 }
340  
341 sub ip_table {
342 my $foundry = shift;
343  
344 return $foundry->SUPER::ip_table()
345 unless $foundry->_foundry_ip_source() eq 'foundry';
346  
347 my %ip_table;
348 foreach my $row ( @{ $foundry->_foundry_ip_rows() } ) {
349 $ip_table{ $row->{address} } = $row->{address};
350 }
351  
352 return \%ip_table;
353 }
354  
355 sub ip_netmask {
356 my $foundry = shift;
357  
358 return $foundry->SUPER::ip_netmask()
359 unless $foundry->_foundry_ip_source() eq 'foundry';
360  
361 my %ip_netmask;
362 foreach my $row ( @{ $foundry->_foundry_ip_rows() } ) {
363 $ip_netmask{ $row->{address} } = $row->{netmask};
364 }
365  
366 return \%ip_netmask;
367 }
368  
369 sub i_vlan {
370 my $foundry = shift;
371 my $partial = shift;
372  
373 return $foundry->SUPER::i_vlan($partial)
374 unless $foundry->_foundry_vlan_source() eq 'foundry';
375  
376 my $rows = $foundry->_vlan_member_rows();
377  
378 my %i_vlan;
379 foreach my $row ( @$rows ) {
380 next unless defined $row->{tag_mode};
381 next unless $row->{tag_mode} =~ /^(?:untagged|2)(?:\(2\))?$/i;
382 next if defined $partial and $row->{ifindex} ne $partial;
383 $i_vlan{ $row->{ifindex} } = $row->{vlan};
384 }
385  
386 return \%i_vlan;
387 }
388  
389 sub i_untagged { goto &i_vlan }
390  
391 sub i_vlan_membership {
392 my $foundry = shift;
393 my $partial = shift;
394  
395 return $foundry->SUPER::i_vlan_membership($partial)
396 unless $foundry->_foundry_member_source() eq 'foundry';
397  
398 my $rows = $foundry->_vlan_member_rows();
399  
400 my %membership;
401 foreach my $row ( @$rows ) {
402 next if defined $partial and $row->{ifindex} ne $partial;
403 push @{ $membership{ $row->{ifindex} } }, $row->{vlan};
404 }
405  
406 return \%membership;
407 }
408  
409 sub i_vlan_membership_untagged {
410 my $foundry = shift;
411 my $partial = shift;
412  
413 return $foundry->SUPER::i_vlan_membership_untagged($partial)
414 unless $foundry->_foundry_member_source() eq 'foundry';
415  
416 my $rows = $foundry->_vlan_member_rows();
417  
418 my %membership;
419 foreach my $row ( @$rows ) {
420 next unless defined $row->{tag_mode};
421 next unless $row->{tag_mode} =~ /^(?:untagged|2)(?:\(2\))?$/i;
422 next if defined $partial and $row->{ifindex} ne $partial;
423 push @{ $membership{ $row->{ifindex} } }, $row->{vlan};
424 }
425  
426 return \%membership;
427 }
428  
429 sub i_ignore {
430 my $foundry = shift;
431 my $partial = shift;
432  
433 my $interfaces = $foundry->interfaces($partial) || {};
434  
435 my %i_ignore;
436 foreach my $if ( keys %$interfaces ) {
437 if ( $interfaces->{$if} =~ /(tunnel|loopback|\blo\b|lb|null)/i ) {
438 $i_ignore{$if}++;
439 }
440 }
441 return \%i_ignore;
442 }
443  
444 sub i_duplex {
445 my $foundry = shift;
446 my $partial = shift;
447  
448 my $sw_index = $foundry->sw_index($partial);
449 my $sw_duplex = $foundry->sw_duplex($partial);
450  
451 unless ( defined $sw_index and defined $sw_duplex ) {
452 return $foundry->SUPER::i_duplex();
453 }
454  
455 my %i_duplex;
456 foreach my $sw_port ( keys %$sw_duplex ) {
457 my $iid = $sw_index->{$sw_port};
458 my $duplex = $sw_duplex->{$sw_port};
459 next if $duplex =~ /none/i;
460 $i_duplex{$iid} = 'half' if $duplex =~ /half/i;
461 $i_duplex{$iid} = 'full' if $duplex =~ /full/i;
462 }
463 return \%i_duplex;
464 }
465  
466 # FastIron 10 serves snSwIfInfoSpeed and snSwIfInfoChnMode as the port's
467 # configuration, while FastIron 08 serves the running link there (none
468 # on a port that is down). They agree for a port that does not
469 # auto-negotiate, so they are read only for those. FastIron 10 keys
470 # ifMauAutoNegTable by ifIndex alone, rather than ifIndex.ifMauIndex.
471 sub _foundry_autoneg_by_ifindex {
472 my $foundry = shift;
473  
474 my $autoneg = $foundry->_mau_autoneg_admin() || {};
475  
476 my %by_ifindex;
477 foreach my $key ( keys %$autoneg ) {
478 my ($ifindex) = split /\./, $key;
479 $by_ifindex{$ifindex} = $autoneg->{$key};
480 }
481 return \%by_ifindex;
482 }
483  
484 my %_sw_if_speed_admin = (
485 's10M' => '10 Mbps',
486 's100M' => '100 Mbps',
487 's1G' => '1.0 Gbps',
488 's1GM' => '1.0 Gbps',
489 's2500M' => '2.5 Gbps',
490 's5000M' => '5.0 Gbps',
491 's10G' => '10 Gbps',
492 's25G' => '25 Gbps',
493 's40G' => '40 Gbps',
494 's100G' => '100 Gbps',
495 );
496  
497 sub i_speed_admin {
498 my $foundry = shift;
499  
500 my $autoneg = $foundry->_foundry_autoneg_by_ifindex();
501 return {} unless %$autoneg;
502 my $speeds = $foundry->_sw_if_speed() || {};
503  
504 my %i_speed_admin;
505 foreach my $ifindex ( keys %$autoneg ) {
506 my $speed
507 = $autoneg->{$ifindex} eq 'enabled'
508 ? 'auto'
509 : $_sw_if_speed_admin{ $speeds->{$ifindex} // '' };
510 $i_speed_admin{$ifindex} = $speed if defined $speed;
511 }
512 return \%i_speed_admin;
513 }
514  
515 my %_sw_if_duplex_admin = (
516 'halfDuplex' => 'half',
517 'fullDuplex' => 'full',
518 );
519  
520 sub i_duplex_admin {
521 my $foundry = shift;
522  
523 my $autoneg = $foundry->_foundry_autoneg_by_ifindex();
524 return {} unless %$autoneg;
525 my $duplexes = $foundry->_sw_if_duplex() || {};
526  
527 my %i_duplex_admin;
528 foreach my $ifindex ( keys %$autoneg ) {
529 my $duplex
530 = $autoneg->{$ifindex} eq 'enabled'
531 ? 'auto'
532 : $_sw_if_duplex_admin{ $duplexes->{$ifindex} // '' };
533 $i_duplex_admin{$ifindex} = $duplex if defined $duplex;
534 }
535 return \%i_duplex_admin;
536 }
537  
538 sub model {
539 my $foundry = shift;
540 my $id = $foundry->id();
541 my $model = &SNMP::translateObj($id);
542  
543 # A standalone ICX names its model as the CLI does:
544 # "Ruckus Wireless, Inc. ICX7150-C12-POE, IronWare Version ..."
545 my $descr = $foundry->description() || '';
546 return $1 if $descr =~ /^[^,]+, Inc\. (ICX[\w-]+), /;
547  
548 # EdgeIron
549 if ( $id =~ /\.1991\.1\.[45]\./ ) {
550  
551 my $e_name = $foundry->e_name();
552  
553 # Find entity table entry for "unit.1"
554 my $unit_iid = undef;
555 foreach my $e ( keys %$e_name ) {
556 my $name = $e_name->{$e} || '';
557 $unit_iid = $e if $name eq 'unit.1';
558 }
559  
560 # Find Model Name
561 my $e_model = $foundry->e_model();
562 if ( defined $e_model->{$unit_iid} ) {
563 return $e_model->{$unit_iid};
564 }
565 }
566  
567 return $id unless defined $model;
568  
569 $model =~ s/^sn//;
570 $model =~ s/Switch//;
571  
572 return $model;
573 }
574  
575 sub os {
576 return 'brocade';
577 }
578  
579 sub vendor {
580 return 'brocade';
581 }
582  
583 sub os_ver {
584 my $foundry = shift;
585  
586 return $foundry->img_ver() if ( defined $foundry->img_ver() );
587  
588 # Some older ones don't have this value,so we cull it from the description
589 my $descr = $foundry->description();
590 if ( $descr =~ m/Version (\d\S*)/ ) {
591 return $1;
592 }
593  
594 # EdgeIron
595 my $e_name = $foundry->e_name();
596  
597 # find entity table entry for "stackmanaget.1"
598 my $unit_iid = undef;
599 foreach my $e ( keys %$e_name ) {
600 my $name = $e_name->{$e} || '';
601 $unit_iid = $e if $name eq 'stackmanaget.1';
602 }
603  
604 if ( defined $unit_iid ) {
605  
606 # Find Model Name
607 my $e_fwver = $foundry->e_fwver();
608 if ( defined $e_fwver->{$unit_iid} ) {
609 return $e_fwver->{$unit_iid};
610 }
611 }
612  
613 # See if we report from Flash if wouldn't report from running above
614 return $foundry->snAgFlashImgVer() if ( defined $foundry->snAgFlashImgVer() );
615  
616 # Last resort
617 return $foundry->SUPER::os_ver();
618  
619 }
620  
621 sub serial {
622 my $foundry = shift;
623  
624 # Return chassis serial number if available
625 return $foundry->ch_serial() if ( $foundry->ch_serial() );
626  
627 # If no chassis serial use first module serial
628 my $mod_serials = $foundry->snAgentConfigModuleSerialNumber() || {};
629  
630 foreach my $mod ( sort keys %$mod_serials ) {
631 my $serial = $mod_serials->{$mod} || '';
632 next unless defined $serial;
633 return $serial;
634 }
635  
636 # EdgeIron
637 my $e_name = $foundry->e_name();
638  
639 # find entity table entry for "unit.1"
640 my $unit_iid = undef;
641 foreach my $e ( keys %$e_name ) {
642 my $name = $e_name->{$e} || '';
643 $unit_iid = $e if $name eq 'unit.1';
644 }
645  
646 if ( defined $unit_iid ) {
647  
648 # Look up serial of found entry.
649 my $e_serial = $foundry->e_serial();
650 return $e_serial->{$unit_iid} if defined $e_serial->{$unit_iid};
651 }
652  
653 # Last resort
654 return $foundry->SUPER::serial();
655 }
656  
657 sub interfaces {
658 my $foundry = shift;
659 my $partial = shift;
660  
661 my $i_descr = $foundry->i_description($partial) || {};
662 my $i_name = $foundry->i_name($partial) || {};
663  
664 # Use ifName for EdgeIrons else use ifDescr
665 foreach my $iid ( keys %$i_name ) {
666 my $name = $i_name->{$iid};
667 next unless defined $name;
668 $i_descr->{$iid} = $name
669 if $name =~ /^port\d+/i;
670 }
671  
672 return $i_descr;
673 }
674  
675 sub set_i_vlan {
676 my ($foundry, $vlan, $iid) = @_;
677 my $i_vlan = $foundry->i_vlan($iid);
678  
679 if ( defined $i_vlan ) {
680 print
681 "Changing VLAN from $i_vlan->{$iid} to $vlan on IfIndex: $iid\n"
682 if $foundry->debug();
683  
684 # quietly try to remove new vlan association
685 $foundry->set_snVLanByPortMemberRowStatus( 3, "${vlan}.${iid}" );
686 sleep 1;
687 # try to remove old vlan association
688 my $rv_rem = $foundry->set_snVLanByPortMemberRowStatus( 3, "$i_vlan->{$iid}.$iid" );
689 sleep 1;
690  
691 unless ($rv_rem) {
692 print "Unable to remove untagged(?) VLAN $i_vlan->{$iid} from IfIndex: $iid\n"
693 if $foundry->debug();
694 # $foundry->error_throw(
695 # "Unable to remove VLAN $i_vlan->{$iid} from interface: $iid");
696 # return;
697 }
698  
699 # try to add new one untagged
700 my $rv_add = $foundry->set_multi([
701 ['snVLanByPortMemberRowStatus', "${vlan}.${iid}", 4],
702 ['snVLanByPortMemberTagMode', "${vlan}.${iid}", 2],
703 ]);
704 sleep 1;
705  
706 unless ($rv_add) {
707 print "Unable to add untagged VLAN $vlan to IfIndex: $iid\n"
708 if $foundry->debug();
709  
710 # did not work to add new untagged so add tagged
711 my $rv_add_tagged = $foundry->set_multi([
712 ['snVLanByPortMemberRowStatus', "${vlan}.${iid}", 4],
713 ['snVLanByPortMemberTagMode', "${vlan}.${iid}", 1],
714 ]);
715 sleep 1;
716  
717 if ($rv_add_tagged) {
718 my $rv_change_untagged = $foundry->set_snVLanByPortMemberTagMode( 2, "${vlan}.${iid}" );
719 sleep 1;
720 return $rv_change_untagged if $rv_change_untagged;
721 }
722  
723 # try to remove old vlan association
724 # $foundry->set_snVLanByPortMemberRowStatus( 3, "${vlan}.${iid}" );
725  
726 # then change to untagged
727 $foundry->error_throw(
728 "Unable to add VLAN $vlan to interface: $iid");
729 return;
730 }
731  
732 return $rv_add;
733 }
734 $foundry->error_throw("Can't find ifIndex: $iid - Is it an access port?");
735 return;
736 }
737  
738 # Entity MIB is supported on the Brocade NetIron XMR, NetIron MLX, MLXe,
739 # NetIron CES, NetIron CER, and older EdgeIron series devices.
740 # Try Entity MIB methods first and fall back to Pseudo ENTITY-MIB methods for
741 # other devices.
742 # e_fwver, e_hwver, e_swver not supported in pseudo methods, no need to
743 # override
744  
745 sub e_index {
746 my $foundry = shift;
747 my $partial = shift;
748  
749 return $foundry->SUPER::e_index($partial)
750 || $foundry->brcd_e_index($partial);
751 }
752  
753 sub e_class {
754 my $foundry = shift;
755 my $partial = shift;
756  
757 return $foundry->SUPER::e_class($partial)
758 || $foundry->brcd_e_class($partial);
759 }
760  
761 sub e_descr {
762 my $foundry = shift;
763 my $partial = shift;
764  
765 return $foundry->SUPER::e_descr($partial)
766 || $foundry->brcd_e_descr($partial);
767 }
768  
769 sub e_name {
770 my $foundry = shift;
771 my $partial = shift;
772  
773 return $foundry->SUPER::e_name($partial)
774 || $foundry->brcd_e_name($partial);
775 }
776  
777 sub e_parent {
778 my $foundry = shift;
779 my $partial = shift;
780  
781 return $foundry->SUPER::e_parent($partial)
782 || $foundry->brcd_e_parent($partial);
783 }
784  
785 sub e_pos {
786 my $foundry = shift;
787 my $partial = shift;
788  
789 return $foundry->SUPER::e_pos($partial) || $foundry->brcd_e_pos($partial);
790 }
791  
792 sub e_serial {
793 my $foundry = shift;
794 my $partial = shift;
795  
796 return $foundry->SUPER::e_serial($partial)
797 || $foundry->brcd_e_serial($partial);
798 }
799  
800 sub e_type {
801 my $foundry = shift;
802 my $partial = shift;
803  
804 return $foundry->SUPER::e_type($partial)
805 || $foundry->brcd_e_type($partial);
806 }
807  
808 sub e_vendor {
809 my $foundry = shift;
810 my $partial = shift;
811  
812 return $foundry->SUPER::e_vendor($partial)
813 || $foundry->brcd_e_vendor($partial);
814 }
815  
816 # Pseudo ENTITY-MIB methods
817  
818 # This class supports both stackable and chassis based switches, identify if
819 # we have a stackable so that we return appropriate entPhysicalClass
820  
821 # Identify if the stackable is actually a stack vs. single switch
822 sub _brcd_stack_master {
823 my $foundry = shift;
824  
825 my $roles = $foundry->snStackingOperUnitRole() || {};
826  
827 foreach my $iid ( keys %$roles ) {
828 my $role = $roles->{$iid};
829 next unless $role;
830 if ( $role eq 'active' ) {
831 return $iid;
832 }
833 }
834 return;
835 }
836  
837 sub brcd_e_index {
838 my $foundry = shift;
839 my $partial = shift;
840  
841 my $stack_master = $foundry->_brcd_stack_master();
842 my $brcd_e_idx
843 = $foundry->snAgentConfigModule2Description($partial)
844 || $foundry->snAgentConfigModuleDescription($partial)
845 || {};
846  
847 my %brcd_e_index;
848 if ($stack_master) {
849  
850 # Stack Entity
851 $brcd_e_index{0} = 1;
852 }
853  
854 foreach my $iid ( keys %$brcd_e_idx ) {
855  
856 my $index = $iid;
857  
858 # Format into consistent integer format so that numeric sorting works
859 if ( $iid =~ /(\d+)\.(\d+)/ ) {
860 $index = "$1" . sprintf "%02d", $2;
861 }
862 $brcd_e_index{$iid} = $index;
863 }
864 return \%brcd_e_index;
865 }
866  
867 sub brcd_e_class {
868 my $foundry = shift;
869 my $partial = shift;
870  
871 my $e_idx = $foundry->brcd_e_index($partial) || {};
872  
873 my %e_class;
874 foreach my $iid ( keys %$e_idx ) {
875 if ( $iid == 0 ) {
876 $e_class{$iid} = 'stack';
877 }
878  
879 # Were going to assume chassis at slot/index 1
880 # If this turns out to be false in some cases we can check
881 # snAgentConfigModuleNumberOfCpus as other modules won't have cpus?
882 elsif ( $iid =~ /1$/ ) {
883 $e_class{$iid} = 'chassis';
884 }
885 else {
886 $e_class{$iid} = 'module';
887 }
888 }
889 return \%e_class;
890 }
891  
892 sub brcd_e_descr {
893 my $foundry = shift;
894 my $partial = shift;
895  
896 my $brcd_e_idx = $foundry->brcd_e_index($partial) || {};
897 my $m_descrs
898 = $foundry->snAgentConfigModule2Description($partial)
899 || $foundry->snAgentConfigModuleDescription($partial)
900 || {};
901  
902 my %brcd_e_descr;
903 foreach my $iid ( keys %$brcd_e_idx ) {
904  
905 if ( $iid == 0 ) {
906 $brcd_e_descr{$iid} = $foundry->description();
907 }
908  
909 my $descr = $m_descrs->{$iid};
910 next unless defined $descr;
911  
912 $brcd_e_descr{$iid} = $descr;
913 }
914 return \%brcd_e_descr;
915 }
916  
917 sub brcd_e_name {
918 my $foundry = shift;
919 my $partial = shift;
920  
921 my $stack_master = $foundry->_brcd_stack_master();
922 my $e_idx = $foundry->brcd_e_index($partial) || {};
923  
924 my %brcd_e_name;
925 foreach my $iid ( keys %$e_idx ) {
926 if ( $iid == 0 ) {
927 $brcd_e_name{$iid} = 'Stack Master Unit';
928 }
929  
930 elsif ( $stack_master && $iid =~ /(\d+)\.1$/ ) {
931 $brcd_e_name{$iid} = "Switch Stack Unit $1";
932 }
933 elsif ( $iid =~ /1$/ ) {
934 $brcd_e_name{$iid} = "Switch";
935 }
936 else {
937 $brcd_e_name{$iid} = 'Module';
938 }
939 }
940 return \%brcd_e_name;
941 }
942  
943 sub brcd_e_vendor {
944 my $foundry = shift;
945 my $partial = shift;
946  
947 my $e_idx = $foundry->brcd_e_index($partial) || {};
948  
949 my %brcd_e_vendor;
950 foreach my $iid ( keys %$e_idx ) {
951 my $vendor = 'brocade';
952  
953 $brcd_e_vendor{$iid} = $vendor;
954 }
955 return \%brcd_e_vendor;
956 }
957  
958 sub brcd_e_serial {
959 my $foundry = shift;
960 my $partial = shift;
961  
962 my $e_idx = $foundry->brcd_e_index($partial) || {};
963 my $serials
964 = $foundry->snAgentConfigModule2SerialNumber($partial)
965 || $foundry->snAgentConfigModuleSerialNumber($partial)
966 || {};
967  
968 my %brcd_e_serial;
969 foreach my $iid ( keys %$e_idx ) {
970  
971 if ( $iid == 0 ) {
972 $brcd_e_serial{$iid} = $foundry->serial();
973 }
974  
975 my $serial = $serials->{$iid};
976 next unless defined $serial;
977  
978 $brcd_e_serial{$iid} = $serial;
979 }
980 return \%brcd_e_serial;
981 }
982  
983 sub brcd_e_type {
984 my $foundry = shift;
985 my $partial = shift;
986  
987 my $e_idx = $foundry->brcd_e_index($partial) || {};
988 my $types
989 = $foundry->ag_mod2_type($partial)
990 || $foundry->ag_mod_type($partial)
991 || {};
992  
993 my %brcd_e_type;
994 foreach my $iid ( keys %$e_idx ) {
995  
996 if ( $iid == 0 ) {
997 $brcd_e_type{$iid} = $foundry->model();
998 }
999  
1000 my $type = $types->{$iid};
1001 next unless defined $type;
1002  
1003 $brcd_e_type{$iid} = $type;
1004 }
1005 return \%brcd_e_type;
1006 }
1007  
1008 sub brcd_e_pos {
1009 my $foundry = shift;
1010 my $partial = shift;
1011  
1012 my $e_idx = $foundry->brcd_e_index($partial) || {};
1013  
1014 my %brcd_e_pos;
1015 foreach my $iid ( keys %$e_idx ) {
1016  
1017 my $pos;
1018 if ( $iid == 0 ) {
1019 $pos = -1;
1020 }
1021 elsif ( $iid =~ /(\d+)\.1$/ ) {
1022 $pos = $1;
1023 }
1024 elsif ( $iid =~ /(\d+)$/ ) {
1025 $pos = $1;
1026 }
1027  
1028 $brcd_e_pos{$iid} = $pos;
1029 }
1030 return \%brcd_e_pos;
1031 }
1032  
1033 sub brcd_e_parent {
1034 my $foundry = shift;
1035 my $partial = shift;
1036  
1037 my $stack_master = $foundry->_brcd_stack_master();
1038 my $e_idx = $foundry->brcd_e_index($partial) || {};
1039  
1040 my %brcd_e_parent;
1041 foreach my $iid ( keys %$e_idx ) {
1042  
1043 if ( $iid == 0 ) {
1044 $brcd_e_parent{$iid} = 0;
1045 }
1046 elsif ( $stack_master && $iid =~ /(\d+)\.1$/ ) {
1047 $brcd_e_parent{$iid} = 1;
1048 }
1049 elsif ( $iid =~ /1$/ ) {
1050 $brcd_e_parent{$iid} = 0;
1051 }
1052 elsif ( $iid =~ /(\d+).\d+/ ) {
1053 $brcd_e_parent{$iid} = "$1" . "01";
1054 }
1055  
1056 # assume non-stacked and chassis at index 1
1057 else {
1058 $brcd_e_parent{$iid} = 1;
1059 }
1060 }
1061 return \%brcd_e_parent;
1062 }
1063  
1064 # The index of snAgentPoePortTable is snAgentPoePortNumber which equals
1065 # ifIndex; however, to emulate POWER-ETHERNET-MIB we need a "module.port"
1066 # index. If ifDescr has the format x/x/x use it to determine the module
1067 # otherwise default to 1. Unfortunately, this means we can't map any
1068 # snAgentPoePortTable leafs directly and partials will not be supported.
1069 sub peth_port_ifindex {
1070 my $foundry = shift;
1071  
1072 my $indexes = $foundry->snAgentPoePortNumber() || {};
1073 my $controls = $foundry->snAgentPoePortControl() || {};
1074 my $descrs = $foundry->i_description() || {};
1075  
1076 my $peth_port_ifindex = {};
1077 foreach my $i ( keys %$indexes ) {
1078 my $descr = $descrs->{$i};
1079 next unless $descr;
1080  
1081 # other(1): the port has no inline power hardware
1082 next if ( $controls->{$i} // '' ) eq 'other';
1083  
1084 my $new_idx = "1.$i";
1085  
1086 if ( $descr =~ /(\d+)\/\d+\/\d+/ ) {
1087 $new_idx = "$1.$i";
1088 }
1089 $peth_port_ifindex->{$new_idx} = $i;
1090 }
1091 return $peth_port_ifindex;
1092 }
1093  
1094 sub peth_port_admin {
1095 my $foundry = shift;
1096  
1097 my $p_index = $foundry->peth_port_ifindex() || {};
1098 my $admin_states = $foundry->snAgentPoePortControl() || {};
1099  
1100 my $peth_port_admin = {};
1101 foreach my $i ( keys %$p_index ) {
1102 my ( $module, $port ) = split( /\./, $i );
1103 my $state = $admin_states->{$port};
1104  
1105 if ( $state =~ /enable/ ) {
1106 $peth_port_admin->{$i} = 'true';
1107 }
1108 else {
1109 $peth_port_admin->{$i} = 'false';
1110 }
1111 }
1112 return $peth_port_admin;
1113 }
1114  
1115 my %_poe_class_max_mw = (
1116 '0' => 12950,
1117 '1' => 3840,
1118 '2' => 6490,
1119 '3' => 12950,
1120 '4' => 25500
1121 );
1122  
1123 sub peth_port_neg_power {
1124 my $foundry = shift;
1125  
1126 my $p_index = $foundry->peth_port_ifindex() || {};
1127 my $consumed = $foundry->snAgentPoePortConsumed() || {};
1128 my $wattage = $foundry->snAgentPoePortWattage() || {};
1129 my $classes = $foundry->_foundry_poe_pd_class();
1130  
1131 my $peth_port_neg_power = {};
1132 foreach my $i ( keys %$p_index ) {
1133 my $port = $p_index->{$i};
1134 next unless $consumed->{$port};
1135  
1136 my $power = $wattage->{$port}
1137 || $_poe_class_max_mw{ $classes->{$port} // 0 };
1138 $peth_port_neg_power->{$i} = $power;
1139 }
1140 return $peth_port_neg_power;
1141 }
1142  
1143 # snAgentPoePortClass is the class the user configured, 0 when none.
1144 # FastIron 10 reports the class the powered device signalled in
1145 # snAgentPoePortPDClass; FastIron 08 has no such column and reports it in
1146 # snAgentPoePortClass.
1147 sub _foundry_poe_pd_class {
1148 my $foundry = shift;
1149  
1150 my $configured = $foundry->snAgentPoePortClass() || {};
1151 my $signalled = $foundry->snAgentPoePortPDClass() || {};
1152  
1153 my %pd_class;
1154 foreach my $port ( keys %$configured ) {
1155 $pd_class{$port} = $signalled->{$port} || $configured->{$port};
1156 }
1157 return \%pd_class;
1158 }
1159  
1160 sub peth_port_power {
1161 my $foundry = shift;
1162  
1163 my $p_index = $foundry->peth_port_ifindex() || {};
1164 my $port_consumed = $foundry->snAgentPoePortConsumed() || {};
1165  
1166 my $peth_port_power = {};
1167 foreach my $i ( keys %$p_index ) {
1168 my ( $module, $port ) = split( /\./, $i );
1169 my $power = $port_consumed->{$port};
1170 next unless $power;
1171  
1172 $peth_port_power->{$i} = $power;
1173 }
1174 return $peth_port_power;
1175 }
1176  
1177 sub peth_port_class {
1178 my $foundry = shift;
1179  
1180 my $p_index = $foundry->peth_port_ifindex() || {};
1181 my $classes = $foundry->_foundry_poe_pd_class();
1182  
1183 my $peth_port_class = {};
1184 foreach my $i ( keys %$p_index ) {
1185 my $class = $classes->{ $p_index->{$i} };
1186 next unless $class;
1187  
1188 $peth_port_class->{$i} = "class$class";
1189 }
1190 return $peth_port_class;
1191 }
1192  
1193 # The table has no detection status: a port consuming power is delivering
1194 # it, an enabled port that is not is searching.
1195 sub peth_port_status {
1196 my $foundry = shift;
1197  
1198 my $p_index = $foundry->peth_port_ifindex() || {};
1199 my $admin_states = $foundry->snAgentPoePortControl() || {};
1200 my $consumed = $foundry->snAgentPoePortConsumed() || {};
1201  
1202 my $peth_port_status = {};
1203 foreach my $i ( keys %$p_index ) {
1204 my $port = $p_index->{$i};
1205  
1206 if ( ( $admin_states->{$port} // '' ) !~ /enable/ ) {
1207 $peth_port_status->{$i} = 'disabled';
1208 }
1209 elsif ( $consumed->{$port} ) {
1210 $peth_port_status->{$i} = 'deliveringPower';
1211 }
1212 else {
1213 $peth_port_status->{$i} = 'searching';
1214 }
1215 }
1216 return $peth_port_status;
1217 }
1218  
1219 sub peth_power_status {
1220 my $foundry = shift;
1221 my $partial = shift;
1222  
1223 my $watts = $foundry->snAgentPoeUnitPowerCapacityTotal($partial) || {};
1224  
1225 my $peth_power_status = {};
1226 foreach my $i ( keys %$watts ) {
1227 $peth_power_status->{$i} = 'on';
1228 }
1229 return $peth_power_status;
1230 }
1231  
1232 sub peth_power_watts {
1233 my $foundry = shift;
1234 my $partial = shift;
1235  
1236 my $watts_total = $foundry->snAgentPoeUnitPowerCapacityTotal($partial)
1237 || {};
1238  
1239 my $peth_power_watts = {};
1240 foreach my $i ( keys %$watts_total ) {
1241 my $total = $watts_total->{$i};
1242 next unless $total;
1243  
1244 $peth_power_watts->{$i} = $total / 1000;
1245 }
1246 return $peth_power_watts;
1247 }
1248  
1249 sub peth_power_consumption {
1250 my $foundry = shift;
1251 my $partial = shift;
1252  
1253 my $watts_total = $foundry->snAgentPoeUnitPowerCapacityTotal($partial)
1254 || {};
1255 my $watts_free = $foundry->snAgentPoeUnitPowerCapacityFree($partial)
1256 || {};
1257  
1258 my $peth_power_consumed = {};
1259 foreach my $i ( keys %$watts_total ) {
1260 my $total = $watts_total->{$i};
1261 next unless $total;
1262 my $free = $watts_free->{$i} || 0;
1263  
1264 $peth_power_consumed->{$i} = ( $total - $free ) / 1000;
1265 }
1266 return $peth_power_consumed;
1267 }
1268  
1269 sub agg_ports {
1270 my $dev = shift;
1271  
1272 # TODO: implement partial
1273 my $trunks = $dev->snMSTrunkPortList;
1274 my $ports = $dev->snSwPortIfIndex; # sw_index()
1275  
1276 return {} unless
1277 ref {} eq ref $trunks and scalar keys %$trunks
1278 and ref {} eq ref $ports and scalar keys %$ports;
1279  
1280 my $ret = {};
1281 foreach my $m (keys %$trunks) {
1282 next unless $m and exists $ports->{$m} and defined $ports->{$m};
1283  
1284 foreach my $s (unpack('n*', $trunks->{$m})) {
1285 next if $s == $m;
1286 next unless $s and exists $ports->{$s} and defined $ports->{$s};
1287  
1288 $ret->{ $ports->{$s} } = $ports->{$m};
1289 }
1290 }
1291  
1292 return $ret;
1293 }
1294  
1295 sub _agg_ports_walked { return qw/snMSTrunkPortList snSwPortIfIndex/ }
1296  
1297 sub i_stp_state {
1298 my $foundry = shift;
1299 my $partial = shift;
1300  
1301 my $bp_index = $foundry->bp_index($partial);
1302 my $stp_p_state = $foundry->dot1dStpPortState($partial);
1303  
1304 my %i_stp_state;
1305  
1306 foreach my $index ( keys %$stp_p_state ) {
1307 my $state = $stp_p_state->{$index};
1308 my $iid = $bp_index->{$index};
1309 next unless defined $iid;
1310 next unless defined $state;
1311 $i_stp_state{$iid} = $state;
1312 }
1313  
1314 return \%i_stp_state;
1315 }
1316  
1317 # Default tree is not the common tree: per-VLAN STP (snPortStp*)
1318 sub _stp_default_is_common { return 0; }
1319  
1320 1;
1321 __END__