09bf3596fa9d5d782e1524e70a45646677996da9
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / BarrierUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.util;
10
11 import com.google.common.base.Function;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.JdkFutureAdapters;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import org.apache.commons.lang3.tuple.MutablePair;
17 import org.apache.commons.lang3.tuple.Pair;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24
25 /**
26  * Provides barrier message chaining and factory methods.
27  */
28 public final class BarrierUtil {
29
30
31     private BarrierUtil() {
32         throw new IllegalStateException("This class should not be instantiated.");
33     }
34
35
36     /**
37      * Chain a barrier message - regardless of previous result and use given {@link Function} to combine
38      * original result and barrier result.
39      *
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 composite transform
45      * @return future holding both results (input and of the barrier)
46      */
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<SendBarrierOutput>>, RpcResult<T>> compositeTransform) {
51         final MutablePair<RpcResult<T>, RpcResult<SendBarrierOutput>> resultPair = new MutablePair<>();
52
53         // store input result and append barrier
54         final ListenableFuture<RpcResult<SendBarrierOutput>> barrierResult = Futures.transformAsync(input,
55             interInput -> {
56                 resultPair.setLeft(interInput);
57                 final SendBarrierInput barrierInput = createSendBarrierInput(nodeRef);
58                 return JdkFutureAdapters.listenInPoolThread(transactionService.sendBarrier(barrierInput));
59             }, MoreExecutors.directExecutor());
60         // store barrier result and return initiated pair
61         final ListenableFuture<Pair<RpcResult<T>, RpcResult<SendBarrierOutput>>> compositeResult = Futures.transform(
62                 barrierResult,
63             input1 -> {
64                 resultPair.setRight(input1);
65                 return resultPair;
66             }, MoreExecutors.directExecutor());
67         // append assembling transform to barrier result
68         return Futures.transform(compositeResult, compositeTransform, MoreExecutors.directExecutor());
69     }
70
71     /**
72      * Creates barrier input.
73      *
74      * @param nodeRef rpc routing context
75      * @return input for {@link FlowCapableTransactionService#sendBarrier(SendBarrierInput)}
76      */
77     public static SendBarrierInput createSendBarrierInput(final NodeRef nodeRef) {
78         return new SendBarrierInputBuilder()
79                 .setNode(nodeRef)
80                 .build();
81     }
82 }