Merge "Bug 6366 - of-switch-config-pusher - DTCL instead of DTL"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / util / FlatBatchUtilTest.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.util;
10
11 import com.google.common.collect.Lists;
12 import java.util.ArrayList;
13 import java.util.EnumSet;
14 import java.util.List;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.openflowplugin.impl.services.batch.BatchPlanStep;
18 import org.opendaylight.openflowplugin.impl.services.batch.BatchStepType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.Batch;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.BatchBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.BatchChoice;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddFlowCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddGroupCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddMeterCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveFlowCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveGroupCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveMeterCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateFlowCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateGroupCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateMeterCaseBuilder;
33 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;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.group._case.FlatBatchAddGroupBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.meter._case.FlatBatchAddMeterBuilder;
36 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;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.group._case.FlatBatchRemoveGroupBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.meter._case.FlatBatchRemoveMeterBuilder;
39 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;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.group._case.FlatBatchUpdateGroupBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.meter._case.FlatBatchUpdateMeterBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailure;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailureBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.batch.failure.batch.item.id.choice.FlatBatchFailureFlowIdCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
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.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * Test for {@link FlatBatchUtil}.
54  */
55 public class FlatBatchUtilTest {
56
57     private static final Logger LOG = LoggerFactory.getLogger(FlatBatchUtilTest.class);
58
59     @Test
60     public void testMarkBarriersWhereNeeded_noBarrier() throws Exception {
61         final List<Batch> batches = Lists.newArrayList(
62                 //general part - no flush required
63                 createBatch(BatchStepType.GROUP_REMOVE),
64                 createBatch(BatchStepType.METER_REMOVE),
65                 createBatch(BatchStepType.FLOW_ADD),
66                 createBatch(BatchStepType.FLOW_REMOVE, 2),
67                 createBatch(BatchStepType.FLOW_ADD),
68                 createBatch(BatchStepType.FLOW_UPDATE),
69                 createBatch(BatchStepType.GROUP_ADD),
70                 createBatch(BatchStepType.GROUP_UPDATE),
71                 createBatch(BatchStepType.METER_ADD),
72                 createBatch(BatchStepType.METER_UPDATE)
73         );
74
75         final List<BatchPlanStep> batchPlan = FlatBatchUtil.assembleBatchPlan(batches);
76         FlatBatchUtil.markBarriersWhereNeeded(batchPlan);
77
78         Assert.assertEquals(10, batchPlan.size());
79         for (int i = 0; i < batchPlan.size(); i++) {
80             final BatchPlanStep planStep = batchPlan.get(i);
81             final boolean barrierBefore = planStep.isBarrierAfter();
82             LOG.debug("checking barrier mark @ {} {} -> {}",
83                     i, planStep.getStepType(), barrierBefore);
84
85             Assert.assertFalse(barrierBefore);
86         }
87     }
88
89     @Test
90     public void testMarkBarriersWhereNeeded_allBarriers() throws Exception {
91         // need to flush G+/F+
92         checkBarriersBetween(BatchStepType.GROUP_ADD, BatchStepType.FLOW_ADD);
93         // need to flush G+/F*
94         checkBarriersBetween(BatchStepType.GROUP_ADD, BatchStepType.FLOW_UPDATE);
95         // need to flush F-/G-
96         checkBarriersBetween(BatchStepType.FLOW_REMOVE, BatchStepType.GROUP_REMOVE);
97         // need to flush F*/G-
98         checkBarriersBetween(BatchStepType.FLOW_UPDATE, BatchStepType.GROUP_REMOVE);
99
100         // need to flush G+/G+
101         checkBarriersBetween(BatchStepType.GROUP_ADD, BatchStepType.GROUP_ADD);
102         // need to flush G-/G-
103         checkBarriersBetween(BatchStepType.GROUP_REMOVE, BatchStepType.GROUP_REMOVE);
104         // need to flush G*/G+
105         checkBarriersBetween(BatchStepType.GROUP_UPDATE, BatchStepType.GROUP_ADD);
106         // need to flush G*/G-
107         checkBarriersBetween(BatchStepType.GROUP_UPDATE, BatchStepType.GROUP_REMOVE);
108
109         // need to flush M+/F+
110         checkBarriersBetween(BatchStepType.METER_ADD, BatchStepType.FLOW_ADD);
111         // need to flush M+/F*
112         checkBarriersBetween(BatchStepType.METER_ADD, BatchStepType.FLOW_UPDATE);
113         // need to flush F-/M-
114         checkBarriersBetween(BatchStepType.FLOW_REMOVE, BatchStepType.METER_REMOVE);
115         // need to flush F*/M-
116         checkBarriersBetween(BatchStepType.FLOW_UPDATE, BatchStepType.METER_REMOVE);
117     }
118
119     private void checkBarriersBetween(final BatchStepType typeOfFirst, final BatchStepType typeOfSecond) {
120         final List<Batch> batches = Lists.newArrayList(createBatch(typeOfFirst), createBatch(typeOfSecond));
121         final List<BatchPlanStep> batchPlan = FlatBatchUtil.assembleBatchPlan(batches);
122         FlatBatchUtil.markBarriersWhereNeeded(batchPlan);
123         LOG.debug("checking barrier between {} / {}", typeOfFirst, typeOfSecond);
124         Assert.assertEquals(2, batchPlan.size());
125         Assert.assertTrue("barrier expected between " + typeOfFirst + " / " + typeOfSecond, batchPlan.get(0).isBarrierAfter());
126         Assert.assertFalse(batchPlan.get(1).isBarrierAfter());
127     }
128
129     @Test
130     public void testMarkBarriersWhereNeeded_single() throws Exception {
131         final List<Batch> batches = Lists.newArrayList(
132                 //general part - no flush required
133                 createBatch(BatchStepType.GROUP_REMOVE)
134         );
135
136         final List<BatchPlanStep> batchPlan = FlatBatchUtil.assembleBatchPlan(batches);
137         FlatBatchUtil.markBarriersWhereNeeded(batchPlan);
138
139         Assert.assertEquals(1, batchPlan.size());
140         Assert.assertFalse(batchPlan.get(0).isBarrierAfter());
141     }
142
143     @Test
144     public void testDecideBarrier() throws Exception {
145         Assert.assertTrue(FlatBatchUtil.decideBarrier(EnumSet.of(BatchStepType.GROUP_ADD), BatchStepType.FLOW_ADD));
146         Assert.assertTrue(FlatBatchUtil.decideBarrier(EnumSet.of(BatchStepType.GROUP_ADD), BatchStepType.FLOW_UPDATE));
147
148         Assert.assertTrue(FlatBatchUtil.decideBarrier(EnumSet.of(BatchStepType.FLOW_REMOVE), BatchStepType.GROUP_REMOVE));
149         Assert.assertTrue(FlatBatchUtil.decideBarrier(EnumSet.of(BatchStepType.FLOW_UPDATE), BatchStepType.GROUP_REMOVE));
150
151         Assert.assertTrue(FlatBatchUtil.decideBarrier(EnumSet.of(BatchStepType.METER_ADD), BatchStepType.FLOW_ADD));
152         Assert.assertTrue(FlatBatchUtil.decideBarrier(EnumSet.of(BatchStepType.METER_ADD), BatchStepType.FLOW_UPDATE));
153
154         Assert.assertTrue(FlatBatchUtil.decideBarrier(EnumSet.of(BatchStepType.FLOW_REMOVE), BatchStepType.METER_REMOVE));
155         Assert.assertTrue(FlatBatchUtil.decideBarrier(EnumSet.of(BatchStepType.FLOW_UPDATE), BatchStepType.METER_REMOVE));
156     }
157
158     @Test
159     public void testAssembleBatchPlan() throws Exception {
160         final List<Batch> batches = Lists.newArrayList(
161                 createBatch(BatchStepType.GROUP_ADD),
162                 createBatch(BatchStepType.GROUP_REMOVE, 2),
163                 createBatch(BatchStepType.GROUP_REMOVE),
164                 createBatch(BatchStepType.GROUP_ADD),
165                 createBatch(BatchStepType.GROUP_UPDATE, 3)
166         );
167
168         final List<BatchPlanStep> batchPlanSteps = FlatBatchUtil.assembleBatchPlan(batches);
169         Assert.assertEquals(5, batchPlanSteps.size());
170
171         int i = 0;
172         checkSegment(batchPlanSteps.get(i++), BatchStepType.GROUP_ADD, 1);
173         checkSegment(batchPlanSteps.get(i++), BatchStepType.GROUP_REMOVE, 2);
174         checkSegment(batchPlanSteps.get(i++), BatchStepType.GROUP_REMOVE, 1);
175         checkSegment(batchPlanSteps.get(i++), BatchStepType.GROUP_ADD, 1);
176         checkSegment(batchPlanSteps.get(i++), BatchStepType.GROUP_UPDATE, 3);
177     }
178
179     private void checkSegment(final BatchPlanStep planStep, final BatchStepType stepType, final int expected) {
180         Assert.assertEquals(stepType, planStep.getStepType());
181         Assert.assertEquals(expected, planStep.getTaskBag().size());
182     }
183
184     @Test
185     public void testDetectBatchStepType() throws Exception {
186         for (BatchStepType stepType : BatchStepType.values()) {
187             LOG.debug("checking detection of: {}", stepType);
188             final Batch batch = createBatch(stepType);
189             final BatchStepType actualType = FlatBatchUtil.detectBatchStepType(batch.getBatchChoice());
190             Assert.assertEquals(stepType, actualType);
191         }
192     }
193
194     private Batch createBatch(BatchStepType type) {
195         return createBatch(type, 1);
196     }
197
198     private Batch createBatch(BatchStepType type, final int size) {
199         final BatchChoice batchCase;
200         switch (type) {
201             case FLOW_ADD:
202                 batchCase = new FlatBatchAddFlowCaseBuilder()
203                         .setFlatBatchAddFlow(repeatIntoList(new FlatBatchAddFlowBuilder().build(), size))
204                         .build();
205                 break;
206             case FLOW_REMOVE:
207                 batchCase = new FlatBatchRemoveFlowCaseBuilder()
208                         .setFlatBatchRemoveFlow(repeatIntoList(new FlatBatchRemoveFlowBuilder().build(), size))
209                         .build();
210                 break;
211             case FLOW_UPDATE:
212                 batchCase = new FlatBatchUpdateFlowCaseBuilder()
213                         .setFlatBatchUpdateFlow(repeatIntoList(new FlatBatchUpdateFlowBuilder().build(), size))
214                         .build();
215                 break;
216             case GROUP_ADD:
217                 batchCase = new FlatBatchAddGroupCaseBuilder()
218                         .setFlatBatchAddGroup(repeatIntoList(new FlatBatchAddGroupBuilder().build(), size))
219                         .build();
220                 break;
221             case GROUP_REMOVE:
222                 batchCase = new FlatBatchRemoveGroupCaseBuilder()
223                         .setFlatBatchRemoveGroup(repeatIntoList(new FlatBatchRemoveGroupBuilder().build(), size))
224                         .build();
225                 break;
226             case GROUP_UPDATE:
227                 batchCase = new FlatBatchUpdateGroupCaseBuilder()
228                         .setFlatBatchUpdateGroup(repeatIntoList(new FlatBatchUpdateGroupBuilder().build(), size))
229                         .build();
230                 break;
231             case METER_ADD:
232                 batchCase = new FlatBatchAddMeterCaseBuilder()
233                         .setFlatBatchAddMeter(repeatIntoList(new FlatBatchAddMeterBuilder().build(), size))
234                         .build();
235                 break;
236             case METER_REMOVE:
237                 batchCase = new FlatBatchRemoveMeterCaseBuilder()
238                         .setFlatBatchRemoveMeter(repeatIntoList(new FlatBatchRemoveMeterBuilder().build(), size))
239                         .build();
240                 break;
241             case METER_UPDATE:
242                 batchCase = new FlatBatchUpdateMeterCaseBuilder()
243                         .setFlatBatchUpdateMeter(repeatIntoList(new FlatBatchUpdateMeterBuilder().build(), size))
244                         .build();
245                 break;
246             default:
247                 LOG.warn("unsupported batch type: {}", type);
248                 throw new IllegalArgumentException("unsupported batch type: " + type);
249         }
250
251         return new BatchBuilder()
252                 .setBatchChoice(batchCase)
253                 .build();
254     }
255
256     private <T> List<T> repeatIntoList(final T element, final int size) {
257         final List<T> list = new ArrayList<>();
258         for (int i = 0; i < size; i++) {
259             list.add(element);
260         }
261         return list;
262     }
263
264     @Test
265     public void testMergeJobsResultsFutures() throws Exception {
266         final BatchFailure batchFailure = new BatchFailureBuilder()
267                 .setBatchOrder(9)
268                 .setBatchItemIdChoice(new FlatBatchFailureFlowIdCaseBuilder()
269                         .setFlowId(new FlowId("11"))
270                         .build())
271                 .build();
272
273         final ProcessFlatBatchOutput output = new ProcessFlatBatchOutputBuilder().setBatchFailure(Lists.newArrayList(batchFailure)).build();
274
275         final RpcResult<ProcessFlatBatchOutput> rpcResultFailed = RpcResultBuilder.<ProcessFlatBatchOutput>failed()
276                 .withError(RpcError.ErrorType.APPLICATION, "ut-rpcError")
277                 .withResult(output).build();
278         final RpcResult<ProcessFlatBatchOutput> rpcResultSuccess = RpcResultBuilder.<ProcessFlatBatchOutput>success()
279                 .withResult(new ProcessFlatBatchOutputBuilder().setBatchFailure(new ArrayList<>())).build();
280
281         final RpcResult<ProcessFlatBatchOutput> rpcResult1 = FlatBatchUtil.mergeRpcResults().apply(Lists.newArrayList(rpcResultFailed, rpcResultSuccess));
282         Assert.assertEquals(1, rpcResult1.getErrors().size());
283         Assert.assertFalse(rpcResult1.isSuccessful());
284
285         final RpcResult<ProcessFlatBatchOutput> rpcResult2 = FlatBatchUtil.mergeRpcResults().apply(Lists.newArrayList(rpcResultFailed, rpcResultFailed));
286         Assert.assertEquals(2, rpcResult2.getErrors().size());
287         Assert.assertFalse(rpcResult2.isSuccessful());
288
289         final RpcResult<ProcessFlatBatchOutput> rpcResult3 = FlatBatchUtil.mergeRpcResults().apply(Lists.newArrayList(rpcResultSuccess, rpcResultSuccess));
290         Assert.assertEquals(0, rpcResult3.getErrors().size());
291         Assert.assertTrue(rpcResult3.isSuccessful());
292     }
293 }