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.base.Preconditions;
14 import com.google.common.collect.ImmutableSet;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
21 import java.util.concurrent.atomic.LongAdder;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import javax.annotation.concurrent.GuardedBy;
25 import org.opendaylight.protocol.bgp.rib.DefaultRibReference;
26 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPMessagesListener;
27 import org.opendaylight.protocol.bgp.rib.impl.state.peer.PrefixesInstalledCounters;
28 import org.opendaylight.protocol.bgp.rib.impl.state.peer.PrefixesReceivedCounters;
29 import org.opendaylight.protocol.bgp.rib.impl.state.peer.PrefixesSentCounters;
30 import org.opendaylight.protocol.bgp.rib.spi.state.BGPAfiSafiState;
31 import org.opendaylight.protocol.bgp.rib.spi.state.BGPErrorHandlingState;
32 import org.opendaylight.protocol.bgp.rib.spi.state.BGPGracelfulRestartState;
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.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.rev130919.Notify;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.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, BGPErrorHandlingState, BGPPeerMessagesState, BGPPeerStateConsumer, BGPMessagesListener {
47 private static final long NONE = 0L;
48 private final IpAddress neighborAddress;
49 private final Set<TablesKey> afiSafisAdvertized;
50 private final Set<TablesKey> afiSafisGracefulAdvertized;
51 private final Set<TablesKey> afiSafisGracefulReceived = new HashSet<>();
52 private final LongAdder updateSentCounter = new LongAdder();
53 private final LongAdder notificationSentCounter = new LongAdder();
54 private final LongAdder updateReceivedCounter = new LongAdder();
55 private final LongAdder notificationReceivedCounter = new LongAdder();
56 private final LongAdder erroneousUpdate = new LongAdder();
57 private final String groupId;
60 private final Map<TablesKey, PrefixesSentCounters> prefixesSent = new HashMap<>();
62 private PrefixesReceivedCounters prefixesReceived;
64 private PrefixesInstalledCounters prefixesInstalled;
66 private boolean localRestarting;
68 private int peerRestartTime;
70 private boolean peerRestarting;
72 public BGPPeerStateImpl(@Nonnull final KeyedInstanceIdentifier<Rib, RibKey> instanceIdentifier,
73 @Nullable final String groupId, @Nonnull final IpAddress neighborAddress,
74 @Nonnull final Set<TablesKey> afiSafisAdvertized,
75 @Nonnull final Set<TablesKey> afiSafisGracefulAdvertized) {
76 super(instanceIdentifier);
77 this.neighborAddress = requireNonNull(neighborAddress);
78 this.groupId = groupId;
79 this.afiSafisAdvertized = requireNonNull(afiSafisAdvertized);
80 this.afiSafisGracefulAdvertized = requireNonNull(afiSafisGracefulAdvertized);
84 public final String getGroupId() {
89 public final IpAddress getNeighborAddress() {
90 return this.neighborAddress;
94 public final synchronized long getTotalPrefixes() {
95 if (this.prefixesInstalled == null) {
98 return this.prefixesInstalled.getTotalPrefixesInstalled();
102 public final BGPPeerMessagesState getBGPPeerMessagesState() {
107 public final BGPGracelfulRestartState getBGPGracelfulRestart() {
112 public final synchronized boolean isAfiSafiSupported(final TablesKey tablesKey) {
113 return this.prefixesReceived != null && this.prefixesReceived.isSupported(tablesKey) &&
114 this.afiSafisAdvertized.contains(tablesKey);
118 public final synchronized long getPrefixesInstalledCount(final TablesKey tablesKey) {
119 if (this.prefixesInstalled == null) {
122 return this.prefixesInstalled.getPrefixedInstalledCount(tablesKey);
126 public final synchronized long getPrefixesSentCount(@Nonnull final TablesKey tablesKey) {
127 if (this.prefixesSent == null) {
130 final PrefixesSentCounters counter = this.prefixesSent.get(tablesKey);
131 if (counter == null) {
134 return counter.getPrefixesSentCount();
138 public final synchronized long getPrefixesReceivedCount(final TablesKey tablesKey) {
139 if (this.prefixesReceived == null) {
142 return this.prefixesReceived.getPrefixedReceivedCount(tablesKey);
146 public final Set<TablesKey> getAfiSafisAdvertized() {
147 return ImmutableSet.copyOf(this.afiSafisAdvertized);
151 public final synchronized Set<TablesKey> getAfiSafisReceived() {
152 if (this.prefixesReceived == null) {
153 return Collections.emptySet();
155 return this.prefixesReceived.getTableKeys();
159 public final boolean isGracefulRestartAdvertized(final TablesKey tablesKey) {
160 return this.afiSafisGracefulAdvertized.contains(tablesKey);
164 public final boolean isGracefulRestartReceived(final TablesKey tablesKey) {
165 return this.afiSafisGracefulReceived.contains(tablesKey);
169 public final synchronized boolean isLocalRestarting() {
170 return this.localRestarting;
174 public final synchronized int getPeerRestartTime() {
175 return this.peerRestartTime;
179 public final synchronized boolean isPeerRestarting() {
180 return this.peerRestarting;
184 public final void setAfiSafiGracefulRestartState(final int peerRestartTime, final boolean peerRestarting,
185 final boolean localRestarting) {
186 this.peerRestartTime = peerRestartTime;
187 this.peerRestarting = peerRestarting;
188 this.localRestarting = localRestarting;
191 protected final synchronized void setAdvertizedGracefulRestartTableTypes(final List<TablesKey> receivedGraceful) {
192 this.afiSafisGracefulReceived.addAll(receivedGraceful);
195 protected final synchronized void registerPrefixesSentCounter(final TablesKey tablesKey,
196 final PrefixesSentCounters prefixesSentCounter) {
197 this.prefixesSent.put(tablesKey, prefixesSentCounter);
200 protected final synchronized void registerPrefixesCounters(@Nonnull final PrefixesReceivedCounters prefixesReceived,
201 @Nonnull final PrefixesInstalledCounters prefixesInstalled) {
202 this.prefixesReceived = prefixesReceived;
203 this.prefixesInstalled = prefixesInstalled;
206 protected final synchronized void resetState() {
207 this.localRestarting = false;
208 this.peerRestartTime = 0;
209 this.peerRestarting = false;
213 public final BGPPeerState getPeerState() {
218 public final long getErroneousUpdateReceivedCount() {
220 return this.erroneousUpdate.longValue();
224 public final long getUpdateMessagesSentCount() {
225 return this.updateSentCounter.longValue();
229 public final long getNotificationMessagesSentCount() {
230 return this.notificationSentCounter.longValue();
234 public final long getUpdateMessagesReceivedCount() {
235 return this.updateReceivedCounter.longValue();
239 public final long getNotificationMessagesReceivedCount() {
240 return this.notificationReceivedCounter.longValue();
244 public final void messageSent(final Notification msg) {
245 if (msg instanceof Notify) {
246 this.notificationSentCounter.increment();
247 } else if (msg instanceof Update) {
248 this.updateSentCounter.increment();
253 public final void messageReceived(final Notification msg) {
254 if (msg instanceof Notify) {
255 this.notificationReceivedCounter.increment();
256 } else if (msg instanceof Update) {
257 this.updateReceivedCounter.increment();