2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.state;
10 import com.google.common.collect.ImmutableMap;
11 import java.util.List;
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;
27 * Util for create OpenConfig Peer group with corresponding openConfig state.
29 public final class PeerGroupUtil {
30 private PeerGroupUtil() {
35 * Build Openconfig PeerGroups containing Peer group stats from a list of BGPPeerGroupState.
37 * @param bgpStateConsumer providing BGPPeerGroupState
38 * @return PeerGroups containing Peer group stats
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()) {
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())))
57 * Build Openconfig PeerGroup containing Peer group stats from BGPPeerGroupState.
59 * @param groupId Peer group Id
60 * @param groups providing state of the group
61 * @return PeerGroups containing Peer group stats
63 public static @NonNull PeerGroup buildPeerGroupState(final @NonNull String groupId,
64 final @NonNull List<BGPPeerState> groups) {
65 final PeerGroupStateAugmentation groupState = new PeerGroupStateAugmentationBuilder()
66 .setTotalPrefixes(saturatedUint32(groups.stream().mapToLong(BGPPeerState::getTotalPrefixes).sum()))
67 .setTotalPaths(saturatedUint32(groups.stream().mapToLong(BGPPeerState::getTotalPathsCount).sum()))
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();
76 // FIXME: remove this with YANGTOOLS-5.0.7+
77 private static Uint32 saturatedUint32(final long value) {
78 return value < 4294967295L ? Uint32.valueOf(value) : Uint32.MAX_VALUE;