Fix findbug issues
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPSessionImpl.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.base.Preconditions;
16 import com.google.common.collect.Lists;
17 import com.google.common.collect.Sets;
18 import io.netty.buffer.ByteBufUtil;
19 import io.netty.channel.Channel;
20 import io.netty.channel.ChannelFuture;
21 import io.netty.channel.ChannelFutureListener;
22 import io.netty.channel.ChannelHandlerContext;
23 import io.netty.channel.ChannelPipeline;
24 import io.netty.channel.SimpleChannelInboundHandler;
25 import java.io.IOException;
26 import java.nio.channels.NonWritableChannelException;
27 import java.util.Collections;
28 import java.util.Date;
29 import java.util.List;
30 import java.util.Set;
31 import java.util.concurrent.TimeUnit;
32 import javax.annotation.concurrent.GuardedBy;
33 import org.opendaylight.protocol.bgp.parser.AsNumberUtil;
34 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
35 import org.opendaylight.protocol.bgp.parser.BGPError;
36 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
37 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
38 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
39 import org.opendaylight.protocol.bgp.parser.spi.pojo.MultiPathSupportImpl;
40 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPMessagesListener;
41 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
42 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
43 import org.opendaylight.protocol.bgp.rib.impl.state.BGPSessionStateImpl;
44 import org.opendaylight.protocol.bgp.rib.impl.state.BGPSessionStateProvider;
45 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
46 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
47 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
48 import org.opendaylight.protocol.bgp.rib.spi.State;
49 import org.opendaylight.protocol.bgp.rib.spi.state.BGPSessionState;
50 import org.opendaylight.protocol.bgp.rib.spi.state.BGPTimersState;
51 import org.opendaylight.protocol.bgp.rib.spi.state.BGPTransportState;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Keepalive;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.KeepaliveBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Notify;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.NotifyBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Open;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Update;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParameters;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.OptionalCapabilities;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.optional.capabilities.CParameters;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.BgpTableType;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.CParameters1;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.RouteRefresh;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.mp.capabilities.AddPathCapability;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.mp.capabilities.MultiprotocolCapability;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.mp.capabilities.add.path.capability.AddressFamilies;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.TablesKey;
70 import org.opendaylight.yangtools.yang.binding.Notification;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73
74 @VisibleForTesting
75 public class BGPSessionImpl extends SimpleChannelInboundHandler<Notification> implements BGPSession,
76         BGPSessionStateProvider, AutoCloseable {
77
78     private static final Logger LOG = LoggerFactory.getLogger(BGPSessionImpl.class);
79
80     private static final Notification KEEP_ALIVE = new KeepaliveBuilder().build();
81
82     private static final int KA_TO_DEADTIMER_RATIO = 3;
83
84     private static final String EXTENDED_MSG_DECODER = "EXTENDED_MSG_DECODER";
85
86     static final String END_OF_INPUT = "End of input detected. Close the session.";
87
88     /**
89      * System.nanoTime value about when was sent the last message.
90      */
91     @VisibleForTesting
92     private long lastMessageSentAt;
93
94     /**
95      * System.nanoTime value about when was received the last message
96      */
97     private long lastMessageReceivedAt;
98
99     private final BGPSessionListener listener;
100
101     private final BGPSynchronization sync;
102
103     private int kaCounter = 0;
104
105     private final Channel channel;
106
107     @GuardedBy("this")
108     private State state = State.OPEN_CONFIRM;
109
110     private final Set<BgpTableType> tableTypes;
111     private final List<AddressFamilies> addPathTypes;
112     private final int holdTimerValue;
113     private final int keepAlive;
114     private final AsNumber asNumber;
115     private final Ipv4Address bgpId;
116     private final BGPPeerRegistry peerRegistry;
117     private final ChannelOutputLimiter limiter;
118     private final BGPSessionStateImpl sessionState;
119     private boolean terminationReasonNotified;
120
121     public BGPSessionImpl(final BGPSessionListener listener, final Channel channel, final Open remoteOpen,
122             final BGPSessionPreferences localPreferences, final BGPPeerRegistry peerRegistry) {
123         this(listener, channel, remoteOpen, localPreferences.getHoldTime(), peerRegistry);
124     }
125
126     public BGPSessionImpl(final BGPSessionListener listener, final Channel channel, final Open remoteOpen,
127             final int localHoldTimer, final BGPPeerRegistry peerRegistry) {
128         this.listener = requireNonNull(listener);
129         this.channel = requireNonNull(channel);
130         this.limiter = new ChannelOutputLimiter(this);
131         this.channel.pipeline().addLast(this.limiter);
132         this.holdTimerValue = (remoteOpen.getHoldTimer() < localHoldTimer) ? remoteOpen.getHoldTimer() : localHoldTimer;
133         LOG.info("BGP HoldTimer new value: {}", this.holdTimerValue);
134         this.keepAlive = this.holdTimerValue / KA_TO_DEADTIMER_RATIO;
135         this.asNumber = AsNumberUtil.advertizedAsNumber(remoteOpen);
136         this.peerRegistry = peerRegistry;
137         this.sessionState = new BGPSessionStateImpl();
138
139         final Set<TablesKey> tts = Sets.newHashSet();
140         final Set<BgpTableType> tats = Sets.newHashSet();
141         final List<AddressFamilies> addPathCapabilitiesList = Lists.newArrayList();
142         if (remoteOpen.getBgpParameters() != null) {
143             for (final BgpParameters param : remoteOpen.getBgpParameters()) {
144                 for (final OptionalCapabilities optCapa : param.getOptionalCapabilities()) {
145                     final CParameters cParam = optCapa.getCParameters();
146                     if (cParam.getAugmentation(CParameters1.class) == null) {
147                         continue;
148                     }
149                     if (cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability() != null) {
150                         final MultiprotocolCapability multi = cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability();
151                         final TablesKey tt = new TablesKey(multi.getAfi(), multi.getSafi());
152                         LOG.trace("Added table type to sync {}", tt);
153                         tts.add(tt);
154                         tats.add(new BgpTableTypeImpl(tt.getAfi(), tt.getSafi()));
155                     } else if (cParam.getAugmentation(CParameters1.class).getAddPathCapability() != null) {
156                         final AddPathCapability addPathCap = cParam.getAugmentation(CParameters1.class).getAddPathCapability();
157                         addPathCapabilitiesList.addAll(addPathCap.getAddressFamilies());
158                     }
159                 }
160             }
161         }
162
163         this.sync = new BGPSynchronization(this.listener, tts);
164         this.tableTypes = tats;
165         this.addPathTypes = addPathCapabilitiesList;
166
167         if (!this.addPathTypes.isEmpty()) {
168             final ChannelPipeline pipeline = this.channel.pipeline();
169             final BGPByteToMessageDecoder decoder = pipeline.get(BGPByteToMessageDecoder.class);
170             decoder.addDecoderConstraint(MultiPathSupport.class,
171                     MultiPathSupportImpl.createParserMultiPathSupport(this.addPathTypes));
172         }
173
174         if (this.holdTimerValue != 0) {
175             channel.eventLoop().schedule(this::handleHoldTimer, this.holdTimerValue, TimeUnit.SECONDS);
176             channel.eventLoop().schedule(this::handleKeepaliveTimer, this.keepAlive, TimeUnit.SECONDS);
177         }
178         this.bgpId = remoteOpen.getBgpIdentifier();
179         this.sessionState.advertizeCapabilities(this.holdTimerValue, channel.remoteAddress(), channel.localAddress(),
180                 this.tableTypes, remoteOpen.getBgpParameters());
181     }
182
183     /**
184      * Set the extend message coder for current channel
185      * The reason for separating this part from constructor is, in #channel.pipeline().replace(..), the
186      * invokeChannelRead() will be invoked after the original message coder handler got removed. And there
187      * is chance that before the session instance is fully initiated (constructor returns), a KeepAlive
188      * message arrived already in the channel buffer. Thus #AbstractBGPSessionNegotiator.handleMessage(..)
189      * gets invoked again and a deadlock is caused.  A BGP final state machine error will happen as BGP
190      * negotiator is still in OPEN_SENT state as the session constructor hasn't returned yet.
191      *
192      * @param remoteOpen
193      */
194     public synchronized void setChannelExtMsgCoder(final Open remoteOpen) {
195         final boolean enableExMess = BgpExtendedMessageUtil.advertizedBgpExtendedMessageCapability(remoteOpen);
196         if (enableExMess) {
197             this.channel.pipeline().replace(BGPMessageHeaderDecoder.class, EXTENDED_MSG_DECODER,
198                     BGPMessageHeaderDecoder.getExtendedBGPMessageHeaderDecoder());
199         }
200     }
201
202     @Override
203     public synchronized void close() {
204         if (this.state != State.IDLE && !this.terminationReasonNotified) {
205             this.writeAndFlush(new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode())
206                     .setErrorSubcode(BGPError.CEASE.getSubcode()).build());
207             this.closeWithoutMessage();
208         }
209     }
210
211     /**
212      * Handles incoming message based on their type.
213      *
214      * @param msg incoming message
215      */
216     synchronized void handleMessage(final Notification msg) {
217         if (this.state == State.IDLE) {
218             return;
219         }
220         try {
221             // Update last reception time
222             this.lastMessageReceivedAt = System.nanoTime();
223
224             if (msg instanceof Open) {
225                 // Open messages should not be present here
226                 this.terminate(new BGPDocumentedException(null, BGPError.FSM_ERROR));
227             } else if (msg instanceof Notify) {
228                 final Notify notify = (Notify) msg;
229                 // Notifications are handled internally
230                 LOG.info("Session closed because Notification message received: {} / {}, data={}",
231                         notify.getErrorCode(),
232                         notify.getErrorSubcode(),
233                         notify.getData() != null ? ByteBufUtil.hexDump(notify.getData()) : null);
234                 notifyTerminationReasonAndCloseWithoutMessage(notify.getErrorCode(), notify.getErrorSubcode());
235             } else if (msg instanceof Keepalive) {
236                 // Keepalives are handled internally
237                 LOG.trace("Received KeepAlive message.");
238                 this.kaCounter++;
239                 if (this.kaCounter >= 2) {
240                     this.sync.kaReceived();
241                 }
242             } else if (msg instanceof RouteRefresh) {
243                 this.listener.onMessage(this, msg);
244             } else if (msg instanceof Update) {
245                 this.listener.onMessage(this, msg);
246                 this.sync.updReceived((Update) msg);
247             } else {
248                 LOG.warn("Ignoring unhandled message: {}.", msg.getClass());
249             }
250
251             this.sessionState.messageReceived(msg);
252         } catch (final BGPDocumentedException e) {
253             this.terminate(e);
254         }
255     }
256
257     private synchronized void notifyTerminationReasonAndCloseWithoutMessage(
258             final Short errorCode,
259             final Short errorSubcode) {
260         this.terminationReasonNotified = true;
261         this.listener.onSessionTerminated(this, new BGPTerminationReason(
262                 BGPError.forValue(errorCode, errorSubcode)));
263         this.closeWithoutMessage();
264     }
265
266     synchronized void endOfInput() {
267         if (this.state == State.UP) {
268             LOG.info(END_OF_INPUT);
269             this.listener.onSessionDown(this, new IOException(END_OF_INPUT));
270         }
271     }
272
273     @GuardedBy("this")
274     private ChannelFuture writeEpilogue(final ChannelFuture future, final Notification msg) {
275         future.addListener(
276                 (ChannelFutureListener) f -> {
277                     if (!f.isSuccess()) {
278                         LOG.warn("Failed to send message {} to socket {}", msg, BGPSessionImpl.this.channel, f.cause());
279                     } else {
280                         LOG.trace("Message {} sent to socket {}", msg, BGPSessionImpl.this.channel);
281                     }
282                 });
283         this.lastMessageSentAt = System.nanoTime();
284         this.sessionState.messageSent(msg);
285         return future;
286     }
287
288     void flush() {
289         this.channel.flush();
290     }
291
292     synchronized void write(final Notification msg) {
293         try {
294             writeEpilogue(this.channel.write(msg), msg);
295         } catch (final Exception e) {
296             LOG.warn("Message {} was not sent.", msg, e);
297         }
298     }
299
300     synchronized ChannelFuture writeAndFlush(final Notification msg) {
301         if (isWritable()) {
302             return writeEpilogue(this.channel.writeAndFlush(msg), msg);
303         }
304         return this.channel.newFailedFuture(new NonWritableChannelException());
305     }
306
307     private synchronized void closeWithoutMessage() {
308         if (this.state == State.IDLE) {
309             return;
310         }
311         LOG.info("Closing session: {}", this);
312         this.channel.close().addListener((ChannelFutureListener) future -> Preconditions.checkArgument(future.isSuccess(), "Channel failed to close: %s", future.cause()));
313         this.state = State.IDLE;
314         removePeerSession();
315         this.sessionState.setSessionState(this.state);
316     }
317
318     /**
319      * Closes BGP session from the parent with given reason. A message needs to be sent, but parent doesn't have to be
320      * modified, because he initiated the closing. (To prevent concurrent modification exception).
321      *
322      * @param e BGPDocumentedException
323      */
324     private synchronized void terminate(final BGPDocumentedException e) {
325         final BGPError error = e.getError();
326         final byte[] data = e.getData();
327         final NotifyBuilder builder = new NotifyBuilder().setErrorCode(error.getCode()).setErrorSubcode(error.getSubcode());
328         if (data != null && data.length != 0) {
329             builder.setData(data);
330         }
331         this.writeAndFlush(builder.build());
332         notifyTerminationReasonAndCloseWithoutMessage(error.getCode(), error.getSubcode());
333     }
334
335     private void removePeerSession() {
336         if (this.peerRegistry != null) {
337             this.peerRegistry.removePeerSession(StrictBGPPeerRegistry.getIpAddress(this.channel.remoteAddress()));
338         }
339     }
340
341     /**
342      * If HoldTimer expires, the session ends. If a message (whichever) was received during this period, the HoldTimer
343      * will be rescheduled by HOLD_TIMER_VALUE + the time that has passed from the start of the HoldTimer to the time at
344      * which the message was received. If the session was closed by the time this method starts to execute (the session
345      * state will become IDLE), then rescheduling won't occur.
346      */
347     private synchronized void handleHoldTimer() {
348         if (this.state == State.IDLE) {
349             return;
350         }
351
352         final long ct = System.nanoTime();
353         final long nextHold = this.lastMessageReceivedAt + TimeUnit.SECONDS.toNanos(this.holdTimerValue);
354
355         if (ct >= nextHold) {
356             LOG.debug("HoldTimer expired. {}", new Date());
357             this.terminate(new BGPDocumentedException(BGPError.HOLD_TIMER_EXPIRED));
358         } else {
359             this.channel.eventLoop().schedule(this::handleHoldTimer, nextHold - ct, TimeUnit.NANOSECONDS);
360         }
361     }
362
363     /**
364      * If KeepAlive Timer expires, sends KeepAlive message. If a message (whichever) was send during this period, the
365      * KeepAlive Timer will be rescheduled by KEEP_ALIVE_TIMER_VALUE + the time that has passed from the start of the
366      * KeepAlive timer to the time at which the message was sent. If the session was closed by the time this method
367      * starts to execute (the session state will become IDLE), that rescheduling won't occur.
368      */
369     private synchronized void handleKeepaliveTimer() {
370         if (this.state == State.IDLE) {
371             return;
372         }
373
374         final long ct = System.nanoTime();
375         long nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(this.keepAlive);
376
377         if (ct >= nextKeepalive) {
378             this.writeAndFlush(KEEP_ALIVE);
379             nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(this.keepAlive);
380         }
381         this.channel.eventLoop().schedule(this::handleKeepaliveTimer, nextKeepalive - ct, TimeUnit.NANOSECONDS);
382     }
383
384     @Override
385     public final String toString() {
386         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
387     }
388
389     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
390         toStringHelper.add("channel", this.channel);
391         toStringHelper.add("state", this.getState());
392         return toStringHelper;
393     }
394
395     @Override
396     public Set<BgpTableType> getAdvertisedTableTypes() {
397         return this.tableTypes;
398     }
399
400     @Override
401     public List<AddressFamilies> getAdvertisedAddPathTableTypes() {
402         return this.addPathTypes;
403     }
404
405     @Override
406     public List<BgpTableType> getAdvertisedGracefulRestartTableTypes() {
407         return Collections.emptyList();
408     }
409
410     @VisibleForTesting
411     synchronized void sessionUp() {
412         this.state = State.UP;
413         this.sessionState.setSessionState(this.state);
414         this.listener.onSessionUp(this);
415     }
416
417     public synchronized State getState() {
418         return this.state;
419     }
420
421     @Override
422     public final Ipv4Address getBgpId() {
423         return this.bgpId;
424     }
425
426     @Override
427     public final AsNumber getAsNumber() {
428         return this.asNumber;
429     }
430
431     private synchronized boolean isWritable() {
432         return this.channel != null && this.channel.isWritable();
433     }
434
435     public ChannelOutputLimiter getLimiter() {
436         return this.limiter;
437     }
438
439     @Override
440     public final void channelInactive(final ChannelHandlerContext ctx) {
441         LOG.debug("Channel {} inactive.", ctx.channel());
442         this.endOfInput();
443
444         try {
445             super.channelInactive(ctx);
446         } catch (final Exception e) {
447             throw new IllegalStateException("Failed to delegate channel inactive event on channel " + ctx.channel(), e);
448         }
449     }
450
451     @Override
452     protected final void channelRead0(final ChannelHandlerContext ctx, final Notification msg) {
453         LOG.debug("Message was received: {}", msg);
454         this.handleMessage(msg);
455     }
456
457     @Override
458     public final void handlerAdded(final ChannelHandlerContext ctx) {
459         this.sessionUp();
460     }
461
462     @Override
463     public synchronized void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
464         LOG.warn("BGP session encountered error", cause);
465         final Throwable docCause = cause.getCause();
466         if (docCause instanceof BGPDocumentedException) {
467             this.terminate((BGPDocumentedException) docCause);
468         } else {
469             this.close();
470         }
471     }
472
473     @Override
474     public BGPSessionState getBGPSessionState() {
475         return this.sessionState;
476     }
477
478     @Override
479     public BGPTimersState getBGPTimersState() {
480         return this.sessionState;
481     }
482
483     @Override
484     public BGPTransportState getBGPTransportState() {
485         return this.sessionState;
486     }
487
488     @Override
489     public void registerMessagesCounter(final BGPMessagesListener bgpMessagesListener) {
490         this.sessionState.registerMessagesCounter(bgpMessagesListener);
491     }
492 }