BGPCEP-742 Fix BGP NPE filter null BGP State
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / state / BGPStateCollectorImpl.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.rib.impl.state;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Objects;
14 import java.util.stream.Collectors;
15 import javax.annotation.concurrent.GuardedBy;
16 import javax.annotation.concurrent.ThreadSafe;
17 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
18 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
19 import org.opendaylight.protocol.bgp.rib.spi.state.BGPRIBState;
20 import org.opendaylight.protocol.bgp.rib.spi.state.BGPRIBStateConsumer;
21 import org.opendaylight.protocol.bgp.rib.spi.state.BGPStateConsumer;
22 import org.opendaylight.protocol.bgp.rib.spi.state.BGPStateProvider;
23
24 @ThreadSafe
25 public class BGPStateCollectorImpl implements BGPStateProvider, BGPStateConsumer {
26     @GuardedBy("this")
27     private final List<BGPRIBStateConsumer> bgpRibStates = new ArrayList<>();
28     @GuardedBy("this")
29     private final List<BGPPeerStateConsumer> bgpPeerStates = new ArrayList<>();
30
31     @Override
32     public List<BGPRIBState> getRibStats() {
33         synchronized (this.bgpRibStates) {
34             return ImmutableList.copyOf(this.bgpRibStates
35                     .stream()
36                     .map(BGPRIBStateConsumer::getRIBState)
37                     .filter(Objects::nonNull)
38                     .collect(Collectors.toList()));
39         }
40     }
41
42     @Override
43     public List<BGPPeerState> getPeerStats() {
44         synchronized (this.bgpPeerStates) {
45             return ImmutableList.copyOf(this.bgpPeerStates
46                     .stream()
47                     .map(BGPPeerStateConsumer::getPeerState)
48                     .filter(Objects::nonNull)
49                     .collect(Collectors.toList()));
50         }
51     }
52
53     @Override
54     public void bind(final BGPRIBStateConsumer bgpState) {
55         if (bgpState == null) {
56             return;
57         }
58         synchronized (this.bgpRibStates) {
59             this.bgpRibStates.add(bgpState);
60         }
61     }
62
63     @Override
64     public void unbind(final BGPRIBStateConsumer bgpState) {
65         if (bgpState == null) {
66             return;
67         }
68         synchronized (this.bgpRibStates) {
69             this.bgpRibStates.remove(bgpState);
70         }
71     }
72
73     @Override
74     public void bind(final BGPPeerStateConsumer bgpState) {
75         if (bgpState == null) {
76             return;
77         }
78         synchronized (this.bgpPeerStates) {
79             this.bgpPeerStates.add(bgpState);
80         }
81     }
82
83     @Override
84     public void unbind(final BGPPeerStateConsumer bgpState) {
85         if (bgpState == null) {
86             return;
87         }
88         synchronized (this.bgpPeerStates) {
89             this.bgpPeerStates.remove(bgpState);
90         }
91     }
92 }