Bump versions by x.y.(z+1)
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / AbstractBGPSessionNegotiator.java
index d99872f27cfd5c4ee0853b1e5ee734e11a89ee62..cef181ab081f9f491a793992a493e7be62a0ee32 100644 (file)
@@ -10,24 +10,24 @@ package org.opendaylight.protocol.bgp.rib.impl;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
-
 import io.netty.channel.Channel;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
 import io.netty.util.concurrent.Promise;
-
+import io.netty.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
-
 import javax.annotation.concurrent.GuardedBy;
-
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPError;
-import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
 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.BGPSessionValidator;
-import org.opendaylight.protocol.framework.AbstractSessionNegotiator;
+import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
+import org.opendaylight.protocol.bgp.rib.spi.SessionNegotiator;
+import org.opendaylight.protocol.util.Ipv6Util;
 import org.opendaylight.protocol.util.Values;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.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;
@@ -39,12 +39,12 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Bgp Session negotiator. Common for local -> remote and remote -> local connections.
+ * Bgp Session negotiator. Common for local-to-remote and remote-to-local connections.
  * One difference is session validation performed by injected BGPSessionValidator when OPEN message is received.
  */
-public abstract class AbstractBGPSessionNegotiator extends AbstractSessionNegotiator<Notification, BGPSessionImpl> {
+abstract class AbstractBGPSessionNegotiator extends ChannelInboundHandlerAdapter implements SessionNegotiator {
     // 4 minutes recommended in http://tools.ietf.org/html/rfc4271#section-8.2.2
-    protected static final int INITIAL_HOLDTIMER = 4;
+    private static final int INITIAL_HOLDTIMER = 4;
 
     /**
      * @see <a href="http://tools.ietf.org/html/rfc6793">BGP Support for 4-Octet AS Number Space</a>
@@ -56,94 +56,108 @@ public abstract class AbstractBGPSessionNegotiator extends AbstractSessionNegoti
         /**
          * Negotiation has not started yet.
          */
-        Idle,
+        IDLE,
         /**
          * We have sent our Open message, and are waiting for the peer's Open message.
          */
-        OpenSent,
+        OPEN_SENT,
         /**
          * We have received the peer's Open message, which is acceptable, and we're waiting the acknowledgement of our
          * Open message.
          */
-        OpenConfirm,
+        OPEN_CONFIRM,
         /**
          * The negotiation finished.
          */
-        Finished,
+        FINISHED,
     }
 
     private static final Logger LOG = LoggerFactory.getLogger(AbstractBGPSessionNegotiator.class);
     private final BGPPeerRegistry registry;
-    private final BGPSessionValidator sessionValidator;
-
+    private final Promise<BGPSessionImpl> promise;
+    private final Channel channel;
     @GuardedBy("this")
-    private State state = State.Idle;
-
+    private State state = State.IDLE;
     @GuardedBy("this")
     private BGPSessionImpl session;
+    @GuardedBy("this")
+    private ScheduledFuture<?> pending;
 
-    public AbstractBGPSessionNegotiator(final Promise<BGPSessionImpl> promise, final Channel channel,
-            final BGPPeerRegistry registry, final BGPSessionValidator sessionValidator) {
-        super(promise, channel);
+    AbstractBGPSessionNegotiator(final Promise<BGPSessionImpl> promise, final Channel channel,
+            final BGPPeerRegistry registry) {
+        this.promise = Preconditions.checkNotNull(promise);
+        this.channel = Preconditions.checkNotNull(channel);
         this.registry = registry;
-        this.sessionValidator = sessionValidator;
     }
 
-    @Override
-    protected synchronized void startNegotiation() {
-        Preconditions.checkState(this.state == State.Idle);
-
-        // Check if peer is configured in registry before retrieving preferences
-        if (!registry.isPeerConfigured(getRemoteIp())) {
-            final BGPDocumentedException cause = new BGPDocumentedException(
-                    "BGP peer with ip: " + getRemoteIp()
-                    + " not configured, check configured peers in : "
-                    + registry, BGPError.CEASE);
-            negotiationFailed(cause);
+    private synchronized void startNegotiation() {
+        if (!(this.state == State.IDLE || this.state == State.OPEN_CONFIRM)) {
             return;
         }
+        // Open can be sent first either from ODL (IDLE) or from peer (OPEN_CONFIRM)
+        final IpAddress remoteIp = getRemoteIp();
+        try {
+            // Check if peer is configured in registry before retrieving preferences
+            if (!this.registry.isPeerConfigured(remoteIp)) {
+                final BGPDocumentedException cause = new BGPDocumentedException(
+                    String.format("BGP peer with ip: %s not configured, check configured peers in : %s", remoteIp, this.registry), BGPError.CONNECTION_REJECTED);
+                negotiationFailed(cause);
+                return;
+            }
 
-        final BGPSessionPreferences preferences = getPreferences();
+            final BGPSessionPreferences preferences = this.registry.getPeerPreferences(remoteIp);
 
-        int as = preferences.getMyAs().getValue().intValue();
-        // Set as AS_TRANS if the value is bigger than 2B
-        if (as > Values.UNSIGNED_SHORT_MAX_VALUE) {
-            as = AS_TRANS;
-        }
-        this.sendMessage(new OpenBuilder().setMyAsNumber(as).setHoldTimer(preferences.getHoldTime()).setBgpIdentifier(
+            int as = preferences.getMyAs().getValue().intValue();
+            // Set as AS_TRANS if the value is bigger than 2B
+            if (as > Values.UNSIGNED_SHORT_MAX_VALUE) {
+                as = AS_TRANS;
+            }
+            sendMessage(new OpenBuilder().setMyAsNumber(as).setHoldTimer(preferences.getHoldTime()).setBgpIdentifier(
                 preferences.getBgpId()).setBgpParameters(preferences.getParams()).build());
-        this.state = State.OpenSent;
-
-        this.channel.eventLoop().schedule(new Runnable() {
-            @Override
-            public void run() {
-                if (AbstractBGPSessionNegotiator.this.state != State.Finished) {
-                    AbstractBGPSessionNegotiator.this.sendMessage(buildErrorNotify(BGPError.HOLD_TIMER_EXPIRED));
-                    negotiationFailed(new BGPDocumentedException("HoldTimer expired", BGPError.FSM_ERROR));
-                    AbstractBGPSessionNegotiator.this.state = State.Finished;
-                }
+            if (this.state != State.FINISHED) {
+                this.state = State.OPEN_SENT;
+                this.pending = this.channel.eventLoop().schedule(() -> {
+                    synchronized (AbstractBGPSessionNegotiator.this) {
+                        AbstractBGPSessionNegotiator.this.pending = null;
+                        if (AbstractBGPSessionNegotiator.this.state != State.FINISHED) {
+                            AbstractBGPSessionNegotiator.this
+                                .sendMessage(buildErrorNotify(BGPError.HOLD_TIMER_EXPIRED));
+                            negotiationFailed(new BGPDocumentedException("HoldTimer expired", BGPError.FSM_ERROR));
+                            AbstractBGPSessionNegotiator.this.state = State.FINISHED;
+                        }
+                    }
+                }, INITIAL_HOLDTIMER, TimeUnit.MINUTES);
             }
-        }, INITIAL_HOLDTIMER, TimeUnit.MINUTES);
-    }
-
-    private BGPSessionPreferences getPreferences() {
-        return registry.getPeerPreferences(getRemoteIp());
+        } catch (final Exception e) {
+            LOG.warn("Unexpected negotiation failure", e);
+            negotiationFailedCloseChannel(e);
+        }
     }
 
     private IpAddress getRemoteIp() {
-        return StrictBGPPeerRegistry.getIpAddress(channel.remoteAddress());
+        final IpAddress remoteIp = StrictBGPPeerRegistry.getIpAddress(this.channel.remoteAddress());
+        if (remoteIp.getIpv6Address() != null) {
+            return new IpAddress(Ipv6Util.getFullForm(remoteIp.getIpv6Address()));
+        }
+        return remoteIp;
     }
 
-    @Override
     protected synchronized void handleMessage(final Notification msg) {
-        LOG.debug("Channel {} handling message in state {}", this.channel, this.state);
-
+        LOG.debug("Channel {} handling message in state {}, msg: {}", this.channel, this.state, msg);
         switch (this.state) {
-        case Finished:
-        case Idle:
-            this.sendMessage(buildErrorNotify(BGPError.FSM_ERROR));
+        case FINISHED:
+            sendMessage(buildErrorNotify(BGPError.FSM_ERROR));
             return;
-        case OpenConfirm:
+        case IDLE:
+            // to avoid race condition when Open message was sent by the peer before startNegotiation could be executed
+            if (msg instanceof Open) {
+                startNegotiation();
+                handleOpen((Open) msg);
+                return;
+            }
+            sendMessage(buildErrorNotify(BGPError.FSM_ERROR));
+            break;
+        case OPEN_CONFIRM:
             if (msg instanceof Keepalive) {
                 negotiationSuccessful(this.session);
                 LOG.info("BGP Session with peer {} established successfully.", this.channel);
@@ -151,66 +165,134 @@ public abstract class AbstractBGPSessionNegotiator extends AbstractSessionNegoti
                 final Notify ntf = (Notify) msg;
                 negotiationFailed(new BGPDocumentedException("Peer refusal", BGPError.forValue(ntf.getErrorCode(), ntf.getErrorSubcode())));
             }
-            this.state = State.Finished;
+            this.state = State.FINISHED;
             return;
-        case OpenSent:
+        case OPEN_SENT:
             if (msg instanceof Open) {
-                final Open openObj = (Open) msg;
-                handleOpen(openObj);
+                handleOpen((Open) msg);
                 return;
             }
             break;
+        default:
+            break;
         }
 
         // Catch-all for unexpected message
         LOG.warn("Channel {} state {} unexpected message {}", this.channel, this.state, msg);
-        this.sendMessage(buildErrorNotify(BGPError.FSM_ERROR));
-        negotiationFailed(new BGPDocumentedException("Unexpected message", BGPError.FSM_ERROR));
-        this.state = State.Finished;
+        sendMessage(buildErrorNotify(BGPError.FSM_ERROR));
+        negotiationFailed(new BGPDocumentedException("Unexpected message channel: " + this.channel + ", state: " + this.state + ", message: " + msg, BGPError.FSM_ERROR));
+        this.state = State.FINISHED;
     }
 
     private static Notify buildErrorNotify(final BGPError err) {
-        return new NotifyBuilder().setErrorCode(err.getCode()).setErrorSubcode(err.getSubcode()).build();
+        return buildErrorNotify(err, null);
     }
 
-    private void handleOpen(final Open openObj) {
-        try {
-            sessionValidator.validate(openObj, getPreferences());
-        } catch (final BGPDocumentedException e) {
-            negotiationFailed(e);
-            return;
+    private static Notify buildErrorNotify(final BGPError err, final byte[] data) {
+        final NotifyBuilder builder = new NotifyBuilder().setErrorCode(err.getCode()).setErrorSubcode(err.getSubcode());
+        if (data != null && data.length != 0) {
+            builder.setData(data);
         }
+        return builder.build();
+    }
 
+    private synchronized void handleOpen(final Open openObj) {
+        final IpAddress remoteIp = getRemoteIp();
+        final BGPSessionPreferences preferences = this.registry.getPeerPreferences(remoteIp);
         try {
-            final BGPSessionListener peer = registry.getPeer(getRemoteIp(), getSourceId(openObj, getPreferences()), getDestinationId(openObj, getPreferences()));
-            this.sendMessage(new KeepaliveBuilder().build());
-            this.session = new BGPSessionImpl(peer, this.channel, openObj, getPreferences().getHoldTime());
-            this.state = State.OpenConfirm;
-            LOG.debug("Channel {} moved to OpenConfirm state with remote proposal {}", this.channel, openObj);
+            final BGPSessionListener peer = this.registry.getPeer(remoteIp, getSourceId(openObj, preferences), getDestinationId(openObj, preferences), openObj);
+            sendMessage(new KeepaliveBuilder().build());
+            this.state = State.OPEN_CONFIRM;
+            this.session = new BGPSessionImpl(peer, this.channel, openObj, preferences, this.registry);
+            this.session.setChannelExtMsgCoder(openObj);
+            LOG.debug("Channel {} moved to OPEN_CONFIRM state with remote proposal {}", this.channel, openObj);
         } catch (final BGPDocumentedException e) {
             LOG.warn("Channel {} negotiation failed", this.channel, e);
             negotiationFailed(e);
         }
     }
 
-    private void negotiationFailed(final BGPDocumentedException e) {
+    private synchronized void negotiationFailed(final Throwable e) {
         LOG.warn("Channel {} negotiation failed: {}", this.channel, e.getMessage());
-        this.sendMessage(buildErrorNotify(e.getError()));
-        super.negotiationFailed(e);
-        this.state = State.Finished;
+        if (e instanceof BGPDocumentedException) {
+            // although sendMessage() can also result in calling this method, it won't create a cycle. In case sendMessage() fails to
+            // deliver the message, this method gets called with different exception (definitely not with BGPDocumentedException).
+            sendMessage(buildErrorNotify(((BGPDocumentedException)e).getError(), ((BGPDocumentedException) e).getData()));
+        }
+        if (this.state == State.OPEN_CONFIRM) {
+            this.registry.removePeerSession(getRemoteIp());
+        }
+        negotiationFailedCloseChannel(e);
+        this.state = State.FINISHED;
     }
 
     /**
+     * @param openMsg Open message received from remote BGP speaker
+     * @param preferences Local BGP speaker preferences
      * @return BGP Id of device that accepted the connection
      */
     protected abstract Ipv4Address getDestinationId(final Open openMsg, final BGPSessionPreferences preferences);
 
     /**
-     * @return BGP Id of device that initiated the connection
+     * @param openMsg Open message received from remote BGP speaker
+     * @param preferences Local BGP speaker preferences
+     * @return BGP Id of device that accepted the connection
      */
     protected abstract Ipv4Address getSourceId(final Open openMsg, final BGPSessionPreferences preferences);
 
     public synchronized State getState() {
         return this.state;
     }
+
+    private void negotiationSuccessful(final BGPSessionImpl session) {
+        LOG.debug("Negotiation on channel {} successful with session {}", this.channel, session);
+        this.channel.pipeline().replace(this, "session", session);
+        this.promise.setSuccess(session);
+    }
+
+    private void negotiationFailedCloseChannel(final Throwable cause) {
+        LOG.debug("Negotiation on channel {} failed", this.channel, cause);
+        this.channel.close();
+        synchronized (AbstractBGPSessionNegotiator.this) {
+            if (this.pending != null && this.pending.isCancellable()) {
+                this.pending.cancel(true);
+                this.pending = null;
+            }
+        }
+    }
+
+    private void sendMessage(final Notification msg) {
+        this.channel.writeAndFlush(msg).addListener((ChannelFutureListener) f -> {
+            if (!f.isSuccess()) {
+                LOG.warn("Failed to send message {} to channel {}", msg,  AbstractBGPSessionNegotiator.this.channel, f.cause());
+                negotiationFailedCloseChannel(f.cause());
+            } else {
+                LOG.trace("Message {} sent to channel {}", msg, AbstractBGPSessionNegotiator.this.channel);
+            }
+        });
+    }
+
+    @Override
+    public final void channelActive(final ChannelHandlerContext ctx) {
+        LOG.debug("Starting session negotiation on channel {}", this.channel);
+        startNegotiation();
+    }
+
+    @Override
+    public final void channelRead(final ChannelHandlerContext ctx, final Object msg) {
+        LOG.debug("Negotiation read invoked on channel {}", this.channel);
+        try {
+            handleMessage((Notification) msg);
+        } catch (final Exception e) {
+            LOG.debug("Unexpected error while handling negotiation message {}", msg, e);
+            negotiationFailedCloseChannel(e);
+        }
+
+    }
+
+    @Override
+    public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
+        LOG.info("Unexpected error during negotiation", cause);
+        negotiationFailedCloseChannel(cause);
+    }
 }