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