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.rev130919.Notify;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.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;
59 private final Map<TablesKey, PrefixesSentCounters> prefixesSent = new HashMap<>();
61 private PrefixesReceivedCounters prefixesReceived;
63 private PrefixesInstalledCounters prefixesInstalled;
65 private boolean localRestarting;
67 private int peerRestartTime;
69 private boolean peerRestarting;
71 public BGPPeerStateImpl(@Nonnull final KeyedInstanceIdentifier<Rib, RibKey> instanceIdentifier,
72 @Nullable final String groupId, @Nonnull final IpAddress neighborAddress,
73 @Nonnull final Set<TablesKey> afiSafisAdvertized,
74 @Nonnull final Set<TablesKey> afiSafisGracefulAdvertized) {
75 super(instanceIdentifier);
76 this.neighborAddress = requireNonNull(neighborAddress);
77 this.groupId = groupId;
78 this.afiSafisAdvertized = requireNonNull(afiSafisAdvertized);
79 this.afiSafisGracefulAdvertized = requireNonNull(afiSafisGracefulAdvertized);
83 public final String getGroupId() {
88 public final IpAddress getNeighborAddress() {
89 return this.neighborAddress;
93 public final synchronized long getTotalPrefixes() {
94 if (this.prefixesInstalled == null) {
97 return this.prefixesInstalled.getTotalPrefixesInstalled();
101 public final BGPPeerMessagesState getBGPPeerMessagesState() {
106 public final BGPGracelfulRestartState getBGPGracelfulRestart() {
111 public final synchronized boolean isAfiSafiSupported(final TablesKey tablesKey) {
112 return this.prefixesReceived != null && this.prefixesReceived.isSupported(tablesKey) &&
113 this.afiSafisAdvertized.contains(tablesKey);
117 public final synchronized long getPrefixesInstalledCount(final TablesKey tablesKey) {
118 if (this.prefixesInstalled == null) {
121 return this.prefixesInstalled.getPrefixedInstalledCount(tablesKey);
125 public final synchronized long getPrefixesSentCount(@Nonnull final TablesKey tablesKey) {
126 if (this.prefixesSent == null) {
129 final PrefixesSentCounters counter = this.prefixesSent.get(tablesKey);
130 if (counter == null) {
133 return counter.getPrefixesSentCount();
137 public final synchronized long getPrefixesReceivedCount(final TablesKey tablesKey) {
138 if (this.prefixesReceived == null) {
141 return this.prefixesReceived.getPrefixedReceivedCount(tablesKey);
145 public final Set<TablesKey> getAfiSafisAdvertized() {
146 return ImmutableSet.copyOf(this.afiSafisAdvertized);
150 public final synchronized Set<TablesKey> getAfiSafisReceived() {
151 if (this.prefixesReceived == null) {
152 return Collections.emptySet();
154 return this.prefixesReceived.getTableKeys();
158 public final boolean isGracefulRestartAdvertized(final TablesKey tablesKey) {
159 return this.afiSafisGracefulAdvertized.contains(tablesKey);
163 public final boolean isGracefulRestartReceived(final TablesKey tablesKey) {
164 return this.afiSafisGracefulReceived.contains(tablesKey);
168 public final synchronized boolean isLocalRestarting() {
169 return this.localRestarting;
173 public final synchronized int getPeerRestartTime() {
174 return this.peerRestartTime;
178 public final synchronized boolean isPeerRestarting() {
179 return this.peerRestarting;
183 public final void setAfiSafiGracefulRestartState(final int peerRestartTime, final boolean peerRestarting,
184 final boolean localRestarting) {
185 this.peerRestartTime = peerRestartTime;
186 this.peerRestarting = peerRestarting;
187 this.localRestarting = localRestarting;
190 protected final synchronized void setAdvertizedGracefulRestartTableTypes(final List<TablesKey> receivedGraceful) {
191 this.afiSafisGracefulReceived.addAll(receivedGraceful);
194 protected final synchronized void registerPrefixesSentCounter(final TablesKey tablesKey,
195 final PrefixesSentCounters prefixesSentCounter) {
196 this.prefixesSent.put(tablesKey, prefixesSentCounter);
199 protected final synchronized void registerPrefixesCounters(@Nonnull final PrefixesReceivedCounters prefixesReceived,
200 @Nonnull final PrefixesInstalledCounters prefixesInstalled) {
201 this.prefixesReceived = prefixesReceived;
202 this.prefixesInstalled = prefixesInstalled;
205 protected final synchronized void resetState() {
206 this.localRestarting = false;
207 this.peerRestartTime = 0;
208 this.peerRestarting = false;
212 public final BGPPeerState getPeerState() {
217 public final long getErroneousUpdateReceivedCount() {
219 return this.erroneousUpdate.longValue();
223 public final long getUpdateMessagesSentCount() {
224 return this.updateSentCounter.longValue();
228 public final long getNotificationMessagesSentCount() {
229 return this.notificationSentCounter.longValue();
233 public final long getUpdateMessagesReceivedCount() {
234 return this.updateReceivedCounter.longValue();
238 public final long getNotificationMessagesReceivedCount() {
239 return this.notificationReceivedCounter.longValue();
243 public final void messageSent(final Notification msg) {
244 if (msg instanceof Notify) {
245 this.notificationSentCounter.increment();
246 } else if (msg instanceof Update) {
247 this.updateSentCounter.increment();
252 public final void messageReceived(final Notification msg) {
253 if (msg instanceof Notify) {
254 this.notificationReceivedCounter.increment();
255 } else if (msg instanceof Update) {
256 this.updateReceivedCounter.increment();