Info/RapidCity.pm untested

filter: Statementstmt 0.0% 0 / 584 Branchbran 0.0% 0 / 256 Conditioncond 0.0% 0 / 260 MC/DCmcdc 0.0% 0 / 142 Subroutinesub 0.0% 0 / 83 Podpod 0.0% 0 / 78 totaltotal 0.0% 0 / 1403 CC 129 SCAR 35.0
CC
129
Cov
0%
set_i_vlan
210.0
_rc_i_vlan_tagging
72.0
set_add_i_vlan_tagged
72.0
CRAP 33.0
1 # SNMP::Info::RapidCity
2 #
3 # Copyright (c) 2014 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::RapidCity;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use SNMP::Info;
36 use SNMP::Info::IEEE802dot3ad ();
37  
38 @SNMP::Info::RapidCity::ISA = qw/SNMP::Info Exporter/;
39 @SNMP::Info::RapidCity::EXPORT_OK = qw//;
40  
41 our ($VERSION, %FUNCS, %GLOBALS, %MIBS, %MUNGE);
42  
43 $VERSION = '3.985003';
44  
45 %MIBS = (
46 'RAPID-CITY' => 'rapidCity',
47 # These are distinct from RAPID-CITY but are potentially used by
48 # classes which inherit the RAPID-CITY class
49 'NORTEL-NETWORKS-RAPID-SPANNING-TREE-MIB' => 'nnRstDot1dStpVersion',
50 'NORTEL-NETWORKS-MULTIPLE-SPANNING-TREE-MIB' => 'nnMstBrgAddress',
51 );
52  
53 %GLOBALS = (
54 'rc_serial' => 'rcChasSerialNumber',
55 'chassis' => 'rcChasType',
56 'slots' => 'rcChasNumSlots',
57 'tftp_host' => 'rcTftpHost',
58 'tftp_file' => 'rcTftpFile',
59 'tftp_action' => 'rcTftpAction',
60 'tftp_result' => 'rcTftpResult',
61 'rc_ch_rev' => 'rcChasHardwareRevision',
62 'rc_base_mac' => 'rc2kChassisBaseMacAddr',
63  
64 # NORTEL-NETWORKS-MULTIPLE-SPANNING-TREE-MIB
65 'nn_mst_version' => 'nnMstForceProtocolVersion',
66 'nn_mst_brg_address' => 'nnMstBrgAddress',
67 'nn_mst_cist_root' => 'nnMstCistRoot',
68 'nn_mst_cist_priority' => 'nnMstCistBridgePriority',
69  
70 # NORTEL-NETWORKS-RAPID-SPANNING-TREE-MIB
71 'nn_rst_version' => 'nnRstDot1dStpVersion',
72  
73 # From RAPID-CITY::rcVirtualIst
74 'rc_vist_status' => 'rcVirtualIstSessionStatus',
75 'rc_vist_peer' => 'rcVirtualIstPeerIp',
76 'rc_vist_vlan' => 'rcVirtualIstVlanId',
77  
78 # From RAPID-CITY::rcLacpGlobal
79 'rc_smlt_sys_id' => 'rcLacpGlobalSmltSysId',
80 );
81  
82 %FUNCS = (
83  
84 # From RAPID-CITY::rcPortTable
85 'rc_index' => 'rcPortIndex',
86 'rc_duplex' => 'rcPortOperDuplex',
87 'rc_duplex_admin' => 'rcPortAdminDuplex',
88 'rc_speed_admin' => 'rcPortAdminSpeed',
89 'rc_auto' => 'rcPortAutoNegotiate',
90 'rc_alias' => 'rcPortName',
91  
92 # From RAPID-CITY::rcVlanPortTable
93 'rc_i_vlan_if' => 'rcVlanPortIndex',
94 'rc_i_vlan_num' => 'rcVlanPortNumVlanIds',
95 'rc_i_vlan_type' => 'rcVlanPortType',
96 'rc_i_vlan_pvid' => 'rcVlanPortDefaultVlanId',
97 'rc_i_vlan_tag' => 'rcVlanPortPerformTagging',
98  
99 # From RAPID-CITY::rcVlanTable
100 'rc_vlan_id' => 'rcVlanId',
101 'v_name' => 'rcVlanName',
102 'rc_vlan_color' => 'rcVlanColor',
103 'rc_vlan_if' => 'rcVlanIfIndex',
104 'rc_vlan_stg' => 'rcVlanStgId',
105 'rc_vlan_type' => 'rcVlanType',
106 'rc_vlan_members' => 'rcVlanPortMembers',
107 'rc_vlan_no_join' => 'rcVlanNotAllowToJoin',
108 'rc_vlan_mac' => 'rcVlanMacAddress',
109 'rc_vlan_rstatus' => 'rcVlanRowStatus',
110 'rc_vlan_isid' => 'rcVlanPlsbIsid',
111  
112 # From RAPID-CITY::rcIsidInterfaceTable
113 'rc_isid_if_vlan' => 'rcIsidInterfaceVlan',
114 'rc_isid_if_cvid' => 'rcIsidInterfaceCvid',
115 'rc_isid_if_type' => 'rcIsidInterfaceType',
116  
117 # From RAPID-CITY::rcIpAddrTable
118 'rc_ip_index' => 'rcIpAdEntIfIndex',
119 'rc_ip_addr' => 'rcIpAdEntAddr',
120 'rc_ip_type' => 'rcIpAdEntIfType',
121  
122 # From RAPID-CITY::rcChasFanTable
123 'rc_fan_op' => 'rcChasFanOperStatus',
124  
125 # From RAPID-CITY::rcChasPowerSupplyTable
126 'rc_ps_op' => 'rcChasPowerSupplyOperStatus',
127  
128 # From RAPID-CITY::rcChasPowerSupplyDetailTable
129 'rc_ps_type' => 'rcChasPowerSupplyDetailType',
130 'rc_ps_serial' => 'rcChasPowerSupplyDetailSerialNumber',
131 'rc_ps_rev' => 'rcChasPowerSupplyDetailHardwareRevision',
132 'rc_ps_part' => 'rcChasPowerSupplyDetailPartNumber',
133 'rc_ps_detail' => 'rcChasPowerSupplyDetailDescription',
134  
135 # From RAPID-CITY::rc2kCardTable
136 'rc2k_c_ftype' => 'rc2kCardFrontType',
137 'rc2k_c_fdesc' => 'rc2kCardFrontDescription',
138 'rc2k_c_fserial' => 'rc2kCardFrontSerialNum',
139 'rc2k_c_frev' => 'rc2kCardFrontHwVersion',
140 'rc2k_c_fpart' => 'rc2kCardFrontPartNumber',
141 'rc2k_c_fdate' => 'rc2kCardFrontDateCode',
142 'rc2k_c_fdev' => 'rc2kCardFrontDeviations',
143  
144 # From RAPID-CITY::rcMltTable
145 'rc_mlt_ports' => 'rcMltPortMembers',
146 'rc_mlt_index' => 'rcMltIfIndex',
147 'rc_mlt_dp' => 'rcMltDesignatedPort',
148 'rc_mlt_type' => 'rcMltMltType',
149 'rc_mlt_run_type' => 'rcMltRunningType',
150  
151 # From RAPID-CITY::rcStgTable
152 'stp_i_id' => 'rcStgId',
153 'rc_stp_i_mac' => 'rcStgBridgeAddress',
154 'rc_stp_i_time' => 'rcStgTimeSinceTopologyChange',
155 'rc_stp_i_ntop' => 'rcStgTopChanges',
156 'rc_stp_i_root' => 'rcStgDesignatedRoot',
157 'rc_stp_i_root_port' => 'rcStgRootPort',
158 'rc_stp_i_priority' => 'rcStgPriority',
159  
160 # From RAPID-CITY::rcStgPortTable
161 'rc_stp_p_id' => 'rcStgPort',
162 'stp_p_stg_id' => 'rcStgPortStgId',
163 'rc_stp_p_priority' => 'rcStgPortPriority',
164 'rc_stp_p_state' => 'rcStgPortState',
165 'rc_stp_p_cost' => 'rcStgPortPathCost',
166 'rc_stp_p_root' => 'rcStgPortDesignatedRoot',
167 'rc_stp_p_bridge' => 'rcStgPortDesignatedBridge',
168 'rc_stp_p_port' => 'rcStgPortDesignatedPort',
169 'rc_stp_p_enable' => 'rcStgPortEnableStp',
170  
171 # From RAPID-CITY::rcStgTable, indexed by STG id
172 'rc_stg_enable' => 'rcStgEnableStp',
173  
174 # NORTEL-NETWORKS-MULTIPLE-SPANNING-TREE-MIB, indexed by instance
175 'nn_msti_priority' => 'nnMstBridgePriority',
176 'nn_msti_root' => 'nnMstBridgeRegionalRoot',
177  
178 # NORTEL-NETWORKS-MULTIPLE-SPANNING-TREE-MIB, indexed by bridge port
179 'nn_cist_p_state' => 'nnMstCistPortState',
180 'nn_cist_p_role' => 'nnMstCistCurrentPortRole',
181 'nn_cist_p_bridge' => 'nnMstCistPortDesignatedBridge',
182 'nn_cist_p_port' => 'nnMstCistPortDesignatedPort',
183 'nn_cist_p_enable' => 'nnMstCistForcePortState',
184  
185 # NORTEL-NETWORKS-MULTIPLE-SPANNING-TREE-MIB admin edge, by bridge port
186 'nn_cist_p_edge_admin' => 'nnMstCistPortAdminEdgeStatus',
187  
188 # NORTEL-NETWORKS-MULTIPLE-SPANNING-TREE-MIB oper edge, by bridge port
189 'nn_cist_p_edge_oper' => 'nnMstCistPortOperEdgeStatus',
190  
191 # NORTEL-NETWORKS-MULTIPLE-SPANNING-TREE-MIB, indexed <port>.<instance>
192 'nn_msti_p_state' => 'nnMstPortState',
193 'nn_msti_p_role' => 'nnMstPortCurrentPortRole',
194 'nn_msti_p_bridge' => 'nnMstPortDesignatedBridge',
195 'nn_msti_p_port' => 'nnMstPortDesignatedPort',
196 'nn_msti_p_enable' => 'nnMstPortForcePortState',
197  
198 # NORTEL-NETWORKS-RAPID-SPANNING-TREE-MIB, indexed by bridge port
199 'nn_rst_p_state' => 'nnRstDot1wPortStateTransSmState',
200 'nn_rst_p_role' => 'nnRstDot1wPortRole',
201  
202 # NORTEL-NETWORKS-RAPID-SPANNING-TREE-MIB admin edge, by bridge port
203 'nn_rst_p_edge_admin' => 'nnRstDot1dStpPortAdminEdgePort',
204  
205 # NORTEL-NETWORKS-RAPID-SPANNING-TREE-MIB oper edge, by bridge port
206 'nn_rst_p_edge_oper' => 'nnRstDot1dStpPortOperEdgePort',
207  
208 # BRIDGE-MIB beside nnRst; stp_p_* prefer rcStgPortTable
209 'rc_dot1d_p_bridge' => 'dot1dStpPortDesignatedBridge',
210 'rc_dot1d_p_port' => 'dot1dStpPortDesignatedPort',
211 'rc_dot1d_p_enable' => 'dot1dStpPortEnable',
212 );
213  
214 %MUNGE = (
215 'rc_base_mac' => \&SNMP::Info::munge_mac,
216 'rc_vlan_mac' => \&SNMP::Info::munge_mac,
217 'rc_vlan_members' => \&SNMP::Info::munge_port_list,
218 'rc_vlan_no_join' => \&SNMP::Info::munge_port_list,
219 'rc_mlt_ports' => \&SNMP::Info::munge_port_list,
220 'rc_smlt_sys_id' => \&SNMP::Info::munge_mac,
221 'rc_stp_i_mac' => \&SNMP::Info::munge_mac,
222 'rc_stp_i_root' => \&SNMP::Info::munge_prio_mac,
223 'rc_stp_p_root' => \&SNMP::Info::munge_prio_mac,
224 'rc_stp_p_bridge' => \&SNMP::Info::munge_prio_mac,
225 'rc_stp_p_port' => \&SNMP::Info::munge_prio_port,
226 'nn_mst_brg_address' => \&SNMP::Info::munge_mac,
227 'nn_mst_cist_root' => \&SNMP::Info::munge_prio_mac,
228 'nn_msti_root' => \&SNMP::Info::munge_prio_mac,
229 'nn_cist_p_bridge' => \&SNMP::Info::munge_prio_mac,
230 'nn_msti_p_bridge' => \&SNMP::Info::munge_prio_mac,
231 'rc_dot1d_p_bridge' => \&SNMP::Info::munge_prio_mac,
232 'nn_cist_p_port' => \&SNMP::Info::munge_prio_port,
233 'nn_msti_p_port' => \&SNMP::Info::munge_prio_port,
234 'rc_dot1d_p_port' => \&SNMP::Info::munge_prio_port,
235 'nn_cist_p_role' => \&SNMP::Info::munge_dot1s_port_role,
236 'nn_msti_p_role' => \&SNMP::Info::munge_dot1s_port_role,
237 'nn_rst_p_role' => \&SNMP::Info::munge_dot1w_port_role,
238 );
239  
240 # Need to override here since overridden in Layer2 and Layer3 classes
241 sub serial {
242 my $rapidcity = shift;
243  
244 my $ver = $rapidcity->rc_serial();
245 return $ver unless !defined $ver;
246  
247 return;
248 }
249  
250 sub i_duplex {
251 my $rapidcity = shift;
252 my $partial = shift;
253  
254 my $rc_duplex = $rapidcity->rc_duplex($partial) || {};
255  
256 my %i_duplex;
257 foreach my $if ( keys %$rc_duplex ) {
258 my $duplex = $rc_duplex->{$if};
259 next unless defined $duplex;
260  
261 $duplex = 'half' if $duplex =~ /half/i;
262 $duplex = 'full' if $duplex =~ /full/i;
263 $i_duplex{$if} = $duplex;
264 }
265  
266 return \%i_duplex;
267 }
268  
269 sub i_duplex_admin {
270 my $rapidcity = shift;
271 my $partial = shift;
272  
273 my $rc_duplex_admin = $rapidcity->rc_duplex_admin() || {};
274 my $rc_auto = $rapidcity->rc_auto($partial) || {};
275  
276 my %i_duplex_admin;
277 foreach my $if ( keys %$rc_duplex_admin ) {
278 my $duplex = $rc_duplex_admin->{$if};
279 next unless defined $duplex;
280 my $auto = $rc_auto->{$if} || 'false';
281  
282 my $string = 'other';
283 $string = 'half' if ( $duplex =~ /half/i and $auto =~ /false/i );
284 $string = 'full' if ( $duplex =~ /full/i and $auto =~ /false/i );
285 $string = 'auto' if $auto =~ /true/i;
286  
287 $i_duplex_admin{$if} = $string;
288 }
289  
290 return \%i_duplex_admin;
291 }
292  
293 sub set_i_duplex_admin {
294 my $rapidcity = shift;
295 my ( $duplex, $iid ) = @_;
296  
297 $duplex = lc($duplex);
298 return unless ( $duplex =~ /^(half|full|auto)$/ and $iid =~ /\d+/ );
299  
300 if ( $duplex eq "auto" ) {
301 return $rapidcity->set_rc_auto( '1', $iid );
302 }
303  
304 # map a textual duplex to an integer one the switch understands
305 my %duplexes = qw/full 2 half 1/;
306  
307 return unless ( $rapidcity->_disable_auto_negotiation($iid) );
308 return $rapidcity->set_rc_duplex_admin( $duplexes{$duplex}, $iid );
309 }
310  
311 sub set_i_speed_admin {
312 my $rapidcity = shift;
313 my ( $speed, $iid ) = @_;
314  
315 $speed = lc($speed);
316  
317 # map a speed in Mbps to the rcPortAdminSpeed value the switch understands
318 my %speeds = qw/10 1 100 2 1000 3 10000 4 40000 5 100000 6 25000 7/;
319 return unless ( ( $speed eq "auto" or exists $speeds{$speed} )
320 and $iid =~ /\d+/ );
321  
322 if ( $speed eq "auto" ) {
323 return $rapidcity->set_rc_auto( '1', $iid );
324 }
325  
326 return unless ( $rapidcity->_disable_auto_negotiation($iid) );
327 return $rapidcity->set_rc_speed_admin( $speeds{$speed}, $iid );
328 }
329  
330 # rcPortAutoNegotiate is a TruthValue, which UseEnums reads as true/false
331 sub _disable_auto_negotiation {
332 my $rapidcity = shift;
333 my $iid = shift;
334  
335 my $i_auto = $rapidcity->rc_auto($iid) || {};
336 my $state = $i_auto->{$iid};
337 return 1 unless ( defined $state and $state =~ /^(?:true|1)$/i );
338  
339 return $rapidcity->set_rc_auto( '2', $iid );
340 }
341  
342 sub v_index {
343 my $rapidcity = shift;
344 my $partial = shift;
345  
346 return $rapidcity->rc_vlan_id($partial);
347 }
348  
349 sub i_vlan {
350 my $rapidcity = shift;
351 my $partial = shift;
352  
353 my $i_pvid = $rapidcity->rc_i_vlan_pvid($partial) || {};
354  
355 return $i_pvid;
356 }
357  
358 sub i_vlan_membership {
359 my $rapidcity = shift;
360  
361 my $rc_v_ports = $rapidcity->rc_vlan_members();
362  
363 my $i_vlan_membership = {};
364 foreach my $vlan ( keys %$rc_v_ports ) {
365 my $portlist = $rc_v_ports->{$vlan};
366 my $ret = [];
367  
368 # Convert portlist bit array to ifIndex array
369 for ( my $i = 0; $i <= scalar(@$portlist); $i++ ) {
370 push( @{$ret}, $i ) if ( @$portlist[$i] );
371 }
372  
373 #Create HoA ifIndex -> VLAN array
374 foreach my $port ( @{$ret} ) {
375 push( @{ $i_vlan_membership->{$port} }, $vlan );
376 }
377 }
378 return $i_vlan_membership;
379 }
380  
381 sub i_vlan_membership_untagged {
382 my $rapidcity = shift;
383  
384 # Traditionally access ports have one VLAN untagged and trunk ports have
385 # one or more VLANs all tagged
386 # Newer VOSS device trunks have PerformTagging true or false and also can
387 # UntagDefaultVlan
388 # Newer BOSS device trunks have four PerformTagging options true, false,
389 # tagPvidOnly, and untagPvidOnly
390  
391 my $p_members = $rapidcity->i_vlan_membership();
392 my $i_vlan = $rapidcity->i_vlan();
393 my $p_tagging = $rapidcity->_rc_i_vlan_tagging();
394  
395 my $members_untagged = {};
396 foreach my $port ( keys %$p_tagging ) {
397 my $tagging = $p_tagging->{$port};
398 my $pvid = $i_vlan->{$port} // '';
399 my @vlans = @{ $p_members->{$port} || [] };
400  
401 if ( $tagging eq 'untagAll' ) {
402 $members_untagged->{$port} = $p_members->{$port};
403 }
404 elsif ( $tagging eq 'untagPvidOnly' ) {
405 # A PVID outside the port's VLANs carries no untagged egress
406 my @untagged = grep { $_ eq $pvid } @vlans;
407 $members_untagged->{$port} = \@untagged if @untagged;
408 }
409 elsif ( $tagging eq 'tagPvidOnly' ) {
410 $members_untagged->{$port} = [ grep { $_ ne $pvid } @vlans ];
411 }
412 }
413  
414 return $members_untagged;
415 }
416  
417 # Returns the tagging setting of each port: untagAll, tagAll, untagPvidOnly
418 # or tagPvidOnly. rcVlanPortType only says access or trunk, and some BOSS
419 # releases report access on every port whatever the PerformTagging setting,
420 # so PerformTagging decides wherever the agent serves it.
421 sub _rc_i_vlan_tagging {
422 my $rapidcity = shift;
423 my $partial = shift;
424  
425 my $p_type = $rapidcity->rcVlanPortType($partial) || {};
426 my $p_tag_opt = $rapidcity->rcVlanPortPerformTagging($partial) || {};
427 my $p_untag_def = $rapidcity->rcVlanPortUntagDefaultVlan($partial) || {};
428  
429 my %tagging;
430 foreach my $port ( keys %$p_type ) {
431 my $type = $p_type->{$port};
432 my $tag_opt = $p_tag_opt->{$port};
433 next unless $type;
434  
435 if ( !$tag_opt ) {
436 $tagging{$port} = $type eq 'access' ? 'untagAll' : 'tagAll';
437 }
438 elsif ( $tag_opt eq 'false' ) {
439 $tagging{$port} = 'untagAll';
440 }
441 elsif ( $tag_opt eq 'true' ) {
442 my $untag_def = $p_untag_def->{$port} || '';
443 $tagging{$port}
444 = $untag_def eq 'true' ? 'untagPvidOnly' : 'tagAll';
445 }
446 elsif ( $tag_opt =~ /^(?:tag|untag)PvidOnly$/ ) {
447 $tagging{$port} = $tag_opt;
448 }
449 }
450 return \%tagging;
451 }
452  
453 sub set_i_pvid {
454 my $rapidcity = shift;
455 my ( $vlan_id, $ifindex ) = @_;
456  
457 return unless ( $rapidcity->_validate_vlan_param( $vlan_id, $ifindex ) );
458  
459 # Without auto-PVID a PVID outside the port's VLANs is accepted and
460 # leaves the PVID and untagged membership out of step
461 my $members = $rapidcity->rc_vlan_members($vlan_id) || {};
462 unless ( $members->{$vlan_id} and $members->{$vlan_id}[$ifindex] ) {
463 $rapidcity->error_throw(
464 "IfIndex: $ifindex is not a member of VLAN: $vlan_id, so setting"
465 . " only its PVID would leave the PVID outside its VLANs."
466 . " Use set_i_vlan() to move the port to VLAN: $vlan_id" );
467 return;
468 }
469 return $rapidcity->_set_rc_i_vlan_pvid( $vlan_id, $ifindex );
470 }
471  
472 sub _set_rc_i_vlan_pvid {
473 my $rapidcity = shift;
474 my ( $vlan_id, $ifindex ) = @_;
475  
476 unless ( $rapidcity->set_rc_i_vlan_pvid( $vlan_id, $ifindex ) ) {
477 $rapidcity->error_throw(
478 "Unable to change PVID to $vlan_id on IfIndex: $ifindex");
479 return;
480 }
481 return 1;
482 }
483  
484 sub set_i_vlan {
485 my $rapidcity = shift;
486 my ( $new_vlan_id, $ifindex ) = @_;
487  
488 return
489 unless ( $rapidcity->_validate_vlan_param( $new_vlan_id, $ifindex ) );
490  
491 my $tagging
492 = $rapidcity->_rc_i_vlan_tagging($ifindex)->{$ifindex} || 'unknown';
493 unless ( $tagging eq 'untagAll' or $tagging eq 'untagPvidOnly' ) {
494 $rapidcity->error_throw(
495 "IfIndex: $ifindex sends its PVID tagged ($tagging), so it has"
496 . " no untagged VLAN to change. Use set_i_pvid() to change"
497 . " the PVID of a tagged port" );
498 return;
499 }
500  
501 my $i_pvid = $rapidcity->rc_i_vlan_pvid($ifindex);
502  
503 # Store current untagged VLAN to remove it from the port list later
504 my $old_vlan_id = $i_pvid->{$ifindex};
505  
506 # Check that haven't been given the same VLAN we are currently using
507 if ( $old_vlan_id eq $new_vlan_id ) {
508 $rapidcity->error_throw(
509 "Current PVID: $old_vlan_id and New VLAN: $new_vlan_id the same, no change."
510 );
511 return;
512 }
513  
514 print "Changing VLAN: $old_vlan_id to $new_vlan_id on IfIndex: $ifindex\n"
515 if $rapidcity->debug();
516  
517 # Check if port in forbidden list for the VLAN, haven't seen this used,
518 # but we'll check anyway
519 return
520 unless (
521 $rapidcity->_check_forbidden_ports( $new_vlan_id, $ifindex ) );
522  
523 my $old_vlan_members = $rapidcity->rc_vlan_members($old_vlan_id);
524 my $new_vlan_members = $rapidcity->rc_vlan_members($new_vlan_id);
525  
526 print "Modifying egress list for VLAN: $new_vlan_id \n"
527 if $rapidcity->debug();
528 my $new_egress
529 = $rapidcity->modify_port_list( $new_vlan_members->{$new_vlan_id},
530 $ifindex, '1' );
531  
532 print "Modifying egress list for VLAN: $old_vlan_id \n"
533 if $rapidcity->debug();
534 my $old_egress
535 = $rapidcity->modify_port_list( $old_vlan_members->{$old_vlan_id},
536 $ifindex, '0' );
537  
538 my $vlan_set = [
539 [ 'rc_vlan_members', "$new_vlan_id", "$new_egress" ],
540  
541 # ['rc_vlan_members',"$old_vlan_id","$old_egress"],
542 ];
543  
544 return
545 unless ( $rapidcity->set_multi($vlan_set) );
546  
547 my $vlan_set2 = [ [ 'rc_vlan_members', "$old_vlan_id", "$old_egress" ], ];
548  
549 return
550 unless ( $rapidcity->set_multi($vlan_set2) );
551  
552 # Set new untagged / native VLAN
553 # Some models/versions do this for us also, so check to see if we need to set
554 $i_pvid = $rapidcity->rc_i_vlan_pvid($ifindex);
555  
556 my $cur_i_pvid = $i_pvid->{$ifindex};
557 print "Current PVID: $cur_i_pvid\n" if $rapidcity->debug();
558 unless ( $cur_i_pvid eq $new_vlan_id ) {
559 return
560 unless $rapidcity->_set_rc_i_vlan_pvid( $new_vlan_id, $ifindex );
561 }
562  
563 print
564 "Successfully changed VLAN: $old_vlan_id to $new_vlan_id on IfIndex: $ifindex\n"
565 if $rapidcity->debug();
566 return 1;
567 }
568  
569 sub set_add_i_vlan_tagged {
570 my $rapidcity = shift;
571 my ( $vlan_id, $ifindex ) = @_;
572  
573 return unless ( $rapidcity->_validate_vlan_param( $vlan_id, $ifindex ) );
574  
575 print "Adding VLAN: $vlan_id to IfIndex: $ifindex\n"
576 if $rapidcity->debug();
577  
578 # Check if port in forbidden list for the VLAN, haven't seen this used, but we'll check anyway
579 return
580 unless ( $rapidcity->_check_forbidden_ports( $vlan_id, $ifindex ) );
581  
582 my $iv_members = $rapidcity->rc_vlan_members($vlan_id);
583  
584 print "Modifying egress list for VLAN: $vlan_id \n"
585 if $rapidcity->debug();
586 my $new_egress
587 = $rapidcity->modify_port_list( $iv_members->{$vlan_id}, $ifindex,
588 '1' );
589  
590 unless ( $rapidcity->set_qb_v_egress( $new_egress, $vlan_id ) ) {
591 print
592 "Error: Unable to add VLAN: $vlan_id to Index: $ifindex egress list.\n"
593 if $rapidcity->debug();
594 return;
595 }
596  
597 print
598 "Successfully added IfIndex: $ifindex to VLAN: $vlan_id egress list\n"
599 if $rapidcity->debug();
600 return 1;
601 }
602  
603 sub set_remove_i_vlan_tagged {
604 my $rapidcity = shift;
605 my ( $vlan_id, $ifindex ) = @_;
606  
607 return unless ( $rapidcity->_validate_vlan_param( $vlan_id, $ifindex ) );
608  
609 print "Removing VLAN: $vlan_id from IfIndex: $ifindex\n"
610 if $rapidcity->debug();
611  
612 my $iv_members = $rapidcity->rc_vlan_members($vlan_id);
613  
614 print "Modifying egress list for VLAN: $vlan_id \n"
615 if $rapidcity->debug();
616 my $new_egress
617 = $rapidcity->modify_port_list( $iv_members->{$vlan_id}, $ifindex,
618 '0' );
619  
620 unless ( $rapidcity->set_qb_v_egress( $new_egress, $vlan_id ) ) {
621 print
622 "Error: Unable to add VLAN: $vlan_id to Index: $ifindex egress list.\n"
623 if $rapidcity->debug();
624 return;
625 }
626  
627 print
628 "Successfully removed IfIndex: $ifindex from VLAN: $vlan_id egress list\n"
629 if $rapidcity->debug();
630 return 1;
631 }
632  
633 sub set_create_vlan {
634 my $rapidcity = shift;
635 my ( $name, $vlan_id ) = @_;
636 return unless ( $vlan_id =~ /\d+/ );
637  
638 my $vlan_set = [
639 [ 'v_name', "$vlan_id", "$name" ],
640 [ 'rc_vlan_rstatus', "$vlan_id", 4 ],
641 ];
642  
643 unless ( $rapidcity->set_multi($vlan_set) ) {
644 print "Error: Unable to create VLAN: $vlan_id\n"
645 if $rapidcity->debug();
646 return;
647 }
648  
649 return 1;
650 }
651  
652 sub set_delete_vlan {
653 my $rapidcity = shift;
654 my ($vlan_id) = shift;
655 return unless ( $vlan_id =~ /^\d+$/ );
656  
657 unless ( $rapidcity->set_rc_vlan_rstatus( '6', $vlan_id ) ) {
658 $rapidcity->error_throw("Unable to delete VLAN: $vlan_id");
659 return;
660 }
661 return 1;
662 }
663  
664 #
665 # These are internal methods and are not documented. Do not use directly.
666 #
667 sub _check_forbidden_ports {
668 my $rapidcity = shift;
669 my ( $vlan_id, $ifindex ) = @_;
670  
671 my $iv_forbidden = $rapidcity->rc_vlan_no_join($vlan_id);
672  
673 my @forbidden_ports
674 = split( //, unpack( "B*", $iv_forbidden->{$vlan_id} ) );
675 print "Forbidden ports: @forbidden_ports\n" if $rapidcity->debug();
676 if ( defined( $forbidden_ports[$ifindex] )
677 and ( $forbidden_ports[$ifindex] eq "1" ) )
678 {
679 $rapidcity->error_throw(
680 "IfIndex: $ifindex in forbidden list for VLAN: $vlan_id unable to add"
681 );
682 return;
683 }
684 return 1;
685 }
686  
687 sub _validate_vlan_param {
688 my $rapidcity = shift;
689 my ( $vlan_id, $ifindex ) = @_;
690  
691 # VID and ifIndex should both be numeric
692 unless (defined $vlan_id
693 and defined $ifindex
694 and $vlan_id =~ /^\d+$/
695 and $ifindex =~ /^\d+$/ )
696 {
697 $rapidcity->error_throw("Invalid parameter");
698 return;
699 }
700  
701 # Check that ifIndex exists on device
702 my $index = $rapidcity->interfaces($ifindex);
703  
704 unless ( exists $index->{$ifindex} ) {
705 $rapidcity->error_throw("ifIndex $ifindex does not exist");
706 return;
707 }
708  
709 #Check that VLAN exists on device
710 unless ( $rapidcity->rc_vlan_id($vlan_id) ) {
711 $rapidcity->error_throw(
712 "VLAN $vlan_id does not exist or is not operational");
713 return;
714 }
715  
716 return 1;
717 }
718  
719 sub agg_ports {
720 my $rapidcity = shift;
721  
722 # TODO: implement partial
723 my $ports = $rapidcity->rc_mlt_ports;
724 my $trunks = $rapidcity->_rc_mlt_keys();
725  
726 return {}
727 unless scalar keys %$trunks
728 and ref {} eq ref $ports
729 and scalar keys %$ports;
730  
731 my $ret = {};
732 foreach my $m ( sort { $a <=> $b } keys %$trunks ) {
733 my $idx = $trunks->{$m};
734 my $portlist = $ports->{$m};
735 next unless $portlist;
736 for ( my $i = 0; $i <= scalar(@$portlist); $i++ ) {
737 next if exists $ret->{$i};
738 $ret->{$i} = $idx if ( @$portlist[$i] );
739 }
740 }
741  
742 return $ret;
743 }
744  
745 # MLT id => the key agg_ports gives it: rcMltDesignatedPort when that is a
746 # configured member, else rcMltIfIndex. The designated port reads 536870912
747 # once every member is down.
748 sub _rc_mlt_keys {
749 my $rapidcity = shift;
750  
751 my $mlts = $rapidcity->rc_mlt_index() || {};
752 my $dps = $rapidcity->rc_mlt_dp() || {};
753 my $members = $rapidcity->rc_mlt_ports() || {};
754 my %keys;
755 foreach my $mlt_id ( keys %$mlts ) {
756 my $ifindex = $mlts->{$mlt_id} or next;
757 my $dp = $dps->{$mlt_id};
758 my $bits = $members->{$mlt_id} || [];
759 $keys{$mlt_id} = ( $dp and $bits->[$dp] ) ? $dp : $ifindex;
760 }
761 return \%keys;
762 }
763  
764 sub _agg_ports_walked { return qw/rc_mlt_ports rc_mlt_index rc_mlt_dp/ }
765  
766 sub _lag_vendor_walked {
767 return qw/rc_mlt_index rc_mlt_dp rc_mlt_type rc_mlt_run_type/;
768 }
769  
770 # The standard table holds a row on the MLT's rcMltIfIndex and, on VOSS, a
771 # row on each member port; agg_ports keys the MLT by its designated port
772 sub _lag_aggregator_map {
773 my $rapidcity = shift;
774  
775 my $map = SNMP::Info::IEEE802dot3ad::_lag_aggregator_map($rapidcity);
776 my $channels = $rapidcity->rc_mlt_index() || {};
777 my $keys = $rapidcity->_rc_mlt_keys();
778 foreach my $mlt_id ( keys %$keys ) {
779 $map->{ $channels->{$mlt_id} } = $keys->{$mlt_id};
780 }
781 return $map;
782 }
783  
784 # The legacy mixin adds the IST of the 8600 and ERS families
785 sub _rc_peer_link_flavors {
786 return ( vist => [ 'rc_vist', 'rcVirtualIst' ] );
787 }
788  
789 # A domain is served when its peer address is set; the scalars exist, with
790 # a 0.0.0.0 peer, on a device that does not use the feature
791 sub _lag_vendor_domains {
792 my $rapidcity = shift;
793  
794 my %flavors = $rapidcity->_rc_peer_link_flavors();
795 my $system_mac = $rapidcity->rc_smlt_sys_id();
796 my $domains = {};
797 foreach my $kind ( keys %flavors ) {
798 my ( $prefix, $leaf_prefix ) = @{ $flavors{$kind} };
799 my $peer = $rapidcity->${\"${prefix}_peer"}();
800 next unless defined $peer and $peer ne '0.0.0.0';
801 my $domain = { kind => $kind, peer => $peer, sources => {} };
802 $domain->{sources}{peer} = "RAPID-CITY::${leaf_prefix}PeerIp";
803 foreach my $field (qw/vlan status/) {
804 my $value = $rapidcity->${\"${prefix}_$field"}();
805 next unless defined $value;
806 $domain->{$field} = $value;
807 $domain->{sources}{$field}
808 = "RAPID-CITY::${leaf_prefix}"
809 . ( $field eq 'vlan' ? 'VlanId' : 'SessionStatus' );
810 }
811 if ( defined $system_mac ) {
812 $domain->{system_mac} = $system_mac;
813 $domain->{sources}{system_mac}
814 = 'RAPID-CITY::rcLacpGlobalSmltSysId';
815 }
816 $domains->{$kind} = $domain;
817 }
818 return $domains;
819 }
820  
821 sub _lag_vendor_groups {
822 my ( $rapidcity, $groups ) = @_;
823  
824 my $keys = $rapidcity->_rc_mlt_keys();
825 my $types = $rapidcity->rc_mlt_type() || {};
826 my $running = $rapidcity->rc_mlt_run_type() || {};
827 my $domain = $rapidcity->_smlt_domain_key();
828 foreach my $mlt_id ( keys %$keys ) {
829 my $ifindex = $keys->{$mlt_id};
830 my $type = $types->{$mlt_id} or next;
831 if ( $type eq 'splitMLT' and defined $domain ) {
832 my $record = _mlt_group_record( $groups, $ifindex );
833 $record->{mc} = {
834 kind => 'smlt',
835 group => $mlt_id,
836 domain => $domain,
837 state => $running->{$mlt_id},
838 };
839 $record->{sources}{mc} = 'RAPID-CITY::rcMltMltType';
840 }
841 elsif ( $type eq 'istMLT' or $type eq 'virtualIstMLT' ) {
842 my $record = _mlt_group_record( $groups, $ifindex );
843 $record->{peer_link} = 1;
844 $record->{sources}{peer_link} = 'RAPID-CITY::rcMltMltType';
845 }
846 }
847 return;
848 }
849  
850 # Split MLTs pair across the virtual IST when it is served, else the IST
851 sub _smlt_domain_key {
852 my $rapidcity = shift;
853  
854 my $domains = $rapidcity->_lag_vendor_domains();
855 return 'vist' if $domains->{vist};
856 return 'ist' if $domains->{ist};
857 return;
858 }
859  
860 # A channel the standard does not list still gets a record
861 sub _mlt_group_record {
862 my ( $groups, $ifindex ) = @_;
863  
864 return $groups->{$ifindex}
865 ||= SNMP::Info::IEEE802dot3ad::_lag_new_record();
866 }
867  
868 sub _l2sb_vendor_walked {
869 return qw/rc_vlan_isid rc_isid_if_vlan rc_isid_if_cvid rc_isid_if_type
870 rc_mlt_index rc_mlt_ports/;
871 }
872  
873 sub _l2sb_vendor_rows {
874 my $rapidcity = shift;
875 return ( $rapidcity->_rc_vlan_isid_rows(),
876 $rapidcity->_rc_isid_port_rows() );
877 }
878  
879 # An I-SID of 0 means the VLAN has none assigned
880 sub _rc_vlan_isid_rows {
881 my $rapidcity = shift;
882  
883 my $isids = $rapidcity->rc_vlan_isid() || {};
884 return map { +{ vlan => $_, type => q{isid}, id => $isids->{$_} } }
885 grep { $isids->{$_} } keys %$isids;
886 }
887  
888 # rcIsidInterfaceTable is indexed by ifIndex and I-SID. A VLAN-based
889 # binding repeats the VLAN's own rcVlanPlsbIsid row and is left out. A
890 # switched UNI has no platform VLAN, only the customer VLAN on the port.
891 sub _rc_isid_port_rows {
892 my $rapidcity = shift;
893  
894 my $types = $rapidcity->rc_isid_if_type() || {};
895 return unless %$types;
896 my $vlans = $rapidcity->rc_isid_if_vlan() || {};
897 my $cvids = $rapidcity->rc_isid_if_cvid() || {};
898 my $vlan_isids = $rapidcity->rc_vlan_isid() || {};
899 my $mlt_members = $rapidcity->_rc_mlt_member_ports();
900  
901 my @rows;
902 foreach my $iid ( keys %$types ) {
903 my ( $ifindex, $isid ) = split /\./, $iid;
904 my $vlan = $vlans->{$iid} || _rc_customer_vlan( $cvids->{$iid} );
905 next if $vlan and ( $vlan_isids->{$vlan} || 0 ) == $isid;
906 my $ports = $mlt_members->{$ifindex} || [$ifindex];
907 push @rows, map {
908 +{ vlan => $vlan || undef,
909 type => q{isid},
910 id => $isid,
911 ifindex => $_,
912 }
913 } @$ports;
914 }
915 return @rows;
916 }
917  
918 # 4096 is the untagged customer VID
919 sub _rc_customer_vlan {
920 my $cvid = shift;
921 return unless defined $cvid and $cvid >= 1 and $cvid <= 4094;
922 return $cvid;
923 }
924  
925 # An MLT's own ifIndex is not in ifTable. Bind its services to each
926 # configured member: the designated port moves with link state, membership
927 # does not. rcMltPortMembers bit N is ifIndex N.
928 sub _rc_mlt_member_ports {
929 my $rapidcity = shift;
930  
931 my $mlts = $rapidcity->rc_mlt_index() || {};
932 my $members = $rapidcity->rc_mlt_ports() || {};
933 my $interfaces = $rapidcity->interfaces() || {};
934 my %ports;
935 foreach my $mlt_id ( keys %$mlts ) {
936 my $bits = $members->{$mlt_id} or next;
937 my @ports = grep { $bits->[$_] and exists $interfaces->{$_} }
938 0 .. $#$bits;
939 $ports{ $mlts->{$mlt_id} } = \@ports if @ports;
940 }
941 return \%ports;
942 }
943  
944 sub stp_ver {
945 my $rapidcity = shift;
946  
947 return $rapidcity->rcSysSpanningTreeOperMode()
948 || $rapidcity->SUPER::stp_ver();
949 }
950  
951 # RSTP and ieee8021d operating modes do not populate RAPID-CITY::rcStgTable
952 # or RAPID-CITY::rcStgPortTable but do populate BRIDGE-MIB so check both
953 sub stp_i_mac {
954 my $rapidcity = shift;
955  
956 return $rapidcity->rc_stp_i_mac()
957 || $rapidcity->SUPER::stp_i_mac();
958 }
959  
960 sub stp_i_time {
961 my $rapidcity = shift;
962  
963 return $rapidcity->rc_stp_i_time()
964 || $rapidcity->SUPER::stp_i_time();
965 }
966  
967 sub stp_i_ntop {
968 my $rapidcity = shift;
969  
970 return $rapidcity->rc_stp_i_ntop()
971 || $rapidcity->SUPER::stp_i_ntop();
972 }
973  
974 sub stp_i_root {
975 my $rapidcity = shift;
976  
977 return $rapidcity->rc_stp_i_root()
978 || $rapidcity->SUPER::stp_i_root();
979 }
980  
981 sub stp_i_root_port {
982 my $rapidcity = shift;
983  
984 return $rapidcity->rc_stp_i_root_port()
985 || $rapidcity->SUPER::stp_i_root_port();
986 }
987  
988 sub stp_i_priority {
989 my $rapidcity = shift;
990  
991 return $rapidcity->rc_stp_i_priority()
992 || $rapidcity->SUPER::stp_i_priority();
993 }
994  
995 sub stp_p_id {
996 my $rapidcity = shift;
997  
998 return $rapidcity->rc_stp_p_id()
999 || $rapidcity->SUPER::stp_p_id();
1000 }
1001  
1002 sub stp_p_priority {
1003 my $rapidcity = shift;
1004  
1005 return $rapidcity->rc_stp_p_priority()
1006 || $rapidcity->SUPER::stp_p_priority();
1007 }
1008  
1009 sub stp_p_state {
1010 my $rapidcity = shift;
1011  
1012 return $rapidcity->rc_stp_p_state()
1013 || $rapidcity->SUPER::stp_p_state();
1014 }
1015  
1016 sub stp_p_cost {
1017 my $rapidcity = shift;
1018  
1019 return $rapidcity->rc_stp_p_cost()
1020 || $rapidcity->SUPER::stp_p_cost();
1021 }
1022  
1023 sub stp_p_root {
1024 my $rapidcity = shift;
1025  
1026 return $rapidcity->rc_stp_p_root()
1027 || $rapidcity->SUPER::stp_p_root();
1028 }
1029  
1030 sub stp_p_bridge {
1031 my $rapidcity = shift;
1032  
1033 return $rapidcity->rc_stp_p_bridge()
1034 || $rapidcity->SUPER::stp_p_bridge();
1035 }
1036  
1037 sub stp_p_port {
1038 my $rapidcity = shift;
1039  
1040 return $rapidcity->rc_stp_p_port()
1041 || $rapidcity->SUPER::stp_p_port();
1042 }
1043  
1044 sub mst_vlan2instance {
1045 my $rapidcity = shift;
1046 my $partial = shift;
1047  
1048 return $rapidcity->rcVlanStgId($partial);
1049 }
1050  
1051 sub i_bpduguard_enabled {
1052 my $rapidcity = shift;
1053 my $partial = shift;
1054  
1055 return $rapidcity->rcPortBpduFilteringOperEnabled();
1056 }
1057  
1058 sub i_edgeport_admin {
1059 my $rapidcity = shift;
1060 my $partial = shift;
1061  
1062 # The role's @ISA is SNMP::Info, so SUPER:: would not reach Bridge
1063 my $edge_admin = $rapidcity->_rc_edge('admin');
1064 return SNMP::Info::Bridge::i_edgeport_admin( $rapidcity, $partial )
1065 unless $edge_admin;
1066 return SNMP::Info::Bridge::_stp_edge_by_ifindex( $rapidcity,
1067 $edge_admin, $partial );
1068 }
1069  
1070 sub i_edgeport_oper {
1071 my $rapidcity = shift;
1072 my $partial = shift;
1073  
1074 my $edge_oper = $rapidcity->_rc_edge('oper');
1075 return SNMP::Info::Bridge::i_edgeport_oper( $rapidcity, $partial )
1076 unless $edge_oper;
1077 return SNMP::Info::Bridge::_stp_edge_by_ifindex( $rapidcity,
1078 $edge_oper, $partial );
1079 }
1080  
1081 # Admin or oper ($kind) edge by bridge port from the running protocol's
1082 # table: nnMst CIST under MSTP, nnRst under RSTP. Under MSTP the nnMst rows
1083 # are the per-port truth even where nnRst lists more ports.
1084 sub _rc_edge {
1085 my $rapidcity = shift;
1086 my $kind = shift;
1087  
1088 my %table_for = (
1089 mstp => "nn_cist_p_edge_$kind",
1090 rstp => "nn_rst_p_edge_$kind",
1091 );
1092 my $method = $table_for{ $rapidcity->_rc_stp_protocol() || '' }
1093 or return;
1094 my $edge = $rapidcity->$method() || {};
1095 return keys %$edge ? $edge : undef;
1096 }
1097  
1098 sub i_stp_state {
1099 my $rapidcity = shift;
1100 my $partial = shift;
1101  
1102 my $bp_index = $rapidcity->bp_index($partial);
1103 my $stp_p_state = $rapidcity->dot1dStpPortState($partial);
1104  
1105 my %i_stp_state;
1106  
1107 foreach my $index ( keys %$stp_p_state ) {
1108 my $state = $stp_p_state->{$index};
1109 my $iid = $bp_index->{$index};
1110 next unless defined $iid;
1111 next unless defined $state;
1112 $i_stp_state{$iid} = $state;
1113 }
1114  
1115 return \%i_stp_state;
1116 }
1117  
1118 # Spanning tree protocol from nnMstForceProtocolVersion and
1119 # nnRstDot1dStpVersion, which mean the same across the family's generations
1120 # where rcStgProtocolSpecification does not.
1121 # rcSysSpanningTreeOperMode nortelStpg and pvst are not read: those modes
1122 # predate 802.1w and 802.1s, and few devices still run them.
1123 sub _rc_stp_protocol {
1124 my $rapidcity = shift;
1125  
1126 my $mst_version = $rapidcity->nn_mst_version();
1127 return 'mstp' if defined $mst_version and $mst_version =~ /^(?:mstp|3)$/;
1128  
1129 my $rst_version = $rapidcity->nn_rst_version();
1130 return unless defined $rst_version;
1131 return 'rstp' if $rst_version =~ /^(?:rstp|2)$/;
1132 return 'stp' if $rst_version =~ /^(?:stpCompatible|0)$/;
1133 return;
1134 }
1135  
1136 sub _rc_stp_measured { return 0; }
1137  
1138 sub hasSTP {
1139 my $rapidcity = shift;
1140 return ( $rapidcity->_rc_stp_protocol() || '' ) eq 'stp' ? 1 : 0;
1141 }
1142  
1143 sub hasRSTP {
1144 my $rapidcity = shift;
1145 return ( $rapidcity->_rc_stp_protocol() || '' ) eq 'rstp' ? 1 : 0;
1146 }
1147  
1148 sub hasMSTP {
1149 my $rapidcity = shift;
1150 return ( $rapidcity->_rc_stp_protocol() || '' ) eq 'mstp' ? 1 : 0;
1151 }
1152  
1153 sub stp_protocols {
1154 my $rapidcity = shift;
1155  
1156 my $protocol = $rapidcity->_rc_stp_protocol();
1157 return [$protocol] if defined $protocol;
1158 return;
1159 }
1160  
1161 # An STG with STP disabled, such as the SPBM B-VLAN tree, is no tree
1162 sub _rc_stg_running {
1163 my ( $rapidcity, $stg ) = @_;
1164  
1165 my $enable = ( $rapidcity->rc_stg_enable() || {} )->{$stg};
1166 return 1 unless defined $enable;
1167 return $enable =~ /^(?:false|2)$/ ? 0 : 1;
1168 }
1169  
1170 sub stp_trees {
1171 my $rapidcity = shift;
1172  
1173 return unless $rapidcity->_stp_default_context_ok();
1174 return $rapidcity->{_stp_trees} if $rapidcity->{_stp_trees};
1175  
1176 my $protocol = $rapidcity->_rc_stp_protocol();
1177 my $trees = {};
1178 if ( defined $protocol ) {
1179 $trees
1180 = $protocol eq 'mstp'
1181 ? $rapidcity->_rc_mstp_trees()
1182 : $rapidcity->_rc_rstp_trees($protocol);
1183 }
1184 $rapidcity->{_stp_trees} = $trees if keys %$trees;
1185 return $trees;
1186 }
1187  
1188 sub _rc_tree_key {
1189 my $instance = shift;
1190 return $instance ? "msti.$instance" : 'cist.0';
1191 }
1192  
1193 sub _rc_tree_record {
1194 my ( $rapidcity, $protocol, $instance, $fields ) = @_;
1195  
1196 return {
1197 type => $instance ? 'msti' : 'cist',
1198 id => $instance,
1199 protocol => $protocol,
1200 measured => $rapidcity->_rc_stp_measured($protocol),
1201 contexts => [ { kind => 'none' } ],
1202 %$fields,
1203 };
1204 }
1205  
1206 # Instances with a port row at a bridge port that bp_index does not map
1207 sub _rc_unmapped_instances {
1208 my $rapidcity = shift;
1209  
1210 my $bp_index = $rapidcity->bp_index() || {};
1211 my %unmapped;
1212 foreach my $port ( keys %{ $rapidcity->nn_cist_p_state() || {} } ) {
1213 $unmapped{0} = 1 unless defined $bp_index->{$port};
1214 }
1215 foreach my $row ( keys %{ $rapidcity->nn_msti_p_state() || {} } ) {
1216 my ( $port, $instance ) = split /\./, $row, 2;
1217 $unmapped{$instance} = 1 unless defined $bp_index->{$port};
1218 }
1219 return \%unmapped;
1220 }
1221  
1222 sub _rc_mstp_trees {
1223 my $rapidcity = shift;
1224  
1225 my $address = $rapidcity->nn_mst_brg_address();
1226 my $priorities = $rapidcity->nn_msti_priority() || {};
1227 my $roots = $rapidcity->nn_msti_root() || {};
1228 my $unmapped = $rapidcity->_rc_unmapped_instances();
1229  
1230 my %instances = ( 0 => [
1231 $rapidcity->nn_mst_cist_priority(), $rapidcity->nn_mst_cist_root()
1232 ] );
1233 foreach my $instance ( keys %$priorities ) {
1234 next unless $rapidcity->_rc_stg_running( $instance + 1 );
1235 $instances{$instance}
1236 = [ $priorities->{$instance}, $roots->{$instance} ];
1237 }
1238  
1239 my %trees;
1240 foreach my $instance ( keys %instances ) {
1241 my ( $priority, $root ) = @{ $instances{$instance} };
1242 my $bridge = SNMP::Info::Bridge::_stp_bridge_id_from( $priority,
1243 $address );
1244 my $root_id = SNMP::Info::Bridge::_stp_decode_bridge_id($root);
1245 $trees{ _rc_tree_key($instance) } = $rapidcity->_rc_tree_record(
1246 'mstp', $instance,
1247 { ports_complete => $unmapped->{$instance} ? 0 : 1,
1248 bridge => $bridge,
1249 root => $root_id,
1250 }
1251 );
1252 }
1253 return \%trees;
1254 }
1255  
1256 # VOSS serves no dot1dStp scalar under RSTP, so the bridge and root come from
1257 # the first STG's rcStg row
1258 sub _rc_rstp_trees {
1259 my ( $rapidcity, $protocol ) = @_;
1260  
1261 my $states = $rapidcity->nn_rst_p_state() || {};
1262 my $bp_index = $rapidcity->bp_index() || {};
1263 my $complete = keys %$states ? 1 : 0;
1264 $complete = 0 if grep { !defined $bp_index->{$_} } keys %$states;
1265 $complete = 0 if $rapidcity->_stp_walk_incomplete('nn_rst_p_state');
1266  
1267 my $priority = ( $rapidcity->rc_stp_i_priority() || {} )->{1};
1268 my $mac = ( $rapidcity->rc_stp_i_mac() || {} )->{1};
1269 my $root = ( $rapidcity->rc_stp_i_root() || {} )->{1};
1270 my $bridge = SNMP::Info::Bridge::_stp_bridge_id_from( $priority, $mac );
1271 my $root_id = SNMP::Info::Bridge::_stp_decode_bridge_id($root);
1272 return {
1273 'cist.0' => $rapidcity->_rc_tree_record(
1274 $protocol, 0,
1275 { ports_complete => $complete,
1276 bridge => $bridge,
1277 root => $root_id,
1278 }
1279 )
1280 };
1281 }
1282  
1283 sub stp_tree_ports {
1284 my ( $rapidcity, $tree_key ) = @_;
1285  
1286 my $tree = $rapidcity->_stp_tree_for_key($tree_key) or return;
1287 return $rapidcity->_rc_mstp_port_rows($tree)
1288 if ( $tree->{protocol} || '' ) eq 'mstp';
1289 return $rapidcity->_rc_rstp_port_rows($tree);
1290 }
1291  
1292 # Under MSTP the per-port enable is the nnMst ForcePortState of the tree, as
1293 # rcStgPortEnableStp is 1 on every port and dot1dStpPortEnable is inert there;
1294 # a port without a row is unknown, not a fallback to either. The enum has
1295 # disabled(0), which _stp_enabled would read as unknown.
1296 sub _rc_mstp_port_enabled {
1297 my ( $rapidcity, $port, $instance ) = @_;
1298  
1299 my $value;
1300 if ( $instance == 0 ) {
1301 $value = ( $rapidcity->nn_cist_p_enable() || {} )->{$port};
1302 }
1303 else {
1304 $value
1305 = ( $rapidcity->nn_msti_p_enable() || {} )->{"$port.$instance"};
1306 }
1307 return unless defined $value;
1308  
1309 return 0 if $value =~ /^(?:disabled|0)$/;
1310 return scalar SNMP::Info::Bridge::_stp_enabled($value);
1311 }
1312  
1313 # Under RSTP and STP, rcStgPortEnableStp is indexed <ifIndex>.<STG id>, and
1314 # a tree's STG is its instance plus one; the BRIDGE-MIB enable, indexed by
1315 # bridge port, is the fallback
1316 sub _rc_port_enabled {
1317 my ( $rapidcity, $tree, $port, $iid ) = @_;
1318  
1319 return $rapidcity->_rc_mstp_port_enabled( $port, $tree->{id} )
1320 if ( $tree->{protocol} || '' ) eq 'mstp';
1321  
1322 my $stg_row = $iid . '.' . ( $tree->{id} + 1 );
1323 my $stg_enable = ( $rapidcity->rc_stp_p_enable() || {} )->{$stg_row};
1324 $stg_enable = ( $rapidcity->rc_dot1d_p_enable() || {} )->{$port}
1325 unless defined $stg_enable;
1326 return scalar SNMP::Info::Bridge::_stp_enabled($stg_enable);
1327 }
1328  
1329 sub _rc_port_row {
1330 my ( $rapidcity, $tree, $port, $iid, $served, $key ) = @_;
1331  
1332 return {
1333 tree_type => $tree->{type},
1334 tree_id => $tree->{id},
1335 state => $served->{state}{$key},
1336 role => $served->{role}{$key},
1337 role_raw => $served->{role_raw}{$key},
1338 enabled =>
1339 scalar $rapidcity->_rc_port_enabled( $tree, $port, $iid ),
1340 designated_bridge =>
1341 scalar SNMP::Info::Bridge::_stp_decode_bridge_id(
1342 $served->{bridge}{$key} ),
1343 designated_port => scalar SNMP::Info::Bridge::_stp_decode_port_id(
1344 $served->{port}{$key} ),
1345 };
1346 }
1347  
1348 sub _rc_mstp_port_rows {
1349 my ( $rapidcity, $tree ) = @_;
1350  
1351 my $cist = $tree->{id} == 0;
1352 my $prefix = $cist ? 'nn_cist_p_' : 'nn_msti_p_';
1353 my %served;
1354 foreach my $column (qw/state role role_raw bridge port/) {
1355 my $method = $prefix . $column;
1356 $served{$column} = $rapidcity->$method() || {};
1357 }
1358 my $bp_index = $rapidcity->bp_index() || {};
1359  
1360 my %rows;
1361 foreach my $key ( keys %{ $served{state} } ) {
1362 my ( $port, $instance ) = split /\./, $key, 2;
1363 next if !$cist and $instance != $tree->{id};
1364  
1365 my $iid = $bp_index->{$port};
1366 next unless defined $iid;
1367 $rows{$iid}
1368 = $rapidcity->_rc_port_row( $tree, $port, $iid, \%served, $key );
1369 }
1370 return \%rows;
1371 }
1372  
1373 # The BRIDGE-MIB and rcStg port states serve forwarding on a blocked port, so
1374 # the state is nnRstDot1wPortStateTransSmState only
1375 sub _rc_rstp_port_rows {
1376 my ( $rapidcity, $tree ) = @_;
1377  
1378 my %served = (
1379 state => $rapidcity->nn_rst_p_state() || {},
1380 role => $rapidcity->nn_rst_p_role() || {},
1381 role_raw => $rapidcity->nn_rst_p_role_raw() || {},
1382 bridge => $rapidcity->rc_dot1d_p_bridge() || {},
1383 port => $rapidcity->rc_dot1d_p_port() || {},
1384 );
1385 my $bp_index = $rapidcity->bp_index() || {};
1386  
1387 my %rows;
1388 foreach my $port ( keys %{ $served{state} } ) {
1389 my $iid = $bp_index->{$port};
1390 next unless defined $iid;
1391 $rows{$iid}
1392 = $rapidcity->_rc_port_row( $tree, $port, $iid, \%served, $port );
1393 }
1394 return \%rows;
1395 }
1396  
1397 sub stp_trees_complete {
1398 my $rapidcity = shift;
1399  
1400 return 0 unless defined $rapidcity->_rc_stp_protocol();
1401 my $trees = $rapidcity->stp_trees() || {};
1402 return 0 if $rapidcity->_stp_walk_incomplete('nn_msti_priority');
1403 return keys %$trees ? 1 : 0;
1404 }
1405  
1406 sub stp_vlan_map {
1407 my $rapidcity = shift;
1408  
1409 return unless ( $rapidcity->_rc_stp_protocol() || '' ) eq 'mstp';
1410  
1411 my $vlan_stg = $rapidcity->rc_vlan_stg() || {};
1412 my %map;
1413 foreach my $vlan ( keys %$vlan_stg ) {
1414 my $stg = $vlan_stg->{$vlan};
1415 next unless $rapidcity->_rc_stg_running($stg);
1416 $map{$vlan} = _rc_tree_key( $stg - 1 );
1417 }
1418 return unless keys %map;
1419 return \%map;
1420 }
1421  
1422 # Default tree is not the common tree: VOSS serves forwarding on a blocked
1423 # port under RSTP
1424 sub _stp_default_is_common { return 0; }
1425  
1426 1;
1427  
1428 __END__