Merge "Drop the odlparent.netty property"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / util / MeterUtilTest.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.base.Function;
12 import com.google.common.collect.Lists;
13 import java.util.Collections;
14 import java.util.List;
15 import org.apache.commons.lang3.tuple.Pair;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.AddMetersBatchOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.AddMetersBatchOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.BatchMeterOutputListGrouping;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.batch.meter.output.list.grouping.BatchFailedMetersOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.batch.meter.output.list.grouping.BatchFailedMetersOutputBuilder;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcError;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35
36 /**
37  * Test for {@link MeterUtil}.
38  */
39 public class MeterUtilTest {
40
41     public static final NodeId DUMMY_NODE_ID = new NodeId("dummyNodeId");
42     private static final MeterId DUMMY_METER_ID = new MeterId(42L);
43     private static final MeterId DUMMY_METER_ID_2 = new MeterId(43L);
44
45     @Test
46     public void testBuildGroupPath() throws Exception {
47         final InstanceIdentifier<Node> nodePath = InstanceIdentifier
48                 .create(Nodes.class)
49                 .child(Node.class, new NodeKey(DUMMY_NODE_ID));
50
51         final MeterRef meterRef = MeterUtil.buildMeterPath(nodePath, DUMMY_METER_ID);
52         final InstanceIdentifier<?> meterRefValue = meterRef.getValue();
53         Assert.assertEquals(DUMMY_NODE_ID, meterRefValue.firstKeyOf(Node.class).getId());
54         Assert.assertEquals(DUMMY_METER_ID, meterRefValue.firstKeyOf(Meter.class).getMeterId());
55     }
56
57     @Test
58     public void testCreateCumulatingFunction() throws Exception {
59         final Function<List<RpcResult<String>>, RpcResult<List<BatchFailedMetersOutput>>> function =
60                 MeterUtil.createCumulativeFunction(Lists.newArrayList(
61                         createBatchMeter(DUMMY_METER_ID),
62                         createBatchMeter(DUMMY_METER_ID_2)));
63
64         final RpcResult<List<BatchFailedMetersOutput>> output = function.apply(Lists.newArrayList(
65                 RpcResultBuilder.success("a").build(),
66                 RpcResultBuilder.<String>failed()
67                         .withError(RpcError.ErrorType.APPLICATION, "ut-meter-error")
68                         .build()));
69
70         Assert.assertFalse(output.isSuccessful());
71         Assert.assertEquals(1, output.getResult().size());
72         Assert.assertEquals(DUMMY_METER_ID_2, output.getResult().get(0).getMeterId());
73         Assert.assertEquals(1, output.getResult().get(0).getBatchOrder().intValue());
74     }
75
76     private org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter createBatchMeter(
77             final MeterId meterId) {
78         return new MeterBuilder()
79                 .setMeterId(meterId)
80                 .build();
81     }
82
83     @Test
84     public void testMeterAddTransformFailure() throws Exception {
85         final RpcResult<List<BatchFailedMetersOutput>> input = createBatchOutcomeWithError();
86         checkBatchErrorOutcomeTransformation(MeterUtil.METER_ADD_TRANSFORM.apply(input));
87     }
88
89     @Test
90     public void testMeterAddTransformSuccess() throws Exception {
91         final RpcResult<List<BatchFailedMetersOutput>> input = createEmptyBatchOutcome();
92         checkBatchSuccessOutcomeTransformation(MeterUtil.METER_ADD_TRANSFORM.apply(input));
93     }
94
95     @Test
96     public void testMeterRemoveTransformFailure() throws Exception {
97         final RpcResult<List<BatchFailedMetersOutput>> input = createBatchOutcomeWithError();
98         checkBatchErrorOutcomeTransformation(MeterUtil.METER_REMOVE_TRANSFORM.apply(input));
99     }
100
101     @Test
102     public void testFlowRemoveTransformSuccess() throws Exception {
103         final RpcResult<List<BatchFailedMetersOutput>> input = createEmptyBatchOutcome();
104         checkBatchSuccessOutcomeTransformation(MeterUtil.METER_REMOVE_TRANSFORM.apply(input));
105     }
106
107     @Test
108     public void testFlowUpdateTransformFailure() throws Exception {
109         final RpcResult<List<BatchFailedMetersOutput>> input = createBatchOutcomeWithError();
110         checkBatchErrorOutcomeTransformation(MeterUtil.METER_UPDATE_TRANSFORM.apply(input));
111     }
112
113     @Test
114     public void testFlowUpdateTransformSuccess() throws Exception {
115         final RpcResult<List<BatchFailedMetersOutput>> input = createEmptyBatchOutcome();
116         checkBatchSuccessOutcomeTransformation(MeterUtil.METER_UPDATE_TRANSFORM.apply(input));
117     }
118
119     private <T extends BatchMeterOutputListGrouping> void checkBatchSuccessOutcomeTransformation(
120             final RpcResult<T> output) {
121         Assert.assertTrue(output.isSuccessful());
122         Assert.assertEquals(0, output.getResult().getBatchFailedMetersOutput().size());
123         Assert.assertEquals(0, output.getErrors().size());
124     }
125
126     private RpcResult<List<BatchFailedMetersOutput>> createEmptyBatchOutcome() {
127         return RpcResultBuilder
128                 .<List<BatchFailedMetersOutput>>success(Collections.<BatchFailedMetersOutput>emptyList())
129                 .build();
130     }
131
132     private RpcResult<List<BatchFailedMetersOutput>> createBatchOutcomeWithError() {
133         return RpcResultBuilder.<List<BatchFailedMetersOutput>>failed()
134                 .withError(RpcError.ErrorType.APPLICATION, "ut-flowAddFail")
135                 .withResult(Collections.singletonList(new BatchFailedMetersOutputBuilder()
136                         .setMeterId(DUMMY_METER_ID)
137                         .build()))
138                 .build();
139     }
140
141     private <T extends BatchMeterOutputListGrouping> void checkBatchErrorOutcomeTransformation(
142             final RpcResult<T> output) {
143         Assert.assertFalse(output.isSuccessful());
144         Assert.assertEquals(1, output.getResult().getBatchFailedMetersOutput().size());
145         Assert.assertEquals(DUMMY_METER_ID, output.getResult().getBatchFailedMetersOutput().get(0).getMeterId());
146
147         Assert.assertEquals(1, output.getErrors().size());
148     }
149
150     @Test
151     public void testCreateComposingFunction_success_success() throws Exception {
152         final Function<Pair<RpcResult<AddMetersBatchOutput>, RpcResult<Void>>, RpcResult<AddMetersBatchOutput>>
153                 compositeFunction = MeterUtil.createComposingFunction();
154
155         final RpcResult<AddMetersBatchOutput> addGroupBatchOutput = createAddMetersBatchSuccessOutput();
156         final RpcResult<Void> barrierOutput = RpcResultBuilder.<Void>success().build();
157         final Pair<RpcResult<AddMetersBatchOutput>, RpcResult<Void>> input =
158                 Pair.of(addGroupBatchOutput, barrierOutput);
159         final RpcResult<AddMetersBatchOutput> composite = compositeFunction.apply(input);
160
161         Assert.assertTrue(composite.isSuccessful());
162         Assert.assertEquals(0, composite.getErrors().size());
163         Assert.assertEquals(0, composite.getResult().getBatchFailedMetersOutput().size());
164     }
165
166     @Test
167     public void testCreateComposingFunction_failure_success() throws Exception {
168         final Function<Pair<RpcResult<AddMetersBatchOutput>, RpcResult<Void>>, RpcResult<AddMetersBatchOutput>>
169                 compositeFunction = MeterUtil.createComposingFunction();
170
171         final RpcResult<AddMetersBatchOutput> addGroupBatchOutput = createAddMetersBatchFailureOutcome();
172         final RpcResult<Void> barrierOutput = RpcResultBuilder.<Void>success().build();
173         final Pair<RpcResult<AddMetersBatchOutput>, RpcResult<Void>> input =
174                 Pair.of(addGroupBatchOutput, barrierOutput);
175         final RpcResult<AddMetersBatchOutput> composite = compositeFunction.apply(input);
176
177         Assert.assertFalse(composite.isSuccessful());
178         Assert.assertEquals(1, composite.getErrors().size());
179         Assert.assertEquals(1, composite.getResult().getBatchFailedMetersOutput().size());
180     }
181
182     @Test
183     public void testCreateComposingFunction_success_failure() throws Exception {
184         final Function<Pair<RpcResult<AddMetersBatchOutput>, RpcResult<Void>>, RpcResult<AddMetersBatchOutput>>
185                 compositeFunction = MeterUtil.createComposingFunction();
186
187         final RpcResult<AddMetersBatchOutput> addGroupBatchOutput = createAddMetersBatchSuccessOutput();
188         final RpcResult<Void> barrierOutput = createBarrierFailureOutcome();
189         final Pair<RpcResult<AddMetersBatchOutput>, RpcResult<Void>> input =
190                 Pair.of(addGroupBatchOutput, barrierOutput);
191         final RpcResult<AddMetersBatchOutput> composite = compositeFunction.apply(input);
192
193         Assert.assertFalse(composite.isSuccessful());
194         Assert.assertEquals(1, composite.getErrors().size());
195         Assert.assertEquals(0, composite.getResult().getBatchFailedMetersOutput().size());
196     }
197
198     @Test
199     public void testCreateComposingFunction_failure_failure() throws Exception {
200         final Function<Pair<RpcResult<AddMetersBatchOutput>, RpcResult<Void>>, RpcResult<AddMetersBatchOutput>>
201                 compositeFunction = MeterUtil.createComposingFunction();
202
203         final RpcResult<AddMetersBatchOutput> addGroupBatchOutput = createAddMetersBatchFailureOutcome();
204         final RpcResult<Void> barrierOutput = createBarrierFailureOutcome();
205         final Pair<RpcResult<AddMetersBatchOutput>, RpcResult<Void>> input =
206                 Pair.of(addGroupBatchOutput, barrierOutput);
207         final RpcResult<AddMetersBatchOutput> composite = compositeFunction.apply(input);
208
209         Assert.assertFalse(composite.isSuccessful());
210         Assert.assertEquals(2, composite.getErrors().size());
211         Assert.assertEquals(1, composite.getResult().getBatchFailedMetersOutput().size());
212     }
213
214     private RpcResult<Void> createBarrierFailureOutcome() {
215         return RpcResultBuilder.<Void>failed()
216                 .withError(RpcError.ErrorType.APPLICATION, "ut-barrier-error")
217                 .build();
218     }
219
220     private RpcResult<AddMetersBatchOutput> createAddMetersBatchSuccessOutput() {
221         return RpcResultBuilder
222                 .success(new AddMetersBatchOutputBuilder()
223                         .setBatchFailedMetersOutput(Collections.<BatchFailedMetersOutput>emptyList())
224                         .build())
225                 .build();
226     }
227
228     private RpcResult<AddMetersBatchOutput> createAddMetersBatchFailureOutcome() {
229         final RpcResult<List<BatchFailedMetersOutput>> batchOutcomeWithError = createBatchOutcomeWithError();
230         return RpcResultBuilder.<AddMetersBatchOutput>failed()
231                 .withResult(new AddMetersBatchOutputBuilder()
232                         .setBatchFailedMetersOutput(batchOutcomeWithError.getResult())
233                         .build())
234                 .withRpcErrors(batchOutcomeWithError.getErrors())
235                 .build();
236     }
237 }