Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / state / BGPPeerStateImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bgp.rib.impl.state;
10
11 import static java.util.Objects.requireNonNull;
12
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;
19 import java.util.Map;
20 import java.util.Set;
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;
44
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;
58
59     @GuardedBy("this")
60     private final Map<TablesKey, PrefixesSentCounters> prefixesSent = new HashMap<>();
61     @GuardedBy("this")
62     private PrefixesReceivedCounters prefixesReceived;
63     @GuardedBy("this")
64     private PrefixesInstalledCounters prefixesInstalled;
65     @GuardedBy("this")
66     private boolean localRestarting;
67     @GuardedBy("this")
68     private int peerRestartTime;
69     @GuardedBy("this")
70     private boolean peerRestarting;
71
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);
81     }
82
83     @Override
84     public final String getGroupId() {
85         return this.groupId;
86     }
87
88     @Override
89     public final IpAddress getNeighborAddress() {
90         return this.neighborAddress;
91     }
92
93     @Override
94     public final synchronized long getTotalPrefixes() {
95         if (this.prefixesInstalled == null) {
96             return NONE;
97         }
98         return this.prefixesInstalled.getTotalPrefixesInstalled();
99     }
100
101     @Override
102     public final BGPPeerMessagesState getBGPPeerMessagesState() {
103         return this;
104     }
105
106     @Override
107     public final BGPGracelfulRestartState getBGPGracelfulRestart() {
108         return this;
109     }
110
111     @Override
112     public final synchronized boolean isAfiSafiSupported(final TablesKey tablesKey) {
113         return this.prefixesReceived != null && this.prefixesReceived.isSupported(tablesKey) &&
114             this.afiSafisAdvertized.contains(tablesKey);
115     }
116
117     @Override
118     public final synchronized long getPrefixesInstalledCount(final TablesKey tablesKey) {
119         if (this.prefixesInstalled == null) {
120             return NONE;
121         }
122         return this.prefixesInstalled.getPrefixedInstalledCount(tablesKey);
123     }
124
125     @Override
126     public final synchronized long getPrefixesSentCount(@Nonnull final TablesKey tablesKey) {
127         if (this.prefixesSent == null) {
128             return 0;
129         }
130         final PrefixesSentCounters counter = this.prefixesSent.get(tablesKey);
131         if (counter == null) {
132             return NONE;
133         }
134         return counter.getPrefixesSentCount();
135     }
136
137     @Override
138     public final synchronized long getPrefixesReceivedCount(final TablesKey tablesKey) {
139         if (this.prefixesReceived == null) {
140             return NONE;
141         }
142         return this.prefixesReceived.getPrefixedReceivedCount(tablesKey);
143     }
144
145     @Override
146     public final Set<TablesKey> getAfiSafisAdvertized() {
147         return ImmutableSet.copyOf(this.afiSafisAdvertized);
148     }
149
150     @Override
151     public final synchronized Set<TablesKey> getAfiSafisReceived() {
152         if (this.prefixesReceived == null) {
153             return Collections.emptySet();
154         }
155         return this.prefixesReceived.getTableKeys();
156     }
157
158     @Override
159     public final boolean isGracefulRestartAdvertized(final TablesKey tablesKey) {
160         return this.afiSafisGracefulAdvertized.contains(tablesKey);
161     }
162
163     @Override
164     public final boolean isGracefulRestartReceived(final TablesKey tablesKey) {
165         return this.afiSafisGracefulReceived.contains(tablesKey);
166     }
167
168     @Override
169     public final synchronized boolean isLocalRestarting() {
170         return this.localRestarting;
171     }
172
173     @Override
174     public final synchronized int getPeerRestartTime() {
175         return this.peerRestartTime;
176     }
177
178     @Override
179     public final synchronized boolean isPeerRestarting() {
180         return this.peerRestarting;
181     }
182
183     //FIXME BUG-196
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;
189     }
190
191     protected final synchronized void setAdvertizedGracefulRestartTableTypes(final List<TablesKey> receivedGraceful) {
192         this.afiSafisGracefulReceived.addAll(receivedGraceful);
193     }
194
195     protected final synchronized void registerPrefixesSentCounter(final TablesKey tablesKey,
196         final PrefixesSentCounters prefixesSentCounter) {
197         this.prefixesSent.put(tablesKey, prefixesSentCounter);
198     }
199
200     protected final synchronized void registerPrefixesCounters(@Nonnull final PrefixesReceivedCounters prefixesReceived,
201         @Nonnull final PrefixesInstalledCounters prefixesInstalled) {
202         this.prefixesReceived = prefixesReceived;
203         this.prefixesInstalled = prefixesInstalled;
204     }
205
206     protected final synchronized void resetState() {
207         this.localRestarting = false;
208         this.peerRestartTime = 0;
209         this.peerRestarting = false;
210     }
211
212     @Override
213     public final BGPPeerState getPeerState() {
214         return this;
215     }
216
217     @Override
218     public final long getErroneousUpdateReceivedCount() {
219         //FIXME BUG-4979
220         return this.erroneousUpdate.longValue();
221     }
222
223     @Override
224     public final long getUpdateMessagesSentCount() {
225         return this.updateSentCounter.longValue();
226     }
227
228     @Override
229     public final long getNotificationMessagesSentCount() {
230         return this.notificationSentCounter.longValue();
231     }
232
233     @Override
234     public final long getUpdateMessagesReceivedCount() {
235         return this.updateReceivedCounter.longValue();
236     }
237
238     @Override
239     public final long getNotificationMessagesReceivedCount() {
240         return this.notificationReceivedCounter.longValue();
241     }
242
243     @Override
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();
249         }
250     }
251
252     @Override
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();
258         }
259     }
260 }