2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl.state;
10 import static java.util.Objects.requireNonNull;
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;
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.IpAddress;
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;
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 IpAddress 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;
62 private boolean active;
65 private final Map<TablesKey, PrefixesSentCounters> prefixesSent = new HashMap<>();
67 private PrefixesReceivedCounters prefixesReceived;
69 private PrefixesInstalledCounters prefixesInstalled;
71 private boolean localRestarting;
73 private int peerRestartTime;
75 private boolean peerRestarting;
77 public BGPPeerStateImpl(final @NonNull KeyedInstanceIdentifier<Rib, RibKey> instanceIdentifier,
78 final @Nullable String groupId, final @NonNull IpAddress 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);
91 public final String getGroupId() {
96 public final IpAddress getNeighborAddress() {
97 return this.neighborAddress;
101 public final synchronized long getTotalPrefixes() {
102 if (this.prefixesInstalled == null) {
105 return this.prefixesInstalled.getTotalPrefixesInstalled();
109 public final BGPPeerMessagesState getBGPPeerMessagesState() {
114 public final BGPGracelfulRestartState getBGPGracelfulRestart() {
119 public final synchronized boolean isAfiSafiSupported(final TablesKey tablesKey) {
120 return this.prefixesReceived != null && this.prefixesReceived.isSupported(tablesKey)
121 && this.afiSafisAdvertized.contains(tablesKey);
125 public final synchronized long getPrefixesInstalledCount(final TablesKey tablesKey) {
126 if (this.prefixesInstalled == null) {
129 return this.prefixesInstalled.getPrefixedInstalledCount(tablesKey);
133 public final synchronized long getPrefixesSentCount(final TablesKey tablesKey) {
134 if (this.prefixesSent == null) {
137 final PrefixesSentCounters counter = this.prefixesSent.get(tablesKey);
138 if (counter == null) {
141 return counter.getPrefixesSentCount();
145 public final synchronized long getPrefixesReceivedCount(final TablesKey tablesKey) {
146 if (this.prefixesReceived == null) {
149 return this.prefixesReceived.getPrefixedReceivedCount(tablesKey);
153 public final Set<TablesKey> getAfiSafisAdvertized() {
154 return ImmutableSet.copyOf(this.afiSafisAdvertized);
158 public final synchronized Set<TablesKey> getAfiSafisReceived() {
159 if (this.prefixesReceived == null) {
160 return Collections.emptySet();
162 return this.prefixesReceived.getTableKeys();
166 public final boolean isGracefulRestartAdvertized(final TablesKey tablesKey) {
167 return this.afiSafisGracefulAdvertized.contains(tablesKey);
171 public final boolean isGracefulRestartReceived(final TablesKey tablesKey) {
172 return this.afiSafisGracefulReceived.contains(tablesKey);
176 public final synchronized boolean isLocalRestarting() {
177 return this.localRestarting;
181 public final synchronized int getPeerRestartTime() {
182 return this.peerRestartTime;
186 public final synchronized boolean isPeerRestarting() {
187 return this.peerRestarting;
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;
197 protected final synchronized void setAdvertizedGracefulRestartTableTypes(final List<TablesKey> receivedGraceful) {
198 this.afiSafisGracefulReceived.clear();
199 this.afiSafisGracefulReceived.addAll(receivedGraceful);
202 protected final synchronized void registerPrefixesSentCounter(final TablesKey tablesKey,
203 final PrefixesSentCounters prefixesSentCounter) {
204 this.prefixesSent.put(tablesKey, prefixesSentCounter);
207 protected final synchronized void registerPrefixesCounters(
208 final @NonNull PrefixesReceivedCounters newPrefixesReceived,
209 final @NonNull PrefixesInstalledCounters newPrefixesInstalled) {
210 this.prefixesReceived = newPrefixesReceived;
211 this.prefixesInstalled = newPrefixesInstalled;
214 protected final synchronized void resetState() {
215 this.localRestarting = false;
216 this.peerRestartTime = 0;
217 this.peerRestarting = false;
220 protected final synchronized void setRestartingState() {
221 this.peerRestarting = true;
224 protected final synchronized void setLocalRestartingState(final boolean restarting) {
225 this.localRestarting = restarting;
229 public final BGPPeerState getPeerState() {
234 public final long getErroneousUpdateReceivedCount() {
236 return this.erroneousUpdate.longValue();
240 public final long getUpdateMessagesSentCount() {
241 return this.updateSentCounter.longValue();
245 public final long getNotificationMessagesSentCount() {
246 return this.notificationSentCounter.longValue();
250 public final long getUpdateMessagesReceivedCount() {
251 return this.updateReceivedCounter.longValue();
255 public final long getNotificationMessagesReceivedCount() {
256 return this.notificationReceivedCounter.longValue();
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();
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();
278 public final synchronized boolean isActive() {
282 protected final synchronized void setActive(final boolean active) {
283 this.active = active;
287 public final synchronized Mode getMode() {
288 if (this.afiSafisGracefulAdvertized.isEmpty()) {
289 return Mode.HELPERONLY;
291 if (this.afiSafisGracefulReceived.isEmpty()) {
292 return Mode.REMOTEHELPER;
294 return Mode.BILATERAL;
297 public final synchronized void setAdvertizedLlGracefulRestartTableTypes(
298 final Map<TablesKey, Integer> afiSafiReceived) {
299 this.afiSafisLlGracefulReceived.clear();
300 this.afiSafisLlGracefulReceived.putAll(afiSafiReceived);
304 public final synchronized boolean isLlGracefulRestartAdvertised(final TablesKey tablesKey) {
305 return this.afiSafisLlGracefulAdvertised.containsKey(tablesKey);
309 public final synchronized boolean isLlGracefulRestartReceived(final TablesKey tablesKey) {
310 return this.afiSafisLlGracefulReceived.containsKey(tablesKey);
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);