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;
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());
- /** Attach barrier response to given {@link RpcResult}<RemoveFlowsBatchOutput> */
+ /**
+ * Attach barrier response to given {@link RpcResult}<RemoveFlowsBatchOutput>
+ */
public static final Function<Pair<RpcResult<RemoveFlowsBatchOutput>, RpcResult<Void>>, RpcResult<RemoveFlowsBatchOutput>>
FLOW_REMOVE_COMPOSING_TRANSFORM = createComposingFunction();
- /** Attach barrier response to given {@link RpcResult}<AddFlowsBatchOutput> */
+ /**
+ * Attach barrier response to given {@link RpcResult}<AddFlowsBatchOutput>
+ */
public static final Function<Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<Void>>, RpcResult<AddFlowsBatchOutput>>
FLOW_ADD_COMPOSING_TRANSFORM = createComposingFunction();
- /** Attach barrier response to given {@link RpcResult}<UpdateFlowsBatchOutput> */
+ /**
+ * Attach barrier response to given {@link RpcResult}<UpdateFlowsBatchOutput>
+ */
public static final Function<Pair<RpcResult<UpdateFlowsBatchOutput>, RpcResult<Void>>, RpcResult<UpdateFlowsBatchOutput>>
FLOW_UPDATE_COMPOSING_TRANSFORM = createComposingFunction();
return resultBld;
}
- 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}<T>
* and changes success flag if needed.
}
/**
+ * Build flow path flow ref.
+ *
* @param nodePath path to {@link Node}
* @param tableId path to {@link Table} under {@link Node}
* @param flowId path to {@link Flow} under {@link Table}
* Factory method: creates {@link Function} which keeps info of original inputs (passed to flow-rpc) and processes
* list of all flow-rpc results.
*
- * @param inputBatchFlows collection of problematic flow-ids wrapped in container of given type <O>
* @param <O> result container type
+ * @param inputBatchFlows collection of problematic flow-ids wrapped in container of given type <O>
* @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 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);
-
- 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());
+ return new CumulatingFunction<O>(inputBatchFlows).invoke();
+ }
+
+ private static class CumulatingFunction<O> {
+ private final List<? extends BatchFlowIdGrouping> inputBatchFlows;
+
+ public CumulatingFunction(List<? extends BatchFlowIdGrouping> inputBatchFlows) {
+ this.inputBatchFlows = inputBatchFlows;
+ }
+
+ 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);
+
+ 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();
- }
- };
+ };
+ }
}
}