Info/Layer3/Juniper.pm untested

filter: Statementstmt 0.0% 0 / 687 Branchbran 0.0% 0 / 294 Conditioncond 0.0% 0 / 324 MC/DCmcdc 0.0% 0 / 180 Subroutinesub 0.0% 0 / 84 Podpod 0.0% 0 / 80 totaltotal 0.0% 0 / 1649 CC 148 SCAR 30.5
CC
148
Cov
0%
e_class
56.0
e_parent
56.0
stp_trees
56.0
CRAP 21.0
1 # SNMP::Info::Layer3::Juniper
2 #
3 # Copyright (c) 2008 Bill Fenner
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::Layer3::Juniper;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use SNMP::Info::Layer3;
36  
37 @SNMP::Info::Layer3::Juniper::ISA = qw/SNMP::Info::Layer3 Exporter/;
38 @SNMP::Info::Layer3::Juniper::EXPORT_OK = qw//;
39  
40 our ($VERSION, $DEBUG, %GLOBALS, %MIBS, %FUNCS, %MUNGE);
41  
42 $VERSION = '3.985003';
43  
44 %MIBS = (
45 %SNMP::Info::Layer3::MIBS,
46 'JUNIPER-CHASSIS-DEFINES-MIB' => 'jnxChassisDefines',
47 'JUNIPER-MIB' => 'jnxBoxAnatomy',
48 'JUNIPER-IF-MIB' => 'ifChassisLogicalUnit',
49 'JUNIPER-VIRTUALCHASSIS-MIB' => 'jnxVirtualChassisMemberTable',
50 'JUNIPER-VLAN-MIB' => 'jnxVlanMIBObjects',
51 'JUNIPER-L2ALD-MIB' => 'jnxL2aldVlanFdbId',
52 'JUNIPER-MIMSTP-MIB' => 'jnxMIMstSystemControl',
53 'SNMP-VIEW-BASED-ACM-MIB' => 'vacmContextName',
54 'JUNIPER-VPN-MIB' => 'jnxVpnName',
55 );
56  
57 %GLOBALS = (
58 %SNMP::Info::Layer3::GLOBALS,
59 'serial' => 'jnxBoxSerialNo',
60 'mac' => 'dot1dBaseBridgeAddress',
61 'box_descr' => 'jnxBoxDescr',
62 'version' => 'jnxVirtualChassisMemberSWVersion',
63 'vc_model' => 'jnxVirtualChassisMemberModel',
64  
65 # RSTP-MIB, which names only stpCompatible(0) and rstp(2)
66 'jnx_stp_version' => 'dot1dStpVersion',
67 );
68  
69 %FUNCS = (
70 %SNMP::Info::Layer3::FUNCS,
71  
72 # JUNIPER-VLAN-MIB::jnxExVlanPortGroupTable
73 'i_trunk' => 'jnxExVlanPortAccessMode',
74  
75 # JUNIPER-MIB
76 'e_contents_type' => 'jnxContentsType',
77 'e_containers_type' => 'jnxContainersType',
78 'e_hwver' => 'jnxContentsRevision',
79  
80 # JUNIPER-VLAN-MIB::jnxExVlanTable
81 'jnx_v_name' => 'jnxExVlanName',
82 'jnx_v_index' => 'jnxExVlanTag',
83 'jnx_v_type' => 'jnxExVlanType',
84  
85 # JUNIPER-L2ALD-MIB::jnxL2aldVlanTable
86 # Used in Enhanced Layer 2 Software (ELS)
87 # replaces JUNIPER-VLAN-MIB::jnxExVlanTable
88 # in newer switches
89 'jnx_els_v_name' => 'jnxL2aldVlanName',
90 'jnx_els_v_index' => 'jnxL2aldVlanTag',
91 'jnx_els_v_type' => 'jnxL2aldVlanType',
92 'jnx_els_v_fdb_id' => 'jnxL2aldVlanFdbId',
93  
94 # JUNIPER-IF-MIB::ifChassisTable
95 'i_logical_unit' => 'ifChassisLogicalUnit',
96  
97 # SNMP-VIEW-BASED-ACM-MIB::vacmContextTable, names the VSTP contexts
98 # and the routing instances; the index is the length-prefixed name
99 'jnx_vacm_context_name' => 'vacmContextName',
100  
101 # JUNIPER-VPN-MIB::jnxVpnTable lists each VPN by its index, jnxVpnType
102 # then the length-prefixed jnxVpnName; type 2 is bgpIpVpn. Both index
103 # leaves are not-accessible, so walk the first readable column.
104 'vrf_jnx_vpn_status' => 'jnxVpnRowStatus',
105  
106 # JUNIPER-MIMSTP-MIB, indexed by context
107 'jnx_mst_control' => 'jnxMIMstSystemControl',
108 'jnx_mst_brg_address' => 'jnxMIMstBrgAddress',
109 'jnx_mst_cist_root' => 'jnxMIMstCistRoot',
110 'jnx_mst_cist_priority' => 'jnxMIMstCistBridgePriority',
111  
112 # JUNIPER-MIMSTP-MIB, indexed <context>.<MSTI>
113 'jnx_msti_priority' => 'jnxMIMstMstiBridgePriority',
114 'jnx_msti_root' => 'jnxMIMstMstiBridgeRegionalRoot',
115 'jnx_msti_vlans_1k' => 'jnxMIMstInstanceVlanMapped',
116 'jnx_msti_vlans_2k' => 'jnxMIMstInstanceVlanMapped2k',
117 'jnx_msti_vlans_3k' => 'jnxMIMstInstanceVlanMapped3k',
118 'jnx_msti_vlans_4k' => 'jnxMIMstInstanceVlanMapped4k',
119  
120 # JUNIPER-MIMSTP-MIB, indexed by bridge port
121 'jnx_cist_p_state' => 'jnxMIMstCistPortState',
122 'jnx_cist_p_role' => 'jnxMIMstCistCurrentPortRole',
123 'jnx_cist_p_bridge' => 'jnxMIMstCistPortDesignatedBridge',
124 'jnx_cist_p_port' => 'jnxMIMstCistPortDesignatedPort',
125 'jnx_cist_p_enable' => 'jnxMIMstCistForcePortState',
126  
127 # JUNIPER-MIMSTP-MIB admin edge, indexed by bridge port
128 'jnx_cist_p_edge_admin' => 'jnxMIMstCistPortAdminEdgeStatus',
129  
130 # JUNIPER-MIMSTP-MIB oper edge, indexed by bridge port
131 'jnx_cist_p_edge_oper' => 'jnxMIMstCistPortOperEdgeStatus',
132  
133 # JUNIPER-MIMSTP-MIB, indexed <bridge port>.<MSTI>
134 'jnx_msti_p_state' => 'jnxMIMstMstiPortState',
135 'jnx_msti_p_role' => 'jnxMIMstMstiCurrentPortRole',
136 'jnx_msti_p_bridge' => 'jnxMIMstMstiPortDesignatedBridge',
137 'jnx_msti_p_port' => 'jnxMIMstMstiPortDesignatedPort',
138 'jnx_msti_p_enable' => 'jnxMIMstMstiForcePortState',
139 );
140  
141 %MUNGE = (
142 %SNMP::Info::Layer3::MUNGE,
143 'e_containers_type' => \&SNMP::Info::munge_e_type,
144 'e_contents_type' => \&SNMP::Info::munge_e_type,
145 'jnx_mst_brg_address' => \&SNMP::Info::munge_mac,
146 'jnx_mst_cist_root' => \&SNMP::Info::munge_prio_mac,
147 'jnx_msti_root' => \&SNMP::Info::munge_prio_mac,
148 'jnx_cist_p_bridge' => \&SNMP::Info::munge_prio_mac,
149 'jnx_msti_p_bridge' => \&SNMP::Info::munge_prio_mac,
150 'jnx_cist_p_port' => \&SNMP::Info::munge_prio_port,
151 'jnx_msti_p_port' => \&SNMP::Info::munge_prio_port,
152 'jnx_cist_p_role' => \&SNMP::Info::munge_dot1s_port_role,
153 'jnx_msti_p_role' => \&SNMP::Info::munge_dot1s_port_role,
154 );
155  
156 sub vendor {
157 return 'juniper';
158 }
159  
160 sub os {
161 return 'junos';
162 }
163  
164 sub layers {
165 my $juniper = shift;
166  
167 my $layers = $juniper->SUPER::layers();
168 # Some models don't report L2 properly
169 my $macs = $juniper->fw_mac();
170  
171 if (keys %$macs) {
172 my $l = substr $layers, 6, 1, "1";
173 }
174  
175 return $layers;
176 }
177  
178 sub os_ver {
179 my $juniper = shift;
180  
181 my $sys_descr = $juniper->description() || '';
182 my $lldp_descr = $juniper->lldp_sysdesc() || '';
183  
184 my $ver = $juniper->version() || '';
185 if (not $ver eq '') {
186 return $ver;
187 }
188 foreach my $descr ($sys_descr, $lldp_descr) {
189 if ( $descr =~ m/kernel JUNOS ([^,\s]+)/ ) {
190 return $1;
191 }
192 elsif ( $descr =~ m/version\s(\S+)\s/ ) {
193 return $1;
194 }
195 }
196 return;
197 }
198  
199 sub model {
200 my $l3 = shift;
201 my $id = $l3->id();
202 # Query the junos device model.
203 my $mod = $l3->vc_model() || '';
204  
205 if (not $mod eq '') {
206 return uc $mod;
207 }
208 # Fallback to old method
209 unless ( defined $id ) {
210 print
211 " SNMP::Info::Layer3::Juniper::model() - Device does not support sysObjectID\n"
212 if $l3->debug();
213 return;
214 }
215  
216 my $model = &SNMP::translateObj($id);
217  
218 return $id unless defined $model;
219  
220 $model =~ s/^jnxProductName//i;
221 return $model;
222 }
223  
224 # Override the fancy Layer3.pm serial function
225 sub serial {
226 my $juniper = shift;
227 return $juniper->orig_serial();
228 }
229  
230 # 'i_trunk' => 'jnxExVlanPortAccessMode',
231 sub i_trunk {
232 my $juniper = shift;
233 my $partial = shift;
234  
235 my $access = $juniper->jnxExVlanPortAccessMode($partial);
236  
237 my %i_trunk;
238  
239 foreach (keys %$access)
240 {
241 my $old_key = $_;
242 my $new_key = $old_key;
243 $new_key = $1 if m/^\d+\.(\d+)$/;
244 $i_trunk{$new_key} = $access->{$old_key};
245 }
246  
247 return \%i_trunk;
248 }
249  
250 # Note: For overridden VLAN methods where we have to distinguish between newer
251 # ELS and older models we try the newer methods first assuming they will be
252 # more common to save on network queries. Short circuit operation depends on
253 # leaf not present and snmp query returning undef
254 sub qb_fdb_index {
255 my $juniper = shift;
256 my $partial = shift;
257  
258 # Lots of indirection here
259 my $v_index = $juniper->jnx_els_v_index($partial);
260  
261 if ( ref {} eq ref $v_index and scalar keys %$v_index ) {
262 my $fdb_index = $juniper->jnx_els_v_fdb_id($partial);
263 my $qb_fdb_index = {};
264 for my $key ( keys(%$v_index) ) {
265 my $vlan = $v_index->{$key};
266 next unless defined $vlan;
267 my $fdb = $fdb_index->{$key};
268 next unless defined $fdb;
269 $qb_fdb_index->{$fdb} = $vlan;
270 }
271 return $qb_fdb_index;
272  
273 }
274  
275 $juniper->jnx_v_index($partial);
276 }
277  
278  
279 sub v_type {
280 my $juniper = shift;
281 my $partial = shift;
282  
283 return $juniper->jnx_els_v_type($partial)
284 || $juniper->jnx_v_type($partial);
285 }
286  
287 sub v_index {
288 my ($juniper) = shift;
289 my ($partial) = shift;
290  
291 return $juniper->jnx_els_v_index($partial)
292 || $juniper->jnx_v_index($partial);
293 }
294  
295  
296 sub i_vlan {
297 my $juniper = shift;
298 my $partial = shift;
299  
300 my $index = $juniper->bp_index() || {};
301 my $types = $juniper->i_type() || {};
302  
303 # If given a partial it will be an ifIndex, we need to use dot1dBasePort
304 my $if_partial = $partial;
305 if ($partial) {
306 my %r_index = reverse %$index;
307 $partial = $r_index{$partial};
308 }
309  
310 my $old_index = $juniper->jnx_v_index() || {};
311 my $untagged = scalar keys %$old_index
312 ? $juniper->i_vlan_membership_untagged($if_partial) || {}
313 : {};
314 my $i_pvid = $juniper->qb_i_vlan($partial) || {};
315 my $i_vlan = $juniper->SUPER::i_vlan() || {};
316  
317 foreach my $bport ( keys %$i_pvid ) {
318 my $ifindex = $index->{$bport};
319 unless ( defined $ifindex ) {
320 print " Port $bport has no bp_index mapping. Skipping.\n"
321 if $DEBUG;
322 next;
323 }
324 $i_vlan->{$ifindex} = $juniper->_pvid_vlan( $i_pvid->{$bport},
325 $old_index, $untagged->{$ifindex} );
326 }
327  
328 # add in i_logical_unit
329 my $i_unit = $juniper->ifChassisLogicalUnit();
330 foreach my $ifindex (keys %$i_unit) {
331 my $type = $types->{$ifindex} or next;
332 next unless $type eq 'l2vlan';
333 # XXX not sure why this is off by one?!
334 my $vlan = $i_unit->{$ifindex} - 1 or next;
335 $i_vlan->{$ifindex} = $vlan;
336 }
337  
338 return $i_vlan;
339 }
340  
341 # ELS returns the VLAN ID in dot1qPvid. Pre-ELS releases differ: 10.4
342 # returns the jnxExVlanTable index, 12.3 the VLAN ID. Prefer the reading the
343 # port's untagged membership confirms, else keep the index translation.
344 sub _pvid_vlan {
345 my ( $juniper, $pvid, $old_index, $untagged ) = @_;
346  
347 my $translated = $old_index->{$pvid};
348 return $pvid unless defined $translated;
349  
350 my %member = map { $_ => 1 } grep {defined} @{ $untagged || [] };
351 return $pvid if $member{$pvid} and !$member{$translated};
352 return $translated;
353 }
354  
355 sub v_name {
356 my $juniper = shift;
357  
358 return $juniper->jnx_els_v_name() || $juniper->jnx_v_name();
359 }
360  
361 # Index doesn't use VLAN ID, so override the HOA private method here to
362 # correct the mapping
363 sub _vlan_hoa {
364 my $juniper = shift;
365 my ( $v_ports, $partial ) = @_;
366  
367 my $index = $juniper->bp_index();
368 my $v_index = $juniper->v_index($partial);
369  
370 my $vlan_hoa = {};
371 foreach my $idx ( keys %$v_ports ) {
372 next unless ( defined $v_ports->{$idx} );
373 my $portlist = $v_ports->{$idx};
374 my $ret = [];
375 my $vlan_ndx;
376  
377 # Strip TimeFilter if we're using VlanCurrentTable
378 ( $vlan_ndx = $idx ) =~ s/^\d+\.//;
379  
380 # Convert portlist bit array to bp_index array
381 for ( my $i = 0; $i <= $#$portlist; $i++ ) {
382 push( @{$ret}, $i + 1 ) if ( @$portlist[$i] );
383 }
384  
385 #Create HoA ifIndex -> VLAN array
386 foreach my $port ( @{$ret} ) {
387 my $ifindex = $index->{$port};
388 next unless ( defined($ifindex) ); # shouldn't happen
389 next if ( defined $partial and $ifindex !~ /^$partial$/ );
390 push( @{ $vlan_hoa->{$ifindex} }, $v_index->{$vlan_ndx} );
391 }
392 }
393 return $vlan_hoa;
394 }
395  
396 # Older switches use RFC 4363 standard PortList definition, newer ELS switches
397 # return an ASCII-encoded, comma separated string of port indexes - wow
398 sub i_vlan_membership {
399 my $juniper = shift;
400 my $partial = shift;
401  
402 # Use presence of JUNIPER-VLAN-MIB::jnxExVlanTag to indicate if we should
403 # treat as RFC 4363 standard PortList
404 my $old_index = $juniper->jnx_v_index();
405  
406 if ( scalar keys %$old_index ) {
407 return $juniper->SUPER::i_vlan_membership($partial);
408 }
409  
410 my $res;
411  
412 # This isn't a PortList so use _raw to prevent munge
413 my $v_ports = $juniper->qb_cv_egress_raw($partial)
414 || $juniper->qb_v_egress_raw($partial);
415 my $bp_index = $juniper->bp_index();
416  
417 foreach my $vlan ( keys %$v_ports ) {
418 my @bp_indexes = split /,/, $v_ports->{$vlan};
419 foreach my $idx (@bp_indexes) {
420 if (!exists $bp_index->{$idx}) {
421 print " VLAN $vlan has no bp_index mapping. Skipping.\n"
422 if $DEBUG;
423 next;
424 }
425 push @{ $res->{ $bp_index->{$idx} } }, $vlan;
426 }
427 }
428 return $res;
429 }
430  
431 sub i_vlan_membership_untagged {
432 my $juniper = shift;
433 my $partial = shift;
434  
435 # Use presence of JUNIPER-VLAN-MIB::jnxExVlanTag to indicate if we should
436 # treat as RFC 4363 standard PortList
437 my $old_index = $juniper->jnx_v_index() || {};
438  
439 if ( scalar keys %$old_index ) {
440 return $juniper->SUPER::i_vlan_membership_untagged($partial);
441 }
442  
443 my $types = $juniper->i_type() || {};
444 my $res = {};
445  
446 # This isn't a PortList so use _raw to prevent munge
447 my $v_ports = $juniper->qb_cv_untagged_raw($partial)
448 || $juniper->qb_v_untagged_raw($partial);
449 my $bp_index = $juniper->bp_index();
450  
451 foreach my $vlan ( keys %$v_ports ) {
452 my @bp_indexes = split /,/, $v_ports->{$vlan};
453 foreach my $idx (@bp_indexes) {
454 if (!exists $bp_index->{$idx}) {
455 print " untagged VLAN $vlan has no bp_index mapping. Skipping.\n"
456 if $DEBUG;
457 next;
458 }
459 push @{ $res->{ $bp_index->{$idx} } }, $vlan;
460 }
461 }
462  
463 # add in i_logical_unit
464 my $i_unit = $juniper->ifChassisLogicalUnit();
465 foreach my $ifindex (keys %$i_unit) {
466 my $type = $types->{$ifindex} or next;
467 next unless $type eq 'l2vlan';
468 # XXX not sure why this is off by one?!
469 my $vlan = $i_unit->{$ifindex} - 1 or next;
470 push @{ $res->{ $ifindex } }, $vlan;
471 }
472  
473 return $res;
474 }
475  
476 # Pseudo ENTITY-MIB methods
477  
478 # This class supports both virtual chassis (stackable) and physical chassis
479 # based devices, identify if we have a virtual chassis so that we return
480 # appropriate entPhysicalClass and correct ordering
481  
482 sub _e_is_virtual {
483 my $juniper = shift;
484  
485 my $v_test = $juniper->jnxVirtualChassisMemberRole() || {};
486  
487 #If we are functioning as a stack someone should be master
488 foreach my $iid ( keys %$v_test ) {
489 my $role = $v_test->{$iid};
490 return 1 if ($role =~ /master/i);
491 }
492 return 0;
493 }
494  
495 sub _e_virtual_index {
496 my $juniper = shift;
497  
498 my $containers = $juniper->jnxContainersWithin() || {};
499 my $members = $juniper->jnxVirtualChassisMemberRole() || {};
500  
501 my %v_index;
502 foreach my $key ( keys %$containers ) {
503 foreach my $member ( keys %$members ) {
504  
505 # Virtual chassis members start at zero
506 $member++;
507  
508 # We will be duplicating and eliminating some keys,
509 # but this is for the benefit of e_parent()
510 my $index = sprintf( "%02d", $key )
511 . sprintf( "%02d", $member ) . "0000";
512 my $iid = "$key\.$member\.0\.0";
513 $v_index{$iid} = $index;
514 }
515 unless ( $containers->{$key} ) {
516 my $index = sprintf( "%02d", $key ) . "000000";
517 $v_index{$key} = $index;
518 }
519 }
520 return \%v_index;
521 }
522  
523 # Member frames are the K.N.0.0 rows built by _e_virtual_index() under the
524 # chassis frame container K, for virtual chassis member id N-1
525 sub _e_vc_member {
526 my $juniper = shift;
527 my $iid = shift;
528  
529 return unless $iid =~ /^(\d+)\.(\d+)\.0\.0$/;
530 my ( $container, $slot ) = ( $1, $2 );
531  
532 my $containers = $juniper->jnxContainersDescr() || {};
533 my $descr = $containers->{$container} || '';
534 return unless $descr =~ /chassis/i;
535 return $slot - 1;
536 }
537  
538 sub e_index {
539 my $juniper = shift;
540  
541 my $contents = $juniper->jnxContentsDescr() || {};
542 my $containers = $juniper->jnxContainersDescr() || {};
543 my $virtuals = $juniper->_e_virtual_index() || {};
544 my $is_virtual = $juniper->_e_is_virtual();
545  
546 # Format into consistent integer format so that numeric sorting works
547 my %e_index;
548 if ($is_virtual) {
549 foreach my $key ( keys %$virtuals ) {
550 $e_index{$key} = $virtuals->{$key};
551 }
552 }
553 else {
554 foreach my $key ( keys %$containers ) {
555 $e_index{$key} = sprintf ("%02d", $key) . "000000";
556 }
557 }
558 foreach my $key ( keys %$contents ) {
559 $e_index{$key} = join( '', map { sprintf "%02d", $_ } split /\./, $key );
560 }
561  
562 return \%e_index;
563 }
564  
565 sub e_class {
566 my $juniper = shift;
567  
568 my $e_index = $juniper->e_index() || {};
569 my $fru_type = $juniper->jnxFruType() || {};
570 my $c_type = $juniper->jnxContainersDescr() || {};
571 my $is_virtual = $juniper->_e_is_virtual();
572  
573 my %e_class;
574 foreach my $iid ( keys %$e_index ) {
575
576 my $type = $fru_type->{$iid} || 0;
577 my $container = $c_type->{$iid} || 0;
578  
579 if ( $type =~ /power/i ) {
580 $e_class{$iid} = 'powerSupply';
581 }
582 elsif ( $type =~ /fan/i ) {
583 $e_class{$iid} = 'fan';
584 }
585 elsif ( $type ) {
586 $e_class{$iid} = 'module';
587 }
588 # Shouldn't get here if we have type which means
589 # we only have container, chassis, and stack left
590 elsif (($container =~ /chassis/i) and (!$is_virtual) ) {
591 $e_class{$iid} = 'chassis';
592 }
593 elsif (($container =~ /chassis/i) and ($is_virtual)) {
594 $e_class{$iid} = 'stack';
595 }
596 elsif ( $is_virtual and defined $juniper->_e_vc_member($iid) ) {
597 $e_class{$iid} = 'chassis';
598 }
599 else {
600 $e_class{$iid} = 'container';
601 }
602 }
603 return \%e_class;
604 }
605  
606 sub e_descr {
607 my $juniper = shift;
608  
609 my $e_index = $juniper->e_index() || {};
610 my $box_descr = $juniper->box_descr;
611 my $contents = $juniper->jnxContentsDescr() || {};
612 my $containers = $juniper->jnxContainersDescr() || {};
613  
614 my %e_descr;
615 foreach my $iid ( keys %$e_index ) {
616
617 my $content_descr = $contents->{$iid} || 0;
618 my $container_descr = $containers->{$iid} || 0;
619  
620 if ($content_descr) {
621 $e_descr{$iid} = $content_descr;
622 }
623 elsif ($container_descr and $container_descr !~ /chassis/) {
624 $e_descr{$iid} = $container_descr;
625 }
626 elsif ($container_descr and $container_descr =~ /chassis/) {
627 $e_descr{$iid} = $box_descr;
628 }
629 # We should only be left with virtual entries created in
630 # _e_virtual_index()
631 elsif ($iid =~ /^(\d+)\.(\d+)(\.0)+?/) {
632 my $descr = $containers->{$1};
633 $e_descr{$iid} = $descr;
634 }
635 # Anything past here undef
636 }
637 return \%e_descr;
638 }
639  
640 sub e_serial {
641 my $juniper = shift;
642  
643 my $e_index = $juniper->e_index() || {};
644 my $serials = $juniper->jnxContentsSerialNo() || {};
645 my $e_class = $juniper->e_class() || {};
646 my $is_virtual = $juniper->_e_is_virtual();
647 my $box_serial = $juniper->serial();
648 my $vc_serials
649 = $is_virtual ? $juniper->jnxVirtualChassisMemberSerialnumber() : {};
650 $vc_serials ||= {};
651  
652 my %e_serial;
653 foreach my $iid ( keys %$e_index ) {
654 my $serial = $serials->{$iid} || '';
655 my $class = $e_class->{$iid} || '';
656 my $member = $juniper->_e_vc_member($iid);
657 # Chassis serial number is seperate on true chassis
658 # Virtual chassis members report their own serial
659 if (!$is_virtual and ($class =~ /chassis/i)){
660 $e_serial{$iid} = $box_serial;
661 }
662 elsif ( $class eq 'chassis' and defined $member
663 and $vc_serials->{$member} )
664 {
665 $e_serial{$iid} = $vc_serials->{$member};
666 }
667 elsif (($serial !~ /^\w/) or ($serial =~ /builtin/i)) {
668 next;
669 }
670 else {
671 $e_serial{$iid} = $serial;
672 }
673 }
674 return \%e_serial;
675 }
676  
677 sub e_model {
678 my $juniper = shift;
679  
680 my $e_model = $juniper->orig_e_model() || {};
681 return $e_model unless $juniper->_e_is_virtual();
682  
683 my $e_class = $juniper->e_class() || {};
684 my $vc_models = $juniper->jnxVirtualChassisMemberModel() || {};
685  
686 my %e_model = %$e_model;
687 foreach my $iid ( keys %$e_class ) {
688 next unless $e_class->{$iid} eq 'chassis';
689 my $member = $juniper->_e_vc_member($iid);
690 next unless defined $member and $vc_models->{$member};
691 $e_model{$iid} = uc $vc_models->{$member};
692 }
693 return \%e_model;
694 }
695  
696 sub e_fru {
697 my $juniper = shift;
698  
699 my $e_index = $juniper->e_index() || {};
700 my $frus = $juniper->jnxContentsPartNo() || {};
701  
702 my %e_fru;
703 foreach my $iid ( keys %$e_index ) {
704 my $fru = $frus->{$iid} || '';
705 if ( ($fru !~ /^\w/) or ($fru =~ /builtin/i)) {
706 $e_fru{$iid} = "false";
707 }
708 else {
709 $e_fru{$iid} = "true";
710 }
711 }
712 return \%e_fru;
713 }
714  
715 sub e_type {
716 my $juniper = shift;
717  
718 my $e_index = $juniper->e_index() || {};
719 my $contents = $juniper->e_contents_type() || {};
720 my $containers = $juniper->e_containers_type() || {};
721  
722 my %e_type;
723 foreach my $iid ( keys %$e_index ) {
724
725 my $content_type = $contents->{$iid} || 0;
726 my $container_type = $containers->{$iid} || 0;
727
728 if ($content_type) {
729 $content_type =~ s/\.0//;
730 $e_type{$iid} = $content_type;
731 }
732 elsif ($container_type) {
733 $container_type =~ s/\.0//;
734 $e_type{$iid} = $container_type;
735 }
736 # We should only be left with virtual entries created in
737 # _e_virtual_index()
738 elsif ($iid =~ /^(\d+)\.(\d+)(\.0)+?/) {
739 my $descr = $containers->{$1};
740 $descr =~ s/\.0//;
741 $e_type{$iid} = $descr;
742 }
743 # Anything past here undef
744 }
745 return \%e_type;
746 }
747  
748 sub e_vendor {
749 my $juniper = shift;
750  
751 my $e_idx = $juniper->e_index() || {};
752  
753 my %e_vendor;
754 foreach my $iid ( keys %$e_idx ) {
755 $e_vendor{$iid} = 'juniper';
756 }
757 return \%e_vendor;
758 }
759  
760 sub e_pos {
761 my $juniper = shift;
762  
763 # We could look at index levels, but this will work as well
764 return $juniper->e_index();
765 }
766  
767 sub e_parent {
768 my $juniper = shift;
769  
770 my $e_idx = $juniper->e_index() || {};
771 my $c_within = $juniper->jnxContainersWithin() || {};
772 my $e_descr = $juniper->e_descr() || {};
773 my $is_virtual = $juniper->_e_is_virtual();
774  
775 my %e_parent;
776 foreach my $iid ( keys %$e_idx ) {
777 next unless $iid;
778  
779 my ( $idx, $l1, $l2, $l3 ) = split /\./, $iid;
780 my $within = $c_within->{$idx};
781 my $descr = $e_descr->{$iid};
782  
783 if ( !$is_virtual and ( $iid =~ /^(\d+)\.\d+/ ) ) {
784 $e_parent{$iid} = sprintf( "%02d", $1 ) . "000000";
785 }
786 elsif ( $is_virtual
787 and ( $descr =~ /chassis/i )
788 and ( $iid =~ /^(\d+)\.(\d+)(\.0)+?/ ) )
789 {
790 $e_parent{$iid} = sprintf( "%02d", $1 ) . "000000";
791 }
792 elsif ( $is_virtual and ( $iid =~ /^(\d+)\.(\d+)(\.0)+?/ ) ) {
793 $e_parent{$iid}
794 = sprintf( "%02d", $within ) . sprintf( "%02d", $2 ) . "0000";
795 }
796 elsif ( $is_virtual and ( $iid =~ /^(\d+)\.(\d+)\.[1-9]+/ ) ) {
797 $e_parent{$iid}
798 = sprintf( "%02d", $1 ) . sprintf( "%02d", $2 ) . "0000";
799 }
800 elsif ( defined $within and $iid =~ /\d+/ ) {
801 $e_parent{$iid} = sprintf( "%02d", $within ) . "000000";
802 }
803 else {
804 next;
805 }
806 }
807 return \%e_parent;
808 }
809  
810 sub peth_port_ifindex {
811 my $peth = shift;
812 my $partial = shift;
813  
814 my $peth_port_ifindex = {};
815 my $i_descr = $peth->i_description();
816  
817 # Juniper doesn't have a translator, but the ports follow a standardized layout.
818 # Only the power status from the first member in a virtual chassis is reported.
819 foreach my $i ( keys %$i_descr ) {
820 # Juniper's port numbering is PHYS-FPC/PIC/PORT, only looking at PIC 0
821 if ($i_descr->{$i} =~ m/^(fe|ge|xe|et)-(\d+)\/(0)\/(\d+)$/) {
822 # Juniper port numbering begins at 0, but for PowerEthernet it begins at 1
823 my $mod = $2+1;
824 my $port = $4+1;
825 $peth_port_ifindex->{"$mod.$port"} = $i;
826 }
827 }
828 return $peth_port_ifindex;
829 }
830  
831 sub i_edgeport_admin {
832 my $juniper = shift;
833 my $partial = shift;
834  
835 my $edge_admin = $juniper->jnx_cist_p_edge_admin() || {};
836 return $juniper->SUPER::i_edgeport_admin($partial)
837 unless keys %$edge_admin;
838 return SNMP::Info::Bridge::_stp_edge_by_ifindex( $juniper, $edge_admin,
839 $partial );
840 }
841  
842 sub i_edgeport_oper {
843 my $juniper = shift;
844 my $partial = shift;
845  
846 my $edge_oper = $juniper->jnx_cist_p_edge_oper() || {};
847 return $juniper->SUPER::i_edgeport_oper($partial)
848 unless keys %$edge_oper;
849 return SNMP::Info::Bridge::_stp_edge_by_ifindex( $juniper, $edge_oper,
850 $partial );
851 }
852  
853 # Junos reports logical units (ge-0/0/1.0, ae0.0) in the LAG tables where the
854 # aggregate and its members are the physical interfaces beneath them
855 sub agg_ports {
856 my $juniper = shift;
857  
858 my $lower = $juniper->_jnx_stack_lower();
859 my $attached = $juniper->lag_port_attached() || {};
860 my %members;
861 foreach my $port ( keys %$attached ) {
862 next unless $attached->{$port};
863 my $member = _jnx_physical( $lower, $port );
864 my $aggregate = _jnx_physical( $lower, $attached->{$port} );
865 $members{$member} = $aggregate unless $member == $aggregate;
866 }
867 return \%members;
868 }
869  
870 sub _agg_ports_walked { return qw/ifStackStatus lag_port_attached/ }
871  
872 sub _lag_vendor_walked { return qw/ifStackStatus/ }
873  
874 # Each aggregator row maps to the physical aggregate its member units attach to
875 sub _lag_aggregator_map {
876 my $juniper = shift;
877  
878 my $rows = $juniper->lag_actor_system() || {};
879 my $lower = $juniper->_jnx_stack_lower();
880 my $attached = $juniper->lag_port_attached() || {};
881 my %map;
882 foreach my $row ( keys %$rows ) {
883 my ($member) = grep { $attached->{$_} }
884 sort { $a <=> $b } keys %{ $lower->{$row} || {} };
885 $map{$row} = defined $member
886 ? _jnx_physical( $lower, $attached->{$member} )
887 : $row;
888 }
889 return \%map;
890 }
891  
892 # Hashref: higher ifIndex => { lower ifIndex => 1 } from ifStackStatus
893 sub _jnx_stack_lower {
894 my $juniper = shift;
895  
896 my $stack = $juniper->ifStackStatus() || {};
897 my %lower;
898 foreach my $index ( keys %$stack ) {
899 my ( $higher, $below ) = split /\./, $index;
900 next unless $higher and $below;
901 $lower{$higher}{$below} = 1;
902 }
903 return \%lower;
904 }
905  
906 # The interface directly below a logical unit that has no interface of its
907 # own below it (the unit's parent); an ifIndex without one is already physical
908 sub _jnx_physical {
909 my ( $lower, $ifindex ) = @_;
910  
911 my @parents = grep { !$lower->{$_} } keys %{ $lower->{$ifindex} || {} };
912 return scalar @parents == 1 ? $parents[0] : $ifindex;
913 }
914  
915 # The protocols the default context reports: 'mstp' when MST is started at
916 # context 0, else the BRIDGE-MIB protocol dot1dStpVersion names, then 'vstp'
917 # when VSTP runs, beside that protocol or alone. Empty in any other context.
918 sub _jnx_stp_protocols {
919 my $juniper = shift;
920  
921 # A VSTP VLAN's own context serves that VLAN's tree as RSTP
922 return [] if length $juniper->_walk_context();
923 return $juniper->{_jnx_stp_protocols}
924 if $juniper->{_jnx_stp_protocols};
925  
926 my $control = $juniper->jnx_mst_control() || {};
927 my $default = $juniper->_jnx_default_protocol($control);
928 my @protocols = grep {defined} $default;
929 push @protocols, 'vstp'
930 if ( $default // '' ) ne 'mstp' and $juniper->_jnx_is_vstp($control);
931 $juniper->{_jnx_stp_protocols} = \@protocols;
932 return \@protocols;
933 }
934  
935 sub _jnx_default_protocol {
936 my ( $juniper, $control ) = @_;
937  
938 my $started = $control->{0};
939 return 'mstp' if defined $started and $started =~ /^(?:start|1)$/;
940  
941 my $version = $juniper->jnx_stp_version();
942 return unless defined $version;
943 return 'rstp' if $version =~ /^(?:rstp|2)$/;
944 return 'stp' if $version =~ /^(?:stpCompatible|0)$/;
945 return;
946 }
947  
948 # VSTP has no flag of its own: each VLAN's control row sits at a context
949 # other than 0 and reads shutdown, and no MSTI exists. A started row there is
950 # MSTP inside a routing instance. The row at context 0 is the default
951 # context's own and says nothing about VSTP.
952 sub _jnx_is_vstp {
953 my ( $juniper, $control ) = @_;
954  
955 my @contexts = grep { $_ ne '0' } keys %$control;
956 return 0 unless @contexts;
957 return 0
958 if grep { ( $control->{$_} // '' ) !~ /^(?:shutdown|2)$/ } @contexts;
959 return 0 if $juniper->_jnx_walk_incomplete('jnx_mst_control');
960 return 0 if keys %{ $juniper->jnx_msti_priority() || {} };
961 return 0 if $juniper->_jnx_walk_incomplete('jnx_msti_priority');
962 return 1;
963 }
964  
965 sub _jnx_walk_incomplete {
966 my ( $juniper, $method ) = @_;
967  
968 my $status = $juniper->walk_status($method);
969 return $status && !$status->{complete} ? 1 : 0;
970 }
971  
972 sub _jnx_runs {
973 my ( $juniper, $protocol ) = @_;
974  
975 my $protocols = $juniper->_jnx_stp_protocols();
976 return ( grep { $_ eq $protocol } @$protocols ) ? 1 : 0;
977 }
978  
979 sub hasSTP {
980 my $juniper = shift;
981 return $juniper->_jnx_runs('stp');
982 }
983  
984 sub hasRSTP {
985 my $juniper = shift;
986 return $juniper->_jnx_runs('rstp');
987 }
988  
989 sub hasMSTP {
990 my $juniper = shift;
991 return $juniper->_jnx_runs('mstp');
992 }
993  
994 sub hasVSTP {
995 my $juniper = shift;
996 return $juniper->_jnx_runs('vstp');
997 }
998  
999 sub _context_kinds {
1000 my ( $juniper, $feature ) = @_;
1001  
1002 return ['vrf'] if ( $feature // '' ) eq 'vrf';
1003 return unless ( $feature // '' ) eq 'stp';
1004 return unless $juniper->_jnx_runs('vstp');
1005 return ['vlan'];
1006 }
1007  
1008 # Junos puts the context first in the v2c community, the reverse of Cisco's
1009 # community@VLAN: red@public for routing instance red
1010 sub _snmp_context {
1011 my ( $juniper, $kind, $id ) = @_;
1012  
1013 return $juniper->_jnx_vrf_context($id)
1014 if ( $kind // '' ) eq 'vrf';
1015 return unless ( $kind // '' ) eq 'vlan' and defined $id;
1016 return {
1017 kind => 'vlan',
1018 id => $id,
1019 v2c => "default::$id\@%s",
1020 v3 => "default::$id",
1021 };
1022 }
1023  
1024 # A Junos v3 context name is the routing instance name; the default
1025 # instance is the default context
1026 sub _jnx_vrf_context {
1027 my ( $juniper, $name ) = @_;
1028  
1029 return if !defined $name or $name eq q() or $name eq 'default';
1030 return {
1031 kind => 'vrf',
1032 id => $name,
1033 v2c => "$name\@%s",
1034 v3 => $name,
1035 };
1036 }
1037  
1038 # Each routing instance reads in its own context
1039 sub _vrf_context_filters { return 1; }
1040  
1041 # The default context also serves every instance's neighbors and addresses
1042 # on the instances' ports
1043 sub _vrf_default_context_leaks { return 1; }
1044  
1045 # Host link-locals are only in IPV6-MIB ipv6NetToMediaTable
1046 sub _vrf_nd_from_ipv6_mib { return 1; }
1047  
1048 # Platform-internal instances: the global table, the layer 2 default,
1049 # double-underscore names and the gRIBI context. mgmt_junos is listed: it
1050 # holds ports and reads in its own context.
1051 sub _vrf_internal_instance {
1052 my ( $juniper, $name ) = @_;
1053  
1054 return 1 if !defined $name;
1055 return 1 if grep { $name eq $_ } q(), 'default', 'default-switch';
1056 return $name =~ /^__.*__$/ || $name =~ /^:/ ? 1 : 0;
1057 }
1058  
1059 # MPLS-L3VPN-STD-MIB lists the VRFs; vacmContextName adds the instances it
1060 # does not list, as VRFs where jnxVpnTable types them bgpIpVpn. A served
1061 # vacm list lists default too (_vrf_list_default in VRF.pm), so a device
1062 # listing no instance reads none.
1063 sub _vrf_served_instances {
1064 my $juniper = shift;
1065  
1066 my $instances = $juniper->SUPER::_vrf_served_instances();
1067 my $vpn_types = $juniper->_jnx_vpn_types();
1068 foreach my $name ( $juniper->_jnx_vacm_names() ) {
1069 my $type = $vpn_types->{$name} // 2;
1070 next if $instances->{$name} or $type != 2;
1071 my $kind = exists $vpn_types->{$name} ? 'vrf' : 'virtual-router';
1072 my $record = SNMP::Info::VRF::_vrf_record( $name, $kind, 'device' );
1073 $record->{sources}{list} = 'SNMP-VIEW-BASED-ACM-MIB';
1074 $juniper->_vrf_set_contexts($record);
1075 $instances->{$name} = $record;
1076 }
1077 $juniper->_vrf_list_default( $instances, 'SNMP-VIEW-BASED-ACM-MIB' )
1078 if %{ $juniper->jnx_vacm_context_name() || {} };
1079 return $instances;
1080 }
1081  
1082 # The names vacmContextName serves, minus the internal ones and the
1083 # instance::N contexts VSTP serves
1084 sub _jnx_vacm_names {
1085 my $juniper = shift;
1086  
1087 my $rows = $juniper->jnx_vacm_context_name() || {};
1088 my %names;
1089 foreach my $iid ( keys %$rows ) {
1090 my ($name) = $juniper->_vrf_decode_name($iid);
1091 next if !defined $name or $juniper->_vrf_internal_instance($name);
1092 next if $name =~ /^[^:]+::\d+$/;
1093 $names{$name} = 1;
1094 }
1095 my @names = sort keys %names;
1096 return @names;
1097 }
1098  
1099 # { name => jnxVpnType } for each VPN jnxVpnTable lists: bgpIpVpn(2) is a
1100 # VRF, any other type a VPLS or other layer 2 instance
1101 sub _jnx_vpn_types {
1102 my $juniper = shift;
1103  
1104 my $rows = $juniper->vrf_jnx_vpn_status() || {};
1105 my %types;
1106 foreach my $iid ( keys %$rows ) {
1107 my ( $type, $rest ) = split /\./, $iid, 2;
1108 next if !defined $rest;
1109 my ($name) = $juniper->_vrf_decode_name($rest);
1110 $types{$name} = $type if defined $name;
1111 }
1112 return \%types;
1113 }
1114  
1115 # The vacm and jnxVpn walks list the instances, so a partial one is a
1116 # short list
1117 sub _vrf_walk_incomplete {
1118 my $juniper = shift;
1119  
1120 return 1 if $juniper->SUPER::_vrf_walk_incomplete();
1121 foreach my $method (qw/jnx_vacm_context_name vrf_jnx_vpn_status/) {
1122 my $status = $juniper->walk_status($method) or next;
1123 return 1 unless $status->{complete};
1124 }
1125 return 0;
1126 }
1127  
1128 sub _jnx_vlan_context {
1129 my ( $instance, $vlan ) = @_;
1130  
1131 my $name = "${instance}::$vlan";
1132 return {
1133 kind => 'vlan',
1134 id => $vlan,
1135 instance => $instance,
1136 v2c => "$name\@%s",
1137 v3 => $name,
1138 };
1139 }
1140  
1141 # The VSTP trees as [instance, VLAN] pairs, from the context names the agent
1142 # binds, <instance>::<vlan>. Nothing else names them: the MST control rows
1143 # carry context ids, and the VLAN table lists VLANs that do not run VSTP.
1144 sub _jnx_vstp_contexts {
1145 my $juniper = shift;
1146  
1147 my $names = $juniper->jnx_vacm_context_name() || {};
1148 my %pairs;
1149 foreach my $name ( values %$names ) {
1150 next unless defined $name and $name =~ /^([^:]+)::([1-9]\d{0,3})$/;
1151 my ( $instance, $vlan ) = ( $1, $2 + 0 );
1152 $pairs{$name} = [ $instance, $vlan ] if $vlan <= 4094;
1153 }
1154 return [ sort { $a->[0] cmp $b->[0] or $a->[1] <=> $b->[1] }
1155 values %pairs ];
1156 }
1157  
1158 sub _jnx_vstp_served {
1159 my $juniper = shift;
1160  
1161 my $names = $juniper->jnx_vacm_context_name() || {};
1162 return 0 if $juniper->_jnx_walk_incomplete('jnx_vacm_context_name');
1163 return keys %$names ? 1 : 0;
1164 }
1165  
1166 sub _jnx_vstp_trees {
1167 my $juniper = shift;
1168  
1169 my $measured = $juniper->_jnx_measured('vstp');
1170 my %trees;
1171 foreach my $pair ( @{ $juniper->_jnx_vstp_contexts() } ) {
1172 my ( $instance, $vlan ) = @$pair;
1173 my $default = $instance eq 'default';
1174 my $key = $default ? "vlan.$vlan" : "vlan.$vlan\@$instance";
1175  
1176 $trees{$key} = {
1177 type => 'vlan',
1178 id => $vlan,
1179 vlan => $vlan,
1180 instance => $instance,
1181 protocol => 'vstp',
1182 measured => $measured,
1183 ports_complete => 1,
1184 contexts => [ _jnx_vlan_context( $instance, $vlan ) ],
1185 bridge => undef,
1186 root => undef,
1187 };
1188 }
1189 return \%trees;
1190 }
1191  
1192 sub stp_protocols {
1193 my $juniper = shift;
1194  
1195 return unless $juniper->_stp_default_context_ok();
1196 my $protocols = $juniper->_jnx_stp_protocols();
1197 return [@$protocols] if @$protocols;
1198 return unless $juniper->_stp_dot1d_tree();
1199 return ['dot1d'];
1200 }
1201  
1202 sub stp_trees {
1203 my $juniper = shift;
1204  
1205 return unless $juniper->_stp_default_context_ok();
1206 return $juniper->{_stp_trees} if $juniper->{_stp_trees};
1207  
1208 my $vstp = $juniper->_jnx_runs('vstp');
1209 my ($default) = grep { $_ ne 'vstp' } @{ $juniper->_jnx_stp_protocols() };
1210 my %trees
1211 = ( $default // '' ) eq 'mstp' ? %{ $juniper->_jnx_mstp_trees() }
1212 : ( $default or !$vstp ) ? %{ $juniper->_jnx_dot1d_trees($default) }
1213 : ();
1214 %trees = ( %trees, %{ $juniper->_jnx_vstp_trees() } ) if $vstp;
1215  
1216 $juniper->{_stp_trees} = \%trees if keys %trees;
1217 return \%trees;
1218 }
1219  
1220 sub _jnx_measured {
1221 my ( $juniper, $protocol ) = @_;
1222  
1223 return 0 unless defined $protocol;
1224 return 0 unless $protocol =~ /^(?:stp|rstp|mstp|vstp)$/;
1225 return $juniper->os() eq 'junos' ? 1 : 0;
1226 }
1227  
1228 sub _jnx_dot1d_trees {
1229 my ( $juniper, $protocol ) = @_;
1230  
1231 my $record = $juniper->_stp_dot1d_tree() or return {};
1232 return {
1233 'cist.0' => {
1234 %$record,
1235 protocol => $protocol,
1236 measured => $juniper->_jnx_measured($protocol),
1237 }
1238 };
1239 }
1240  
1241 sub _jnx_tree_key {
1242 my $instance = shift;
1243 return $instance ? "msti.$instance" : 'cist.0';
1244 }
1245  
1246 # Instances with a port row at a bridge port that bp_index does not map
1247 sub _jnx_unmapped_instances {
1248 my $juniper = shift;
1249  
1250 my $bp_index = $juniper->bp_index() || {};
1251 my %unmapped;
1252 foreach my $port ( keys %{ $juniper->jnx_cist_p_state() || {} } ) {
1253 $unmapped{0} = 1 unless defined $bp_index->{$port};
1254 }
1255 foreach my $row ( keys %{ $juniper->jnx_msti_p_state() || {} } ) {
1256 my ( $port, $instance ) = split /\./, $row, 2;
1257 $unmapped{$instance} = 1 unless defined $bp_index->{$port};
1258 }
1259 return \%unmapped;
1260 }
1261  
1262 sub _jnx_mstp_trees {
1263 my $juniper = shift;
1264  
1265 my $address = ( $juniper->jnx_mst_brg_address() || {} )->{0};
1266 my $priorities = $juniper->jnx_msti_priority() || {};
1267 my $roots = $juniper->jnx_msti_root() || {};
1268 my $cist_prio = ( $juniper->jnx_mst_cist_priority() || {} )->{0};
1269 my $cist_root = ( $juniper->jnx_mst_cist_root() || {} )->{0};
1270 my $unmapped = $juniper->_jnx_unmapped_instances();
1271 my $measured = $juniper->_jnx_measured('mstp');
1272  
1273 my %instances = ( 0 => [ $cist_prio, $cist_root ] );
1274 foreach my $row ( keys %$priorities ) {
1275 my ( $context, $instance ) = split /\./, $row, 2;
1276 next unless $context == 0;
1277 $instances{$instance} = [ $priorities->{$row}, $roots->{$row} ];
1278 }
1279  
1280 my %trees;
1281 foreach my $instance ( keys %instances ) {
1282 my ( $priority, $root ) = @{ $instances{$instance} };
1283 $trees{ _jnx_tree_key($instance) } = {
1284 type => $instance ? 'msti' : 'cist',
1285 id => $instance,
1286 protocol => 'mstp',
1287 measured => $measured,
1288 ports_complete => $unmapped->{$instance} ? 0 : 1,
1289 contexts => [ { kind => 'none' } ],
1290 bridge =>
1291 scalar SNMP::Info::Bridge::_stp_bridge_id_from( $priority,
1292 $address ),
1293 root => scalar SNMP::Info::Bridge::_stp_decode_bridge_id($root),
1294 };
1295 }
1296 return \%trees;
1297 }
1298  
1299 sub stp_tree_ports {
1300 my ( $juniper, $tree_key ) = @_;
1301  
1302 my ( $vlan, $instance )
1303 = ( $tree_key // '' ) =~ /^vlan\.(\d+)(?:\@(?!default$)([^:\@]+))?$/;
1304 my $wanted
1305 = defined $vlan ? ( $instance // 'default' ) . "::$vlan" : undef;
1306 return $juniper->_jnx_vstp_tree_ports( $tree_key, $wanted )
1307 if defined $wanted and length $juniper->_walk_context();
1308  
1309 # Looked up here so that its error names this method
1310 my $tree = $juniper->_stp_tree_for_key($tree_key) or return;
1311 return $juniper->_jnx_vstp_elsewhere( $tree_key, $wanted )
1312 if defined $wanted;
1313 return $juniper->_stp_dot1d_port_rows($tree)
1314 unless ( $tree->{protocol} || '' ) eq 'mstp';
1315 return $juniper->_jnx_mstp_port_rows($tree);
1316 }
1317  
1318 # A VSTP tree is read only inside its own context, which serves BRIDGE-MIB
1319 # for that VLAN alone, so the key itself labels the rows there
1320 sub _jnx_vstp_tree_ports {
1321 my ( $juniper, $tree_key, $wanted ) = @_;
1322  
1323 my $context = $juniper->_walk_context();
1324 return $juniper->_jnx_vstp_wrong_context( $tree_key, $wanted, $context )
1325 unless $context eq $wanted;
1326 my ($vlan) = $wanted =~ /::(\d+)$/;
1327 return $juniper->_stp_dot1d_port_rows(
1328 { type => 'vlan', id => $vlan + 0 } );
1329 }
1330  
1331 sub _jnx_vstp_elsewhere {
1332 my ( $juniper, $tree_key, $wanted ) = @_;
1333  
1334 $juniper->error_throw(
1335 "SNMP::Info::Layer3::Juniper::stp_tree_ports(): tree '$tree_key' is"
1336 . " served in its own context (v2c '$wanted\@<community>',"
1337 . " v3 '$wanted'): switch to one with update() and call"
1338 . " stp_tree_ports('$tree_key') there.\n" );
1339 return;
1340 }
1341  
1342 sub _jnx_vstp_wrong_context {
1343 my ( $juniper, $tree_key, $wanted, $context ) = @_;
1344  
1345 $juniper->error_throw(
1346 "SNMP::Info::Layer3::Juniper::stp_tree_ports(): tree '$tree_key' is"
1347 . " served in context '$wanted', but this session is in context"
1348 . " '$context': switch to '$wanted' with update() and call"
1349 . " stp_tree_ports('$tree_key') there.\n" );
1350 return;
1351 }
1352  
1353 sub _jnx_mstp_port_rows {
1354 my ( $juniper, $tree ) = @_;
1355  
1356 my $cist = $tree->{id} == 0;
1357 my $prefix = $cist ? 'jnx_cist_p_' : 'jnx_msti_p_';
1358 my %served;
1359 foreach my $column (qw/state role role_raw bridge port enable/) {
1360 my $method = $prefix . $column;
1361 $served{$column} = $juniper->$method() || {};
1362 }
1363 my $bp_index = $juniper->bp_index() || {};
1364  
1365 my %rows;
1366 foreach my $row ( keys %{ $served{state} } ) {
1367 my ( $port, $instance ) = split /\./, $row, 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 tree_type => $tree->{type},
1374 tree_id => $tree->{id},
1375 state => $served{state}{$row},
1376 role => $served{role}{$row},
1377 role_raw => $served{role_raw}{$row},
1378 enabled =>
1379 scalar _jnx_port_enabled( $served{enable}{$row} ),
1380 designated_bridge =>
1381 scalar SNMP::Info::Bridge::_stp_decode_bridge_id(
1382 $served{bridge}{$row} ),
1383 designated_port =>
1384 scalar SNMP::Info::Bridge::_stp_decode_port_id(
1385 $served{port}{$row} ),
1386 };
1387 }
1388 return \%rows;
1389 }
1390  
1391 # MSTP's per-port enable is the tree's ForcePortState, whose disabled(0)
1392 # _stp_enabled would read as unknown; dot1dStpPortEnable is not served there
1393 sub _jnx_port_enabled {
1394 my $value = shift;
1395 return unless defined $value;
1396  
1397 return 0 if $value =~ /^(?:disabled|0)$/;
1398 return scalar SNMP::Info::Bridge::_stp_enabled($value);
1399 }
1400  
1401 sub stp_trees_complete {
1402 my $juniper = shift;
1403  
1404 return 0 unless @{ $juniper->_jnx_stp_protocols() };
1405 return 0 if $juniper->_jnx_walk_incomplete('jnx_mst_control');
1406 my $trees = $juniper->stp_trees() || {};
1407 return 0
1408 if $juniper->_jnx_runs('vstp')
1409 and !$juniper->_jnx_vstp_served();
1410 return 0 if $juniper->_jnx_vlan_contexts_unlisted();
1411 return keys %$trees ? 1 : 0;
1412 }
1413  
1414 # Each VSTP tree has one control row at a context other than 0 and one
1415 # <instance>::<vlan> name. Counts that differ mean a name with no tree behind
1416 # it, or a tree whose name the agent's view hides. Under MSTP any such name
1417 # is VSTP in another routing instance, whose tree is not listed.
1418 sub _jnx_vlan_contexts_unlisted {
1419 my $juniper = shift;
1420  
1421 my $names = @{ $juniper->_jnx_vstp_contexts() };
1422 return $names ? 1 : 0 if $juniper->_jnx_runs('mstp');
1423  
1424 my $control = $juniper->jnx_mst_control() || {};
1425 my $rows = grep { $_ ne '0' } keys %$control;
1426 return $rows == $names ? 0 : 1;
1427 }
1428  
1429 # Each bitmap holds 1024 VLANs, most significant bit first; a short bitmap
1430 # stands for zero-padding
1431 sub stp_vlan_map {
1432 my $juniper = shift;
1433  
1434 return unless $juniper->_jnx_runs('mstp');
1435  
1436 my %first_vlan = (
1437 jnx_msti_vlans_1k => 1,
1438 jnx_msti_vlans_2k => 1025,
1439 jnx_msti_vlans_3k => 2049,
1440 jnx_msti_vlans_4k => 3073,
1441 );
1442 my %map;
1443 foreach my $method ( keys %first_vlan ) {
1444 my $bitmaps = $juniper->$method() || {};
1445 foreach my $row ( keys %$bitmaps ) {
1446 my ( $context, $instance ) = split /\./, $row, 2;
1447 next unless $context == 0;
1448 $map{$_} = _jnx_tree_key($instance) for _jnx_mapped_vlans(
1449 $bitmaps->{$row}, $first_vlan{$method} );
1450 }
1451 }
1452 return unless keys %map;
1453 return \%map;
1454 }
1455  
1456 sub _jnx_mapped_vlans {
1457 my ( $bitmap, $first_vlan ) = @_;
1458  
1459 my @vlans;
1460 my $bit = 0;
1461 foreach my $set ( split //, unpack( 'B*', $bitmap // '' ) ) {
1462 my $vlan = $first_vlan + $bit++;
1463 push @vlans, $vlan if $set and $vlan <= 4094;
1464 }
1465 return @vlans;
1466 }
1467  
1468 # Default tree is not the common tree: the BRIDGE-MIB tree is only the
1469 # default context's under RSTP and STP
1470 sub _stp_default_is_common { return 0; }
1471  
1472 1;
1473 __END__