2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.impl.util;
11 import com.google.common.base.Function;
12 import com.google.common.collect.Lists;
13 import java.util.Collections;
14 import java.util.List;
15 import org.apache.commons.lang3.tuple.Pair;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchOutputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.BatchFlowIdGrouping;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.BatchFlowOutputListGrouping;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.output.list.grouping.BatchFailedFlowsOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.output.list.grouping.BatchFailedFlowsOutputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.RpcError;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 public class FlowUtilTest {
39 public static final NodeId DUMMY_NODE_ID = new NodeId("dummyNodeId");
40 public static final FlowId DUMMY_FLOW_ID = new FlowId("dummyFlowId");
41 public static final FlowId DUMMY_FLOW_ID_2 = new FlowId("dummyFlowId_2");
42 public static final Short DUMMY_TABLE_ID = 1;
45 public void testBuildFlowPath() throws Exception {
46 final InstanceIdentifier<Node> nodePath = InstanceIdentifier
48 .child(Node.class, new NodeKey(DUMMY_NODE_ID));
50 final FlowRef flowRef = FlowUtil.buildFlowPath(nodePath, DUMMY_TABLE_ID, DUMMY_FLOW_ID);
51 final InstanceIdentifier<?> flowRefValue = flowRef.getValue();
52 Assert.assertEquals(DUMMY_NODE_ID, flowRefValue.firstKeyOf(Node.class).getId());
53 Assert.assertEquals(DUMMY_TABLE_ID, flowRefValue.firstKeyOf(Table.class).getId());
54 Assert.assertEquals(DUMMY_FLOW_ID, flowRefValue.firstKeyOf(Flow.class).getId());
58 public void testCreateCumulatingFunction() throws Exception {
59 final Function<List<RpcResult<String>>, RpcResult<List<BatchFailedFlowsOutput>>> function =
60 FlowUtil.createCumulatingFunction(Lists.newArrayList(createBatchFlowIdGrouping(DUMMY_FLOW_ID),
61 createBatchFlowIdGrouping(DUMMY_FLOW_ID_2)));
63 final RpcResult<List<BatchFailedFlowsOutput>> summary = function.apply(Lists.newArrayList(
64 RpcResultBuilder.success("a").build(),
65 RpcResultBuilder.<String>failed()
66 .withError(RpcError.ErrorType.APPLICATION, "action-failed reason")
69 Assert.assertFalse(summary.isSuccessful());
70 Assert.assertEquals(1, summary.getResult().size());
71 Assert.assertEquals(1, summary.getErrors().size());
72 Assert.assertEquals(DUMMY_FLOW_ID_2, summary.getResult().get(0).getFlowId());
73 Assert.assertEquals(1, summary.getResult().get(0).getBatchOrder().intValue());
76 protected BatchFlowIdGrouping createBatchFlowIdGrouping(final FlowId flowId) {
77 final BatchFlowIdGrouping mock = Mockito.mock(BatchFlowIdGrouping.class);
78 Mockito.when(mock.getFlowId()).thenReturn(flowId);
83 public void testFlowAddTransformFailure() throws Exception {
84 final RpcResult<List<BatchFailedFlowsOutput>> input = createBatchOutcomeWithError();
85 checkBatchErrorOutcomeTransformation(FlowUtil.FLOW_ADD_TRANSFORM.apply(input));
89 public void testFlowAddTransformSuccess() throws Exception {
90 final RpcResult<List<BatchFailedFlowsOutput>> input = createEmptyBatchOutcome();
91 checkBatchSuccessOutcomeTransformation(FlowUtil.FLOW_ADD_TRANSFORM.apply(input));
95 public void testFlowRemoveTransformFailure() throws Exception {
96 final RpcResult<List<BatchFailedFlowsOutput>> input = createBatchOutcomeWithError();
97 checkBatchErrorOutcomeTransformation(FlowUtil.FLOW_REMOVE_TRANSFORM.apply(input));
101 public void testFlowRemoveTransformSuccess() throws Exception {
102 final RpcResult<List<BatchFailedFlowsOutput>> input = createEmptyBatchOutcome();
103 checkBatchSuccessOutcomeTransformation(FlowUtil.FLOW_REMOVE_TRANSFORM.apply(input));
107 public void testFlowUpdateTransformFailure() throws Exception {
108 final RpcResult<List<BatchFailedFlowsOutput>> input = createBatchOutcomeWithError();
109 checkBatchErrorOutcomeTransformation(FlowUtil.FLOW_UPDATE_TRANSFORM.apply(input));
113 public void testFlowUpdateTransformSuccess() throws Exception {
114 final RpcResult<List<BatchFailedFlowsOutput>> input = createEmptyBatchOutcome();
115 checkBatchSuccessOutcomeTransformation(FlowUtil.FLOW_UPDATE_TRANSFORM.apply(input));
118 private <T extends BatchFlowOutputListGrouping> void checkBatchSuccessOutcomeTransformation(
119 final RpcResult<T> output) {
120 Assert.assertTrue(output.isSuccessful());
121 Assert.assertEquals(0, output.getResult().getBatchFailedFlowsOutput().size());
122 Assert.assertEquals(0, output.getErrors().size());
125 private RpcResult<List<BatchFailedFlowsOutput>> createEmptyBatchOutcome() {
126 return RpcResultBuilder
127 .<List<BatchFailedFlowsOutput>>success(Collections.<BatchFailedFlowsOutput>emptyList())
131 private RpcResult<List<BatchFailedFlowsOutput>> createBatchOutcomeWithError() {
132 return RpcResultBuilder.<List<BatchFailedFlowsOutput>>failed()
133 .withError(RpcError.ErrorType.APPLICATION, "ut-flowAddFail")
134 .withResult(Collections.singletonList(new BatchFailedFlowsOutputBuilder()
135 .setFlowId(DUMMY_FLOW_ID)
140 private <T extends BatchFlowOutputListGrouping> void checkBatchErrorOutcomeTransformation(
141 final RpcResult<T> output) {
142 Assert.assertFalse(output.isSuccessful());
143 Assert.assertEquals(1, output.getResult().getBatchFailedFlowsOutput().size());
144 Assert.assertEquals(DUMMY_FLOW_ID, output.getResult().getBatchFailedFlowsOutput().get(0).getFlowId());
146 Assert.assertEquals(1, output.getErrors().size());
150 public void testCreateComposingFunction_success_success() throws Exception {
151 final Function<Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<Void>>, RpcResult<AddFlowsBatchOutput>>
152 compositeFunction = FlowUtil.createComposingFunction();
154 final RpcResult<AddFlowsBatchOutput> addFlowBatchOutput = createAddFlowsBatchSuccessOutput();
155 final RpcResult<Void> barrierOutput = RpcResultBuilder.<Void>success().build();
156 final Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<Void>> input = Pair.of(addFlowBatchOutput, barrierOutput);
157 final RpcResult<AddFlowsBatchOutput> composite = compositeFunction.apply(input);
159 Assert.assertTrue(composite.isSuccessful());
160 Assert.assertEquals(0, composite.getErrors().size());
161 Assert.assertEquals(0, composite.getResult().getBatchFailedFlowsOutput().size());
165 public void testCreateComposingFunction_failure_success() throws Exception {
166 final Function<Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<Void>>, RpcResult<AddFlowsBatchOutput>>
167 compositeFunction = FlowUtil.createComposingFunction();
169 final RpcResult<AddFlowsBatchOutput> addFlowBatchOutput = createAddFlowsBatchFailureOutcome();
170 final RpcResult<Void> barrierOutput = RpcResultBuilder.<Void>success().build();
171 final Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<Void>> input = Pair.of(addFlowBatchOutput, barrierOutput);
172 final RpcResult<AddFlowsBatchOutput> composite = compositeFunction.apply(input);
174 Assert.assertFalse(composite.isSuccessful());
175 Assert.assertEquals(1, composite.getErrors().size());
176 Assert.assertEquals(1, composite.getResult().getBatchFailedFlowsOutput().size());
180 public void testCreateComposingFunction_success_failure() throws Exception {
181 final Function<Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<Void>>, RpcResult<AddFlowsBatchOutput>>
182 compositeFunction = FlowUtil.createComposingFunction();
184 final RpcResult<AddFlowsBatchOutput> addFlowBatchOutput = createAddFlowsBatchSuccessOutput();
185 final RpcResult<Void> barrierOutput = createBarrierFailureOutcome();
186 final Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<Void>> input = Pair.of(addFlowBatchOutput, barrierOutput);
187 final RpcResult<AddFlowsBatchOutput> composite = compositeFunction.apply(input);
189 Assert.assertFalse(composite.isSuccessful());
190 Assert.assertEquals(1, composite.getErrors().size());
191 Assert.assertEquals(0, composite.getResult().getBatchFailedFlowsOutput().size());
195 public void testCreateComposingFunction_failure_failure() throws Exception {
196 final Function<Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<Void>>, RpcResult<AddFlowsBatchOutput>>
197 compositeFunction = FlowUtil.createComposingFunction();
199 final RpcResult<AddFlowsBatchOutput> addFlowBatchOutput = createAddFlowsBatchFailureOutcome();
200 final RpcResult<Void> barrierOutput = createBarrierFailureOutcome();
201 final Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<Void>> input = Pair.of(addFlowBatchOutput, barrierOutput);
202 final RpcResult<AddFlowsBatchOutput> composite = compositeFunction.apply(input);
204 Assert.assertFalse(composite.isSuccessful());
205 Assert.assertEquals(2, composite.getErrors().size());
206 Assert.assertEquals(1, composite.getResult().getBatchFailedFlowsOutput().size());
209 private RpcResult<Void> createBarrierFailureOutcome() {
210 return RpcResultBuilder.<Void>failed()
211 .withError(RpcError.ErrorType.APPLICATION, "ut-barrier-error")
215 private RpcResult<AddFlowsBatchOutput> createAddFlowsBatchSuccessOutput() {
216 return RpcResultBuilder
217 .success(new AddFlowsBatchOutputBuilder()
218 .setBatchFailedFlowsOutput(Collections.<BatchFailedFlowsOutput>emptyList())
223 private RpcResult<AddFlowsBatchOutput> createAddFlowsBatchFailureOutcome() {
224 final RpcResult<List<BatchFailedFlowsOutput>> batchOutcomeWithError = createBatchOutcomeWithError();
225 return RpcResultBuilder.<AddFlowsBatchOutput>failed()
226 .withResult(new AddFlowsBatchOutputBuilder()
227 .setBatchFailedFlowsOutput(batchOutcomeWithError.getResult())
229 .withRpcErrors(batchOutcomeWithError.getErrors())