OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / connection / ConnectionManagerImplTest.java
index 681c339433f153f04908dc5bbf05eb61795e0d86..0aaeefe9ed1aed991114e2b15b5f8eb196d097c2 100644 (file)
@@ -7,17 +7,20 @@
  */
 package org.opendaylight.openflowplugin.impl.connection;
 
+import static org.mockito.ArgumentMatchers.any;
+
 import com.google.common.util.concurrent.SettableFuture;
 import java.math.BigInteger;
-import java.net.InetAddress;
 import java.net.InetSocketAddress;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.TimeUnit;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
+import org.mockito.ArgumentMatchers;
 import org.mockito.Captor;
-import org.mockito.Matchers;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
@@ -26,6 +29,7 @@ import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyList
 import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
+import org.opendaylight.openflowplugin.impl.util.ThreadPoolLoggingExecutor;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
@@ -34,17 +38,20 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfigBuilder;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 
 /**
- * test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD)
+ * Test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD).
  */
 @RunWith(MockitoJUnitRunner.class)
 public class ConnectionManagerImplTest {
 
-    /** timeout of final step [ms] */
+    // timeout of final step [ms]
     private static final int FINAL_STEP_TIMEOUT = 500;
     private ConnectionManagerImpl connectionManagerImpl;
     @Mock
@@ -56,43 +63,46 @@ public class ConnectionManagerImplTest {
     @Captor
     private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
 
-    private final static int ECHO_REPLY_TIMEOUT = 500;
+    private static final long ECHO_REPLY_TIMEOUT = 500;
+    private static final int DEVICE_CONNECTION_RATE_LIMIT_PER_MIN = 0;
 
-    /**
-     * before each test method
-     */
     @Before
     public void setUp() {
-        connectionManagerImpl = new ConnectionManagerImpl(ECHO_REPLY_TIMEOUT);
+        final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
+                60L, TimeUnit.SECONDS,
+                new SynchronousQueue<>(), "ofppool");
+
+        connectionManagerImpl = new ConnectionManagerImpl(new OpenflowProviderConfigBuilder()
+                .setEchoReplyTimeout(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT))
+                .setDeviceConnectionRateLimitPerMin(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN)
+                .build(), threadPool);
+
         connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
         final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
-        final InetAddress inetAddress = deviceAddress.getAddress();
         Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
         Mockito.when(connection.isAlive()).thenReturn(true);
-        Mockito.when(connection.barrier(Matchers.<BarrierInput>any()))
+        Mockito.when(connection.barrier(ArgumentMatchers.<BarrierInput>any()))
                 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
-//        Mockito.when(connection.getRemoteAddress().getAddress()).thenReturn(inetAddress);
     }
 
-    /**
-     * after each test method
-     * @throws InterruptedException
-     */
     @After
     public void tearDown() throws InterruptedException {
         Thread.sleep(200L);
     }
 
     /**
-     * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
+     * Test method for
+     * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
+     * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
      * invoking onConnectionReady first, scenario:
      * <ol>
-     *  <li>send hello to device (rpc with void output)</li>
-     *  <li>receive hello from device (notification)</li>
-     *  <li>send getFeature to device (rpc with getFeatureOutput)</li>
-     *  <li>wait for rpc to finish with getFeatureOutput</li>
+     * <li>send hello to device (rpc with void output)</li>
+     * <li>receive hello from device (notification)</li>
+     * <li>send getFeature to device (rpc with getFeatureOutput)</li>
+     * <li>wait for rpc to finish with getFeatureOutput</li>
      * </ol>
-     * @throws InterruptedException
+     *
+     * @throws InterruptedException - interrupted exception
      */
     @Test
     public void testOnSwitchConnected1() throws Exception {
@@ -101,11 +111,12 @@ public class ConnectionManagerImplTest {
         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
 
         // prepare void reply (hello rpc output)
-        final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
-        Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
+        final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
+        Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
         // prepare getFeature reply (getFeture rpc output)
-        final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
-        Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
+        final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
+                SettableFuture.<RpcResult<GetFeaturesOutput>>create();
+        Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
 
 
         // fire handshake
@@ -113,7 +124,7 @@ public class ConnectionManagerImplTest {
 
         // deliver hello send output (void)
         Thread.sleep(100L);
-        final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
+        final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
         voidResponseFx.set(helloResponse);
 
         // send hello reply
@@ -123,27 +134,31 @@ public class ConnectionManagerImplTest {
         // deliver getFeature output
         Thread.sleep(100L);
         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
-        .setDatapathId(BigInteger.TEN)
-        .setVersion(OFConstants.OFP_VERSION_1_3)
-        .setXid(2L)
-        .setTables((short) 15)
-        .build();
+                .setDatapathId(BigInteger.TEN)
+                .setVersion(OFConstants.OFP_VERSION_1_3)
+                .setXid(2L)
+                .setTables((short) 15)
+                .build();
         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
         featureResponseFx.set(rpcFeaturesOutput);
 
-        Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
+        Mockito.verify(deviceConnectedHandler,
+                Mockito.timeout(500)).deviceConnected(any(ConnectionContext.class));
     }
 
     /**
-     * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
+     * Test method for
+     * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
+     * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
      * invoking onHelloMessage, scenario:
      * <ol>
-     *  <li>receive hello from device (notification)</li>
-     *  <li>send hello to device (rpc with void output)</li>
-     *  <li>send getFeature to device (rpc with getFeatureOutput)</li>
-     *  <li>wait for rpc to finish with getFeatureOutput</li>
+     * <li>receive hello from device (notification)</li>
+     * <li>send hello to device (rpc with void output)</li>
+     * <li>send getFeature to device (rpc with getFeatureOutput)</li>
+     * <li>wait for rpc to finish with getFeatureOutput</li>
      * </ol>
-     * @throws InterruptedException
+     *
+     * @throws InterruptedException - interrupted exception
      */
     @Test
     public void testOnSwitchConnected2() throws Exception {
@@ -152,11 +167,12 @@ public class ConnectionManagerImplTest {
         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
 
         // prepare void reply (hello rpc output)
-        final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
-        Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
+        final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
+        Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
         // prepare getFeature reply (getFeture rpc output)
-        final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
-        Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
+        final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
+                SettableFuture.<RpcResult<GetFeaturesOutput>>create();
+        Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
 
 
         // fire handshake - send hello reply
@@ -168,20 +184,21 @@ public class ConnectionManagerImplTest {
 
         // deliver hello send output (void)
         Thread.sleep(100L);
-        final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
+        final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
         voidResponseFx.set(helloResponse);
 
         // deliver getFeature output
         Thread.sleep(100L);
         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
-        .setDatapathId(BigInteger.TEN)
-        .setVersion(OFConstants.OFP_VERSION_1_3)
-        .setXid(2L)
-        .setTables((short) 15)
-        .build();
+                .setDatapathId(BigInteger.TEN)
+                .setVersion(OFConstants.OFP_VERSION_1_3)
+                .setXid(2L)
+                .setTables((short) 15)
+                .build();
         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
         featureResponseFx.set(rpcFeaturesOutput);
 
-        Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));
+        Mockito.verify(deviceConnectedHandler,
+                Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(any(ConnectionContext.class));
     }
 }