Use Uint32.saturatedOf()
[bgpcep.git] / bgp / openconfig-state / src / main / java / org / opendaylight / protocol / bgp / state / PeerGroupUtil.java
index 5336470000d88ab0a153384dbbff4fe3f6dd90e7..ed6f322d70a0b8073a56c2e27b0ef9c0b7929e40 100644 (file)
@@ -5,68 +5,71 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.protocol.bgp.state;
 
+import com.google.common.collect.ImmutableMap;
 import java.util.List;
 import java.util.Map;
+import java.util.function.Function;
 import java.util.stream.Collectors;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.peer.group.PeerGroup;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.peer.group.PeerGroupBuilder;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.PeerGroups;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.PeerGroupsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.PeerGroupStateAugmentation;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.PeerGroupStateAugmentationBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.network.instance.protocol.PeerGroupStateAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.network.instance.protocol.PeerGroupStateAugmentationBuilder;
+import org.opendaylight.yangtools.yang.common.Uint32;
 
 /**
  * Util for create OpenConfig Peer group with corresponding openConfig state.
  */
 public final class PeerGroupUtil {
     private PeerGroupUtil() {
-        throw new UnsupportedOperationException();
+        // Hidden on purpose
     }
 
     /**
-     * Build Openconfig PeerGroups containing Peer group stats from a list of BGPPeerGroupState
+     * Build Openconfig PeerGroups containing Peer group stats from a list of BGPPeerGroupState.
      *
      * @param bgpStateConsumer providing BGPPeerGroupState
      * @return PeerGroups containing Peer group stats
      */
-    @Nullable
-    public static PeerGroups buildPeerGroups(@Nonnull final List<BGPPeerState> bgpStateConsumer) {
+
+    public static @Nullable PeerGroups buildPeerGroups(final @NonNull List<BGPPeerState> bgpStateConsumer) {
         final Map<String, List<BGPPeerState>> peerGroups = bgpStateConsumer.stream()
-            .filter(state -> state.getGroupId() != null)
-            .collect(Collectors.groupingBy(BGPPeerState::getGroupId));
+                .filter(state -> state.getGroupId() != null)
+                .collect(Collectors.groupingBy(BGPPeerState::getGroupId));
         if (peerGroups.isEmpty()) {
             return null;
         }
 
-        final List<PeerGroup> peerGroupsList = peerGroups.entrySet().stream()
-            .map(entry -> buildPeerGroupState(entry.getKey(), entry.getValue()))
-            .collect(Collectors.toList());
-        return new PeerGroupsBuilder().setPeerGroup(peerGroupsList).build();
+        return new PeerGroupsBuilder()
+                .setPeerGroup(peerGroups.entrySet().stream()
+                    .map(entry -> buildPeerGroupState(entry.getKey(), entry.getValue()))
+                    .collect(ImmutableMap.toImmutableMap(PeerGroup::key, Function.identity())))
+                .build();
     }
 
     /**
-     * Build Openconfig PeerGroup containing Peer group stats from BGPPeerGroupState
+     * Build Openconfig PeerGroup containing Peer group stats from BGPPeerGroupState.
      *
      * @param groupId Peer group Id
-     * @param groups providing state of the group
+     * @param groups  providing state of the group
      * @return PeerGroups containing Peer group stats
      */
-    @Nonnull
-    public static PeerGroup buildPeerGroupState(@Nonnull final String groupId, @Nonnull List<BGPPeerState> groups) {
+    public static @NonNull PeerGroup buildPeerGroupState(final @NonNull String groupId,
+            final @NonNull List<BGPPeerState> groups) {
         final PeerGroupStateAugmentation groupState = new PeerGroupStateAugmentationBuilder()
-            .setTotalPrefixes(groups.stream().mapToLong(BGPPeerState::getTotalPrefixes).sum())
-            .setTotalPaths(groups.stream().mapToLong(BGPPeerState::getTotalPathsCount).sum())
-            .build();
+                .setTotalPrefixes(Uint32.saturatedOf(groups.stream().mapToLong(BGPPeerState::getTotalPrefixes).sum()))
+                .setTotalPaths(Uint32.saturatedOf(groups.stream().mapToLong(BGPPeerState::getTotalPathsCount).sum()))
+                .build();
 
         return new PeerGroupBuilder()
-            .setPeerGroupName(groupId)
-            .setState(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.
-                StateBuilder().addAugmentation(PeerGroupStateAugmentation.class, groupState).build()).build();
+                .setPeerGroupName(groupId)
+                .setState(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group
+                        .StateBuilder().addAugmentation(groupState).build()).build();
     }
 }