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.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.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Notify;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Update;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.RibKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
41 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.Notification;
44 public abstract class BGPPeerStateImpl extends DefaultRibReference implements BGPPeerState, BGPAfiSafiState,
45 BGPGracelfulRestartState, BGPErrorHandlingState, BGPPeerMessagesState, BGPPeerStateConsumer, BGPMessagesListener {
46 private static final long NONE = 0L;
47 private final IpAddress neighborAddress;
48 private final Set<TablesKey> afiSafisAdvertized;
49 private final Set<TablesKey> afiSafisGracefulAdvertized;
50 private final Set<TablesKey> afiSafisGracefulReceived = new HashSet<>();
51 private final LongAdder updateSentCounter = new LongAdder();
52 private final LongAdder notificationSentCounter = new LongAdder();
53 private final LongAdder updateReceivedCounter = new LongAdder();
54 private final LongAdder notificationReceivedCounter = new LongAdder();
55 private final LongAdder erroneousUpdate = new LongAdder();
56 private final String groupId;
58 private boolean active;
61 private final Map<TablesKey, PrefixesSentCounters> prefixesSent = new HashMap<>();
63 private PrefixesReceivedCounters prefixesReceived;
65 private PrefixesInstalledCounters prefixesInstalled;
67 private boolean localRestarting;
69 private int peerRestartTime;
71 private boolean peerRestarting;
73 public BGPPeerStateImpl(@Nonnull final KeyedInstanceIdentifier<Rib, RibKey> instanceIdentifier,
74 @Nullable final String groupId, @Nonnull final IpAddress neighborAddress,
75 @Nonnull final Set<TablesKey> afiSafisAdvertized,
76 @Nonnull final Set<TablesKey> afiSafisGracefulAdvertized) {
77 super(instanceIdentifier);
78 this.neighborAddress = requireNonNull(neighborAddress);
79 this.groupId = groupId;
80 this.afiSafisAdvertized = requireNonNull(afiSafisAdvertized);
81 this.afiSafisGracefulAdvertized = requireNonNull(afiSafisGracefulAdvertized);
85 public final String getGroupId() {
90 public final IpAddress getNeighborAddress() {
91 return this.neighborAddress;
95 public final synchronized long getTotalPrefixes() {
96 if (this.prefixesInstalled == null) {
99 return this.prefixesInstalled.getTotalPrefixesInstalled();
103 public final BGPPeerMessagesState getBGPPeerMessagesState() {
108 public final BGPGracelfulRestartState getBGPGracelfulRestart() {
113 public final synchronized boolean isAfiSafiSupported(final TablesKey tablesKey) {
114 return this.prefixesReceived != null && this.prefixesReceived.isSupported(tablesKey) &&
115 this.afiSafisAdvertized.contains(tablesKey);
119 public final synchronized long getPrefixesInstalledCount(final TablesKey tablesKey) {
120 if (this.prefixesInstalled == null) {
123 return this.prefixesInstalled.getPrefixedInstalledCount(tablesKey);
127 public final synchronized long getPrefixesSentCount(@Nonnull final TablesKey tablesKey) {
128 if (this.prefixesSent == null) {
131 final PrefixesSentCounters counter = this.prefixesSent.get(tablesKey);
132 if (counter == null) {
135 return counter.getPrefixesSentCount();
139 public final synchronized long getPrefixesReceivedCount(final TablesKey tablesKey) {
140 if (this.prefixesReceived == null) {
143 return this.prefixesReceived.getPrefixedReceivedCount(tablesKey);
147 public final Set<TablesKey> getAfiSafisAdvertized() {
148 return ImmutableSet.copyOf(this.afiSafisAdvertized);
152 public final synchronized Set<TablesKey> getAfiSafisReceived() {
153 if (this.prefixesReceived == null) {
154 return Collections.emptySet();
156 return this.prefixesReceived.getTableKeys();
160 public final boolean isGracefulRestartAdvertized(final TablesKey tablesKey) {
161 return this.afiSafisGracefulAdvertized.contains(tablesKey);
165 public final boolean isGracefulRestartReceived(final TablesKey tablesKey) {
166 return this.afiSafisGracefulReceived.contains(tablesKey);
170 public final synchronized boolean isLocalRestarting() {
171 return this.localRestarting;
175 public final synchronized int getPeerRestartTime() {
176 return this.peerRestartTime;
180 public final synchronized boolean isPeerRestarting() {
181 return this.peerRestarting;
185 public synchronized final void setAfiSafiGracefulRestartState(final int peerRestartTime,
186 final boolean peerRestarting, final boolean localRestarting) {
187 this.peerRestartTime = peerRestartTime;
188 this.peerRestarting = peerRestarting;
189 this.localRestarting = localRestarting;
192 protected final synchronized void setAdvertizedGracefulRestartTableTypes(final List<TablesKey> receivedGraceful) {
193 this.afiSafisGracefulReceived.addAll(receivedGraceful);
196 protected final synchronized void registerPrefixesSentCounter(final TablesKey tablesKey,
197 final PrefixesSentCounters prefixesSentCounter) {
198 this.prefixesSent.put(tablesKey, prefixesSentCounter);
201 protected final synchronized void registerPrefixesCounters(@Nonnull final PrefixesReceivedCounters prefixesReceived,
202 @Nonnull final PrefixesInstalledCounters prefixesInstalled) {
203 this.prefixesReceived = prefixesReceived;
204 this.prefixesInstalled = prefixesInstalled;
207 protected final synchronized void resetState() {
208 this.localRestarting = false;
209 this.peerRestartTime = 0;
210 this.peerRestarting = false;
214 public final BGPPeerState getPeerState() {
219 public final long getErroneousUpdateReceivedCount() {
221 return this.erroneousUpdate.longValue();
225 public final long getUpdateMessagesSentCount() {
226 return this.updateSentCounter.longValue();
230 public final long getNotificationMessagesSentCount() {
231 return this.notificationSentCounter.longValue();
235 public final long getUpdateMessagesReceivedCount() {
236 return this.updateReceivedCounter.longValue();
240 public final long getNotificationMessagesReceivedCount() {
241 return this.notificationReceivedCounter.longValue();
245 public final void messageSent(final Notification msg) {
246 if (msg instanceof Notify) {
247 this.notificationSentCounter.increment();
248 } else if (msg instanceof Update) {
249 this.updateSentCounter.increment();
254 public final void messageReceived(final Notification msg) {
255 if (msg instanceof Notify) {
256 this.notificationReceivedCounter.increment();
257 } else if (msg instanceof Update) {
258 this.updateReceivedCounter.increment();
263 public final synchronized boolean isActive() {
267 protected final synchronized void setActive(final boolean active) {
268 this.active = active;