Bump upstreams
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / FlowUtil.java
index 931532e41e5e7c404619ff8aa5a12e095194524f..3c10e637adcd28f75c54200ab52b427fd7641efa 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.util;
 
 import com.google.common.annotations.VisibleForTesting;
@@ -13,11 +12,9 @@ import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
 import java.util.ArrayList;
 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 java.util.Map;
 import org.apache.commons.lang3.tuple.Pair;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
@@ -25,6 +22,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.ta
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchOutputBuilder;
@@ -38,108 +36,104 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.bat
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.output.list.grouping.BatchFailedFlowsOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.Key;
+import org.opendaylight.yangtools.yang.binding.KeyAware;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap;
 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;
+import org.opendaylight.yangtools.yang.common.Uint16;
+import org.opendaylight.yangtools.yang.common.Uint8;
 
-/**
- * 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());
+            RpcResultBuilder.success(List.of());
 
     /**
-     * Attach barrier response to given {@link RpcResult}&lt;RemoveFlowsBatchOutput&gt;
+     * Attach barrier response to given {@link RpcResult}&lt;RemoveFlowsBatchOutput&gt;.
      */
-    public static final Function<Pair<RpcResult<RemoveFlowsBatchOutput>, RpcResult<Void>>, RpcResult<RemoveFlowsBatchOutput>>
+    public static final Function<Pair<RpcResult<RemoveFlowsBatchOutput>,
+                                 RpcResult<SendBarrierOutput>>,
+                                 RpcResult<RemoveFlowsBatchOutput>>
             FLOW_REMOVE_COMPOSING_TRANSFORM = createComposingFunction();
 
     /**
-     * Attach barrier response to given {@link RpcResult}&lt;AddFlowsBatchOutput&gt;
+     * Attach barrier response to given {@link RpcResult}&lt;AddFlowsBatchOutput&gt;.
      */
-    public static final Function<Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<Void>>, RpcResult<AddFlowsBatchOutput>>
+    public static final Function<Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<SendBarrierOutput>>,
+            RpcResult<AddFlowsBatchOutput>>
             FLOW_ADD_COMPOSING_TRANSFORM = createComposingFunction();
 
     /**
-     * Attach barrier response to given {@link RpcResult}&lt;UpdateFlowsBatchOutput&gt;
+     * Attach barrier response to given {@link RpcResult}&lt;UpdateFlowsBatchOutput&gt;.
      */
-    public static final Function<Pair<RpcResult<UpdateFlowsBatchOutput>, RpcResult<Void>>, RpcResult<UpdateFlowsBatchOutput>>
+    public static final Function<Pair<RpcResult<UpdateFlowsBatchOutput>,
+                                 RpcResult<SendBarrierOutput>>,
+                                 RpcResult<UpdateFlowsBatchOutput>>
             FLOW_UPDATE_COMPOSING_TRANSFORM = createComposingFunction();
 
     /**
-     * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}
+     * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}.
      */
