Update MRI projects for Aluminium
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / FlatBatchUtil.java
index 8bd950cff20db5d2953346b11f69986ad0ab8ea0..292af59b732a4ada82d12f03405d240a003d67d9 100644 (file)
@@ -12,7 +12,9 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Function;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.EnumSet;
 import java.util.List;
 import org.opendaylight.openflowplugin.impl.services.batch.BatchPlanStep;
@@ -36,16 +38,12 @@ import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
- * provides flat batch util methods
+ * Provides flat batch util methods.
  */
 public final class FlatBatchUtil {
 
-    private static final Logger LOG = LoggerFactory.getLogger(FlatBatchUtil.class);
-
     private FlatBatchUtil() {
         throw new IllegalStateException("This class should not be instantiated.");
     }
@@ -67,34 +65,36 @@ public final class FlatBatchUtil {
 
     @VisibleForTesting
     static boolean decideBarrier(final EnumSet<BatchStepType> previousTypes, final BatchStepType type) {
-        final boolean needBarrier;
-        switch (type) {
-            case FLOW_ADD:
-            case FLOW_UPDATE:
-                needBarrier = previousTypes.contains(BatchStepType.GROUP_ADD)
-                        || previousTypes.contains(BatchStepType.METER_ADD);
-                break;
-            case GROUP_ADD:
-                needBarrier = previousTypes.contains(BatchStepType.GROUP_ADD)
-                        || previousTypes.contains(BatchStepType.GROUP_UPDATE);
-                break;
-            case GROUP_REMOVE:
-                needBarrier = previousTypes.contains(BatchStepType.FLOW_REMOVE)
-                        || previousTypes.contains(BatchStepType.FLOW_UPDATE)
-                        || previousTypes.contains(BatchStepType.GROUP_REMOVE)
-                        || previousTypes.contains(BatchStepType.GROUP_UPDATE);
-                break;
-            case METER_REMOVE:
-                needBarrier = previousTypes.contains(BatchStepType.FLOW_REMOVE)
-                        || previousTypes.contains(BatchStepType.FLOW_UPDATE);
-                break;
-            default:
-                needBarrier = false;
-        }
-        return needBarrier;
+        return isFlowBarrierNeeded(previousTypes, type)
+                || isGroupBarrierNeeded(previousTypes, type)
+                || isMeterBarrierNeeded(previousTypes, type);
     }
 
-    public static List<BatchPlanStep> assembleBatchPlan(List<Batch> batches) {
+    private static boolean isFlowBarrierNeeded(final EnumSet<BatchStepType> previousTypes, final BatchStepType type) {
+        return (type == BatchStepType.FLOW_ADD
+                || type == BatchStepType.FLOW_UPDATE)
+                && (previousTypes.contains(BatchStepType.GROUP_ADD)
+                || previousTypes.contains(BatchStepType.METER_ADD));
+    }
+
+    private static boolean isGroupBarrierNeeded(final EnumSet<BatchStepType> previousTypes, final BatchStepType type) {
+        return type == BatchStepType.GROUP_ADD
+                && (previousTypes.contains(BatchStepType.GROUP_ADD)
+                || previousTypes.contains(BatchStepType.GROUP_UPDATE))
+                || type == BatchStepType.GROUP_REMOVE
+                && (previousTypes.contains(BatchStepType.FLOW_REMOVE)
+                || previousTypes.contains(BatchStepType.FLOW_UPDATE)
+                || previousTypes.contains(BatchStepType.GROUP_REMOVE)
+                || previousTypes.contains(BatchStepType.GROUP_UPDATE));
+    }
+
+    private static boolean isMeterBarrierNeeded(final EnumSet<BatchStepType> previousTypes, final BatchStepType type) {
+        return type == BatchStepType.METER_REMOVE
+                && (previousTypes.contains(BatchStepType.FLOW_REMOVE)
+                || previousTypes.contains(BatchStepType.FLOW_UPDATE));
+    }
+
+    public static List<BatchPlanStep> assembleBatchPlan(final Collection<Batch> batches) {
         final List<BatchPlanStep> plan = new ArrayList<>();
 
         BatchPlanStep planStep;
@@ -111,36 +111,36 @@ public final class FlatBatchUtil {
         return plan;
     }
 
-    private static List<? extends BatchOrderGrouping> extractBatchData(final BatchStepType batchStepType,
+    private static Collection<? extends BatchOrderGrouping> extractBatchData(final BatchStepType batchStepType,
                                                                        final BatchChoice batchChoice) {
-        final List<? extends BatchOrderGrouping> batchData;
+        final Collection<? extends BatchOrderGrouping> batchData;
         switch (batchStepType) {
             case FLOW_ADD:
-                batchData = ((FlatBatchAddFlowCase) batchChoice).getFlatBatchAddFlow();
+                batchData = ((FlatBatchAddFlowCase) batchChoice).nonnullFlatBatchAddFlow().values();
                 break;
             case FLOW_REMOVE:
-                batchData = ((FlatBatchRemoveFlowCase) batchChoice).getFlatBatchRemoveFlow();
+                batchData = ((FlatBatchRemoveFlowCase) batchChoice).nonnullFlatBatchRemoveFlow().values();
                 break;
             case FLOW_UPDATE:
-                batchData = ((FlatBatchUpdateFlowCase) batchChoice).getFlatBatchUpdateFlow();
+                batchData = ((FlatBatchUpdateFlowCase) batchChoice).nonnullFlatBatchUpdateFlow().values();
                 break;
             case GROUP_ADD:
-                batchData = ((FlatBatchAddGroupCase) batchChoice).getFlatBatchAddGroup();
+                batchData = ((FlatBatchAddGroupCase) batchChoice).nonnullFlatBatchAddGroup().values();
                 break;
             case GROUP_REMOVE:
-                batchData = ((FlatBatchRemoveGroupCase) batchChoice).getFlatBatchRemoveGroup();
+                batchData = ((FlatBatchRemoveGroupCase) batchChoice).nonnullFlatBatchRemoveGroup().values();
                 break;
             case GROUP_UPDATE:
-                batchData = ((FlatBatchUpdateGroupCase) batchChoice).getFlatBatchUpdateGroup();
+                batchData = ((FlatBatchUpdateGroupCase) batchChoice).nonnullFlatBatchUpdateGroup().values();
                 break;
             case METER_ADD:
-                batchData = ((FlatBatchAddMeterCase) batchChoice).getFlatBatchAddMeter();
+                batchData = ((FlatBatchAddMeterCase) batchChoice).nonnullFlatBatchAddMeter().values();
                 break;
             case METER_REMOVE:
-                batchData = ((FlatBatchRemoveMeterCase) batchChoice).getFlatBatchRemoveMeter();
+                batchData = ((FlatBatchRemoveMeterCase) batchChoice).nonnullFlatBatchRemoveMeter().values();
                 break;
             case METER_UPDATE:
-                batchData = ((FlatBatchUpdateMeterCase) batchChoice).getFlatBatchUpdateMeter();
+                batchData = ((FlatBatchUpdateMeterCase) batchChoice).nonnullFlatBatchUpdateMeter().values();
                 break;
             default:
                 throw new IllegalArgumentException("Unsupported batch step type obtained: " + batchStepType);
@@ -151,8 +151,9 @@ public final class FlatBatchUtil {
     @VisibleForTesting
     static <T extends BatchChoice> BatchStepType detectBatchStepType(final T batchCase) {
         final BatchStepType type;
-        final Class<? extends DataContainer> implementedInterface = batchCase.getImplementedInterface();
+        final Class<? extends DataContainer> implementedInterface = batchCase.implementedInterface();
 
+        // FIXME: use a lookup table instead of this cascade
         if (FlatBatchAddFlowCase.class.equals(implementedInterface)) {
             type = BatchStepType.FLOW_ADD;
         } else if (FlatBatchRemoveFlowCase.class.equals(implementedInterface)) {
@@ -177,9 +178,6 @@ public final class FlatBatchUtil {
         return type;
     }
 
-    /**
-     * @return RPC result incorporating partial results (state, errors, batch failures)
-     */
     @VisibleForTesting
     static Function<List<RpcResult<ProcessFlatBatchOutput>>, RpcResult<ProcessFlatBatchOutput>> mergeRpcResults() {
         return jobsResults -> {
@@ -189,9 +187,9 @@ public final class FlatBatchUtil {
 
             for (RpcResult<ProcessFlatBatchOutput> jobResult : jobsResults) {
                 if (jobResult != null) {
-                    isSuccessful = (isSuccessful && jobResult.isSuccessful());
+                    isSuccessful = isSuccessful && jobResult.isSuccessful();
                     rpcErrors.addAll(jobResult.getErrors());
-                    batchFailures.addAll(jobResult.getResult().getBatchFailure());
+                    batchFailures.addAll(jobResult.getResult().nonnullBatchFailure().values());
                 }
             }
 
@@ -204,14 +202,28 @@ public final class FlatBatchUtil {
 
     /**
      * Merge list of Futures with partial results into one ListenableFuture with single result.
-     * shortcut for {@link #mergeRpcResults()}
      * @param firedJobs list of ListenableFutures with RPC results {@link ProcessFlatBatchOutput}
      * @return ListenableFuture of RPC result with combined status and all errors + batch failures
      */
     public static ListenableFuture<RpcResult<ProcessFlatBatchOutput>> mergeJobsResultsFutures(
             final List<ListenableFuture<RpcResult<ProcessFlatBatchOutput>>> firedJobs) {
-        return Futures.transform(Futures.successfulAsList(firedJobs), mergeRpcResults());
+        return Futures.transform(Futures.successfulAsList(firedJobs),
+                mergeRpcResults(),
+                MoreExecutors.directExecutor());
     }
 
-
+    /**
+     * Creates empty result future for flat batch service.
+     * @param status RPC result status
+     * @return ListenableFuture of RPC result with empty list of errors and batch failures
+     */
+    public static ListenableFuture<RpcResult<ProcessFlatBatchOutput>> createEmptyRpcBatchResultFuture(
+            final boolean status) {
+        return RpcResultBuilder.<ProcessFlatBatchOutput>status(status)
+                               .withRpcErrors(new ArrayList<>())
+                               .withResult(new ProcessFlatBatchOutputBuilder()
+                                       .setBatchFailure(new ArrayList<>())
+                                       .build())
+                               .buildFuture();
+    }
 }