Bump odlparent to 5.0.0
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / batch / FlatBatchFlowAdapters.java
index e110703a33a5e66ee475552925182b70752619de..9c634b30413b9fc9b7912e22a46947dcfa019354 100644 (file)
@@ -13,11 +13,10 @@ import com.google.common.base.Function;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.JdkFutureAdapters;
 import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.Future;
-import javax.annotation.Nullable;
-import org.opendaylight.openflowplugin.impl.util.FlatBatchUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.flow._case.FlatBatchAddFlow;
@@ -46,18 +45,19 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 
 /**
- * transform between FlatBatch API and flow batch API
+ * Transform between FlatBatch API and flow batch API.
  */
-public class FlatBatchFlowAdapters {
+public final class FlatBatchFlowAdapters {
 
     private FlatBatchFlowAdapters() {
-        throw new IllegalStateException("This class should not be instantiated.");
     }
 
     /**
+     * Adapt flat batch add flow.
      * @param planStep batch step containing changes of the same type
      * @param node     pointer for RPC routing
-     * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.SalFlowsBatchService#addFlowsBatch(AddFlowsBatchInput)}
+     * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
+     * .opendaylight.flows.service.rev160314.SalFlowsBatchService#addFlowsBatch(AddFlowsBatchInput)}
      */
     public static AddFlowsBatchInput adaptFlatBatchAddFlow(final BatchPlanStep planStep, final NodeRef node) {
         final List<BatchAddFlows> batchFlows = new ArrayList<>();
@@ -76,9 +76,11 @@ public class FlatBatchFlowAdapters {
     }
 
     /**
+     * Adapt flat batch remove flow.
      * @param planStep batch step containing changes of the same type
      * @param node     pointer for RPC routing
-     * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.SalFlowsBatchService#removeFlowsBatch(RemoveFlowsBatchInput)}
+     * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
+     * .opendaylight.flows.service.rev160314.SalFlowsBatchService#removeFlowsBatch(RemoveFlowsBatchInput)}
      */
     public static RemoveFlowsBatchInput adaptFlatBatchRemoveFlow(final BatchPlanStep planStep, final NodeRef node) {
         final List<BatchRemoveFlows> batchFlows = new ArrayList<>();
@@ -97,9 +99,11 @@ public class FlatBatchFlowAdapters {
     }
 
     /**
+     * Adapt flat batch update flow.
      * @param planStep batch step containing changes of the same type
      * @param node     pointer for RPC routing
-     * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.SalFlowsBatchService#updateFlowsBatch(UpdateFlowsBatchInput)}
+     * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
+     * .opendaylight.flows.service.rev160314.SalFlowsBatchService#updateFlowsBatch(UpdateFlowsBatchInput)}
      */
     public static UpdateFlowsBatchInput adaptFlatBatchUpdateFlow(final BatchPlanStep planStep, final NodeRef node) {
         final List<BatchUpdateFlows> batchFlows = new ArrayList<>();
@@ -117,30 +121,23 @@ public class FlatBatchFlowAdapters {
     }
 
     /**
-     * @param chainInput here all partial results are collected (values + errors)
+     * Convert batch result.
      * @param stepOffset offset of current batch plan step
-     * @return next chained result incorporating results of this step's batch
+     * @return converted {@link ProcessFlatBatchOutput} RPC result
      */
     @VisibleForTesting
     static <T extends BatchFlowOutputListGrouping> Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>
-    createBatchFlowChainingFunction(final RpcResult<ProcessFlatBatchOutput> chainInput,
-                                    final int stepOffset) {
+        convertBatchFlowResult(final int stepOffset) {
         return new Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>() {
-            @Nullable
             @Override
-            public RpcResult<ProcessFlatBatchOutput> apply(@Nullable final RpcResult<T> input) {
-                // create rpcResult builder honoring both success/failure of current input and chained input + join errors
-                final RpcResultBuilder<ProcessFlatBatchOutput> output = FlatBatchUtil.mergeRpcResults(chainInput, input);
-                // convert values and add to chain values
-                final ProcessFlatBatchOutputBuilder outputBuilder = new ProcessFlatBatchOutputBuilder(chainInput.getResult());
-                final List<BatchFailure> batchFailures = wrapBatchFlowFailuresForFlat(input, stepOffset);
-                // join values
-                if (outputBuilder.getBatchFailure() == null) {
-                    outputBuilder.setBatchFailure(new ArrayList<BatchFailure>(batchFailures.size()));
-                }
-                outputBuilder.getBatchFailure().addAll(batchFailures);
-
-                return output.withResult(outputBuilder.build()).build();
+            public RpcResult<ProcessFlatBatchOutput> apply(final RpcResult<T> input) {
+                List<BatchFailure> batchFailures = wrapBatchFlowFailuresForFlat(input, stepOffset);
+                ProcessFlatBatchOutputBuilder outputBuilder =
+                        new ProcessFlatBatchOutputBuilder().setBatchFailure(batchFailures);
+                return RpcResultBuilder.<ProcessFlatBatchOutput>status(input.isSuccessful())
+                                       .withRpcErrors(input.getErrors())
+                                       .withResult(outputBuilder.build())
+                                       .build();
             }
         };
     }
@@ -163,19 +160,18 @@ public class FlatBatchFlowAdapters {
     }
 
     /**
-     * shortcut for {@link #createBatchFlowChainingFunction(RpcResult, int)} with conversion {@link ListenableFuture}
+     * Shortcut for {@link #convertBatchFlowResult(int)} with conversion {@link ListenableFuture}.
      *
      * @param <T>                    exact type of batch flow output
-     * @param chainInput             here all partial results are collected (values + errors)
      * @param resultUpdateFlowFuture batch flow rpc-result (add/remove/update)
      * @param currentOffset          offset of current batch plan step with respect to entire chain of steps
-     * @return next chained result incorporating results of this step's batch
+     * @return ListenableFuture with converted result {@link ProcessFlatBatchOutput}
      */
     public static <T extends BatchFlowOutputListGrouping> ListenableFuture<RpcResult<ProcessFlatBatchOutput>>
-    adaptFlowBatchFutureForChain(final RpcResult<ProcessFlatBatchOutput> chainInput,
-                                 final Future<RpcResult<T>> resultUpdateFlowFuture,
-                                 final int currentOffset) {
+        convertFlowBatchFutureForChain(final Future<RpcResult<T>> resultUpdateFlowFuture,
+                                   final int currentOffset) {
         return Futures.transform(JdkFutureAdapters.listenInPoolThread(resultUpdateFlowFuture),
-                FlatBatchFlowAdapters.<T>createBatchFlowChainingFunction(chainInput, currentOffset));
+                FlatBatchFlowAdapters.convertBatchFlowResult(currentOffset),
+                MoreExecutors.directExecutor());
     }
 }