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