2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.impl.util;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Function;
13 import com.google.common.base.Preconditions;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.Iterator;
18 import java.util.List;
19 import javax.annotation.Nullable;
20 import org.apache.commons.lang3.tuple.Pair;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchOutputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.BatchFlowIdGrouping;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.BatchFlowOutputListGrouping;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.RemoveFlowsBatchOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.RemoveFlowsBatchOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.UpdateFlowsBatchOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.UpdateFlowsBatchOutputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.output.list.grouping.BatchFailedFlowsOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.output.list.grouping.BatchFailedFlowsOutputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.RpcError;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45 public final class FlowUtil {
47 private static final RpcResultBuilder<List<BatchFailedFlowsOutput>> SUCCESSFUL_FLOW_OUTPUT_RPC_RESULT =
48 RpcResultBuilder.success(Collections.<BatchFailedFlowsOutput>emptyList());
51 * Attach barrier response to given {@link RpcResult}<RemoveFlowsBatchOutput>
53 public static final Function<Pair<RpcResult<RemoveFlowsBatchOutput>, RpcResult<Void>>, RpcResult<RemoveFlowsBatchOutput>>
54 FLOW_REMOVE_COMPOSING_TRANSFORM = createComposingFunction();
57 * Attach barrier response to given {@link RpcResult}<AddFlowsBatchOutput>
59 public static final Function<Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<Void>>, RpcResult<AddFlowsBatchOutput>>
60 FLOW_ADD_COMPOSING_TRANSFORM = createComposingFunction();
63 * Attach barrier response to given {@link RpcResult}<UpdateFlowsBatchOutput>
65 public static final Function<Pair<RpcResult<UpdateFlowsBatchOutput>, RpcResult<Void>>, RpcResult<UpdateFlowsBatchOutput>>
66 FLOW_UPDATE_COMPOSING_TRANSFORM = createComposingFunction();
69 * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}
71 public static final Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<RemoveFlowsBatchOutput>> FLOW_REMOVE_TRANSFORM =
72 new Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<RemoveFlowsBatchOutput>>() {
75 public RpcResult<RemoveFlowsBatchOutput> apply(@Nullable final RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult) {
76 final RemoveFlowsBatchOutput batchOutput = new RemoveFlowsBatchOutputBuilder()
77 .setBatchFailedFlowsOutput(batchFlowsCumulativeResult.getResult()).build();
79 final RpcResultBuilder<RemoveFlowsBatchOutput> resultBld =
80 createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
81 return resultBld.build();
86 * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}
88 public static final Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<AddFlowsBatchOutput>> FLOW_ADD_TRANSFORM =
89 new Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<AddFlowsBatchOutput>>() {
92 public RpcResult<AddFlowsBatchOutput> apply(@Nullable final RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult) {
93 final AddFlowsBatchOutput batchOutput = new AddFlowsBatchOutputBuilder()
94 .setBatchFailedFlowsOutput(batchFlowsCumulativeResult.getResult()).build();
96 final RpcResultBuilder<AddFlowsBatchOutput> resultBld =
97 createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
98 return resultBld.build();
103 * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}
105 public static final Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<UpdateFlowsBatchOutput>> FLOW_UPDATE_TRANSFORM =
106 new Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<UpdateFlowsBatchOutput>>() {
109 public RpcResult<UpdateFlowsBatchOutput> apply(@Nullable final RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult) {
110 final UpdateFlowsBatchOutput batchOutput = new UpdateFlowsBatchOutputBuilder()
111 .setBatchFailedFlowsOutput(batchFlowsCumulativeResult.getResult()).build();
113 final RpcResultBuilder<UpdateFlowsBatchOutput> resultBld =
114 createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
115 return resultBld.build();
120 throw new IllegalStateException("This class should not be instantiated.");
124 * Wrap given list of problematic flow-ids into {@link RpcResult} of given type.
126 * @param batchFlowsCumulativeResult list of ids failed flows
128 * @param <T> flow operation type
129 * @return batch flow operation output of given type containing list of flow-ids and corresponding success flag
131 private static <T extends BatchFlowOutputListGrouping>
132 RpcResultBuilder<T> createCumulativeRpcResult(final @Nullable RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult,
133 final T batchOutput) {
134 final RpcResultBuilder<T> resultBld;
135 if (batchFlowsCumulativeResult.isSuccessful()) {
136 resultBld = RpcResultBuilder.success(batchOutput);
138 resultBld = RpcResultBuilder.failed();
139 resultBld.withResult(batchOutput)
140 .withRpcErrors(batchFlowsCumulativeResult.getErrors());
146 * Factory method: create {@link Function} which attaches barrier response to given {@link RpcResult}<T>
147 * and changes success flag if needed.
149 * Original rpcResult is the {@link Pair#getLeft()} and barrier result is the {@link Pair#getRight()}.
151 * @param <T> type of rpcResult value
152 * @return reusable static function
155 static <T extends BatchFlowOutputListGrouping>
156 Function<Pair<RpcResult<T>, RpcResult<Void>>, RpcResult<T>> createComposingFunction() {
157 return new Function<Pair<RpcResult<T>, RpcResult<Void>>, RpcResult<T>>() {
160 public RpcResult<T> apply(@Nullable final Pair<RpcResult<T>, RpcResult<Void>> input) {
161 final RpcResultBuilder<T> resultBld;
162 if (input.getLeft().isSuccessful() && input.getRight().isSuccessful()) {
163 resultBld = RpcResultBuilder.success();
165 resultBld = RpcResultBuilder.failed();
168 final ArrayList<RpcError> rpcErrors = new ArrayList<>(input.getLeft().getErrors());
169 rpcErrors.addAll(input.getRight().getErrors());
170 resultBld.withRpcErrors(rpcErrors);
172 resultBld.withResult(input.getLeft().getResult());
174 return resultBld.build();
180 * Build flow path flow ref.
182 * @param nodePath path to {@link Node}
183 * @param tableId path to {@link Table} under {@link Node}
184 * @param flowId path to {@link Flow} under {@link Table}
185 * @return instance identifier assembled for given node, table and flow
187 public static FlowRef buildFlowPath(final InstanceIdentifier<Node> nodePath,
188 final short tableId, final FlowId flowId) {
189 final KeyedInstanceIdentifier<Flow, FlowKey> flowPath = nodePath
190 .augmentation(FlowCapableNode.class)
191 .child(Table.class, new TableKey(tableId))
192 .child(Flow.class, new FlowKey(new FlowId(flowId)));
194 return new FlowRef(flowPath);
198 * Factory method: creates {@link Function} which keeps info of original inputs (passed to flow-rpc) and processes
199 * list of all flow-rpc results.
201 * @param <O> result container type
202 * @param inputBatchFlows collection of problematic flow-ids wrapped in container of given type <O>
203 * @return static reusable function
205 public static <O> Function<List<RpcResult<O>>, RpcResult<List<BatchFailedFlowsOutput>>> createCumulatingFunction(
206 final List<? extends BatchFlowIdGrouping> inputBatchFlows) {
207 return new CumulatingFunction<O>(inputBatchFlows).invoke();
210 private static class CumulatingFunction<O> {
211 private final List<? extends BatchFlowIdGrouping> inputBatchFlows;
213 public CumulatingFunction(List<? extends BatchFlowIdGrouping> inputBatchFlows) {
214 this.inputBatchFlows = inputBatchFlows;
217 public Function<List<RpcResult<O>>, RpcResult<List<BatchFailedFlowsOutput>>> invoke() {
218 return new Function<List<RpcResult<O>>, RpcResult<List<BatchFailedFlowsOutput>>>() {
221 public RpcResult<List<BatchFailedFlowsOutput>> apply(@Nullable final List<RpcResult<O>> innerInput) {
222 final int sizeOfFutures = innerInput.size();
223 final int sizeOfInputBatch = inputBatchFlows.size();
224 Preconditions.checkArgument(sizeOfFutures == sizeOfInputBatch,
225 "wrong amount of returned futures: {} <> {}", sizeOfFutures, sizeOfInputBatch);
227 final ArrayList<BatchFailedFlowsOutput> batchFlows = new ArrayList<>(sizeOfFutures);
228 final Iterator<? extends BatchFlowIdGrouping> batchFlowIterator = inputBatchFlows.iterator();
230 Collection<RpcError> flowErrors = new ArrayList<>(sizeOfFutures);
233 for (RpcResult<O> flowModOutput : innerInput) {
234 final FlowId flowId = batchFlowIterator.next().getFlowId();
236 if (!flowModOutput.isSuccessful()) {
237 batchFlows.add(new BatchFailedFlowsOutputBuilder()
239 .setBatchOrder(batchOrder)
241 flowErrors.addAll(flowModOutput.getErrors());
246 final RpcResultBuilder<List<BatchFailedFlowsOutput>> resultBuilder;
247 if (!flowErrors.isEmpty()) {
248 resultBuilder = RpcResultBuilder.<List<BatchFailedFlowsOutput>>failed()
249 .withRpcErrors(flowErrors).withResult(batchFlows);
251 resultBuilder = SUCCESSFUL_FLOW_OUTPUT_RPC_RESULT;
253 return resultBuilder.build();