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