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;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public final class FlowUtil {
48 private static final Logger LOG = LoggerFactory.getLogger(FlowUtil.class);
49 private static final RpcResultBuilder<List<BatchFailedFlowsOutput>> SUCCESSFUL_FLOW_OUTPUT_RPC_RESULT =
50 RpcResultBuilder.success(Collections.<BatchFailedFlowsOutput>emptyList());
53 * Attach barrier response to given {@link RpcResult}<RemoveFlowsBatchOutput>
55 public static final Function<Pair<RpcResult<RemoveFlowsBatchOutput>, RpcResult<Void>>, RpcResult<RemoveFlowsBatchOutput>>
56 FLOW_REMOVE_COMPOSING_TRANSFORM = createComposingFunction();
59 * Attach barrier response to given {@link RpcResult}<AddFlowsBatchOutput>
61 public static final Function<Pair<RpcResult<AddFlowsBatchOutput>, RpcResult<Void>>, RpcResult<AddFlowsBatchOutput>>
62 FLOW_ADD_COMPOSING_TRANSFORM = createComposingFunction();
65 * Attach barrier response to given {@link RpcResult}<UpdateFlowsBatchOutput>
67 public static final Function<Pair<RpcResult<UpdateFlowsBatchOutput>, RpcResult<Void>>, RpcResult<UpdateFlowsBatchOutput>>
68 FLOW_UPDATE_COMPOSING_TRANSFORM = createComposingFunction();
71 * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}
73 public static final Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<RemoveFlowsBatchOutput>> FLOW_REMOVE_TRANSFORM =
74 new Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<RemoveFlowsBatchOutput>>() {
77 public RpcResult<RemoveFlowsBatchOutput> apply(@Nullable final RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult) {
78 final RemoveFlowsBatchOutput batchOutput = new RemoveFlowsBatchOutputBuilder()
79 .setBatchFailedFlowsOutput(batchFlowsCumulativeResult.getResult()).build();
81 final RpcResultBuilder<RemoveFlowsBatchOutput> resultBld =
82 createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
83 return resultBld.build();
88 * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}
90 public static final Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<AddFlowsBatchOutput>> FLOW_ADD_TRANSFORM =
91 new Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<AddFlowsBatchOutput>>() {
94 public RpcResult<AddFlowsBatchOutput> apply(@Nullable final RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult) {
95 final AddFlowsBatchOutput batchOutput = new AddFlowsBatchOutputBuilder()
96 .setBatchFailedFlowsOutput(batchFlowsCumulativeResult.getResult()).build();
98 final RpcResultBuilder<AddFlowsBatchOutput> resultBld =
99 createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
100 return resultBld.build();
105 * Gather errors into collection and wrap it into {@link RpcResult} and propagate all {@link RpcError}
107 public static final Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<UpdateFlowsBatchOutput>> FLOW_UPDATE_TRANSFORM =
108 new Function<RpcResult<List<BatchFailedFlowsOutput>>, RpcResult<UpdateFlowsBatchOutput>>() {
111 public RpcResult<UpdateFlowsBatchOutput> apply(@Nullable final RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult) {
112 final UpdateFlowsBatchOutput batchOutput = new UpdateFlowsBatchOutputBuilder()
113 .setBatchFailedFlowsOutput(batchFlowsCumulativeResult.getResult()).build();
115 final RpcResultBuilder<UpdateFlowsBatchOutput> resultBld =
116 createCumulativeRpcResult(batchFlowsCumulativeResult, batchOutput);
117 return resultBld.build();
122 throw new IllegalStateException("This class should not be instantiated.");
126 * Wrap given list of problematic flow-ids into {@link RpcResult} of given type.
128 * @param batchFlowsCumulativeResult list of ids failed flows
130 * @param <T> flow operation type
131 * @return batch flow operation output of given type containing list of flow-ids and corresponding success flag
133 private static <T extends BatchFlowOutputListGrouping>
134 RpcResultBuilder<T> createCumulativeRpcResult(final @Nullable RpcResult<List<BatchFailedFlowsOutput>> batchFlowsCumulativeResult,
135 final T batchOutput) {
136 final RpcResultBuilder<T> resultBld;
137 if (batchFlowsCumulativeResult.isSuccessful()) {
138 resultBld = RpcResultBuilder.success(batchOutput);
140 resultBld = RpcResultBuilder.failed();
141 resultBld.withResult(batchOutput)
142 .withRpcErrors(batchFlowsCumulativeResult.getErrors());
148 * Factory method: create {@link Function} which attaches barrier response to given {@link RpcResult}<T>
149 * and changes success flag if needed.
151 * Original rpcResult is the {@link Pair#getLeft()} and barrier result is the {@link Pair#getRight()}.
153 * @param <T> type of rpcResult value
154 * @return reusable static function
157 static <T extends BatchFlowOutputListGrouping>
158 Function<Pair<RpcResult<T>, RpcResult<Void>>, RpcResult<T>> createComposingFunction() {
159 return new Function<Pair<RpcResult<T>, RpcResult<Void>>, RpcResult<T>>() {
162 public RpcResult<T> apply(@Nullable final Pair<RpcResult<T>, RpcResult<Void>> input) {
163 final RpcResultBuilder<T> resultBld;
164 if (input.getLeft().isSuccessful() && input.getRight().isSuccessful()) {
165 resultBld = RpcResultBuilder.success();
167 resultBld = RpcResultBuilder.failed();
170 final ArrayList<RpcError> rpcErrors = new ArrayList<>(input.getLeft().getErrors());
171 rpcErrors.addAll(input.getRight().getErrors());
172 resultBld.withRpcErrors(rpcErrors);
174 resultBld.withResult(input.getLeft().getResult());
176 return resultBld.build();
182 * Build flow path flow ref.
184 * @param nodePath path to {@link Node}
185 * @param tableId path to {@link Table} under {@link Node}
186 * @param flowId path to {@link Flow} under {@link Table}
187 * @return instance identifier assembled for given node, table and flow
189 public static FlowRef buildFlowPath(final InstanceIdentifier<Node> nodePath,
190 final short tableId, final FlowId flowId) {
191 final KeyedInstanceIdentifier<Flow, FlowKey> flowPath = nodePath
192 .augmentation(FlowCapableNode.class)
193 .child(Table.class, new TableKey(tableId))
194 .child(Flow.class, new FlowKey(new FlowId(flowId)));
196 return new FlowRef(flowPath);
200 * Factory method: creates {@link Function} which keeps info of original inputs (passed to flow-rpc) and processes
201 * list of all flow-rpc results.
203 * @param <O> result container type
204 * @param inputBatchFlows collection of problematic flow-ids wrapped in container of given type <O>
205 * @return static reusable function
207 public static <O> Function<List<RpcResult<O>>, RpcResult<List<BatchFailedFlowsOutput>>> createCumulatingFunction(
208 final List<? extends BatchFlowIdGrouping> inputBatchFlows) {
209 return new Function<List<RpcResult<O>>, RpcResult<List<BatchFailedFlowsOutput>>>() {
212 public RpcResult<List<BatchFailedFlowsOutput>> apply(@Nullable final List<RpcResult<O>> innerInput) {
213 final int sizeOfFutures = innerInput.size();
214 final int sizeOfInputBatch = inputBatchFlows.size();
215 Preconditions.checkArgument(sizeOfFutures == sizeOfInputBatch,
216 "wrong amount of returned futures: {} <> {}", sizeOfFutures, sizeOfInputBatch);
218 final ArrayList<BatchFailedFlowsOutput> batchFlows = new ArrayList<>(sizeOfFutures);
219 final Iterator<? extends BatchFlowIdGrouping> batchFlowIterator = inputBatchFlows.iterator();
221 Collection<RpcError> flowErrors = new ArrayList<>(sizeOfFutures);
224 for (RpcResult<O> flowModOutput : innerInput) {
225 final FlowId flowId = batchFlowIterator.next().getFlowId();
227 if (!flowModOutput.isSuccessful()) {
228 batchFlows.add(new BatchFailedFlowsOutputBuilder()
230 .setBatchOrder(batchOrder)
232 flowErrors.addAll(flowModOutput.getErrors());
237 final RpcResultBuilder<List<BatchFailedFlowsOutput>> resultBuilder;
238 if (!flowErrors.isEmpty()) {
239 resultBuilder = RpcResultBuilder.<List<BatchFailedFlowsOutput>>failed()
240 .withRpcErrors(flowErrors).withResult(batchFlows);
242 resultBuilder = SUCCESSFUL_FLOW_OUTPUT_RPC_RESULT;
244 return resultBuilder.build();