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.util;
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;
37 * Test for {@link MeterUtil}.
39 public class MeterUtilTest {
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);
46 public void testBuildGroupPath() throws Exception {
47 final InstanceIdentifier<Node> nodePath = InstanceIdentifier
49 .child(Node.class, new NodeKey(DUMMY_NODE_ID));
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());
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)));
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")
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());
76 private org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter createBatchMeter(
77 final MeterId meterId) {
78 return new MeterBuilder()
84 public void testMeterAddTransformFailure() throws Exception {
85 final RpcResult<List<BatchFailedMetersOutput>> input = createBatchOutcomeWithError();
86 checkBatchErrorOutcomeTransformation(MeterUtil.METER_ADD_TRANSFORM.apply(input));
90 public void testMeterAddTransformSuccess() throws Exception {
91 final RpcResult<List<BatchFailedMetersOutput>> input = createEmptyBatchOutcome();
92 checkBatchSuccessOutcomeTransformation(MeterUtil.METER_ADD_TRANSFORM.apply(input));
96 public void testMeterRemoveTransformFailure() throws Exception {
97 final RpcResult<List<BatchFailedMetersOutput>> input = createBatchOutcomeWithError();
98 checkBatchErrorOutcomeTransformation(MeterUtil.METER_REMOVE_TRANSFORM.apply(input));
102 public void testFlowRemoveTransformSuccess() throws Exception {
103 final RpcResult<List<BatchFailedMetersOutput>> input = createEmptyBatchOutcome();
104 checkBatchSuccessOutcomeTransformation(MeterUtil.METER_REMOVE_TRANSFORM.apply(input));
108 public void testFlowUpdateTransformFailure() throws Exception {
109 final RpcResult<List<BatchFailedMetersOutput>> input = createBatchOutcomeWithError();
110 checkBatchErrorOutcomeTransformation(MeterUtil.METER_UPDATE_TRANSFORM.apply(input));
114 public void testFlowUpdateTransformSuccess() throws Exception {
115 final RpcResult<List<BatchFailedMetersOutput>> input = createEmptyBatchOutcome();
116 checkBatchSuccessOutcomeTransformation(MeterUtil.METER_UPDATE_TRANSFORM.apply(input));
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());
126 private RpcResult<List<BatchFailedMetersOutput>> createEmptyBatchOutcome() {
127 return RpcResultBuilder
128 .<List<BatchFailedMetersOutput>>success(Collections.<BatchFailedMetersOutput>emptyList())
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)
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());
147 Assert.assertEquals(1, output.getErrors().size());
151 public void testCreateComposingFunction_success_success() throws Exception {
152 final Function<Pair<RpcResult<AddMetersBatchOutput>, RpcResult<Void>>, RpcResult<AddMetersBatchOutput>>
153 compositeFunction = MeterUtil.createComposingFunction();
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);
161 Assert.assertTrue(composite.isSuccessful());
162 Assert.assertEquals(0, composite.getErrors().size());
163 Assert.assertEquals(0, composite.getResult().getBatchFailedMetersOutput().size());
167 public void testCreateComposingFunction_failure_success() throws Exception {
168 final Function<Pair<RpcResult<AddMetersBatchOutput>, RpcResult<Void>>, RpcResult<AddMetersBatchOutput>>
169 compositeFunction = MeterUtil.createComposingFunction();
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);
177 Assert.assertFalse(composite.isSuccessful());
178 Assert.assertEquals(1, composite.getErrors().size());
179 Assert.assertEquals(1, composite.getResult().getBatchFailedMetersOutput().size());
183 public void testCreateComposingFunction_success_failure() throws Exception {
184 final Function<Pair<RpcResult<AddMetersBatchOutput>, RpcResult<Void>>, RpcResult<AddMetersBatchOutput>>
185 compositeFunction = MeterUtil.createComposingFunction();
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);
193 Assert.assertFalse(composite.isSuccessful());
194 Assert.assertEquals(1, composite.getErrors().size());
195 Assert.assertEquals(0, composite.getResult().getBatchFailedMetersOutput().size());
199 public void testCreateComposingFunction_failure_failure() throws Exception {
200 final Function<Pair<RpcResult<AddMetersBatchOutput>, RpcResult<Void>>, RpcResult<AddMetersBatchOutput>>
201 compositeFunction = MeterUtil.createComposingFunction();
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);
209 Assert.assertFalse(composite.isSuccessful());
210 Assert.assertEquals(2, composite.getErrors().size());
211 Assert.assertEquals(1, composite.getResult().getBatchFailedMetersOutput().size());
214 private RpcResult<Void> createBarrierFailureOutcome() {
215 return RpcResultBuilder.<Void>failed()
216 .withError(RpcError.ErrorType.APPLICATION, "ut-barrier-error")
220 private RpcResult<AddMetersBatchOutput> createAddMetersBatchSuccessOutput() {
221 return RpcResultBuilder
222 .success(new AddMetersBatchOutputBuilder()
223 .setBatchFailedMetersOutput(Collections.<BatchFailedMetersOutput>emptyList())
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())
234 .withRpcErrors(batchOutcomeWithError.getErrors())