X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflowplugin-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fimpl%2Futil%2FFlowUtilTest.java;h=7d86563b592eefec9c4528020a1f2aaa0663f692;hb=cfe3a97837951ebbedb337dc988027f10c49f714;hp=61386b44fcb108bf707125c1bdf82807cc84e425;hpb=d63b5ef5242dd999695b0e96144b6cd07a960a3b;p=openflowplugin.git diff --git a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/util/FlowUtilTest.java b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/util/FlowUtilTest.java index 61386b44fc..7d86563b59 100644 --- a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/util/FlowUtilTest.java +++ b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/util/FlowUtilTest.java @@ -1,49 +1,237 @@ /* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * - * * 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 - * - * + * 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.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - +import com.google.common.base.Function; +import com.google.common.collect.Lists; +import java.util.Collections; +import java.util.List; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Assert; import org.junit.Test; +import org.mockito.Mockito; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; +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.table.Flow; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.BatchFlowIdGrouping; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.BatchFlowOutputListGrouping; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.output.list.grouping.BatchFailedFlowsOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.output.list.grouping.BatchFailedFlowsOutputBuilder; +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; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.common.RpcError; +import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.common.RpcResultBuilder; public class FlowUtilTest { + public static final NodeId DUMMY_NODE_ID = new NodeId("dummyNodeId"); + public static final FlowId DUMMY_FLOW_ID = new FlowId("dummyFlowId"); + public static final FlowId DUMMY_FLOW_ID_2 = new FlowId("dummyFlowId_2"); + public static final Short DUMMY_TABLE_ID = 1; + + @Test + public void testBuildFlowPath() throws Exception { + final InstanceIdentifier nodePath = InstanceIdentifier + .create(Nodes.class) + .child(Node.class, new NodeKey(DUMMY_NODE_ID)); + + final FlowRef flowRef = FlowUtil.buildFlowPath(nodePath, DUMMY_TABLE_ID, DUMMY_FLOW_ID); + final InstanceIdentifier flowRefValue = flowRef.getValue(); + Assert.assertEquals(DUMMY_NODE_ID, flowRefValue.firstKeyOf(Node.class).getId()); + Assert.assertEquals(DUMMY_TABLE_ID, flowRefValue.firstKeyOf(Table.class).getId()); + Assert.assertEquals(DUMMY_FLOW_ID, flowRefValue.firstKeyOf(Flow.class).getId()); + } + + @Test + public void testCreateCumulatingFunction() throws Exception { + final Function>, RpcResult>> function = + FlowUtil.createCumulatingFunction(Lists.newArrayList(createBatchFlowIdGrouping(DUMMY_FLOW_ID), + createBatchFlowIdGrouping(DUMMY_FLOW_ID_2))); + + final RpcResult> summary = function.apply(Lists.newArrayList( + RpcResultBuilder.success("a").build(), + RpcResultBuilder.failed() + .withError(RpcError.ErrorType.APPLICATION, "action-failed reason") + .build())); + + Assert.assertFalse(summary.isSuccessful()); + Assert.assertEquals(1, summary.getResult().size()); + Assert.assertEquals(1, summary.getErrors().size()); + Assert.assertEquals(DUMMY_FLOW_ID_2, summary.getResult().get(0).getFlowId()); + Assert.assertEquals(1, summary.getResult().get(0).getBatchOrder().intValue()); + } + + protected BatchFlowIdGrouping createBatchFlowIdGrouping(final FlowId flowId) { + final BatchFlowIdGrouping mock = Mockito.mock(BatchFlowIdGrouping.class); + Mockito.when(mock.getFlowId()).thenReturn(flowId); + return mock; + } + + @Test + public void testFlowAddTransformFailure() throws Exception { + final RpcResult> input = createBatchOutcomeWithError(); + checkBatchErrorOutcomeTransformation(FlowUtil.FLOW_ADD_TRANSFORM.apply(input)); + } + + @Test + public void testFlowAddTransformSuccess() throws Exception { + final RpcResult> input = createEmptyBatchOutcome(); + checkBatchSuccessOutcomeTransformation(FlowUtil.FLOW_ADD_TRANSFORM.apply(input)); + } + + @Test + public void testFlowRemoveTransformFailure() throws Exception { + final RpcResult> input = createBatchOutcomeWithError(); + checkBatchErrorOutcomeTransformation(FlowUtil.FLOW_REMOVE_TRANSFORM.apply(input)); + } + + @Test + public void testFlowRemoveTransformSuccess() throws Exception { + final RpcResult> input = createEmptyBatchOutcome(); + checkBatchSuccessOutcomeTransformation(FlowUtil.FLOW_REMOVE_TRANSFORM.apply(input)); + } + + @Test + public void testFlowUpdateTransformFailure() throws Exception { + final RpcResult> input = createBatchOutcomeWithError(); + checkBatchErrorOutcomeTransformation(FlowUtil.FLOW_UPDATE_TRANSFORM.apply(input)); + } - private static final short DUMMY_TABLE_ID = 1; - public static final Pattern INDEX_PATTERN = Pattern.compile("^#UF\\$TABLE\\*1-([0-9]+)$"); + @Test + public void testFlowUpdateTransformSuccess() throws Exception { + final RpcResult> input = createEmptyBatchOutcome(); + checkBatchSuccessOutcomeTransformation(FlowUtil.FLOW_UPDATE_TRANSFORM.apply(input)); + } + + private void checkBatchSuccessOutcomeTransformation( + final RpcResult output) { + Assert.assertTrue(output.isSuccessful()); + Assert.assertEquals(0, output.getResult().getBatchFailedFlowsOutput().size()); + Assert.assertEquals(0, output.getErrors().size()); + } + + private RpcResult> createEmptyBatchOutcome() { + return RpcResultBuilder + .>success(Collections.emptyList()) + .build(); + } + + private RpcResult> createBatchOutcomeWithError() { + return RpcResultBuilder.>failed() + .withError(RpcError.ErrorType.APPLICATION, "ut-flowAddFail") + .withResult(Collections.singletonList(new BatchFailedFlowsOutputBuilder() + .setFlowId(DUMMY_FLOW_ID) + .build())) + .build(); + } + + private void checkBatchErrorOutcomeTransformation( + final RpcResult output) { + Assert.assertFalse(output.isSuccessful()); + Assert.assertEquals(1, output.getResult().getBatchFailedFlowsOutput().size()); + Assert.assertEquals(DUMMY_FLOW_ID, output.getResult().getBatchFailedFlowsOutput().get(0).getFlowId()); + + Assert.assertEquals(1, output.getErrors().size()); + } + + @Test + public void testCreateComposingFunction_success_success() throws Exception { + final Function, RpcResult>, + RpcResult> compositeFunction = FlowUtil.createComposingFunction(); + + final RpcResult addFlowBatchOutput = createAddFlowsBatchSuccessOutput(); + final RpcResult barrierOutput = RpcResultBuilder.success().build(); + final Pair, RpcResult> input + = Pair.of(addFlowBatchOutput, barrierOutput); + final RpcResult composite = compositeFunction.apply(input); + + Assert.assertTrue(composite.isSuccessful()); + Assert.assertEquals(0, composite.getErrors().size()); + Assert.assertEquals(0, composite.getResult().getBatchFailedFlowsOutput().size()); + } @Test - public void createAlienFlowIdTest() { - final String alienFlowId1 = FlowUtil.createAlienFlowId(DUMMY_TABLE_ID).getValue(); - final Integer index1 = parseIndex(alienFlowId1); - final String alienFlowId2 = FlowUtil.createAlienFlowId(DUMMY_TABLE_ID).getValue(); - final Integer index2 = parseIndex(alienFlowId2); + public void testCreateComposingFunction_failure_success() throws Exception { + final Function, RpcResult>, + RpcResult> compositeFunction = FlowUtil.createComposingFunction(); + + final RpcResult addFlowBatchOutput = createAddFlowsBatchFailureOutcome(); + final RpcResult barrierOutput = RpcResultBuilder.success().build(); + final Pair, RpcResult> input + = Pair.of(addFlowBatchOutput, barrierOutput); + final RpcResult composite = compositeFunction.apply(input); - assertNotNull("index1 parsing failed: "+alienFlowId1, index1); - assertNotNull("index2 parsing failed: "+alienFlowId2, index2); - assertTrue(index1 < index2); + Assert.assertFalse(composite.isSuccessful()); + Assert.assertEquals(1, composite.getErrors().size()); + Assert.assertEquals(1, composite.getResult().getBatchFailedFlowsOutput().size()); } - private static Integer parseIndex(String alienFlowIdValue) { - final Matcher mach = INDEX_PATTERN.matcher(alienFlowIdValue); - if (mach.find()) { - return Integer.valueOf(mach.group(1)); - } - return null; + @Test + public void testCreateComposingFunction_success_failure() throws Exception { + final Function, RpcResult>, + RpcResult> compositeFunction = FlowUtil.createComposingFunction(); + + final RpcResult addFlowBatchOutput = createAddFlowsBatchSuccessOutput(); + final RpcResult barrierOutput = createBarrierFailureOutcome(); + final Pair, RpcResult> input + = Pair.of(addFlowBatchOutput, barrierOutput); + final RpcResult composite = compositeFunction.apply(input); + + Assert.assertFalse(composite.isSuccessful()); + Assert.assertEquals(1, composite.getErrors().size()); + Assert.assertEquals(0, composite.getResult().getBatchFailedFlowsOutput().size()); } + @Test + public void testCreateComposingFunction_failure_failure() throws Exception { + final Function, RpcResult>, + RpcResult> compositeFunction = FlowUtil.createComposingFunction(); + + final RpcResult addFlowBatchOutput = createAddFlowsBatchFailureOutcome(); + final RpcResult barrierOutput = createBarrierFailureOutcome(); + final Pair, RpcResult> input + = Pair.of(addFlowBatchOutput, barrierOutput); + final RpcResult composite = compositeFunction.apply(input); + + Assert.assertFalse(composite.isSuccessful()); + Assert.assertEquals(2, composite.getErrors().size()); + Assert.assertEquals(1, composite.getResult().getBatchFailedFlowsOutput().size()); + } + + private RpcResult createBarrierFailureOutcome() { + return RpcResultBuilder.failed() + .withError(RpcError.ErrorType.APPLICATION, "ut-barrier-error") + .build(); + } + + private RpcResult createAddFlowsBatchSuccessOutput() { + return RpcResultBuilder + .success(new AddFlowsBatchOutputBuilder() + .setBatchFailedFlowsOutput(Collections.emptyList()) + .build()) + .build(); + } + + private RpcResult createAddFlowsBatchFailureOutcome() { + final RpcResult> batchOutcomeWithError = createBatchOutcomeWithError(); + return RpcResultBuilder.failed() + .withResult(new AddFlowsBatchOutputBuilder() + .setBatchFailedFlowsOutput(batchOutcomeWithError.getResult()) + .build()) + .withRpcErrors(batchOutcomeWithError.getErrors()) + .build(); + } }