Remove deprecated EOS services
[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 javax.annotation.Nullable;
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.inventory.rev130819.NodeRef;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23
24 /**
25  * Provides barrier message chaining and factory methods.
26  */
27 public final class BarrierUtil {
28
29
30     private BarrierUtil() {
31         throw new IllegalStateException("This class should not be instantiated.");
32     }
33
34
35     /**
36      * Chain a barrier message - regardless of previous result and use given {@link Function} to combine
37      * original result and barrier result.
38      *
39      * @param <T>                type of input future
40      * @param input              future to chain barrier to
41      * @param nodeRef            target device
42      * @param transactionService barrier service
43      * @param compositeTransform composite transform
44      * @return future holding both results (input and of the barrier)
45      */
46     public static <T> ListenableFuture<RpcResult<T>> chainBarrier(
47             final ListenableFuture<RpcResult<T>> input, final NodeRef nodeRef,
48             final FlowCapableTransactionService transactionService,
49             final Function<Pair<RpcResult<T>, RpcResult<Void>>, RpcResult<T>> compositeTransform) {
50         final MutablePair<RpcResult<T>, RpcResult<Void>> resultPair = new MutablePair<>();
51
52         // store input result and append barrier
53         final ListenableFuture<RpcResult<Void>> barrierResult = Futures.transformAsync(input,
54             interInput -> {
55                 resultPair.setLeft(interInput);
56                 final SendBarrierInput barrierInput = createSendBarrierInput(nodeRef);
57                 return JdkFutureAdapters.listenInPoolThread(transactionService.sendBarrier(barrierInput));
58             });
59         // store barrier result and return initiated pair
60         final ListenableFuture<Pair<RpcResult<T>, RpcResult<Void>>> compositeResult = Futures.transform(
61                 barrierResult, new Function<RpcResult<Void>, Pair<RpcResult<T>, RpcResult<Void>>>() {
62                     @Nullable
63                     @Override
64                     public Pair<RpcResult<T>, RpcResult<Void>> apply(@Nullable final RpcResult<Void> input) {
65                         resultPair.setRight(input);
66                         return resultPair;
67                     }
68                 });
69         // append assembling transform to barrier result
70         return Futures.transform(compositeResult, compositeTransform);
71     }
72
73     /**
74      * Creates barrier input.
75      *
76      * @param nodeRef rpc routing context
77      * @return input for {@link FlowCapableTransactionService#sendBarrier(SendBarrierInput)}
78      */
79     public static SendBarrierInput createSendBarrierInput(final NodeRef nodeRef) {
80         return new SendBarrierInputBuilder()
81                 .setNode(nodeRef)
82                 .build();
83     }
84 }