Switch to MD-SAL APIs
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / MultipartRequestOnTheFlyCallbackTest.java
index 3e4c4f0162caa738b967c46acea0d27829dcf537..61c5148a3f41322444dd4d15014168db90b85d74 100644 (file)
@@ -1,46 +1,54 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
 package org.opendaylight.openflowplugin.impl.services;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Matchers.eq;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+
 import com.google.common.base.Optional;
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.Futures;
 import java.math.BigInteger;
 import java.util.Collections;
 import java.util.List;
-import java.util.concurrent.ExecutionException;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.mockito.Matchers;
+import org.mockito.ArgumentMatchers;
 import org.mockito.Mock;
 import org.mockito.Mockito;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.mdsal.binding.api.ReadTransaction;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
-import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
+import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
+import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory;
 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
+import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerFlowMultipartRequestOnTheFlyCallback;
 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
@@ -79,8 +87,6 @@ public class MultipartRequestOnTheFlyCallbackTest {
     @Mock
     private DeviceContext mockedDeviceContext;
     @Mock
-    private RequestContext<List<MultipartReply>> mockedRequestContext;
-    @Mock
     private ConnectionContext mockedPrimaryConnection;
     @Mock
     private NodeId mockedNodeId;
@@ -95,44 +101,37 @@ public class MultipartRequestOnTheFlyCallbackTest {
     @Mock
     private DeviceFlowRegistry mockedFlowRegistry;
     @Mock
-    private ReadOnlyTransaction mockedReadOnlyTx;
+    private FlowDescriptor mockedFlowDescriptor;
+    @Mock
+    private ReadTransaction mockedReadOnlyTx;
 
     private AbstractRequestContext<List<MultipartReply>> dummyRequestContext;
     private final EventIdentifier dummyEventIdentifier = new EventIdentifier(DUMMY_EVENT_NAME, DUMMY_DEVICE_ID);
-    private MultipartRequestOnTheFlyCallback multipartRequestOnTheFlyCallback;
+    private AbstractMultipartRequestOnTheFlyCallback<MultipartReply> multipartRequestOnTheFlyCallback;
     private final short tableId = 0;
 
     @Before
     public void initialization() {
         when(mockedDeviceContext.getMessageSpy()).thenReturn(new MessageIntelligenceAgencyImpl());
         when(mockedNodeId.toString()).thenReturn(DUMMY_NODE_ID);
-        when(mockedPrimaryConnection.getNodeId()).thenReturn(mockedNodeId);
         when(mockedPrimaryConnection.getFeatures()).thenReturn(mockedFeaturesReply);
-        when(mockedFeaturesReply.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
-        when(mockedFeaturesReply.getDatapathId()).thenReturn(BigInteger.valueOf(123L));
-
-        when(mocketGetFeaturesOutput.getTables()).thenReturn(tableId);
-        when(mocketGetFeaturesOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
-        when(mocketGetFeaturesOutput.getDatapathId()).thenReturn(BigInteger.valueOf(123L));
 
         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimaryConnection);
         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_PATH);
-        when(mockedDeviceState.deviceSynchronized()).thenReturn(true);
         when(mockedDeviceInfo.getNodeId()).thenReturn(mockedNodeId);
         when(mockedDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
+        when(mockedDeviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
 
-        when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(mockedFlowRegistry);
+        when(mockedFlowRegistry.retrieveDescriptor(any(FlowRegistryKey.class)))
+                .thenReturn(mockedFlowDescriptor);
 
-        final InstanceIdentifier<FlowCapableNode> nodePath = mockedDeviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
+        final InstanceIdentifier<FlowCapableNode> nodePath =
+                mockedDeviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
         final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
-        flowNodeBuilder.setTable(Collections.<Table> emptyList());
+        flowNodeBuilder.setTable(Collections.<Table>emptyList());
         final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
-        final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture = Futures.immediateCheckedFuture(flowNodeOpt);
-        when(mockedReadOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodePath)).thenReturn(flowNodeFuture);
-        when(mockedDeviceContext.getReadTransaction()).thenReturn(mockedReadOnlyTx);
-
         dummyRequestContext = new AbstractRequestContext<List<MultipartReply>>(DUMMY_XID) {
 
             @Override
@@ -140,16 +139,23 @@ public class MultipartRequestOnTheFlyCallbackTest {
                 //NOOP
             }
         };
-        multipartRequestOnTheFlyCallback = new MultipartRequestOnTheFlyCallback(dummyRequestContext, String.class,
-                mockedDeviceContext.getMessageSpy(),dummyEventIdentifier, mockedDeviceInfo,
-                mockedDeviceContext.getDeviceFlowRegistry(), mockedDeviceContext, mockedDeviceState);
+
+        final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
+        multipartRequestOnTheFlyCallback = new MultiLayerFlowMultipartRequestOnTheFlyCallback<>(
+            dummyRequestContext,
+            String.class,
+            mockedDeviceContext,
+            dummyEventIdentifier,
+            MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext),
+            convertorManager);
     }
 
 
     @Test
     public void testOnSuccessWithNull() throws Exception {
         multipartRequestOnTheFlyCallback.onSuccess(null);
-        final RpcResult<List<MultipartReply>> expectedRpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
+        final RpcResult<List<MultipartReply>> expectedRpcResult =
+                RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
         final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
         assertEquals(expectedRpcResult.getErrors(), actualResult.getErrors());
         assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
@@ -163,34 +169,34 @@ public class MultipartRequestOnTheFlyCallbackTest {
 
         final RpcResult<List<MultipartReply>> expectedRpcResult =
                 RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
-                        String.format("Unexpected response type received %s.", mockedHelloMessage.getClass())).build();
+                        String.format("Unexpected response type received: %s.", mockedHelloMessage.getClass())).build();
         final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
         assertNotNull(actualResult.getErrors());
         assertEquals(1, actualResult.getErrors().size());
 
         final RpcError actualError = actualResult.getErrors().iterator().next();
-        assertEquals(actualError.getMessage(), String.format("Unexpected response type received %s.", mockedHelloMessage.getClass()));
+        assertEquals(actualError.getMessage(),
+                     String.format("Unexpected response type received: %s.",
+                     mockedHelloMessage.getClass()));
         assertEquals(actualError.getErrorType(),RpcError.ErrorType.APPLICATION);
         assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
         assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
 
-        Mockito.verify(mockedDeviceContext, Mockito.never()).writeToTransaction(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
-                Matchers.<InstanceIdentifier>any(), Matchers.<DataObject>any());
+        Mockito.verify(mockedDeviceContext, Mockito.never())
+                .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
+                        ArgumentMatchers.<InstanceIdentifier>any(), ArgumentMatchers.<DataObject>any());
         Mockito.verify(mockedDeviceContext).submitTransaction();
     }
 
     /**
-     * not the last reply
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
+     * Not the last reply.
      */
     @Test
     public void testOnSuccessWithValidMultipart1() throws Exception {
         final MatchBuilder matchBuilder = new MatchBuilder()
                 .setMatchEntry(Collections.<MatchEntry>emptyList());
         final FlowStatsBuilder flowStatsBuilder = new FlowStatsBuilder()
-.setTableId(tableId)
+                .setTableId(tableId)
                 .setPriority(2)
                 .setCookie(BigInteger.ZERO)
                 .setByteCount(BigInteger.TEN)
@@ -216,32 +222,17 @@ public class MultipartRequestOnTheFlyCallbackTest {
         tableDataBld.setId(tableId);
         flowNodeBuilder.setTable(Collections.singletonList(tableDataBld.build()));
         final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
-        final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture = Futures
-                .immediateCheckedFuture(flowNodeOpt);
-        when(mockedReadOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodePath)).thenReturn(flowNodeFuture);
-        when(mockedDeviceContext.getReadTransaction()).thenReturn(mockedReadOnlyTx);
 
         multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
-        final InstanceIdentifier<Table> tableIdent = nodePath.child(Table.class, new TableKey(tableId));
 
-        verify(mockedReadOnlyTx, times(1)).read(LogicalDatastoreType.OPERATIONAL, nodePath);
-        verify(mockedReadOnlyTx, times(1)).close();
-        verify(mockedFlowRegistry).storeIfNecessary(Matchers.<FlowRegistryKey> any(), Matchers.anyShort());
+        verify(mockedReadOnlyTx, times(0)).read(LogicalDatastoreType.OPERATIONAL, nodePath);
+        verify(mockedReadOnlyTx, times(0)).close();
         verify(mockedDeviceContext, times(1)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
-                eq(tableIdent), Matchers.<Table> any());
-        /*
-         * One call for Table one call for Flow
-         * we are not able to create Flow InstanceIdentifier because we are missing FlowId
-         */
-        verify(mockedDeviceContext, times(2)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
-                Matchers.<InstanceIdentifier> any(), Matchers.<DataObject> any());
+                any(), any());
     }
 
     /**
-     * the last reply
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
+     * The last reply.
      */
     @Test
     public void testOnSuccessWithValidMultipart2() throws Exception {
@@ -252,13 +243,13 @@ public class MultipartRequestOnTheFlyCallbackTest {
         multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
 
         final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
-        assertNotNull(actualResult.getErrors());
-        assertTrue(actualResult.getErrors().isEmpty());
-        assertNotNull(actualResult.getResult());
-        assertTrue(actualResult.getResult().isEmpty());
 
-        Mockito.verify(mockedFlowRegistry, Mockito.never()).storeIfNecessary(Matchers.<FlowRegistryKey>any(), Matchers.anyShort());
-        Mockito.verify(mockedDeviceContext, Mockito.never()).writeToTransaction(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
-                Matchers.<InstanceIdentifier>any(), Matchers.<DataObject>any());
+        // Nothing else than flow is supported by on the fly callback
+        assertNotNull(actualResult.getErrors());
+        assertFalse(actualResult.getErrors().isEmpty());
+        Mockito.verify(mockedFlowRegistry, Mockito.never()).store(any());
+        Mockito.verify(mockedDeviceContext, Mockito.never())
+                .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
+                        ArgumentMatchers.<InstanceIdentifier>any(), ArgumentMatchers.<DataObject>any());
     }
-}
\ No newline at end of file
+}