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