BUG-5032: BGP Operational State
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / state / BGPPeerStateImpl.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.rib.impl.state;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.concurrent.atomic.LongAdder;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import javax.annotation.concurrent.GuardedBy;
23 import org.opendaylight.protocol.bgp.rib.DefaultRibReference;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPMessagesListener;
25 import org.opendaylight.protocol.bgp.rib.impl.state.peer.PrefixesInstalledCounters;
26 import org.opendaylight.protocol.bgp.rib.impl.state.peer.PrefixesReceivedCounters;
27 import org.opendaylight.protocol.bgp.rib.impl.state.peer.PrefixesSentCounters;
28 import org.opendaylight.protocol.bgp.rib.spi.state.BGPAfiSafiState;
29 import org.opendaylight.protocol.bgp.rib.spi.state.BGPErrorHandlingState;
30 import org.opendaylight.protocol.bgp.rib.spi.state.BGPGracelfulRestartState;
31 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerMessagesState;
32 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
33 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
40 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.binding.Notification;
42
43 public abstract class BGPPeerStateImpl extends DefaultRibReference implements BGPPeerState, BGPAfiSafiState,
44     BGPGracelfulRestartState, BGPErrorHandlingState, BGPPeerMessagesState, BGPPeerStateConsumer, BGPMessagesListener {
45     private static final long NONE = 0L;
46     private final IpAddress neighborAddress;
47     private final Set<TablesKey> afiSafisAdvertized;
48     private final Set<TablesKey> afiSafisGracefulAdvertized;
49     private final Set<TablesKey> afiSafisGracefulReceived = new HashSet<>();
50     private final LongAdder updateSentCounter = new LongAdder();
51     private final LongAdder notificationSentCounter = new LongAdder();
52     private final LongAdder updateReceivedCounter = new LongAdder();
53     private final LongAdder notificationReceivedCounter = new LongAdder();
54     private final LongAdder erroneousUpdate = new LongAdder();
55     private final String groupId;
56
57     @GuardedBy("this")
58     private final Map<TablesKey, PrefixesSentCounters> prefixesSent = new HashMap<>();
59     @GuardedBy("this")
60     private PrefixesReceivedCounters prefixesReceived;
61     @GuardedBy("this")
62     private PrefixesInstalledCounters prefixesInstalled;
63     @GuardedBy("this")
64     private boolean localRestarting;
65     @GuardedBy("this")
66     private int peerRestartTime;
67     @GuardedBy("this")
68     private boolean peerRestarting;
69
70     public BGPPeerStateImpl(@Nonnull final KeyedInstanceIdentifier<Rib, RibKey> instanceIdentifier,
71         @Nullable final String groupId, @Nonnull final IpAddress neighborAddress,
72         @Nonnull final Set<TablesKey> afiSafisAdvertized,
73         @Nonnull final Set<TablesKey> afiSafisGracefulAdvertized) {
74         super(instanceIdentifier);
75         this.neighborAddress = Preconditions.checkNotNull(neighborAddress);
76         this.groupId = groupId;
77         this.afiSafisAdvertized = Preconditions.checkNotNull(afiSafisAdvertized);
78         this.afiSafisGracefulAdvertized = Preconditions.checkNotNull(afiSafisGracefulAdvertized);
79     }
80
81     @Override
82     public final String getGroupId() {
83         return this.groupId;
84     }
85
86     @Override
87     public final IpAddress getNeighborAddress() {
88         return this.neighborAddress;
89     }
90
91     @Override
92     public final synchronized long getTotalPrefixes() {
93         if (this.prefixesInstalled == null) {
94             return NONE;
95         }
96         return this.prefixesInstalled.getTotalPrefixesInstalled();
97     }
98
99     @Override
100     public final BGPPeerMessagesState getBGPPeerMessagesState() {
101         return this;
102     }
103
104     @Override
105     public final BGPGracelfulRestartState getBGPGracelfulRestart() {
106         return this;
107     }
108
109     @Override
110     public final synchronized boolean isAfiSafiSupported(final TablesKey tablesKey) {
111         return this.prefixesReceived != null && this.prefixesReceived.isSupported(tablesKey) &&
112             this.afiSafisAdvertized.contains(tablesKey);
113     }
114
115     @Override
116     public final synchronized long getPrefixesInstalledCount(final TablesKey tablesKey) {
117         if (this.prefixesInstalled == null) {
118             return NONE;
119         }
120         return this.prefixesInstalled.getPrefixedInstalledCount(tablesKey);
121     }
122
123     @Override
124     public final synchronized long getPrefixesSentCount(@Nonnull final TablesKey tablesKey) {
125         if (this.prefixesSent == null) {
126             return 0;
127         }
128         final PrefixesSentCounters counter = this.prefixesSent.get(tablesKey);
129         if (counter == null) {
130             return NONE;
131         }
132         return counter.getPrefixesSentCount();
133     }
134
135     @Override
136     public final synchronized long getPrefixesReceivedCount(final TablesKey tablesKey) {
137         if (this.prefixesReceived == null) {
138             return NONE;
139         }
140         return this.prefixesReceived.getPrefixedReceivedCount(tablesKey);
141     }
142
143     @Override
144     public final Set<TablesKey> getAfiSafisAdvertized() {
145         return ImmutableSet.copyOf(this.afiSafisAdvertized);
146     }
147
148     @Override
149     public final synchronized Set<TablesKey> getAfiSafisReceived() {
150         if (this.prefixesReceived == null) {
151             return Collections.emptySet();
152         }
153         return this.prefixesReceived.getTableKeys();
154     }
155
156     @Override
157     public final boolean isGracefulRestartAdvertized(final TablesKey tablesKey) {
158         return this.afiSafisGracefulAdvertized.contains(tablesKey);
159     }
160
161     @Override
162     public final boolean isGracefulRestartReceived(final TablesKey tablesKey) {
163         return this.afiSafisGracefulReceived.contains(tablesKey);
164     }
165
166     @Override
167     public final synchronized boolean isLocalRestarting() {
168         return this.localRestarting;
169     }
170
171     @Override
172     public final synchronized int getPeerRestartTime() {
173         return this.peerRestartTime;
174     }
175
176     @Override
177     public final synchronized boolean isPeerRestarting() {
178         return this.peerRestarting;
179     }
180
181     //FIXME BUG-196
182     public final void setAfiSafiGracefulRestartState(final int peerRestartTime, final boolean peerRestarting,
183         final boolean localRestarting) {
184         this.peerRestartTime = peerRestartTime;
185         this.peerRestarting = peerRestarting;
186         this.localRestarting = localRestarting;
187     }
188
189     protected final synchronized void setAdvertizedGracefulRestartTableTypes(final List<TablesKey> receivedGraceful) {
190         this.afiSafisGracefulReceived.addAll(receivedGraceful);
191     }
192
193     protected final synchronized void registerPrefixesSentCounter(final TablesKey tablesKey,
194         final PrefixesSentCounters prefixesSentCounter) {
195         this.prefixesSent.put(tablesKey, prefixesSentCounter);
196     }
197
198     protected final synchronized void registerPrefixesCounters(@Nonnull final PrefixesReceivedCounters prefixesReceived,
199         @Nonnull final PrefixesInstalledCounters prefixesInstalled) {
200         this.prefixesReceived = prefixesReceived;
201         this.prefixesInstalled = prefixesInstalled;
202     }
203
204     protected final synchronized void resetState() {
205         this.localRestarting = false;
206         this.peerRestartTime = 0;
207         this.peerRestarting = false;
208     }
209
210     @Override
211     public final BGPPeerState getPeerState() {
212         return this;
213     }
214
215     @Override
216     public final long getErroneousUpdateReceivedCount() {
217         //FIXME BUG-4979
218         return this.erroneousUpdate.longValue();
219     }
220
221     @Override
222     public final long getUpdateMessagesSentCount() {
223         return this.updateSentCounter.longValue();
224     }
225
226     @Override
227     public final long getNotificationMessagesSentCount() {
228         return this.notificationSentCounter.longValue();
229     }
230
231     @Override
232     public final long getUpdateMessagesReceivedCount() {
233         return this.updateReceivedCounter.longValue();
234     }
235
236     @Override
237     public final long getNotificationMessagesReceivedCount() {
238         return this.notificationReceivedCounter.longValue();
239     }
240
241     @Override
242     public final void messageSent(final Notification msg) {
243         if (msg instanceof Notify) {
244             this.notificationSentCounter.increment();
245         } else if (msg instanceof Update) {
246             this.updateSentCounter.increment();
247         }
248     }
249
250     @Override
251     public final void messageReceived(final Notification msg) {
252         if (msg instanceof Notify) {
253             this.notificationReceivedCounter.increment();
254         } else if (msg instanceof Update) {
255             this.updateReceivedCounter.increment();
256         }
257     }
258 }