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
8 package org.opendaylight.openflowplugin.impl.util;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Function;
12 import com.google.common.base.Preconditions;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.Iterator;
17 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.Identifiable;
40 import org.opendaylight.yangtools.yang.binding.Identifier;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
44 import org.opendaylight.yangtools.yang.common.RpcError;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
47 import org.opendaylight.yangtools.yang.common.Uint16;
48 import org.opendaylight.yangtools.yang.common.Uint8;
50 public final class FlowUtil {
52 private static final RpcResultBuilder<List<BatchFailedFlowsOutput>> SUCCESSFUL_FLOW_OUTPUT_RPC_RESULT =
53 RpcResultBuilder.success(Collections.emptyList());
56 * Attach barrier response to given {@link RpcResult}<RemoveFlowsBatchOutput>.
58 public static final Function<Pair<RpcResult<RemoveFlowsBatchOutput>,
59 RpcResult<SendBarrierOutput>>,
60 RpcResult<RemoveFlowsBatchOutput>>
61 FLOW_REMOVE_COMPOSING_TRANSFORM = createComposingFunction();
64 * Attach barrier response to given {@link RpcResult}<AddFlowsBatchOutput>.
66 public static final Function<Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<SendBarrierOutput>>,
67 RpcResult<AddFlowsBatchOutput>>
68 FLOW_ADD_COMPOSING_TRANSFORM = createComposingFunction();
71 * Attach barrier response to given {@link RpcResult}<UpdateFlowsBatchOutput>.
73 public static final Function<Pair<RpcResult<UpdateFlowsBatchOutput>,
74 RpcResult<SendBarrierOutput>>,
75 RpcResult<UpdateFlowsBatchOutput>>
76 FLOW_UPDATE_COMPOSING_TRANSFORM = createComposingFunction();
79 * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}.
81 public static final Function<RpcResult<List<BatchFailedFlowsOutput>>,
82 RpcResult<RemoveFlowsBatchOutput>> FLOW_REMOVE_TRANSFORM =
83 batchFlowsCumulativeResult -> {
84 final RemoveFlowsBatchOutput batchOutput = new RemoveFlowsBatchOutputBuilder()
85 .setBatchFailedFlowsOutput(index(batchFlowsCumulativeResult.getResult())).build();
87 final RpcResultBuilder<RemoveFlowsBatchOutput> resultBld =
88 createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
89 return resultBld.build();
93 * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}.
95 public static final Function<RpcResult<List<BatchFailedFlowsOutput>>,
96 RpcResult<AddFlowsBatchOutput>> FLOW_ADD_TRANSFORM =
97 batchFlowsCumulativeResult -> {
98 final AddFlowsBatchOutput batchOutput = new AddFlowsBatchOutputBuilder()
99 .setBatchFailedFlowsOutput(index(batchFlowsCumulativeResult.getResult())).build();
101 final RpcResultBuilder<AddFlowsBatchOutput> resultBld =
102 createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
103 return resultBld.build();
107 * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}.
109 public static final Function<RpcResult<List<BatchFailedFlowsOutput>>,
110 RpcResult<UpdateFlowsBatchOutput>> FLOW_UPDATE_TRANSFORM =
111 batchFlowsCumulativeResult -> {
112 final UpdateFlowsBatchOutput batchOutput = new UpdateFlowsBatchOutputBuilder()
113 .setBatchFailedFlowsOutput(index(batchFlowsCumulativeResult.getResult())).build();
115 final RpcResultBuilder<UpdateFlowsBatchOutput> resultBld =
116 createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
117 return resultBld.build();
124 static <K extends Identifier<V>, V extends Identifiable<K>> Map<K, V> index(final List<V> list) {
125 return list == null ? null : BindingMap.ordered(list);
129 * Wrap given list of problematic flow-ids into {@link RpcResult} of given type.
131 * @param batchFlowsCumulativeResult list of ids failed flows
132 * @param batchOutput flow operation type
133 * @return batch flow operation output of given type containing list of flow-ids and corresponding success flag
135 private static <T extends BatchFlowOutputListGrouping> RpcResultBuilder<T> createCumulativeRpcResult(
136 final RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult,
137 final T batchOutput) {
138 final RpcResultBuilder<T> resultBld;
139 if (batchFlowsCumulativeResult.isSuccessful()) {
140 resultBld = RpcResultBuilder.success(batchOutput);
142 resultBld = RpcResultBuilder.failed();
143 resultBld.withResult(batchOutput)
144 .withRpcErrors(batchFlowsCumulativeResult.getErrors());
150 * Factory method: create {@link Function} which attaches barrier response to given {@link RpcResult}<T>
151 * and changes success flag if needed.
153 * Original rpcResult is the {@link Pair#getLeft()} and barrier result is the {@link Pair#getRight()}.
155 * @param <T> type of rpcResult value
156 * @return reusable static function
159 static <T extends BatchFlowOutputListGrouping>
160 Function<Pair<RpcResult<T>, RpcResult<SendBarrierOutput>>, RpcResult<T>> createComposingFunction() {
162 final RpcResultBuilder<T> resultBld;
163 if (input.getLeft().isSuccessful() && input.getRight().isSuccessful()) {
164 resultBld = RpcResultBuilder.success();
166 resultBld = RpcResultBuilder.failed();
169 final ArrayList<RpcError> rpcErrors = new ArrayList<>(input.getLeft().getErrors());
170 rpcErrors.addAll(input.getRight().getErrors());
171 resultBld.withRpcErrors(rpcErrors);
173 resultBld.withResult(input.getLeft().getResult());
175 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 Uint8 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 Collection<? extends BatchFlowIdGrouping> inputBatchFlows) {
207 return new CumulatingFunction<O>(inputBatchFlows).invoke();
210 private static class CumulatingFunction<O> {
211 private final Collection<? extends BatchFlowIdGrouping> inputBatchFlows;
213 CumulatingFunction(final Collection<? extends BatchFlowIdGrouping> inputBatchFlows) {
214 this.inputBatchFlows = inputBatchFlows;
217 public Function<List<RpcResult<O>>, RpcResult<List<BatchFailedFlowsOutput>>> invoke() {
218 return (final List<RpcResult<O>> innerInput) -> {
219 final int sizeOfFutures = innerInput.size();
220 final int sizeOfInputBatch = inputBatchFlows.size();
221 Preconditions.checkArgument(sizeOfFutures == sizeOfInputBatch,
222 "wrong amount of returned futures: {} <> {}", sizeOfFutures, sizeOfInputBatch);
224 final ArrayList<BatchFailedFlowsOutput> batchFlows = new ArrayList<>(sizeOfFutures);
225 final Iterator<? extends BatchFlowIdGrouping> batchFlowIterator = inputBatchFlows.iterator();
227 Collection<RpcError> flowErrors = new ArrayList<>(sizeOfFutures);
230 for (RpcResult<O> flowModOutput : innerInput) {
231 final FlowId flowId = batchFlowIterator.next().getFlowId();
233 if (!flowModOutput.isSuccessful()) {
234 batchFlows.add(new BatchFailedFlowsOutputBuilder()
236 .setBatchOrder(Uint16.valueOf(batchOrder))
238 flowErrors.addAll(flowModOutput.getErrors());
243 final RpcResultBuilder<List<BatchFailedFlowsOutput>> resultBuilder;
244 if (!flowErrors.isEmpty()) {
245 resultBuilder = RpcResultBuilder.<List<BatchFailedFlowsOutput>>failed()
246 .withRpcErrors(flowErrors).withResult(batchFlows);
248 resultBuilder = SUCCESSFUL_FLOW_OUTPUT_RPC_RESULT;
250 return resultBuilder.build();