Info/Layer3/Huawei.pm untested

filter: Statementstmt 0.0% 0 / 321 Branchbran 0.0% 0 / 126 Conditioncond 0.0% 0 / 136 MC/DCmcdc 0.0% 0 / 76 Subroutinesub 0.0% 0 / 41 Podpod 0.0% 0 / 36 totaltotal 0.0% 0 / 736 CC 64 SCAR 29.3
CC
64
Cov
0%
_hw_fdb
56.0
i_duplex_admin
30.0
agg_ports
30.0
CRAP 18.7
1 # SNMP::Info::Layer3::Huawei
2 #
3 # Copyright (c) 2018 Jeroen van Ingen and 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::Layer3::Huawei;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use SNMP::Info::Layer3;
36 use SNMP::Info::IEEE802dot3ad;
37  
38 @SNMP::Info::Layer3::Huawei::ISA = qw/
39 SNMP::Info::IEEE802dot3ad
40 SNMP::Info::Layer3
41 Exporter
42 /;
43 @SNMP::Info::Layer3::Huawei::EXPORT_OK = qw//;
44  
45 our ($VERSION, %GLOBALS, %MIBS, %FUNCS, %MUNGE);
46  
47 $VERSION = '3.985003';
48  
49 %MIBS = (
50 %SNMP::Info::Layer3::MIBS,
51 %SNMP::Info::IEEE802dot3ad::MIBS,
52 'HUAWEI-MIB' => 'quidway',
53 'HUAWEI-PORT-MIB' => 'hwEthernetDuplex',
54 'HUAWEI-IF-EXT-MIB' => 'hwTrunkIfIndex',
55 'HUAWEI-L2IF-MIB' => 'hwL2IfPortIfIndex',
56 'HUAWEI-L2MAM-MIB' => 'hwDynFdbPort',
57 'HUAWEI-POE-MIB' => 'hwPoePower',
58 'HUAWEI-SYS-MAN-MIB' => 'hwPatchVersion',
59 'HUAWEI-ENTITY-EXTENT-MIB' => 'hwEntityFanState',
60 'HUAWEI-MSTP-MIB' => 'hwMstpiStpPortEdgeport',
61 );
62  
63 %GLOBALS = (
64 %SNMP::Info::Layer3::GLOBALS,
65  
66 # HUAWEI-MSTP-MIB
67 'hw_mstp_edge_default' => 'hwMstpEdgedPortDefault',
68 );
69  
70 %FUNCS = (
71 %SNMP::Info::Layer3::FUNCS,
72 %SNMP::Info::IEEE802dot3ad::FUNCS,
73  
74 # HUAWEI-PORT-MIB::hwEthernetTable
75 'hw_eth_speed_admin' => 'hwEthernetSpeedSet',
76 'hw_eth_duplex' => 'hwEthernetDuplex',
77 'hw_eth_auto' => 'hwEthernetNegotiation',
78 'hw_eth_frame_len' => 'hwEthernetJumboframeMaxLength',
79  
80 # HUAWEI-PORT-MIB::hwPhysicalPortTable
81 'hw_phy_port_slot' => 'hwPhysicalPortInSlot',
82  
83 # HUAWEI-IF-EXT-MIB::hwTrunkIfTable
84 'hw_trunk_if_idx' => 'hwTrunkIfIndex',
85 'hw_trunk_entry' => 'hwTrunkValidEntry',
86  
87 # HUAWEI-L2IF-MIB::hwL2IfTable
88 'hw_l2if_port_idx' => 'hwL2IfPortIfIndex',
89  
90 # HUAWEI-L2MAM-MIB::hwdbDynFdbTable, indexed MAC.VLAN.VSI name; the
91 # value is an ifIndex
92 'hw_dyn_fdb_port' => 'hwDynFdbPort',
93  
94 # HUAWEI-POE-MIB::hwPoePortTable
95 'hw_peth_port_admin' => 'hwPoePortEnable',
96 'hw_peth_port_status' => 'hwPoePortPowerStatus',
97 'hw_peth_port_class' => 'hwPoePortPdClass',
98 'hw_peth_port_power' => 'hwPoePortConsumingPower',
99  
100 # HUAWEI-POE-MIB::hwPoeSlotTable
101 'peth_power_watts' => 'hwPoeSlotMaximumPower',
102 'peth_power_consumption' => 'hwPoeSlotConsumingPower',
103 'peth_power_threshold' => 'hwPoeSlotPowerUtilizationThreshold',
104  
105 # HUAWEI-ENTITY-EXTENT-MIB::hwFanStatusTable
106 'hw_fan_state' => 'hwEntityFanState',
107 'hw_fan_descr' => 'hwEntityFanDesc',
108  
109 # HUAWEI-ENTITY-EXTENT-MIB::hwPwrStatusTable
110 'hw_pwr_state' => 'hwEntityPwrState',
111 'hw_pwr_descr' => 'hwEntityPwrDesc',
112  
113 # HUAWEI-SYS-MAN-MIB::hwPatchFileTable
114 'hw_patch_version' => 'hwPatchVersion',
115  
116 # HUAWEI-MSTP-MIB::hwMstpPortTable, indexed <instance>.<port>: a bridge
117 # port on VRP 5, an ifIndex on VRP 8
118 'hw_mstp_p_edge' => 'hwMstpiStpPortEdgeport',
119 );
120  
121 %MUNGE = (
122 %SNMP::Info::Layer3::MUNGE,
123 %SNMP::Info::IEEE802dot3ad::MUNGE,
124 'hw_peth_port_admin' => \&SNMP::Info::Layer3::Huawei::munge_hw_peth_admin,
125 'peth_power_watts' => \&SNMP::Info::Layer3::Huawei::munge_hw_peth_power,
126 'peth_power_consumption' =>
127 \&SNMP::Info::Layer3::Huawei::munge_hw_peth_power,
128 'hw_peth_port_status' =>
129 \&SNMP::Info::Layer3::Huawei::munge_hw_peth_status,
130 'hw_peth_port_class' => \&SNMP::Info::Layer3::Huawei::munge_hw_peth_class,
131 );
132  
133 sub vendor {
134 return "huawei";
135 }
136  
137 sub os {
138 my $huawei = shift;
139 my $descr = $huawei->description();
140  
141 if ( defined ($descr) && $descr =~ /\b(VRP)\b/ ) {
142 return $1;
143 }elsif ( defined ($descr) && $descr =~ /\b(YunShan OS)\b/i ) {
144 return "YunShan OS";
145 } else {
146 return "huawei";
147 }
148 }
149  
150 sub os_ver {
151 my $huawei = shift;
152  
153 my $patchstr = $huawei->_os_patch();
154 # Prefer extracting uniform V600R023C10SPC500-style version strings from sysDescr first
155 # we trim the base kernel version like 5.170 to make the netdisco inventory page nicer
156 my $descr = $huawei->description();
157 if ( defined $descr && $descr =~ /(V\d{3}[A-Za-z0-9]+)/ ) {
158 return $patchstr ? ($1 . " ".$patchstr) : $1;
159 #return $1 + $patchstr;
160 }else{
161 # if there is no version in the V\d{3}-style, use the old approach of returning more
162 return $huawei->_os_ver_legacy();
163 }
164 }
165  
166 sub _os_patch {
167 my $huawei = shift;
168  
169 # Patches are based on the release, e.g.
170 # os_ver hw_patch_version
171 # V600R023C10SPC500 -> V600R023HP1501 -> Hot Patch 1501
172 # V200R011C10SPC600 -> V200R011SPC102 -> Service Pack Collection 102
173 # V200R011C10SPC600 -> V200R011SPH033 -> Special Patch Hotfix 033
174 # V200R022C00SPC100 -> V200R022CO0SPC100B189 -> APs are somehow different, this is AP Build 189 - just use B189
175 # V200R023C00SPC100 -> V200R023CO0SPH180 -> APs without baseline in patch name, use SPH180
176 #
177 # we only return the last segment like HP1501
178  
179 my $patches = $huawei->hw_patch_version();
180  
181 # hwPatchTable.hwPatchEntry.hwPatchVersion should only return one row (at a somewhat random-looking index),
182 # but we join up multiple results just in case
183 my $patchstr = join ' ',
184 map {
185 my $s = $_;
186 $s =~ s/^V\d{3}R\d{3}//; # drop VRP prefix
187 $s =~ s/.*?([A-Z]+\d+)$/$1/; # only use last suffix to skip the extra bits in APs
188 $s;
189 } values %$patches;
190  
191 return $patchstr;
192 }
193  
194  
195 sub _os_ver_legacy {
196 my $huawei = shift;
197  
198 my $entity_os = $huawei->entity_derived_os_ver();
199 if ( defined $entity_os and $entity_os !~ /^\s*$/ ) {
200 return $entity_os;
201 }
202  
203 my $descr = $huawei->description();
204 my $os_ver = undef;
205  
206 if (defined ($descr) && $descr =~ /Version\s # Start match on Version string
207 ([\d\.]+) # Capture the primary version in 1
208 ,? # There may be a comma
209 \s # Always a space
210 (?:Release|Feature)? # Don't capture stanza if present
211 (?:\(\w+)? # If paren & model don't capture
212 \s # Always a space
213 (\w+) # If 2nd part of version capture in 2
214 /xi
215 )
216 {
217 $os_ver = $2 ? "$1 $2" : $1;
218 }
219  
220 return $os_ver;
221 }
222  
223 sub mac {
224 my $huawei = shift;
225  
226 return $huawei->b_mac();
227 }
228  
229 sub serial {
230 my $huawei = shift;
231  
232 my $serial = $huawei->SUPER::serial();
233 return $serial if defined $serial and $serial !~ /^\s*$/;
234  
235 my $e_class = $huawei->e_class() || {};
236 my $e_serial = $huawei->e_serial() || {};
237  
238 # Some models (AC6605) leave the chassis serial empty and report it
239 # on the main board, which has the lowest module index.
240 foreach my $iid ( sort { $a <=> $b } keys %$e_class ) {
241 next unless $e_class->{$iid} eq 'module';
242 my $board_serial = $e_serial->{$iid};
243 return $board_serial
244 if defined $board_serial and $board_serial !~ /^\s*$/;
245 }
246 return;
247 }
248  
249 sub i_ignore {
250 my $huawei = shift;
251 my $partial = shift;
252  
253 my $interfaces = $huawei->interfaces($partial) || {};
254  
255 my %i_ignore;
256 foreach my $if ( keys %$interfaces ) {
257  
258 # lo0 etc
259 if ( $interfaces->{$if} =~ /\b(inloopback|console)\d*\b/ix ) {
260 $i_ignore{$if}++;
261 }
262 }
263 return \%i_ignore;
264 }
265  
266 sub bp_index {
267 my $huawei = shift;
268  
269 my $hw_index = $huawei->hw_l2if_port_idx();
270 return $hw_index
271 if ( ref {} eq ref $hw_index and scalar keys %$hw_index );
272  
273 return $huawei->SUPER::bp_index();
274 }
275  
276 # Huawei switches serve no dot1qTpFdbTable; hwdbDynFdbTable gives each
277 # learned address its VLAN. Configured addresses, such as authenticated
278 # clients, appear only in dot1dTpFdbTable, so those are kept from there
279 # without a VLAN.
280 sub _hw_fdb {
281 my ( $huawei, $partial ) = @_;
282  
283 my $rows = $huawei->hw_dyn_fdb_port($partial) || {};
284 return unless scalar keys %$rows;
285 my %port_of_ifindex = reverse %{ $huawei->bp_index() || {} };
286  
287 my ( %port, %mac, %vlan );
288 foreach my $idx ( keys %$rows ) {
289 my ( $addr, $vid ) = _hw_fdb_index($idx);
290 next unless defined $addr;
291 $mac{$idx} = $addr;
292 $vlan{$idx} = $vid;
293 my $bp = $port_of_ifindex{ $rows->{$idx} };
294 $port{$idx} = $bp if defined $bp;
295 }
296 return { port => \%port, mac => \%mac, vlan => \%vlan }
297 if defined $partial;
298  
299 my %learned = map { $_ => 1 } values %mac;
300 my $d1d_mac = $huawei->orig_fw_mac() || {};
301 my $d1d_port = $huawei->orig_fw_port() || {};
302 foreach my $idx ( keys %$d1d_mac ) {
303 my $addr = $d1d_mac->{$idx};
304 next if !defined $addr or $learned{$addr};
305 $mac{$idx} = $addr;
306 $port{$idx} = $d1d_port->{$idx} if defined $d1d_port->{$idx};
307 }
308 return { port => \%port, mac => \%mac, vlan => \%vlan };
309 }
310  
311 # MAC (six sub-identifiers), VLAN, then the VSI name with its length
312 # prefix: "1.48" ("0") on VRP 5, "0" (empty) on VRP 8.
313 sub _hw_fdb_index {
314 my $idx = shift;
315  
316 my @oids = split /\./, $idx;
317 return unless @oids >= 8;
318 my @mac = splice @oids, 0, 6;
319 my $vlan = shift @oids;
320 my $len = shift @oids;
321 return unless $len == scalar @oids;
322  
323 return ( join( ':', map { sprintf '%02x', $_ } @mac ), $vlan );
324 }
325  
326 sub qb_fw_port {
327 my ( $huawei, $partial ) = @_;
328  
329 my $fdb = $huawei->_hw_fdb($partial)
330 or return $huawei->SUPER::qb_fw_port($partial);
331 return $fdb->{port};
332 }
333  
334 sub qb_fw_mac {
335 my ( $huawei, $partial ) = @_;
336  
337 my $fdb = $huawei->_hw_fdb($partial)
338 or return $huawei->SUPER::qb_fw_mac($partial);
339 return $fdb->{mac};
340 }
341  
342 sub qb_fw_vlan {
343 my ( $huawei, $partial ) = @_;
344  
345 my $fdb = $huawei->_hw_fdb($partial)
346 or return $huawei->SUPER::qb_fw_vlan($partial);
347 return $fdb->{vlan};
348 }
349  
350 sub i_duplex {
351 my $huawei = shift;
352 my $partial = shift;
353  
354 my $hw_duplex = $huawei->hw_eth_duplex($partial);
355 return $hw_duplex
356 if ( ref {} eq ref $hw_duplex and scalar keys %$hw_duplex );
357  
358 return $huawei->SUPER::i_duplex($partial);
359 }
360  
361 sub i_duplex_admin {
362 my $huawei = shift;
363 my $partial = shift;
364  
365 my $hw_duplex_admin = $huawei->hw_eth_duplex($partial) || {};
366 my $hw_auto = $huawei->hw_eth_auto($partial) || {};
367  
368 my %i_duplex_admin;
369 foreach my $if ( keys %$hw_duplex_admin ) {
370 my $duplex = $hw_duplex_admin->{$if};
371 next unless defined $duplex;
372 my $auto = $hw_auto->{$if} || 'disabled';
373  
374 my $string = 'other';
375 $string = 'half' if ( $duplex =~ /half/i and $auto =~ /disabled/i );
376 $string = 'full' if ( $duplex =~ /full/i and $auto =~ /disabled/i );
377 $string = 'auto' if $auto =~ /enabled/i;
378  
379 $i_duplex_admin{$if} = $string;
380 }
381 return \%i_duplex_admin;
382 }
383  
384 sub agg_ports {
385 my $huawei = shift;
386  
387 # First use proprietary MIB for broader implementation across
388 # devices type / os and no xref of hwL2IfPortIfIndex
389 my $masters = $huawei->hw_trunk_if_idx();
390 my $slaves = $huawei->hw_trunk_entry();
391  
392 my $ret = {};
393  
394 if ( ref {} eq ref $masters
395 and scalar keys %$masters
396 and ref {} eq ref $slaves
397 and scalar keys %$slaves )
398 {
399 foreach my $s ( keys %$slaves ) {
400 next if $slaves->{$s} ne 'valid';
401 my ( $trunk, $sl_idx ) = split( /\./, $s );
402 foreach my $m ( keys %$masters ) {
403 next unless $m == $trunk;
404 next unless defined $masters->{$m};
405 $ret->{$sl_idx} = $masters->{$m};
406 last;
407 }
408 }
409 return $ret;
410 }
411  
412 # If for some reason we don't get the info, try IEEE8023-LAG-MIB
413 return $huawei->agg_ports_lag();
414 }
415  
416 sub _agg_ports_walked {
417 return qw/hw_trunk_if_idx hw_trunk_entry ad_lag_ports hw_l2if_port_idx
418 bp_index/;
419 }
420  
421 # The standard IEEE 802.af POWER-ETHERNET-MIB has an index of module.port
422 # The HUAWEI-POE-MIB only indexes by ifIndex, we need to match the standard
423 # for so method calls across classes work the same
424 #
425 sub peth_port_ifindex {
426 my $huawei = shift;
427  
428 my $peth_port_status = $huawei->hw_peth_port_status() || {};
429 my $peth_port_slot = $huawei->hw_phy_port_slot() || {};
430 my $i_descr = $huawei->i_description() || {};
431  
432 my $peth_port_ifindex = {};
433  
434 foreach my $i ( keys %$peth_port_status ) {
435 my $slot = 0;
436 if ( exists $peth_port_slot->{$i}
437 && defined $peth_port_slot->{$i} )
438 {
439 $slot = $peth_port_slot->{$i};
440 }
441 elsif ( exists $i_descr->{$i}
442 && $i_descr->{$i} =~ /(\d+)(?:\/\d+){2,3}$/x )
443 {
444 $slot = $1;
445 }
446 $peth_port_ifindex->{"$slot.$i"} = $i;
447 }
448 return $peth_port_ifindex;
449 }
450  
451 sub peth_port_admin {
452 my $huawei = shift;
453  
454 my $port_admin = $huawei->hw_peth_port_admin() || {};
455 my $port_ifindex = $huawei->peth_port_ifindex() || {};
456  
457 my $peth_port_admin = {};
458  
459 foreach my $idx ( keys %$port_ifindex ) {
460 my $ifindex = $port_ifindex->{$idx};
461 my $admin = $port_admin->{$ifindex};
462 next unless $admin;
463  
464 $peth_port_admin->{$idx} = $admin;
465 }
466 return $peth_port_admin;
467 }
468  
469 sub peth_port_status {
470 my $huawei = shift;
471  
472 my $port_status = $huawei->hw_peth_port_status() || {};
473 my $port_ifindex = $huawei->peth_port_ifindex() || {};
474  
475 my $peth_port_status = {};
476  
477 foreach my $idx ( keys %$port_ifindex ) {
478 my $ifindex = $port_ifindex->{$idx};
479 my $status = $port_status->{$ifindex};
480 next unless $status;
481  
482 $peth_port_status->{$idx} = $status;
483 }
484 return $peth_port_status;
485 }
486  
487 sub peth_port_class {
488 my $huawei = shift;
489  
490 my $port_class = $huawei->hw_peth_port_class() || {};
491 my $port_ifindex = $huawei->peth_port_ifindex() || {};
492  
493 my $peth_port_class = {};
494  
495 foreach my $idx ( keys %$port_ifindex ) {
496 my $ifindex = $port_ifindex->{$idx};
497 my $class = $port_class->{$ifindex};
498 next unless $class;
499  
500 $peth_port_class->{$idx} = $class;
501 }
502 return $peth_port_class;
503 }
504  
505 sub peth_port_power {
506 my $huawei = shift;
507  
508 my $port_power = $huawei->hw_peth_port_power() || {};
509 my $port_ifindex = $huawei->peth_port_ifindex() || {};
510  
511 my $peth_port_power = {};
512  
513 foreach my $idx ( keys %$port_ifindex ) {
514 my $ifindex = $port_ifindex->{$idx};
515 my $power = $port_power->{$ifindex};
516 next unless defined $power;
517  
518 $peth_port_power->{$idx} = $power;
519 }
520 return $peth_port_power;
521 }
522  
523 sub peth_port_neg_power {
524 my $huawei = shift;
525  
526 my $peth_port_status = $huawei->peth_port_status() || {};
527 my $peth_port_class = $huawei->peth_port_class() || {};
528 my $port_ifindex = $huawei->peth_port_ifindex() || {};
529  
530 my $huaweimax = {
531 'class0' => 12950,
532 'class1' => 3840,
533 'class2' => 6490,
534 'class3' => 12950,
535 'class4' => 25500
536 };
537  
538 my $peth_port_neg_power = {};
539  
540 foreach my $idx ( keys %$port_ifindex ) {
541 if ( $peth_port_status->{$idx} eq 'deliveringPower' ) {
542 $peth_port_neg_power->{$idx}
543 = $huaweimax->{ $peth_port_class->{$idx} };
544 }
545 }
546 return $peth_port_neg_power;
547 }
548  
549 sub fan {
550 my $huawei = shift;
551  
552 my $fan = $huawei->hw_fan_descr() || {};
553 my $state = $huawei->hw_fan_state() || {};
554  
555 if ( scalar keys %$state ) {
556 my @messages = ();
557  
558 foreach my $k ( keys %$state ) {
559 next if $state->{$k} and $state->{$k} eq 'normal';
560 my ($slot, $num) = split(/\./, $k);
561 my $descr = "Slot $slot,Fan $num";
562 $descr = $fan->{$k} if ($fan->{$k});
563  
564 push @messages, "$descr: $state->{$k}";
565 }
566  
567 push @messages, ( ( scalar keys %$state ) . " fans OK" )
568 if scalar @messages == 0;
569  
570 return ( join ", ", @messages );
571 }
572 return;
573 }
574  
575 sub ps1_status {
576 my $huawei = shift;
577  
578 my $pwr_state = $huawei->hw_pwr_state() || {};
579 my $pwr_descr = $huawei->hw_pwr_descr() || {};
580  
581 my $ret = "";
582 my $s = "";
583 foreach my $i ( sort keys %$pwr_state ) {
584 my ( $slot, $num ) = split( /\./, $i );
585 next unless $num == 1;
586 my $descr = "Slot $slot,PS $num";
587 $descr = $pwr_descr->{$i} if ($pwr_descr->{$i});
588  
589 $ret .= $s . $descr . ": " . $pwr_state->{$i};
590 $s = ", ";
591 }
592 return if ( $s eq "" );
593 return $ret;
594 }
595  
596 sub ps2_status {
597 my $huawei = shift;
598  
599 my $pwr_state = $huawei->hw_pwr_state() || {};
600 my $pwr_descr = $huawei->hw_pwr_descr() || {};
601  
602 my $ret = "";
603 my $s = "";
604 foreach my $i ( sort keys %$pwr_state ) {
605 my ( $slot, $num ) = split( /\./, $i );
606 next unless $num == 2;
607 my $descr = "Slot $slot,PS $num";
608 $descr = $pwr_descr->{$i} if ($pwr_descr->{$i});
609  
610 $ret .= $s . $descr . ": " . $pwr_state->{$i};
611 $s = ", ";
612 }
613 return if ( $s eq "" );
614 return $ret;
615 }
616  
617 sub i_mtu {
618 my $huawei = shift;
619  
620 my $mtus = $huawei->SUPER::i_mtu() || {};
621 my $frames = $huawei->hw_eth_frame_len() || {};
622  
623 foreach my $idx ( keys %$mtus ) {
624 my $frame_sz = $frames->{$idx};
625 next unless $frame_sz;
626  
627 $mtus->{$idx} = $frame_sz;
628 }
629 return $mtus;
630 }
631  
632 sub i_edgeport_admin {
633 my $huawei = shift;
634 my $partial = shift;
635  
636 my $edge = $huawei->hw_mstp_p_edge() || {};
637 my %cist_edge;
638 foreach my $row ( keys %$edge ) {
639 my ( $instance, $port ) = split /\./, $row, 2;
640 $cist_edge{$port} = $edge->{$row} if $instance eq '0';
641 }
642 return $huawei->SUPER::i_edgeport_admin($partial)
643 unless keys %cist_edge;
644  
645 my $port_index = $huawei->_hw_mstp_port_index($partial);
646 return {} unless $port_index;
647  
648 my $default
649 = SNMP::Info::Bridge::_stp_enabled( $huawei->hw_mstp_edge_default() );
650 return SNMP::Info::Bridge::_stp_edge_by_ifindex( $huawei, \%cist_edge,
651 $partial, sub { _hw_edge_enabled( shift, $default ) }, $port_index );
652 }
653  
654 # hwMstpiPortIndex is a bridge port on VRP 5 but an ifIndex on VRP 8
655 # CloudEngine, though the MIB says bridge port. Only CloudEngine has shown
656 # the VRP 8 form, so other VRP 8 families and an unknown VRP version stay
657 # empty: a guessed form puts values on the wrong ports.
658 sub _hw_mstp_port_index {
659 my ( $huawei, $partial ) = @_;
660  
661 my $vrp = $huawei->_vrp_major();
662 return unless defined $vrp;
663 return $huawei->bp_index($partial) if $vrp < 8;
664  
665 my $id = $huawei->id() || '';
666 return unless $id =~ /^\.?1\.3\.6\.1\.4\.1\.2011\.2\.239\./;
667  
668 my $i_index = $huawei->i_index($partial) || {};
669 return { map { $_ => $_ } keys %$i_index };
670 }
671  
672 # The sysDescr "Version N." that _os_ver_legacy reads; the V200R010-style
673 # string os_ver prefers is the same on VRP 5 and VRP 8. YunShan OS
674 # numbers its own versions, so it has no VRP major.
675 sub _vrp_major {
676 my $huawei = shift;
677  
678 my $os = $huawei->os();
679 my $descr = $huawei->description();
680 return unless defined $os and $os eq 'VRP';
681 return unless defined $descr and $descr =~ /Version\s(\d+)\./i;
682 return $1;
683 }
684  
685 # hwMstpiStpPortEdgeport is disable(1), enable(2), undo(3), the reverse of
686 # EnabledStatus, so _stp_enabled would misread it; undo takes the device
687 # default from hwMstpEdgedPortDefault
688 sub _hw_edge_enabled {
689 my ( $value, $default ) = @_;
690 return unless defined $value;
691  
692 return 1 if $value =~ /^(?:enable|2)$/;
693 return 0 if $value =~ /^(?:disable|1)$/;
694 return $default if $value =~ /^(?:undo|3)$/;
695 return;
696 }
697  
698 sub munge_hw_peth_admin {
699 my $admin = shift;
700  
701 $admin =~ s/enabled/true/;
702 $admin =~ s/disabled/false/;
703 return $admin;
704 }
705  
706 sub munge_hw_peth_power {
707 my $pwr = shift;
708  
709 $pwr = $pwr / 1000;
710 return sprintf( "%.0f", $pwr );
711 }
712  
713 sub munge_hw_peth_class {
714 my $pwr = shift;
715  
716 return "class$pwr";
717 }
718  
719 # The values are from the MIB reference guide
720 sub munge_hw_peth_status {
721 my $pwr = shift;
722  
723 # The status is an octet string rather than enum
724 # so use regex rather than hash lookup
725 $pwr = 'disabled' if $pwr =~ /Disabled/i;
726 $pwr = 'searching' if $pwr =~ /(Powering|Power-ready|Detecting)/ix;
727 $pwr = 'deliveringPower' if $pwr =~ /Powered/i;
728 $pwr = 'fault' if $pwr =~ /fault/i;
729  
730 return $pwr;
731 }
732  
733 1;
734  
735 __END__