Info/RapidCity.pm untested

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