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