2 * Copyright (c) 2016 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.base.Function;
12 import com.google.common.util.concurrent.AsyncFunction;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.JdkFutureAdapters;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import javax.annotation.Nullable;
17 import org.apache.commons.lang3.tuple.MutablePair;
18 import org.apache.commons.lang3.tuple.Pair;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
26 * provides barrier message chaining and factory methods
28 public final class BarrierUtil {
31 private BarrierUtil() {
32 throw new IllegalStateException("This class should not be instantiated.");
37 * chain a barrier message - regardless of previous result and use given {@link Function} to combine
38 * original result and barrier result
40 * @param <T> type of input future
41 * @param input future to chain barrier to
42 * @param nodeRef target device
43 * @param transactionService barrier service
44 * @param compositeTransform
45 * @return future holding both results (input and of the barrier)
47 public static <T> ListenableFuture<RpcResult<T>> chainBarrier(
48 final ListenableFuture<RpcResult<T>> input, final NodeRef nodeRef,
49 final FlowCapableTransactionService transactionService,
50 final Function<Pair<RpcResult<T>, RpcResult<Void>>, RpcResult<T>> compositeTransform) {
51 final MutablePair<RpcResult<T>, RpcResult<Void>> resultPair = new MutablePair<>();
53 // store input result and append barrier
54 final ListenableFuture<RpcResult<Void>> barrierResult = Futures.transformAsync(input,
55 new AsyncFunction<RpcResult<T>, RpcResult<Void>>() {
57 public ListenableFuture<RpcResult<Void>> apply(@Nullable final RpcResult<T> interInput) throws Exception {
58 resultPair.setLeft(interInput);
59 final SendBarrierInput barrierInput = createSendBarrierInput(nodeRef);
60 return JdkFutureAdapters.listenInPoolThread(transactionService.sendBarrier(barrierInput));
63 // store barrier result and return initiated pair
64 final ListenableFuture<Pair<RpcResult<T>, RpcResult<Void>>> compositeResult = Futures.transform(
65 barrierResult, new Function<RpcResult<Void>, Pair<RpcResult<T>, RpcResult<Void>>>() {
68 public Pair<RpcResult<T>, RpcResult<Void>> apply(@Nullable final RpcResult<Void> input) {
69 resultPair.setRight(input);
73 // append assembling transform to barrier result
74 return Futures.transform(compositeResult, compositeTransform);
78 * @param nodeRef rpc routing context
79 * @return input for {@link FlowCapableTransactionService#sendBarrier(SendBarrierInput)}
81 public static SendBarrierInput createSendBarrierInput(final NodeRef nodeRef) {
82 return new SendBarrierInputBuilder()