OPNFLWPLUG-930 Inconsistent flow IDs between flows
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / MeterUtil.java
index d224c702792fbec2c9abaee9a92c0c954a78ecb4..78c60721088e2e72daaed840d15f76df5dc184a7 100644 (file)
@@ -41,68 +41,81 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 
 /**
- * provides meter util methods
+ * Provides meter util methods.
  */
 public final class MeterUtil {
 
     private static final RpcResultBuilder<List<BatchFailedMetersOutput>> SUCCESSFUL_METER_OUTPUT_RPC_RESULT =
             RpcResultBuilder.success(Collections.<BatchFailedMetersOutput>emptyList());
 
-    public static final Function<RpcResult<List<BatchFailedMetersOutput>>, RpcResult<AddMetersBatchOutput>> METER_ADD_TRANSFORM =
-            new Function<RpcResult<List<BatchFailedMetersOutput>>, RpcResult<AddMetersBatchOutput>>() {
-                @Nullable
-                @Override
-                public RpcResult<AddMetersBatchOutput> apply(@Nullable final RpcResult<List<BatchFailedMetersOutput>> batchMetersCumulatedResult) {
-                    final AddMetersBatchOutput batchOutput = new AddMetersBatchOutputBuilder()
-                            .setBatchFailedMetersOutput(batchMetersCumulatedResult.getResult()).build();
-
-                    final RpcResultBuilder<AddMetersBatchOutput> resultBld =
-                            createCumulativeRpcResult(batchMetersCumulatedResult, batchOutput);
-                    return resultBld.build();
-                }
-            };
-    public static final Function<Pair<RpcResult<AddMetersBatchOutput>, RpcResult<Void>>, RpcResult<AddMetersBatchOutput>>
-            METER_ADD_COMPOSING_TRANSFORM = createComposingFunction();
-
-    public static final Function<RpcResult<List<BatchFailedMetersOutput>>, RpcResult<RemoveMetersBatchOutput>> METER_REMOVE_TRANSFORM =
-            new Function<RpcResult<List<BatchFailedMetersOutput>>, RpcResult<RemoveMetersBatchOutput>>() {
-                @Nullable
-                @Override
-                public RpcResult<RemoveMetersBatchOutput> apply(@Nullable final RpcResult<List<BatchFailedMetersOutput>> batchMetersCumulatedResult) {
-                    final RemoveMetersBatchOutput batchOutput = new RemoveMetersBatchOutputBuilder()
-                            .setBatchFailedMetersOutput(batchMetersCumulatedResult.getResult()).build();
+    public static final Function<RpcResult<List<BatchFailedMetersOutput>>, RpcResult<AddMetersBatchOutput>>
+        METER_ADD_TRANSFORM =
+        new Function<RpcResult<List<BatchFailedMetersOutput>>, RpcResult<AddMetersBatchOutput>>() {
+            @Nullable
+            @Override
+            public RpcResult<AddMetersBatchOutput> apply(
+                    @Nullable final RpcResult<List<BatchFailedMetersOutput>> batchMetersCumulatedResult) {
+                final AddMetersBatchOutput batchOutput = new AddMetersBatchOutputBuilder()
+                        .setBatchFailedMetersOutput(batchMetersCumulatedResult.getResult()).build();
 
-                    final RpcResultBuilder<RemoveMetersBatchOutput> resultBld =
-                            createCumulativeRpcResult(batchMetersCumulatedResult, batchOutput);
-                    return resultBld.build();
-                }
-            };
-    public static final Function<Pair<RpcResult<RemoveMetersBatchOutput>, RpcResult<Void>>, RpcResult<RemoveMetersBatchOutput>>
-            METER_REMOVE_COMPOSING_TRANSFORM = createComposingFunction();
+                final RpcResultBuilder<AddMetersBatchOutput> resultBld =
+                        createCumulativeRpcResult(batchMetersCumulatedResult, batchOutput);
+                return resultBld.build();
+            }
+        };
+    public static final Function<Pair<RpcResult<AddMetersBatchOutput>,
+                                      RpcResult<Void>>,
+                                      RpcResult<AddMetersBatchOutput>>
+        METER_ADD_COMPOSING_TRANSFORM = createComposingFunction();
+
+    public static final Function<RpcResult<List<BatchFailedMetersOutput>>, RpcResult<RemoveMetersBatchOutput>>
+        METER_REMOVE_TRANSFORM =
+        new Function<RpcResult<List<BatchFailedMetersOutput>>, RpcResult<RemoveMetersBatchOutput>>() {
+            @Nullable
+            @Override
+            public RpcResult<RemoveMetersBatchOutput> apply(
+                    @Nullable final RpcResult<List<BatchFailedMetersOutput>> batchMetersCumulatedResult) {
+                final RemoveMetersBatchOutput batchOutput = new RemoveMetersBatchOutputBuilder()
+                        .setBatchFailedMetersOutput(batchMetersCumulatedResult.getResult()).build();
 
-    public static final Function<RpcResult<List<BatchFailedMetersOutput>>, RpcResult<UpdateMetersBatchOutput>> METER_UPDATE_TRANSFORM =
-            new Function<RpcResult<List<BatchFailedMetersOutput>>, RpcResult<UpdateMetersBatchOutput>>() {
-                @Nullable
-                @Override
-                public RpcResult<UpdateMetersBatchOutput> apply(@Nullable final RpcResult<List<BatchFailedMetersOutput>> batchMetersCumulatedResult) {
-                    final UpdateMetersBatchOutput batchOutput = new UpdateMetersBatchOutputBuilder()
-                            .setBatchFailedMetersOutput(batchMetersCumulatedResult.getResult()).build();
+                final RpcResultBuilder<RemoveMetersBatchOutput> resultBld =
+                        createCumulativeRpcResult(batchMetersCumulatedResult, batchOutput);
+                return resultBld.build();
+            }
+        };
+    public static final Function<Pair<RpcResult<RemoveMetersBatchOutput>,
+                                      RpcResult<Void>>,
+                                      RpcResult<RemoveMetersBatchOutput>>
+        METER_REMOVE_COMPOSING_TRANSFORM = createComposingFunction();
+
+    public static final Function<RpcResult<List<BatchFailedMetersOutput>>, RpcResult<UpdateMetersBatchOutput>>
+        METER_UPDATE_TRANSFORM =
+        new Function<RpcResult<List<BatchFailedMetersOutput>>, RpcResult<UpdateMetersBatchOutput>>() {
+            @Nullable
+            @Override
+            public RpcResult<UpdateMetersBatchOutput> apply(
+                    @Nullable final RpcResult<List<BatchFailedMetersOutput>> batchMetersCumulatedResult) {
+                final UpdateMetersBatchOutput batchOutput = new UpdateMetersBatchOutputBuilder()
+                        .setBatchFailedMetersOutput(batchMetersCumulatedResult.getResult()).build();
 
-                    final RpcResultBuilder<UpdateMetersBatchOutput> resultBld =
-                            createCumulativeRpcResult(batchMetersCumulatedResult, batchOutput);
-                    return resultBld.build();
-                }
-            };
-    public static final Function<Pair<RpcResult<UpdateMetersBatchOutput>, RpcResult<Void>>, RpcResult<UpdateMetersBatchOutput>>
-            METER_UPDATE_COMPOSING_TRANSFORM = createComposingFunction();
+                final RpcResultBuilder<UpdateMetersBatchOutput> resultBld =
+                        createCumulativeRpcResult(batchMetersCumulatedResult, batchOutput);
+                return resultBld.build();
+            }
+        };
+    public static final Function<Pair<RpcResult<UpdateMetersBatchOutput>,
+                                      RpcResult<Void>>,
+                                      RpcResult<UpdateMetersBatchOutput>>
+        METER_UPDATE_COMPOSING_TRANSFORM = createComposingFunction();
 
     private MeterUtil() {
         throw new IllegalStateException("This class should not be instantiated.");
     }
 
     /**
-     * @param nodePath
-     * @param meterId
+     * Create meter path.
+     * @param nodePath node path
+     * @param meterId meter Id
      * @return instance identifier assembled for given node and meter
      */
     public static MeterRef buildMeterPath(final InstanceIdentifier<Node> nodePath, final MeterId meterId) {
@@ -114,51 +127,16 @@ public final class MeterUtil {
     }
 
     public static <O> Function<List<RpcResult<O>>, RpcResult<List<BatchFailedMetersOutput>>> createCumulativeFunction(
-            final Iterable<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter> inputBatchMeters) {
+            final Iterable<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter>
+                    inputBatchMeters) {
         return createCumulativeFunction(inputBatchMeters, Iterables.size(inputBatchMeters));
     }
 
     public static <O> Function<List<RpcResult<O>>, RpcResult<List<BatchFailedMetersOutput>>> createCumulativeFunction(
-            final Iterable<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter> inputBatchMeters,
+            final Iterable<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter>
+                    inputBatchMeters,
             final int sizeOfInputBatch) {
-        return new Function<List<RpcResult<O>>, RpcResult<List<BatchFailedMetersOutput>>>() {
-            @Nullable
-            @Override
-            public RpcResult<List<BatchFailedMetersOutput>> apply(@Nullable final List<RpcResult<O>> innerInput) {
-                final int sizeOfFutures = innerInput.size();
-                Preconditions.checkArgument(sizeOfFutures == sizeOfInputBatch,
-                        "wrong amount of returned futures: {} <> {}", sizeOfFutures, sizeOfInputBatch);
-
-                final List<BatchFailedMetersOutput> batchMeters = new ArrayList<>();
-                final Iterator<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter>
-                        batchMeterIterator = inputBatchMeters.iterator();
-
-                Collection<RpcError> meterErrors = new ArrayList<>(sizeOfFutures);
-
-                int batchOrder = 0;
-                for (RpcResult<O> meterModOutput : innerInput) {
-                    final MeterId meterId = batchMeterIterator.next().getMeterId();
-
-                    if (!meterModOutput.isSuccessful()) {
-                        batchMeters.add(new BatchFailedMetersOutputBuilder()
-                                .setBatchOrder(batchOrder)
-                                .setMeterId(meterId)
-                                .build());
-                        meterErrors.addAll(meterModOutput.getErrors());
-                    }
-                    batchOrder++;
-                }
-
-                final RpcResultBuilder<List<BatchFailedMetersOutput>> resultBuilder;
-                if (!meterErrors.isEmpty()) {
-                    resultBuilder = RpcResultBuilder.<List<BatchFailedMetersOutput>>failed()
-                            .withRpcErrors(meterErrors).withResult(batchMeters);
-                } else {
-                    resultBuilder = SUCCESSFUL_METER_OUTPUT_RPC_RESULT;
-                }
-                return resultBuilder.build();
-            }
-        };
+        return new CumulativeFunction<O>(inputBatchMeters, sizeOfInputBatch).invoke();
     }
 
     /**
@@ -172,7 +150,7 @@ public final class MeterUtil {
      */
     @VisibleForTesting
     static <T extends BatchMeterOutputListGrouping>
-    Function<Pair<RpcResult<T>, RpcResult<Void>>, RpcResult<T>> createComposingFunction() {
+        Function<Pair<RpcResult<T>, RpcResult<Void>>, RpcResult<T>> createComposingFunction() {
         return new Function<Pair<RpcResult<T>, RpcResult<Void>>, RpcResult<T>>() {
             @Nullable
             @Override
@@ -199,13 +177,13 @@ public final class MeterUtil {
      * Wrap given list of problematic group-ids into {@link RpcResult} of given type.
      *
      * @param batchMetersCumulativeResult list of ids failed groups
-     * @param batchOutput
-     * @param <T>                         group operation type
+     * @param batchOutput group operation type
      * @return batch group operation output of given type containing list of group-ids and corresponding success flag
      */
     private static <T extends BatchMeterOutputListGrouping>
-    RpcResultBuilder<T> createCumulativeRpcResult(final @Nullable RpcResult<List<BatchFailedMetersOutput>> batchMetersCumulativeResult,
-                                                  final T batchOutput) {
+        RpcResultBuilder<T> createCumulativeRpcResult(
+            final @Nullable RpcResult<List<BatchFailedMetersOutput>> batchMetersCumulativeResult,
+            final T batchOutput) {
         final RpcResultBuilder<T> resultBld;
         if (batchMetersCumulativeResult.isSuccessful()) {
             resultBld = RpcResultBuilder.success(batchOutput);
@@ -216,4 +194,58 @@ public final class MeterUtil {
         }
         return resultBld;
     }
+
+    private static class CumulativeFunction<O> {
+        private final Iterable<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter>
+                inputBatchMeters;
+        private final int sizeOfInputBatch;
+
+        CumulativeFunction(
+                Iterable<? extends org.opendaylight.yang.gen.v1.urn
+                        .opendaylight.meter.types.rev130918.Meter> inputBatchMeters, int sizeOfInputBatch) {
+            this.inputBatchMeters = inputBatchMeters;
+            this.sizeOfInputBatch = sizeOfInputBatch;
+        }
+
+        public Function<List<RpcResult<O>>, RpcResult<List<BatchFailedMetersOutput>>> invoke() {
+            return new Function<List<RpcResult<O>>, RpcResult<List<BatchFailedMetersOutput>>>() {
+                @Nullable
+                @Override
+                public RpcResult<List<BatchFailedMetersOutput>> apply(@Nullable final List<RpcResult<O>> innerInput) {
+                    final int sizeOfFutures = innerInput.size();
+                    Preconditions.checkArgument(sizeOfFutures == sizeOfInputBatch,
+                            "wrong amount of returned futures: {} <> {}", sizeOfFutures, sizeOfInputBatch);
+
+                    final List<BatchFailedMetersOutput> batchMeters = new ArrayList<>();
+                    final Iterator<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter>
+                            batchMeterIterator = inputBatchMeters.iterator();
+
+                    Collection<RpcError> meterErrors = new ArrayList<>(sizeOfFutures);
+
+                    int batchOrder = 0;
+                    for (RpcResult<O> meterModOutput : innerInput) {
+                        final MeterId meterId = batchMeterIterator.next().getMeterId();
+
+                        if (!meterModOutput.isSuccessful()) {
+                            batchMeters.add(new BatchFailedMetersOutputBuilder()
+                                    .setBatchOrder(batchOrder)
+                                    .setMeterId(meterId)
+                                    .build());
+                            meterErrors.addAll(meterModOutput.getErrors());
+                        }
+                        batchOrder++;
+                    }
+
+                    final RpcResultBuilder<List<BatchFailedMetersOutput>> resultBuilder;
+                    if (!meterErrors.isEmpty()) {
+                        resultBuilder = RpcResultBuilder.<List<BatchFailedMetersOutput>>failed()
+                                .withRpcErrors(meterErrors).withResult(batchMeters);
+                    } else {
+                        resultBuilder = SUCCESSFUL_METER_OUTPUT_RPC_RESULT;
+                    }
+                    return resultBuilder.build();
+                }
+            };
+        }
+    }
 }