Merge "Make sure we include maven plugin in execution"
authorTony Tkacik <ttkacik@cisco.com>
Mon, 1 Dec 2014 09:02:42 +0000 (09:02 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 1 Dec 2014 09:02:42 +0000 (09:02 +0000)
opendaylight/commons/protocol-framework/src/main/java/org/opendaylight/protocol/framework/ReconnectPromise.java
opendaylight/commons/protocol-framework/src/test/java/org/opendaylight/protocol/framework/ServerTest.java
opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/AbstractBindingAwareConsumer.java
opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/AbstractBindingAwareProvider.java
opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/listener/NetconfDeviceCommunicatorTest.java
opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java
opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java
opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/netty/SSHTest.java
opendaylight/networkconfiguration/neutron/northbound/src/main/java/org/opendaylight/controller/networkconfig/neutron/northbound/NeutronLoadBalancerPoolNorthbound.java

index 98a2c2cca16cbc8c085e530af206aa497c9e5d49..b2ab27a82671cdc0c2380ac8f1084e540ee61691 100644 (file)
@@ -80,7 +80,7 @@ final class ReconnectPromise<S extends ProtocolSession<?>, L extends SessionList
 
     /**
      * Channel handler that responds to channelInactive event and reconnects the session.
-     * Only if the initial connection was successfully established and promise was not canceled.
+     * Only if the promise was not canceled.
      */
     private static final class ClosedChannelHandler extends ChannelInboundHandlerAdapter {
         private final ReconnectPromise<?, ?> promise;
index fc38888de30af0158d9b4d20fc7e6387d73767eb..6c4af0186f911a672902a2e7ad941d5791b95938 100644 (file)
@@ -250,6 +250,52 @@ public class ServerTest {
         assertFalse(session.isSuccess());
     }
 
+    @Test
+    public void testNegotiationFailedReconnect() throws Exception {
+        final Promise<Boolean> p = new DefaultPromise<>(GlobalEventExecutor.INSTANCE);
+
+        this.dispatcher = getServerDispatcher(p);
+
+        this.server = this.dispatcher.createServer(this.serverAddress, new SessionListenerFactory<SimpleSessionListener>() {
+            @Override
+            public SimpleSessionListener getSessionListener() {
+                return new SimpleSessionListener();
+            }
+        });
+
+        this.server.get();
+
+        this.clientDispatcher = new SimpleDispatcher(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
+            @Override
+            public SessionNegotiator<SimpleSession> getSessionNegotiator(final SessionListenerFactory<SimpleSessionListener> factory,
+                                                                         final Channel channel, final Promise<SimpleSession> promise) {
+
+                return new SimpleSessionNegotiator(promise, channel) {
+                    @Override
+                    protected void startNegotiation() throws Exception {
+                        negotiationFailed(new IllegalStateException("Negotiation failed"));
+                    }
+                };
+            }
+        }, new DefaultPromise<SimpleSession>(GlobalEventExecutor.INSTANCE), eventLoopGroup);
+
+        final ReconnectStrategyFactory reconnectStrategyFactory = mock(ReconnectStrategyFactory.class);
+        final ReconnectStrategy reconnectStrategy = getMockedReconnectStrategy();
+        doReturn(reconnectStrategy).when(reconnectStrategyFactory).createReconnectStrategy();
+
+        this.clientDispatcher.createReconnectingClient(this.serverAddress,
+                reconnectStrategyFactory, new SessionListenerFactory<SimpleSessionListener>() {
+                    @Override
+                    public SimpleSessionListener getSessionListener() {
+                        return new SimpleSessionListener();
+                    }
+                });
+
+
+        // Reconnect strategy should be consulted at least twice, for initial connect and reconnect attempts after drop
+        verify(reconnectStrategyFactory, timeout((int) TimeUnit.MINUTES.toMillis(3)).atLeast(2)).createReconnectStrategy();
+    }
+
     private SimpleDispatcher getClientDispatcher() {
         return new SimpleDispatcher(new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() {
             @Override
index 02bb6b35b9165e29aa22f077488c1969354a2175..fc9d9fa2bae6a7db9991020fb62aefcda2e50d49 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.controller.sal.binding.api;
 
 import org.osgi.framework.BundleContext;
 
+@Deprecated
 public abstract class AbstractBindingAwareConsumer extends AbstractBrokerAwareActivator implements BindingAwareConsumer {
 
     @Override
index 068b6c204f3d75db69a57dcd0db873548d158343..d32fa36b668e352bc2b5142650a2d0cb42f743df 100644 (file)
@@ -15,6 +15,7 @@ import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderCo
 import org.opendaylight.yangtools.yang.binding.RpcService;
 import org.osgi.framework.BundleContext;
 
+@Deprecated
 public abstract class AbstractBindingAwareProvider extends AbstractBrokerAwareActivator implements BindingAwareProvider {
 
     @Override
index 001b9a8d3abc411b041d3f3624ea27505722560c..a24034d2f009700bd6d8bd3dd152783e19f0bfcd 100644 (file)
@@ -19,6 +19,8 @@ import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.timeout;
 import static org.mockito.Mockito.verify;
 import static org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants.RPC_REPLY_KEY;
 import static org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
@@ -27,9 +29,15 @@ import com.google.common.base.Strings;
 import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.ListenableFuture;
 import io.netty.channel.ChannelFuture;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.util.HashedWheelTimer;
+import io.netty.util.Timer;
 import io.netty.util.concurrent.Future;
 import io.netty.util.concurrent.GenericFutureListener;
+import io.netty.util.concurrent.GlobalEventExecutor;
 import java.io.ByteArrayInputStream;
+import java.net.InetSocketAddress;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.UUID;
@@ -45,11 +53,18 @@ import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
+import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
 import org.opendaylight.controller.netconf.client.NetconfClientSession;
+import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
+import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
+import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
 import org.opendaylight.controller.sal.connect.api.RemoteDevice;
 import org.opendaylight.controller.sal.connect.api.RemoteDeviceCommunicator;
 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
+import org.opendaylight.protocol.framework.ReconnectStrategy;
+import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
+import org.opendaylight.protocol.framework.TimedReconnectStrategy;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
@@ -320,6 +335,60 @@ public class NetconfDeviceCommunicatorTest {
                       errorInfo.contains( "<bad-element>bar</bad-element>" ) );
     }
 
+    /**
+     * Test whether reconnect is scheduled properly
+     */
+    @Test
+    public void testNetconfDeviceReconnectInCommunicator() throws Exception {
+        final RemoteDevice<NetconfSessionCapabilities, NetconfMessage> device = mock(RemoteDevice.class);
+
+        final TimedReconnectStrategy timedReconnectStrategy = new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, 10000, 0, 1.0, null, 100L, null);
+        final ReconnectStrategy reconnectStrategy = spy(new ReconnectStrategy() {
+            @Override
+            public int getConnectTimeout() throws Exception {
+                return timedReconnectStrategy.getConnectTimeout();
+            }
+
+            @Override
+            public Future<Void> scheduleReconnect(final Throwable cause) {
+                return timedReconnectStrategy.scheduleReconnect(cause);
+            }
+
+            @Override
+            public void reconnectSuccessful() {
+                timedReconnectStrategy.reconnectSuccessful();
+            }
+        });
+
+        final NetconfDeviceCommunicator listener = new NetconfDeviceCommunicator(new RemoteDeviceId("test"), device);
+        final EventLoopGroup group = new NioEventLoopGroup();
+        final Timer time = new HashedWheelTimer();
+        try {
+            final NetconfClientConfiguration cfg = NetconfReconnectingClientConfigurationBuilder.create()
+                    .withAddress(new InetSocketAddress("localhost", 65000))
+                    .withReconnectStrategy(reconnectStrategy)
+                    .withConnectStrategyFactory(new ReconnectStrategyFactory() {
+                        @Override
+                        public ReconnectStrategy createReconnectStrategy() {
+                            return reconnectStrategy;
+                        }
+                    })
+                    .withAuthHandler(new LoginPassword("admin", "admin"))
+                    .withConnectionTimeoutMillis(10000)
+                    .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH)
+                    .withSessionListener(listener)
+                    .build();
+
+
+            listener.initializeRemoteConnection(new NetconfClientDispatcherImpl(group, group, time), cfg);
+
+            verify(reconnectStrategy, timeout((int) TimeUnit.MINUTES.toMillis(3)).times(101)).scheduleReconnect(any(Throwable.class));
+        } finally {
+            time.stop();
+            group.shutdownGracefully();
+        }
+    }
+
     @Test
     public void testOnResponseMessageWithWrongMessageID() throws Exception {
         setupSession();
index 14d753f1f8b59c25a7ea871e3bfe5e430c1d9970..c8c912828279e72eab9ac12dba25e2a29e2c10d4 100644 (file)
@@ -137,7 +137,6 @@ public class AsyncSshHandler extends ChannelOutboundHandlerAdapter {
         LOG.trace("SSH subsystem channel opened successfully on channel: {}", ctx.channel());
 
         connectPromise.setSuccess();
-        connectPromise = null;
 
         // TODO we should also read from error stream and at least log from that
 
@@ -162,9 +161,12 @@ public class AsyncSshHandler extends ChannelOutboundHandlerAdapter {
 
     private synchronized void handleSshSetupFailure(final ChannelHandlerContext ctx, final Throwable e) {
         LOG.warn("Unable to setup SSH connection on channel: {}", ctx.channel(), e);
-        connectPromise.setFailure(e);
-        connectPromise = null;
-        throw new IllegalStateException("Unable to setup SSH connection on channel: " + ctx.channel(), e);
+        disconnect(ctx, ctx.newPromise());
+
+        // If the promise is not yet done, we have failed with initial connect and set connectPromise to failure
+        if(!connectPromise.isDone()) {
+            connectPromise.setFailure(e);
+        }
     }
 
     @Override
@@ -185,6 +187,15 @@ public class AsyncSshHandler extends ChannelOutboundHandlerAdapter {
 
     @Override
     public synchronized void disconnect(final ChannelHandlerContext ctx, final ChannelPromise promise) {
+        // Super disconnect is necessary in this case since we are using NioSocketChannel and it needs to cleanup its resources
+        // e.g. Socket that it tries to open in its constructor (https://bugs.opendaylight.org/show_bug.cgi?id=2430)
+        // TODO better solution would be to implement custom ChannelFactory + Channel that will use mina SSH lib internally: port this to custom channel implementation
+        try {
+            super.disconnect(ctx, ctx.newPromise());
+        } catch (final Exception e) {
+            LOG.warn("Unable to cleanup all resources for channel: {}. Ignoring.", ctx.channel(), e);
+        }
+
         if(sshReadAsyncListener != null) {
             sshReadAsyncListener.close();
         }
@@ -205,11 +216,15 @@ public class AsyncSshHandler extends ChannelOutboundHandlerAdapter {
             });
         }
 
+        // If we have already succeeded and the session was dropped after, we need to fire inactive to notify reconnect logic
+        if(connectPromise.isSuccess()) {
+            ctx.fireChannelInactive();
+        }
+
         channel = null;
-        promise.setSuccess();
 
+        promise.setSuccess();
         LOG.debug("SSH session closed on channel: {}", ctx.channel());
-        ctx.fireChannelInactive();
     }
 
 }
index 73f2287c8bce6151b9dd33f8fa0bc7ed8d39ac40..dfca1b80c41afd3d855216babc3a3ad6e8d45db4 100644 (file)
@@ -8,7 +8,6 @@
 
 package org.opendaylight.controller.netconf.nettyutil.handler.ssh.client;
 
-import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyBoolean;
 import static org.mockito.Matchers.anyObject;
@@ -18,9 +17,9 @@ import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.verifyZeroInteractions;
 
 import com.google.common.util.concurrent.FutureCallback;
@@ -30,8 +29,10 @@ import com.google.common.util.concurrent.SettableFuture;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelPromise;
+import io.netty.channel.DefaultChannelPromise;
 import java.io.IOException;
 import java.net.SocketAddress;
 import org.apache.sshd.ClientChannel;
@@ -143,6 +144,7 @@ public class AsyncSshHandlerTest {
         doReturn(ctx).when(ctx).fireChannelActive();
         doReturn(ctx).when(ctx).fireChannelInactive();
         doReturn(ctx).when(ctx).fireChannelRead(anyObject());
+        doReturn(mock(ChannelFuture.class)).when(ctx).disconnect(any(ChannelPromise.class));
         doReturn(getMockedPromise()).when(ctx).newPromise();
     }
 
@@ -179,7 +181,6 @@ public class AsyncSshHandlerTest {
         verify(subsystemChannel).setStreaming(ClientChannel.Streaming.Async);
 
         verify(promise).setSuccess();
-        verifyNoMoreInteractions(promise);
         verify(ctx).fireChannelActive();
     }
 
@@ -533,14 +534,8 @@ public class AsyncSshHandlerTest {
 
         verify(subsystemChannel).setStreaming(ClientChannel.Streaming.Async);
 
-        try {
-            sshChannelOpenListener.operationComplete(getFailedOpenFuture());
-            fail("Exception expected");
-        } catch (final Exception e) {
-            verify(promise).setFailure(any(Throwable.class));
-            verifyNoMoreInteractions(promise);
-            // TODO should ctx.channelInactive be called if we throw exception ?
-        }
+        sshChannelOpenListener.operationComplete(getFailedOpenFuture());
+        verify(promise).setFailure(any(Throwable.class));
     }
 
     @Test
@@ -555,14 +550,8 @@ public class AsyncSshHandlerTest {
 
         final AuthFuture authFuture = getFailedAuthFuture();
 
-        try {
-            sshAuthListener.operationComplete(authFuture);
-            fail("Exception expected");
-        } catch (final Exception e) {
-            verify(promise).setFailure(any(Throwable.class));
-            verifyNoMoreInteractions(promise);
-            // TODO should ctx.channelInactive be called ?
-        }
+        sshAuthListener.operationComplete(authFuture);
+        verify(promise).setFailure(any(Throwable.class));
     }
 
     private AuthFuture getFailedAuthFuture() {
@@ -584,14 +573,8 @@ public class AsyncSshHandlerTest {
         asyncSshHandler.connect(ctx, remoteAddress, localAddress, promise);
 
         final ConnectFuture connectFuture = getFailedConnectFuture();
-        try {
-            sshConnectListener.operationComplete(connectFuture);
-            fail("Exception expected");
-        } catch (final Exception e) {
-            verify(promise).setFailure(any(Throwable.class));
-            verifyNoMoreInteractions(promise);
-            // TODO should ctx.channelInactive be called ?
-        }
+        sshConnectListener.operationComplete(connectFuture);
+        verify(promise).setFailure(any(Throwable.class));
     }
 
     private ConnectFuture getFailedConnectFuture() {
@@ -602,10 +585,7 @@ public class AsyncSshHandlerTest {
     }
 
     private ChannelPromise getMockedPromise() {
-        final ChannelPromise promise = mock(ChannelPromise.class);
-        doReturn(promise).when(promise).setSuccess();
-        doReturn(promise).when(promise).setFailure(any(Throwable.class));
-        return promise;
+        return spy(new DefaultChannelPromise(channel));
     }
 
     private static abstract class SuccessFutureListener<T extends SshFuture<T>> implements FutureCallback<SshFutureListener<T>> {
index 2802488170ef9cc9ef6d27b3846592b19219ca9b..72534e242e92dc4a4d8a19690caecd0fcf5d0cdd 100644 (file)
@@ -136,7 +136,7 @@ public class SSHTest {
             Thread.sleep(100);
         }
         assertFalse(echoClientHandler.isConnected());
-        assertEquals(State.CONNECTION_CLOSED, echoClientHandler.getState());
+        assertEquals(State.FAILED_TO_CONNECT, echoClientHandler.getState());
     }
 
 }
index 5235030b4f47da59d89582add0ee535be3e67dc5..f0ae6094d4528e44a9f3059358e6ba3f0db039bc 100644 (file)
@@ -200,8 +200,6 @@ public class NeutronLoadBalancerPoolNorthbound {
             if (loadBalancerPoolInterface.neutronLoadBalancerPoolExists(singleton.getLoadBalancerPoolID())) {
                 throw new BadRequestException("LoadBalancerPool UUID already exists");
             }
-            loadBalancerPoolInterface.addNeutronLoadBalancerPool(singleton);
-
             Object[] instances = ServiceHelper.getGlobalInstances(INeutronLoadBalancerPoolAware.class, this, null);
             if (instances != null) {
                 for (Object instance : instances) {