0423d25bffcb2d763e6b9ff4202ac88decafb06b
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / connection / ConnectionAdapterImpl.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
9 package org.opendaylight.openflowjava.protocol.impl.connection;
10
11 import io.netty.channel.Channel;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.util.concurrent.GenericFutureListener;
14
15 import java.util.Collection;
16 import java.util.concurrent.Future;
17
18 import org.opendaylight.controller.sal.common.util.RpcErrors;
19 import org.opendaylight.controller.sal.common.util.Rpcs;
20 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
47 import org.opendaylight.yangtools.yang.common.RpcError;
48 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
49 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51
52 import com.google.common.collect.Lists;
53 import com.google.common.util.concurrent.SettableFuture;
54
55 /**
56  * @author mirehak
57  *
58  */
59 public class ConnectionAdapterImpl implements ConnectionAdapter {
60     
61     private static final String APPLICATION_TAG = "OPENFLOW_LIBRARY";
62     private static final String TAG = "OPENFLOW";
63     private Channel channel;
64     private OpenflowProtocolListener messageListener;
65     
66     /**
67      * @param channel the channel to set
68      */
69     public void setChannel(Channel channel) {
70         this.channel = channel;
71     }
72
73     @Override
74     public Future<RpcResult<BarrierOutput>> barrier(BarrierInput input) {
75         // TODO Auto-generated method stub
76         return null;
77     }
78
79     @Override
80     public Future<RpcResult<EchoOutput>> echo(EchoInput input) {
81         // TODO Auto-generated method stub
82         return null;
83     }
84
85     @Override
86     public Future<RpcResult<Void>> echoReply(EchoReplyInput input) {
87         return sendToSwitchFuture(input, "echo reply sending failed");
88     }
89
90     @Override
91     public Future<RpcResult<Void>> experimenter(ExperimenterInput input) {
92         // TODO Auto-generated method stub
93         return null;
94     }
95
96     @Override
97     public Future<RpcResult<Void>> flowMod(FlowModInput input) {
98         // TODO Auto-generated method stub
99         return null;
100     }
101
102     @Override
103     public Future<RpcResult<GetConfigOutput>> getConfig(GetConfigInput input) {
104         // TODO Auto-generated method stub
105         return null;
106     }
107
108     @Override
109     public Future<RpcResult<GetFeaturesOutput>> getFeatures(
110             GetFeaturesInput input) {
111         // TODO Auto-generated method stub
112         return null;
113     }
114
115     @Override
116     public Future<RpcResult<GetQueueConfigOutput>> getQueueConfig(
117             GetQueueConfigInput input) {
118         // TODO Auto-generated method stub
119         return null;
120     }
121
122     @Override
123     public Future<RpcResult<Void>> groupMod(GroupModInput input) {
124         // TODO Auto-generated method stub
125         return null;
126     }
127
128     @Override
129     public Future<RpcResult<Void>> hello(HelloInput input) {
130         // TODO Auto-generated method stub
131         return null;
132     }
133
134     @Override
135     public Future<RpcResult<Void>> meterMod(MeterModInput input) {
136         // TODO Auto-generated method stub
137         return null;
138     }
139
140     @Override
141     public Future<RpcResult<Void>> packetOut(PacketOutInput input) {
142         // TODO Auto-generated method stub
143         return null;
144     }
145
146     @Override
147     public Future<RpcResult<Void>> portMod(PortModInput input) {
148         // TODO Auto-generated method stub
149         return null;
150     }
151
152     @Override
153     public Future<RpcResult<RoleRequestOutput>> roleRequest(
154             RoleRequestInput input) {
155         // TODO Auto-generated method stub
156         return null;
157     }
158
159     @Override
160     public Future<RpcResult<Void>> setConfig(SetConfigInput input) {
161         // TODO Auto-generated method stub
162         return null;
163     }
164
165     @Override
166     public Future<RpcResult<Void>> tableMod(TableModInput input) {
167         // TODO Auto-generated method stub
168         return null;
169     }
170
171     /* (non-Javadoc)
172      * @see org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolService#getAsync(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput)
173      */
174     @Override
175     public Future<RpcResult<GetAsyncOutput>> getAsync(GetAsyncInput input) {
176         // TODO Auto-generated method stub
177         return null;
178     }
179
180     /* (non-Javadoc)
181      * @see org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolService#setAsync(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput)
182      */
183     @Override
184     public Future<RpcResult<Void>> setAsync(SetAsyncInput input) {
185         // TODO Auto-generated method stub
186         return null;
187     }
188
189     @Override
190     public Future<Boolean> disconnect() {
191         ChannelFuture disconnectResult = channel.disconnect();
192         
193         String failureInfo = "switch disconnecting failed";
194         ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
195         String message = "Check the switch connection";
196         return handleTransportChannelFuture(disconnectResult, failureInfo, errorSeverity, message);
197     }
198
199     @Override
200     public boolean isAlive() {
201         return channel.isOpen();
202     }
203
204     @Override
205     public void setMessageListener(OpenflowProtocolListener messageListener) {
206         this.messageListener = messageListener;
207     }
208
209     /**
210      * @param input
211      * @return
212      */
213     private SettableFuture<RpcResult<Void>> sendToSwitchFuture(
214             EchoReplyInput input, final String failureInfo) {
215         ChannelFuture resultFuture = channel.writeAndFlush(input);
216         
217         ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
218         String message = "check switch connection";
219         return handleRpcChannelFuture(resultFuture, failureInfo, errorSeverity, message);
220     }
221
222     /**
223      * @param resultFuture
224      * @param failureInfo
225      * @return
226      */
227     private SettableFuture<RpcResult<Void>> handleRpcChannelFuture(
228             ChannelFuture resultFuture, final String failureInfo, 
229             final ErrorSeverity errorSeverity, final String message) {
230         
231         final SettableFuture<RpcResult<Void>> rpcResult = SettableFuture.create();
232         
233         resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
234             
235             @Override
236             public void operationComplete(
237                     io.netty.util.concurrent.Future<? super Void> future)
238                     throws Exception {
239                 Collection<RpcError> errors = null;
240                 
241                 if (future.cause() != null) {
242                     RpcError rpcError = buildRpcError(failureInfo, 
243                             errorSeverity, message, future.cause());
244                     errors = Lists.newArrayList(rpcError);
245                 }
246                 
247                 rpcResult.set(Rpcs.getRpcResult(
248                         future.isSuccess(), 
249                         (Void) null, 
250                         errors)
251                 );
252             }
253         });
254         return rpcResult;
255     }
256     
257     /**
258      * @param resultFuture
259      * @param failureInfo
260      * @return
261      */
262     private SettableFuture<Boolean> handleTransportChannelFuture(
263             ChannelFuture resultFuture, final String failureInfo, 
264             final ErrorSeverity errorSeverity, final String message) {
265         
266         final SettableFuture<Boolean> transportResult = SettableFuture.create();
267         
268         resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
269             
270             @Override
271             public void operationComplete(
272                     io.netty.util.concurrent.Future<? super Void> future)
273                     throws Exception {
274                 transportResult.set(future.isSuccess());
275             }
276         });
277         return transportResult;
278     }
279
280     /**
281      * @param cause
282      * @return
283      */
284     protected RpcError buildRpcError(String info, ErrorSeverity severity, String message, 
285             Throwable cause) {
286         RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message, 
287                 ErrorType.RPC, cause);
288         return error;
289     }
290     
291     /**
292      * @param cause
293      * @return
294      */
295     protected RpcError buildTransportError(String info, ErrorSeverity severity, String message, 
296             Throwable cause) {
297         RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message, 
298                 ErrorType.TRANSPORT, cause);
299         return error;
300     }
301
302 }