Use Uint32.saturatedOf()
[bgpcep.git] / bgp / openconfig-state / src / main / java / org / opendaylight / protocol / bgp / state / PeerGroupUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.state;
9
10 import com.google.common.collect.ImmutableMap;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.function.Function;
14 import java.util.stream.Collectors;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
18 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.peer.group.PeerGroup;
19 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.peer.group.PeerGroupBuilder;
20 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.PeerGroups;
21 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.PeerGroupsBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.network.instance.protocol.PeerGroupStateAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.network.instance.protocol.PeerGroupStateAugmentationBuilder;
24 import org.opendaylight.yangtools.yang.common.Uint32;
25
26 /**
27  * Util for create OpenConfig Peer group with corresponding openConfig state.
28  */
29 public final class PeerGroupUtil {
30     private PeerGroupUtil() {
31         // Hidden on purpose
32     }
33
34     /**
35      * Build Openconfig PeerGroups containing Peer group stats from a list of BGPPeerGroupState.
36      *
37      * @param bgpStateConsumer providing BGPPeerGroupState
38      * @return PeerGroups containing Peer group stats
39      */
40
41     public static @Nullable PeerGroups buildPeerGroups(final @NonNull List<BGPPeerState> bgpStateConsumer) {
42         final Map<String, List<BGPPeerState>> peerGroups = bgpStateConsumer.stream()
43                 .filter(state -> state.getGroupId() != null)
44                 .collect(Collectors.groupingBy(BGPPeerState::getGroupId));
45         if (peerGroups.isEmpty()) {
46             return null;
47         }
48
49         return new PeerGroupsBuilder()
50                 .setPeerGroup(peerGroups.entrySet().stream()
51                     .map(entry -> buildPeerGroupState(entry.getKey(), entry.getValue()))
52                     .collect(ImmutableMap.toImmutableMap(PeerGroup::key, Function.identity())))
53                 .build();
54     }
55
56     /**
57      * Build Openconfig PeerGroup containing Peer group stats from BGPPeerGroupState.
58      *
59      * @param groupId Peer group Id
60      * @param groups  providing state of the group
61      * @return PeerGroups containing Peer group stats
62      */
63     public static @NonNull PeerGroup buildPeerGroupState(final @NonNull String groupId,
64             final @NonNull List<BGPPeerState> groups) {
65         final PeerGroupStateAugmentation groupState = new PeerGroupStateAugmentationBuilder()
66                 .setTotalPrefixes(Uint32.saturatedOf(groups.stream().mapToLong(BGPPeerState::getTotalPrefixes).sum()))
67                 .setTotalPaths(Uint32.saturatedOf(groups.stream().mapToLong(BGPPeerState::getTotalPathsCount).sum()))
68                 .build();
69
70         return new PeerGroupBuilder()
71                 .setPeerGroupName(groupId)
72                 .setState(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group
73                         .StateBuilder().addAugmentation(groupState).build()).build();
74     }
75 }