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