BGPCEP-578: Extended peer-group support
[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
9 package org.opendaylight.protocol.bgp.state;
10
11 import java.util.List;
12 import java.util.Map;
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.rev180321.PeerGroupStateAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180321.PeerGroupStateAugmentationBuilder;
23
24 /**
25  * Util for create OpenConfig Peer group with corresponding openConfig state.
26  */
27 public final class PeerGroupUtil {
28     private PeerGroupUtil() {
29         throw new UnsupportedOperationException();
30     }
31
32     /**
33      * Build Openconfig PeerGroups containing Peer group stats from a list of BGPPeerGroupState.
34      *
35      * @param bgpStateConsumer providing BGPPeerGroupState
36      * @return PeerGroups containing Peer group stats
37      */
38     @Nullable
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()) {
44             return null;
45         }
46
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();
51     }
52
53     /**
54      * Build Openconfig PeerGroup containing Peer group stats from BGPPeerGroupState.
55      *
56      * @param groupId Peer group Id
57      * @param groups  providing state of the group
58      * @return PeerGroups containing Peer group stats
59      */
60     @Nonnull
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())
65                 .build();
66
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();
71     }
72 }