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
9 package org.opendaylight.protocol.bgp.rib.impl.state;
11 import static java.util.Objects.requireNonNull;
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;
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;
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;
63 private boolean active;
66 private final Map<TablesKey, PrefixesSentCounters> prefixesSent = new HashMap<>();
68 private PrefixesReceivedCounters prefixesReceived;
70 private PrefixesInstalledCounters prefixesInstalled;
72 private boolean localRestarting;
74 private int peerRestartTime;
76 private boolean peerRestarting;
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);
92 public final String getGroupId() {
97 public final IpAddress getNeighborAddress() {
98 return this.neighborAddress;
102 public final synchronized long getTotalPrefixes() {
103 if (this.prefixesInstalled == null) {
106 return this.prefixesInstalled.getTotalPrefixesInstalled();
110 public final BGPPeerMessagesState getBGPPeerMessagesState() {
115 public final BGPGracelfulRestartState getBGPGracelfulRestart() {
120 public final synchronized boolean isAfiSafiSupported(final TablesKey tablesKey) {
121 return this.prefixesReceived != null && this.prefixesReceived.isSupported(tablesKey)
122 && this.afiSafisAdvertized.contains(tablesKey);
126 public final synchronized long getPrefixesInstalledCount(final TablesKey tablesKey) {
127 if (this.prefixesInstalled == null) {
130 return this.prefixesInstalled.getPrefixedInstalledCount(tablesKey);
134 public final synchronized long getPrefixesSentCount(@Nonnull final TablesKey tablesKey) {
135 if (this.prefixesSent == null) {
138 final PrefixesSentCounters counter = this.prefixesSent.get(tablesKey);
139 if (counter == null) {
142 return counter.getPrefixesSentCount();
146 public final synchronized long getPrefixesReceivedCount(final TablesKey tablesKey) {
147 if (this.prefixesReceived == null) {
150 return this.prefixesReceived.getPrefixedReceivedCount(tablesKey);
154 public final Set<TablesKey> getAfiSafisAdvertized() {
155 return ImmutableSet.copyOf(this.afiSafisAdvertized);
159 public final synchronized Set<TablesKey> getAfiSafisReceived() {
160 if (this.prefixesReceived == null) {
161 return Collections.emptySet();
163 return this.prefixesReceived.getTableKeys();
167 public final boolean isGracefulRestartAdvertized(final TablesKey tablesKey) {
168 return this.afiSafisGracefulAdvertized.contains(tablesKey);
172 public final boolean isGracefulRestartReceived(final TablesKey tablesKey) {
173 return this.afiSafisGracefulReceived.contains(tablesKey);
177 public final synchronized boolean isLocalRestarting() {
178 return this.localRestarting;
182 public final synchronized int getPeerRestartTime() {
183 return this.peerRestartTime;
187 public final synchronized boolean isPeerRestarting() {
188 return this.peerRestarting;
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;
198 protected final synchronized void setAdvertizedGracefulRestartTableTypes(final List<TablesKey> receivedGraceful) {
199 this.afiSafisGracefulReceived.clear();
200 this.afiSafisGracefulReceived.addAll(receivedGraceful);
203 protected final synchronized void registerPrefixesSentCounter(final TablesKey tablesKey,
204 final PrefixesSentCounters prefixesSentCounter) {
205 this.prefixesSent.put(tablesKey, prefixesSentCounter);
208 protected final synchronized void registerPrefixesCounters(@Nonnull final PrefixesReceivedCounters prefixesReceived,
209 @Nonnull final PrefixesInstalledCounters prefixesInstalled) {
210 this.prefixesReceived = prefixesReceived;
211 this.prefixesInstalled = prefixesInstalled;
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(@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);