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 org.apache.commons.lang3.tuple.Pair;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierOutput;
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;
44 import org.opendaylight.yangtools.yang.common.Uint8;
46 public final class FlowUtil {
48 private static final RpcResultBuilder<List<BatchFailedFlowsOutput>> SUCCESSFUL_FLOW_OUTPUT_RPC_RESULT =
49 RpcResultBuilder.success(Collections.emptyList());
52 * Attach barrier response to given {@link RpcResult}<RemoveFlowsBatchOutput>.
54 public static final Function<Pair<RpcResult<RemoveFlowsBatchOutput>,
55 RpcResult<SendBarrierOutput>>,
56 RpcResult<RemoveFlowsBatchOutput>>
57 FLOW_REMOVE_COMPOSING_TRANSFORM = createComposingFunction();
60 * Attach barrier response to given {@link RpcResult}<AddFlowsBatchOutput>.
62 public static final Function<Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<SendBarrierOutput>>,
63 RpcResult<AddFlowsBatchOutput>>
64 FLOW_ADD_COMPOSING_TRANSFORM = createComposingFunction();
67 * Attach barrier response to given {@link RpcResult}<UpdateFlowsBatchOutput>.
69 public static final Function<Pair<RpcResult<UpdateFlowsBatchOutput>,
70 RpcResult<SendBarrierOutput>>,
71 RpcResult<UpdateFlowsBatchOutput>>
72 FLOW_UPDATE_COMPOSING_TRANSFORM = createComposingFunction();
75 * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}.
77 public static final Function<RpcResult<List<BatchFailedFlowsOutput>>,
78 RpcResult<RemoveFlowsBatchOutput>> FLOW_REMOVE_TRANSFORM =
79 batchFlowsCumulativeResult -> {
80 final RemoveFlowsBatchOutput batchOutput = new RemoveFlowsBatchOutputBuilder()
81 .setBatchFailedFlowsOutput(batchFlowsCumulativeResult.getResult()).build();
83 final RpcResultBuilder<RemoveFlowsBatchOutput> resultBld =
84 createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
85 return resultBld.build();
89 * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}.
91 public static final Function<RpcResult<List<BatchFailedFlowsOutput>>,
92 RpcResult<AddFlowsBatchOutput>> FLOW_ADD_TRANSFORM =
93 batchFlowsCumulativeResult -> {
94 final AddFlowsBatchOutput batchOutput = new AddFlowsBatchOutputBuilder()
95 .setBatchFailedFlowsOutput(batchFlowsCumulativeResult.getResult()).build();
97 final RpcResultBuilder<AddFlowsBatchOutput> resultBld =
98 createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
99 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>>,
106 RpcResult<UpdateFlowsBatchOutput>> FLOW_UPDATE_TRANSFORM =
107 batchFlowsCumulativeResult -> {
108 final UpdateFlowsBatchOutput batchOutput = new UpdateFlowsBatchOutputBuilder()
109 .setBatchFailedFlowsOutput(batchFlowsCumulativeResult.getResult()).build();
111 final RpcResultBuilder<UpdateFlowsBatchOutput> resultBld =
112 createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
113 return resultBld.build();
117 throw new IllegalStateException("This class should not be instantiated.");
121 * Wrap given list of problematic flow-ids into {@link RpcResult} of given type.
123 * @param batchFlowsCumulativeResult list of ids failed flows
124 * @param batchOutput flow operation type
125 * @return batch flow operation output of given type containing list of flow-ids and corresponding success flag
127 private static <T extends BatchFlowOutputListGrouping> RpcResultBuilder<T> createCumulativeRpcResult(
128 final RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult,
129 final T batchOutput) {
130 final RpcResultBuilder<T> resultBld;
131 if (batchFlowsCumulativeResult.isSuccessful()) {
132 resultBld = RpcResultBuilder.success(batchOutput);
134 resultBld = RpcResultBuilder.failed();
135 resultBld.withResult(batchOutput)
136 .withRpcErrors(batchFlowsCumulativeResult.getErrors());
142 * Factory method: create {@link Function} which attaches barrier response to given {@link RpcResult}<T>
143 * and changes success flag if needed.
145 * Original rpcResult is the {@link Pair#getLeft()} and barrier result is the {@link Pair#getRight()}.
147 * @param <T> type of rpcResult value
148 * @return reusable static function
151 static <T extends BatchFlowOutputListGrouping>
152 Function<Pair<RpcResult<T>, RpcResult<SendBarrierOutput>>, RpcResult<T>> createComposingFunction() {
154 final RpcResultBuilder<T> resultBld;
155 if (input.getLeft().isSuccessful() && input.getRight().isSuccessful()) {
156 resultBld = RpcResultBuilder.success();
158 resultBld = RpcResultBuilder.failed();
161 final ArrayList<RpcError> rpcErrors = new ArrayList<>(input.getLeft().getErrors());
162 rpcErrors.addAll(input.getRight().getErrors());
163 resultBld.withRpcErrors(rpcErrors);
165 resultBld.withResult(input.getLeft().getResult());
167 return resultBld.build();
172 * Build flow path flow ref.
174 * @param nodePath path to {@link Node}
175 * @param tableId path to {@link Table} under {@link Node}
176 * @param flowId path to {@link Flow} under {@link Table}
177 * @return instance identifier assembled for given node, table and flow
179 public static FlowRef buildFlowPath(final InstanceIdentifier<Node> nodePath,
180 final Uint8 tableId, final FlowId flowId) {
181 final KeyedInstanceIdentifier<Flow, FlowKey> flowPath = nodePath
182 .augmentation(FlowCapableNode.class)
183 .child(Table.class, new TableKey(tableId))
184 .child(Flow.class, new FlowKey(new FlowId(flowId)));
186 return new FlowRef(flowPath);
190 * Factory method: creates {@link Function} which keeps info of original inputs (passed to flow-rpc) and processes
191 * list of all flow-rpc results.
193 * @param <O> result container type
194 * @param inputBatchFlows collection of problematic flow-ids wrapped in container of given type <O>
195 * @return static reusable function
197 public static <O> Function<List<RpcResult<O>>, RpcResult<List<BatchFailedFlowsOutput>>> createCumulatingFunction(
198 final Collection<? extends BatchFlowIdGrouping> inputBatchFlows) {
199 return new CumulatingFunction<O>(inputBatchFlows).invoke();
202 private static class CumulatingFunction<O> {
203 private final Collection<? extends BatchFlowIdGrouping> inputBatchFlows;
205 CumulatingFunction(final Collection<? extends BatchFlowIdGrouping> inputBatchFlows) {
206 this.inputBatchFlows = inputBatchFlows;
209 public Function<List<RpcResult<O>>, RpcResult<List<BatchFailedFlowsOutput>>> invoke() {
210 return (final List<RpcResult<O>> innerInput) -> {
211 final int sizeOfFutures = innerInput.size();
212 final int sizeOfInputBatch = inputBatchFlows.size();
213 Preconditions.checkArgument(sizeOfFutures == sizeOfInputBatch,
214 "wrong amount of returned futures: {} <> {}", sizeOfFutures, sizeOfInputBatch);
216 final ArrayList<BatchFailedFlowsOutput> batchFlows = new ArrayList<>(sizeOfFutures);
217 final Iterator<? extends BatchFlowIdGrouping> batchFlowIterator = inputBatchFlows.iterator();
219 Collection<RpcError> flowErrors = new ArrayList<>(sizeOfFutures);
222 for (RpcResult<O> flowModOutput : innerInput) {
223 final FlowId flowId = batchFlowIterator.next().getFlowId();
225 if (!flowModOutput.isSuccessful()) {
226 batchFlows.add(new BatchFailedFlowsOutputBuilder()
228 .setBatchOrder(batchOrder)
230 flowErrors.addAll(flowModOutput.getErrors());
235 final RpcResultBuilder<List<BatchFailedFlowsOutput>> resultBuilder;
236 if (!flowErrors.isEmpty()) {
237 resultBuilder = RpcResultBuilder.<List<BatchFailedFlowsOutput>>failed()
238 .withRpcErrors(flowErrors).withResult(batchFlows);
240 resultBuilder = SUCCESSFUL_FLOW_OUTPUT_RPC_RESULT;
242 return resultBuilder.build();