BUG-1006 - removal of bulkTransactionCache
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / OFRpcTaskHelper.java
1 /**
2  * Copyright (c) 2013 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 package org.opendaylight.openflowplugin.openflow.md.core.sal;
9
10 import java.util.concurrent.Future;
11 import java.util.concurrent.TimeUnit;
12
13 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
14 import org.opendaylight.controller.sal.common.util.Rpcs;
15 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
16 import org.opendaylight.openflowplugin.openflow.md.core.session.IMessageDispatchService;
17 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20
21 import com.google.common.base.Objects;
22 import com.google.common.util.concurrent.FutureCallback;
23 import com.google.common.util.concurrent.Futures;
24 import com.google.common.util.concurrent.JdkFutureAdapters;
25 import com.google.common.util.concurrent.SettableFuture;
26
27 /**
28  * 
29  */
30 public class OFRpcTaskHelper {
31
32     private IMessageDispatchService messageService;
33     private SessionContext session;
34     private NotificationProviderService rpcNotificationProviderService;
35     /**
36      * @param cookie
37      * @param messageService
38      * @param session
39      * @param rpcNotificationProviderService 
40      */
41     public OFRpcTaskHelper(IMessageDispatchService messageService, SessionContext session, 
42             NotificationProviderService rpcNotificationProviderService) {
43         this.messageService = messageService;
44         this.session = session;
45         this.rpcNotificationProviderService = rpcNotificationProviderService;
46     }
47     
48     
49     /**
50      * @param task
51      * @param input 
52      * @param cookie 
53      * @return inited task
54      */
55     public <T, K> OFRpcTask<T, K> initTask(OFRpcTask<T, K> task, T input, SwitchConnectionDistinguisher cookie) {
56         task.setMessageService(messageService);
57         task.setSession(session);
58         task.setRpcNotificationProviderService(rpcNotificationProviderService);
59         task.setResult(SettableFuture.<K>create());
60         task.setCookie(cookie);
61         task.setInput(input);
62         return task;
63     }
64     
65     /**
66      * @param intern 
67      * @param wrapper 
68      */
69     public static <K> void chainFutures(final Future<K> intern, final SettableFuture<K> wrapper) {
70         Futures.addCallback(
71                 JdkFutureAdapters.listenInPoolThread(intern),
72                 new FutureCallback<K>() {
73
74                     @Override
75                     public void onSuccess(
76                             K result) {
77                         wrapper.set(result);
78                     }
79
80                     @Override
81                     public void onFailure(Throwable t) {
82                         wrapper.setException(t);
83                     }
84
85                 });
86     }
87     
88     /**
89      * @param maxTimeout
90      * @param maxTimeoutUnit
91      * @param isBarrier 
92      * @param cookie 
93      * @param result 
94      */
95     public <T> void rawBarrierSend(final long maxTimeout, final TimeUnit maxTimeoutUnit, 
96             Boolean isBarrier, SwitchConnectionDistinguisher cookie, SettableFuture<RpcResult<T>> result) {
97         if (Objects.firstNonNull(isBarrier, Boolean.FALSE)) {
98             Future<RpcResult<BarrierOutput>> barrierFuture = ModelDrivenSwitchImpl.sendBarrier(cookie, session, messageService);
99             try {
100                 RpcResult<BarrierOutput> barrierResult = barrierFuture.get(maxTimeout, maxTimeoutUnit);
101                 if (!barrierResult.isSuccessful()) {
102                     result.set(Rpcs.<T>getRpcResult(false, barrierResult.getErrors()));
103                 }
104             } catch (Exception e) {
105                 result.setException(e);
106             }
107         }
108     }
109 }