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