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