Use Uint32.saturatedOf() 17/105117/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 31 Mar 2023 14:39:13 +0000 (16:39 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 31 Mar 2023 14:41:11 +0000 (16:41 +0200)
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 <robert.varga@pantheon.tech>
(cherry picked from commit cc71ea45a8e373d50b063a61966245c5e90d4aa8)

bgp/openconfig-state/src/main/java/org/opendaylight/protocol/bgp/state/PeerGroupUtil.java

index f3666bfc9ba5a76df2cf4c9ba79439968e691d8a..ed6f322d70a0b8073a56c2e27b0ef9c0b7929e40 100644 (file)
@@ -63,8 +63,8 @@ public final class PeerGroupUtil {
     public static @NonNull PeerGroup buildPeerGroupState(final @NonNull String groupId,
             final @NonNull List<BGPPeerState> 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;
-    }
-
 }