BUG-5243: Failed to send message Notify
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPSessionImpl.java
index a2bf6a5d5b224c8b0b3096a222535cb5896584b7..cfc01e536d527ee0932af30004bb2003fd24b6fe 100644 (file)
  */
 package org.opendaylight.protocol.bgp.rib.impl;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.MoreObjects.ToStringHelper;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Sets;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
 import io.netty.channel.ChannelHandlerContext;
-
+import io.netty.channel.SimpleChannelInboundHandler;
 import java.io.IOException;
 import java.util.Date;
 import java.util.Set;
-import java.util.Timer;
-import java.util.TimerTask;
-
-import org.opendaylight.protocol.bgp.concepts.BGPTableType;
-import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.concurrent.GuardedBy;
+import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
+import org.opendaylight.protocol.bgp.parser.AsNumberUtil;
 import org.opendaylight.protocol.bgp.parser.BGPError;
-import org.opendaylight.protocol.bgp.parser.BGPMessage;
-import org.opendaylight.protocol.bgp.parser.BGPParameter;
-import org.opendaylight.protocol.bgp.parser.BGPSession;
-import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
-import org.opendaylight.protocol.bgp.parser.message.BGPKeepAliveMessage;
-import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage;
-import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
-import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
-import org.opendaylight.protocol.bgp.rib.impl.spi.BGPConnection;
+import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
+import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
-import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionProposalChecker;
-import org.opendaylight.protocol.framework.DeserializerException;
-import org.opendaylight.protocol.framework.DocumentedException;
-import org.opendaylight.protocol.framework.ProtocolMessage;
-import org.opendaylight.protocol.framework.ProtocolMessageFactory;
-import org.opendaylight.protocol.framework.ProtocolSession;
-import org.opendaylight.protocol.framework.ProtocolSessionOutboundHandler;
-import org.opendaylight.protocol.framework.SessionParent;
+import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionStatistics;
+import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
+import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
+import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.MultiprotocolCapability;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
+import org.opendaylight.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.Sets;
-
-class BGPSessionImpl implements BGPSession, ProtocolSession {
-
-       private static final Logger logger = LoggerFactory.getLogger(BGPSessionImpl.class);
-
-       /**
-        * KeepAlive Timer is to be scheduled periodically, each time it starts, it sends KeepAlive Message.
-        */
-       private class KeepAliveTimer extends TimerTask {
-               private final BGPSessionImpl parent;
-
-               public KeepAliveTimer(final BGPSessionImpl parent) {
-                       this.parent = parent;
-               }
-
-               @Override
-               public void run() {
-                       this.parent.handleKeepaliveTimer();
-               }
-       }
-
-       /**
-        * HoldTimer is to be scheduled periodically, when it expires, it closes BGP session.
-        */
-       private class HoldTimer extends TimerTask {
-               private final BGPSessionImpl parent;
-
-               public HoldTimer(final BGPSessionImpl parent) {
-                       this.parent = parent;
-               }
-
-               @Override
-               public void run() {
-                       this.parent.handleHoldTimer();
-               }
-       }
-
-       private static final int DEFAULT_HOLD_TIMER_VALUE = 15;
-
-       public static int HOLD_TIMER_VALUE = DEFAULT_HOLD_TIMER_VALUE; // 240
-
-       public int KEEP_ALIVE_TIMER_VALUE;
-
-       /**
-        * Possible states for Finite State Machine
-        */
-       private enum State {
-               IDLE, OPEN_SENT, OPEN_CONFIRM, ESTABLISHED
-       }
-
-       /**
-        * Actual state of the FSM.
-        */
-       private State state;
-
-       /**
-        * System.nanoTime value about when was sent the last message Protected to be updated also in tests.
-        */
-       protected long lastMessageSentAt;
-
-       /**
-        * System.nanoTime value about when was received the last message
-        */
-       private long lastMessageReceivedAt;
-
-       private final int sessionId;
-
-       private final BGPSessionListener listener;
-
-       /**
-        * Open message with session characteristics that were accepted by another BGP (sent from this session).
-        */
-       private BGPSessionPreferences localOpen = null;
-
-       /**
-        * Open Object with session characteristics for this session (sent from another BGP speaker).
-        */
-       private BGPSessionPreferences remoteOpen = null;
-
-       /**
-        * Timer object grouping FSM Timers
-        */
-       private final Timer stateTimer;
-
-       private final SessionParent parent;
-
-       private final ProtocolMessageFactory parser;
-
-       private final BGPSessionProposalChecker checker;
-
-       private final BGPSynchronization sync;
-
-       private final ProtocolSessionOutboundHandler handler;
-
-       private int kaCounter = 0;
-
-       private final ChannelHandlerContext ctx;
-
-       BGPSessionImpl(final SessionParent parent, final Timer timer, final BGPConnection connection, final int sessionId,
-                       final ProtocolMessageFactory parser, final ChannelHandlerContext ctx) {
-               this.state = State.IDLE;
-               this.listener = connection.getListener();
-               this.sessionId = sessionId;
-               this.localOpen = connection.getProposal();
-               this.stateTimer = timer;
-               this.parent = parent;
-               this.parser = parser;
-               this.ctx = ctx;
-               this.checker = connection.getProposalChecker();
-               this.sync = new BGPSynchronization(this.listener);
-               this.handler = new ProtocolSessionOutboundHandler();
-       }
-
-       @Override
-       public void close() {
-               logger.debug("Closing session: " + this);
-               if (this.state == State.ESTABLISHED) {
-                       this.sendMessage(new BGPNotificationMessage(BGPError.CEASE));
-               }
-               this.changeState(State.IDLE);
-               this.parent.onSessionClosed(this);
-       }
-
-       @Override
-       public void startSession() {
-               logger.debug("Session started.");
-               this.sendMessage(new BGPOpenMessage(this.localOpen.getMyAs(), (short) this.localOpen.getHoldTime(), this.localOpen.getBgpId(), this.localOpen.getParams()));
-               this.stateTimer.schedule(new HoldTimer(this), DEFAULT_HOLD_TIMER_VALUE * 1000);
-               this.changeState(State.OPEN_SENT);
-       }
-
-       /**
-        * Handles incoming message based on their type.
-        * 
-        * @param msg incoming message
-        */
-       @Override
-       public void handleMessage(final ProtocolMessage msg) {
-               final BGPMessage bgpMsg = (BGPMessage) msg;
-               // Update last reception time
-               this.lastMessageReceivedAt = System.nanoTime();
-
-               // Open messages are handled internally, but are parsed also in bgp-parser, so notify bgp listener
-               if (bgpMsg instanceof BGPOpenMessage) {
-                       this.handleOpenMessage((BGPOpenMessage) bgpMsg);
-               }
-               // Keepalives are handled internally
-               else if (bgpMsg instanceof BGPKeepAliveMessage) {
-                       this.handleKeepAliveMessage();
-               }
-               // Notifications are handled internally
-               else if (bgpMsg instanceof BGPNotificationMessage) {
-                       logger.info("Session closed because Notification message received: {}" + ((BGPNotificationMessage) bgpMsg).getError());
-                       this.closeWithoutMessage();
-                       this.listener.onSessionTerminated(((BGPNotificationMessage) bgpMsg).getError());
-               } else {
-                       this.listener.onMessage(bgpMsg);
-               }
-       }
-
-       @Override
-       public void handleMalformedMessage(final DeserializerException e) {
-               logger.warn("Received malformed message: {}", e.getMessage(), e);
-               this.terminate(BGPError.FSM_ERROR);
-       }
-
-       @Override
-       public void handleMalformedMessage(final DocumentedException e) {
-               logger.warn("Received malformed message: {}", e.getMessage(), e);
-               this.terminate(((BGPDocumentedException) e).getError());
-       }
-
-       @Override
-       public void endOfInput() {
-               if (this.state != State.IDLE) {
-                       this.listener.onSessionDown(this, new IOException("End of input detected. Close the session."));
-               }
-       }
-
-       @Override
-       public ProtocolMessageFactory getMessageFactory() {
-               return this.parser;
-       }
-
-       @Override
-       public void onConnectionFailed(final IOException e) {
-               logger.info("Connection failed before finishing: {}", e.getMessage(), e);
-               this.listener.onSessionDown(this, e);
-       }
-
-       @Override
-       public int maximumMessageSize() {
-               return 4096;
-       }
-
-       void sendMessage(final BGPMessage msg) {
-               try {
-                       this.handler.writeDown(this.ctx, msg);
-                       this.lastMessageSentAt = System.nanoTime();
-                       logger.debug("Sent message: " + msg);
-               } catch (final Exception e) {
-                       logger.warn("Message {} was not sent.", msg, e);
-               }
-       }
-
-       private void closeWithoutMessage() {
-               logger.debug("Closing session: " + this);
-               HOLD_TIMER_VALUE = DEFAULT_HOLD_TIMER_VALUE;
-               this.changeState(State.IDLE);
-               this.parent.onSessionClosed(this);
-       }
-
-       /**
-        * Closes PCEP session from the parent with given reason. A message needs to be sent, but parent doesn't have to be
-        * modified, because he initiated the closing. (To prevent concurrent modification exception).
-        * 
-        * @param closeObject
-        */
-       private void terminate(final BGPError error) {
-               this.sendMessage(new BGPNotificationMessage(error));
-               this.closeWithoutMessage();
-               this.listener.onSessionTerminated(error);
-       }
-
-       /**
-        * If HoldTimer expires, the session ends. If a message (whichever) was received during this period, the HoldTimer
-        * will be rescheduled by HOLD_TIMER_VALUE + the time that has passed from the start of the HoldTimer to the time at
-        * which the message was received. If the session was closed by the time this method starts to execute (the session
-        * state will become IDLE), then rescheduling won't occur.
-        */
-       private synchronized void handleHoldTimer() {
-               final long ct = System.nanoTime();
-
-               final long nextHold = (long) (this.lastMessageReceivedAt + (HOLD_TIMER_VALUE * 1E9));
-
-               if (this.state != State.IDLE) {
-                       if (ct >= nextHold) {
-                               logger.debug("HoldTimer expired. " + new Date());
-                               this.terminate(BGPError.HOLD_TIMER_EXPIRED);
-                               return;
-                       }
-                       this.stateTimer.schedule(new HoldTimer(this), (long) ((nextHold - ct) / 1E6));
-               }
-       }
-
-       /**
-        * If KeepAlive Timer expires, sends KeepAlive message. If a message (whichever) was send during this period, the
-        * KeepAlive Timer will be rescheduled by KEEP_ALIVE_TIMER_VALUE + the time that has passed from the start of the
-        * KeepAlive timer to the time at which the message was sent. If the session was closed by the time this method
-        * starts to execute (the session state will become IDLE), that rescheduling won't occur.
-        */
-       private synchronized void handleKeepaliveTimer() {
-               final long ct = System.nanoTime();
-
-               long nextKeepalive = (long) (this.lastMessageSentAt + (this.KEEP_ALIVE_TIMER_VALUE * 1E9));
-
-               if (this.state == State.ESTABLISHED) {
-                       if (ct >= nextKeepalive) {
-                               this.sendMessage(new BGPKeepAliveMessage());
-                               nextKeepalive = (long) (this.lastMessageSentAt + (this.KEEP_ALIVE_TIMER_VALUE * 1E9));
-                       }
-                       this.stateTimer.schedule(new KeepAliveTimer(this), (long) ((nextKeepalive - ct) / 1E6));
-               }
-       }
-
-       private void changeState(final State finalState) {
-               final String desc = "Changed to state: ";
-               switch (finalState) {
-               case IDLE:
-                       logger.debug(desc + State.IDLE);
-                       this.state = State.IDLE;
-                       return;
-               case OPEN_SENT:
-                       logger.debug(desc + State.OPEN_SENT);
-                       if (this.state != State.IDLE) {
-                               throw new IllegalArgumentException("Cannot change state from " + this.state + " to " + State.OPEN_SENT);
-                       }
-                       this.state = State.OPEN_SENT;
-                       return;
-               case OPEN_CONFIRM:
-                       logger.debug(desc + State.OPEN_CONFIRM);
-                       if (this.state == State.ESTABLISHED) {
-                               throw new IllegalArgumentException("Cannot change state from " + this.state + " to " + State.OPEN_CONFIRM);
-                       }
-                       this.state = State.OPEN_CONFIRM;
-                       return;
-               case ESTABLISHED:
-                       logger.debug(desc + State.ESTABLISHED);
-                       if (this.state != State.OPEN_CONFIRM) {
-                               throw new IllegalArgumentException("Cannot change state from " + this.state + " to " + State.ESTABLISHED);
-                       }
-                       this.state = State.ESTABLISHED;
-                       return;
-               }
-       }
-
-       /**
-        * Open message should be handled only if the FSM is in OPEN_SENT or IDLE state. When in IDLE state, the Open
-        * message was received _before_ local Open message was sent. When in OPEN_SENT state, the message was received
-        * _after_ local Open message was sent.
-        * 
-        * @param msg received Open Message.
-        */
-       private void handleOpenMessage(final BGPOpenMessage msg) {
-               this.remoteOpen = new BGPSessionPreferences(msg.getMyAS(), msg.getHoldTime(), msg.getBgpId(), msg.getOptParams());
-               logger.debug("Received message: {}", msg.toString());
-               if (this.state != State.IDLE && this.state != State.OPEN_SENT) {
-                       this.terminate(BGPError.FSM_ERROR);
-                       return;
-               }
-               // if the session characteristics were unacceptable, the session is terminated
-               // with given BGP error
-               try {
-                       this.checker.checkSessionCharacteristics(this.remoteOpen);
-               } catch (final BGPDocumentedException e) {
-                       this.terminate(e.getError());
-               }
-               // the session characteristics were acceptable
-               HOLD_TIMER_VALUE = this.remoteOpen.getHoldTime();
-               logger.debug("Session chars are acceptable. Overwriting: holdtimer: {}", HOLD_TIMER_VALUE);
-               // when in IDLE state, we haven't send Open Message yet, do it now
-               if (this.state == State.IDLE) {
-                       this.sendMessage(new BGPOpenMessage(this.localOpen.getMyAs(), (short) this.localOpen.getHoldTime(), this.localOpen.getBgpId(), this.localOpen.getParams()));
-               }
-               this.sendMessage(new BGPKeepAliveMessage());
-               // if the timer is not disabled
-               if (HOLD_TIMER_VALUE != 0) {
-                       this.KEEP_ALIVE_TIMER_VALUE = HOLD_TIMER_VALUE / 3;
-                       this.stateTimer.schedule(new KeepAliveTimer(this), this.KEEP_ALIVE_TIMER_VALUE * 1000);
-                       this.stateTimer.schedule(new HoldTimer(this), HOLD_TIMER_VALUE * 1000);
-               }
-               this.changeState(State.OPEN_CONFIRM);
-       }
-
-       /**
-        * KeepAlive message should be explicitly parsed in FSM when its state is OPEN_CONFIRM. Otherwise is handled by the
-        * KeepAliveTimer or it's invalid.
-        */
-       private void handleKeepAliveMessage() {
-               logger.debug("Received KeepAlive messsage.");
-               if (this.state == State.OPEN_CONFIRM) {
-                       if (HOLD_TIMER_VALUE != 0) {
-                               this.stateTimer.schedule(new HoldTimer(this), HOLD_TIMER_VALUE * 1000);
-                               this.stateTimer.schedule(new KeepAliveTimer(this), this.KEEP_ALIVE_TIMER_VALUE * 1000);
-                       }
-                       this.changeState(State.ESTABLISHED);
-                       final Set<BGPTableType> tts = Sets.newHashSet();
-                       if (this.remoteOpen.getParams() != null) {
-                               for (final BGPParameter param : this.remoteOpen.getParams()) {
-                                       if (param instanceof MultiprotocolCapability) {
-                                               tts.add(((MultiprotocolCapability) param).getTableType());
-                                       }
-                               }
-                       }
-                       this.sync.addTableTypes(tts);
-                       this.listener.onSessionUp(tts);
-                       // check if the KA is EOR for some AFI/SAFI
-               } else if (this.state == State.ESTABLISHED) {
-                       this.kaCounter++;
-                       if (this.kaCounter >= 2) {
-                               this.sync.kaReceived();
-                       }
-               }
-       }
-
-       @Override
-       public String toString() {
-               final StringBuilder builder = new StringBuilder();
-               builder.append("BGPSessionImpl [state=");
-               builder.append(this.state);
-               builder.append(", sessionId=");
-               builder.append(this.sessionId);
-               builder.append(", localOpen=");
-               builder.append(this.localOpen);
-               builder.append(", remoteOpen=");
-               builder.append(this.remoteOpen);
-               builder.append("]");
-               return builder.toString();
-       }
+@VisibleForTesting
+public class BGPSessionImpl extends SimpleChannelInboundHandler<Notification> implements BGPSession, BGPSessionStatistics, AutoCloseable {
+
+    private static final Logger LOG = LoggerFactory.getLogger(BGPSessionImpl.class);
+
+    private static final Notification KEEP_ALIVE = new KeepaliveBuilder().build();
+
+    private static final int KA_TO_DEADTIMER_RATIO = 3;
+
+    static final String END_OF_INPUT = "End of input detected. Close the session.";
+
+    /**
+     * Internal session state.
+     */
+    public enum State {
+        /**
+         * The session object is created by the negotiator in OpenConfirm state. While in this state, the session object
+         * is half-alive, e.g. the timers are running, but the session is not completely up, e.g. it has not been
+         * announced to the listener. If the session is torn down in this state, we do not inform the listener.
+         */
+        OPEN_CONFIRM,
+        /**
+         * The session has been completely established.
+         */
+        UP,
+        /**
+         * The session has been closed. It will not be resurrected.
+         */
+        IDLE,
+    }
+
+    /**
+     * System.nanoTime value about when was sent the last message.
+     */
+    @VisibleForTesting
+    private long lastMessageSentAt;
+
+    /**
+     * System.nanoTime value about when was received the last message
+     */
+    private long lastMessageReceivedAt;
+
+    private final BGPSessionListener listener;
+
+    private final BGPSynchronization sync;
+
+    private int kaCounter = 0;
+
+    private final Channel channel;
+
+    @GuardedBy("this")
+    private State state = State.OPEN_CONFIRM;
+
+    private final Set<BgpTableType> tableTypes;
+    private final int holdTimerValue;
+    private final int keepAlive;
+    private final AsNumber asNumber;
+    private final Ipv4Address bgpId;
+    private final BGPPeerRegistry peerRegistry;
+    private final ChannelOutputLimiter limiter;
+
+    private BGPSessionStats sessionStats;
+
+    public BGPSessionImpl(final BGPSessionListener listener, final Channel channel, final Open remoteOpen, final BGPSessionPreferences localPreferences,
+            final BGPPeerRegistry peerRegistry) {
+        this(listener, channel, remoteOpen, localPreferences.getHoldTime(), peerRegistry);
+        this.sessionStats = new BGPSessionStats(remoteOpen, this.holdTimerValue, this.keepAlive, channel, Optional.of(localPreferences), this.tableTypes);
+    }
+
+    public BGPSessionImpl(final BGPSessionListener listener, final Channel channel, final Open remoteOpen, final int localHoldTimer,
+            final BGPPeerRegistry peerRegistry) {
+        this.listener = Preconditions.checkNotNull(listener);
+        this.channel = Preconditions.checkNotNull(channel);
+        this.limiter = new ChannelOutputLimiter(this);
+        this.channel.pipeline().addLast(this.limiter);
+        this.holdTimerValue = (remoteOpen.getHoldTimer() < localHoldTimer) ? remoteOpen.getHoldTimer() : localHoldTimer;
+        LOG.info("BGP HoldTimer new value: {}", this.holdTimerValue);
+        this.keepAlive = this.holdTimerValue / KA_TO_DEADTIMER_RATIO;
+        this.asNumber = AsNumberUtil.advertizedAsNumber(remoteOpen);
+        this.peerRegistry = peerRegistry;
+
+        final Set<TablesKey> tts = Sets.newHashSet();
+        final Set<BgpTableType> tats = Sets.newHashSet();
+        if (remoteOpen.getBgpParameters() != null) {
+            for (final BgpParameters param : remoteOpen.getBgpParameters()) {
+                for (final OptionalCapabilities optCapa : param.getOptionalCapabilities()) {
+                    final CParameters cParam = optCapa.getCParameters();
+                    if ( cParam.getAugmentation(CParameters1.class) == null ||
+                            cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability() == null ) {
+                        continue;
+                    }
+                    final MultiprotocolCapability multi = cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability();
+                    final TablesKey tt = new TablesKey(multi.getAfi(), multi.getSafi());
+                    LOG.trace("Added table type to sync {}", tt);
+                    tts.add(tt);
+                    tats.add(new BgpTableTypeImpl(tt.getAfi(), tt.getSafi()));
+                }
+            }
+        }
+
+        this.sync = new BGPSynchronization(this.listener, tts);
+        this.tableTypes = tats;
+
+        if (this.holdTimerValue != 0) {
+            channel.eventLoop().schedule(new Runnable() {
+                @Override
+                public void run() {
+                    handleHoldTimer();
+                }
+            }, this.holdTimerValue, TimeUnit.SECONDS);
+
+            channel.eventLoop().schedule(new Runnable() {
+                @Override
+                public void run() {
+                    handleKeepaliveTimer();
+                }
+            }, this.keepAlive, TimeUnit.SECONDS);
+        }
+        this.bgpId = remoteOpen.getBgpIdentifier();
+        this.sessionStats = new BGPSessionStats(remoteOpen, this.holdTimerValue, this.keepAlive, channel, Optional.<BGPSessionPreferences>absent(),
+                this.tableTypes);
+    }
+
+    @Override
+    public synchronized void close() {
+        if (this.state != State.IDLE && this.channel.isActive()) {
+            this.writeAndFlush(new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).setErrorSubcode(
+                BGPError.CEASE.getSubcode()).build());
+        }
+        this.closeWithoutMessage();
+    }
+
+    /**
+     * Handles incoming message based on their type.
+     *
+     * @param msg incoming message
+     */
+    public synchronized void handleMessage(final Notification msg) {
+        // Update last reception time
+        this.lastMessageReceivedAt = System.nanoTime();
+        this.sessionStats.updateReceivedMsgTotal();
+
+        if (msg instanceof Open) {
+            // Open messages should not be present here
+            this.terminate(BGPError.FSM_ERROR);
+        } else if (msg instanceof Notify) {
+            // Notifications are handled internally
+            LOG.info("Session closed because Notification message received: {} / {}", ((Notify) msg).getErrorCode(),
+                ((Notify) msg).getErrorSubcode());
+            this.closeWithoutMessage();
+            this.listener.onSessionTerminated(this, new BGPTerminationReason(BGPError.forValue(((Notify) msg).getErrorCode(),
+                ((Notify) msg).getErrorSubcode())));
+            this.sessionStats.updateReceivedMsgErr((Notify) msg);
+        } else if (msg instanceof Keepalive) {
+            // Keepalives are handled internally
+            LOG.trace("Received KeepAlive messsage.");
+            this.kaCounter++;
+            this.sessionStats.updateReceivedMsgKA();
+            if (this.kaCounter >= 2) {
+                this.sync.kaReceived();
+            }
+        } else {
+            // All others are passed up
+            this.listener.onMessage(this, msg);
+            this.sync.updReceived((Update) msg);
+            this.sessionStats.updateReceivedMsgUpd();
+        }
+    }
+
+    public synchronized void endOfInput() {
+        if (this.state == State.UP) {
+            LOG.info(END_OF_INPUT);
+            this.listener.onSessionDown(this, new IOException(END_OF_INPUT));
+        }
+    }
+
+    @GuardedBy("this")
+    private void writeEpilogue(final ChannelFuture future, final Notification msg) {
+        future.addListener(
+            new ChannelFutureListener() {
+                @Override
+                public void operationComplete(final ChannelFuture f) {
+                    if (!f.isSuccess()) {
+                        LOG.warn("Failed to send message {} to socket {}", msg, f.cause(), BGPSessionImpl.this.channel);
+                    } else {
+                        LOG.trace("Message {} sent to socket {}", msg, BGPSessionImpl.this.channel);
+                    }
+                }
+            });
+        this.lastMessageSentAt = System.nanoTime();
+        this.sessionStats.updateSentMsgTotal();
+        if (msg instanceof Update) {
+            this.sessionStats.updateSentMsgUpd();
+        } else if (msg instanceof Notify) {
+            this.sessionStats.updateSentMsgErr((Notify) msg);
+        }
+    }
+
+    void flush() {
+        this.channel.flush();
+    }
+
+    synchronized void write(final Notification msg) {
+        try {
+            writeEpilogue(this.channel.write(msg), msg);
+        } catch (final Exception e) {
+            LOG.warn("Message {} was not sent.", msg, e);
+        }
+    }
+
+    synchronized void writeAndFlush(final Notification msg) {
+        writeEpilogue(this.channel.writeAndFlush(msg), msg);
+    }
+
+    private synchronized void closeWithoutMessage() {
+        LOG.info("Closing session: {}", this);
+        removePeerSession();
+        this.channel.close();
+        this.state = State.IDLE;
+    }
+
+    /**
+     * Closes PCEP session from the parent with given reason. A message needs to be sent, but parent doesn't have to be
+     * modified, because he initiated the closing. (To prevent concurrent modification exception).
+     *
+     * @param error
+     */
+    private void terminate(final BGPError error) {
+        this.writeAndFlush(new NotifyBuilder().setErrorCode(error.getCode()).setErrorSubcode(error.getSubcode()).build());
+        this.closeWithoutMessage();
+
+        this.listener.onSessionTerminated(this, new BGPTerminationReason(error));
+    }
+
+    private void removePeerSession() {
+        if (this.peerRegistry != null) {
+            this.peerRegistry.removePeerSession(StrictBGPPeerRegistry.getIpAddress(this.channel.remoteAddress()));
+        }
+    }
+
+    /**
+     * If HoldTimer expires, the session ends. If a message (whichever) was received during this period, the HoldTimer
+     * will be rescheduled by HOLD_TIMER_VALUE + the time that has passed from the start of the HoldTimer to the time at
+     * which the message was received. If the session was closed by the time this method starts to execute (the session
+     * state will become IDLE), then rescheduling won't occur.
+     */
+    private synchronized void handleHoldTimer() {
+        if (this.state == State.IDLE) {
+            return;
+        }
+
+        final long ct = System.nanoTime();
+        final long nextHold = this.lastMessageReceivedAt + TimeUnit.SECONDS.toNanos(this.holdTimerValue);
+
+        if (ct >= nextHold) {
+            LOG.debug("HoldTimer expired. {}", new Date());
+            this.terminate(BGPError.HOLD_TIMER_EXPIRED);
+        } else {
+            this.channel.eventLoop().schedule(new Runnable() {
+                @Override
+                public void run() {
+                    handleHoldTimer();
+                }
+            }, nextHold - ct, TimeUnit.NANOSECONDS);
+        }
+    }
+
+    /**
+     * If KeepAlive Timer expires, sends KeepAlive message. If a message (whichever) was send during this period, the
+     * KeepAlive Timer will be rescheduled by KEEP_ALIVE_TIMER_VALUE + the time that has passed from the start of the
+     * KeepAlive timer to the time at which the message was sent. If the session was closed by the time this method
+     * starts to execute (the session state will become IDLE), that rescheduling won't occur.
+     */
+    private synchronized void handleKeepaliveTimer() {
+        if (this.state == State.IDLE) {
+            return;
+        }
+
+        final long ct = System.nanoTime();
+        long nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(this.keepAlive);
+
+        if (ct >= nextKeepalive) {
+            this.writeAndFlush(KEEP_ALIVE);
+            nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(this.keepAlive);
+            this.sessionStats.updateSentMsgKA();
+        }
+        this.channel.eventLoop().schedule(new Runnable() {
+            @Override
+            public void run() {
+                handleKeepaliveTimer();
+            }
+        }, nextKeepalive - ct, TimeUnit.NANOSECONDS);
+    }
+
+    @Override
+    public final String toString() {
+        return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
+    }
+
+    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+        toStringHelper.add("channel", this.channel);
+        toStringHelper.add("state", this.getState());
+        return toStringHelper;
+    }
+
+    @Override
+    public Set<BgpTableType> getAdvertisedTableTypes() {
+        return this.tableTypes;
+    }
+
+    protected synchronized void sessionUp() {
+        this.sessionStats.startSessionStopwatch();
+        this.state = State.UP;
+        this.listener.onSessionUp(this);
+    }
+
+    public synchronized State getState() {
+        return this.state;
+    }
+
+    @Override
+    public final Ipv4Address getBgpId() {
+        return this.bgpId;
+    }
+
+    @Override
+    public final AsNumber getAsNumber() {
+        return this.asNumber;
+    }
+
+    synchronized boolean isWritable() {
+        return this.channel != null && this.channel.isWritable();
+    }
+
+    void schedule(final Runnable task) {
+        Preconditions.checkState(this.channel != null);
+        this.channel.eventLoop().submit(task);
+    }
+
+    @VisibleForTesting
+    protected synchronized void setLastMessageSentAt(final long lastMessageSentAt) {
+        this.lastMessageSentAt = lastMessageSentAt;
+    }
+
+    @Override
+    public synchronized BgpSessionState getBgpSesionState() {
+        return this.sessionStats.getBgpSessionState(this.state);
+    }
+
+    @Override
+    public synchronized void resetSessionStats() {
+        this.sessionStats.resetStats();
+    }
+
+    ChannelOutputLimiter getLimiter() {
+        return this.limiter;
+    }
+
+    @Override
+    public final void channelInactive(final ChannelHandlerContext ctx) {
+        LOG.debug("Channel {} inactive.", ctx.channel());
+        this.endOfInput();
+
+        try {
+            super.channelInactive(ctx);
+        } catch (final Exception e) {
+            throw new IllegalStateException("Failed to delegate channel inactive event on channel " + ctx.channel(), e);
+        }
+    }
+
+    @Override
+    protected final void channelRead0(final ChannelHandlerContext ctx, final Notification msg) {
+        LOG.debug("Message was received: {}", msg);
+        this.handleMessage(msg);
+    }
+
+    @Override
+    public final void handlerAdded(final ChannelHandlerContext ctx) {
+        this.sessionUp();
+    }
 }