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