Merge "Sonar - technical debt of FRS app"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / FlowUtil.java
index 931532e41e5e7c404619ff8aa5a12e095194524f..616256174bc1ac0d1d80a6abbf56ea426de61ce2 100644 (file)
@@ -16,7 +16,6 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicInteger;
 import javax.annotation.Nullable;
 import org.apache.commons.lang3.tuple.Pair;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
@@ -42,17 +41,9 @@ import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 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;
 
-/**
- * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
- */
 public final class FlowUtil {
 
-    private static final String ALIEN_SYSTEM_FLOW_ID = "#UF$TABLE*";
-    private static final AtomicInteger unaccountedFlowsCounter = new AtomicInteger(0);
-    private static final Logger LOG = LoggerFactory.getLogger(FlowUtil.class);
     private static final RpcResultBuilder<List<BatchFailedFlowsOutput>> SUCCESSFUL_FLOW_OUTPUT_RPC_RESULT =
             RpcResultBuilder.success(Collections.<BatchFailedFlowsOutput>emptyList());
 
@@ -151,20 +142,6 @@ public final class FlowUtil {
         return resultBld;
     }
 
-    /**
-     * Create alien flow id
-     *
-     * @param tableId the table id
-     * @return the flow id
-     */
-    public static FlowId createAlienFlowId(final short tableId) {
-        final StringBuilder sBuilder = new StringBuilder(ALIEN_SYSTEM_FLOW_ID)
-                .append(tableId).append('-').append(unaccountedFlowsCounter.incrementAndGet());
-        String alienId = sBuilder.toString();
-        return new FlowId(alienId);
-
-    }
-
     /**
      * Factory method: create {@link Function} which attaches barrier response to given {@link RpcResult}&lt;T&gt;
      * and changes success flag if needed.
@@ -227,43 +204,55 @@ public final class FlowUtil {
      */
     public static <O> Function<List<RpcResult<O>>, RpcResult<List<BatchFailedFlowsOutput>>> createCumulatingFunction(
             final List<? extends BatchFlowIdGrouping> inputBatchFlows) {
-        return new Function<List<RpcResult<O>>, RpcResult<List<BatchFailedFlowsOutput>>>() {
-            @Nullable
-            @Override
-            public RpcResult<List<BatchFailedFlowsOutput>> apply(@Nullable final List<RpcResult<O>> innerInput) {
-                final int sizeOfFutures = innerInput.size();
-                final int sizeOfInputBatch = inputBatchFlows.size();
-                Preconditions.checkArgument(sizeOfFutures == sizeOfInputBatch,
-                        "wrong amount of returned futures: {} <> {}", sizeOfFutures, sizeOfInputBatch);
+        return new CumulatingFunction<O>(inputBatchFlows).invoke();
+    }
 
-                final ArrayList<BatchFailedFlowsOutput> batchFlows = new ArrayList<>(sizeOfFutures);
-                final Iterator<? extends BatchFlowIdGrouping> batchFlowIterator = inputBatchFlows.iterator();
+    private static class CumulatingFunction<O> {
+        private final List<? extends BatchFlowIdGrouping> inputBatchFlows;
 
-                Collection<RpcError> flowErrors = new ArrayList<>(sizeOfFutures);
+        public CumulatingFunction(List<? extends BatchFlowIdGrouping> inputBatchFlows) {
+            this.inputBatchFlows = inputBatchFlows;
+        }
 
-                int batchOrder = 0;
-                for (RpcResult<O> flowModOutput : innerInput) {
-                    final FlowId flowId = batchFlowIterator.next().getFlowId();
+        public Function<List<RpcResult<O>>, RpcResult<List<BatchFailedFlowsOutput>>> invoke() {
+            return new Function<List<RpcResult<O>>, RpcResult<List<BatchFailedFlowsOutput>>>() {
+                @Nullable
+                @Override
+                public RpcResult<List<BatchFailedFlowsOutput>> apply(@Nullable final List<RpcResult<O>> innerInput) {
+                    final int sizeOfFutures = innerInput.size();
+                    final int sizeOfInputBatch = inputBatchFlows.size();
+                    Preconditions.checkArgument(sizeOfFutures == sizeOfInputBatch,
+                            "wrong amount of returned futures: {} <> {}", sizeOfFutures, sizeOfInputBatch);
+
+                    final ArrayList<BatchFailedFlowsOutput> batchFlows = new ArrayList<>(sizeOfFutures);
+                    final Iterator<? extends BatchFlowIdGrouping> batchFlowIterator = inputBatchFlows.iterator();
+
+                    Collection<RpcError> flowErrors = new ArrayList<>(sizeOfFutures);
 
-                    if (!flowModOutput.isSuccessful()) {
-                        batchFlows.add(new BatchFailedFlowsOutputBuilder()
-                                .setFlowId(flowId)
-                                .setBatchOrder(batchOrder)
-                                .build());
-                        flowErrors.addAll(flowModOutput.getErrors());
+                    int batchOrder = 0;
+                    for (RpcResult<O> flowModOutput : innerInput) {
+                        final FlowId flowId = batchFlowIterator.next().getFlowId();
+
+                        if (!flowModOutput.isSuccessful()) {
+                            batchFlows.add(new BatchFailedFlowsOutputBuilder()
+                                    .setFlowId(flowId)
+                                    .setBatchOrder(batchOrder)
+                                    .build());
+                            flowErrors.addAll(flowModOutput.getErrors());
+                        }
+                        batchOrder++;
                     }
-                    batchOrder++;
-                }
 
-                final RpcResultBuilder<List<BatchFailedFlowsOutput>> resultBuilder;
-                if (!flowErrors.isEmpty()) {
-                    resultBuilder = RpcResultBuilder.<List<BatchFailedFlowsOutput>>failed()
-                            .withRpcErrors(flowErrors).withResult(batchFlows);
-                } else {
-                    resultBuilder = SUCCESSFUL_FLOW_OUTPUT_RPC_RESULT;
+                    final RpcResultBuilder<List<BatchFailedFlowsOutput>> resultBuilder;
+                    if (!flowErrors.isEmpty()) {
+                        resultBuilder = RpcResultBuilder.<List<BatchFailedFlowsOutput>>failed()
+                                .withRpcErrors(flowErrors).withResult(batchFlows);
+                    } else {
+                        resultBuilder = SUCCESSFUL_FLOW_OUTPUT_RPC_RESULT;
+                    }
+                    return resultBuilder.build();
                 }
-                return resultBuilder.build();
-            }
-        };
+            };
+        }
     }
 }