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 com.google.common.base.Preconditions;
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 javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import javax.annotation.concurrent.GuardedBy;
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.BGPPeerMessagesState;
32 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
33 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
40 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.binding.Notification;
43 public abstract class BGPPeerStateImpl extends DefaultRibReference implements BGPPeerState, BGPAfiSafiState,
44 BGPGracelfulRestartState, BGPErrorHandlingState, BGPPeerMessagesState, BGPPeerStateConsumer, BGPMessagesListener {
45 private static final long NONE = 0L;
46 private final IpAddress neighborAddress;
47 private final Set<TablesKey> afiSafisAdvertized;
48 private final Set<TablesKey> afiSafisGracefulAdvertized;
49 private final Set<TablesKey> afiSafisGracefulReceived = new HashSet<>();
50 private final LongAdder updateSentCounter = new LongAdder();
51 private final LongAdder notificationSentCounter = new LongAdder();
52 private final LongAdder updateReceivedCounter = new LongAdder();
53 private final LongAdder notificationReceivedCounter = new LongAdder();
54 private final LongAdder erroneousUpdate = new LongAdder();
55 private final String groupId;
58 private final Map<TablesKey, PrefixesSentCounters> prefixesSent = new HashMap<>();
60 private PrefixesReceivedCounters prefixesReceived;
62 private PrefixesInstalledCounters prefixesInstalled;
64 private boolean localRestarting;
66 private int peerRestartTime;
68 private boolean peerRestarting;
70 public BGPPeerStateImpl(@Nonnull final KeyedInstanceIdentifier<Rib, RibKey> instanceIdentifier,
71 @Nullable final String groupId, @Nonnull final IpAddress neighborAddress,
72 @Nonnull final Set<TablesKey> afiSafisAdvertized,
73 @Nonnull final Set<TablesKey> afiSafisGracefulAdvertized) {
74 super(instanceIdentifier);
75 this.neighborAddress = Preconditions.checkNotNull(neighborAddress);
76 this.groupId = groupId;
77 this.afiSafisAdvertized = Preconditions.checkNotNull(afiSafisAdvertized);
78 this.afiSafisGracefulAdvertized = Preconditions.checkNotNull(afiSafisGracefulAdvertized);
82 public final String getGroupId() {
87 public final IpAddress getNeighborAddress() {
88 return this.neighborAddress;
92 public final synchronized long getTotalPrefixes() {
93 if (this.prefixesInstalled == null) {
96 return this.prefixesInstalled.getTotalPrefixesInstalled();
100 public final BGPPeerMessagesState getBGPPeerMessagesState() {
105 public final BGPGracelfulRestartState getBGPGracelfulRestart() {
110 public final synchronized boolean isAfiSafiSupported(final TablesKey tablesKey) {
111 return this.prefixesReceived != null && this.prefixesReceived.isSupported(tablesKey) &&
112 this.afiSafisAdvertized.contains(tablesKey);
116 public final synchronized long getPrefixesInstalledCount(final TablesKey tablesKey) {
117 if (this.prefixesInstalled == null) {
120 return this.prefixesInstalled.getPrefixedInstalledCount(tablesKey);
124 public final synchronized long getPrefixesSentCount(@Nonnull final TablesKey tablesKey) {
125 if (this.prefixesSent == null) {
128 final PrefixesSentCounters counter = this.prefixesSent.get(tablesKey);
129 if (counter == null) {
132 return counter.getPrefixesSentCount();
136 public final synchronized long getPrefixesReceivedCount(final TablesKey tablesKey) {
137 if (this.prefixesReceived == null) {
140 return this.prefixesReceived.getPrefixedReceivedCount(tablesKey);
144 public final Set<TablesKey> getAfiSafisAdvertized() {
145 return ImmutableSet.copyOf(this.afiSafisAdvertized);
149 public final synchronized Set<TablesKey> getAfiSafisReceived() {
150 if (this.prefixesReceived == null) {
151 return Collections.emptySet();
153 return this.prefixesReceived.getTableKeys();
157 public final boolean isGracefulRestartAdvertized(final TablesKey tablesKey) {
158 return this.afiSafisGracefulAdvertized.contains(tablesKey);
162 public final boolean isGracefulRestartReceived(final TablesKey tablesKey) {
163 return this.afiSafisGracefulReceived.contains(tablesKey);
167 public final synchronized boolean isLocalRestarting() {
168 return this.localRestarting;
172 public final synchronized int getPeerRestartTime() {
173 return this.peerRestartTime;
177 public final synchronized boolean isPeerRestarting() {
178 return this.peerRestarting;
182 public final void setAfiSafiGracefulRestartState(final int peerRestartTime, final boolean peerRestarting,
183 final boolean localRestarting) {
184 this.peerRestartTime = peerRestartTime;
185 this.peerRestarting = peerRestarting;
186 this.localRestarting = localRestarting;
189 protected final synchronized void setAdvertizedGracefulRestartTableTypes(final List<TablesKey> receivedGraceful) {
190 this.afiSafisGracefulReceived.addAll(receivedGraceful);
193 protected final synchronized void registerPrefixesSentCounter(final TablesKey tablesKey,
194 final PrefixesSentCounters prefixesSentCounter) {
195 this.prefixesSent.put(tablesKey, prefixesSentCounter);
198 protected final synchronized void registerPrefixesCounters(@Nonnull final PrefixesReceivedCounters prefixesReceived,
199 @Nonnull final PrefixesInstalledCounters prefixesInstalled) {
200 this.prefixesReceived = prefixesReceived;
201 this.prefixesInstalled = prefixesInstalled;
204 protected final synchronized void resetState() {
205 this.localRestarting = false;
206 this.peerRestartTime = 0;
207 this.peerRestarting = false;
211 public final BGPPeerState getPeerState() {
216 public final long getErroneousUpdateReceivedCount() {
218 return this.erroneousUpdate.longValue();
222 public final long getUpdateMessagesSentCount() {
223 return this.updateSentCounter.longValue();
227 public final long getNotificationMessagesSentCount() {
228 return this.notificationSentCounter.longValue();
232 public final long getUpdateMessagesReceivedCount() {
233 return this.updateReceivedCounter.longValue();
237 public final long getNotificationMessagesReceivedCount() {
238 return this.notificationReceivedCounter.longValue();
242 public final void messageSent(final Notification msg) {
243 if (msg instanceof Notify) {
244 this.notificationSentCounter.increment();
245 } else if (msg instanceof Update) {
246 this.updateSentCounter.increment();
251 public final void messageReceived(final Notification msg) {
252 if (msg instanceof Notify) {
253 this.notificationReceivedCounter.increment();
254 } else if (msg instanceof Update) {
255 this.updateReceivedCounter.increment();