-    public static final Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<RemoveFlowsBatchOutput>> FLOW_REMOVE_TRANSFORM =
-            new Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<RemoveFlowsBatchOutput>>() {
-                @Nullable
-                @Override
-                public RpcResult<RemoveFlowsBatchOutput> apply(@Nullable final RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult) {
-                    final RemoveFlowsBatchOutput batchOutput = new RemoveFlowsBatchOutputBuilder()
-                            .setBatchFailedFlowsOutput(batchFlowsCumulativeResult.getResult()).build();
-
-                    final RpcResultBuilder<RemoveFlowsBatchOutput> resultBld =
-                            createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
-                    return resultBld.build();
-                }
+    public static final Function<RpcResult<List<BatchFailedFlowsOutput>>,
+        RpcResult<RemoveFlowsBatchOutput>> FLOW_REMOVE_TRANSFORM =
+            batchFlowsCumulativeResult -> {
+                final RemoveFlowsBatchOutput batchOutput = new RemoveFlowsBatchOutputBuilder()
+                        .setBatchFailedFlowsOutput(index(batchFlowsCumulativeResult.getResult())).build();
+
+                final RpcResultBuilder<RemoveFlowsBatchOutput> resultBld =
+                        createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
+                return resultBld.build();
             };
 
     /**
-     * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}
+     * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}.
      */
-    public static final Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<AddFlowsBatchOutput>> FLOW_ADD_TRANSFORM =
-            new Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<AddFlowsBatchOutput>>() {
-                @Nullable
-                @Override
-                public RpcResult<AddFlowsBatchOutput> apply(@Nullable final RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult) {
-                    final AddFlowsBatchOutput batchOutput = new AddFlowsBatchOutputBuilder()
-                            .setBatchFailedFlowsOutput(batchFlowsCumulativeResult.getResult()).build();
-
-                    final RpcResultBuilder<AddFlowsBatchOutput> resultBld =
-                            createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
-                    return resultBld.build();
-                }
+    public static final Function<RpcResult<List<BatchFailedFlowsOutput>>,
+        RpcResult<AddFlowsBatchOutput>> FLOW_ADD_TRANSFORM =
+            batchFlowsCumulativeResult -> {
+                final AddFlowsBatchOutput batchOutput = new AddFlowsBatchOutputBuilder()
+                        .setBatchFailedFlowsOutput(index(batchFlowsCumulativeResult.getResult())).build();
+
+                final RpcResultBuilder<AddFlowsBatchOutput> resultBld =
+                        createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
+                return resultBld.build();
             };
 
     /**
-     * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}
+     * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}.
      */
-    public static final Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<UpdateFlowsBatchOutput>> FLOW_UPDATE_TRANSFORM =
-            new Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<UpdateFlowsBatchOutput>>() {
-                @Nullable
-                @Override
-                public RpcResult<UpdateFlowsBatchOutput> apply(@Nullable final RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult) {
-                    final UpdateFlowsBatchOutput batchOutput = new UpdateFlowsBatchOutputBuilder()
-                            .setBatchFailedFlowsOutput(batchFlowsCumulativeResult.getResult()).build();
-
-                    final RpcResultBuilder<UpdateFlowsBatchOutput> resultBld =
-                            createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
-                    return resultBld.build();
-                }
+    public static final Function<RpcResult<List<BatchFailedFlowsOutput>>,
+        RpcResult<UpdateFlowsBatchOutput>> FLOW_UPDATE_TRANSFORM =
+            batchFlowsCumulativeResult -> {
+                final UpdateFlowsBatchOutput batchOutput = new UpdateFlowsBatchOutputBuilder()
+                        .setBatchFailedFlowsOutput(index(batchFlowsCumulativeResult.getResult())).build();
+
+                final RpcResultBuilder<UpdateFlowsBatchOutput> resultBld =
+                        createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
+                return resultBld.build();
             };
 
     private FlowUtil() {
-        throw new IllegalStateException("This class should not be instantiated.");
+        // Hidden on purpose
+    }
+
+    static <K extends Key<V>, V extends KeyAware<K>> Map<K, V> index(final List<V> list) {
+        return list == null ? null : BindingMap.ordered(list);
     }
 
     /**
      * Wrap given list of problematic flow-ids into {@link RpcResult} of given type.
      *
      * @param batchFlowsCumulativeResult list of ids failed flows
-     * @param batchOutput
-     * @param <T>                        flow operation type
+     * @param batchOutput flow operation type
      * @return batch flow operation output of given type containing list of flow-ids and corresponding success flag
      */
-    private static <T extends BatchFlowOutputListGrouping>
-    RpcResultBuilder<T> createCumulativeRpcResult(final @Nullable RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult,
-                                                  final T batchOutput) {
+    private static <T extends BatchFlowOutputListGrouping> RpcResultBuilder<T> createCumulativeRpcResult(
+            final RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult,
+            final T batchOutput) {
         final RpcResultBuilder<T> resultBld;
         if (batchFlowsCumulativeResult.isSuccessful()) {
             resultBld = RpcResultBuilder.success(batchOutput);
@@ -151,20 +145,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.
@@ -176,26 +156,22 @@ public final class FlowUtil {
      */
     @VisibleForTesting
     static <T extends BatchFlowOutputListGrouping>
-    Function<Pair<RpcResult<T>, RpcResult<Void>>, RpcResult<T>> createComposingFunction() {
-        return new Function<Pair<RpcResult<T>, RpcResult<Void>>, RpcResult<T>>() {
-            @Nullable
-            @Override
-            public RpcResult<T> apply(@Nullable final Pair<RpcResult<T>, RpcResult<Void>> input) {
-                final RpcResultBuilder<T> resultBld;
-                if (input.getLeft().isSuccessful() && input.getRight().isSuccessful()) {
-                    resultBld = RpcResultBuilder.success();
-                } else {
-                    resultBld = RpcResultBuilder.failed();
-                }
+        Function<Pair<RpcResult<T>, RpcResult<SendBarrierOutput>>, RpcResult<T>> createComposingFunction() {
+        return input -> {
+            final RpcResultBuilder<T> resultBld;
+            if (input.getLeft().isSuccessful() && input.getRight().isSuccessful()) {
+                resultBld = RpcResultBuilder.success();
+            } else {
+                resultBld = RpcResultBuilder.failed();
+            }
 
-                final ArrayList<RpcError> rpcErrors = new ArrayList<>(input.getLeft().getErrors());
-                rpcErrors.addAll(input.getRight().getErrors());
-                resultBld.withRpcErrors(rpcErrors);
+            final ArrayList<RpcError> rpcErrors = new ArrayList<>(input.getLeft().getErrors());
+            rpcErrors.addAll(input.getRight().getErrors());
+            resultBld.withRpcErrors(rpcErrors);
 
-                resultBld.withResult(input.getLeft().getResult());
+            resultBld.withResult(input.getLeft().getResult());
 
-                return resultBld.build();
-            }
+            return resultBld.build();
         };
     }
 
@@ -208,7 +184,7 @@ public final class FlowUtil {
      * @return instance identifier assembled for given node, table and flow
      */
     public static FlowRef buildFlowPath(final InstanceIdentifier<Node> nodePath,
-                                        final short tableId, final FlowId flowId) {
+                                        final Uint8 tableId, final FlowId flowId) {
         final KeyedInstanceIdentifier<Flow, FlowKey> flowPath = nodePath
                 .augmentation(FlowCapableNode.class)
                 .child(Table.class, new TableKey(tableId))
@@ -226,11 +202,19 @@ public final class FlowUtil {
      * @return static reusable function
      */
     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 Collection<? extends BatchFlowIdGrouping> inputBatchFlows) {
+        return new CumulatingFunction<O>(inputBatchFlows).invoke();
+    }
+
+    private static class CumulatingFunction<O> {
+        private final Collection<? extends BatchFlowIdGrouping> inputBatchFlows;
+
+        CumulatingFunction(final Collection<? extends BatchFlowIdGrouping> inputBatchFlows) {
+            this.inputBatchFlows = inputBatchFlows;
+        }
+
+        public Function<List<RpcResult<O>>, RpcResult<List<BatchFailedFlowsOutput>>> invoke() {
+            return (final List<RpcResult<O>> innerInput) -> {
                 final int sizeOfFutures = innerInput.size();
                 final int sizeOfInputBatch = inputBatchFlows.size();
                 Preconditions.checkArgument(sizeOfFutures == sizeOfInputBatch,
@@ -248,7 +232,7 @@ public final class FlowUtil {
                     if (!flowModOutput.isSuccessful()) {
                         batchFlows.add(new BatchFailedFlowsOutputBuilder()
                                 .setFlowId(flowId)
-                                .setBatchOrder(batchOrder)
+                                .setBatchOrder(Uint16.valueOf(batchOrder))
                                 .build());
                         flowErrors.addAll(flowModOutput.getErrors());
                     }
@@ -263,7 +247,7 @@ public final class FlowUtil {
                     resultBuilder = SUCCESSFUL_FLOW_OUTPUT_RPC_RESULT;
                 }
                 return resultBuilder.build();
-            }
-        };
+            };
+        }
     }
 }