2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.services.batch;
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.ProcessFlatBatchOutputBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.meter._case.FlatBatchAddMeter;
17 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;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.meter._case.FlatBatchRemoveMeter;
19 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;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.meter._case.FlatBatchUpdateMeter;
21 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;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailure;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailureBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.batch.failure.batch.item.id.choice.FlatBatchFailureMeterIdCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.batch.failure.batch.item.id.choice.FlatBatchFailureMeterIdCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.AddMetersBatchInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.AddMetersBatchOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.BatchMeterOutputListGrouping;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.RemoveMetersBatchInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.UpdateMetersBatchInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.batch.meter.input.update.grouping.OriginalBatchedMeterBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.batch.meter.input.update.grouping.UpdatedBatchedMeterBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.batch.meter.output.list.grouping.BatchFailedMetersOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.batch.meter.output.list.grouping.BatchFailedMetersOutputBuilder;
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;
47 * Test for {@link FlatBatchMeterAdapters}.
49 public class FlatBatchMeterAdaptersTest {
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);
57 public void testAdaptFlatBatchAddMeter() throws Exception {
58 final BatchPlanStep planStep = new BatchPlanStep(BatchStepType.FLOW_ADD);
59 planStep.setBarrierAfter(true);
60 planStep.getTaskBag().addAll(Lists.newArrayList(
61 createAddMeterBatch(1L),
62 createAddMeterBatch(2L)));
64 final AddMetersBatchInput addMetersBatchInput = FlatBatchMeterAdapters.adaptFlatBatchAddMeter(planStep, NODE_REF);
66 Assert.assertTrue(addMetersBatchInput.isBarrierAfter());
67 Assert.assertEquals(2, addMetersBatchInput.getBatchAddMeters().size());
68 Assert.assertEquals(1L, addMetersBatchInput.getBatchAddMeters().get(0).getMeterId().getValue().longValue());
69 Assert.assertEquals(2L, addMetersBatchInput.getBatchAddMeters().get(1).getMeterId().getValue().longValue());
72 private FlatBatchAddMeter createAddMeterBatch(final long groupIdValue) {
73 return new FlatBatchAddMeterBuilder()
74 .setMeterId(new MeterId(groupIdValue))
78 private FlatBatchRemoveMeter createRemoveMeterBatch(final long groupIdValue) {
79 return new FlatBatchRemoveMeterBuilder()
80 .setMeterId(new MeterId(groupIdValue))
84 private FlatBatchUpdateMeter createUpdateMeterBatch(final long groupIdValue) {
85 return new FlatBatchUpdateMeterBuilder()
86 .setOriginalBatchedMeter(new OriginalBatchedMeterBuilder()
87 .setMeterId(new MeterId(groupIdValue))
89 .setUpdatedBatchedMeter(new UpdatedBatchedMeterBuilder()
90 .setMeterId(new MeterId(groupIdValue))
96 public void testAdaptFlatBatchRemoveMeter() throws Exception {
97 final BatchPlanStep planStep = new BatchPlanStep(BatchStepType.FLOW_REMOVE);
98 planStep.setBarrierAfter(true);
99 planStep.getTaskBag().addAll(Lists.newArrayList(
100 createRemoveMeterBatch(1L),
101 createRemoveMeterBatch(2L)));
103 final RemoveMetersBatchInput removeMetersBatchInput = FlatBatchMeterAdapters.adaptFlatBatchRemoveMeter(planStep, NODE_REF);
105 Assert.assertTrue(removeMetersBatchInput.isBarrierAfter());
106 Assert.assertEquals(2, removeMetersBatchInput.getBatchRemoveMeters().size());
107 Assert.assertEquals(1L, removeMetersBatchInput.getBatchRemoveMeters().get(0).getMeterId().getValue().longValue());
108 Assert.assertEquals(2L, removeMetersBatchInput.getBatchRemoveMeters().get(1).getMeterId().getValue().longValue());
112 public void testAdaptFlatBatchUpdateMeter() throws Exception {
113 final BatchPlanStep planStep = new BatchPlanStep(BatchStepType.FLOW_UPDATE);
114 planStep.setBarrierAfter(true);
115 planStep.getTaskBag().addAll(Lists.newArrayList(
116 createUpdateMeterBatch(1L),
117 createUpdateMeterBatch(2L)));
119 final UpdateMetersBatchInput updateMetersBatchInput = FlatBatchMeterAdapters.adaptFlatBatchUpdateMeter(planStep, NODE_REF);
121 Assert.assertTrue(updateMetersBatchInput.isBarrierAfter());
122 Assert.assertEquals(2, updateMetersBatchInput.getBatchUpdateMeters().size());
123 Assert.assertEquals(1L, updateMetersBatchInput.getBatchUpdateMeters().get(0).getUpdatedBatchedMeter().getMeterId().getValue().longValue());
124 Assert.assertEquals(2L, updateMetersBatchInput.getBatchUpdateMeters().get(1).getUpdatedBatchedMeter().getMeterId().getValue().longValue());
128 public void testCreateBatchMeterChainingFunction_failures() throws Exception {
129 final RpcResult<ProcessFlatBatchOutput> chainInput = RpcResultBuilder.<ProcessFlatBatchOutput>failed()
130 .withError(RpcError.ErrorType.APPLICATION, "ut-chainError")
131 .withResult(new ProcessFlatBatchOutputBuilder()
132 .setBatchFailure(Lists.newArrayList(
133 createChainFailure(0, 1L),
134 createChainFailure(1, 2L)))
138 final RpcResult<BatchMeterOutputListGrouping> input = RpcResultBuilder.<BatchMeterOutputListGrouping>failed()
139 .withError(RpcError.ErrorType.APPLICATION, "ut-groupError")
140 .withResult(new AddMetersBatchOutputBuilder()
141 .setBatchFailedMetersOutput(Lists.newArrayList(
142 createBatchFailedMetersOutput(0, 3L),
143 createBatchFailedMetersOutput(1, 4L)
148 final RpcResult<ProcessFlatBatchOutput> rpcResult = FlatBatchMeterAdapters
149 .createBatchMeterChainingFunction(chainInput, 2).apply(input);
151 Assert.assertFalse(rpcResult.isSuccessful());
152 Assert.assertEquals(2, rpcResult.getErrors().size());
153 Assert.assertEquals(4, rpcResult.getResult().getBatchFailure().size());
154 Assert.assertEquals(0, rpcResult.getResult().getBatchFailure().get(0).getBatchOrder().intValue());
155 Assert.assertEquals(1, rpcResult.getResult().getBatchFailure().get(1).getBatchOrder().intValue());
156 Assert.assertEquals(2, rpcResult.getResult().getBatchFailure().get(2).getBatchOrder().intValue());
157 Assert.assertEquals(3, rpcResult.getResult().getBatchFailure().get(3).getBatchOrder().intValue());
158 Assert.assertEquals(4L, ((FlatBatchFailureMeterIdCase) rpcResult.getResult().getBatchFailure().get(3).getBatchItemIdChoice()).getMeterId().getValue().longValue());
162 public void testCreateBatchMeterChainingFunction_successes() throws Exception {
163 final RpcResult<ProcessFlatBatchOutput> chainInput = RpcResultBuilder
164 .success(new ProcessFlatBatchOutputBuilder().build())
166 final RpcResult<BatchMeterOutputListGrouping> input = RpcResultBuilder
167 .<BatchMeterOutputListGrouping>success(new AddMetersBatchOutputBuilder().build())
170 final RpcResult<ProcessFlatBatchOutput> rpcResult = FlatBatchMeterAdapters
171 .createBatchMeterChainingFunction(chainInput, 0).apply(input);
173 Assert.assertTrue(rpcResult.isSuccessful());
174 Assert.assertEquals(0, rpcResult.getErrors().size());
175 Assert.assertEquals(0, rpcResult.getResult().getBatchFailure().size());
178 private BatchFailedMetersOutput createBatchFailedMetersOutput(final Integer batchOrder, final long groupIdValue) {
179 return new BatchFailedMetersOutputBuilder()
180 .setMeterId(new MeterId(groupIdValue))
181 .setBatchOrder(batchOrder)
185 private BatchFailure createChainFailure(final int batchOrder, final long groupIdValue) {
186 return new BatchFailureBuilder()
187 .setBatchOrder(batchOrder)
188 .setBatchItemIdChoice(new FlatBatchFailureMeterIdCaseBuilder()
189 .setMeterId(new MeterId(groupIdValue))