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