Javadoc update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / connection / ConnectionAdapterImpl.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2
3 package org.opendaylight.openflowjava.protocol.impl.connection;
4
5 import io.netty.channel.Channel;
6 import io.netty.channel.ChannelFuture;
7 import io.netty.util.concurrent.GenericFutureListener;
8
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.List;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.TimeUnit;
14
15 import org.opendaylight.controller.sal.common.util.RpcErrors;
16 import org.opendaylight.controller.sal.common.util.Rpcs;
17 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
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.FlowRemovedMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
57 import org.opendaylight.yangtools.yang.binding.DataContainer;
58 import org.opendaylight.yangtools.yang.binding.DataObject;
59 import org.opendaylight.yangtools.yang.binding.Notification;
60 import org.opendaylight.yangtools.yang.common.RpcError;
61 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
62 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
63 import org.opendaylight.yangtools.yang.common.RpcResult;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67 import com.google.common.cache.Cache;
68 import com.google.common.cache.CacheBuilder;
69 import com.google.common.cache.RemovalListener;
70 import com.google.common.cache.RemovalNotification;
71 import com.google.common.collect.Lists;
72 import com.google.common.util.concurrent.SettableFuture;
73
74 /**
75  * Handles messages (notifications + rpcs) and connections
76  * @author mirehak
77  * @author michal.polkorab
78  */
79 public class ConnectionAdapterImpl implements ConnectionFacade {
80     
81     /** after this time, rpc future response objects will be thrown away (in minutes) */
82     public static final int RPC_RESPONSE_EXPIRATION = 1;
83
84     protected static final Logger LOG = LoggerFactory
85             .getLogger(ConnectionAdapterImpl.class);
86     
87     private static final String APPLICATION_TAG = "OPENFLOW_LIBRARY";
88     private static final String TAG = "OPENFLOW";
89     private Channel channel;
90     private OpenflowProtocolListener messageListener;
91     /** expiring cache for future rpcResponses */
92     protected Cache<RpcResponseKey, SettableFuture<?>> responseCache;
93     private SystemNotificationsListener systemListener;
94     private boolean disconnectOccured = false;
95
96     protected ConnectionReadyListener connectionReadyListener;
97
98     /**
99      * default ctor 
100      */
101     public ConnectionAdapterImpl() {
102         responseCache = CacheBuilder.newBuilder()
103                 .concurrencyLevel(1)
104                 .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
105                 .removalListener(new ResponseRemovalListener()).build();
106         LOG.debug("ConnectionAdapter created");
107     }
108     
109     /**
110      * @param channel the channel to be set - used for communication
111      */
112     public void setChannel(Channel channel) {
113         this.channel = channel;
114     }
115
116     @Override
117     public Future<RpcResult<BarrierOutput>> barrier(BarrierInput input) {
118         return sendToSwitchExpectRpcResultFuture(
119                 input, BarrierOutput.class, "barrier-input sending failed");
120     }
121
122     @Override
123     public Future<RpcResult<EchoOutput>> echo(EchoInput input) {
124         return sendToSwitchExpectRpcResultFuture(
125                 input, EchoOutput.class, "echo-input sending failed");
126     }
127
128     @Override
129     public Future<RpcResult<Void>> echoReply(EchoReplyInput input) {
130         return sendToSwitchFuture(input, "echo-reply sending failed");
131     }
132
133     @Override
134     public Future<RpcResult<Void>> experimenter(ExperimenterInput input) {
135         return sendToSwitchFuture(input, "experimenter sending failed");
136     }
137
138     @Override
139     public Future<RpcResult<Void>> flowMod(FlowModInput input) {
140         return sendToSwitchFuture(input, "flow-mod sending failed");
141     }
142
143     @Override
144     public Future<RpcResult<GetConfigOutput>> getConfig(GetConfigInput input) {
145         return sendToSwitchExpectRpcResultFuture(
146                 input, GetConfigOutput.class, "get-config-input sending failed");
147     }
148
149     @Override
150     public Future<RpcResult<GetFeaturesOutput>> getFeatures(
151             GetFeaturesInput input) {
152         return sendToSwitchExpectRpcResultFuture(
153                 input, GetFeaturesOutput.class, "get-features-input sending failed");
154     }
155
156     @Override
157     public Future<RpcResult<GetQueueConfigOutput>> getQueueConfig(
158             GetQueueConfigInput input) {
159         return sendToSwitchExpectRpcResultFuture(
160                 input, GetQueueConfigOutput.class, "get-queue-config-input sending failed");
161     }
162
163     @Override
164     public Future<RpcResult<Void>> groupMod(GroupModInput input) {
165         return sendToSwitchFuture(input, "group-mod-input sending failed");
166     }
167
168     @Override
169     public Future<RpcResult<Void>> hello(HelloInput input) {
170         return sendToSwitchFuture(input, "hello-input sending failed");
171     }
172
173     @Override
174     public Future<RpcResult<Void>> meterMod(MeterModInput input) {
175         return sendToSwitchFuture(input, "meter-mod-input sending failed");
176     }
177
178     @Override
179     public Future<RpcResult<Void>> packetOut(PacketOutInput input) {
180         return sendToSwitchFuture(input, "packet-out-input sending failed");
181     }
182
183     @Override
184     public Future<RpcResult<Void>> portMod(PortModInput input) {
185         return sendToSwitchFuture(input, "port-mod-input sending failed");
186     }
187
188     @Override
189     public Future<RpcResult<RoleRequestOutput>> roleRequest(
190             RoleRequestInput input) {
191         return sendToSwitchExpectRpcResultFuture(
192                 input, RoleRequestOutput.class, "role-request-config-input sending failed");
193     }
194
195     @Override
196     public Future<RpcResult<Void>> setConfig(SetConfigInput input) {
197         return sendToSwitchFuture(input, "set-config-input sending failed");
198     }
199
200     @Override
201     public Future<RpcResult<Void>> tableMod(TableModInput input) {
202         return sendToSwitchFuture(input, "table-mod-input sending failed");
203     }
204
205     @Override
206     public Future<RpcResult<GetAsyncOutput>> getAsync(GetAsyncInput input) {
207         return sendToSwitchExpectRpcResultFuture(
208                 input, GetAsyncOutput.class, "get-async-input sending failed");
209     }
210
211     @Override
212     public Future<RpcResult<Void>> setAsync(SetAsyncInput input) {
213         return sendToSwitchFuture(input, "set-async-input sending failed");
214     }
215
216     @Override
217     public Future<Boolean> disconnect() {
218         ChannelFuture disconnectResult = channel.disconnect();
219         responseCache.invalidateAll();
220         disconnectOccured = true;
221
222         String failureInfo = "switch disconnecting failed";
223         ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
224         String message = "Check the switch connection";
225         return handleTransportChannelFuture(disconnectResult, failureInfo, errorSeverity, message);
226     }
227
228     @Override
229     public boolean isAlive() {
230         return channel.isOpen();
231     }
232
233     @Override
234     public void setMessageListener(OpenflowProtocolListener messageListener) {
235         this.messageListener = messageListener;
236     }
237     
238     @Override
239     public void consume(DataObject message) {
240         LOG.debug("ConsumeIntern msg");
241         if (disconnectOccured ) {
242             return;
243         }
244         if (message instanceof Notification) {
245             // System events
246             if (message instanceof DisconnectEvent) {
247                 systemListener.onDisconnectEvent((DisconnectEvent) message);
248                 responseCache.invalidateAll();
249                 disconnectOccured = true;
250             } else if (message instanceof SwitchIdleEvent) {
251                 systemListener.onSwitchIdleEvent((SwitchIdleEvent) message);
252             }
253             // OpenFlow messages
254               else if (message instanceof EchoRequestMessage) {
255                 messageListener.onEchoRequestMessage((EchoRequestMessage) message);
256             } else if (message instanceof ErrorMessage) {
257                 messageListener.onErrorMessage((ErrorMessage) message);
258             } else if (message instanceof ExperimenterMessage) {
259                 messageListener.onExperimenterMessage((ExperimenterMessage) message);
260             } else if (message instanceof FlowRemovedMessage) {
261                 messageListener.onFlowRemovedMessage((FlowRemovedMessage) message);
262             } else if (message instanceof HelloMessage) {
263                 LOG.info("Hello received / branch");
264                 messageListener.onHelloMessage((HelloMessage) message);
265             } else if (message instanceof MultipartReplyMessage) {
266                 messageListener.onMultipartReplyMessage((MultipartReplyMessage) message);
267             } else if (message instanceof MultipartRequestMessage) {
268                 messageListener.onMultipartRequestMessage((MultipartRequestMessage) message);
269             } else if (message instanceof PacketInMessage) {
270                 messageListener.onPacketInMessage((PacketInMessage) message);
271             } else if (message instanceof PortStatusMessage) {
272                 messageListener.onPortStatusMessage((PortStatusMessage) message);
273             } else {
274                 LOG.warn("message listening not supported for type: "+message.getClass());
275             }
276         } else {
277             if (message instanceof OfHeader) {
278                 LOG.debug("OFheader msg received");
279                 RpcResponseKey key = createRpcResponseKey((OfHeader) message);
280                 final SettableFuture<RpcResult<?>> rpcFuture = findRpcResponse(key);
281                 if (rpcFuture != null) {
282                     LOG.debug("corresponding rpcFuture found");
283                     List<RpcError> errors = Collections.emptyList();
284                     LOG.debug("before setting rpcFuture");
285                     rpcFuture.set(Rpcs.getRpcResult(true, message, errors));
286                     LOG.debug("after setting rpcFuture");
287                     responseCache.invalidate(key);
288                 } else {
289                     LOG.warn("received unexpected rpc response: "+key);
290                 }
291             } else {
292                 LOG.warn("message listening not supported for type: "+message.getClass());
293             }
294         }
295     }
296
297     /**
298      * sends given message to switch, sending result will be reported via return value
299      * @param input message to send
300      * @param failureInfo describes, what type of message caused failure by sending 
301      * @return future object, <ul>
302      *  <li>if send successful, {@link RpcResult} without errors and successful 
303      *  status will be returned, </li>
304      *  <li>else {@link RpcResult} will contain errors and failed status</li>
305      *  </ul>    
306      */
307     private SettableFuture<RpcResult<Void>> sendToSwitchFuture(
308             DataObject input, final String failureInfo) {
309         LOG.debug("going to flush");
310         ChannelFuture resultFuture = channel.writeAndFlush(input);
311         LOG.debug("flushed");
312         
313         ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
314         String errorMessage = "check switch connection";
315         return handleRpcChannelFuture(resultFuture, failureInfo, errorSeverity, errorMessage);
316     }
317     
318     /**
319      * sends given message to switch, sending result or switch response will be reported via return value
320      * @param input message to send
321      * @param responseClazz type of response
322      * @param failureInfo describes, what type of message caused failure by sending 
323      * @return future object, <ul>
324      *  <li>if send fails, {@link RpcResult} will contain errors and failed status </li>
325      *  <li>else {@link RpcResult} will be stored in responseCache and wait for particular timeout 
326      *  ({@link ConnectionAdapterImpl#RPC_RESPONSE_EXPIRATION}), 
327      *  <ul><li>either switch will manage to answer
328      *  and then corresponding response message will be set into returned future</li>
329      *  <li>or response in cache will expire and returned future will be cancelled</li></ul>
330      *  </li>
331      *  </ul>     
332      */
333     private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> sendToSwitchExpectRpcResultFuture(
334             IN input, Class<OUT> responseClazz, final String failureInfo) {
335         LOG.debug("going to flush");
336         SettableFuture<RpcResult<OUT>> rpcResult = SettableFuture.create();
337         RpcResponseKey key = new RpcResponseKey(input.getXid(), responseClazz);
338         responseCache.put(key, rpcResult);
339         ChannelFuture resultFuture = channel.writeAndFlush(input);
340         LOG.debug("flushed");
341         
342         ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
343         String errorMessage = "check switch connection";
344         
345         return handleRpcChannelFutureWithResponse(resultFuture, failureInfo, errorSeverity, 
346                 errorMessage, input, responseClazz, rpcResult, key);
347     }
348
349     /**
350      * @param resultFuture
351      * @param failureInfo
352      * @return
353      */
354     private SettableFuture<RpcResult<Void>> handleRpcChannelFuture(
355             ChannelFuture resultFuture, final String failureInfo, 
356             final ErrorSeverity errorSeverity, final String errorMessage) {
357         
358         final SettableFuture<RpcResult<Void>> rpcResult = SettableFuture.create();
359         LOG.debug("handlerpcchannelfuture");
360         resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
361             
362             @Override
363             public void operationComplete(
364                     io.netty.util.concurrent.Future<? super Void> future)
365                     throws Exception {
366                 LOG.debug("operation complete");
367                 Collection<RpcError> errors = Collections.emptyList();
368                 
369                 if (future.cause() != null) {
370                     LOG.debug("future.cause != null");
371                     RpcError rpcError = buildRpcError(failureInfo, 
372                             errorSeverity, errorMessage, future.cause());
373                     errors = Lists.newArrayList(rpcError);
374                 }
375                 
376                 rpcResult.set(Rpcs.getRpcResult(
377                         future.isSuccess(), 
378                         (Void) null, 
379                         errors)
380                 );
381             }
382         });
383         return rpcResult;
384     }
385     
386     /**
387      * @param resultFuture
388      * @param failureInfo
389      * @param errorSeverity
390      * @param errorMessage
391      * @param input
392      * @param responseClazz
393      * @param key of rpcResponse
394      * @return
395      */
396     private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> handleRpcChannelFutureWithResponse(
397             ChannelFuture resultFuture, final String failureInfo,
398             final ErrorSeverity errorSeverity, final String errorMessage,
399             final IN input, Class<OUT> responseClazz, final SettableFuture<RpcResult<OUT>> rpcResult, final RpcResponseKey key) {
400         LOG.debug("handleRpcchanfuture with response");
401         
402         resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
403             
404             @Override
405             public void operationComplete(
406                     io.netty.util.concurrent.Future<? super Void> future)
407                     throws Exception {
408                 
409                 LOG.debug("operation complete");
410                 Collection<RpcError> errors = Collections.emptyList();
411                 if (future.cause() != null) {
412                     LOG.debug("ChannelFuture.cause != null");
413                     RpcError rpcError = buildRpcError(failureInfo, 
414                             errorSeverity, errorMessage, future.cause());
415                     errors = Lists.newArrayList(rpcError);
416                     rpcResult.set(Rpcs.getRpcResult(
417                             future.isSuccess(), 
418                             (OUT) null, 
419                             errors)
420                             );
421                     responseCache.invalidate(key);
422                 } else {
423                     LOG.debug("ChannelFuture.cause == null");
424                     if (responseCache.getIfPresent(key) == null) {
425                         LOG.debug("responcache: key wasn't present");
426                     }
427                 }
428             }
429         });
430         return rpcResult;
431     }
432
433     /**
434      * @param resultFuture
435      * @param failureInfo
436      * @param errorSeverity 
437      * @param message 
438      * @return
439      */
440     private static SettableFuture<Boolean> handleTransportChannelFuture(
441             ChannelFuture resultFuture, final String failureInfo, 
442             final ErrorSeverity errorSeverity, final String message) {
443         
444         final SettableFuture<Boolean> transportResult = SettableFuture.create();
445         
446         resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
447             
448             @Override
449             public void operationComplete(
450                     io.netty.util.concurrent.Future<? super Void> future)
451                     throws Exception {
452                 transportResult.set(future.isSuccess());
453                 if (!future.isSuccess()) {
454                     transportResult.setException(future.cause());
455                 }
456             }
457         });
458         return transportResult;
459     }
460
461     /**
462      * @param cause
463      * @return
464      */
465     protected RpcError buildRpcError(String info, ErrorSeverity severity, String message, 
466             Throwable cause) {
467         RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message, 
468                 ErrorType.RPC, cause);
469         return error;
470     }
471     
472     /**
473      * @param cause
474      * @return
475      */
476     protected RpcError buildTransportError(String info, ErrorSeverity severity, String message, 
477             Throwable cause) {
478         RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message, 
479                 ErrorType.TRANSPORT, cause);
480         return error;
481     }
482
483     /**
484      * @param message
485      * @return
486      */
487     private static RpcResponseKey createRpcResponseKey(OfHeader message) {
488         return new RpcResponseKey(message.getXid(), message.getClass());
489     }
490
491     /**
492      * @return
493      */
494     @SuppressWarnings("unchecked")
495     private SettableFuture<RpcResult<?>> findRpcResponse(RpcResponseKey key) {
496         return (SettableFuture<RpcResult<?>>) responseCache.getIfPresent(key);
497     }
498
499     @Override
500     public void setSystemListener(SystemNotificationsListener systemListener) {
501         this.systemListener = systemListener;
502     }
503     
504     @Override
505     public void checkListeners() {
506         StringBuffer buffer =  new StringBuffer();
507         if (systemListener == null) {
508             buffer.append("SystemListener ");
509         }
510         if (messageListener == null) {
511             buffer.append("MessageListener ");
512         }
513         if (connectionReadyListener == null) {
514             buffer.append("ConnectionReadyListener ");
515         }
516         
517         if (buffer.length() > 0) {
518             throw new IllegalStateException("Missing listeners: " + buffer.toString());
519         }
520     }
521
522     static class ResponseRemovalListener implements RemovalListener<RpcResponseKey, SettableFuture<?>> {
523         @Override
524         public void onRemoval(
525                 RemovalNotification<RpcResponseKey, SettableFuture<?>> notification) {
526             SettableFuture<?> future = notification.getValue();
527             if (!future.isDone()) {
528                 LOG.warn("rpc response discarded: " + notification.getKey());
529                 future.cancel(true);
530             }
531         }
532     }
533
534     /**
535      * Class is used ONLY for exiting msgQueue processing thread
536      * @author michal.polkorab
537      */
538     static class ExitingDataObject implements DataObject {
539         @Override
540         public Class<? extends DataContainer> getImplementedInterface() {
541             return null;
542         }
543     }
544     
545     @Override
546     public void fireConnectionReadyNotification() {
547         new Thread(new Runnable() {
548             @Override
549             public void run() {
550                 connectionReadyListener.onConnectionReady();
551             }
552         }).start();
553     }
554     
555     
556     @Override
557     public void setConnectionReadyListener(
558             ConnectionReadyListener connectionReadyListener) {
559         this.connectionReadyListener = connectionReadyListener;
560     }
561     
562 }