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 java.util.List;
12 import java.util.stream.Collectors;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
16 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.peer.group.PeerGroup;
17 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.peer.group.PeerGroupBuilder;
18 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.PeerGroups;
19 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.PeerGroupsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.PeerGroupStateAugmentation;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.PeerGroupStateAugmentationBuilder;
24 * Util for create OpenConfig Peer group with corresponding openConfig state.
26 public final class PeerGroupUtil {
27 private PeerGroupUtil() {
32 * Build Openconfig PeerGroups containing Peer group stats from a list of BGPPeerGroupState.
34 * @param bgpStateConsumer providing BGPPeerGroupState
35 * @return PeerGroups containing Peer group stats
38 public static @Nullable PeerGroups buildPeerGroups(final @NonNull List<BGPPeerState> bgpStateConsumer) {
39 final Map<String, List<BGPPeerState>> peerGroups = bgpStateConsumer.stream()
40 .filter(state -> state.getGroupId() != null)
41 .collect(Collectors.groupingBy(BGPPeerState::getGroupId));
42 if (peerGroups.isEmpty()) {
46 final List<PeerGroup> peerGroupsList = peerGroups.entrySet().stream()
47 .map(entry -> buildPeerGroupState(entry.getKey(), entry.getValue()))
48 .collect(Collectors.toList());
49 return new PeerGroupsBuilder().setPeerGroup(peerGroupsList).build();
53 * Build Openconfig PeerGroup containing Peer group stats from BGPPeerGroupState.
55 * @param groupId Peer group Id
56 * @param groups providing state of the group
57 * @return PeerGroups containing Peer group stats
59 public static @NonNull PeerGroup buildPeerGroupState(final @NonNull String groupId,
60 final @NonNull List<BGPPeerState> groups) {
61 final PeerGroupStateAugmentation groupState = new PeerGroupStateAugmentationBuilder()
62 .setTotalPrefixes(groups.stream().mapToLong(BGPPeerState::getTotalPrefixes).sum())
63 .setTotalPaths(groups.stream().mapToLong(BGPPeerState::getTotalPathsCount).sum())
66 return new PeerGroupBuilder()
67 .setPeerGroupName(groupId)
68 .setState(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group
69 .StateBuilder().addAugmentation(PeerGroupStateAugmentation.class, groupState).build()).build();