2 * Copyright (c) 2016 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.services.batch;
11 import com.google.common.collect.Lists;
12 import java.util.Iterator;
13 import org.junit.Assert;
14 import org.junit.Test;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutput;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.flow._case.FlatBatchAddFlow;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.flow._case.FlatBatchAddFlowBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.flow._case.FlatBatchRemoveFlow;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.flow._case.FlatBatchRemoveFlowBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.flow._case.FlatBatchUpdateFlow;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.flow._case.FlatBatchUpdateFlowBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailure;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailureBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.batch.failure.batch.item.id.choice.FlatBatchFailureFlowIdCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.batch.failure.batch.item.id.choice.FlatBatchFailureFlowIdCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchOutputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.BatchFlowOutputListGrouping;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.RemoveFlowsBatchInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.UpdateFlowsBatchInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.add.flows.batch.input.BatchAddFlows;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.output.list.grouping.BatchFailedFlowsOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.output.list.grouping.BatchFailedFlowsOutputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.remove.flows.batch.input.BatchRemoveFlows;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.update.flows.batch.input.BatchUpdateFlows;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.common.RpcError;
44 import org.opendaylight.yangtools.yang.common.RpcResult;
45 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 * Test for {@link FlatBatchFlowAdapters}.
50 public class FlatBatchFlowAdaptersTest {
52 private static final NodeId NODE_ID = new NodeId("ut-node-id");
53 private static final InstanceIdentifier<Node> NODE_II = InstanceIdentifier.create(Nodes.class)
54 .child(Node.class, new NodeKey(NODE_ID));
55 private static final NodeRef NODE_REF = new NodeRef(NODE_II);
58 public void testAdaptFlatBatchAddFlow() {
59 final BatchPlanStep planStep = new BatchPlanStep(BatchStepType.FLOW_ADD);
60 planStep.setBarrierAfter(true);
61 planStep.getTaskBag().addAll(Lists.newArrayList(
62 createAddFlowBatch("1"),
63 createAddFlowBatch("2")));
65 final AddFlowsBatchInput addFlowsBatchInput = FlatBatchFlowAdapters.adaptFlatBatchAddFlow(planStep, NODE_REF);
66 Iterator<BatchAddFlows> iterator = addFlowsBatchInput.nonnullBatchAddFlows().values().iterator();
68 Assert.assertTrue(addFlowsBatchInput.isBarrierAfter());
69 Assert.assertEquals(2, addFlowsBatchInput.getBatchAddFlows().size());
70 Assert.assertEquals("1", iterator.next().getFlowId().getValue());
71 Assert.assertEquals("2", iterator.next().getFlowId().getValue());
74 private FlatBatchAddFlow createAddFlowBatch(final String flowIdValue) {
75 return new FlatBatchAddFlowBuilder()
76 .setFlowId(new FlowId(flowIdValue))
80 private FlatBatchRemoveFlow createRemoveFlowBatch(final String flowIdValue) {
81 return new FlatBatchRemoveFlowBuilder()
82 .setFlowId(new FlowId(flowIdValue))
86 private FlatBatchUpdateFlow createUpdateFlowBatch(final String flowIdValue) {
87 return new FlatBatchUpdateFlowBuilder()
88 .setFlowId(new FlowId(flowIdValue))
93 public void testAdaptFlatBatchRemoveFlow() {
94 final BatchPlanStep planStep = new BatchPlanStep(BatchStepType.FLOW_REMOVE);
95 planStep.setBarrierAfter(true);
96 planStep.getTaskBag().addAll(Lists.newArrayList(
97 createRemoveFlowBatch("1"),
98 createRemoveFlowBatch("2")));
100 final RemoveFlowsBatchInput removeFlowsBatchInput =
101 FlatBatchFlowAdapters.adaptFlatBatchRemoveFlow(planStep, NODE_REF);
102 Iterator<BatchRemoveFlows> iterator = removeFlowsBatchInput.nonnullBatchRemoveFlows().values().iterator();
104 Assert.assertTrue(removeFlowsBatchInput.isBarrierAfter());
105 Assert.assertEquals(2, removeFlowsBatchInput.getBatchRemoveFlows().size());
106 Assert.assertEquals("1", iterator.next().getFlowId().getValue());
107 Assert.assertEquals("2", iterator.next().getFlowId().getValue());
111 public void testAdaptFlatBatchUpdateFlow() {
112 final BatchPlanStep planStep = new BatchPlanStep(BatchStepType.FLOW_UPDATE);
113 planStep.setBarrierAfter(true);
114 planStep.getTaskBag().addAll(Lists.newArrayList(
115 createUpdateFlowBatch("1"),
116 createUpdateFlowBatch("2")));
118 final UpdateFlowsBatchInput updateFlowsBatchInput =
119 FlatBatchFlowAdapters.adaptFlatBatchUpdateFlow(planStep, NODE_REF);
120 Iterator<BatchUpdateFlows> iterator = updateFlowsBatchInput.nonnullBatchUpdateFlows().values().iterator();
122 Assert.assertTrue(updateFlowsBatchInput.isBarrierAfter());
123 Assert.assertEquals(2, updateFlowsBatchInput.getBatchUpdateFlows().size());
124 Assert.assertEquals("1", iterator.next().getFlowId().getValue());
125 Assert.assertEquals("2", iterator.next().getFlowId().getValue());
129 public void testCreateBatchFlowChainingFunction_failures() {
130 final RpcResult<BatchFlowOutputListGrouping> input = RpcResultBuilder.<BatchFlowOutputListGrouping>failed()
131 .withError(RpcError.ErrorType.APPLICATION, "ut-flowError")
132 .withResult(new AddFlowsBatchOutputBuilder()
133 .setBatchFailedFlowsOutput(Lists.newArrayList(
134 createBatchFailedFlowsOutput(0, "f1"),
135 createBatchFailedFlowsOutput(1, "f2")
140 final RpcResult<ProcessFlatBatchOutput> rpcResult = FlatBatchFlowAdapters
141 .convertBatchFlowResult(3).apply(input);
142 Iterator<BatchFailure> iterator = rpcResult.getResult().nonnullBatchFailure().values().iterator();
144 Assert.assertFalse(rpcResult.isSuccessful());
145 Assert.assertEquals(1, rpcResult.getErrors().size());
146 Assert.assertEquals(2, rpcResult.getResult().getBatchFailure().size());
147 Assert.assertEquals(3, iterator.next()
148 .getBatchOrder().intValue());
149 BatchFailure secondBatchFailure = iterator.next();
150 Assert.assertEquals(4, secondBatchFailure.getBatchOrder().intValue());
151 Assert.assertEquals("f2",
152 ((FlatBatchFailureFlowIdCase) secondBatchFailure.getBatchItemIdChoice())
153 .getFlowId().getValue());
157 public void testCreateBatchFlowChainingFunction_successes() {
158 final RpcResult<BatchFlowOutputListGrouping> input = RpcResultBuilder
159 .<BatchFlowOutputListGrouping>success(new AddFlowsBatchOutputBuilder().build())
162 final RpcResult<ProcessFlatBatchOutput> rpcResult = FlatBatchFlowAdapters
163 .convertBatchFlowResult(0).apply(input);
165 Assert.assertTrue(rpcResult.isSuccessful());
166 Assert.assertEquals(0, rpcResult.getErrors().size());
167 Assert.assertEquals(0, rpcResult.getResult().nonnullBatchFailure().size());
170 private BatchFailedFlowsOutput createBatchFailedFlowsOutput(final Integer batchOrder, final String flowIdValue) {
171 return new BatchFailedFlowsOutputBuilder()
172 .setFlowId(new FlowId(flowIdValue))
173 .setBatchOrder(batchOrder)
177 private BatchFailure createChainFailure(final int batchOrder, final String flowIdValue) {
178 return new BatchFailureBuilder()
179 .setBatchOrder(batchOrder)
180 .setBatchItemIdChoice(new FlatBatchFailureFlowIdCaseBuilder()
181 .setFlowId(new FlowId(flowIdValue))