Bug 1764 - moved Session related interfaces to openflowplugin-api
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / OFRpcTask.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.Callable;
11 import java.util.concurrent.TimeUnit;
12
13 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
14 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
15 import org.opendaylight.openflowplugin.api.openflow.md.core.session.IMessageDispatchService;
16 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
17
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20
21 /**
22  * @param <T> input type
23  * @param <K> future output type
24  */
25 public abstract class OFRpcTask<T, K> implements Callable<ListenableFuture<K>> {
26     
27     private OFRpcTaskContext taskContext;
28     private T input;
29     private SwitchConnectionDistinguisher cookie;
30     
31     /**
32      * @param taskContext
33      * @param input
34      * @param cookie 
35      */
36     public OFRpcTask(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie, T input) {
37         this.taskContext = taskContext;
38         this.cookie = cookie;
39         this.input = input;
40     }
41
42     /**
43      * @return the cookie
44      */
45     public SwitchConnectionDistinguisher getCookie() {
46         return cookie;
47     }
48
49     /**
50      * @param cookie the cookie to set
51      */
52     public void setCookie(SwitchConnectionDistinguisher cookie) {
53         this.cookie = cookie;
54     }
55
56     /**
57      * @return the input
58      */
59     public T getInput() {
60         return input;
61     }
62
63     /**
64      * @param input the input to set
65      */
66     public void setInput(T input) {
67         this.input = input;
68     }
69
70     /**
71      * @return the rpcNotificationProviderService
72      */
73     public NotificationProviderService getRpcNotificationProviderService() {
74         return taskContext.getRpcNotificationProviderService();
75     }
76
77     /**
78      * @return message service
79      * @see org.opendaylight.openflowplugin.openflow.md.core.sal.OFRpcTaskContext#getMessageService()
80      */
81     public IMessageDispatchService getMessageService() {
82         return taskContext.getMessageService();
83     }
84
85     /**
86      * @return session
87      * @see org.opendaylight.openflowplugin.openflow.md.core.sal.OFRpcTaskContext#getSession()
88      */
89     public SessionContext getSession() {
90         return taskContext.getSession();
91     }
92
93     /**
94      * @return max timeout
95      * @see org.opendaylight.openflowplugin.openflow.md.core.sal.OFRpcTaskContext#getMaxTimeout()
96      */
97     public long getMaxTimeout() {
98         return taskContext.getMaxTimeout();
99     }
100
101     /**
102      * @return time unit for max timeout
103      * @see org.opendaylight.openflowplugin.openflow.md.core.sal.OFRpcTaskContext#getMaxTimeoutUnit()
104      */
105     public TimeUnit getMaxTimeoutUnit() {
106         return taskContext.getMaxTimeoutUnit();
107     }
108     
109     /**
110      * @return protocol version
111      */
112     public Short getVersion() {
113         return taskContext.getSession().getFeatures().getVersion();
114         
115     }
116     
117     /**
118      * @return the taskContext
119      */
120     public OFRpcTaskContext getTaskContext() {
121         return taskContext;
122     }
123     
124     /**
125      * submit task into rpc worker pool
126      * @return future result of task 
127      */
128     public ListenableFuture<K> submit() {
129         ListenableFuture<ListenableFuture<K>> compoundResult = getTaskContext().getRpcPool().submit(this);
130         return Futures.dereference(compoundResult);
131     }
132     
133     /**
134      * @return required barrier value
135      */
136     public Boolean isBarrier() {
137         return null;
138     }
139 }