Mass-convert all compontents to use -no-zone addresses
[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 package org.opendaylight.protocol.bgp.rib.impl.state;
9
10 import static java.util.Objects.requireNonNull;
11
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 org.checkerframework.checker.lock.qual.GuardedBy;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
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.BGPLlGracelfulRestartState;
32 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerMessagesState;
33 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
34 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
35 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.operational.rev151009.BgpAfiSafiGracefulRestartState.Mode;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Notify;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.RibKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
42 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.binding.Notification;
44
45 public abstract class BGPPeerStateImpl extends DefaultRibReference implements BGPPeerState, BGPAfiSafiState,
46         BGPGracelfulRestartState, BGPLlGracelfulRestartState,BGPErrorHandlingState, BGPPeerMessagesState,
47         BGPPeerStateConsumer, BGPMessagesListener {
48     private static final long NONE = 0L;
49     private final IpAddressNoZone neighborAddress;
50     private final Set<TablesKey> afiSafisAdvertized;
51     private final Set<TablesKey> afiSafisGracefulAdvertized;
52     private final Set<TablesKey> afiSafisGracefulReceived = new HashSet<>();
53     private final Map<TablesKey, Integer> afiSafisLlGracefulAdvertised;
54     private final Map<TablesKey, Integer> afiSafisLlGracefulReceived = new HashMap<>();
55     private final LongAdder updateSentCounter = new LongAdder();
56     private final LongAdder notificationSentCounter = new LongAdder();
57     private final LongAdder updateReceivedCounter = new LongAdder();
58     private final LongAdder notificationReceivedCounter = new LongAdder();
59     private final LongAdder erroneousUpdate = new LongAdder();
60     private final String groupId;
61     @GuardedBy("this")
62     private boolean active;
63
64     @GuardedBy("this")
65     private final Map<TablesKey, PrefixesSentCounters> prefixesSent = new HashMap<>();
66     @GuardedBy("this")
67     private PrefixesReceivedCounters prefixesReceived;
68     @GuardedBy("this")
69     private PrefixesInstalledCounters prefixesInstalled;
70     @GuardedBy("this")
71     private boolean localRestarting;
72     @GuardedBy("this")
73     private int peerRestartTime;
74     @GuardedBy("this")
75     private boolean peerRestarting;
76
77     public BGPPeerStateImpl(final @NonNull KeyedInstanceIdentifier<Rib, RibKey> instanceIdentifier,
78             final @Nullable String groupId, final @NonNull IpAddressNoZone neighborAddress,
79             final @NonNull Set<TablesKey> afiSafisAdvertized,
80             final @NonNull Set<TablesKey> afiSafisGracefulAdvertized,
81             final @NonNull Map<TablesKey, Integer> afiSafisLlGracefulAdvertized) {
82         super(instanceIdentifier);
83         this.neighborAddress = requireNonNull(neighborAddress);
84         this.groupId = groupId;
85         this.afiSafisAdvertized = requireNonNull(afiSafisAdvertized);
86         this.afiSafisGracefulAdvertized = requireNonNull(afiSafisGracefulAdvertized);
87         this.afiSafisLlGracefulAdvertised = requireNonNull(afiSafisLlGracefulAdvertized);
88     }
89
90     @Override
91     public final String getGroupId() {
92         return this.groupId;
93     }
94
95     @Override
96     public final IpAddressNoZone getNeighborAddress() {
97         return this.neighborAddress;
98     }
99
100     @Override
101     public final synchronized long getTotalPrefixes() {
102         if (this.prefixesInstalled == null) {
103             return NONE;
104         }
105         return this.prefixesInstalled.getTotalPrefixesInstalled();
106     }
107
108     @Override
109     public final BGPPeerMessagesState getBGPPeerMessagesState() {
110         return this;
111     }
112
113     @Override
114     public final BGPGracelfulRestartState getBGPGracelfulRestart() {
115         return this;
116     }
117
118     @Override
119     public final synchronized boolean isAfiSafiSupported(final TablesKey tablesKey) {
120         return this.prefixesReceived != null && this.prefixesReceived.isSupported(tablesKey)
121                 && this.afiSafisAdvertized.contains(tablesKey);
122     }
123
124     @Override
125     public final synchronized long getPrefixesInstalledCount(final TablesKey tablesKey) {
126         if (this.prefixesInstalled == null) {
127             return NONE;
128         }
129         return this.prefixesInstalled.getPrefixedInstalledCount(tablesKey);
130     }
131
132     @Override
133     public final synchronized long getPrefixesSentCount(final TablesKey tablesKey) {
134         if (this.prefixesSent == null) {
135             return 0;
136         }
137         final PrefixesSentCounters counter = this.prefixesSent.get(tablesKey);
138         if (counter == null) {
139             return NONE;
140         }
141         return counter.getPrefixesSentCount();
142     }
143
144     @Override
145     public final synchronized long getPrefixesReceivedCount(final TablesKey tablesKey) {
146         if (this.prefixesReceived == null) {
147             return NONE;
148         }
149         return this.prefixesReceived.getPrefixedReceivedCount(tablesKey);
150     }
151
152     @Override
153     public final Set<TablesKey> getAfiSafisAdvertized() {
154         return ImmutableSet.copyOf(this.afiSafisAdvertized);
155     }
156
157     @Override
158     public final synchronized Set<TablesKey> getAfiSafisReceived() {
159         if (this.prefixesReceived == null) {
160             return Collections.emptySet();
161         }
162         return this.prefixesReceived.getTableKeys();
163     }
164
165     @Override
166     public final boolean isGracefulRestartAdvertized(final TablesKey tablesKey) {
167         return this.afiSafisGracefulAdvertized.contains(tablesKey);
168     }
169
170     @Override
171     public final boolean isGracefulRestartReceived(final TablesKey tablesKey) {
172         return this.afiSafisGracefulReceived.contains(tablesKey);
173     }
174
175     @Override
176     public final synchronized boolean isLocalRestarting() {
177         return this.localRestarting;
178     }
179
180     @Override
181     public final synchronized int getPeerRestartTime() {
182         return this.peerRestartTime;
183     }
184
185     @Override
186     public final synchronized boolean isPeerRestarting() {
187         return this.peerRestarting;
188     }
189
190     public final synchronized void setAfiSafiGracefulRestartState(final int newPeerRestartTime,
191             final boolean newPeerRestarting, final boolean newLocalRestarting) {
192         this.peerRestartTime = newPeerRestartTime;
193         this.peerRestarting = newPeerRestarting;
194         this.localRestarting = newLocalRestarting;
195     }
196
197     protected final synchronized void setAdvertizedGracefulRestartTableTypes(final List<TablesKey> receivedGraceful) {
198         this.afiSafisGracefulReceived.clear();
199         this.afiSafisGracefulReceived.addAll(receivedGraceful);
200     }
201
202     protected final synchronized void registerPrefixesSentCounter(final TablesKey tablesKey,
203         final PrefixesSentCounters prefixesSentCounter) {
204         this.prefixesSent.put(tablesKey, prefixesSentCounter);
205     }
206
207     protected final synchronized void registerPrefixesCounters(
208             final @NonNull PrefixesReceivedCounters newPrefixesReceived,
209             final @NonNull PrefixesInstalledCounters newPrefixesInstalled) {
210         this.prefixesReceived = newPrefixesReceived;
211         this.prefixesInstalled = newPrefixesInstalled;
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(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 }