From: Robert Varga Date: Fri, 31 Mar 2023 14:39:13 +0000 (+0200) Subject: Use Uint32.saturatedOf() X-Git-Tag: v0.19.4~2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=ea0c792364c98451ded28448d30a2905569e788e;p=bgpcep.git Use Uint32.saturatedOf() We are receiving a splat when the sum ends up being negative. While the underlying cause needs to be investigated, we also should be properly defensive. Uint32.saturatedOf() catches negative values and squashes them to 0. JIRA: BGPCEP-1021 Change-Id: Ia376305aa677ebd05a41629c5083f8829891615f Signed-off-by: Robert Varga (cherry picked from commit cc71ea45a8e373d50b063a61966245c5e90d4aa8) --- diff --git a/bgp/openconfig-state/src/main/java/org/opendaylight/protocol/bgp/state/PeerGroupUtil.java b/bgp/openconfig-state/src/main/java/org/opendaylight/protocol/bgp/state/PeerGroupUtil.java index f3666bfc9b..ed6f322d70 100644 --- a/bgp/openconfig-state/src/main/java/org/opendaylight/protocol/bgp/state/PeerGroupUtil.java +++ b/bgp/openconfig-state/src/main/java/org/opendaylight/protocol/bgp/state/PeerGroupUtil.java @@ -63,8 +63,8 @@ public final class PeerGroupUtil { public static @NonNull PeerGroup buildPeerGroupState(final @NonNull String groupId, final @NonNull List groups) { final PeerGroupStateAugmentation groupState = new PeerGroupStateAugmentationBuilder() - .setTotalPrefixes(saturatedUint32(groups.stream().mapToLong(BGPPeerState::getTotalPrefixes).sum())) - .setTotalPaths(saturatedUint32(groups.stream().mapToLong(BGPPeerState::getTotalPathsCount).sum())) + .setTotalPrefixes(Uint32.saturatedOf(groups.stream().mapToLong(BGPPeerState::getTotalPrefixes).sum())) + .setTotalPaths(Uint32.saturatedOf(groups.stream().mapToLong(BGPPeerState::getTotalPathsCount).sum())) .build(); return new PeerGroupBuilder() @@ -72,10 +72,4 @@ public final class PeerGroupUtil { .setState(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group .StateBuilder().addAugmentation(groupState).build()).build(); } - - // FIXME: remove this with YANGTOOLS-5.0.7+ - private static Uint32 saturatedUint32(final long value) { - return value < 4294967295L ? Uint32.valueOf(value) : Uint32.MAX_VALUE; - } - }