Migrate openflowplugim-impl tests to uint types
[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 import org.opendaylight.yangtools.yang.common.Uint16;
46
47 /**
48  * Test for {@link FlatBatchFlowAdapters}.
49  */
50 public class FlatBatchFlowAdaptersTest {
51
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);
56
57     @Test
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")));
64
65         final AddFlowsBatchInput addFlowsBatchInput = FlatBatchFlowAdapters.adaptFlatBatchAddFlow(planStep, NODE_REF);
66         Iterator<BatchAddFlows> iterator = addFlowsBatchInput.nonnullBatchAddFlows().values().iterator();
67
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());
72     }
73
74     private static FlatBatchAddFlow createAddFlowBatch(final String flowIdValue) {
75         return new FlatBatchAddFlowBuilder()
76                 .setFlowId(new FlowId(flowIdValue))
77                 .build();
78     }
79
80     private static FlatBatchRemoveFlow createRemoveFlowBatch(final String flowIdValue) {
81         return new FlatBatchRemoveFlowBuilder()
82                 .setFlowId(new FlowId(flowIdValue))
83                 .build();
84     }
85
86     private static FlatBatchUpdateFlow createUpdateFlowBatch(final String flowIdValue) {
87         return new FlatBatchUpdateFlowBuilder()
88                 .setFlowId(new FlowId(flowIdValue))
89                 .build();
90     }
91
92     @Test
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")));
99
100         final RemoveFlowsBatchInput removeFlowsBatchInput =
101                 FlatBatchFlowAdapters.adaptFlatBatchRemoveFlow(planStep, NODE_REF);
102         Iterator<BatchRemoveFlows> iterator = removeFlowsBatchInput.nonnullBatchRemoveFlows().values().iterator();
103
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());
108     }
109
110     @Test
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")));
117
118         final UpdateFlowsBatchInput updateFlowsBatchInput =
119                 FlatBatchFlowAdapters.adaptFlatBatchUpdateFlow(planStep, NODE_REF);
120         Iterator<BatchUpdateFlows> iterator = updateFlowsBatchInput.nonnullBatchUpdateFlows().values().iterator();
121
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());
126     }
127
128     @Test
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(Uint16.ZERO, "f1"),
135                                 createBatchFailedFlowsOutput(Uint16.ONE, "f2")
136                         ))
137                         .build())
138                 .build();
139
140         final RpcResult<ProcessFlatBatchOutput> rpcResult = FlatBatchFlowAdapters
141                 .convertBatchFlowResult(3).apply(input);
142         Iterator<BatchFailure> iterator = rpcResult.getResult().nonnullBatchFailure().values().iterator();
143
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());
154     }
155
156     @Test
157     public void testCreateBatchFlowChainingFunction_successes() {
158         final RpcResult<BatchFlowOutputListGrouping> input = RpcResultBuilder
159                 .<BatchFlowOutputListGrouping>success(new AddFlowsBatchOutputBuilder().build())
160                 .build();
161
162         final RpcResult<ProcessFlatBatchOutput> rpcResult = FlatBatchFlowAdapters
163                 .convertBatchFlowResult(0).apply(input);
164
165         Assert.assertTrue(rpcResult.isSuccessful());
166         Assert.assertEquals(0, rpcResult.getErrors().size());
167         Assert.assertEquals(0, rpcResult.getResult().nonnullBatchFailure().size());
168     }
169
170     private static BatchFailedFlowsOutput createBatchFailedFlowsOutput(final Uint16 batchOrder,
171             final String flowIdValue) {
172         return new BatchFailedFlowsOutputBuilder()
173                 .setFlowId(new FlowId(flowIdValue))
174                 .setBatchOrder(batchOrder)
175                 .build();
176     }
177
178     private static BatchFailure createChainFailure(final Uint16 batchOrder, final String flowIdValue) {
179         return new BatchFailureBuilder()
180                 .setBatchOrder(batchOrder)
181                 .setBatchItemIdChoice(new FlatBatchFailureFlowIdCaseBuilder()
182                         .setFlowId(new FlowId(flowIdValue))
183                         .build())
184                 .build();
185     }
186 }