Speed up packetin throttling
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / device / DeviceContextImplTest.java
index 841b7be83dbbc5ae903c17dbb87db8a3d28a92bb..237a145fe7d1411abfb45452fad23d1febcb3c3a 100644 (file)
@@ -1,36 +1,49 @@
 package org.opendaylight.openflowplugin.impl.device;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import static org.mockito.Matchers.any;
 import com.google.common.util.concurrent.SettableFuture;
+import io.netty.util.HashedWheelTimer;
+import java.util.ArrayList;
 import java.util.Collections;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicLong;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
 import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.stubbing.Answer;
 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
 import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
-import org.opendaylight.openflowplugin.api.openflow.device.XidGenerator;
+import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncReply;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMask;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMask;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMask;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDescBuilder;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -39,10 +52,15 @@ import org.slf4j.LoggerFactory;
 public class DeviceContextImplTest {
     private static final Logger LOG = LoggerFactory
             .getLogger(DeviceContextImplTest.class);
-    XidGenerator xidGen;
+    Xid xid;
+    Xid xidMulti;
     DeviceContextImpl deviceContext;
+    TransactionChainManager txChainManager;
     @Mock
     RequestContext<GetAsyncReply> requestContext;
+    @Mock
+    RequestContext<MultipartReply> requestContextMultiReply;
+
     @Mock
     ConnectionContext connectionContext;
     @Mock
@@ -55,32 +73,70 @@ public class DeviceContextImplTest {
     ReadOnlyTransaction rTx;
     @Mock
     BindingTransactionChain txChainFactory;
+    @Mock
+    HashedWheelTimer timer;
+    @Mock
+    MessageIntelligenceAgency messageIntelligenceAgency;
+    @Mock
+    OutboundQueueProvider outboundQueueProvider;
+    @Mock
+    ConnectionAdapter connectionAdapter;
+
+    private final AtomicLong atomicLong = new AtomicLong(0);
 
     @Before
     public void setUp() {
-        Mockito.when(txChainFactory.newWriteOnlyTransaction()).thenReturn(wTx);
-        Mockito.when(dataBroker.createTransactionChain(Mockito.any(DeviceContextImpl.class))).thenReturn(txChainFactory);
-        Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
-        deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker);
-        xidGen = new XidGenerator();
+        Mockito.when(dataBroker.createTransactionChain(Mockito.any(TransactionChainManager.class))).thenReturn(txChainFactory);
+        txChainManager = new TransactionChainManager(dataBroker, deviceState);
         final SettableFuture<RpcResult<GetAsyncReply>> settableFuture = SettableFuture.create();
+        final SettableFuture<RpcResult<MultipartReply>> settableFutureMultiReply = SettableFuture.create();
         Mockito.when(requestContext.getFuture()).thenReturn(settableFuture);
-        deviceContext.hookRequestCtx(deviceContext.getNextXid(), requestContext);
+        Mockito.doAnswer(new Answer<Object>() {
+            @SuppressWarnings("unchecked")
+            @Override
+            public Object answer(final InvocationOnMock invocation) {
+                settableFuture.set((RpcResult<GetAsyncReply>) invocation.getArguments()[0]);
+                return null;
+            }
+        }).when(requestContext).setResult(any(RpcResult.class));
+
+        Mockito.when(requestContextMultiReply.getFuture()).thenReturn(settableFutureMultiReply);
+        Mockito.doAnswer(new Answer<Object>() {
+            @SuppressWarnings("unchecked")
+            @Override
+            public Object answer(final InvocationOnMock invocation) {
+                settableFutureMultiReply.set((RpcResult<MultipartReply>) invocation.getArguments()[0]);
+                return null;
+            }
+        }).when(requestContextMultiReply).setResult(any(RpcResult.class));
+        Mockito.when(txChainFactory.newWriteOnlyTransaction()).thenReturn(wTx);
+        Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
+        Mockito.when(connectionContext.getOutboundQueueProvider()).thenReturn(outboundQueueProvider);
+        Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
+        deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker, timer, messageIntelligenceAgency, outboundQueueProvider);
+
+        xid = new Xid(atomicLong.incrementAndGet());
+        xidMulti = new Xid(atomicLong.incrementAndGet());
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testDeviceContextImplConstructorNullConnectionContext() throws Exception {
+        new DeviceContextImpl(null, deviceState, dataBroker, timer, messageIntelligenceAgency, outboundQueueProvider).close();
     }
 
-    @Test(expected=NullPointerException.class)
-    public void testDeviceContextImplConstructorNullConnectionContext() {
-        new DeviceContextImpl(null, deviceState, dataBroker);
+    @Test(expected = NullPointerException.class)
+    public void testDeviceContextImplConstructorNullDataBroker() throws Exception {
+        new DeviceContextImpl(connectionContext, deviceState, null, timer, messageIntelligenceAgency, outboundQueueProvider).close();
     }
 
-    @Test(expected=NullPointerException.class)
-    public void testDeviceContextImplConstructorNullDataBroker() {
-        new DeviceContextImpl(connectionContext, deviceState, null);
+    @Test(expected = NullPointerException.class)
+    public void testDeviceContextImplConstructorNullDeviceState() throws Exception {
+        new DeviceContextImpl(connectionContext, null, dataBroker, timer, messageIntelligenceAgency, outboundQueueProvider).close();
     }
 
-    @Test(expected=NullPointerException.class)
-    public void testDeviceContextImplConstructorNullDeviceState() {
-        new DeviceContextImpl(connectionContext, null, dataBroker);
+    @Test(expected = NullPointerException.class)
+    public void testDeviceContextImplConstructorNullTimer() throws Exception {
+        new DeviceContextImpl(null, deviceState, dataBroker, null, messageIntelligenceAgency, outboundQueueProvider).close();
     }
 
     @Test
@@ -97,45 +153,5 @@ public class DeviceContextImplTest {
         Assert.assertEquals(rTx, readTx);
     }
 
-    @Test
-    public void testGetWriteTransaction() {
-        final WriteTransaction writeTx = deviceContext.getWriteTransaction();
-        Assert.assertNotNull(writeTx);
-        Assert.assertEquals(wTx, writeTx);
-    }
-
-    @Test
-    public void testProcessReply() {
-        final Xid xid = xidGen.generate();
-        final GetAsyncOutput asyncOutput = createAsyncOutput(xid);
-        LOG.info("Hooking RequestContext");
-        deviceContext.hookRequestCtx(xid, requestContext);
-        Assert.assertEquals(requestContext, deviceContext.getRequests().get(xid));
-
-        Assert.assertFalse(requestContext.getFuture().isDone());
-        LOG.info("Sending reply from device");
-        deviceContext.processReply(xid, asyncOutput);
-        Assert.assertTrue(requestContext.getFuture().isDone());
-
-        LOG.info("Checking RequestContext.future");
-        try {
-            final Object object = requestContext.getFuture().get(1L, TimeUnit.SECONDS);
-            final GetAsyncOutput getAsyncOutput = (GetAsyncOutput) object;
-            assertEquals(asyncOutput.getVersion(), getAsyncOutput.getVersion());
-        } catch (InterruptedException | ExecutionException | TimeoutException e) {
-            LOG.error("Test failed when checking RequestContext.future", e);
-            fail("fail");
-        }
-    }
-
-    private GetAsyncOutput createAsyncOutput(final Xid xid) {
-        final GetAsyncOutputBuilder asyncOutputBuilder = new GetAsyncOutputBuilder();
-        asyncOutputBuilder.setFlowRemovedMask(Collections.<FlowRemovedMask> emptyList());
-        asyncOutputBuilder.setPacketInMask(Collections.<PacketInMask> emptyList());
-        asyncOutputBuilder.setPortStatusMask(Collections.<PortStatusMask> emptyList());
-        asyncOutputBuilder.setVersion(OFConstants.OFP_VERSION_1_3);
-        asyncOutputBuilder.setXid(xid.getValue());
-        return asyncOutputBuilder.build();
-    }
 
 }