*/
package org.opendaylight.openflowplugin.impl.services.sal;
-
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;
import com.google.common.util.concurrent.Futures;
-import java.math.BigInteger;
+import com.google.common.util.concurrent.ListenableFuture;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
-import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.Matchers;
import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
import org.opendaylight.openflowplugin.api.OFConstants;
import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
import org.opendaylight.openflowplugin.api.openflow.device.Xid;
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.rpc.listener.ItemLifecycleListener;
import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
-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.FlowId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
-
-@RunWith(MockitoJUnitRunner.class)
-public class SalFlowServiceImplTest extends TestCase {
-
- private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
+import org.opendaylight.yangtools.yang.common.Uint16;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.Uint64;
+import org.opendaylight.yangtools.yang.common.Uint8;
+
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
+public class SalFlowServiceImplTest {
+ private static final Uint64 DUMMY_DATAPATH_ID = Uint64.valueOf(444);
private static final String DUMMY_NODE_ID = "dummyNodeID";
private static final String DUMMY_FLOW_ID = "dummyFlowID";
- private static final Short DUMMY_TABLE_ID = (short) 0;
+ private static final Uint8 DUMMY_TABLE_ID = Uint8.ZERO;
+
+ private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II =
+ InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
- private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
- = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
+ private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II =
+ NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
- private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
- = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
+ private final NodeRef noderef = new NodeRef(NODE_II);
@Mock
private RequestContextStack mockedRequestContextStack;
@Before
public void initialization() {
- when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
- when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
- when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
- when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
- when(requestContext.getXid()).thenReturn(new Xid(84L));
+ when(requestContext.getXid()).thenReturn(new Xid(Uint32.valueOf(84L)));
when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
- when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
- when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
}
- private SalFlowServiceImpl mockSalFlowService(final short version) {
+ private AddFlowImpl mockAddFlow(final Uint8 version) {
when(mockedFeatures.getVersion()).thenReturn(version);
when(mockedFeaturesOutput.getVersion()).thenReturn(version);
when(mockedDeviceInfo.getVersion()).thenReturn(version);
- if (OFConstants.OFP_VERSION_1_3 >= version) {
- when(mockedDeviceContext.canUseSingleLayerSerialization()).thenReturn(true);
- }
+ final var convertorManager = ConvertorManagerFactory.createDefaultManager();
+ return new AddFlowImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
+ }
- final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
- return new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
+ private RemoveFlowImpl mockRemoveFlow(final Uint8 version) {
+ when(mockedFeatures.getVersion()).thenReturn(version);
+ when(mockedFeaturesOutput.getVersion()).thenReturn(version);
+ when(mockedDeviceInfo.getVersion()).thenReturn(version);
+
+ final var convertorManager = ConvertorManagerFactory.createDefaultManager();
+ return new RemoveFlowImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
+ }
+
+ private UpdateFlowImpl mockUpdateFlow(final Uint8 version) {
+ when(mockedFeatures.getVersion()).thenReturn(version);
+ when(mockedFeaturesOutput.getVersion()).thenReturn(version);
+ when(mockedDeviceInfo.getVersion()).thenReturn(version);
+
+ final var convertorManager = ConvertorManagerFactory.createDefaultManager();
+ return new UpdateFlowImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
}
@Test
public void testAddFlow() throws Exception {
- addFlow(null, OFConstants.OFP_VERSION_1_0);
- addFlow(null, OFConstants.OFP_VERSION_1_3);
+ addFlow(OFConstants.OFP_VERSION_1_0);
+ addFlow(OFConstants.OFP_VERSION_1_3);
}
@Test
addFlowFailCallback(OFConstants.OFP_VERSION_1_3);
}
- private void addFlowFailCallback(short version) throws InterruptedException, ExecutionException {
+ private void addFlowFailCallback(final Uint8 version) throws InterruptedException, ExecutionException {
AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
.setMatch(match)
- .setTableId((short)1)
+ .setTableId(Uint8.ONE)
+ .setNode(noderef)
.build();
- Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
+ doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
.when(requestContext).getFuture();
- mockingFlowRegistryLookup();
- final Future<RpcResult<AddFlowOutput>> rpcResultFuture = mockSalFlowService(version).addFlow(mockedAddFlowInput);
+ final var rpcResultFuture = mockAddFlow(version).invoke(mockedAddFlowInput);
assertNotNull(rpcResultFuture);
- final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
+ final var addFlowOutputRpcResult = rpcResultFuture.get();
assertNotNull(addFlowOutputRpcResult);
assertFalse(addFlowOutputRpcResult.isSuccessful());
}
removeFlowFailCallback(OFConstants.OFP_VERSION_1_3);
}
- private void removeFlowFailCallback(short version) throws InterruptedException, ExecutionException {
+ private void removeFlowFailCallback(final Uint8 version) throws InterruptedException, ExecutionException {
RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
+ .setTableId(Uint8.ONE)
.setMatch(match)
+ .setNode(noderef)
.build();
- Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
+ doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
.when(requestContext).getFuture();
- final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = mockSalFlowService(version).removeFlow(mockedRemoveFlowInput);
+ final var rpcResultFuture = mockRemoveFlow(version).invoke(mockedRemoveFlowInput);
assertNotNull(rpcResultFuture);
- final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
+ final var removeFlowOutputRpcResult = rpcResultFuture.get();
assertNotNull(removeFlowOutputRpcResult);
assertFalse(removeFlowOutputRpcResult.isSuccessful());
}
@Test
public void testAddFlowWithItemLifecycle() throws Exception {
- addFlow(mock(ItemLifecycleListener.class), OFConstants.OFP_VERSION_1_0);
- addFlow(mock(ItemLifecycleListener.class), OFConstants.OFP_VERSION_1_3);
+ addFlow(OFConstants.OFP_VERSION_1_0);
+ addFlow(OFConstants.OFP_VERSION_1_3);
}
- private void addFlow(final ItemLifecycleListener itemLifecycleListener, short version) throws ExecutionException, InterruptedException {
+ private void addFlow(final Uint8 version) throws ExecutionException, InterruptedException {
AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
.setMatch(match)
- .setTableId((short)1)
+ .setTableId(Uint8.ONE)
+ .setNode(noderef)
.build();
- SalFlowServiceImpl salFlowService = mockSalFlowService(version);
- salFlowService.setItemLifecycleListener(itemLifecycleListener);
-
- mockingFlowRegistryLookup();
- verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
- if (itemLifecycleListener != null) {
- Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
- }
+ var addFlow = mockAddFlow(version);
+
+ verifyOutput(addFlow.invoke(mockedAddFlowInput));
}
@Test
public void testRemoveFlow() throws Exception {
- removeFlow(null, OFConstants.OFP_VERSION_1_0);
- removeFlow(null, OFConstants.OFP_VERSION_1_3);
+ removeFlow(OFConstants.OFP_VERSION_1_0);
+ removeFlow(OFConstants.OFP_VERSION_1_3);
}
@Test
public void testRemoveFlowWithItemLifecycle() throws Exception {
- removeFlow(mock(ItemLifecycleListener.class), OFConstants.OFP_VERSION_1_0);
- removeFlow(mock(ItemLifecycleListener.class), OFConstants.OFP_VERSION_1_3);
+ removeFlow(OFConstants.OFP_VERSION_1_0);
+ removeFlow(OFConstants.OFP_VERSION_1_3);
}
- private void removeFlow(final ItemLifecycleListener itemLifecycleListener, short version) throws Exception {
+ private void removeFlow(final Uint8 version) throws Exception {
RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
.setMatch(match)
- .setTableId((short)1)
+ .setTableId(Uint8.ONE)
+ .setNode(noderef)
.build();
- SalFlowServiceImpl salFlowService = mockSalFlowService(version);
- if (itemLifecycleListener != null) {
- salFlowService.setItemLifecycleListener(itemLifecycleListener);
- mockingFlowRegistryLookup();
-
- }
-
- verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
- if (itemLifecycleListener != null) {
- Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
- }
-
+ final var removeFlow = mockRemoveFlow(version);
+ verifyOutput(removeFlow.invoke(mockedRemoveFlowInput));
}
@Test
public void testUpdateFlow() throws Exception {
- updateFlow(null, OFConstants.OFP_VERSION_1_0);
- updateFlow(null, OFConstants.OFP_VERSION_1_3);
+ updateFlow(OFConstants.OFP_VERSION_1_0);
+ updateFlow(OFConstants.OFP_VERSION_1_3);
}
@Test
public void testUpdateFlowWithItemLifecycle() throws Exception {
- updateFlow(mock(ItemLifecycleListener.class), OFConstants.OFP_VERSION_1_0);
- updateFlow(mock(ItemLifecycleListener.class), OFConstants.OFP_VERSION_1_3);
+ updateFlow(OFConstants.OFP_VERSION_1_0);
+ updateFlow(OFConstants.OFP_VERSION_1_3);
}
- private void updateFlow(final ItemLifecycleListener itemLifecycleListener, short version) throws Exception {
+ private void updateFlow(final Uint8 version) throws Exception {
UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
UpdateFlowInput mockedUpdateFlowInput1 = mock(UpdateFlowInput.class);
UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
.setMatch(match)
- .setTableId((short)1)
+ .setTableId(Uint8.ONE)
.build();
UpdatedFlow mockedUpdateFlow1 = new UpdatedFlowBuilder()
.setMatch(match)
- .setTableId((short)1)
- .setPriority(Integer.valueOf(1))
+ .setTableId(Uint8.ONE)
+ .setPriority(Uint16.ONE)
.build();
when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
when(mockedUpdateFlowInput1.getUpdatedFlow()).thenReturn(mockedUpdateFlow1);
FlowRef mockedFlowRef = mock(FlowRef.class);
- Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
+ doReturn(TABLE_II.child(Flow.class,
+ new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
when(mockedUpdateFlowInput1.getFlowRef()).thenReturn(mockedFlowRef);
OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
.setMatch(match)
- .setTableId((short)1)
+ .setTableId(Uint8.ONE)
.build();
OriginalFlow mockedOriginalFlow1 = new OriginalFlowBuilder()
.setMatch(match)
- .setTableId((short)1)
- .setPriority(Integer.valueOf(2))
+ .setTableId(Uint8.ONE)
+ .setPriority(Uint16.TWO)
.build();
when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
when(mockedUpdateFlowInput1.getOriginalFlow()).thenReturn(mockedOriginalFlow1);
- SalFlowServiceImpl salFlowService = mockSalFlowService(version);
- if (itemLifecycleListener != null) {
- salFlowService.setItemLifecycleListener(itemLifecycleListener);
- mockingFlowRegistryLookup();
- }
-
- verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
- verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput1));
-
- if (itemLifecycleListener != null) {
- Mockito.verify(itemLifecycleListener, times(2)).onUpdated(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
- }
-
- }
-
- private void mockingFlowRegistryLookup() {
- FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
- FlowId flowId = new FlowId(DUMMY_FLOW_ID);
- when(mockedFlowDescriptor.getFlowId()).thenReturn(flowId);
- when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
-
- when(deviceFlowRegistry.retrieveDescriptor(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
+ final var updateFlow = mockUpdateFlow(version);
+ verifyOutput(updateFlow.invoke(mockedUpdateFlowInput));
+ verifyOutput(updateFlow.invoke(mockedUpdateFlowInput1));
}
- private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
+ private static <T extends DataObject> void verifyOutput(final ListenableFuture<RpcResult<T>> rpcResultFuture)
+ throws ExecutionException, InterruptedException {
assertNotNull(rpcResultFuture);
- final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
+ final var addFlowOutputRpcResult = rpcResultFuture.get();
assertNotNull(addFlowOutputRpcResult);
assertTrue(addFlowOutputRpcResult.isSuccessful());
}