Fix more rib-impl checkstyle
[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 static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.atomic.LongAdder;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import javax.annotation.concurrent.GuardedBy;
24 import org.opendaylight.protocol.bgp.rib.DefaultRibReference;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPMessagesListener;
26 import org.opendaylight.protocol.bgp.rib.impl.state.peer.PrefixesInstalledCounters;
27 import org.opendaylight.protocol.bgp.rib.impl.state.peer.PrefixesReceivedCounters;
28 import org.opendaylight.protocol.bgp.rib.impl.state.peer.PrefixesSentCounters;
29 import org.opendaylight.protocol.bgp.rib.spi.state.BGPAfiSafiState;
30 import org.opendaylight.protocol.bgp.rib.spi.state.BGPErrorHandlingState;
31 import org.opendaylight.protocol.bgp.rib.spi.state.BGPGracelfulRestartState;
32 import org.opendaylight.protocol.bgp.rib.spi.state.BGPLlGracelfulRestartState;
33 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerMessagesState;
34 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
35 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
36 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.operational.rev151009.BgpAfiSafiGracefulRestartState.Mode;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Notify;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.RibKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
43 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.binding.Notification;
45
46 public abstract class BGPPeerStateImpl extends DefaultRibReference implements BGPPeerState, BGPAfiSafiState,
47         BGPGracelfulRestartState, BGPLlGracelfulRestartState,BGPErrorHandlingState, BGPPeerMessagesState,
48         BGPPeerStateConsumer, BGPMessagesListener {
49     private static final long NONE = 0L;
50     private final IpAddress neighborAddress;
51     private final Set<TablesKey> afiSafisAdvertized;
52     private final Set<TablesKey> afiSafisGracefulAdvertized;
53     private final Set<TablesKey> afiSafisGracefulReceived = new HashSet<>();
54     private final Map<TablesKey, Integer> afiSafisLlGracefulAdvertised;
55     private final Map<TablesKey, Integer> afiSafisLlGracefulReceived = new HashMap<>();
56     private final LongAdder updateSentCounter = new LongAdder();
57     private final LongAdder notificationSentCounter = new LongAdder();
58     private final LongAdder updateReceivedCounter = new LongAdder();
59     private final LongAdder notificationReceivedCounter = new LongAdder();
60     private final LongAdder erroneousUpdate = new LongAdder();
61     private final String groupId;
62     @GuardedBy("this")
63     private boolean active;
64
65     @GuardedBy("this")
66     private final Map<TablesKey, PrefixesSentCounters> prefixesSent = new HashMap<>();
67     @GuardedBy("this")
68     private PrefixesReceivedCounters prefixesReceived;
69     @GuardedBy("this")
70     private PrefixesInstalledCounters prefixesInstalled;
71     @GuardedBy("this")
72     private boolean localRestarting;
73     @GuardedBy("this")
74     private int peerRestartTime;
75     @GuardedBy("this")
76     private boolean peerRestarting;
77
78     public BGPPeerStateImpl(@Nonnull final KeyedInstanceIdentifier<Rib, RibKey> instanceIdentifier,
79         @Nullable final String groupId, @Nonnull final IpAddress neighborAddress,
80         @Nonnull final Set<TablesKey> afiSafisAdvertized,
81         @Nonnull final Set<TablesKey> afiSafisGracefulAdvertized,
82         @Nonnull final Map<TablesKey, Integer> afiSafisLlGracefulAdvertized) {
83         super(instanceIdentifier);
84         this.neighborAddress = requireNonNull(neighborAddress);
85         this.groupId = groupId;
86         this.afiSafisAdvertized = requireNonNull(afiSafisAdvertized);
87         this.afiSafisGracefulAdvertized = requireNonNull(afiSafisGracefulAdvertized);
88         this.afiSafisLlGracefulAdvertised = requireNonNull(afiSafisLlGracefulAdvertized);
89     }
90
91     @Override
92     public final String getGroupId() {
93         return this.groupId;
94     }
95
96     @Override
97     public final IpAddress getNeighborAddress() {
98         return this.neighborAddress;
99     }
100
101     @Override
102     public final synchronized long getTotalPrefixes() {
103         if (this.prefixesInstalled == null) {
104             return NONE;
105         }
106         return this.prefixesInstalled.getTotalPrefixesInstalled();
107     }
108
109     @Override
110     public final BGPPeerMessagesState getBGPPeerMessagesState() {
111         return this;
112     }
113
114     @Override
115     public final BGPGracelfulRestartState getBGPGracelfulRestart() {
116         return this;
117     }
118
119     @Override
120     public final synchronized boolean isAfiSafiSupported(final TablesKey tablesKey) {
121         return this.prefixesReceived != null && this.prefixesReceived.isSupported(tablesKey)
122                 && this.afiSafisAdvertized.contains(tablesKey);
123     }
124
125     @Override
126     public final synchronized long getPrefixesInstalledCount(final TablesKey tablesKey) {
127         if (this.prefixesInstalled == null) {
128             return NONE;
129         }
130         return this.prefixesInstalled.getPrefixedInstalledCount(tablesKey);
131     }
132
133     @Override
134     public final synchronized long getPrefixesSentCount(@Nonnull final TablesKey tablesKey) {
135         if (this.prefixesSent == null) {
136             return 0;
137         }
138         final PrefixesSentCounters counter = this.prefixesSent.get(tablesKey);
139         if (counter == null) {
140             return NONE;
141         }
142         return counter.getPrefixesSentCount();
143     }
144
145     @Override
146     public final synchronized long getPrefixesReceivedCount(final TablesKey tablesKey) {
147         if (this.prefixesReceived == null) {
148             return NONE;
149         }
150         return this.prefixesReceived.getPrefixedReceivedCount(tablesKey);
151     }
152
153     @Override
154     public final Set<TablesKey> getAfiSafisAdvertized() {
155         return ImmutableSet.copyOf(this.afiSafisAdvertized);
156     }
157
158     @Override
159     public final synchronized Set<TablesKey> getAfiSafisReceived() {
160         if (this.prefixesReceived == null) {
161             return Collections.emptySet();
162         }
163         return this.prefixesReceived.getTableKeys();
164     }
165
166     @Override
167     public final boolean isGracefulRestartAdvertized(final TablesKey tablesKey) {
168         return this.afiSafisGracefulAdvertized.contains(tablesKey);
169     }
170
171     @Override
172     public final boolean isGracefulRestartReceived(final TablesKey tablesKey) {
173         return this.afiSafisGracefulReceived.contains(tablesKey);
174     }
175
176     @Override
177     public final synchronized boolean isLocalRestarting() {
178         return this.localRestarting;
179     }
180
181     @Override
182     public final synchronized int getPeerRestartTime() {
183         return this.peerRestartTime;
184     }
185
186     @Override
187     public final synchronized boolean isPeerRestarting() {
188         return this.peerRestarting;
189     }
190
191     public final synchronized void setAfiSafiGracefulRestartState(final int peerRestartTime,
192             final boolean peerRestarting, final boolean localRestarting) {
193         this.peerRestartTime = peerRestartTime;
194         this.peerRestarting = peerRestarting;
195         this.localRestarting = localRestarting;
196     }
197
198     protected final synchronized void setAdvertizedGracefulRestartTableTypes(final List<TablesKey> receivedGraceful) {
199         this.afiSafisGracefulReceived.clear();
200         this.afiSafisGracefulReceived.addAll(receivedGraceful);
201     }
202
203     protected final synchronized void registerPrefixesSentCounter(final TablesKey tablesKey,
204         final PrefixesSentCounters prefixesSentCounter) {
205         this.prefixesSent.put(tablesKey, prefixesSentCounter);
206     }
207
208     protected final synchronized void registerPrefixesCounters(@Nonnull final PrefixesReceivedCounters prefixesReceived,
209         @Nonnull final PrefixesInstalledCounters prefixesInstalled) {
210         this.prefixesReceived = prefixesReceived;
211         this.prefixesInstalled = prefixesInstalled;
212     }
213
214     protected final synchronized void resetState() {
215         this.localRestarting = false;
216         this.peerRestartTime = 0;
217         this.peerRestarting = false;
218     }
219
220     protected final synchronized void setRestartingState() {
221         this.peerRestarting = true;
222     }
223
224     protected final synchronized void setLocalRestartingState(final boolean restarting) {
225         this.localRestarting = restarting;
226     }
227
228     @Override
229     public final BGPPeerState getPeerState() {
230         return this;
231     }
232
233     @Override
234     public final long getErroneousUpdateReceivedCount() {
235         //FIXME BUG-4979
236         return this.erroneousUpdate.longValue();
237     }
238
239     @Override
240     public final long getUpdateMessagesSentCount() {
241         return this.updateSentCounter.longValue();
242     }
243
244     @Override
245     public final long getNotificationMessagesSentCount() {
246         return this.notificationSentCounter.longValue();
247     }
248
249     @Override
250     public final long getUpdateMessagesReceivedCount() {
251         return this.updateReceivedCounter.longValue();
252     }
253
254     @Override
255     public final long getNotificationMessagesReceivedCount() {
256         return this.notificationReceivedCounter.longValue();
257     }
258
259     @Override
260     public final void messageSent(final Notification msg) {
261         if (msg instanceof Notify) {
262             this.notificationSentCounter.increment();
263         } else if (msg instanceof Update) {
264             this.updateSentCounter.increment();
265         }
266     }
267
268     @Override
269     public final void messageReceived(final Notification msg) {
270         if (msg instanceof Notify) {
271             this.notificationReceivedCounter.increment();
272         } else if (msg instanceof Update) {
273             this.updateReceivedCounter.increment();
274         }
275     }
276
277     @Override
278     public final synchronized boolean isActive() {
279         return this.active;
280     }
281
282     protected final synchronized void setActive(final boolean active) {
283         this.active = active;
284     }
285
286     @Override
287     public final synchronized Mode getMode() {
288         if (this.afiSafisGracefulAdvertized.isEmpty()) {
289             return Mode.HELPERONLY;
290         }
291         if (this.afiSafisGracefulReceived.isEmpty()) {
292             return Mode.REMOTEHELPER;
293         }
294         return Mode.BILATERAL;
295     }
296
297     public final synchronized void setAdvertizedLlGracefulRestartTableTypes(
298             final Map<TablesKey, Integer> afiSafiReceived) {
299         this.afiSafisLlGracefulReceived.clear();
300         this.afiSafisLlGracefulReceived.putAll(afiSafiReceived);
301     }
302
303     @Override
304     public final synchronized boolean isLlGracefulRestartAdvertised(final TablesKey tablesKey) {
305         return this.afiSafisLlGracefulAdvertised.containsKey(tablesKey);
306     }
307
308     @Override
309     public final synchronized boolean isLlGracefulRestartReceived(final TablesKey tablesKey) {
310         return this.afiSafisLlGracefulReceived.containsKey(tablesKey);
311     }
312
313     @Override
314     public final synchronized int getLlGracefulRestartTimer(@Nonnull final TablesKey tablesKey) {
315         final int timerAdvertised = isLlGracefulRestartAdvertised(tablesKey)
316                 ? this.afiSafisLlGracefulAdvertised.get(tablesKey) : 0;
317         final int timerReceived = isLlGracefulRestartReceived(tablesKey)
318                 ? this.afiSafisLlGracefulReceived.get(tablesKey) : 0;
319         return Integer.min(timerAdvertised, timerReceived);
320     }
321 }